security/nss/lib/sqlite/sqlite3.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /******************************************************************************
     2 ** This file is an amalgamation of many separate C source files from SQLite
     3 ** version 3.7.15.  By combining all the individual C code files into this 
     4 ** single large file, the entire code can be compiled as a single translation
     5 ** unit.  This allows many compilers to do optimizations that would not be
     6 ** possible if the files were compiled separately.  Performance improvements
     7 ** of 5% or more are commonly seen when SQLite is compiled as a single
     8 ** translation unit.
     9 **
    10 ** This file is all you need to compile SQLite.  To use SQLite in other
    11 ** programs, you need this file and the "sqlite3.h" header file that defines
    12 ** the programming interface to the SQLite library.  (If you do not have 
    13 ** the "sqlite3.h" header file at hand, you will find a copy embedded within
    14 ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
    15 ** of the embedded sqlite3.h header file.) Additional code files may be needed
    16 ** if you want a wrapper to interface SQLite with your choice of programming
    17 ** language. The code for the "sqlite3" command-line shell is also in a
    18 ** separate file. This file contains only code for the core SQLite library.
    19 */
    20 #define SQLITE_CORE 1
    21 #define SQLITE_AMALGAMATION 1
    22 #ifndef SQLITE_PRIVATE
    23 # define SQLITE_PRIVATE static
    24 #endif
    25 #ifndef SQLITE_API
    26 # define SQLITE_API
    27 #endif
    28 /************** Begin file sqliteInt.h ***************************************/
    29 /*
    30 ** 2001 September 15
    31 **
    32 ** The author disclaims copyright to this source code.  In place of
    33 ** a legal notice, here is a blessing:
    34 **
    35 **    May you do good and not evil.
    36 **    May you find forgiveness for yourself and forgive others.
    37 **    May you share freely, never taking more than you give.
    38 **
    39 *************************************************************************
    40 ** Internal interface definitions for SQLite.
    41 **
    42 */
    43 #ifndef _SQLITEINT_H_
    44 #define _SQLITEINT_H_
    46 /*
    47 ** These #defines should enable >2GB file support on POSIX if the
    48 ** underlying operating system supports it.  If the OS lacks
    49 ** large file support, or if the OS is windows, these should be no-ops.
    50 **
    51 ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
    52 ** system #includes.  Hence, this block of code must be the very first
    53 ** code in all source files.
    54 **
    55 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    56 ** on the compiler command line.  This is necessary if you are compiling
    57 ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
    58 ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
    59 ** without this option, LFS is enable.  But LFS does not exist in the kernel
    60 ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
    61 ** portability you should omit LFS.
    62 **
    63 ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
    64 */
    65 #ifndef SQLITE_DISABLE_LFS
    66 # define _LARGE_FILE       1
    67 # ifndef _FILE_OFFSET_BITS
    68 #   define _FILE_OFFSET_BITS 64
    69 # endif
    70 # define _LARGEFILE_SOURCE 1
    71 #endif
    73 /*
    74 ** Include the configuration header output by 'configure' if we're using the
    75 ** autoconf-based build
    76 */
    77 #ifdef _HAVE_SQLITE_CONFIG_H
    78 #include "config.h"
    79 #endif
    81 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
    82 /************** Begin file sqliteLimit.h *************************************/
    83 /*
    84 ** 2007 May 7
    85 **
    86 ** The author disclaims copyright to this source code.  In place of
    87 ** a legal notice, here is a blessing:
    88 **
    89 **    May you do good and not evil.
    90 **    May you find forgiveness for yourself and forgive others.
    91 **    May you share freely, never taking more than you give.
    92 **
    93 *************************************************************************
    94 ** 
    95 ** This file defines various limits of what SQLite can process.
    96 */
    98 /*
    99 ** The maximum length of a TEXT or BLOB in bytes.   This also
   100 ** limits the size of a row in a table or index.
   101 **
   102 ** The hard limit is the ability of a 32-bit signed integer
   103 ** to count the size: 2^31-1 or 2147483647.
   104 */
   105 #ifndef SQLITE_MAX_LENGTH
   106 # define SQLITE_MAX_LENGTH 1000000000
   107 #endif
   109 /*
   110 ** This is the maximum number of
   111 **
   112 **    * Columns in a table
   113 **    * Columns in an index
   114 **    * Columns in a view
   115 **    * Terms in the SET clause of an UPDATE statement
   116 **    * Terms in the result set of a SELECT statement
   117 **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
   118 **    * Terms in the VALUES clause of an INSERT statement
   119 **
   120 ** The hard upper limit here is 32676.  Most database people will
   121 ** tell you that in a well-normalized database, you usually should
   122 ** not have more than a dozen or so columns in any table.  And if
   123 ** that is the case, there is no point in having more than a few
   124 ** dozen values in any of the other situations described above.
   125 */
   126 #ifndef SQLITE_MAX_COLUMN
   127 # define SQLITE_MAX_COLUMN 2000
   128 #endif
   130 /*
   131 ** The maximum length of a single SQL statement in bytes.
   132 **
   133 ** It used to be the case that setting this value to zero would
   134 ** turn the limit off.  That is no longer true.  It is not possible
   135 ** to turn this limit off.
   136 */
   137 #ifndef SQLITE_MAX_SQL_LENGTH
   138 # define SQLITE_MAX_SQL_LENGTH 1000000000
   139 #endif
   141 /*
   142 ** The maximum depth of an expression tree. This is limited to 
   143 ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might 
   144 ** want to place more severe limits on the complexity of an 
   145 ** expression.
   146 **
   147 ** A value of 0 used to mean that the limit was not enforced.
   148 ** But that is no longer true.  The limit is now strictly enforced
   149 ** at all times.
   150 */
   151 #ifndef SQLITE_MAX_EXPR_DEPTH
   152 # define SQLITE_MAX_EXPR_DEPTH 1000
   153 #endif
   155 /*
   156 ** The maximum number of terms in a compound SELECT statement.
   157 ** The code generator for compound SELECT statements does one
   158 ** level of recursion for each term.  A stack overflow can result
   159 ** if the number of terms is too large.  In practice, most SQL
   160 ** never has more than 3 or 4 terms.  Use a value of 0 to disable
   161 ** any limit on the number of terms in a compount SELECT.
   162 */
   163 #ifndef SQLITE_MAX_COMPOUND_SELECT
   164 # define SQLITE_MAX_COMPOUND_SELECT 500
   165 #endif
   167 /*
   168 ** The maximum number of opcodes in a VDBE program.
   169 ** Not currently enforced.
   170 */
   171 #ifndef SQLITE_MAX_VDBE_OP
   172 # define SQLITE_MAX_VDBE_OP 25000
   173 #endif
   175 /*
   176 ** The maximum number of arguments to an SQL function.
   177 */
   178 #ifndef SQLITE_MAX_FUNCTION_ARG
   179 # define SQLITE_MAX_FUNCTION_ARG 127
   180 #endif
   182 /*
   183 ** The maximum number of in-memory pages to use for the main database
   184 ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
   185 */
   186 #ifndef SQLITE_DEFAULT_CACHE_SIZE
   187 # define SQLITE_DEFAULT_CACHE_SIZE  2000
   188 #endif
   189 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
   190 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
   191 #endif
   193 /*
   194 ** The default number of frames to accumulate in the log file before
   195 ** checkpointing the database in WAL mode.
   196 */
   197 #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
   198 # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
   199 #endif
   201 /*
   202 ** The maximum number of attached databases.  This must be between 0
   203 ** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
   204 ** is used internally to track attached databases.
   205 */
   206 #ifndef SQLITE_MAX_ATTACHED
   207 # define SQLITE_MAX_ATTACHED 10
   208 #endif
   211 /*
   212 ** The maximum value of a ?nnn wildcard that the parser will accept.
   213 */
   214 #ifndef SQLITE_MAX_VARIABLE_NUMBER
   215 # define SQLITE_MAX_VARIABLE_NUMBER 999
   216 #endif
   218 /* Maximum page size.  The upper bound on this value is 65536.  This a limit
   219 ** imposed by the use of 16-bit offsets within each page.
   220 **
   221 ** Earlier versions of SQLite allowed the user to change this value at
   222 ** compile time. This is no longer permitted, on the grounds that it creates
   223 ** a library that is technically incompatible with an SQLite library 
   224 ** compiled with a different limit. If a process operating on a database 
   225 ** with a page-size of 65536 bytes crashes, then an instance of SQLite 
   226 ** compiled with the default page-size limit will not be able to rollback 
   227 ** the aborted transaction. This could lead to database corruption.
   228 */
   229 #ifdef SQLITE_MAX_PAGE_SIZE
   230 # undef SQLITE_MAX_PAGE_SIZE
   231 #endif
   232 #define SQLITE_MAX_PAGE_SIZE 65536
   235 /*
   236 ** The default size of a database page.
   237 */
   238 #ifndef SQLITE_DEFAULT_PAGE_SIZE
   239 # define SQLITE_DEFAULT_PAGE_SIZE 1024
   240 #endif
   241 #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
   242 # undef SQLITE_DEFAULT_PAGE_SIZE
   243 # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
   244 #endif
   246 /*
   247 ** Ordinarily, if no value is explicitly provided, SQLite creates databases
   248 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
   249 ** device characteristics (sector-size and atomic write() support),
   250 ** SQLite may choose a larger value. This constant is the maximum value
   251 ** SQLite will choose on its own.
   252 */
   253 #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
   254 # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
   255 #endif
   256 #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
   257 # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
   258 # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
   259 #endif
   262 /*
   263 ** Maximum number of pages in one database file.
   264 **
   265 ** This is really just the default value for the max_page_count pragma.
   266 ** This value can be lowered (or raised) at run-time using that the
   267 ** max_page_count macro.
   268 */
   269 #ifndef SQLITE_MAX_PAGE_COUNT
   270 # define SQLITE_MAX_PAGE_COUNT 1073741823
   271 #endif
   273 /*
   274 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
   275 ** operator.
   276 */
   277 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
   278 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
   279 #endif
   281 /*
   282 ** Maximum depth of recursion for triggers.
   283 **
   284 ** A value of 1 means that a trigger program will not be able to itself
   285 ** fire any triggers. A value of 0 means that no trigger programs at all 
   286 ** may be executed.
   287 */
   288 #ifndef SQLITE_MAX_TRIGGER_DEPTH
   289 # define SQLITE_MAX_TRIGGER_DEPTH 1000
   290 #endif
   292 /************** End of sqliteLimit.h *****************************************/
   293 /************** Continuing where we left off in sqliteInt.h ******************/
   295 /* Disable nuisance warnings on Borland compilers */
   296 #if defined(__BORLANDC__)
   297 #pragma warn -rch /* unreachable code */
   298 #pragma warn -ccc /* Condition is always true or false */
   299 #pragma warn -aus /* Assigned value is never used */
   300 #pragma warn -csu /* Comparing signed and unsigned */
   301 #pragma warn -spa /* Suspicious pointer arithmetic */
   302 #endif
   304 /* Needed for various definitions... */
   305 #ifndef _GNU_SOURCE
   306 # define _GNU_SOURCE
   307 #endif
   309 /*
   310 ** Include standard header files as necessary
   311 */
   312 #ifdef HAVE_STDINT_H
   313 #include <stdint.h>
   314 #endif
   315 #ifdef HAVE_INTTYPES_H
   316 #include <inttypes.h>
   317 #endif
   319 /*
   320 ** The following macros are used to cast pointers to integers and
   321 ** integers to pointers.  The way you do this varies from one compiler
   322 ** to the next, so we have developed the following set of #if statements
   323 ** to generate appropriate macros for a wide range of compilers.
   324 **
   325 ** The correct "ANSI" way to do this is to use the intptr_t type. 
   326 ** Unfortunately, that typedef is not available on all compilers, or
   327 ** if it is available, it requires an #include of specific headers
   328 ** that vary from one machine to the next.
   329 **
   330 ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
   331 ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
   332 ** So we have to define the macros in different ways depending on the
   333 ** compiler.
   334 */
   335 #if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
   336 # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
   337 # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
   338 #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
   339 # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
   340 # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
   341 #elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
   342 # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
   343 # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
   344 #else                          /* Generates a warning - but it always works */
   345 # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
   346 # define SQLITE_PTR_TO_INT(X)  ((int)(X))
   347 #endif
   349 /*
   350 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
   351 ** 0 means mutexes are permanently disable and the library is never
   352 ** threadsafe.  1 means the library is serialized which is the highest
   353 ** level of threadsafety.  2 means the libary is multithreaded - multiple
   354 ** threads can use SQLite as long as no two threads try to use the same
   355 ** database connection at the same time.
   356 **
   357 ** Older versions of SQLite used an optional THREADSAFE macro.
   358 ** We support that for legacy.
   359 */
   360 #if !defined(SQLITE_THREADSAFE)
   361 #if defined(THREADSAFE)
   362 # define SQLITE_THREADSAFE THREADSAFE
   363 #else
   364 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
   365 #endif
   366 #endif
   368 /*
   369 ** Powersafe overwrite is on by default.  But can be turned off using
   370 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
   371 */
   372 #ifndef SQLITE_POWERSAFE_OVERWRITE
   373 # define SQLITE_POWERSAFE_OVERWRITE 1
   374 #endif
   376 /*
   377 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
   378 ** It determines whether or not the features related to 
   379 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
   380 ** be overridden at runtime using the sqlite3_config() API.
   381 */
   382 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
   383 # define SQLITE_DEFAULT_MEMSTATUS 1
   384 #endif
   386 /*
   387 ** Exactly one of the following macros must be defined in order to
   388 ** specify which memory allocation subsystem to use.
   389 **
   390 **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
   391 **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
   392 **     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
   393 **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
   394 **
   395 ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
   396 ** assert() macro is enabled, each call into the Win32 native heap subsystem
   397 ** will cause HeapValidate to be called.  If heap validation should fail, an
   398 ** assertion will be triggered.
   399 **
   400 ** (Historical note:  There used to be several other options, but we've
   401 ** pared it down to just these three.)
   402 **
   403 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
   404 ** the default.
   405 */
   406 #if defined(SQLITE_SYSTEM_MALLOC) \
   407   + defined(SQLITE_WIN32_MALLOC) \
   408   + defined(SQLITE_ZERO_MALLOC) \
   409   + defined(SQLITE_MEMDEBUG)>1
   410 # error "Two or more of the following compile-time configuration options\
   411  are defined but at most one is allowed:\
   412  SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
   413  SQLITE_ZERO_MALLOC"
   414 #endif
   415 #if defined(SQLITE_SYSTEM_MALLOC) \
   416   + defined(SQLITE_WIN32_MALLOC) \
   417   + defined(SQLITE_ZERO_MALLOC) \
   418   + defined(SQLITE_MEMDEBUG)==0
   419 # define SQLITE_SYSTEM_MALLOC 1
   420 #endif
   422 /*
   423 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
   424 ** sizes of memory allocations below this value where possible.
   425 */
   426 #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
   427 # define SQLITE_MALLOC_SOFT_LIMIT 1024
   428 #endif
   430 /*
   431 ** We need to define _XOPEN_SOURCE as follows in order to enable
   432 ** recursive mutexes on most Unix systems.  But Mac OS X is different.
   433 ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
   434 ** so it is omitted there.  See ticket #2673.
   435 **
   436 ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
   437 ** implemented on some systems.  So we avoid defining it at all
   438 ** if it is already defined or if it is unneeded because we are
   439 ** not doing a threadsafe build.  Ticket #2681.
   440 **
   441 ** See also ticket #2741.
   442 */
   443 #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
   444 #  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
   445 #endif
   447 /*
   448 ** The TCL headers are only needed when compiling the TCL bindings.
   449 */
   450 #if defined(SQLITE_TCL) || defined(TCLSH)
   451 # include <tcl.h>
   452 #endif
   454 /*
   455 ** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
   456 ** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
   457 ** make it true by defining or undefining NDEBUG.
   458 **
   459 ** Setting NDEBUG makes the code smaller and run faster by disabling the
   460 ** number assert() statements in the code.  So we want the default action
   461 ** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
   462 ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
   463 ** feature.
   464 */
   465 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
   466 # define NDEBUG 1
   467 #endif
   468 #if defined(NDEBUG) && defined(SQLITE_DEBUG)
   469 # undef NDEBUG
   470 #endif
   472 /*
   473 ** The testcase() macro is used to aid in coverage testing.  When 
   474 ** doing coverage testing, the condition inside the argument to
   475 ** testcase() must be evaluated both true and false in order to
   476 ** get full branch coverage.  The testcase() macro is inserted
   477 ** to help ensure adequate test coverage in places where simple
   478 ** condition/decision coverage is inadequate.  For example, testcase()
   479 ** can be used to make sure boundary values are tested.  For
   480 ** bitmask tests, testcase() can be used to make sure each bit
   481 ** is significant and used at least once.  On switch statements
   482 ** where multiple cases go to the same block of code, testcase()
   483 ** can insure that all cases are evaluated.
   484 **
   485 */
   486 #ifdef SQLITE_COVERAGE_TEST
   487 SQLITE_PRIVATE   void sqlite3Coverage(int);
   488 # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
   489 #else
   490 # define testcase(X)
   491 #endif
   493 /*
   494 ** The TESTONLY macro is used to enclose variable declarations or
   495 ** other bits of code that are needed to support the arguments
   496 ** within testcase() and assert() macros.
   497 */
   498 #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
   499 # define TESTONLY(X)  X
   500 #else
   501 # define TESTONLY(X)
   502 #endif
   504 /*
   505 ** Sometimes we need a small amount of code such as a variable initialization
   506 ** to setup for a later assert() statement.  We do not want this code to
   507 ** appear when assert() is disabled.  The following macro is therefore
   508 ** used to contain that setup code.  The "VVA" acronym stands for
   509 ** "Verification, Validation, and Accreditation".  In other words, the
   510 ** code within VVA_ONLY() will only run during verification processes.
   511 */
   512 #ifndef NDEBUG
   513 # define VVA_ONLY(X)  X
   514 #else
   515 # define VVA_ONLY(X)
   516 #endif
   518 /*
   519 ** The ALWAYS and NEVER macros surround boolean expressions which 
   520 ** are intended to always be true or false, respectively.  Such
   521 ** expressions could be omitted from the code completely.  But they
   522 ** are included in a few cases in order to enhance the resilience
   523 ** of SQLite to unexpected behavior - to make the code "self-healing"
   524 ** or "ductile" rather than being "brittle" and crashing at the first
   525 ** hint of unplanned behavior.
   526 **
   527 ** In other words, ALWAYS and NEVER are added for defensive code.
   528 **
   529 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
   530 ** be true and false so that the unreachable code then specify will
   531 ** not be counted as untested code.
   532 */
   533 #if defined(SQLITE_COVERAGE_TEST)
   534 # define ALWAYS(X)      (1)
   535 # define NEVER(X)       (0)
   536 #elif !defined(NDEBUG)
   537 # define ALWAYS(X)      ((X)?1:(assert(0),0))
   538 # define NEVER(X)       ((X)?(assert(0),1):0)
   539 #else
   540 # define ALWAYS(X)      (X)
   541 # define NEVER(X)       (X)
   542 #endif
   544 /*
   545 ** Return true (non-zero) if the input is a integer that is too large
   546 ** to fit in 32-bits.  This macro is used inside of various testcase()
   547 ** macros to verify that we have tested SQLite for large-file support.
   548 */
   549 #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
   551 /*
   552 ** The macro unlikely() is a hint that surrounds a boolean
   553 ** expression that is usually false.  Macro likely() surrounds
   554 ** a boolean expression that is usually true.  GCC is able to
   555 ** use these hints to generate better code, sometimes.
   556 */
   557 #if defined(__GNUC__) && 0
   558 # define likely(X)    __builtin_expect((X),1)
   559 # define unlikely(X)  __builtin_expect((X),0)
   560 #else
   561 # define likely(X)    !!(X)
   562 # define unlikely(X)  !!(X)
   563 #endif
   565 /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
   566 /************** Begin file sqlite3.h *****************************************/
   567 /*
   568 ** 2001 September 15
   569 **
   570 ** The author disclaims copyright to this source code.  In place of
   571 ** a legal notice, here is a blessing:
   572 **
   573 **    May you do good and not evil.
   574 **    May you find forgiveness for yourself and forgive others.
   575 **    May you share freely, never taking more than you give.
   576 **
   577 *************************************************************************
   578 ** This header file defines the interface that the SQLite library
   579 ** presents to client programs.  If a C-function, structure, datatype,
   580 ** or constant definition does not appear in this file, then it is
   581 ** not a published API of SQLite, is subject to change without
   582 ** notice, and should not be referenced by programs that use SQLite.
   583 **
   584 ** Some of the definitions that are in this file are marked as
   585 ** "experimental".  Experimental interfaces are normally new
   586 ** features recently added to SQLite.  We do not anticipate changes
   587 ** to experimental interfaces but reserve the right to make minor changes
   588 ** if experience from use "in the wild" suggest such changes are prudent.
   589 **
   590 ** The official C-language API documentation for SQLite is derived
   591 ** from comments in this file.  This file is the authoritative source
   592 ** on how SQLite interfaces are suppose to operate.
   593 **
   594 ** The name of this file under configuration management is "sqlite.h.in".
   595 ** The makefile makes some minor changes to this file (such as inserting
   596 ** the version number) and changes its name to "sqlite3.h" as
   597 ** part of the build process.
   598 */
   599 #ifndef _SQLITE3_H_
   600 #define _SQLITE3_H_
   601 #include <stdarg.h>     /* Needed for the definition of va_list */
   603 /*
   604 ** Make sure we can call this stuff from C++.
   605 */
   606 #if 0
   607 extern "C" {
   608 #endif
   611 /*
   612 ** Add the ability to override 'extern'
   613 */
   614 #ifndef SQLITE_EXTERN
   615 # define SQLITE_EXTERN extern
   616 #endif
   618 #ifndef SQLITE_API
   619 # define SQLITE_API
   620 #endif
   623 /*
   624 ** These no-op macros are used in front of interfaces to mark those
   625 ** interfaces as either deprecated or experimental.  New applications
   626 ** should not use deprecated interfaces - they are support for backwards
   627 ** compatibility only.  Application writers should be aware that
   628 ** experimental interfaces are subject to change in point releases.
   629 **
   630 ** These macros used to resolve to various kinds of compiler magic that
   631 ** would generate warning messages when they were used.  But that
   632 ** compiler magic ended up generating such a flurry of bug reports
   633 ** that we have taken it all out and gone back to using simple
   634 ** noop macros.
   635 */
   636 #define SQLITE_DEPRECATED
   637 #define SQLITE_EXPERIMENTAL
   639 /*
   640 ** Ensure these symbols were not defined by some previous header file.
   641 */
   642 #ifdef SQLITE_VERSION
   643 # undef SQLITE_VERSION
   644 #endif
   645 #ifdef SQLITE_VERSION_NUMBER
   646 # undef SQLITE_VERSION_NUMBER
   647 #endif
   649 /*
   650 ** CAPI3REF: Compile-Time Library Version Numbers
   651 **
   652 ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
   653 ** evaluates to a string literal that is the SQLite version in the
   654 ** format "X.Y.Z" where X is the major version number (always 3 for
   655 ** SQLite3) and Y is the minor version number and Z is the release number.)^
   656 ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
   657 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
   658 ** numbers used in [SQLITE_VERSION].)^
   659 ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
   660 ** be larger than the release from which it is derived.  Either Y will
   661 ** be held constant and Z will be incremented or else Y will be incremented
   662 ** and Z will be reset to zero.
   663 **
   664 ** Since version 3.6.18, SQLite source code has been stored in the
   665 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
   666 ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   667 ** a string which identifies a particular check-in of SQLite
   668 ** within its configuration management system.  ^The SQLITE_SOURCE_ID
   669 ** string contains the date and time of the check-in (UTC) and an SHA1
   670 ** hash of the entire source tree.
   671 **
   672 ** See also: [sqlite3_libversion()],
   673 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   674 ** [sqlite_version()] and [sqlite_source_id()].
   675 */
   676 #define SQLITE_VERSION        "3.7.15"
   677 #define SQLITE_VERSION_NUMBER 3007015
   678 #define SQLITE_SOURCE_ID      "2012-12-12 13:36:53 cd0b37c52658bfdf992b1e3dc467bae1835a94ae"
   680 /*
   681 ** CAPI3REF: Run-Time Library Version Numbers
   682 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   683 **
   684 ** These interfaces provide the same information as the [SQLITE_VERSION],
   685 ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
   686 ** but are associated with the library instead of the header file.  ^(Cautious
   687 ** programmers might include assert() statements in their application to
   688 ** verify that values returned by these interfaces match the macros in
   689 ** the header, and thus insure that the application is
   690 ** compiled with matching library and header files.
   691 **
   692 ** <blockquote><pre>
   693 ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
   694 ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
   695 ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
   696 ** </pre></blockquote>)^
   697 **
   698 ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
   699 ** macro.  ^The sqlite3_libversion() function returns a pointer to the
   700 ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
   701 ** function is provided for use in DLLs since DLL users usually do not have
   702 ** direct access to string constants within the DLL.  ^The
   703 ** sqlite3_libversion_number() function returns an integer equal to
   704 ** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
   705 ** a pointer to a string constant whose value is the same as the 
   706 ** [SQLITE_SOURCE_ID] C preprocessor macro.
   707 **
   708 ** See also: [sqlite_version()] and [sqlite_source_id()].
   709 */
   710 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
   711 SQLITE_API const char *sqlite3_libversion(void);
   712 SQLITE_API const char *sqlite3_sourceid(void);
   713 SQLITE_API int sqlite3_libversion_number(void);
   715 /*
   716 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
   717 **
   718 ** ^The sqlite3_compileoption_used() function returns 0 or 1 
   719 ** indicating whether the specified option was defined at 
   720 ** compile time.  ^The SQLITE_ prefix may be omitted from the 
   721 ** option name passed to sqlite3_compileoption_used().  
   722 **
   723 ** ^The sqlite3_compileoption_get() function allows iterating
   724 ** over the list of options that were defined at compile time by
   725 ** returning the N-th compile time option string.  ^If N is out of range,
   726 ** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_ 
   727 ** prefix is omitted from any strings returned by 
   728 ** sqlite3_compileoption_get().
   729 **
   730 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
   731 ** and sqlite3_compileoption_get() may be omitted by specifying the 
   732 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
   733 **
   734 ** See also: SQL functions [sqlite_compileoption_used()] and
   735 ** [sqlite_compileoption_get()] and the [compile_options pragma].
   736 */
   737 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   738 SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
   739 SQLITE_API const char *sqlite3_compileoption_get(int N);
   740 #endif
   742 /*
   743 ** CAPI3REF: Test To See If The Library Is Threadsafe
   744 **
   745 ** ^The sqlite3_threadsafe() function returns zero if and only if
   746 ** SQLite was compiled with mutexing code omitted due to the
   747 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
   748 **
   749 ** SQLite can be compiled with or without mutexes.  When
   750 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
   751 ** are enabled and SQLite is threadsafe.  When the
   752 ** [SQLITE_THREADSAFE] macro is 0, 
   753 ** the mutexes are omitted.  Without the mutexes, it is not safe
   754 ** to use SQLite concurrently from more than one thread.
   755 **
   756 ** Enabling mutexes incurs a measurable performance penalty.
   757 ** So if speed is of utmost importance, it makes sense to disable
   758 ** the mutexes.  But for maximum safety, mutexes should be enabled.
   759 ** ^The default behavior is for mutexes to be enabled.
   760 **
   761 ** This interface can be used by an application to make sure that the
   762 ** version of SQLite that it is linking against was compiled with
   763 ** the desired setting of the [SQLITE_THREADSAFE] macro.
   764 **
   765 ** This interface only reports on the compile-time mutex setting
   766 ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
   767 ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
   768 ** can be fully or partially disabled using a call to [sqlite3_config()]
   769 ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
   770 ** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
   771 ** sqlite3_threadsafe() function shows only the compile-time setting of
   772 ** thread safety, not any run-time changes to that setting made by
   773 ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
   774 ** is unchanged by calls to sqlite3_config().)^
   775 **
   776 ** See the [threading mode] documentation for additional information.
   777 */
   778 SQLITE_API int sqlite3_threadsafe(void);
   780 /*
   781 ** CAPI3REF: Database Connection Handle
   782 ** KEYWORDS: {database connection} {database connections}
   783 **
   784 ** Each open SQLite database is represented by a pointer to an instance of
   785 ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
   786 ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
   787 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
   788 ** and [sqlite3_close_v2()] are its destructors.  There are many other
   789 ** interfaces (such as
   790 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
   791 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
   792 ** sqlite3 object.
   793 */
   794 typedef struct sqlite3 sqlite3;
   796 /*
   797 ** CAPI3REF: 64-Bit Integer Types
   798 ** KEYWORDS: sqlite_int64 sqlite_uint64
   799 **
   800 ** Because there is no cross-platform way to specify 64-bit integer types
   801 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
   802 **
   803 ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
   804 ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
   805 ** compatibility only.
   806 **
   807 ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
   808 ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
   809 ** sqlite3_uint64 and sqlite_uint64 types can store integer values 
   810 ** between 0 and +18446744073709551615 inclusive.
   811 */
   812 #ifdef SQLITE_INT64_TYPE
   813   typedef SQLITE_INT64_TYPE sqlite_int64;
   814   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
   815 #elif defined(_MSC_VER) || defined(__BORLANDC__)
   816   typedef __int64 sqlite_int64;
   817   typedef unsigned __int64 sqlite_uint64;
   818 #else
   819   typedef long long int sqlite_int64;
   820   typedef unsigned long long int sqlite_uint64;
   821 #endif
   822 typedef sqlite_int64 sqlite3_int64;
   823 typedef sqlite_uint64 sqlite3_uint64;
   825 /*
   826 ** If compiling for a processor that lacks floating point support,
   827 ** substitute integer for floating-point.
   828 */
   829 #ifdef SQLITE_OMIT_FLOATING_POINT
   830 # define double sqlite3_int64
   831 #endif
   833 /*
   834 ** CAPI3REF: Closing A Database Connection
   835 **
   836 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   837 ** for the [sqlite3] object.
   838 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
   839 ** the [sqlite3] object is successfully destroyed and all associated
   840 ** resources are deallocated.
   841 **
   842 ** ^If the database connection is associated with unfinalized prepared
   843 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
   844 ** will leave the database connection open and return [SQLITE_BUSY].
   845 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
   846 ** and unfinished sqlite3_backups, then the database connection becomes
   847 ** an unusable "zombie" which will automatically be deallocated when the
   848 ** last prepared statement is finalized or the last sqlite3_backup is
   849 ** finished.  The sqlite3_close_v2() interface is intended for use with
   850 ** host languages that are garbage collected, and where the order in which
   851 ** destructors are called is arbitrary.
   852 **
   853 ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
   854 ** [sqlite3_blob_close | close] all [BLOB handles], and 
   855 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
   856 ** with the [sqlite3] object prior to attempting to close the object.  ^If
   857 ** sqlite3_close() is called on a [database connection] that still has
   858 ** outstanding [prepared statements], [BLOB handles], and/or
   859 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
   860 ** of resources is deferred until all [prepared statements], [BLOB handles],
   861 ** and [sqlite3_backup] objects are also destroyed.
   862 **
   863 ** ^If an [sqlite3] object is destroyed while a transaction is open,
   864 ** the transaction is automatically rolled back.
   865 **
   866 ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
   867 ** must be either a NULL
   868 ** pointer or an [sqlite3] object pointer obtained
   869 ** from [sqlite3_open()], [sqlite3_open16()], or
   870 ** [sqlite3_open_v2()], and not previously closed.
   871 ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
   872 ** argument is a harmless no-op.
   873 */
   874 SQLITE_API int sqlite3_close(sqlite3*);
   875 SQLITE_API int sqlite3_close_v2(sqlite3*);
   877 /*
   878 ** The type for a callback function.
   879 ** This is legacy and deprecated.  It is included for historical
   880 ** compatibility and is not documented.
   881 */
   882 typedef int (*sqlite3_callback)(void*,int,char**, char**);
   884 /*
   885 ** CAPI3REF: One-Step Query Execution Interface
   886 **
   887 ** The sqlite3_exec() interface is a convenience wrapper around
   888 ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
   889 ** that allows an application to run multiple statements of SQL
   890 ** without having to use a lot of C code. 
   891 **
   892 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
   893 ** semicolon-separate SQL statements passed into its 2nd argument,
   894 ** in the context of the [database connection] passed in as its 1st
   895 ** argument.  ^If the callback function of the 3rd argument to
   896 ** sqlite3_exec() is not NULL, then it is invoked for each result row
   897 ** coming out of the evaluated SQL statements.  ^The 4th argument to
   898 ** sqlite3_exec() is relayed through to the 1st argument of each
   899 ** callback invocation.  ^If the callback pointer to sqlite3_exec()
   900 ** is NULL, then no callback is ever invoked and result rows are
   901 ** ignored.
   902 **
   903 ** ^If an error occurs while evaluating the SQL statements passed into
   904 ** sqlite3_exec(), then execution of the current statement stops and
   905 ** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
   906 ** is not NULL then any error message is written into memory obtained
   907 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
   908 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
   909 ** on error message strings returned through the 5th parameter of
   910 ** of sqlite3_exec() after the error message string is no longer needed.
   911 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
   912 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
   913 ** NULL before returning.
   914 **
   915 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
   916 ** routine returns SQLITE_ABORT without invoking the callback again and
   917 ** without running any subsequent SQL statements.
   918 **
   919 ** ^The 2nd argument to the sqlite3_exec() callback function is the
   920 ** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
   921 ** callback is an array of pointers to strings obtained as if from
   922 ** [sqlite3_column_text()], one for each column.  ^If an element of a
   923 ** result row is NULL then the corresponding string pointer for the
   924 ** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
   925 ** sqlite3_exec() callback is an array of pointers to strings where each
   926 ** entry represents the name of corresponding result column as obtained
   927 ** from [sqlite3_column_name()].
   928 **
   929 ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
   930 ** to an empty string, or a pointer that contains only whitespace and/or 
   931 ** SQL comments, then no SQL statements are evaluated and the database
   932 ** is not changed.
   933 **
   934 ** Restrictions:
   935 **
   936 ** <ul>
   937 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
   938 **      is a valid and open [database connection].
   939 ** <li> The application must not close [database connection] specified by
   940 **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
   941 ** <li> The application must not modify the SQL statement text passed into
   942 **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
   943 ** </ul>
   944 */
   945 SQLITE_API int sqlite3_exec(
   946   sqlite3*,                                  /* An open database */
   947   const char *sql,                           /* SQL to be evaluated */
   948   int (*callback)(void*,int,char**,char**),  /* Callback function */
   949   void *,                                    /* 1st argument to callback */
   950   char **errmsg                              /* Error msg written here */
   951 );
   953 /*
   954 ** CAPI3REF: Result Codes
   955 ** KEYWORDS: SQLITE_OK {error code} {error codes}
   956 ** KEYWORDS: {result code} {result codes}
   957 **
   958 ** Many SQLite functions return an integer result code from the set shown
   959 ** here in order to indicate success or failure.
   960 **
   961 ** New error codes may be added in future versions of SQLite.
   962 **
   963 ** See also: [SQLITE_IOERR_READ | extended result codes],
   964 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
   965 */
   966 #define SQLITE_OK           0   /* Successful result */
   967 /* beginning-of-error-codes */
   968 #define SQLITE_ERROR        1   /* SQL error or missing database */
   969 #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
   970 #define SQLITE_PERM         3   /* Access permission denied */
   971 #define SQLITE_ABORT        4   /* Callback routine requested an abort */
   972 #define SQLITE_BUSY         5   /* The database file is locked */
   973 #define SQLITE_LOCKED       6   /* A table in the database is locked */
   974 #define SQLITE_NOMEM        7   /* A malloc() failed */
   975 #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
   976 #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
   977 #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
   978 #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
   979 #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
   980 #define SQLITE_FULL        13   /* Insertion failed because database is full */
   981 #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
   982 #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
   983 #define SQLITE_EMPTY       16   /* Database is empty */
   984 #define SQLITE_SCHEMA      17   /* The database schema changed */
   985 #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
   986 #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
   987 #define SQLITE_MISMATCH    20   /* Data type mismatch */
   988 #define SQLITE_MISUSE      21   /* Library used incorrectly */
   989 #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
   990 #define SQLITE_AUTH        23   /* Authorization denied */
   991 #define SQLITE_FORMAT      24   /* Auxiliary database format error */
   992 #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
   993 #define SQLITE_NOTADB      26   /* File opened that is not a database file */
   994 #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
   995 #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
   996 /* end-of-error-codes */
   998 /*
   999 ** CAPI3REF: Extended Result Codes
  1000 ** KEYWORDS: {extended error code} {extended error codes}
  1001 ** KEYWORDS: {extended result code} {extended result codes}
  1002 **
  1003 ** In its default configuration, SQLite API routines return one of 26 integer
  1004 ** [SQLITE_OK | result codes].  However, experience has shown that many of
  1005 ** these result codes are too coarse-grained.  They do not provide as
  1006 ** much information about problems as programmers might like.  In an effort to
  1007 ** address this, newer versions of SQLite (version 3.3.8 and later) include
  1008 ** support for additional result codes that provide more detailed information
  1009 ** about errors. The extended result codes are enabled or disabled
  1010 ** on a per database connection basis using the
  1011 ** [sqlite3_extended_result_codes()] API.
  1012 **
  1013 ** Some of the available extended result codes are listed here.
  1014 ** One may expect the number of extended result codes will be expand
  1015 ** over time.  Software that uses extended result codes should expect
  1016 ** to see new result codes in future releases of SQLite.
  1017 **
  1018 ** The SQLITE_OK result code will never be extended.  It will always
  1019 ** be exactly zero.
  1020 */
  1021 #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
  1022 #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
  1023 #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
  1024 #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
  1025 #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
  1026 #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
  1027 #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
  1028 #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
  1029 #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
  1030 #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
  1031 #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
  1032 #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
  1033 #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
  1034 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
  1035 #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
  1036 #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
  1037 #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
  1038 #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
  1039 #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
  1040 #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
  1041 #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
  1042 #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
  1043 #define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
  1044 #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
  1045 #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
  1046 #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
  1047 #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
  1048 #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
  1049 #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
  1050 #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
  1051 #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
  1052 #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
  1054 /*
  1055 ** CAPI3REF: Flags For File Open Operations
  1056 **
  1057 ** These bit values are intended for use in the
  1058 ** 3rd parameter to the [sqlite3_open_v2()] interface and
  1059 ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
  1060 */
  1061 #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
  1062 #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
  1063 #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
  1064 #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
  1065 #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
  1066 #define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
  1067 #define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
  1068 #define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
  1069 #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
  1070 #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
  1071 #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
  1072 #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
  1073 #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
  1074 #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
  1075 #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
  1076 #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
  1077 #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
  1078 #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
  1079 #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
  1080 #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
  1082 /* Reserved:                         0x00F00000 */
  1084 /*
  1085 ** CAPI3REF: Device Characteristics
  1086 **
  1087 ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
  1088 ** object returns an integer which is a vector of these
  1089 ** bit values expressing I/O characteristics of the mass storage
  1090 ** device that holds the file that the [sqlite3_io_methods]
  1091 ** refers to.
  1092 **
  1093 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1094 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
  1095 ** mean that writes of blocks that are nnn bytes in size and
  1096 ** are aligned to an address which is an integer multiple of
  1097 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
  1098 ** that when data is appended to a file, the data is appended
  1099 ** first then the size of the file is extended, never the other
  1100 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
  1101 ** information is written to disk in the same order as calls
  1102 ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
  1103 ** after reboot following a crash or power loss, the only bytes in a
  1104 ** file that were written at the application level might have changed
  1105 ** and that adjacent bytes, even bytes within the same sector are
  1106 ** guaranteed to be unchanged.
  1107 */
  1108 #define SQLITE_IOCAP_ATOMIC                 0x00000001
  1109 #define SQLITE_IOCAP_ATOMIC512              0x00000002
  1110 #define SQLITE_IOCAP_ATOMIC1K               0x00000004
  1111 #define SQLITE_IOCAP_ATOMIC2K               0x00000008
  1112 #define SQLITE_IOCAP_ATOMIC4K               0x00000010
  1113 #define SQLITE_IOCAP_ATOMIC8K               0x00000020
  1114 #define SQLITE_IOCAP_ATOMIC16K              0x00000040
  1115 #define SQLITE_IOCAP_ATOMIC32K              0x00000080
  1116 #define SQLITE_IOCAP_ATOMIC64K              0x00000100
  1117 #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
  1118 #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
  1119 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
  1120 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
  1122 /*
  1123 ** CAPI3REF: File Locking Levels
  1124 **
  1125 ** SQLite uses one of these integer values as the second
  1126 ** argument to calls it makes to the xLock() and xUnlock() methods
  1127 ** of an [sqlite3_io_methods] object.
  1128 */
  1129 #define SQLITE_LOCK_NONE          0
  1130 #define SQLITE_LOCK_SHARED        1
  1131 #define SQLITE_LOCK_RESERVED      2
  1132 #define SQLITE_LOCK_PENDING       3
  1133 #define SQLITE_LOCK_EXCLUSIVE     4
  1135 /*
  1136 ** CAPI3REF: Synchronization Type Flags
  1137 **
  1138 ** When SQLite invokes the xSync() method of an
  1139 ** [sqlite3_io_methods] object it uses a combination of
  1140 ** these integer values as the second argument.
  1141 **
  1142 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
  1143 ** sync operation only needs to flush data to mass storage.  Inode
  1144 ** information need not be flushed. If the lower four bits of the flag
  1145 ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
  1146 ** If the lower four bits equal SQLITE_SYNC_FULL, that means
  1147 ** to use Mac OS X style fullsync instead of fsync().
  1148 **
  1149 ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
  1150 ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
  1151 ** settings.  The [synchronous pragma] determines when calls to the
  1152 ** xSync VFS method occur and applies uniformly across all platforms.
  1153 ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
  1154 ** energetic or rigorous or forceful the sync operations are and
  1155 ** only make a difference on Mac OSX for the default SQLite code.
  1156 ** (Third-party VFS implementations might also make the distinction
  1157 ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
  1158 ** operating systems natively supported by SQLite, only Mac OSX
  1159 ** cares about the difference.)
  1160 */
  1161 #define SQLITE_SYNC_NORMAL        0x00002
  1162 #define SQLITE_SYNC_FULL          0x00003
  1163 #define SQLITE_SYNC_DATAONLY      0x00010
  1165 /*
  1166 ** CAPI3REF: OS Interface Open File Handle
  1167 **
  1168 ** An [sqlite3_file] object represents an open file in the 
  1169 ** [sqlite3_vfs | OS interface layer].  Individual OS interface
  1170 ** implementations will
  1171 ** want to subclass this object by appending additional fields
  1172 ** for their own use.  The pMethods entry is a pointer to an
  1173 ** [sqlite3_io_methods] object that defines methods for performing
  1174 ** I/O operations on the open file.
  1175 */
  1176 typedef struct sqlite3_file sqlite3_file;
  1177 struct sqlite3_file {
  1178   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
  1179 };
  1181 /*
  1182 ** CAPI3REF: OS Interface File Virtual Methods Object
  1183 **
  1184 ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
  1185 ** [sqlite3_file] object (or, more commonly, a subclass of the
  1186 ** [sqlite3_file] object) with a pointer to an instance of this object.
  1187 ** This object defines the methods used to perform various operations
  1188 ** against the open file represented by the [sqlite3_file] object.
  1189 **
  1190 ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element 
  1191 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
  1192 ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
  1193 ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
  1194 ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
  1195 ** to NULL.
  1196 **
  1197 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
  1198 ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
  1199 ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
  1200 ** flag may be ORed in to indicate that only the data of the file
  1201 ** and not its inode needs to be synced.
  1202 **
  1203 ** The integer values to xLock() and xUnlock() are one of
  1204 ** <ul>
  1205 ** <li> [SQLITE_LOCK_NONE],
  1206 ** <li> [SQLITE_LOCK_SHARED],
  1207 ** <li> [SQLITE_LOCK_RESERVED],
  1208 ** <li> [SQLITE_LOCK_PENDING], or
  1209 ** <li> [SQLITE_LOCK_EXCLUSIVE].
  1210 ** </ul>
  1211 ** xLock() increases the lock. xUnlock() decreases the lock.
  1212 ** The xCheckReservedLock() method checks whether any database connection,
  1213 ** either in this process or in some other process, is holding a RESERVED,
  1214 ** PENDING, or EXCLUSIVE lock on the file.  It returns true
  1215 ** if such a lock exists and false otherwise.
  1216 **
  1217 ** The xFileControl() method is a generic interface that allows custom
  1218 ** VFS implementations to directly control an open file using the
  1219 ** [sqlite3_file_control()] interface.  The second "op" argument is an
  1220 ** integer opcode.  The third argument is a generic pointer intended to
  1221 ** point to a structure that may contain arguments or space in which to
  1222 ** write return values.  Potential uses for xFileControl() might be
  1223 ** functions to enable blocking locks with timeouts, to change the
  1224 ** locking strategy (for example to use dot-file locks), to inquire
  1225 ** about the status of a lock, or to break stale locks.  The SQLite
  1226 ** core reserves all opcodes less than 100 for its own use.
  1227 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
  1228 ** Applications that define a custom xFileControl method should use opcodes
  1229 ** greater than 100 to avoid conflicts.  VFS implementations should
  1230 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
  1231 ** recognize.
  1232 **
  1233 ** The xSectorSize() method returns the sector size of the
  1234 ** device that underlies the file.  The sector size is the
  1235 ** minimum write that can be performed without disturbing
  1236 ** other bytes in the file.  The xDeviceCharacteristics()
  1237 ** method returns a bit vector describing behaviors of the
  1238 ** underlying device:
  1239 **
  1240 ** <ul>
  1241 ** <li> [SQLITE_IOCAP_ATOMIC]
  1242 ** <li> [SQLITE_IOCAP_ATOMIC512]
  1243 ** <li> [SQLITE_IOCAP_ATOMIC1K]
  1244 ** <li> [SQLITE_IOCAP_ATOMIC2K]
  1245 ** <li> [SQLITE_IOCAP_ATOMIC4K]
  1246 ** <li> [SQLITE_IOCAP_ATOMIC8K]
  1247 ** <li> [SQLITE_IOCAP_ATOMIC16K]
  1248 ** <li> [SQLITE_IOCAP_ATOMIC32K]
  1249 ** <li> [SQLITE_IOCAP_ATOMIC64K]
  1250 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
  1251 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
  1252 ** </ul>
  1253 **
  1254 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1255 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
  1256 ** mean that writes of blocks that are nnn bytes in size and
  1257 ** are aligned to an address which is an integer multiple of
  1258 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
  1259 ** that when data is appended to a file, the data is appended
  1260 ** first then the size of the file is extended, never the other
  1261 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
  1262 ** information is written to disk in the same order as calls
  1263 ** to xWrite().
  1264 **
  1265 ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
  1266 ** in the unread portions of the buffer with zeros.  A VFS that
  1267 ** fails to zero-fill short reads might seem to work.  However,
  1268 ** failure to zero-fill short reads will eventually lead to
  1269 ** database corruption.
  1270 */
  1271 typedef struct sqlite3_io_methods sqlite3_io_methods;
  1272 struct sqlite3_io_methods {
  1273   int iVersion;
  1274   int (*xClose)(sqlite3_file*);
  1275   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  1276   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
  1277   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
  1278   int (*xSync)(sqlite3_file*, int flags);
  1279   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
  1280   int (*xLock)(sqlite3_file*, int);
  1281   int (*xUnlock)(sqlite3_file*, int);
  1282   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
  1283   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
  1284   int (*xSectorSize)(sqlite3_file*);
  1285   int (*xDeviceCharacteristics)(sqlite3_file*);
  1286   /* Methods above are valid for version 1 */
  1287   int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
  1288   int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
  1289   void (*xShmBarrier)(sqlite3_file*);
  1290   int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
  1291   /* Methods above are valid for version 2 */
  1292   /* Additional methods may be added in future releases */
  1293 };
  1295 /*
  1296 ** CAPI3REF: Standard File Control Opcodes
  1297 **
  1298 ** These integer constants are opcodes for the xFileControl method
  1299 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
  1300 ** interface.
  1301 **
  1302 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
  1303 ** opcode causes the xFileControl method to write the current state of
  1304 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
  1305 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
  1306 ** into an integer that the pArg argument points to. This capability
  1307 ** is used during testing and only needs to be supported when SQLITE_TEST
  1308 ** is defined.
  1309 ** <ul>
  1310 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
  1311 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
  1312 ** layer a hint of how large the database file will grow to be during the
  1313 ** current transaction.  This hint is not guaranteed to be accurate but it
  1314 ** is often close.  The underlying VFS might choose to preallocate database
  1315 ** file space based on this hint in order to help writes to the database
  1316 ** file run faster.
  1317 **
  1318 ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
  1319 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
  1320 ** extends and truncates the database file in chunks of a size specified
  1321 ** by the user. The fourth argument to [sqlite3_file_control()] should 
  1322 ** point to an integer (type int) containing the new chunk-size to use
  1323 ** for the nominated database. Allocating database file space in large
  1324 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
  1325 ** improve performance on some systems.
  1326 **
  1327 ** <li>[[SQLITE_FCNTL_FILE_POINTER]]
  1328 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
  1329 ** to the [sqlite3_file] object associated with a particular database
  1330 ** connection.  See the [sqlite3_file_control()] documentation for
  1331 ** additional information.
  1332 **
  1333 ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
  1334 ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
  1335 ** SQLite and sent to all VFSes in place of a call to the xSync method
  1336 ** when the database connection has [PRAGMA synchronous] set to OFF.)^
  1337 ** Some specialized VFSes need this signal in order to operate correctly
  1338 ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
  1339 ** VFSes do not need this signal and should silently ignore this opcode.
  1340 ** Applications should not call [sqlite3_file_control()] with this
  1341 ** opcode as doing so may disrupt the operation of the specialized VFSes
  1342 ** that do require it.  
  1343 **
  1344 ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
  1345 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
  1346 ** retry counts and intervals for certain disk I/O operations for the
  1347 ** windows [VFS] in order to provide robustness in the presence of
  1348 ** anti-virus programs.  By default, the windows VFS will retry file read,
  1349 ** file write, and file delete operations up to 10 times, with a delay
  1350 ** of 25 milliseconds before the first retry and with the delay increasing
  1351 ** by an additional 25 milliseconds with each subsequent retry.  This
  1352 ** opcode allows these two values (10 retries and 25 milliseconds of delay)
  1353 ** to be adjusted.  The values are changed for all database connections
  1354 ** within the same process.  The argument is a pointer to an array of two
  1355 ** integers where the first integer i the new retry count and the second
  1356 ** integer is the delay.  If either integer is negative, then the setting
  1357 ** is not changed but instead the prior value of that setting is written
  1358 ** into the array entry, allowing the current retry settings to be
  1359 ** interrogated.  The zDbName parameter is ignored.
  1360 **
  1361 ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
  1362 ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
  1363 ** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
  1364 ** write ahead log and shared memory files used for transaction control
  1365 ** are automatically deleted when the latest connection to the database
  1366 ** closes.  Setting persistent WAL mode causes those files to persist after
  1367 ** close.  Persisting the files is useful when other processes that do not
  1368 ** have write permission on the directory containing the database file want
  1369 ** to read the database file, as the WAL and shared memory files must exist
  1370 ** in order for the database to be readable.  The fourth parameter to
  1371 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
  1372 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
  1373 ** WAL mode.  If the integer is -1, then it is overwritten with the current
  1374 ** WAL persistence setting.
  1375 **
  1376 ** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
  1377 ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
  1378 ** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
  1379 ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
  1380 ** xDeviceCharacteristics methods. The fourth parameter to
  1381 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
  1382 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
  1383 ** mode.  If the integer is -1, then it is overwritten with the current
  1384 ** zero-damage mode setting.
  1385 **
  1386 ** <li>[[SQLITE_FCNTL_OVERWRITE]]
  1387 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
  1388 ** a write transaction to indicate that, unless it is rolled back for some
  1389 ** reason, the entire database file will be overwritten by the current 
  1390 ** transaction. This is used by VACUUM operations.
  1391 **
  1392 ** <li>[[SQLITE_FCNTL_VFSNAME]]
  1393 ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
  1394 ** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
  1395 ** final bottom-level VFS are written into memory obtained from 
  1396 ** [sqlite3_malloc()] and the result is stored in the char* variable
  1397 ** that the fourth parameter of [sqlite3_file_control()] points to.
  1398 ** The caller is responsible for freeing the memory when done.  As with
  1399 ** all file-control actions, there is no guarantee that this will actually
  1400 ** do anything.  Callers should initialize the char* variable to a NULL
  1401 ** pointer in case this file-control is not implemented.  This file-control
  1402 ** is intended for diagnostic use only.
  1403 **
  1404 ** <li>[[SQLITE_FCNTL_PRAGMA]]
  1405 ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] 
  1406 ** file control is sent to the open [sqlite3_file] object corresponding
  1407 ** to the database file to which the pragma statement refers. ^The argument
  1408 ** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
  1409 ** pointers to strings (char**) in which the second element of the array
  1410 ** is the name of the pragma and the third element is the argument to the
  1411 ** pragma or NULL if the pragma has no argument.  ^The handler for an
  1412 ** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
  1413 ** of the char** argument point to a string obtained from [sqlite3_mprintf()]
  1414 ** or the equivalent and that string will become the result of the pragma or
  1415 ** the error message if the pragma fails. ^If the
  1416 ** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal 
  1417 ** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
  1418 ** file control returns [SQLITE_OK], then the parser assumes that the
  1419 ** VFS has handled the PRAGMA itself and the parser generates a no-op
  1420 ** prepared statement.  ^If the [SQLITE_FCNTL_PRAGMA] file control returns
  1421 ** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
  1422 ** that the VFS encountered an error while handling the [PRAGMA] and the
  1423 ** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
  1424 ** file control occurs at the beginning of pragma statement analysis and so
  1425 ** it is able to override built-in [PRAGMA] statements.
  1426 **
  1427 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
  1428 ** ^This file-control may be invoked by SQLite on the database file handle
  1429 ** shortly after it is opened in order to provide a custom VFS with access
  1430 ** to the connections busy-handler callback. The argument is of type (void **)
  1431 ** - an array of two (void *) values. The first (void *) actually points
  1432 ** to a function of type (int (*)(void *)). In order to invoke the connections
  1433 ** busy-handler, this function should be invoked with the second (void *) in
  1434 ** the array as the only argument. If it returns non-zero, then the operation
  1435 ** should be retried. If it returns zero, the custom VFS should abandon the
  1436 ** current operation.
  1437 **
  1438 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
  1439 ** ^Application can invoke this file-control to have SQLite generate a
  1440 ** temporary filename using the same algorithm that is followed to generate
  1441 ** temporary filenames for TEMP tables and other internal uses.  The
  1442 ** argument should be a char** which will be filled with the filename
  1443 ** written into memory obtained from [sqlite3_malloc()].  The caller should
  1444 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
  1445 **
  1446 ** </ul>
  1447 */
  1448 #define SQLITE_FCNTL_LOCKSTATE               1
  1449 #define SQLITE_GET_LOCKPROXYFILE             2
  1450 #define SQLITE_SET_LOCKPROXYFILE             3
  1451 #define SQLITE_LAST_ERRNO                    4
  1452 #define SQLITE_FCNTL_SIZE_HINT               5
  1453 #define SQLITE_FCNTL_CHUNK_SIZE              6
  1454 #define SQLITE_FCNTL_FILE_POINTER            7
  1455 #define SQLITE_FCNTL_SYNC_OMITTED            8
  1456 #define SQLITE_FCNTL_WIN32_AV_RETRY          9
  1457 #define SQLITE_FCNTL_PERSIST_WAL            10
  1458 #define SQLITE_FCNTL_OVERWRITE              11
  1459 #define SQLITE_FCNTL_VFSNAME                12
  1460 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
  1461 #define SQLITE_FCNTL_PRAGMA                 14
  1462 #define SQLITE_FCNTL_BUSYHANDLER            15
  1463 #define SQLITE_FCNTL_TEMPFILENAME           16
  1465 /*
  1466 ** CAPI3REF: Mutex Handle
  1467 **
  1468 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1469 ** abstract type for a mutex object.  The SQLite core never looks
  1470 ** at the internal representation of an [sqlite3_mutex].  It only
  1471 ** deals with pointers to the [sqlite3_mutex] object.
  1472 **
  1473 ** Mutexes are created using [sqlite3_mutex_alloc()].
  1474 */
  1475 typedef struct sqlite3_mutex sqlite3_mutex;
  1477 /*
  1478 ** CAPI3REF: OS Interface Object
  1479 **
  1480 ** An instance of the sqlite3_vfs object defines the interface between
  1481 ** the SQLite core and the underlying operating system.  The "vfs"
  1482 ** in the name of the object stands for "virtual file system".  See
  1483 ** the [VFS | VFS documentation] for further information.
  1484 **
  1485 ** The value of the iVersion field is initially 1 but may be larger in
  1486 ** future versions of SQLite.  Additional fields may be appended to this
  1487 ** object when the iVersion value is increased.  Note that the structure
  1488 ** of the sqlite3_vfs object changes in the transaction between
  1489 ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
  1490 ** modified.
  1491 **
  1492 ** The szOsFile field is the size of the subclassed [sqlite3_file]
  1493 ** structure used by this VFS.  mxPathname is the maximum length of
  1494 ** a pathname in this VFS.
  1495 **
  1496 ** Registered sqlite3_vfs objects are kept on a linked list formed by
  1497 ** the pNext pointer.  The [sqlite3_vfs_register()]
  1498 ** and [sqlite3_vfs_unregister()] interfaces manage this list
  1499 ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
  1500 ** searches the list.  Neither the application code nor the VFS
  1501 ** implementation should use the pNext pointer.
  1502 **
  1503 ** The pNext field is the only field in the sqlite3_vfs
  1504 ** structure that SQLite will ever modify.  SQLite will only access
  1505 ** or modify this field while holding a particular static mutex.
  1506 ** The application should never modify anything within the sqlite3_vfs
  1507 ** object once the object has been registered.
  1508 **
  1509 ** The zName field holds the name of the VFS module.  The name must
  1510 ** be unique across all VFS modules.
  1511 **
  1512 ** [[sqlite3_vfs.xOpen]]
  1513 ** ^SQLite guarantees that the zFilename parameter to xOpen
  1514 ** is either a NULL pointer or string obtained
  1515 ** from xFullPathname() with an optional suffix added.
  1516 ** ^If a suffix is added to the zFilename parameter, it will
  1517 ** consist of a single "-" character followed by no more than
  1518 ** 11 alphanumeric and/or "-" characters.
  1519 ** ^SQLite further guarantees that
  1520 ** the string will be valid and unchanged until xClose() is
  1521 ** called. Because of the previous sentence,
  1522 ** the [sqlite3_file] can safely store a pointer to the
  1523 ** filename if it needs to remember the filename for some reason.
  1524 ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
  1525 ** must invent its own temporary name for the file.  ^Whenever the 
  1526 ** xFilename parameter is NULL it will also be the case that the
  1527 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
  1528 **
  1529 ** The flags argument to xOpen() includes all bits set in
  1530 ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
  1531 ** or [sqlite3_open16()] is used, then flags includes at least
  1532 ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. 
  1533 ** If xOpen() opens a file read-only then it sets *pOutFlags to
  1534 ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
  1535 **
  1536 ** ^(SQLite will also add one of the following flags to the xOpen()
  1537 ** call, depending on the object being opened:
  1538 **
  1539 ** <ul>
  1540 ** <li>  [SQLITE_OPEN_MAIN_DB]
  1541 ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
  1542 ** <li>  [SQLITE_OPEN_TEMP_DB]
  1543 ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
  1544 ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
  1545 ** <li>  [SQLITE_OPEN_SUBJOURNAL]
  1546 ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
  1547 ** <li>  [SQLITE_OPEN_WAL]
  1548 ** </ul>)^
  1549 **
  1550 ** The file I/O implementation can use the object type flags to
  1551 ** change the way it deals with files.  For example, an application
  1552 ** that does not care about crash recovery or rollback might make
  1553 ** the open of a journal file a no-op.  Writes to this journal would
  1554 ** also be no-ops, and any attempt to read the journal would return
  1555 ** SQLITE_IOERR.  Or the implementation might recognize that a database
  1556 ** file will be doing page-aligned sector reads and writes in a random
  1557 ** order and set up its I/O subsystem accordingly.
  1558 **
  1559 ** SQLite might also add one of the following flags to the xOpen method:
  1560 **
  1561 ** <ul>
  1562 ** <li> [SQLITE_OPEN_DELETEONCLOSE]
  1563 ** <li> [SQLITE_OPEN_EXCLUSIVE]
  1564 ** </ul>
  1565 **
  1566 ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
  1567 ** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
  1568 ** will be set for TEMP databases and their journals, transient
  1569 ** databases, and subjournals.
  1570 **
  1571 ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
  1572 ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
  1573 ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
  1574 ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the 
  1575 ** SQLITE_OPEN_CREATE, is used to indicate that file should always
  1576 ** be created, and that it is an error if it already exists.
  1577 ** It is <i>not</i> used to indicate the file should be opened 
  1578 ** for exclusive access.
  1579 **
  1580 ** ^At least szOsFile bytes of memory are allocated by SQLite
  1581 ** to hold the  [sqlite3_file] structure passed as the third
  1582 ** argument to xOpen.  The xOpen method does not have to
  1583 ** allocate the structure; it should just fill it in.  Note that
  1584 ** the xOpen method must set the sqlite3_file.pMethods to either
  1585 ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
  1586 ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
  1587 ** element will be valid after xOpen returns regardless of the success
  1588 ** or failure of the xOpen call.
  1589 **
  1590 ** [[sqlite3_vfs.xAccess]]
  1591 ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
  1592 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
  1593 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
  1594 ** to test whether a file is at least readable.   The file can be a
  1595 ** directory.
  1596 **
  1597 ** ^SQLite will always allocate at least mxPathname+1 bytes for the
  1598 ** output buffer xFullPathname.  The exact size of the output buffer
  1599 ** is also passed as a parameter to both  methods. If the output buffer
  1600 ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
  1601 ** handled as a fatal error by SQLite, vfs implementations should endeavor
  1602 ** to prevent this by setting mxPathname to a sufficiently large value.
  1603 **
  1604 ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
  1605 ** interfaces are not strictly a part of the filesystem, but they are
  1606 ** included in the VFS structure for completeness.
  1607 ** The xRandomness() function attempts to return nBytes bytes
  1608 ** of good-quality randomness into zOut.  The return value is
  1609 ** the actual number of bytes of randomness obtained.
  1610 ** The xSleep() method causes the calling thread to sleep for at
  1611 ** least the number of microseconds given.  ^The xCurrentTime()
  1612 ** method returns a Julian Day Number for the current date and time as
  1613 ** a floating point value.
  1614 ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
  1615 ** Day Number multiplied by 86400000 (the number of milliseconds in 
  1616 ** a 24-hour day).  
  1617 ** ^SQLite will use the xCurrentTimeInt64() method to get the current
  1618 ** date and time if that method is available (if iVersion is 2 or 
  1619 ** greater and the function pointer is not NULL) and will fall back
  1620 ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
  1621 **
  1622 ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
  1623 ** are not used by the SQLite core.  These optional interfaces are provided
  1624 ** by some VFSes to facilitate testing of the VFS code. By overriding 
  1625 ** system calls with functions under its control, a test program can
  1626 ** simulate faults and error conditions that would otherwise be difficult
  1627 ** or impossible to induce.  The set of system calls that can be overridden
  1628 ** varies from one VFS to another, and from one version of the same VFS to the
  1629 ** next.  Applications that use these interfaces must be prepared for any
  1630 ** or all of these interfaces to be NULL or for their behavior to change
  1631 ** from one release to the next.  Applications must not attempt to access
  1632 ** any of these methods if the iVersion of the VFS is less than 3.
  1633 */
  1634 typedef struct sqlite3_vfs sqlite3_vfs;
  1635 typedef void (*sqlite3_syscall_ptr)(void);
  1636 struct sqlite3_vfs {
  1637   int iVersion;            /* Structure version number (currently 3) */
  1638   int szOsFile;            /* Size of subclassed sqlite3_file */
  1639   int mxPathname;          /* Maximum file pathname length */
  1640   sqlite3_vfs *pNext;      /* Next registered VFS */
  1641   const char *zName;       /* Name of this virtual file system */
  1642   void *pAppData;          /* Pointer to application-specific data */
  1643   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
  1644                int flags, int *pOutFlags);
  1645   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
  1646   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
  1647   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
  1648   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
  1649   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
  1650   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
  1651   void (*xDlClose)(sqlite3_vfs*, void*);
  1652   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
  1653   int (*xSleep)(sqlite3_vfs*, int microseconds);
  1654   int (*xCurrentTime)(sqlite3_vfs*, double*);
  1655   int (*xGetLastError)(sqlite3_vfs*, int, char *);
  1656   /*
  1657   ** The methods above are in version 1 of the sqlite_vfs object
  1658   ** definition.  Those that follow are added in version 2 or later
  1659   */
  1660   int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
  1661   /*
  1662   ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
  1663   ** Those below are for version 3 and greater.
  1664   */
  1665   int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
  1666   sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
  1667   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
  1668   /*
  1669   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
  1670   ** New fields may be appended in figure versions.  The iVersion
  1671   ** value will increment whenever this happens. 
  1672   */
  1673 };
  1675 /*
  1676 ** CAPI3REF: Flags for the xAccess VFS method
  1677 **
  1678 ** These integer constants can be used as the third parameter to
  1679 ** the xAccess method of an [sqlite3_vfs] object.  They determine
  1680 ** what kind of permissions the xAccess method is looking for.
  1681 ** With SQLITE_ACCESS_EXISTS, the xAccess method
  1682 ** simply checks whether the file exists.
  1683 ** With SQLITE_ACCESS_READWRITE, the xAccess method
  1684 ** checks whether the named directory is both readable and writable
  1685 ** (in other words, if files can be added, removed, and renamed within
  1686 ** the directory).
  1687 ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
  1688 ** [temp_store_directory pragma], though this could change in a future
  1689 ** release of SQLite.
  1690 ** With SQLITE_ACCESS_READ, the xAccess method
  1691 ** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
  1692 ** currently unused, though it might be used in a future release of
  1693 ** SQLite.
  1694 */
  1695 #define SQLITE_ACCESS_EXISTS    0
  1696 #define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
  1697 #define SQLITE_ACCESS_READ      2   /* Unused */
  1699 /*
  1700 ** CAPI3REF: Flags for the xShmLock VFS method
  1701 **
  1702 ** These integer constants define the various locking operations
  1703 ** allowed by the xShmLock method of [sqlite3_io_methods].  The
  1704 ** following are the only legal combinations of flags to the
  1705 ** xShmLock method:
  1706 **
  1707 ** <ul>
  1708 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
  1709 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
  1710 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
  1711 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
  1712 ** </ul>
  1713 **
  1714 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
  1715 ** was given no the corresponding lock.  
  1716 **
  1717 ** The xShmLock method can transition between unlocked and SHARED or
  1718 ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
  1719 ** and EXCLUSIVE.
  1720 */
  1721 #define SQLITE_SHM_UNLOCK       1
  1722 #define SQLITE_SHM_LOCK         2
  1723 #define SQLITE_SHM_SHARED       4
  1724 #define SQLITE_SHM_EXCLUSIVE    8
  1726 /*
  1727 ** CAPI3REF: Maximum xShmLock index
  1728 **
  1729 ** The xShmLock method on [sqlite3_io_methods] may use values
  1730 ** between 0 and this upper bound as its "offset" argument.
  1731 ** The SQLite core will never attempt to acquire or release a
  1732 ** lock outside of this range
  1733 */
  1734 #define SQLITE_SHM_NLOCK        8
  1737 /*
  1738 ** CAPI3REF: Initialize The SQLite Library
  1739 **
  1740 ** ^The sqlite3_initialize() routine initializes the
  1741 ** SQLite library.  ^The sqlite3_shutdown() routine
  1742 ** deallocates any resources that were allocated by sqlite3_initialize().
  1743 ** These routines are designed to aid in process initialization and
  1744 ** shutdown on embedded systems.  Workstation applications using
  1745 ** SQLite normally do not need to invoke either of these routines.
  1746 **
  1747 ** A call to sqlite3_initialize() is an "effective" call if it is
  1748 ** the first time sqlite3_initialize() is invoked during the lifetime of
  1749 ** the process, or if it is the first time sqlite3_initialize() is invoked
  1750 ** following a call to sqlite3_shutdown().  ^(Only an effective call
  1751 ** of sqlite3_initialize() does any initialization.  All other calls
  1752 ** are harmless no-ops.)^
  1753 **
  1754 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
  1755 ** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
  1756 ** an effective call to sqlite3_shutdown() does any deinitialization.
  1757 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
  1758 **
  1759 ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
  1760 ** is not.  The sqlite3_shutdown() interface must only be called from a
  1761 ** single thread.  All open [database connections] must be closed and all
  1762 ** other SQLite resources must be deallocated prior to invoking
  1763 ** sqlite3_shutdown().
  1764 **
  1765 ** Among other things, ^sqlite3_initialize() will invoke
  1766 ** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
  1767 ** will invoke sqlite3_os_end().
  1768 **
  1769 ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
  1770 ** ^If for some reason, sqlite3_initialize() is unable to initialize
  1771 ** the library (perhaps it is unable to allocate a needed resource such
  1772 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
  1773 **
  1774 ** ^The sqlite3_initialize() routine is called internally by many other
  1775 ** SQLite interfaces so that an application usually does not need to
  1776 ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
  1777 ** calls sqlite3_initialize() so the SQLite library will be automatically
  1778 ** initialized when [sqlite3_open()] is called if it has not be initialized
  1779 ** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
  1780 ** compile-time option, then the automatic calls to sqlite3_initialize()
  1781 ** are omitted and the application must call sqlite3_initialize() directly
  1782 ** prior to using any other SQLite interface.  For maximum portability,
  1783 ** it is recommended that applications always invoke sqlite3_initialize()
  1784 ** directly prior to using any other SQLite interface.  Future releases
  1785 ** of SQLite may require this.  In other words, the behavior exhibited
  1786 ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
  1787 ** default behavior in some future release of SQLite.
  1788 **
  1789 ** The sqlite3_os_init() routine does operating-system specific
  1790 ** initialization of the SQLite library.  The sqlite3_os_end()
  1791 ** routine undoes the effect of sqlite3_os_init().  Typical tasks
  1792 ** performed by these routines include allocation or deallocation
  1793 ** of static resources, initialization of global variables,
  1794 ** setting up a default [sqlite3_vfs] module, or setting up
  1795 ** a default configuration using [sqlite3_config()].
  1796 **
  1797 ** The application should never invoke either sqlite3_os_init()
  1798 ** or sqlite3_os_end() directly.  The application should only invoke
  1799 ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
  1800 ** interface is called automatically by sqlite3_initialize() and
  1801 ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
  1802 ** implementations for sqlite3_os_init() and sqlite3_os_end()
  1803 ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
  1804 ** When [custom builds | built for other platforms]
  1805 ** (using the [SQLITE_OS_OTHER=1] compile-time
  1806 ** option) the application must supply a suitable implementation for
  1807 ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
  1808 ** implementation of sqlite3_os_init() or sqlite3_os_end()
  1809 ** must return [SQLITE_OK] on success and some other [error code] upon
  1810 ** failure.
  1811 */
  1812 SQLITE_API int sqlite3_initialize(void);
  1813 SQLITE_API int sqlite3_shutdown(void);
  1814 SQLITE_API int sqlite3_os_init(void);
  1815 SQLITE_API int sqlite3_os_end(void);
  1817 /*
  1818 ** CAPI3REF: Configuring The SQLite Library
  1819 **
  1820 ** The sqlite3_config() interface is used to make global configuration
  1821 ** changes to SQLite in order to tune SQLite to the specific needs of
  1822 ** the application.  The default configuration is recommended for most
  1823 ** applications and so this routine is usually not necessary.  It is
  1824 ** provided to support rare applications with unusual needs.
  1825 **
  1826 ** The sqlite3_config() interface is not threadsafe.  The application
  1827 ** must insure that no other SQLite interfaces are invoked by other
  1828 ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
  1829 ** may only be invoked prior to library initialization using
  1830 ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
  1831 ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
  1832 ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
  1833 ** Note, however, that ^sqlite3_config() can be called as part of the
  1834 ** implementation of an application-defined [sqlite3_os_init()].
  1835 **
  1836 ** The first argument to sqlite3_config() is an integer
  1837 ** [configuration option] that determines
  1838 ** what property of SQLite is to be configured.  Subsequent arguments
  1839 ** vary depending on the [configuration option]
  1840 ** in the first argument.
  1841 **
  1842 ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
  1843 ** ^If the option is unknown or SQLite is unable to set the option
  1844 ** then this routine returns a non-zero [error code].
  1845 */
  1846 SQLITE_API int sqlite3_config(int, ...);
  1848 /*
  1849 ** CAPI3REF: Configure database connections
  1850 **
  1851 ** The sqlite3_db_config() interface is used to make configuration
  1852 ** changes to a [database connection].  The interface is similar to
  1853 ** [sqlite3_config()] except that the changes apply to a single
  1854 ** [database connection] (specified in the first argument).
  1855 **
  1856 ** The second argument to sqlite3_db_config(D,V,...)  is the
  1857 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code 
  1858 ** that indicates what aspect of the [database connection] is being configured.
  1859 ** Subsequent arguments vary depending on the configuration verb.
  1860 **
  1861 ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
  1862 ** the call is considered successful.
  1863 */
  1864 SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
  1866 /*
  1867 ** CAPI3REF: Memory Allocation Routines
  1868 **
  1869 ** An instance of this object defines the interface between SQLite
  1870 ** and low-level memory allocation routines.
  1871 **
  1872 ** This object is used in only one place in the SQLite interface.
  1873 ** A pointer to an instance of this object is the argument to
  1874 ** [sqlite3_config()] when the configuration option is
  1875 ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].  
  1876 ** By creating an instance of this object
  1877 ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
  1878 ** during configuration, an application can specify an alternative
  1879 ** memory allocation subsystem for SQLite to use for all of its
  1880 ** dynamic memory needs.
  1881 **
  1882 ** Note that SQLite comes with several [built-in memory allocators]
  1883 ** that are perfectly adequate for the overwhelming majority of applications
  1884 ** and that this object is only useful to a tiny minority of applications
  1885 ** with specialized memory allocation requirements.  This object is
  1886 ** also used during testing of SQLite in order to specify an alternative
  1887 ** memory allocator that simulates memory out-of-memory conditions in
  1888 ** order to verify that SQLite recovers gracefully from such
  1889 ** conditions.
  1890 **
  1891 ** The xMalloc, xRealloc, and xFree methods must work like the
  1892 ** malloc(), realloc() and free() functions from the standard C library.
  1893 ** ^SQLite guarantees that the second argument to
  1894 ** xRealloc is always a value returned by a prior call to xRoundup.
  1895 **
  1896 ** xSize should return the allocated size of a memory allocation
  1897 ** previously obtained from xMalloc or xRealloc.  The allocated size
  1898 ** is always at least as big as the requested size but may be larger.
  1899 **
  1900 ** The xRoundup method returns what would be the allocated size of
  1901 ** a memory allocation given a particular requested size.  Most memory
  1902 ** allocators round up memory allocations at least to the next multiple
  1903 ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
  1904 ** Every memory allocation request coming in through [sqlite3_malloc()]
  1905 ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0, 
  1906 ** that causes the corresponding memory allocation to fail.
  1907 **
  1908 ** The xInit method initializes the memory allocator.  (For example,
  1909 ** it might allocate any require mutexes or initialize internal data
  1910 ** structures.  The xShutdown method is invoked (indirectly) by
  1911 ** [sqlite3_shutdown()] and should deallocate any resources acquired
  1912 ** by xInit.  The pAppData pointer is used as the only parameter to
  1913 ** xInit and xShutdown.
  1914 **
  1915 ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
  1916 ** the xInit method, so the xInit method need not be threadsafe.  The
  1917 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  1918 ** not need to be threadsafe either.  For all other methods, SQLite
  1919 ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
  1920 ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
  1921 ** it is by default) and so the methods are automatically serialized.
  1922 ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
  1923 ** methods must be threadsafe or else make their own arrangements for
  1924 ** serialization.
  1925 **
  1926 ** SQLite will never invoke xInit() more than once without an intervening
  1927 ** call to xShutdown().
  1928 */
  1929 typedef struct sqlite3_mem_methods sqlite3_mem_methods;
  1930 struct sqlite3_mem_methods {
  1931   void *(*xMalloc)(int);         /* Memory allocation function */
  1932   void (*xFree)(void*);          /* Free a prior allocation */
  1933   void *(*xRealloc)(void*,int);  /* Resize an allocation */
  1934   int (*xSize)(void*);           /* Return the size of an allocation */
  1935   int (*xRoundup)(int);          /* Round up request size to allocation size */
  1936   int (*xInit)(void*);           /* Initialize the memory allocator */
  1937   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
  1938   void *pAppData;                /* Argument to xInit() and xShutdown() */
  1939 };
  1941 /*
  1942 ** CAPI3REF: Configuration Options
  1943 ** KEYWORDS: {configuration option}
  1944 **
  1945 ** These constants are the available integer configuration options that
  1946 ** can be passed as the first argument to the [sqlite3_config()] interface.
  1947 **
  1948 ** New configuration options may be added in future releases of SQLite.
  1949 ** Existing configuration options might be discontinued.  Applications
  1950 ** should check the return code from [sqlite3_config()] to make sure that
  1951 ** the call worked.  The [sqlite3_config()] interface will return a
  1952 ** non-zero [error code] if a discontinued or unsupported configuration option
  1953 ** is invoked.
  1954 **
  1955 ** <dl>
  1956 ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
  1957 ** <dd>There are no arguments to this option.  ^This option sets the
  1958 ** [threading mode] to Single-thread.  In other words, it disables
  1959 ** all mutexing and puts SQLite into a mode where it can only be used
  1960 ** by a single thread.   ^If SQLite is compiled with
  1961 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1962 ** it is not possible to change the [threading mode] from its default
  1963 ** value of Single-thread and so [sqlite3_config()] will return 
  1964 ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
  1965 ** configuration option.</dd>
  1966 **
  1967 ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
  1968 ** <dd>There are no arguments to this option.  ^This option sets the
  1969 ** [threading mode] to Multi-thread.  In other words, it disables
  1970 ** mutexing on [database connection] and [prepared statement] objects.
  1971 ** The application is responsible for serializing access to
  1972 ** [database connections] and [prepared statements].  But other mutexes
  1973 ** are enabled so that SQLite will be safe to use in a multi-threaded
  1974 ** environment as long as no two threads attempt to use the same
  1975 ** [database connection] at the same time.  ^If SQLite is compiled with
  1976 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1977 ** it is not possible to set the Multi-thread [threading mode] and
  1978 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1979 ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
  1980 **
  1981 ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
  1982 ** <dd>There are no arguments to this option.  ^This option sets the
  1983 ** [threading mode] to Serialized. In other words, this option enables
  1984 ** all mutexes including the recursive
  1985 ** mutexes on [database connection] and [prepared statement] objects.
  1986 ** In this mode (which is the default when SQLite is compiled with
  1987 ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
  1988 ** to [database connections] and [prepared statements] so that the
  1989 ** application is free to use the same [database connection] or the
  1990 ** same [prepared statement] in different threads at the same time.
  1991 ** ^If SQLite is compiled with
  1992 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1993 ** it is not possible to set the Serialized [threading mode] and
  1994 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1995 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
  1996 **
  1997 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
  1998 ** <dd> ^(This option takes a single argument which is a pointer to an
  1999 ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
  2000 ** alternative low-level memory allocation routines to be used in place of
  2001 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
  2002 ** its own private copy of the content of the [sqlite3_mem_methods] structure
  2003 ** before the [sqlite3_config()] call returns.</dd>
  2004 **
  2005 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
  2006 ** <dd> ^(This option takes a single argument which is a pointer to an
  2007 ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
  2008 ** structure is filled with the currently defined memory allocation routines.)^
  2009 ** This option can be used to overload the default memory allocation
  2010 ** routines with a wrapper that simulations memory allocation failure or
  2011 ** tracks memory usage, for example. </dd>
  2012 **
  2013 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  2014 ** <dd> ^This option takes single argument of type int, interpreted as a 
  2015 ** boolean, which enables or disables the collection of memory allocation 
  2016 ** statistics. ^(When memory allocation statistics are disabled, the 
  2017 ** following SQLite interfaces become non-operational:
  2018 **   <ul>
  2019 **   <li> [sqlite3_memory_used()]
  2020 **   <li> [sqlite3_memory_highwater()]
  2021 **   <li> [sqlite3_soft_heap_limit64()]
  2022 **   <li> [sqlite3_status()]
  2023 **   </ul>)^
  2024 ** ^Memory allocation statistics are enabled by default unless SQLite is
  2025 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  2026 ** allocation statistics are disabled by default.
  2027 ** </dd>
  2028 **
  2029 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
  2030 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  2031 ** scratch memory.  There are three arguments:  A pointer an 8-byte
  2032 ** aligned memory buffer from which the scratch allocations will be
  2033 ** drawn, the size of each scratch allocation (sz),
  2034 ** and the maximum number of scratch allocations (N).  The sz
  2035 ** argument must be a multiple of 16.
  2036 ** The first argument must be a pointer to an 8-byte aligned buffer
  2037 ** of at least sz*N bytes of memory.
  2038 ** ^SQLite will use no more than two scratch buffers per thread.  So
  2039 ** N should be set to twice the expected maximum number of threads.
  2040 ** ^SQLite will never require a scratch buffer that is more than 6
  2041 ** times the database page size. ^If SQLite needs needs additional
  2042 ** scratch memory beyond what is provided by this configuration option, then 
  2043 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
  2044 **
  2045 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
  2046 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  2047 ** the database page cache with the default page cache implementation.  
  2048 ** This configuration should not be used if an application-define page
  2049 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
  2050 ** There are three arguments to this option: A pointer to 8-byte aligned
  2051 ** memory, the size of each page buffer (sz), and the number of pages (N).
  2052 ** The sz argument should be the size of the largest database page
  2053 ** (a power of two between 512 and 32768) plus a little extra for each
  2054 ** page header.  ^The page header size is 20 to 40 bytes depending on
  2055 ** the host architecture.  ^It is harmless, apart from the wasted memory,
  2056 ** to make sz a little too large.  The first
  2057 ** argument should point to an allocation of at least sz*N bytes of memory.
  2058 ** ^SQLite will use the memory provided by the first argument to satisfy its
  2059 ** memory needs for the first N pages that it adds to cache.  ^If additional
  2060 ** page cache memory is needed beyond what is provided by this option, then
  2061 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
  2062 ** The pointer in the first argument must
  2063 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
  2064 ** will be undefined.</dd>
  2065 **
  2066 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
  2067 ** <dd> ^This option specifies a static memory buffer that SQLite will use
  2068 ** for all of its dynamic memory allocation needs beyond those provided
  2069 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
  2070 ** There are three arguments: An 8-byte aligned pointer to the memory,
  2071 ** the number of bytes in the memory buffer, and the minimum allocation size.
  2072 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
  2073 ** to using its default memory allocator (the system malloc() implementation),
  2074 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
  2075 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  2076 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  2077 ** allocator is engaged to handle all of SQLites memory allocation needs.
  2078 ** The first pointer (the memory pointer) must be aligned to an 8-byte
  2079 ** boundary or subsequent behavior of SQLite will be undefined.
  2080 ** The minimum allocation size is capped at 2**12. Reasonable values
  2081 ** for the minimum allocation size are 2**5 through 2**8.</dd>
  2082 **
  2083 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
  2084 ** <dd> ^(This option takes a single argument which is a pointer to an
  2085 ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
  2086 ** alternative low-level mutex routines to be used in place
  2087 ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  2088 ** content of the [sqlite3_mutex_methods] structure before the call to
  2089 ** [sqlite3_config()] returns. ^If SQLite is compiled with
  2090 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  2091 ** the entire mutexing subsystem is omitted from the build and hence calls to
  2092 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
  2093 ** return [SQLITE_ERROR].</dd>
  2094 **
  2095 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
  2096 ** <dd> ^(This option takes a single argument which is a pointer to an
  2097 ** instance of the [sqlite3_mutex_methods] structure.  The
  2098 ** [sqlite3_mutex_methods]
  2099 ** structure is filled with the currently defined mutex routines.)^
  2100 ** This option can be used to overload the default mutex allocation
  2101 ** routines with a wrapper used to track mutex usage for performance
  2102 ** profiling or testing, for example.   ^If SQLite is compiled with
  2103 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  2104 ** the entire mutexing subsystem is omitted from the build and hence calls to
  2105 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
  2106 ** return [SQLITE_ERROR].</dd>
  2107 **
  2108 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
  2109 ** <dd> ^(This option takes two arguments that determine the default
  2110 ** memory allocation for the lookaside memory allocator on each
  2111 ** [database connection].  The first argument is the
  2112 ** size of each lookaside buffer slot and the second is the number of
  2113 ** slots allocated to each database connection.)^  ^(This option sets the
  2114 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
  2115 ** verb to [sqlite3_db_config()] can be used to change the lookaside
  2116 ** configuration on individual connections.)^ </dd>
  2117 **
  2118 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
  2119 ** <dd> ^(This option takes a single argument which is a pointer to
  2120 ** an [sqlite3_pcache_methods2] object.  This object specifies the interface
  2121 ** to a custom page cache implementation.)^  ^SQLite makes a copy of the
  2122 ** object and uses it for page cache memory allocations.</dd>
  2123 **
  2124 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
  2125 ** <dd> ^(This option takes a single argument which is a pointer to an
  2126 ** [sqlite3_pcache_methods2] object.  SQLite copies of the current
  2127 ** page cache implementation into that object.)^ </dd>
  2128 **
  2129 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
  2130 ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
  2131 ** function with a call signature of void(*)(void*,int,const char*), 
  2132 ** and a pointer to void. ^If the function pointer is not NULL, it is
  2133 ** invoked by [sqlite3_log()] to process each logging event.  ^If the
  2134 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
  2135 ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
  2136 ** passed through as the first parameter to the application-defined logger
  2137 ** function whenever that function is invoked.  ^The second parameter to
  2138 ** the logger function is a copy of the first parameter to the corresponding
  2139 ** [sqlite3_log()] call and is intended to be a [result code] or an
  2140 ** [extended result code].  ^The third parameter passed to the logger is
  2141 ** log message after formatting via [sqlite3_snprintf()].
  2142 ** The SQLite logging interface is not reentrant; the logger function
  2143 ** supplied by the application must not invoke any SQLite interface.
  2144 ** In a multi-threaded application, the application-defined logger
  2145 ** function must be threadsafe. </dd>
  2146 **
  2147 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
  2148 ** <dd> This option takes a single argument of type int. If non-zero, then
  2149 ** URI handling is globally enabled. If the parameter is zero, then URI handling
  2150 ** is globally disabled. If URI handling is globally enabled, all filenames
  2151 ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
  2152 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  2153 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  2154 ** connection is opened. If it is globally disabled, filenames are
  2155 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  2156 ** database connection is opened. By default, URI handling is globally
  2157 ** disabled. The default value may be changed by compiling with the
  2158 ** [SQLITE_USE_URI] symbol defined.
  2159 **
  2160 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
  2161 ** <dd> This option takes a single integer argument which is interpreted as
  2162 ** a boolean in order to enable or disable the use of covering indices for
  2163 ** full table scans in the query optimizer.  The default setting is determined
  2164 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
  2165 ** if that compile-time option is omitted.
  2166 ** The ability to disable the use of covering indices for full table scans
  2167 ** is because some incorrectly coded legacy applications might malfunction
  2168 ** malfunction when the optimization is enabled.  Providing the ability to
  2169 ** disable the optimization allows the older, buggy application code to work
  2170 ** without change even with newer versions of SQLite.
  2171 **
  2172 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  2173 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
  2174 ** <dd> These options are obsolete and should not be used by new code.
  2175 ** They are retained for backwards compatibility but are now no-ops.
  2176 ** </dl>
  2177 **
  2178 ** [[SQLITE_CONFIG_SQLLOG]]
  2179 ** <dt>SQLITE_CONFIG_SQLLOG
  2180 ** <dd>This option is only available if sqlite is compiled with the
  2181 ** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
  2182 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
  2183 ** The second should be of type (void*). The callback is invoked by the library
  2184 ** in three separate circumstances, identified by the value passed as the
  2185 ** fourth parameter. If the fourth parameter is 0, then the database connection
  2186 ** passed as the second argument has just been opened. The third argument
  2187 ** points to a buffer containing the name of the main database file. If the
  2188 ** fourth parameter is 1, then the SQL statement that the third parameter
  2189 ** points to has just been executed. Or, if the fourth parameter is 2, then
  2190 ** the connection being passed as the second parameter is being closed. The
  2191 ** third parameter is passed NULL In this case.
  2192 ** </dl>
  2193 */
  2194 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  2195 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  2196 #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  2197 #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  2198 #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
  2199 #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
  2200 #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
  2201 #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
  2202 #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
  2203 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
  2204 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
  2205 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
  2206 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  2207 #define SQLITE_CONFIG_PCACHE       14  /* no-op */
  2208 #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  2209 #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  2210 #define SQLITE_CONFIG_URI          17  /* int */
  2211 #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  2212 #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
  2213 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
  2214 #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
  2216 /*
  2217 ** CAPI3REF: Database Connection Configuration Options
  2218 **
  2219 ** These constants are the available integer configuration options that
  2220 ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  2221 **
  2222 ** New configuration options may be added in future releases of SQLite.
  2223 ** Existing configuration options might be discontinued.  Applications
  2224 ** should check the return code from [sqlite3_db_config()] to make sure that
  2225 ** the call worked.  ^The [sqlite3_db_config()] interface will return a
  2226 ** non-zero [error code] if a discontinued or unsupported configuration option
  2227 ** is invoked.
  2228 **
  2229 ** <dl>
  2230 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  2231 ** <dd> ^This option takes three additional arguments that determine the 
  2232 ** [lookaside memory allocator] configuration for the [database connection].
  2233 ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  2234 ** pointer to a memory buffer to use for lookaside memory.
  2235 ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  2236 ** may be NULL in which case SQLite will allocate the
  2237 ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
  2238 ** size of each lookaside buffer slot.  ^The third argument is the number of
  2239 ** slots.  The size of the buffer in the first argument must be greater than
  2240 ** or equal to the product of the second and third arguments.  The buffer
  2241 ** must be aligned to an 8-byte boundary.  ^If the second argument to
  2242 ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
  2243 ** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
  2244 ** configuration for a database connection can only be changed when that
  2245 ** connection is not currently using lookaside memory, or in other words
  2246 ** when the "current value" returned by
  2247 ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  2248 ** Any attempt to change the lookaside memory configuration when lookaside
  2249 ** memory is in use leaves the configuration unchanged and returns 
  2250 ** [SQLITE_BUSY].)^</dd>
  2251 **
  2252 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  2253 ** <dd> ^This option is used to enable or disable the enforcement of
  2254 ** [foreign key constraints].  There should be two additional arguments.
  2255 ** The first argument is an integer which is 0 to disable FK enforcement,
  2256 ** positive to enable FK enforcement or negative to leave FK enforcement
  2257 ** unchanged.  The second parameter is a pointer to an integer into which
  2258 ** is written 0 or 1 to indicate whether FK enforcement is off or on
  2259 ** following this call.  The second parameter may be a NULL pointer, in
  2260 ** which case the FK enforcement setting is not reported back. </dd>
  2261 **
  2262 ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  2263 ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  2264 ** There should be two additional arguments.
  2265 ** The first argument is an integer which is 0 to disable triggers,
  2266 ** positive to enable triggers or negative to leave the setting unchanged.
  2267 ** The second parameter is a pointer to an integer into which
  2268 ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  2269 ** following this call.  The second parameter may be a NULL pointer, in
  2270 ** which case the trigger setting is not reported back. </dd>
  2271 **
  2272 ** </dl>
  2273 */
  2274 #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
  2275 #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
  2276 #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
  2279 /*
  2280 ** CAPI3REF: Enable Or Disable Extended Result Codes
  2281 **
  2282 ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2283 ** [extended result codes] feature of SQLite. ^The extended result
  2284 ** codes are disabled by default for historical compatibility.
  2285 */
  2286 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
  2288 /*
  2289 ** CAPI3REF: Last Insert Rowid
  2290 **
  2291 ** ^Each entry in an SQLite table has a unique 64-bit signed
  2292 ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  2293 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  2294 ** names are not also used by explicitly declared columns. ^If
  2295 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
  2296 ** is another alias for the rowid.
  2297 **
  2298 ** ^This routine returns the [rowid] of the most recent
  2299 ** successful [INSERT] into the database from the [database connection]
  2300 ** in the first argument.  ^As of SQLite version 3.7.7, this routines
  2301 ** records the last insert rowid of both ordinary tables and [virtual tables].
  2302 ** ^If no successful [INSERT]s
  2303 ** have ever occurred on that database connection, zero is returned.
  2304 **
  2305 ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
  2306 ** method, then this routine will return the [rowid] of the inserted
  2307 ** row as long as the trigger or virtual table method is running.
  2308 ** But once the trigger or virtual table method ends, the value returned 
  2309 ** by this routine reverts to what it was before the trigger or virtual
  2310 ** table method began.)^
  2311 **
  2312 ** ^An [INSERT] that fails due to a constraint violation is not a
  2313 ** successful [INSERT] and does not change the value returned by this
  2314 ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
  2315 ** and INSERT OR ABORT make no changes to the return value of this
  2316 ** routine when their insertion fails.  ^(When INSERT OR REPLACE
  2317 ** encounters a constraint violation, it does not fail.  The
  2318 ** INSERT continues to completion after deleting rows that caused
  2319 ** the constraint problem so INSERT OR REPLACE will always change
  2320 ** the return value of this interface.)^
  2321 **
  2322 ** ^For the purposes of this routine, an [INSERT] is considered to
  2323 ** be successful even if it is subsequently rolled back.
  2324 **
  2325 ** This function is accessible to SQL statements via the
  2326 ** [last_insert_rowid() SQL function].
  2327 **
  2328 ** If a separate thread performs a new [INSERT] on the same
  2329 ** database connection while the [sqlite3_last_insert_rowid()]
  2330 ** function is running and thus changes the last insert [rowid],
  2331 ** then the value returned by [sqlite3_last_insert_rowid()] is
  2332 ** unpredictable and might not equal either the old or the new
  2333 ** last insert [rowid].
  2334 */
  2335 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  2337 /*
  2338 ** CAPI3REF: Count The Number Of Rows Modified
  2339 **
  2340 ** ^This function returns the number of database rows that were changed
  2341 ** or inserted or deleted by the most recently completed SQL statement
  2342 ** on the [database connection] specified by the first parameter.
  2343 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
  2344 ** or [DELETE] statement are counted.  Auxiliary changes caused by
  2345 ** triggers or [foreign key actions] are not counted.)^ Use the
  2346 ** [sqlite3_total_changes()] function to find the total number of changes
  2347 ** including changes caused by triggers and foreign key actions.
  2348 **
  2349 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
  2350 ** are not counted.  Only real table changes are counted.
  2351 **
  2352 ** ^(A "row change" is a change to a single row of a single table
  2353 ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
  2354 ** are changed as side effects of [REPLACE] constraint resolution,
  2355 ** rollback, ABORT processing, [DROP TABLE], or by any other
  2356 ** mechanisms do not count as direct row changes.)^
  2357 **
  2358 ** A "trigger context" is a scope of execution that begins and
  2359 ** ends with the script of a [CREATE TRIGGER | trigger]. 
  2360 ** Most SQL statements are
  2361 ** evaluated outside of any trigger.  This is the "top level"
  2362 ** trigger context.  If a trigger fires from the top level, a
  2363 ** new trigger context is entered for the duration of that one
  2364 ** trigger.  Subtriggers create subcontexts for their duration.
  2365 **
  2366 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
  2367 ** not create a new trigger context.
  2368 **
  2369 ** ^This function returns the number of direct row changes in the
  2370 ** most recent INSERT, UPDATE, or DELETE statement within the same
  2371 ** trigger context.
  2372 **
  2373 ** ^Thus, when called from the top level, this function returns the
  2374 ** number of changes in the most recent INSERT, UPDATE, or DELETE
  2375 ** that also occurred at the top level.  ^(Within the body of a trigger,
  2376 ** the sqlite3_changes() interface can be called to find the number of
  2377 ** changes in the most recently completed INSERT, UPDATE, or DELETE
  2378 ** statement within the body of the same trigger.
  2379 ** However, the number returned does not include changes
  2380 ** caused by subtriggers since those have their own context.)^
  2381 **
  2382 ** See also the [sqlite3_total_changes()] interface, the
  2383 ** [count_changes pragma], and the [changes() SQL function].
  2384 **
  2385 ** If a separate thread makes changes on the same database connection
  2386 ** while [sqlite3_changes()] is running then the value returned
  2387 ** is unpredictable and not meaningful.
  2388 */
  2389 SQLITE_API int sqlite3_changes(sqlite3*);
  2391 /*
  2392 ** CAPI3REF: Total Number Of Rows Modified
  2393 **
  2394 ** ^This function returns the number of row changes caused by [INSERT],
  2395 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
  2396 ** ^(The count returned by sqlite3_total_changes() includes all changes
  2397 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
  2398 ** [foreign key actions]. However,
  2399 ** the count does not include changes used to implement [REPLACE] constraints,
  2400 ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
  2401 ** count does not include rows of views that fire an [INSTEAD OF trigger],
  2402 ** though if the INSTEAD OF trigger makes changes of its own, those changes 
  2403 ** are counted.)^
  2404 ** ^The sqlite3_total_changes() function counts the changes as soon as
  2405 ** the statement that makes them is completed (when the statement handle
  2406 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
  2407 **
  2408 ** See also the [sqlite3_changes()] interface, the
  2409 ** [count_changes pragma], and the [total_changes() SQL function].
  2410 **
  2411 ** If a separate thread makes changes on the same database connection
  2412 ** while [sqlite3_total_changes()] is running then the value
  2413 ** returned is unpredictable and not meaningful.
  2414 */
  2415 SQLITE_API int sqlite3_total_changes(sqlite3*);
  2417 /*
  2418 ** CAPI3REF: Interrupt A Long-Running Query
  2419 **
  2420 ** ^This function causes any pending database operation to abort and
  2421 ** return at its earliest opportunity. This routine is typically
  2422 ** called in response to a user action such as pressing "Cancel"
  2423 ** or Ctrl-C where the user wants a long query operation to halt
  2424 ** immediately.
  2425 **
  2426 ** ^It is safe to call this routine from a thread different from the
  2427 ** thread that is currently running the database operation.  But it
  2428 ** is not safe to call this routine with a [database connection] that
  2429 ** is closed or might close before sqlite3_interrupt() returns.
  2430 **
  2431 ** ^If an SQL operation is very nearly finished at the time when
  2432 ** sqlite3_interrupt() is called, then it might not have an opportunity
  2433 ** to be interrupted and might continue to completion.
  2434 **
  2435 ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
  2436 ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
  2437 ** that is inside an explicit transaction, then the entire transaction
  2438 ** will be rolled back automatically.
  2439 **
  2440 ** ^The sqlite3_interrupt(D) call is in effect until all currently running
  2441 ** SQL statements on [database connection] D complete.  ^Any new SQL statements
  2442 ** that are started after the sqlite3_interrupt() call and before the 
  2443 ** running statements reaches zero are interrupted as if they had been
  2444 ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
  2445 ** that are started after the running statement count reaches zero are
  2446 ** not effected by the sqlite3_interrupt().
  2447 ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
  2448 ** SQL statements is a no-op and has no effect on SQL statements
  2449 ** that are started after the sqlite3_interrupt() call returns.
  2450 **
  2451 ** If the database connection closes while [sqlite3_interrupt()]
  2452 ** is running then bad things will likely happen.
  2453 */
  2454 SQLITE_API void sqlite3_interrupt(sqlite3*);
  2456 /*
  2457 ** CAPI3REF: Determine If An SQL Statement Is Complete
  2458 **
  2459 ** These routines are useful during command-line input to determine if the
  2460 ** currently entered text seems to form a complete SQL statement or
  2461 ** if additional input is needed before sending the text into
  2462 ** SQLite for parsing.  ^These routines return 1 if the input string
  2463 ** appears to be a complete SQL statement.  ^A statement is judged to be
  2464 ** complete if it ends with a semicolon token and is not a prefix of a
  2465 ** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
  2466 ** string literals or quoted identifier names or comments are not
  2467 ** independent tokens (they are part of the token in which they are
  2468 ** embedded) and thus do not count as a statement terminator.  ^Whitespace
  2469 ** and comments that follow the final semicolon are ignored.
  2470 **
  2471 ** ^These routines return 0 if the statement is incomplete.  ^If a
  2472 ** memory allocation fails, then SQLITE_NOMEM is returned.
  2473 **
  2474 ** ^These routines do not parse the SQL statements thus
  2475 ** will not detect syntactically incorrect SQL.
  2476 **
  2477 ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior 
  2478 ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
  2479 ** automatically by sqlite3_complete16().  If that initialization fails,
  2480 ** then the return value from sqlite3_complete16() will be non-zero
  2481 ** regardless of whether or not the input SQL is complete.)^
  2482 **
  2483 ** The input to [sqlite3_complete()] must be a zero-terminated
  2484 ** UTF-8 string.
  2485 **
  2486 ** The input to [sqlite3_complete16()] must be a zero-terminated
  2487 ** UTF-16 string in native byte order.
  2488 */
  2489 SQLITE_API int sqlite3_complete(const char *sql);
  2490 SQLITE_API int sqlite3_complete16(const void *sql);
  2492 /*
  2493 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  2494 **
  2495 ** ^This routine sets a callback function that might be invoked whenever
  2496 ** an attempt is made to open a database table that another thread
  2497 ** or process has locked.
  2498 **
  2499 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
  2500 ** is returned immediately upon encountering the lock.  ^If the busy callback
  2501 ** is not NULL, then the callback might be invoked with two arguments.
  2502 **
  2503 ** ^The first argument to the busy handler is a copy of the void* pointer which
  2504 ** is the third argument to sqlite3_busy_handler().  ^The second argument to
  2505 ** the busy handler callback is the number of times that the busy handler has
  2506 ** been invoked for this locking event.  ^If the
  2507 ** busy callback returns 0, then no additional attempts are made to
  2508 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
  2509 ** ^If the callback returns non-zero, then another attempt
  2510 ** is made to open the database for reading and the cycle repeats.
  2511 **
  2512 ** The presence of a busy handler does not guarantee that it will be invoked
  2513 ** when there is lock contention. ^If SQLite determines that invoking the busy
  2514 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
  2515 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
  2516 ** Consider a scenario where one process is holding a read lock that
  2517 ** it is trying to promote to a reserved lock and
  2518 ** a second process is holding a reserved lock that it is trying
  2519 ** to promote to an exclusive lock.  The first process cannot proceed
  2520 ** because it is blocked by the second and the second process cannot
  2521 ** proceed because it is blocked by the first.  If both processes
  2522 ** invoke the busy handlers, neither will make any progress.  Therefore,
  2523 ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
  2524 ** will induce the first process to release its read lock and allow
  2525 ** the second process to proceed.
  2526 **
  2527 ** ^The default busy callback is NULL.
  2528 **
  2529 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
  2530 ** when SQLite is in the middle of a large transaction where all the
  2531 ** changes will not fit into the in-memory cache.  SQLite will
  2532 ** already hold a RESERVED lock on the database file, but it needs
  2533 ** to promote this lock to EXCLUSIVE so that it can spill cache
  2534 ** pages into the database file without harm to concurrent
  2535 ** readers.  ^If it is unable to promote the lock, then the in-memory
  2536 ** cache will be left in an inconsistent state and so the error
  2537 ** code is promoted from the relatively benign [SQLITE_BUSY] to
  2538 ** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
  2539 ** forces an automatic rollback of the changes.  See the
  2540 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
  2541 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
  2542 ** this is important.
  2543 **
  2544 ** ^(There can only be a single busy handler defined for each
  2545 ** [database connection].  Setting a new busy handler clears any
  2546 ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
  2547 ** will also set or clear the busy handler.
  2548 **
  2549 ** The busy callback should not take any actions which modify the
  2550 ** database connection that invoked the busy handler.  Any such actions
  2551 ** result in undefined behavior.
  2552 ** 
  2553 ** A busy handler must not close the database connection
  2554 ** or [prepared statement] that invoked the busy handler.
  2555 */
  2556 SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2558 /*
  2559 ** CAPI3REF: Set A Busy Timeout
  2560 **
  2561 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2562 ** for a specified amount of time when a table is locked.  ^The handler
  2563 ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2564 ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
  2565 ** the handler returns 0 which causes [sqlite3_step()] to return
  2566 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
  2567 **
  2568 ** ^Calling this routine with an argument less than or equal to zero
  2569 ** turns off all busy handlers.
  2570 **
  2571 ** ^(There can only be a single busy handler for a particular
  2572 ** [database connection] any any given moment.  If another busy handler
  2573 ** was defined  (using [sqlite3_busy_handler()]) prior to calling
  2574 ** this routine, that other busy handler is cleared.)^
  2575 */
  2576 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
  2578 /*
  2579 ** CAPI3REF: Convenience Routines For Running Queries
  2580 **
  2581 ** This is a legacy interface that is preserved for backwards compatibility.
  2582 ** Use of this interface is not recommended.
  2583 **
  2584 ** Definition: A <b>result table</b> is memory data structure created by the
  2585 ** [sqlite3_get_table()] interface.  A result table records the
  2586 ** complete query results from one or more queries.
  2587 **
  2588 ** The table conceptually has a number of rows and columns.  But
  2589 ** these numbers are not part of the result table itself.  These
  2590 ** numbers are obtained separately.  Let N be the number of rows
  2591 ** and M be the number of columns.
  2592 **
  2593 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
  2594 ** There are (N+1)*M elements in the array.  The first M pointers point
  2595 ** to zero-terminated strings that  contain the names of the columns.
  2596 ** The remaining entries all point to query results.  NULL values result
  2597 ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
  2598 ** string representation as returned by [sqlite3_column_text()].
  2599 **
  2600 ** A result table might consist of one or more memory allocations.
  2601 ** It is not safe to pass a result table directly to [sqlite3_free()].
  2602 ** A result table should be deallocated using [sqlite3_free_table()].
  2603 **
  2604 ** ^(As an example of the result table format, suppose a query result
  2605 ** is as follows:
  2606 **
  2607 ** <blockquote><pre>
  2608 **        Name        | Age
  2609 **        -----------------------
  2610 **        Alice       | 43
  2611 **        Bob         | 28
  2612 **        Cindy       | 21
  2613 ** </pre></blockquote>
  2614 **
  2615 ** There are two column (M==2) and three rows (N==3).  Thus the
  2616 ** result table has 8 entries.  Suppose the result table is stored
  2617 ** in an array names azResult.  Then azResult holds this content:
  2618 **
  2619 ** <blockquote><pre>
  2620 **        azResult&#91;0] = "Name";
  2621 **        azResult&#91;1] = "Age";
  2622 **        azResult&#91;2] = "Alice";
  2623 **        azResult&#91;3] = "43";
  2624 **        azResult&#91;4] = "Bob";
  2625 **        azResult&#91;5] = "28";
  2626 **        azResult&#91;6] = "Cindy";
  2627 **        azResult&#91;7] = "21";
  2628 ** </pre></blockquote>)^
  2629 **
  2630 ** ^The sqlite3_get_table() function evaluates one or more
  2631 ** semicolon-separated SQL statements in the zero-terminated UTF-8
  2632 ** string of its 2nd parameter and returns a result table to the
  2633 ** pointer given in its 3rd parameter.
  2634 **
  2635 ** After the application has finished with the result from sqlite3_get_table(),
  2636 ** it must pass the result table pointer to sqlite3_free_table() in order to
  2637 ** release the memory that was malloced.  Because of the way the
  2638 ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
  2639 ** function must not try to call [sqlite3_free()] directly.  Only
  2640 ** [sqlite3_free_table()] is able to release the memory properly and safely.
  2641 **
  2642 ** The sqlite3_get_table() interface is implemented as a wrapper around
  2643 ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
  2644 ** to any internal data structures of SQLite.  It uses only the public
  2645 ** interface defined here.  As a consequence, errors that occur in the
  2646 ** wrapper layer outside of the internal [sqlite3_exec()] call are not
  2647 ** reflected in subsequent calls to [sqlite3_errcode()] or
  2648 ** [sqlite3_errmsg()].
  2649 */
  2650 SQLITE_API int sqlite3_get_table(
  2651   sqlite3 *db,          /* An open database */
  2652   const char *zSql,     /* SQL to be evaluated */
  2653   char ***pazResult,    /* Results of the query */
  2654   int *pnRow,           /* Number of result rows written here */
  2655   int *pnColumn,        /* Number of result columns written here */
  2656   char **pzErrmsg       /* Error msg written here */
  2657 );
  2658 SQLITE_API void sqlite3_free_table(char **result);
  2660 /*
  2661 ** CAPI3REF: Formatted String Printing Functions
  2662 **
  2663 ** These routines are work-alikes of the "printf()" family of functions
  2664 ** from the standard C library.
  2665 **
  2666 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2667 ** results into memory obtained from [sqlite3_malloc()].
  2668 ** The strings returned by these two routines should be
  2669 ** released by [sqlite3_free()].  ^Both routines return a
  2670 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
  2671 ** memory to hold the resulting string.
  2672 **
  2673 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  2674 ** the standard C library.  The result is written into the
  2675 ** buffer supplied as the second parameter whose size is given by
  2676 ** the first parameter. Note that the order of the
  2677 ** first two parameters is reversed from snprintf().)^  This is an
  2678 ** historical accident that cannot be fixed without breaking
  2679 ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
  2680 ** returns a pointer to its buffer instead of the number of
  2681 ** characters actually written into the buffer.)^  We admit that
  2682 ** the number of characters written would be a more useful return
  2683 ** value but we cannot change the implementation of sqlite3_snprintf()
  2684 ** now without breaking compatibility.
  2685 **
  2686 ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
  2687 ** guarantees that the buffer is always zero-terminated.  ^The first
  2688 ** parameter "n" is the total size of the buffer, including space for
  2689 ** the zero terminator.  So the longest string that can be completely
  2690 ** written will be n-1 characters.
  2691 **
  2692 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  2693 **
  2694 ** These routines all implement some additional formatting
  2695 ** options that are useful for constructing SQL statements.
  2696 ** All of the usual printf() formatting options apply.  In addition, there
  2697 ** is are "%q", "%Q", and "%z" options.
  2698 **
  2699 ** ^(The %q option works like %s in that it substitutes a nul-terminated
  2700 ** string from the argument list.  But %q also doubles every '\'' character.
  2701 ** %q is designed for use inside a string literal.)^  By doubling each '\''
  2702 ** character it escapes that character and allows it to be inserted into
  2703 ** the string.
  2704 **
  2705 ** For example, assume the string variable zText contains text as follows:
  2706 **
  2707 ** <blockquote><pre>
  2708 **  char *zText = "It's a happy day!";
  2709 ** </pre></blockquote>
  2710 **
  2711 ** One can use this text in an SQL statement as follows:
  2712 **
  2713 ** <blockquote><pre>
  2714 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2715 **  sqlite3_exec(db, zSQL, 0, 0, 0);
  2716 **  sqlite3_free(zSQL);
  2717 ** </pre></blockquote>
  2718 **
  2719 ** Because the %q format string is used, the '\'' character in zText
  2720 ** is escaped and the SQL generated is as follows:
  2721 **
  2722 ** <blockquote><pre>
  2723 **  INSERT INTO table1 VALUES('It''s a happy day!')
  2724 ** </pre></blockquote>
  2725 **
  2726 ** This is correct.  Had we used %s instead of %q, the generated SQL
  2727 ** would have looked like this:
  2728 **
  2729 ** <blockquote><pre>
  2730 **  INSERT INTO table1 VALUES('It's a happy day!');
  2731 ** </pre></blockquote>
  2732 **
  2733 ** This second example is an SQL syntax error.  As a general rule you should
  2734 ** always use %q instead of %s when inserting text into a string literal.
  2735 **
  2736 ** ^(The %Q option works like %q except it also adds single quotes around
  2737 ** the outside of the total string.  Additionally, if the parameter in the
  2738 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2739 ** single quotes).)^  So, for example, one could say:
  2740 **
  2741 ** <blockquote><pre>
  2742 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2743 **  sqlite3_exec(db, zSQL, 0, 0, 0);
  2744 **  sqlite3_free(zSQL);
  2745 ** </pre></blockquote>
  2746 **
  2747 ** The code above will render a correct SQL statement in the zSQL
  2748 ** variable even if the zText variable is a NULL pointer.
  2749 **
  2750 ** ^(The "%z" formatting option works like "%s" but with the
  2751 ** addition that after the string has been read and copied into
  2752 ** the result, [sqlite3_free()] is called on the input string.)^
  2753 */
  2754 SQLITE_API char *sqlite3_mprintf(const char*,...);
  2755 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  2756 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  2757 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  2759 /*
  2760 ** CAPI3REF: Memory Allocation Subsystem
  2761 **
  2762 ** The SQLite core uses these three routines for all of its own
  2763 ** internal memory allocation needs. "Core" in the previous sentence
  2764 ** does not include operating-system specific VFS implementation.  The
  2765 ** Windows VFS uses native malloc() and free() for some operations.
  2766 **
  2767 ** ^The sqlite3_malloc() routine returns a pointer to a block
  2768 ** of memory at least N bytes in length, where N is the parameter.
  2769 ** ^If sqlite3_malloc() is unable to obtain sufficient free
  2770 ** memory, it returns a NULL pointer.  ^If the parameter N to
  2771 ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
  2772 ** a NULL pointer.
  2773 **
  2774 ** ^Calling sqlite3_free() with a pointer previously returned
  2775 ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
  2776 ** that it might be reused.  ^The sqlite3_free() routine is
  2777 ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
  2778 ** to sqlite3_free() is harmless.  After being freed, memory
  2779 ** should neither be read nor written.  Even reading previously freed
  2780 ** memory might result in a segmentation fault or other severe error.
  2781 ** Memory corruption, a segmentation fault, or other severe error
  2782 ** might result if sqlite3_free() is called with a non-NULL pointer that
  2783 ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
  2784 **
  2785 ** ^(The sqlite3_realloc() interface attempts to resize a
  2786 ** prior memory allocation to be at least N bytes, where N is the
  2787 ** second parameter.  The memory allocation to be resized is the first
  2788 ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
  2789 ** is a NULL pointer then its behavior is identical to calling
  2790 ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
  2791 ** ^If the second parameter to sqlite3_realloc() is zero or
  2792 ** negative then the behavior is exactly the same as calling
  2793 ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
  2794 ** ^sqlite3_realloc() returns a pointer to a memory allocation
  2795 ** of at least N bytes in size or NULL if sufficient memory is unavailable.
  2796 ** ^If M is the size of the prior allocation, then min(N,M) bytes
  2797 ** of the prior allocation are copied into the beginning of buffer returned
  2798 ** by sqlite3_realloc() and the prior allocation is freed.
  2799 ** ^If sqlite3_realloc() returns NULL, then the prior allocation
  2800 ** is not freed.
  2801 **
  2802 ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
  2803 ** is always aligned to at least an 8 byte boundary, or to a
  2804 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
  2805 ** option is used.
  2806 **
  2807 ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  2808 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  2809 ** implementation of these routines to be omitted.  That capability
  2810 ** is no longer provided.  Only built-in memory allocators can be used.
  2811 **
  2812 ** Prior to SQLite version 3.7.10, the Windows OS interface layer called
  2813 ** the system malloc() and free() directly when converting
  2814 ** filenames between the UTF-8 encoding used by SQLite
  2815 ** and whatever filename encoding is used by the particular Windows
  2816 ** installation.  Memory allocation errors were detected, but
  2817 ** they were reported back as [SQLITE_CANTOPEN] or
  2818 ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
  2819 **
  2820 ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
  2821 ** must be either NULL or else pointers obtained from a prior
  2822 ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
  2823 ** not yet been released.
  2824 **
  2825 ** The application must not read or write any part of
  2826 ** a block of memory after it has been released using
  2827 ** [sqlite3_free()] or [sqlite3_realloc()].
  2828 */
  2829 SQLITE_API void *sqlite3_malloc(int);
  2830 SQLITE_API void *sqlite3_realloc(void*, int);
  2831 SQLITE_API void sqlite3_free(void*);
  2833 /*
  2834 ** CAPI3REF: Memory Allocator Statistics
  2835 **
  2836 ** SQLite provides these two interfaces for reporting on the status
  2837 ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
  2838 ** routines, which form the built-in memory allocation subsystem.
  2839 **
  2840 ** ^The [sqlite3_memory_used()] routine returns the number of bytes
  2841 ** of memory currently outstanding (malloced but not freed).
  2842 ** ^The [sqlite3_memory_highwater()] routine returns the maximum
  2843 ** value of [sqlite3_memory_used()] since the high-water mark
  2844 ** was last reset.  ^The values returned by [sqlite3_memory_used()] and
  2845 ** [sqlite3_memory_highwater()] include any overhead
  2846 ** added by SQLite in its implementation of [sqlite3_malloc()],
  2847 ** but not overhead added by the any underlying system library
  2848 ** routines that [sqlite3_malloc()] may call.
  2849 **
  2850 ** ^The memory high-water mark is reset to the current value of
  2851 ** [sqlite3_memory_used()] if and only if the parameter to
  2852 ** [sqlite3_memory_highwater()] is true.  ^The value returned
  2853 ** by [sqlite3_memory_highwater(1)] is the high-water mark
  2854 ** prior to the reset.
  2855 */
  2856 SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
  2857 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
  2859 /*
  2860 ** CAPI3REF: Pseudo-Random Number Generator
  2861 **
  2862 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
  2863 ** select random [ROWID | ROWIDs] when inserting new records into a table that
  2864 ** already uses the largest possible [ROWID].  The PRNG is also used for
  2865 ** the build-in random() and randomblob() SQL functions.  This interface allows
  2866 ** applications to access the same PRNG for other purposes.
  2867 **
  2868 ** ^A call to this routine stores N bytes of randomness into buffer P.
  2869 **
  2870 ** ^The first time this routine is invoked (either internally or by
  2871 ** the application) the PRNG is seeded using randomness obtained
  2872 ** from the xRandomness method of the default [sqlite3_vfs] object.
  2873 ** ^On all subsequent invocations, the pseudo-randomness is generated
  2874 ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2875 ** method.
  2876 */
  2877 SQLITE_API void sqlite3_randomness(int N, void *P);
  2879 /*
  2880 ** CAPI3REF: Compile-Time Authorization Callbacks
  2881 **
  2882 ** ^This routine registers an authorizer callback with a particular
  2883 ** [database connection], supplied in the first argument.
  2884 ** ^The authorizer callback is invoked as SQL statements are being compiled
  2885 ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2886 ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
  2887 ** points during the compilation process, as logic is being created
  2888 ** to perform various actions, the authorizer callback is invoked to
  2889 ** see if those actions are allowed.  ^The authorizer callback should
  2890 ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
  2891 ** specific action but allow the SQL statement to continue to be
  2892 ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
  2893 ** rejected with an error.  ^If the authorizer callback returns
  2894 ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
  2895 ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
  2896 ** the authorizer will fail with an error message.
  2897 **
  2898 ** When the callback returns [SQLITE_OK], that means the operation
  2899 ** requested is ok.  ^When the callback returns [SQLITE_DENY], the
  2900 ** [sqlite3_prepare_v2()] or equivalent call that triggered the
  2901 ** authorizer will fail with an error message explaining that
  2902 ** access is denied. 
  2903 **
  2904 ** ^The first parameter to the authorizer callback is a copy of the third
  2905 ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
  2906 ** to the callback is an integer [SQLITE_COPY | action code] that specifies
  2907 ** the particular action to be authorized. ^The third through sixth parameters
  2908 ** to the callback are zero-terminated strings that contain additional
  2909 ** details about the action to be authorized.
  2910 **
  2911 ** ^If the action code is [SQLITE_READ]
  2912 ** and the callback returns [SQLITE_IGNORE] then the
  2913 ** [prepared statement] statement is constructed to substitute
  2914 ** a NULL value in place of the table column that would have
  2915 ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
  2916 ** return can be used to deny an untrusted user access to individual
  2917 ** columns of a table.
  2918 ** ^If the action code is [SQLITE_DELETE] and the callback returns
  2919 ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
  2920 ** [truncate optimization] is disabled and all rows are deleted individually.
  2921 **
  2922 ** An authorizer is used when [sqlite3_prepare | preparing]
  2923 ** SQL statements from an untrusted source, to ensure that the SQL statements
  2924 ** do not try to access data they are not allowed to see, or that they do not
  2925 ** try to execute malicious statements that damage the database.  For
  2926 ** example, an application may allow a user to enter arbitrary
  2927 ** SQL queries for evaluation by a database.  But the application does
  2928 ** not want the user to be able to make arbitrary changes to the
  2929 ** database.  An authorizer could then be put in place while the
  2930 ** user-entered SQL is being [sqlite3_prepare | prepared] that
  2931 ** disallows everything except [SELECT] statements.
  2932 **
  2933 ** Applications that need to process SQL from untrusted sources
  2934 ** might also consider lowering resource limits using [sqlite3_limit()]
  2935 ** and limiting database size using the [max_page_count] [PRAGMA]
  2936 ** in addition to using an authorizer.
  2937 **
  2938 ** ^(Only a single authorizer can be in place on a database connection
  2939 ** at a time.  Each call to sqlite3_set_authorizer overrides the
  2940 ** previous call.)^  ^Disable the authorizer by installing a NULL callback.
  2941 ** The authorizer is disabled by default.
  2942 **
  2943 ** The authorizer callback must not do anything that will modify
  2944 ** the database connection that invoked the authorizer callback.
  2945 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  2946 ** database connections for the meaning of "modify" in this paragraph.
  2947 **
  2948 ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
  2949 ** statement might be re-prepared during [sqlite3_step()] due to a 
  2950 ** schema change.  Hence, the application should ensure that the
  2951 ** correct authorizer callback remains in place during the [sqlite3_step()].
  2952 **
  2953 ** ^Note that the authorizer callback is invoked only during
  2954 ** [sqlite3_prepare()] or its variants.  Authorization is not
  2955 ** performed during statement evaluation in [sqlite3_step()], unless
  2956 ** as stated in the previous paragraph, sqlite3_step() invokes
  2957 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
  2958 */
  2959 SQLITE_API int sqlite3_set_authorizer(
  2960   sqlite3*,
  2961   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
  2962   void *pUserData
  2963 );
  2965 /*
  2966 ** CAPI3REF: Authorizer Return Codes
  2967 **
  2968 ** The [sqlite3_set_authorizer | authorizer callback function] must
  2969 ** return either [SQLITE_OK] or one of these two constants in order
  2970 ** to signal SQLite whether or not the action is permitted.  See the
  2971 ** [sqlite3_set_authorizer | authorizer documentation] for additional
  2972 ** information.
  2973 **
  2974 ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
  2975 ** from the [sqlite3_vtab_on_conflict()] interface.
  2976 */
  2977 #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
  2978 #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
  2980 /*
  2981 ** CAPI3REF: Authorizer Action Codes
  2982 **
  2983 ** The [sqlite3_set_authorizer()] interface registers a callback function
  2984 ** that is invoked to authorize certain SQL statement actions.  The
  2985 ** second parameter to the callback is an integer code that specifies
  2986 ** what action is being authorized.  These are the integer action codes that
  2987 ** the authorizer callback may be passed.
  2988 **
  2989 ** These action code values signify what kind of operation is to be
  2990 ** authorized.  The 3rd and 4th parameters to the authorization
  2991 ** callback function will be parameters or NULL depending on which of these
  2992 ** codes is used as the second parameter.  ^(The 5th parameter to the
  2993 ** authorizer callback is the name of the database ("main", "temp",
  2994 ** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
  2995 ** is the name of the inner-most trigger or view that is responsible for
  2996 ** the access attempt or NULL if this access attempt is directly from
  2997 ** top-level SQL code.
  2998 */
  2999 /******************************************* 3rd ************ 4th ***********/
  3000 #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
  3001 #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
  3002 #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
  3003 #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
  3004 #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
  3005 #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
  3006 #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
  3007 #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
  3008 #define SQLITE_DELETE                9   /* Table Name      NULL            */
  3009 #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
  3010 #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
  3011 #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
  3012 #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
  3013 #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
  3014 #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
  3015 #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
  3016 #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
  3017 #define SQLITE_INSERT               18   /* Table Name      NULL            */
  3018 #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
  3019 #define SQLITE_READ                 20   /* Table Name      Column Name     */
  3020 #define SQLITE_SELECT               21   /* NULL            NULL            */
  3021 #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
  3022 #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
  3023 #define SQLITE_ATTACH               24   /* Filename        NULL            */
  3024 #define SQLITE_DETACH               25   /* Database Name   NULL            */
  3025 #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
  3026 #define SQLITE_REINDEX              27   /* Index Name      NULL            */
  3027 #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
  3028 #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
  3029 #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
  3030 #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
  3031 #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  3032 #define SQLITE_COPY                  0   /* No longer used */
  3034 /*
  3035 ** CAPI3REF: Tracing And Profiling Functions
  3036 **
  3037 ** These routines register callback functions that can be used for
  3038 ** tracing and profiling the execution of SQL statements.
  3039 **
  3040 ** ^The callback function registered by sqlite3_trace() is invoked at
  3041 ** various times when an SQL statement is being run by [sqlite3_step()].
  3042 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
  3043 ** SQL statement text as the statement first begins executing.
  3044 ** ^(Additional sqlite3_trace() callbacks might occur
  3045 ** as each triggered subprogram is entered.  The callbacks for triggers
  3046 ** contain a UTF-8 SQL comment that identifies the trigger.)^
  3047 **
  3048 ** ^The callback function registered by sqlite3_profile() is invoked
  3049 ** as each SQL statement finishes.  ^The profile callback contains
  3050 ** the original statement text and an estimate of wall-clock time
  3051 ** of how long that statement took to run.  ^The profile callback
  3052 ** time is in units of nanoseconds, however the current implementation
  3053 ** is only capable of millisecond resolution so the six least significant
  3054 ** digits in the time are meaningless.  Future versions of SQLite
  3055 ** might provide greater resolution on the profiler callback.  The
  3056 ** sqlite3_profile() function is considered experimental and is
  3057 ** subject to change in future versions of SQLite.
  3058 */
  3059 SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  3060 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
  3061    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  3063 /*
  3064 ** CAPI3REF: Query Progress Callbacks
  3065 **
  3066 ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  3067 ** function X to be invoked periodically during long running calls to
  3068 ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  3069 ** database connection D.  An example use for this
  3070 ** interface is to keep a GUI updated during a large query.
  3071 **
  3072 ** ^The parameter P is passed through as the only parameter to the 
  3073 ** callback function X.  ^The parameter N is the number of 
  3074 ** [virtual machine instructions] that are evaluated between successive
  3075 ** invocations of the callback X.
  3076 **
  3077 ** ^Only a single progress handler may be defined at one time per
  3078 ** [database connection]; setting a new progress handler cancels the
  3079 ** old one.  ^Setting parameter X to NULL disables the progress handler.
  3080 ** ^The progress handler is also disabled by setting N to a value less
  3081 ** than 1.
  3082 **
  3083 ** ^If the progress callback returns non-zero, the operation is
  3084 ** interrupted.  This feature can be used to implement a
  3085 ** "Cancel" button on a GUI progress dialog box.
  3086 **
  3087 ** The progress handler callback must not do anything that will modify
  3088 ** the database connection that invoked the progress handler.
  3089 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  3090 ** database connections for the meaning of "modify" in this paragraph.
  3091 **
  3092 */
  3093 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  3095 /*
  3096 ** CAPI3REF: Opening A New Database Connection
  3097 **
  3098 ** ^These routines open an SQLite database file as specified by the 
  3099 ** filename argument. ^The filename argument is interpreted as UTF-8 for
  3100 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  3101 ** order for sqlite3_open16(). ^(A [database connection] handle is usually
  3102 ** returned in *ppDb, even if an error occurs.  The only exception is that
  3103 ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
  3104 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
  3105 ** object.)^ ^(If the database is opened (and/or created) successfully, then
  3106 ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
  3107 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
  3108 ** an English language description of the error following a failure of any
  3109 ** of the sqlite3_open() routines.
  3110 **
  3111 ** ^The default encoding for the database will be UTF-8 if
  3112 ** sqlite3_open() or sqlite3_open_v2() is called and
  3113 ** UTF-16 in the native byte order if sqlite3_open16() is used.
  3114 **
  3115 ** Whether or not an error occurs when it is opened, resources
  3116 ** associated with the [database connection] handle should be released by
  3117 ** passing it to [sqlite3_close()] when it is no longer required.
  3118 **
  3119 ** The sqlite3_open_v2() interface works like sqlite3_open()
  3120 ** except that it accepts two additional parameters for additional control
  3121 ** over the new database connection.  ^(The flags parameter to
  3122 ** sqlite3_open_v2() can take one of
  3123 ** the following three values, optionally combined with the 
  3124 ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
  3125 ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
  3126 **
  3127 ** <dl>
  3128 ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
  3129 ** <dd>The database is opened in read-only mode.  If the database does not
  3130 ** already exist, an error is returned.</dd>)^
  3131 **
  3132 ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
  3133 ** <dd>The database is opened for reading and writing if possible, or reading
  3134 ** only if the file is write protected by the operating system.  In either
  3135 ** case the database must already exist, otherwise an error is returned.</dd>)^
  3136 **
  3137 ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
  3138 ** <dd>The database is opened for reading and writing, and is created if
  3139 ** it does not already exist. This is the behavior that is always used for
  3140 ** sqlite3_open() and sqlite3_open16().</dd>)^
  3141 ** </dl>
  3142 **
  3143 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
  3144 ** combinations shown above optionally combined with other
  3145 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
  3146 ** then the behavior is undefined.
  3147 **
  3148 ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
  3149 ** opens in the multi-thread [threading mode] as long as the single-thread
  3150 ** mode has not been set at compile-time or start-time.  ^If the
  3151 ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
  3152 ** in the serialized [threading mode] unless single-thread was
  3153 ** previously selected at compile-time or start-time.
  3154 ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
  3155 ** eligible to use [shared cache mode], regardless of whether or not shared
  3156 ** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
  3157 ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
  3158 ** participate in [shared cache mode] even if it is enabled.
  3159 **
  3160 ** ^The fourth parameter to sqlite3_open_v2() is the name of the
  3161 ** [sqlite3_vfs] object that defines the operating system interface that
  3162 ** the new database connection should use.  ^If the fourth parameter is
  3163 ** a NULL pointer then the default [sqlite3_vfs] object is used.
  3164 **
  3165 ** ^If the filename is ":memory:", then a private, temporary in-memory database
  3166 ** is created for the connection.  ^This in-memory database will vanish when
  3167 ** the database connection is closed.  Future versions of SQLite might
  3168 ** make use of additional special filenames that begin with the ":" character.
  3169 ** It is recommended that when a database filename actually does begin with
  3170 ** a ":" character you should prefix the filename with a pathname such as
  3171 ** "./" to avoid ambiguity.
  3172 **
  3173 ** ^If the filename is an empty string, then a private, temporary
  3174 ** on-disk database will be created.  ^This private database will be
  3175 ** automatically deleted as soon as the database connection is closed.
  3176 **
  3177 ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
  3178 **
  3179 ** ^If [URI filename] interpretation is enabled, and the filename argument
  3180 ** begins with "file:", then the filename is interpreted as a URI. ^URI
  3181 ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
  3182 ** set in the fourth argument to sqlite3_open_v2(), or if it has
  3183 ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
  3184 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
  3185 ** As of SQLite version 3.7.7, URI filename interpretation is turned off
  3186 ** by default, but future releases of SQLite might enable URI filename
  3187 ** interpretation by default.  See "[URI filenames]" for additional
  3188 ** information.
  3189 **
  3190 ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
  3191 ** authority, then it must be either an empty string or the string 
  3192 ** "localhost". ^If the authority is not an empty string or "localhost", an 
  3193 ** error is returned to the caller. ^The fragment component of a URI, if 
  3194 ** present, is ignored.
  3195 **
  3196 ** ^SQLite uses the path component of the URI as the name of the disk file
  3197 ** which contains the database. ^If the path begins with a '/' character, 
  3198 ** then it is interpreted as an absolute path. ^If the path does not begin 
  3199 ** with a '/' (meaning that the authority section is omitted from the URI)
  3200 ** then the path is interpreted as a relative path. 
  3201 ** ^On windows, the first component of an absolute path 
  3202 ** is a drive specification (e.g. "C:").
  3203 **
  3204 ** [[core URI query parameters]]
  3205 ** The query component of a URI may contain parameters that are interpreted
  3206 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
  3207 ** SQLite interprets the following three query parameters:
  3208 **
  3209 ** <ul>
  3210 **   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
  3211 **     a VFS object that provides the operating system interface that should
  3212 **     be used to access the database file on disk. ^If this option is set to
  3213 **     an empty string the default VFS object is used. ^Specifying an unknown
  3214 **     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
  3215 **     present, then the VFS specified by the option takes precedence over
  3216 **     the value passed as the fourth parameter to sqlite3_open_v2().
  3217 **
  3218 **   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
  3219 **     "rwc", or "memory". Attempting to set it to any other value is
  3220 **     an error)^. 
  3221 **     ^If "ro" is specified, then the database is opened for read-only 
  3222 **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 
  3223 **     third argument to sqlite3_open_v2(). ^If the mode option is set to 
  3224 **     "rw", then the database is opened for read-write (but not create) 
  3225 **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 
  3226 **     been set. ^Value "rwc" is equivalent to setting both 
  3227 **     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
  3228 **     set to "memory" then a pure [in-memory database] that never reads
  3229 **     or writes from disk is used. ^It is an error to specify a value for
  3230 **     the mode parameter that is less restrictive than that specified by
  3231 **     the flags passed in the third parameter to sqlite3_open_v2().
  3232 **
  3233 **   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
  3234 **     "private". ^Setting it to "shared" is equivalent to setting the
  3235 **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
  3236 **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is 
  3237 **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
  3238 **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
  3239 **     a URI filename, its value overrides any behaviour requested by setting
  3240 **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
  3241 ** </ul>
  3242 **
  3243 ** ^Specifying an unknown parameter in the query component of a URI is not an
  3244 ** error.  Future versions of SQLite might understand additional query
  3245 ** parameters.  See "[query parameters with special meaning to SQLite]" for
  3246 ** additional information.
  3247 **
  3248 ** [[URI filename examples]] <h3>URI filename examples</h3>
  3249 **
  3250 ** <table border="1" align=center cellpadding=5>
  3251 ** <tr><th> URI filenames <th> Results
  3252 ** <tr><td> file:data.db <td> 
  3253 **          Open the file "data.db" in the current directory.
  3254 ** <tr><td> file:/home/fred/data.db<br>
  3255 **          file:///home/fred/data.db <br> 
  3256 **          file://localhost/home/fred/data.db <br> <td> 
  3257 **          Open the database file "/home/fred/data.db".
  3258 ** <tr><td> file://darkstar/home/fred/data.db <td> 
  3259 **          An error. "darkstar" is not a recognized authority.
  3260 ** <tr><td style="white-space:nowrap"> 
  3261 **          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
  3262 **     <td> Windows only: Open the file "data.db" on fred's desktop on drive
  3263 **          C:. Note that the %20 escaping in this example is not strictly 
  3264 **          necessary - space characters can be used literally
  3265 **          in URI filenames.
  3266 ** <tr><td> file:data.db?mode=ro&cache=private <td> 
  3267 **          Open file "data.db" in the current directory for read-only access.
  3268 **          Regardless of whether or not shared-cache mode is enabled by
  3269 **          default, use a private cache.
  3270 ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
  3271 **          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
  3272 ** <tr><td> file:data.db?mode=readonly <td> 
  3273 **          An error. "readonly" is not a valid option for the "mode" parameter.
  3274 ** </table>
  3275 **
  3276 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
  3277 ** query components of a URI. A hexadecimal escape sequence consists of a
  3278 ** percent sign - "%" - followed by exactly two hexadecimal digits 
  3279 ** specifying an octet value. ^Before the path or query components of a
  3280 ** URI filename are interpreted, they are encoded using UTF-8 and all 
  3281 ** hexadecimal escape sequences replaced by a single byte containing the
  3282 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
  3283 ** the results are undefined.
  3284 **
  3285 ** <b>Note to Windows users:</b>  The encoding used for the filename argument
  3286 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
  3287 ** codepage is currently defined.  Filenames containing international
  3288 ** characters must be converted to UTF-8 prior to passing them into
  3289 ** sqlite3_open() or sqlite3_open_v2().
  3290 **
  3291 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
  3292 ** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
  3293 ** features that require the use of temporary files may fail.
  3294 **
  3295 ** See also: [sqlite3_temp_directory]
  3296 */
  3297 SQLITE_API int sqlite3_open(
  3298   const char *filename,   /* Database filename (UTF-8) */
  3299   sqlite3 **ppDb          /* OUT: SQLite db handle */
  3300 );
  3301 SQLITE_API int sqlite3_open16(
  3302   const void *filename,   /* Database filename (UTF-16) */
  3303   sqlite3 **ppDb          /* OUT: SQLite db handle */
  3304 );
  3305 SQLITE_API int sqlite3_open_v2(
  3306   const char *filename,   /* Database filename (UTF-8) */
  3307   sqlite3 **ppDb,         /* OUT: SQLite db handle */
  3308   int flags,              /* Flags */
  3309   const char *zVfs        /* Name of VFS module to use */
  3310 );
  3312 /*
  3313 ** CAPI3REF: Obtain Values For URI Parameters
  3314 **
  3315 ** These are utility routines, useful to VFS implementations, that check
  3316 ** to see if a database file was a URI that contained a specific query 
  3317 ** parameter, and if so obtains the value of that query parameter.
  3318 **
  3319 ** If F is the database filename pointer passed into the xOpen() method of 
  3320 ** a VFS implementation when the flags parameter to xOpen() has one or 
  3321 ** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
  3322 ** P is the name of the query parameter, then
  3323 ** sqlite3_uri_parameter(F,P) returns the value of the P
  3324 ** parameter if it exists or a NULL pointer if P does not appear as a 
  3325 ** query parameter on F.  If P is a query parameter of F
  3326 ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
  3327 ** a pointer to an empty string.
  3328 **
  3329 ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
  3330 ** parameter and returns true (1) or false (0) according to the value
  3331 ** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
  3332 ** value of query parameter P is one of "yes", "true", or "on" in any
  3333 ** case or if the value begins with a non-zero number.  The 
  3334 ** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
  3335 ** query parameter P is one of "no", "false", or "off" in any case or
  3336 ** if the value begins with a numeric zero.  If P is not a query
  3337 ** parameter on F or if the value of P is does not match any of the
  3338 ** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
  3339 **
  3340 ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
  3341 ** 64-bit signed integer and returns that integer, or D if P does not
  3342 ** exist.  If the value of P is something other than an integer, then
  3343 ** zero is returned.
  3344 ** 
  3345 ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
  3346 ** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
  3347 ** is not a database file pathname pointer that SQLite passed into the xOpen
  3348 ** VFS method, then the behavior of this routine is undefined and probably
  3349 ** undesirable.
  3350 */
  3351 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  3352 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  3353 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  3356 /*
  3357 ** CAPI3REF: Error Codes And Messages
  3358 **
  3359 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
  3360 ** [extended result code] for the most recent failed sqlite3_* API call
  3361 ** associated with a [database connection]. If a prior API call failed
  3362 ** but the most recent API call succeeded, the return value from
  3363 ** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
  3364 ** interface is the same except that it always returns the 
  3365 ** [extended result code] even when extended result codes are
  3366 ** disabled.
  3367 **
  3368 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
  3369 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
  3370 ** ^(Memory to hold the error message string is managed internally.
  3371 ** The application does not need to worry about freeing the result.
  3372 ** However, the error string might be overwritten or deallocated by
  3373 ** subsequent calls to other SQLite interface functions.)^
  3374 **
  3375 ** ^The sqlite3_errstr() interface returns the English-language text
  3376 ** that describes the [result code], as UTF-8.
  3377 ** ^(Memory to hold the error message string is managed internally
  3378 ** and must not be freed by the application)^.
  3379 **
  3380 ** When the serialized [threading mode] is in use, it might be the
  3381 ** case that a second error occurs on a separate thread in between
  3382 ** the time of the first error and the call to these interfaces.
  3383 ** When that happens, the second error will be reported since these
  3384 ** interfaces always report the most recent result.  To avoid
  3385 ** this, each thread can obtain exclusive use of the [database connection] D
  3386 ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
  3387 ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
  3388 ** all calls to the interfaces listed here are completed.
  3389 **
  3390 ** If an interface fails with SQLITE_MISUSE, that means the interface
  3391 ** was invoked incorrectly by the application.  In that case, the
  3392 ** error code and message may or may not be set.
  3393 */
  3394 SQLITE_API int sqlite3_errcode(sqlite3 *db);
  3395 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
  3396 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
  3397 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
  3398 SQLITE_API const char *sqlite3_errstr(int);
  3400 /*
  3401 ** CAPI3REF: SQL Statement Object
  3402 ** KEYWORDS: {prepared statement} {prepared statements}
  3403 **
  3404 ** An instance of this object represents a single SQL statement.
  3405 ** This object is variously known as a "prepared statement" or a
  3406 ** "compiled SQL statement" or simply as a "statement".
  3407 **
  3408 ** The life of a statement object goes something like this:
  3409 **
  3410 ** <ol>
  3411 ** <li> Create the object using [sqlite3_prepare_v2()] or a related
  3412 **      function.
  3413 ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
  3414 **      interfaces.
  3415 ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  3416 ** <li> Reset the statement using [sqlite3_reset()] then go back
  3417 **      to step 2.  Do this zero or more times.
  3418 ** <li> Destroy the object using [sqlite3_finalize()].
  3419 ** </ol>
  3420 **
  3421 ** Refer to documentation on individual methods above for additional
  3422 ** information.
  3423 */
  3424 typedef struct sqlite3_stmt sqlite3_stmt;
  3426 /*
  3427 ** CAPI3REF: Run-time Limits
  3428 **
  3429 ** ^(This interface allows the size of various constructs to be limited
  3430 ** on a connection by connection basis.  The first parameter is the
  3431 ** [database connection] whose limit is to be set or queried.  The
  3432 ** second parameter is one of the [limit categories] that define a
  3433 ** class of constructs to be size limited.  The third parameter is the
  3434 ** new limit for that construct.)^
  3435 **
  3436 ** ^If the new limit is a negative number, the limit is unchanged.
  3437 ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a 
  3438 ** [limits | hard upper bound]
  3439 ** set at compile-time by a C preprocessor macro called
  3440 ** [limits | SQLITE_MAX_<i>NAME</i>].
  3441 ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
  3442 ** ^Attempts to increase a limit above its hard upper bound are
  3443 ** silently truncated to the hard upper bound.
  3444 **
  3445 ** ^Regardless of whether or not the limit was changed, the 
  3446 ** [sqlite3_limit()] interface returns the prior value of the limit.
  3447 ** ^Hence, to find the current value of a limit without changing it,
  3448 ** simply invoke this interface with the third parameter set to -1.
  3449 **
  3450 ** Run-time limits are intended for use in applications that manage
  3451 ** both their own internal database and also databases that are controlled
  3452 ** by untrusted external sources.  An example application might be a
  3453 ** web browser that has its own databases for storing history and
  3454 ** separate databases controlled by JavaScript applications downloaded
  3455 ** off the Internet.  The internal databases can be given the
  3456 ** large, default limits.  Databases managed by external sources can
  3457 ** be given much smaller limits designed to prevent a denial of service
  3458 ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
  3459 ** interface to further control untrusted SQL.  The size of the database
  3460 ** created by an untrusted script can be contained using the
  3461 ** [max_page_count] [PRAGMA].
  3462 **
  3463 ** New run-time limit categories may be added in future releases.
  3464 */
  3465 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
  3467 /*
  3468 ** CAPI3REF: Run-Time Limit Categories
  3469 ** KEYWORDS: {limit category} {*limit categories}
  3470 **
  3471 ** These constants define various performance limits
  3472 ** that can be lowered at run-time using [sqlite3_limit()].
  3473 ** The synopsis of the meanings of the various limits is shown below.
  3474 ** Additional information is available at [limits | Limits in SQLite].
  3475 **
  3476 ** <dl>
  3477 ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
  3478 ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
  3479 **
  3480 ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
  3481 ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
  3482 **
  3483 ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
  3484 ** <dd>The maximum number of columns in a table definition or in the
  3485 ** result set of a [SELECT] or the maximum number of columns in an index
  3486 ** or in an ORDER BY or GROUP BY clause.</dd>)^
  3487 **
  3488 ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
  3489 ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  3490 **
  3491 ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  3492 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  3493 **
  3494 ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  3495 ** <dd>The maximum number of instructions in a virtual machine program
  3496 ** used to implement an SQL statement.  This limit is not currently
  3497 ** enforced, though that might be added in some future release of
  3498 ** SQLite.</dd>)^
  3499 **
  3500 ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  3501 ** <dd>The maximum number of arguments on a function.</dd>)^
  3502 **
  3503 ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  3504 ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  3505 **
  3506 ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
  3507 ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
  3508 ** <dd>The maximum length of the pattern argument to the [LIKE] or
  3509 ** [GLOB] operators.</dd>)^
  3510 **
  3511 ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
  3512 ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
  3513 ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
  3514 **
  3515 ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
  3516 ** <dd>The maximum depth of recursion for triggers.</dd>)^
  3517 ** </dl>
  3518 */
  3519 #define SQLITE_LIMIT_LENGTH                    0
  3520 #define SQLITE_LIMIT_SQL_LENGTH                1
  3521 #define SQLITE_LIMIT_COLUMN                    2
  3522 #define SQLITE_LIMIT_EXPR_DEPTH                3
  3523 #define SQLITE_LIMIT_COMPOUND_SELECT           4
  3524 #define SQLITE_LIMIT_VDBE_OP                   5
  3525 #define SQLITE_LIMIT_FUNCTION_ARG              6
  3526 #define SQLITE_LIMIT_ATTACHED                  7
  3527 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
  3528 #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3529 #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3531 /*
  3532 ** CAPI3REF: Compiling An SQL Statement
  3533 ** KEYWORDS: {SQL statement compiler}
  3534 **
  3535 ** To execute an SQL query, it must first be compiled into a byte-code
  3536 ** program using one of these routines.
  3537 **
  3538 ** The first argument, "db", is a [database connection] obtained from a
  3539 ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  3540 ** [sqlite3_open16()].  The database connection must not have been closed.
  3541 **
  3542 ** The second argument, "zSql", is the statement to be compiled, encoded
  3543 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
  3544 ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
  3545 ** use UTF-16.
  3546 **
  3547 ** ^If the nByte argument is less than zero, then zSql is read up to the
  3548 ** first zero terminator. ^If nByte is non-negative, then it is the maximum
  3549 ** number of  bytes read from zSql.  ^When nByte is non-negative, the
  3550 ** zSql string ends at either the first '\000' or '\u0000' character or
  3551 ** the nByte-th byte, whichever comes first. If the caller knows
  3552 ** that the supplied string is nul-terminated, then there is a small
  3553 ** performance advantage to be gained by passing an nByte parameter that
  3554 ** is equal to the number of bytes in the input string <i>including</i>
  3555 ** the nul-terminator bytes as this saves SQLite from having to
  3556 ** make a copy of the input string.
  3557 **
  3558 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
  3559 ** past the end of the first SQL statement in zSql.  These routines only
  3560 ** compile the first statement in zSql, so *pzTail is left pointing to
  3561 ** what remains uncompiled.
  3562 **
  3563 ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
  3564 ** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
  3565 ** to NULL.  ^If the input text contains no SQL (if the input is an empty
  3566 ** string or a comment) then *ppStmt is set to NULL.
  3567 ** The calling procedure is responsible for deleting the compiled
  3568 ** SQL statement using [sqlite3_finalize()] after it has finished with it.
  3569 ** ppStmt may not be NULL.
  3570 **
  3571 ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
  3572 ** otherwise an [error code] is returned.
  3573 **
  3574 ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
  3575 ** recommended for all new programs. The two older interfaces are retained
  3576 ** for backwards compatibility, but their use is discouraged.
  3577 ** ^In the "v2" interfaces, the prepared statement
  3578 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
  3579 ** original SQL text. This causes the [sqlite3_step()] interface to
  3580 ** behave differently in three ways:
  3581 **
  3582 ** <ol>
  3583 ** <li>
  3584 ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
  3585 ** always used to do, [sqlite3_step()] will automatically recompile the SQL
  3586 ** statement and try to run it again.
  3587 ** </li>
  3588 **
  3589 ** <li>
  3590 ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
  3591 ** [error codes] or [extended error codes].  ^The legacy behavior was that
  3592 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
  3593 ** and the application would have to make a second call to [sqlite3_reset()]
  3594 ** in order to find the underlying cause of the problem. With the "v2" prepare
  3595 ** interfaces, the underlying reason for the error is returned immediately.
  3596 ** </li>
  3597 **
  3598 ** <li>
  3599 ** ^If the specific value bound to [parameter | host parameter] in the 
  3600 ** WHERE clause might influence the choice of query plan for a statement,
  3601 ** then the statement will be automatically recompiled, as if there had been 
  3602 ** a schema change, on the first  [sqlite3_step()] call following any change
  3603 ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3604 ** ^The specific value of WHERE-clause [parameter] might influence the 
  3605 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3606 ** or [GLOB] operator or if the parameter is compared to an indexed column
  3607 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3608 ** the 
  3609 ** </li>
  3610 ** </ol>
  3611 */
  3612 SQLITE_API int sqlite3_prepare(
  3613   sqlite3 *db,            /* Database handle */
  3614   const char *zSql,       /* SQL statement, UTF-8 encoded */
  3615   int nByte,              /* Maximum length of zSql in bytes. */
  3616   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3617   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
  3618 );
  3619 SQLITE_API int sqlite3_prepare_v2(
  3620   sqlite3 *db,            /* Database handle */
  3621   const char *zSql,       /* SQL statement, UTF-8 encoded */
  3622   int nByte,              /* Maximum length of zSql in bytes. */
  3623   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3624   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
  3625 );
  3626 SQLITE_API int sqlite3_prepare16(
  3627   sqlite3 *db,            /* Database handle */
  3628   const void *zSql,       /* SQL statement, UTF-16 encoded */
  3629   int nByte,              /* Maximum length of zSql in bytes. */
  3630   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3631   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3632 );
  3633 SQLITE_API int sqlite3_prepare16_v2(
  3634   sqlite3 *db,            /* Database handle */
  3635   const void *zSql,       /* SQL statement, UTF-16 encoded */
  3636   int nByte,              /* Maximum length of zSql in bytes. */
  3637   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3638   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3639 );
  3641 /*
  3642 ** CAPI3REF: Retrieving Statement SQL
  3643 **
  3644 ** ^This interface can be used to retrieve a saved copy of the original
  3645 ** SQL text used to create a [prepared statement] if that statement was
  3646 ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  3647 */
  3648 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3650 /*
  3651 ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3652 **
  3653 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3654 ** and only if the [prepared statement] X makes no direct changes to
  3655 ** the content of the database file.
  3656 **
  3657 ** Note that [application-defined SQL functions] or
  3658 ** [virtual tables] might change the database indirectly as a side effect.  
  3659 ** ^(For example, if an application defines a function "eval()" that 
  3660 ** calls [sqlite3_exec()], then the following SQL statement would
  3661 ** change the database file through side-effects:
  3662 **
  3663 ** <blockquote><pre>
  3664 **    SELECT eval('DELETE FROM t1') FROM t2;
  3665 ** </pre></blockquote>
  3666 **
  3667 ** But because the [SELECT] statement does not change the database file
  3668 ** directly, sqlite3_stmt_readonly() would still return true.)^
  3669 **
  3670 ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
  3671 ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
  3672 ** since the statements themselves do not actually modify the database but
  3673 ** rather they control the timing of when other statements modify the 
  3674 ** database.  ^The [ATTACH] and [DETACH] statements also cause
  3675 ** sqlite3_stmt_readonly() to return true since, while those statements
  3676 ** change the configuration of a database connection, they do not make 
  3677 ** changes to the content of the database files on disk.
  3678 */
  3679 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  3681 /*
  3682 ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
  3683 **
  3684 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  3685 ** [prepared statement] S has been stepped at least once using 
  3686 ** [sqlite3_step(S)] but has not run to completion and/or has not 
  3687 ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
  3688 ** interface returns false if S is a NULL pointer.  If S is not a 
  3689 ** NULL pointer and is not a pointer to a valid [prepared statement]
  3690 ** object, then the behavior is undefined and probably undesirable.
  3691 **
  3692 ** This interface can be used in combination [sqlite3_next_stmt()]
  3693 ** to locate all prepared statements associated with a database 
  3694 ** connection that are in need of being reset.  This can be used,
  3695 ** for example, in diagnostic routines to search for prepared 
  3696 ** statements that are holding a transaction open.
  3697 */
  3698 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
  3700 /*
  3701 ** CAPI3REF: Dynamically Typed Value Object
  3702 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
  3703 **
  3704 ** SQLite uses the sqlite3_value object to represent all values
  3705 ** that can be stored in a database table. SQLite uses dynamic typing
  3706 ** for the values it stores.  ^Values stored in sqlite3_value objects
  3707 ** can be integers, floating point values, strings, BLOBs, or NULL.
  3708 **
  3709 ** An sqlite3_value object may be either "protected" or "unprotected".
  3710 ** Some interfaces require a protected sqlite3_value.  Other interfaces
  3711 ** will accept either a protected or an unprotected sqlite3_value.
  3712 ** Every interface that accepts sqlite3_value arguments specifies
  3713 ** whether or not it requires a protected sqlite3_value.
  3714 **
  3715 ** The terms "protected" and "unprotected" refer to whether or not
  3716 ** a mutex is held.  An internal mutex is held for a protected
  3717 ** sqlite3_value object but no mutex is held for an unprotected
  3718 ** sqlite3_value object.  If SQLite is compiled to be single-threaded
  3719 ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
  3720 ** or if SQLite is run in one of reduced mutex modes 
  3721 ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
  3722 ** then there is no distinction between protected and unprotected
  3723 ** sqlite3_value objects and they can be used interchangeably.  However,
  3724 ** for maximum code portability it is recommended that applications
  3725 ** still make the distinction between protected and unprotected
  3726 ** sqlite3_value objects even when not strictly required.
  3727 **
  3728 ** ^The sqlite3_value objects that are passed as parameters into the
  3729 ** implementation of [application-defined SQL functions] are protected.
  3730 ** ^The sqlite3_value object returned by
  3731 ** [sqlite3_column_value()] is unprotected.
  3732 ** Unprotected sqlite3_value objects may only be used with
  3733 ** [sqlite3_result_value()] and [sqlite3_bind_value()].
  3734 ** The [sqlite3_value_blob | sqlite3_value_type()] family of
  3735 ** interfaces require protected sqlite3_value objects.
  3736 */
  3737 typedef struct Mem sqlite3_value;
  3739 /*
  3740 ** CAPI3REF: SQL Function Context Object
  3741 **
  3742 ** The context in which an SQL function executes is stored in an
  3743 ** sqlite3_context object.  ^A pointer to an sqlite3_context object
  3744 ** is always first parameter to [application-defined SQL functions].
  3745 ** The application-defined SQL function implementation will pass this
  3746 ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
  3747 ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
  3748 ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
  3749 ** and/or [sqlite3_set_auxdata()].
  3750 */
  3751 typedef struct sqlite3_context sqlite3_context;
  3753 /*
  3754 ** CAPI3REF: Binding Values To Prepared Statements
  3755 ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  3756 ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
  3757 **
  3758 ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  3759 ** literals may be replaced by a [parameter] that matches one of following
  3760 ** templates:
  3761 **
  3762 ** <ul>
  3763 ** <li>  ?
  3764 ** <li>  ?NNN
  3765 ** <li>  :VVV
  3766 ** <li>  @VVV
  3767 ** <li>  $VVV
  3768 ** </ul>
  3769 **
  3770 ** In the templates above, NNN represents an integer literal,
  3771 ** and VVV represents an alphanumeric identifier.)^  ^The values of these
  3772 ** parameters (also called "host parameter names" or "SQL parameters")
  3773 ** can be set using the sqlite3_bind_*() routines defined here.
  3774 **
  3775 ** ^The first argument to the sqlite3_bind_*() routines is always
  3776 ** a pointer to the [sqlite3_stmt] object returned from
  3777 ** [sqlite3_prepare_v2()] or its variants.
  3778 **
  3779 ** ^The second argument is the index of the SQL parameter to be set.
  3780 ** ^The leftmost SQL parameter has an index of 1.  ^When the same named
  3781 ** SQL parameter is used more than once, second and subsequent
  3782 ** occurrences have the same index as the first occurrence.
  3783 ** ^The index for named parameters can be looked up using the
  3784 ** [sqlite3_bind_parameter_index()] API if desired.  ^The index
  3785 ** for "?NNN" parameters is the value of NNN.
  3786 ** ^The NNN value must be between 1 and the [sqlite3_limit()]
  3787 ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
  3788 **
  3789 ** ^The third argument is the value to bind to the parameter.
  3790 **
  3791 ** ^(In those routines that have a fourth argument, its value is the
  3792 ** number of bytes in the parameter.  To be clear: the value is the
  3793 ** number of <u>bytes</u> in the value, not the number of characters.)^
  3794 ** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
  3795 ** is negative, then the length of the string is
  3796 ** the number of bytes up to the first zero terminator.
  3797 ** If the fourth parameter to sqlite3_bind_blob() is negative, then
  3798 ** the behavior is undefined.
  3799 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
  3800 ** or sqlite3_bind_text16() then that parameter must be the byte offset
  3801 ** where the NUL terminator would occur assuming the string were NUL
  3802 ** terminated.  If any NUL characters occur at byte offsets less than 
  3803 ** the value of the fourth parameter then the resulting string value will
  3804 ** contain embedded NULs.  The result of expressions involving strings
  3805 ** with embedded NULs is undefined.
  3806 **
  3807 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  3808 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  3809 ** string after SQLite has finished with it.  ^The destructor is called
  3810 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
  3811 ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
  3812 ** ^If the fifth argument is
  3813 ** the special value [SQLITE_STATIC], then SQLite assumes that the
  3814 ** information is in static, unmanaged space and does not need to be freed.
  3815 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  3816 ** SQLite makes its own private copy of the data immediately, before
  3817 ** the sqlite3_bind_*() routine returns.
  3818 **
  3819 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  3820 ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
  3821 ** (just an integer to hold its size) while it is being processed.
  3822 ** Zeroblobs are intended to serve as placeholders for BLOBs whose
  3823 ** content is later written using
  3824 ** [sqlite3_blob_open | incremental BLOB I/O] routines.
  3825 ** ^A negative value for the zeroblob results in a zero-length BLOB.
  3826 **
  3827 ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
  3828 ** for the [prepared statement] or with a prepared statement for which
  3829 ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
  3830 ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
  3831 ** routine is passed a [prepared statement] that has been finalized, the
  3832 ** result is undefined and probably harmful.
  3833 **
  3834 ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
  3835 ** ^Unbound parameters are interpreted as NULL.
  3836 **
  3837 ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
  3838 ** [error code] if anything goes wrong.
  3839 ** ^[SQLITE_RANGE] is returned if the parameter
  3840 ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
  3841 **
  3842 ** See also: [sqlite3_bind_parameter_count()],
  3843 ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
  3844 */
  3845 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
  3846 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
  3847 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
  3848 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  3849 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
  3850 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
  3851 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
  3852 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3853 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3855 /*
  3856 ** CAPI3REF: Number Of SQL Parameters
  3857 **
  3858 ** ^This routine can be used to find the number of [SQL parameters]
  3859 ** in a [prepared statement].  SQL parameters are tokens of the
  3860 ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  3861 ** placeholders for values that are [sqlite3_bind_blob | bound]
  3862 ** to the parameters at a later time.
  3863 **
  3864 ** ^(This routine actually returns the index of the largest (rightmost)
  3865 ** parameter. For all forms except ?NNN, this will correspond to the
  3866 ** number of unique parameters.  If parameters of the ?NNN form are used,
  3867 ** there may be gaps in the list.)^
  3868 **
  3869 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3870 ** [sqlite3_bind_parameter_name()], and
  3871 ** [sqlite3_bind_parameter_index()].
  3872 */
  3873 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
  3875 /*
  3876 ** CAPI3REF: Name Of A Host Parameter
  3877 **
  3878 ** ^The sqlite3_bind_parameter_name(P,N) interface returns
  3879 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
  3880 ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3881 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3882 ** respectively.
  3883 ** In other words, the initial ":" or "$" or "@" or "?"
  3884 ** is included as part of the name.)^
  3885 ** ^Parameters of the form "?" without a following integer have no name
  3886 ** and are referred to as "nameless" or "anonymous parameters".
  3887 **
  3888 ** ^The first host parameter has an index of 1, not 0.
  3889 **
  3890 ** ^If the value N is out of range or if the N-th parameter is
  3891 ** nameless, then NULL is returned.  ^The returned string is
  3892 ** always in UTF-8 encoding even if the named parameter was
  3893 ** originally specified as UTF-16 in [sqlite3_prepare16()] or
  3894 ** [sqlite3_prepare16_v2()].
  3895 **
  3896 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3897 ** [sqlite3_bind_parameter_count()], and
  3898 ** [sqlite3_bind_parameter_index()].
  3899 */
  3900 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  3902 /*
  3903 ** CAPI3REF: Index Of A Parameter With A Given Name
  3904 **
  3905 ** ^Return the index of an SQL parameter given its name.  ^The
  3906 ** index value returned is suitable for use as the second
  3907 ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
  3908 ** is returned if no matching parameter is found.  ^The parameter
  3909 ** name must be given in UTF-8 even if the original statement
  3910 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
  3911 **
  3912 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3913 ** [sqlite3_bind_parameter_count()], and
  3914 ** [sqlite3_bind_parameter_index()].
  3915 */
  3916 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  3918 /*
  3919 ** CAPI3REF: Reset All Bindings On A Prepared Statement
  3920 **
  3921 ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  3922 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  3923 ** ^Use this routine to reset all host parameters to NULL.
  3924 */
  3925 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
  3927 /*
  3928 ** CAPI3REF: Number Of Columns In A Result Set
  3929 **
  3930 ** ^Return the number of columns in the result set returned by the
  3931 ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3932 ** statement that does not return data (for example an [UPDATE]).
  3933 **
  3934 ** See also: [sqlite3_data_count()]
  3935 */
  3936 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
  3938 /*
  3939 ** CAPI3REF: Column Names In A Result Set
  3940 **
  3941 ** ^These routines return the name assigned to a particular column
  3942 ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
  3943 ** interface returns a pointer to a zero-terminated UTF-8 string
  3944 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3945 ** UTF-16 string.  ^The first parameter is the [prepared statement]
  3946 ** that implements the [SELECT] statement. ^The second parameter is the
  3947 ** column number.  ^The leftmost column is number 0.
  3948 **
  3949 ** ^The returned string pointer is valid until either the [prepared statement]
  3950 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
  3951 ** reprepared by the first call to [sqlite3_step()] for a particular run
  3952 ** or until the next call to
  3953 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
  3954 **
  3955 ** ^If sqlite3_malloc() fails during the processing of either routine
  3956 ** (for example during a conversion from UTF-8 to UTF-16) then a
  3957 ** NULL pointer is returned.
  3958 **
  3959 ** ^The name of a result column is the value of the "AS" clause for
  3960 ** that column, if there is an AS clause.  If there is no AS clause
  3961 ** then the name of the column is unspecified and may change from
  3962 ** one release of SQLite to the next.
  3963 */
  3964 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
  3965 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
  3967 /*
  3968 ** CAPI3REF: Source Of Data In A Query Result
  3969 **
  3970 ** ^These routines provide a means to determine the database, table, and
  3971 ** table column that is the origin of a particular result column in
  3972 ** [SELECT] statement.
  3973 ** ^The name of the database or table or column can be returned as
  3974 ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
  3975 ** the database name, the _table_ routines return the table name, and
  3976 ** the origin_ routines return the column name.
  3977 ** ^The returned string is valid until the [prepared statement] is destroyed
  3978 ** using [sqlite3_finalize()] or until the statement is automatically
  3979 ** reprepared by the first call to [sqlite3_step()] for a particular run
  3980 ** or until the same information is requested
  3981 ** again in a different encoding.
  3982 **
  3983 ** ^The names returned are the original un-aliased names of the
  3984 ** database, table, and column.
  3985 **
  3986 ** ^The first argument to these interfaces is a [prepared statement].
  3987 ** ^These functions return information about the Nth result column returned by
  3988 ** the statement, where N is the second function argument.
  3989 ** ^The left-most column is column 0 for these routines.
  3990 **
  3991 ** ^If the Nth column returned by the statement is an expression or
  3992 ** subquery and is not a column value, then all of these functions return
  3993 ** NULL.  ^These routine might also return NULL if a memory allocation error
  3994 ** occurs.  ^Otherwise, they return the name of the attached database, table,
  3995 ** or column that query result column was extracted from.
  3996 **
  3997 ** ^As with all other SQLite APIs, those whose names end with "16" return
  3998 ** UTF-16 encoded strings and the other functions return UTF-8.
  3999 **
  4000 ** ^These APIs are only available if the library was compiled with the
  4001 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
  4002 **
  4003 ** If two or more threads call one or more of these routines against the same
  4004 ** prepared statement and column at the same time then the results are
  4005 ** undefined.
  4006 **
  4007 ** If two or more threads call one or more
  4008 ** [sqlite3_column_database_name | column metadata interfaces]
  4009 ** for the same [prepared statement] and result column
  4010 ** at the same time then the results are undefined.
  4011 */
  4012 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
  4013 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
  4014 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
  4015 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
  4016 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
  4017 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
  4019 /*
  4020 ** CAPI3REF: Declared Datatype Of A Query Result
  4021 **
  4022 ** ^(The first parameter is a [prepared statement].
  4023 ** If this statement is a [SELECT] statement and the Nth column of the
  4024 ** returned result set of that [SELECT] is a table column (not an
  4025 ** expression or subquery) then the declared type of the table
  4026 ** column is returned.)^  ^If the Nth column of the result set is an
  4027 ** expression or subquery, then a NULL pointer is returned.
  4028 ** ^The returned string is always UTF-8 encoded.
  4029 **
  4030 ** ^(For example, given the database schema:
  4031 **
  4032 ** CREATE TABLE t1(c1 VARIANT);
  4033 **
  4034 ** and the following statement to be compiled:
  4035 **
  4036 ** SELECT c1 + 1, c1 FROM t1;
  4037 **
  4038 ** this routine would return the string "VARIANT" for the second result
  4039 ** column (i==1), and a NULL pointer for the first result column (i==0).)^
  4040 **
  4041 ** ^SQLite uses dynamic run-time typing.  ^So just because a column
  4042 ** is declared to contain a particular type does not mean that the
  4043 ** data stored in that column is of the declared type.  SQLite is
  4044 ** strongly typed, but the typing is dynamic not static.  ^Type
  4045 ** is associated with individual values, not with the containers
  4046 ** used to hold those values.
  4047 */
  4048 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
  4049 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
  4051 /*
  4052 ** CAPI3REF: Evaluate An SQL Statement
  4053 **
  4054 ** After a [prepared statement] has been prepared using either
  4055 ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  4056 ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  4057 ** must be called one or more times to evaluate the statement.
  4058 **
  4059 ** The details of the behavior of the sqlite3_step() interface depend
  4060 ** on whether the statement was prepared using the newer "v2" interface
  4061 ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
  4062 ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
  4063 ** new "v2" interface is recommended for new applications but the legacy
  4064 ** interface will continue to be supported.
  4065 **
  4066 ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
  4067 ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
  4068 ** ^With the "v2" interface, any of the other [result codes] or
  4069 ** [extended result codes] might be returned as well.
  4070 **
  4071 ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
  4072 ** database locks it needs to do its job.  ^If the statement is a [COMMIT]
  4073 ** or occurs outside of an explicit transaction, then you can retry the
  4074 ** statement.  If the statement is not a [COMMIT] and occurs within an
  4075 ** explicit transaction then you should rollback the transaction before
  4076 ** continuing.
  4077 **
  4078 ** ^[SQLITE_DONE] means that the statement has finished executing
  4079 ** successfully.  sqlite3_step() should not be called again on this virtual
  4080 ** machine without first calling [sqlite3_reset()] to reset the virtual
  4081 ** machine back to its initial state.
  4082 **
  4083 ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
  4084 ** is returned each time a new row of data is ready for processing by the
  4085 ** caller. The values may be accessed using the [column access functions].
  4086 ** sqlite3_step() is called again to retrieve the next row of data.
  4087 **
  4088 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
  4089 ** violation) has occurred.  sqlite3_step() should not be called again on
  4090 ** the VM. More information may be found by calling [sqlite3_errmsg()].
  4091 ** ^With the legacy interface, a more specific error code (for example,
  4092 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
  4093 ** can be obtained by calling [sqlite3_reset()] on the
  4094 ** [prepared statement].  ^In the "v2" interface,
  4095 ** the more specific error code is returned directly by sqlite3_step().
  4096 **
  4097 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
  4098 ** Perhaps it was called on a [prepared statement] that has
  4099 ** already been [sqlite3_finalize | finalized] or on one that had
  4100 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
  4101 ** be the case that the same database connection is being used by two or
  4102 ** more threads at the same moment in time.
  4103 **
  4104 ** For all versions of SQLite up to and including 3.6.23.1, a call to
  4105 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
  4106 ** other than [SQLITE_ROW] before any subsequent invocation of
  4107 ** sqlite3_step().  Failure to reset the prepared statement using 
  4108 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
  4109 ** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
  4110 ** calling [sqlite3_reset()] automatically in this circumstance rather
  4111 ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
  4112 ** break because any application that ever receives an SQLITE_MISUSE error
  4113 ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
  4114 ** can be used to restore the legacy behavior.
  4115 **
  4116 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
  4117 ** API always returns a generic error code, [SQLITE_ERROR], following any
  4118 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
  4119 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
  4120 ** specific [error codes] that better describes the error.
  4121 ** We admit that this is a goofy design.  The problem has been fixed
  4122 ** with the "v2" interface.  If you prepare all of your SQL statements
  4123 ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
  4124 ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
  4125 ** then the more specific [error codes] are returned directly
  4126 ** by sqlite3_step().  The use of the "v2" interface is recommended.
  4127 */
  4128 SQLITE_API int sqlite3_step(sqlite3_stmt*);
  4130 /*
  4131 ** CAPI3REF: Number of columns in a result set
  4132 **
  4133 ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  4134 ** current row of the result set of [prepared statement] P.
  4135 ** ^If prepared statement P does not have results ready to return
  4136 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  4137 ** interfaces) then sqlite3_data_count(P) returns 0.
  4138 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
  4139 ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
  4140 ** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
  4141 ** will return non-zero if previous call to [sqlite3_step](P) returned
  4142 ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
  4143 ** where it always returns zero since each step of that multi-step
  4144 ** pragma returns 0 columns of data.
  4145 **
  4146 ** See also: [sqlite3_column_count()]
  4147 */
  4148 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
  4150 /*
  4151 ** CAPI3REF: Fundamental Datatypes
  4152 ** KEYWORDS: SQLITE_TEXT
  4153 **
  4154 ** ^(Every value in SQLite has one of five fundamental datatypes:
  4155 **
  4156 ** <ul>
  4157 ** <li> 64-bit signed integer
  4158 ** <li> 64-bit IEEE floating point number
  4159 ** <li> string
  4160 ** <li> BLOB
  4161 ** <li> NULL
  4162 ** </ul>)^
  4163 **
  4164 ** These constants are codes for each of those types.
  4165 **
  4166 ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
  4167 ** for a completely different meaning.  Software that links against both
  4168 ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
  4169 ** SQLITE_TEXT.
  4170 */
  4171 #define SQLITE_INTEGER  1
  4172 #define SQLITE_FLOAT    2
  4173 #define SQLITE_BLOB     4
  4174 #define SQLITE_NULL     5
  4175 #ifdef SQLITE_TEXT
  4176 # undef SQLITE_TEXT
  4177 #else
  4178 # define SQLITE_TEXT     3
  4179 #endif
  4180 #define SQLITE3_TEXT     3
  4182 /*
  4183 ** CAPI3REF: Result Values From A Query
  4184 ** KEYWORDS: {column access functions}
  4185 **
  4186 ** These routines form the "result set" interface.
  4187 **
  4188 ** ^These routines return information about a single column of the current
  4189 ** result row of a query.  ^In every case the first argument is a pointer
  4190 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  4191 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
  4192 ** and the second argument is the index of the column for which information
  4193 ** should be returned. ^The leftmost column of the result set has the index 0.
  4194 ** ^The number of columns in the result can be determined using
  4195 ** [sqlite3_column_count()].
  4196 **
  4197 ** If the SQL statement does not currently point to a valid row, or if the
  4198 ** column index is out of range, the result is undefined.
  4199 ** These routines may only be called when the most recent call to
  4200 ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
  4201 ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
  4202 ** If any of these routines are called after [sqlite3_reset()] or
  4203 ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
  4204 ** something other than [SQLITE_ROW], the results are undefined.
  4205 ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
  4206 ** are called from a different thread while any of these routines
  4207 ** are pending, then the results are undefined.
  4208 **
  4209 ** ^The sqlite3_column_type() routine returns the
  4210 ** [SQLITE_INTEGER | datatype code] for the initial data type
  4211 ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
  4212 ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
  4213 ** returned by sqlite3_column_type() is only meaningful if no type
  4214 ** conversions have occurred as described below.  After a type conversion,
  4215 ** the value returned by sqlite3_column_type() is undefined.  Future
  4216 ** versions of SQLite may change the behavior of sqlite3_column_type()
  4217 ** following a type conversion.
  4218 **
  4219 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
  4220 ** routine returns the number of bytes in that BLOB or string.
  4221 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
  4222 ** the string to UTF-8 and then returns the number of bytes.
  4223 ** ^If the result is a numeric value then sqlite3_column_bytes() uses
  4224 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
  4225 ** the number of bytes in that string.
  4226 ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
  4227 **
  4228 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
  4229 ** routine returns the number of bytes in that BLOB or string.
  4230 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
  4231 ** the string to UTF-16 and then returns the number of bytes.
  4232 ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
  4233 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
  4234 ** the number of bytes in that string.
  4235 ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
  4236 **
  4237 ** ^The values returned by [sqlite3_column_bytes()] and 
  4238 ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
  4239 ** of the string.  ^For clarity: the values returned by
  4240 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
  4241 ** bytes in the string, not the number of characters.
  4242 **
  4243 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
  4244 ** even empty strings, are always zero-terminated.  ^The return
  4245 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
  4246 **
  4247 ** ^The object returned by [sqlite3_column_value()] is an
  4248 ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
  4249 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
  4250 ** If the [unprotected sqlite3_value] object returned by
  4251 ** [sqlite3_column_value()] is used in any other way, including calls
  4252 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
  4253 ** or [sqlite3_value_bytes()], then the behavior is undefined.
  4254 **
  4255 ** These routines attempt to convert the value where appropriate.  ^For
  4256 ** example, if the internal representation is FLOAT and a text result
  4257 ** is requested, [sqlite3_snprintf()] is used internally to perform the
  4258 ** conversion automatically.  ^(The following table details the conversions
  4259 ** that are applied:
  4260 **
  4261 ** <blockquote>
  4262 ** <table border="1">
  4263 ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
  4264 **
  4265 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
  4266 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
  4267 ** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
  4268 ** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
  4269 ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
  4270 ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
  4271 ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
  4272 ** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
  4273 ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
  4274 ** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
  4275 ** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
  4276 ** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
  4277 ** <tr><td>  TEXT    <td>   BLOB    <td> No change
  4278 ** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
  4279 ** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
  4280 ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
  4281 ** </table>
  4282 ** </blockquote>)^
  4283 **
  4284 ** The table above makes reference to standard C library functions atoi()
  4285 ** and atof().  SQLite does not really use these functions.  It has its
  4286 ** own equivalent internal routines.  The atoi() and atof() names are
  4287 ** used in the table for brevity and because they are familiar to most
  4288 ** C programmers.
  4289 **
  4290 ** Note that when type conversions occur, pointers returned by prior
  4291 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
  4292 ** sqlite3_column_text16() may be invalidated.
  4293 ** Type conversions and pointer invalidations might occur
  4294 ** in the following cases:
  4295 **
  4296 ** <ul>
  4297 ** <li> The initial content is a BLOB and sqlite3_column_text() or
  4298 **      sqlite3_column_text16() is called.  A zero-terminator might
  4299 **      need to be added to the string.</li>
  4300 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
  4301 **      sqlite3_column_text16() is called.  The content must be converted
  4302 **      to UTF-16.</li>
  4303 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
  4304 **      sqlite3_column_text() is called.  The content must be converted
  4305 **      to UTF-8.</li>
  4306 ** </ul>
  4307 **
  4308 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
  4309 ** not invalidate a prior pointer, though of course the content of the buffer
  4310 ** that the prior pointer references will have been modified.  Other kinds
  4311 ** of conversion are done in place when it is possible, but sometimes they
  4312 ** are not possible and in those cases prior pointers are invalidated.
  4313 **
  4314 ** The safest and easiest to remember policy is to invoke these routines
  4315 ** in one of the following ways:
  4316 **
  4317 ** <ul>
  4318 **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
  4319 **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
  4320 **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
  4321 ** </ul>
  4322 **
  4323 ** In other words, you should call sqlite3_column_text(),
  4324 ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
  4325 ** into the desired format, then invoke sqlite3_column_bytes() or
  4326 ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
  4327 ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
  4328 ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
  4329 ** with calls to sqlite3_column_bytes().
  4330 **
  4331 ** ^The pointers returned are valid until a type conversion occurs as
  4332 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
  4333 ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
  4334 ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
  4335 ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
  4336 ** [sqlite3_free()].
  4337 **
  4338 ** ^(If a memory allocation error occurs during the evaluation of any
  4339 ** of these routines, a default value is returned.  The default value
  4340 ** is either the integer 0, the floating point number 0.0, or a NULL
  4341 ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
  4342 ** [SQLITE_NOMEM].)^
  4343 */
  4344 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
  4345 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
  4346 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
  4347 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
  4348 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
  4349 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
  4350 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  4351 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
  4352 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
  4353 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
  4355 /*
  4356 ** CAPI3REF: Destroy A Prepared Statement Object
  4357 **
  4358 ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  4359 ** ^If the most recent evaluation of the statement encountered no errors
  4360 ** or if the statement is never been evaluated, then sqlite3_finalize() returns
  4361 ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
  4362 ** sqlite3_finalize(S) returns the appropriate [error code] or
  4363 ** [extended error code].
  4364 **
  4365 ** ^The sqlite3_finalize(S) routine can be called at any point during
  4366 ** the life cycle of [prepared statement] S:
  4367 ** before statement S is ever evaluated, after
  4368 ** one or more calls to [sqlite3_reset()], or after any call
  4369 ** to [sqlite3_step()] regardless of whether or not the statement has
  4370 ** completed execution.
  4371 **
  4372 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
  4373 **
  4374 ** The application must finalize every [prepared statement] in order to avoid
  4375 ** resource leaks.  It is a grievous error for the application to try to use
  4376 ** a prepared statement after it has been finalized.  Any use of a prepared
  4377 ** statement after it has been finalized can result in undefined and
  4378 ** undesirable behavior such as segfaults and heap corruption.
  4379 */
  4380 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
  4382 /*
  4383 ** CAPI3REF: Reset A Prepared Statement Object
  4384 **
  4385 ** The sqlite3_reset() function is called to reset a [prepared statement]
  4386 ** object back to its initial state, ready to be re-executed.
  4387 ** ^Any SQL statement variables that had values bound to them using
  4388 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  4389 ** Use [sqlite3_clear_bindings()] to reset the bindings.
  4390 **
  4391 ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
  4392 ** back to the beginning of its program.
  4393 **
  4394 ** ^If the most recent call to [sqlite3_step(S)] for the
  4395 ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
  4396 ** or if [sqlite3_step(S)] has never before been called on S,
  4397 ** then [sqlite3_reset(S)] returns [SQLITE_OK].
  4398 **
  4399 ** ^If the most recent call to [sqlite3_step(S)] for the
  4400 ** [prepared statement] S indicated an error, then
  4401 ** [sqlite3_reset(S)] returns an appropriate [error code].
  4402 **
  4403 ** ^The [sqlite3_reset(S)] interface does not change the values
  4404 ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
  4405 */
  4406 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
  4408 /*
  4409 ** CAPI3REF: Create Or Redefine SQL Functions
  4410 ** KEYWORDS: {function creation routines}
  4411 ** KEYWORDS: {application-defined SQL function}
  4412 ** KEYWORDS: {application-defined SQL functions}
  4413 **
  4414 ** ^These functions (collectively known as "function creation routines")
  4415 ** are used to add SQL functions or aggregates or to redefine the behavior
  4416 ** of existing SQL functions or aggregates.  The only differences between
  4417 ** these routines are the text encoding expected for
  4418 ** the second parameter (the name of the function being created)
  4419 ** and the presence or absence of a destructor callback for
  4420 ** the application data pointer.
  4421 **
  4422 ** ^The first parameter is the [database connection] to which the SQL
  4423 ** function is to be added.  ^If an application uses more than one database
  4424 ** connection then application-defined SQL functions must be added
  4425 ** to each database connection separately.
  4426 **
  4427 ** ^The second parameter is the name of the SQL function to be created or
  4428 ** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
  4429 ** representation, exclusive of the zero-terminator.  ^Note that the name
  4430 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.  
  4431 ** ^Any attempt to create a function with a longer name
  4432 ** will result in [SQLITE_MISUSE] being returned.
  4433 **
  4434 ** ^The third parameter (nArg)
  4435 ** is the number of arguments that the SQL function or
  4436 ** aggregate takes. ^If this parameter is -1, then the SQL function or
  4437 ** aggregate may take any number of arguments between 0 and the limit
  4438 ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
  4439 ** parameter is less than -1 or greater than 127 then the behavior is
  4440 ** undefined.
  4441 **
  4442 ** ^The fourth parameter, eTextRep, specifies what
  4443 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
  4444 ** its parameters.  Every SQL function implementation must be able to work
  4445 ** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
  4446 ** more efficient with one encoding than another.  ^An application may
  4447 ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
  4448 ** times with the same function but with different values of eTextRep.
  4449 ** ^When multiple implementations of the same function are available, SQLite
  4450 ** will pick the one that involves the least amount of data conversion.
  4451 ** If there is only a single implementation which does not care what text
  4452 ** encoding is used, then the fourth argument should be [SQLITE_ANY].
  4453 **
  4454 ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
  4455 ** function can gain access to this pointer using [sqlite3_user_data()].)^
  4456 **
  4457 ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
  4458 ** pointers to C-language functions that implement the SQL function or
  4459 ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
  4460 ** callback only; NULL pointers must be passed as the xStep and xFinal
  4461 ** parameters. ^An aggregate SQL function requires an implementation of xStep
  4462 ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  4463 ** SQL function or aggregate, pass NULL pointers for all three function
  4464 ** callbacks.
  4465 **
  4466 ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
  4467 ** then it is destructor for the application data pointer. 
  4468 ** The destructor is invoked when the function is deleted, either by being
  4469 ** overloaded or when the database connection closes.)^
  4470 ** ^The destructor is also invoked if the call to
  4471 ** sqlite3_create_function_v2() fails.
  4472 ** ^When the destructor callback of the tenth parameter is invoked, it
  4473 ** is passed a single argument which is a copy of the application data 
  4474 ** pointer which was the fifth parameter to sqlite3_create_function_v2().
  4475 **
  4476 ** ^It is permitted to register multiple implementations of the same
  4477 ** functions with the same name but with either differing numbers of
  4478 ** arguments or differing preferred text encodings.  ^SQLite will use
  4479 ** the implementation that most closely matches the way in which the
  4480 ** SQL function is used.  ^A function implementation with a non-negative
  4481 ** nArg parameter is a better match than a function implementation with
  4482 ** a negative nArg.  ^A function where the preferred text encoding
  4483 ** matches the database encoding is a better
  4484 ** match than a function where the encoding is different.  
  4485 ** ^A function where the encoding difference is between UTF16le and UTF16be
  4486 ** is a closer match than a function where the encoding difference is
  4487 ** between UTF8 and UTF16.
  4488 **
  4489 ** ^Built-in functions may be overloaded by new application-defined functions.
  4490 **
  4491 ** ^An application-defined function is permitted to call other
  4492 ** SQLite interfaces.  However, such calls must not
  4493 ** close the database connection nor finalize or reset the prepared
  4494 ** statement in which the function is running.
  4495 */
  4496 SQLITE_API int sqlite3_create_function(
  4497   sqlite3 *db,
  4498   const char *zFunctionName,
  4499   int nArg,
  4500   int eTextRep,
  4501   void *pApp,
  4502   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4503   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4504   void (*xFinal)(sqlite3_context*)
  4505 );
  4506 SQLITE_API int sqlite3_create_function16(
  4507   sqlite3 *db,
  4508   const void *zFunctionName,
  4509   int nArg,
  4510   int eTextRep,
  4511   void *pApp,
  4512   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4513   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4514   void (*xFinal)(sqlite3_context*)
  4515 );
  4516 SQLITE_API int sqlite3_create_function_v2(
  4517   sqlite3 *db,
  4518   const char *zFunctionName,
  4519   int nArg,
  4520   int eTextRep,
  4521   void *pApp,
  4522   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4523   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4524   void (*xFinal)(sqlite3_context*),
  4525   void(*xDestroy)(void*)
  4526 );
  4528 /*
  4529 ** CAPI3REF: Text Encodings
  4530 **
  4531 ** These constant define integer codes that represent the various
  4532 ** text encodings supported by SQLite.
  4533 */
  4534 #define SQLITE_UTF8           1
  4535 #define SQLITE_UTF16LE        2
  4536 #define SQLITE_UTF16BE        3
  4537 #define SQLITE_UTF16          4    /* Use native byte order */
  4538 #define SQLITE_ANY            5    /* sqlite3_create_function only */
  4539 #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
  4541 /*
  4542 ** CAPI3REF: Deprecated Functions
  4543 ** DEPRECATED
  4544 **
  4545 ** These functions are [deprecated].  In order to maintain
  4546 ** backwards compatibility with older code, these functions continue 
  4547 ** to be supported.  However, new applications should avoid
  4548 ** the use of these functions.  To help encourage people to avoid
  4549 ** using these functions, we are not going to tell you what they do.
  4550 */
  4551 #ifndef SQLITE_OMIT_DEPRECATED
  4552 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
  4553 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
  4554 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
  4555 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
  4556 SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
  4557 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
  4558 #endif
  4560 /*
  4561 ** CAPI3REF: Obtaining SQL Function Parameter Values
  4562 **
  4563 ** The C-language implementation of SQL functions and aggregates uses
  4564 ** this set of interface routines to access the parameter values on
  4565 ** the function or aggregate.
  4566 **
  4567 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  4568 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
  4569 ** define callbacks that implement the SQL functions and aggregates.
  4570 ** The 3rd parameter to these callbacks is an array of pointers to
  4571 ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
  4572 ** each parameter to the SQL function.  These routines are used to
  4573 ** extract values from the [sqlite3_value] objects.
  4574 **
  4575 ** These routines work only with [protected sqlite3_value] objects.
  4576 ** Any attempt to use these routines on an [unprotected sqlite3_value]
  4577 ** object results in undefined behavior.
  4578 **
  4579 ** ^These routines work just like the corresponding [column access functions]
  4580 ** except that  these routines take a single [protected sqlite3_value] object
  4581 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
  4582 **
  4583 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
  4584 ** in the native byte-order of the host machine.  ^The
  4585 ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
  4586 ** extract UTF-16 strings as big-endian and little-endian respectively.
  4587 **
  4588 ** ^(The sqlite3_value_numeric_type() interface attempts to apply
  4589 ** numeric affinity to the value.  This means that an attempt is
  4590 ** made to convert the value to an integer or floating point.  If
  4591 ** such a conversion is possible without loss of information (in other
  4592 ** words, if the value is a string that looks like a number)
  4593 ** then the conversion is performed.  Otherwise no conversion occurs.
  4594 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
  4595 **
  4596 ** Please pay particular attention to the fact that the pointer returned
  4597 ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  4598 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  4599 ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  4600 ** or [sqlite3_value_text16()].
  4601 **
  4602 ** These routines must be called from the same thread as
  4603 ** the SQL function that supplied the [sqlite3_value*] parameters.
  4604 */
  4605 SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
  4606 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  4607 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  4608 SQLITE_API double sqlite3_value_double(sqlite3_value*);
  4609 SQLITE_API int sqlite3_value_int(sqlite3_value*);
  4610 SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
  4611 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
  4612 SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
  4613 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  4614 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  4615 SQLITE_API int sqlite3_value_type(sqlite3_value*);
  4616 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  4618 /*
  4619 ** CAPI3REF: Obtain Aggregate Function Context
  4620 **
  4621 ** Implementations of aggregate SQL functions use this
  4622 ** routine to allocate memory for storing their state.
  4623 **
  4624 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
  4625 ** for a particular aggregate function, SQLite
  4626 ** allocates N of memory, zeroes out that memory, and returns a pointer
  4627 ** to the new memory. ^On second and subsequent calls to
  4628 ** sqlite3_aggregate_context() for the same aggregate function instance,
  4629 ** the same buffer is returned.  Sqlite3_aggregate_context() is normally
  4630 ** called once for each invocation of the xStep callback and then one
  4631 ** last time when the xFinal callback is invoked.  ^(When no rows match
  4632 ** an aggregate query, the xStep() callback of the aggregate function
  4633 ** implementation is never called and xFinal() is called exactly once.
  4634 ** In those cases, sqlite3_aggregate_context() might be called for the
  4635 ** first time from within xFinal().)^
  4636 **
  4637 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
  4638 ** less than or equal to zero or if a memory allocate error occurs.
  4639 **
  4640 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
  4641 ** determined by the N parameter on first successful call.  Changing the
  4642 ** value of N in subsequent call to sqlite3_aggregate_context() within
  4643 ** the same aggregate function instance will not resize the memory
  4644 ** allocation.)^
  4645 **
  4646 ** ^SQLite automatically frees the memory allocated by 
  4647 ** sqlite3_aggregate_context() when the aggregate query concludes.
  4648 **
  4649 ** The first parameter must be a copy of the
  4650 ** [sqlite3_context | SQL function context] that is the first parameter
  4651 ** to the xStep or xFinal callback routine that implements the aggregate
  4652 ** function.
  4653 **
  4654 ** This routine must be called from the same thread in which
  4655 ** the aggregate SQL function is running.
  4656 */
  4657 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  4659 /*
  4660 ** CAPI3REF: User Data For Functions
  4661 **
  4662 ** ^The sqlite3_user_data() interface returns a copy of
  4663 ** the pointer that was the pUserData parameter (the 5th parameter)
  4664 ** of the [sqlite3_create_function()]
  4665 ** and [sqlite3_create_function16()] routines that originally
  4666 ** registered the application defined function.
  4667 **
  4668 ** This routine must be called from the same thread in which
  4669 ** the application-defined function is running.
  4670 */
  4671 SQLITE_API void *sqlite3_user_data(sqlite3_context*);
  4673 /*
  4674 ** CAPI3REF: Database Connection For Functions
  4675 **
  4676 ** ^The sqlite3_context_db_handle() interface returns a copy of
  4677 ** the pointer to the [database connection] (the 1st parameter)
  4678 ** of the [sqlite3_create_function()]
  4679 ** and [sqlite3_create_function16()] routines that originally
  4680 ** registered the application defined function.
  4681 */
  4682 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
  4684 /*
  4685 ** CAPI3REF: Function Auxiliary Data
  4686 **
  4687 ** The following two functions may be used by scalar SQL functions to
  4688 ** associate metadata with argument values. If the same value is passed to
  4689 ** multiple invocations of the same SQL function during query execution, under
  4690 ** some circumstances the associated metadata may be preserved. This may
  4691 ** be used, for example, to add a regular-expression matching scalar
  4692 ** function. The compiled version of the regular expression is stored as
  4693 ** metadata associated with the SQL value passed as the regular expression
  4694 ** pattern.  The compiled regular expression can be reused on multiple
  4695 ** invocations of the same function so that the original pattern string
  4696 ** does not need to be recompiled on each invocation.
  4697 **
  4698 ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
  4699 ** associated by the sqlite3_set_auxdata() function with the Nth argument
  4700 ** value to the application-defined function. ^If no metadata has been ever
  4701 ** been set for the Nth argument of the function, or if the corresponding
  4702 ** function parameter has changed since the meta-data was set,
  4703 ** then sqlite3_get_auxdata() returns a NULL pointer.
  4704 **
  4705 ** ^The sqlite3_set_auxdata() interface saves the metadata
  4706 ** pointed to by its 3rd parameter as the metadata for the N-th
  4707 ** argument of the application-defined function.  Subsequent
  4708 ** calls to sqlite3_get_auxdata() might return this data, if it has
  4709 ** not been destroyed.
  4710 ** ^If it is not NULL, SQLite will invoke the destructor
  4711 ** function given by the 4th parameter to sqlite3_set_auxdata() on
  4712 ** the metadata when the corresponding function parameter changes
  4713 ** or when the SQL statement completes, whichever comes first.
  4714 **
  4715 ** SQLite is free to call the destructor and drop metadata on any
  4716 ** parameter of any function at any time.  ^The only guarantee is that
  4717 ** the destructor will be called before the metadata is dropped.
  4718 **
  4719 ** ^(In practice, metadata is preserved between function calls for
  4720 ** expressions that are constant at compile time. This includes literal
  4721 ** values and [parameters].)^
  4722 **
  4723 ** These routines must be called from the same thread in which
  4724 ** the SQL function is running.
  4725 */
  4726 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
  4727 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
  4730 /*
  4731 ** CAPI3REF: Constants Defining Special Destructor Behavior
  4732 **
  4733 ** These are special values for the destructor that is passed in as the
  4734 ** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
  4735 ** argument is SQLITE_STATIC, it means that the content pointer is constant
  4736 ** and will never change.  It does not need to be destroyed.  ^The
  4737 ** SQLITE_TRANSIENT value means that the content will likely change in
  4738 ** the near future and that SQLite should make its own private copy of
  4739 ** the content before returning.
  4740 **
  4741 ** The typedef is necessary to work around problems in certain
  4742 ** C++ compilers.  See ticket #2191.
  4743 */
  4744 typedef void (*sqlite3_destructor_type)(void*);
  4745 #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
  4746 #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
  4748 /*
  4749 ** CAPI3REF: Setting The Result Of An SQL Function
  4750 **
  4751 ** These routines are used by the xFunc or xFinal callbacks that
  4752 ** implement SQL functions and aggregates.  See
  4753 ** [sqlite3_create_function()] and [sqlite3_create_function16()]
  4754 ** for additional information.
  4755 **
  4756 ** These functions work very much like the [parameter binding] family of
  4757 ** functions used to bind values to host parameters in prepared statements.
  4758 ** Refer to the [SQL parameter] documentation for additional information.
  4759 **
  4760 ** ^The sqlite3_result_blob() interface sets the result from
  4761 ** an application-defined function to be the BLOB whose content is pointed
  4762 ** to by the second parameter and which is N bytes long where N is the
  4763 ** third parameter.
  4764 **
  4765 ** ^The sqlite3_result_zeroblob() interfaces set the result of
  4766 ** the application-defined function to be a BLOB containing all zero
  4767 ** bytes and N bytes in size, where N is the value of the 2nd parameter.
  4768 **
  4769 ** ^The sqlite3_result_double() interface sets the result from
  4770 ** an application-defined function to be a floating point value specified
  4771 ** by its 2nd argument.
  4772 **
  4773 ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
  4774 ** cause the implemented SQL function to throw an exception.
  4775 ** ^SQLite uses the string pointed to by the
  4776 ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
  4777 ** as the text of an error message.  ^SQLite interprets the error
  4778 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
  4779 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
  4780 ** byte order.  ^If the third parameter to sqlite3_result_error()
  4781 ** or sqlite3_result_error16() is negative then SQLite takes as the error
  4782 ** message all text up through the first zero character.
  4783 ** ^If the third parameter to sqlite3_result_error() or
  4784 ** sqlite3_result_error16() is non-negative then SQLite takes that many
  4785 ** bytes (not characters) from the 2nd parameter as the error message.
  4786 ** ^The sqlite3_result_error() and sqlite3_result_error16()
  4787 ** routines make a private copy of the error message text before
  4788 ** they return.  Hence, the calling function can deallocate or
  4789 ** modify the text after they return without harm.
  4790 ** ^The sqlite3_result_error_code() function changes the error code
  4791 ** returned by SQLite as a result of an error in a function.  ^By default,
  4792 ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
  4793 ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  4794 **
  4795 ** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
  4796 ** error indicating that a string or BLOB is too long to represent.
  4797 **
  4798 ** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
  4799 ** error indicating that a memory allocation failed.
  4800 **
  4801 ** ^The sqlite3_result_int() interface sets the return value
  4802 ** of the application-defined function to be the 32-bit signed integer
  4803 ** value given in the 2nd argument.
  4804 ** ^The sqlite3_result_int64() interface sets the return value
  4805 ** of the application-defined function to be the 64-bit signed integer
  4806 ** value given in the 2nd argument.
  4807 **
  4808 ** ^The sqlite3_result_null() interface sets the return value
  4809 ** of the application-defined function to be NULL.
  4810 **
  4811 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
  4812 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
  4813 ** set the return value of the application-defined function to be
  4814 ** a text string which is represented as UTF-8, UTF-16 native byte order,
  4815 ** UTF-16 little endian, or UTF-16 big endian, respectively.
  4816 ** ^SQLite takes the text result from the application from
  4817 ** the 2nd parameter of the sqlite3_result_text* interfaces.
  4818 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4819 ** is negative, then SQLite takes result text from the 2nd parameter
  4820 ** through the first zero character.
  4821 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4822 ** is non-negative, then as many bytes (not characters) of the text
  4823 ** pointed to by the 2nd parameter are taken as the application-defined
  4824 ** function result.  If the 3rd parameter is non-negative, then it
  4825 ** must be the byte offset into the string where the NUL terminator would
  4826 ** appear if the string where NUL terminated.  If any NUL characters occur
  4827 ** in the string at a byte offset that is less than the value of the 3rd
  4828 ** parameter, then the resulting string will contain embedded NULs and the
  4829 ** result of expressions operating on strings with embedded NULs is undefined.
  4830 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4831 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
  4832 ** function as the destructor on the text or BLOB result when it has
  4833 ** finished using that result.
  4834 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
  4835 ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
  4836 ** assumes that the text or BLOB result is in constant space and does not
  4837 ** copy the content of the parameter nor call a destructor on the content
  4838 ** when it has finished using that result.
  4839 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4840 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
  4841 ** then SQLite makes a copy of the result into space obtained from
  4842 ** from [sqlite3_malloc()] before it returns.
  4843 **
  4844 ** ^The sqlite3_result_value() interface sets the result of
  4845 ** the application-defined function to be a copy the
  4846 ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
  4847 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
  4848 ** so that the [sqlite3_value] specified in the parameter may change or
  4849 ** be deallocated after sqlite3_result_value() returns without harm.
  4850 ** ^A [protected sqlite3_value] object may always be used where an
  4851 ** [unprotected sqlite3_value] object is required, so either
  4852 ** kind of [sqlite3_value] object can be used with this interface.
  4853 **
  4854 ** If these routines are called from within the different thread
  4855 ** than the one containing the application-defined function that received
  4856 ** the [sqlite3_context] pointer, the results are undefined.
  4857 */
  4858 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
  4859 SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
  4860 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
  4861 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
  4862 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
  4863 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
  4864 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
  4865 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
  4866 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
  4867 SQLITE_API void sqlite3_result_null(sqlite3_context*);
  4868 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
  4869 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  4870 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4871 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4872 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4873 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
  4875 /*
  4876 ** CAPI3REF: Define New Collating Sequences
  4877 **
  4878 ** ^These functions add, remove, or modify a [collation] associated
  4879 ** with the [database connection] specified as the first argument.
  4880 **
  4881 ** ^The name of the collation is a UTF-8 string
  4882 ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  4883 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
  4884 ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
  4885 ** considered to be the same name.
  4886 **
  4887 ** ^(The third argument (eTextRep) must be one of the constants:
  4888 ** <ul>
  4889 ** <li> [SQLITE_UTF8],
  4890 ** <li> [SQLITE_UTF16LE],
  4891 ** <li> [SQLITE_UTF16BE],
  4892 ** <li> [SQLITE_UTF16], or
  4893 ** <li> [SQLITE_UTF16_ALIGNED].
  4894 ** </ul>)^
  4895 ** ^The eTextRep argument determines the encoding of strings passed
  4896 ** to the collating function callback, xCallback.
  4897 ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
  4898 ** force strings to be UTF16 with native byte order.
  4899 ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
  4900 ** on an even byte address.
  4901 **
  4902 ** ^The fourth argument, pArg, is an application data pointer that is passed
  4903 ** through as the first argument to the collating function callback.
  4904 **
  4905 ** ^The fifth argument, xCallback, is a pointer to the collating function.
  4906 ** ^Multiple collating functions can be registered using the same name but
  4907 ** with different eTextRep parameters and SQLite will use whichever
  4908 ** function requires the least amount of data transformation.
  4909 ** ^If the xCallback argument is NULL then the collating function is
  4910 ** deleted.  ^When all collating functions having the same name are deleted,
  4911 ** that collation is no longer usable.
  4912 **
  4913 ** ^The collating function callback is invoked with a copy of the pArg 
  4914 ** application data pointer and with two strings in the encoding specified
  4915 ** by the eTextRep argument.  The collating function must return an
  4916 ** integer that is negative, zero, or positive
  4917 ** if the first string is less than, equal to, or greater than the second,
  4918 ** respectively.  A collating function must always return the same answer
  4919 ** given the same inputs.  If two or more collating functions are registered
  4920 ** to the same collation name (using different eTextRep values) then all
  4921 ** must give an equivalent answer when invoked with equivalent strings.
  4922 ** The collating function must obey the following properties for all
  4923 ** strings A, B, and C:
  4924 **
  4925 ** <ol>
  4926 ** <li> If A==B then B==A.
  4927 ** <li> If A==B and B==C then A==C.
  4928 ** <li> If A&lt;B THEN B&gt;A.
  4929 ** <li> If A&lt;B and B&lt;C then A&lt;C.
  4930 ** </ol>
  4931 **
  4932 ** If a collating function fails any of the above constraints and that
  4933 ** collating function is  registered and used, then the behavior of SQLite
  4934 ** is undefined.
  4935 **
  4936 ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
  4937 ** with the addition that the xDestroy callback is invoked on pArg when
  4938 ** the collating function is deleted.
  4939 ** ^Collating functions are deleted when they are overridden by later
  4940 ** calls to the collation creation functions or when the
  4941 ** [database connection] is closed using [sqlite3_close()].
  4942 **
  4943 ** ^The xDestroy callback is <u>not</u> called if the 
  4944 ** sqlite3_create_collation_v2() function fails.  Applications that invoke
  4945 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should 
  4946 ** check the return code and dispose of the application data pointer
  4947 ** themselves rather than expecting SQLite to deal with it for them.
  4948 ** This is different from every other SQLite interface.  The inconsistency 
  4949 ** is unfortunate but cannot be changed without breaking backwards 
  4950 ** compatibility.
  4951 **
  4952 ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
  4953 */
  4954 SQLITE_API int sqlite3_create_collation(
  4955   sqlite3*, 
  4956   const char *zName, 
  4957   int eTextRep, 
  4958   void *pArg,
  4959   int(*xCompare)(void*,int,const void*,int,const void*)
  4960 );
  4961 SQLITE_API int sqlite3_create_collation_v2(
  4962   sqlite3*, 
  4963   const char *zName, 
  4964   int eTextRep, 
  4965   void *pArg,
  4966   int(*xCompare)(void*,int,const void*,int,const void*),
  4967   void(*xDestroy)(void*)
  4968 );
  4969 SQLITE_API int sqlite3_create_collation16(
  4970   sqlite3*, 
  4971   const void *zName,
  4972   int eTextRep, 
  4973   void *pArg,
  4974   int(*xCompare)(void*,int,const void*,int,const void*)
  4975 );
  4977 /*
  4978 ** CAPI3REF: Collation Needed Callbacks
  4979 **
  4980 ** ^To avoid having to register all collation sequences before a database
  4981 ** can be used, a single callback function may be registered with the
  4982 ** [database connection] to be invoked whenever an undefined collation
  4983 ** sequence is required.
  4984 **
  4985 ** ^If the function is registered using the sqlite3_collation_needed() API,
  4986 ** then it is passed the names of undefined collation sequences as strings
  4987 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
  4988 ** the names are passed as UTF-16 in machine native byte order.
  4989 ** ^A call to either function replaces the existing collation-needed callback.
  4990 **
  4991 ** ^(When the callback is invoked, the first argument passed is a copy
  4992 ** of the second argument to sqlite3_collation_needed() or
  4993 ** sqlite3_collation_needed16().  The second argument is the database
  4994 ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
  4995 ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
  4996 ** sequence function required.  The fourth parameter is the name of the
  4997 ** required collation sequence.)^
  4998 **
  4999 ** The callback function should register the desired collation using
  5000 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
  5001 ** [sqlite3_create_collation_v2()].
  5002 */
  5003 SQLITE_API int sqlite3_collation_needed(
  5004   sqlite3*, 
  5005   void*, 
  5006   void(*)(void*,sqlite3*,int eTextRep,const char*)
  5007 );
  5008 SQLITE_API int sqlite3_collation_needed16(
  5009   sqlite3*, 
  5010   void*,
  5011   void(*)(void*,sqlite3*,int eTextRep,const void*)
  5012 );
  5014 #ifdef SQLITE_HAS_CODEC
  5015 /*
  5016 ** Specify the key for an encrypted database.  This routine should be
  5017 ** called right after sqlite3_open().
  5018 **
  5019 ** The code to implement this API is not available in the public release
  5020 ** of SQLite.
  5021 */
  5022 SQLITE_API int sqlite3_key(
  5023   sqlite3 *db,                   /* Database to be rekeyed */
  5024   const void *pKey, int nKey     /* The key */
  5025 );
  5027 /*
  5028 ** Change the key on an open database.  If the current database is not
  5029 ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
  5030 ** database is decrypted.
  5031 **
  5032 ** The code to implement this API is not available in the public release
  5033 ** of SQLite.
  5034 */
  5035 SQLITE_API int sqlite3_rekey(
  5036   sqlite3 *db,                   /* Database to be rekeyed */
  5037   const void *pKey, int nKey     /* The new key */
  5038 );
  5040 /*
  5041 ** Specify the activation key for a SEE database.  Unless 
  5042 ** activated, none of the SEE routines will work.
  5043 */
  5044 SQLITE_API void sqlite3_activate_see(
  5045   const char *zPassPhrase        /* Activation phrase */
  5046 );
  5047 #endif
  5049 #ifdef SQLITE_ENABLE_CEROD
  5050 /*
  5051 ** Specify the activation key for a CEROD database.  Unless 
  5052 ** activated, none of the CEROD routines will work.
  5053 */
  5054 SQLITE_API void sqlite3_activate_cerod(
  5055   const char *zPassPhrase        /* Activation phrase */
  5056 );
  5057 #endif
  5059 /*
  5060 ** CAPI3REF: Suspend Execution For A Short Time
  5061 **
  5062 ** The sqlite3_sleep() function causes the current thread to suspend execution
  5063 ** for at least a number of milliseconds specified in its parameter.
  5064 **
  5065 ** If the operating system does not support sleep requests with
  5066 ** millisecond time resolution, then the time will be rounded up to
  5067 ** the nearest second. The number of milliseconds of sleep actually
  5068 ** requested from the operating system is returned.
  5069 **
  5070 ** ^SQLite implements this interface by calling the xSleep()
  5071 ** method of the default [sqlite3_vfs] object.  If the xSleep() method
  5072 ** of the default VFS is not implemented correctly, or not implemented at
  5073 ** all, then the behavior of sqlite3_sleep() may deviate from the description
  5074 ** in the previous paragraphs.
  5075 */
  5076 SQLITE_API int sqlite3_sleep(int);
  5078 /*
  5079 ** CAPI3REF: Name Of The Folder Holding Temporary Files
  5080 **
  5081 ** ^(If this global variable is made to point to a string which is
  5082 ** the name of a folder (a.k.a. directory), then all temporary files
  5083 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
  5084 ** will be placed in that directory.)^  ^If this variable
  5085 ** is a NULL pointer, then SQLite performs a search for an appropriate
  5086 ** temporary file directory.
  5087 **
  5088 ** It is not safe to read or modify this variable in more than one
  5089 ** thread at a time.  It is not safe to read or modify this variable
  5090 ** if a [database connection] is being used at the same time in a separate
  5091 ** thread.
  5092 ** It is intended that this variable be set once
  5093 ** as part of process initialization and before any SQLite interface
  5094 ** routines have been called and that this variable remain unchanged
  5095 ** thereafter.
  5096 **
  5097 ** ^The [temp_store_directory pragma] may modify this variable and cause
  5098 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
  5099 ** the [temp_store_directory pragma] always assumes that any string
  5100 ** that this variable points to is held in memory obtained from 
  5101 ** [sqlite3_malloc] and the pragma may attempt to free that memory
  5102 ** using [sqlite3_free].
  5103 ** Hence, if this variable is modified directly, either it should be
  5104 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  5105 ** or else the use of the [temp_store_directory pragma] should be avoided.
  5106 **
  5107 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
  5108 ** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
  5109 ** features that require the use of temporary files may fail.  Here is an
  5110 ** example of how to do this using C++ with the Windows Runtime:
  5111 **
  5112 ** <blockquote><pre>
  5113 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
  5114 ** &nbsp;     TemporaryFolder->Path->Data();
  5115 ** char zPathBuf&#91;MAX_PATH + 1&#93;;
  5116 ** memset(zPathBuf, 0, sizeof(zPathBuf));
  5117 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
  5118 ** &nbsp;     NULL, NULL);
  5119 ** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
  5120 ** </pre></blockquote>
  5121 */
  5122 SQLITE_API char *sqlite3_temp_directory;
  5124 /*
  5125 ** CAPI3REF: Name Of The Folder Holding Database Files
  5126 **
  5127 ** ^(If this global variable is made to point to a string which is
  5128 ** the name of a folder (a.k.a. directory), then all database files
  5129 ** specified with a relative pathname and created or accessed by
  5130 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
  5131 ** to be relative to that directory.)^ ^If this variable is a NULL
  5132 ** pointer, then SQLite assumes that all database files specified
  5133 ** with a relative pathname are relative to the current directory
  5134 ** for the process.  Only the windows VFS makes use of this global
  5135 ** variable; it is ignored by the unix VFS.
  5136 **
  5137 ** Changing the value of this variable while a database connection is
  5138 ** open can result in a corrupt database.
  5139 **
  5140 ** It is not safe to read or modify this variable in more than one
  5141 ** thread at a time.  It is not safe to read or modify this variable
  5142 ** if a [database connection] is being used at the same time in a separate
  5143 ** thread.
  5144 ** It is intended that this variable be set once
  5145 ** as part of process initialization and before any SQLite interface
  5146 ** routines have been called and that this variable remain unchanged
  5147 ** thereafter.
  5148 **
  5149 ** ^The [data_store_directory pragma] may modify this variable and cause
  5150 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
  5151 ** the [data_store_directory pragma] always assumes that any string
  5152 ** that this variable points to is held in memory obtained from 
  5153 ** [sqlite3_malloc] and the pragma may attempt to free that memory
  5154 ** using [sqlite3_free].
  5155 ** Hence, if this variable is modified directly, either it should be
  5156 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  5157 ** or else the use of the [data_store_directory pragma] should be avoided.
  5158 */
  5159 SQLITE_API char *sqlite3_data_directory;
  5161 /*
  5162 ** CAPI3REF: Test For Auto-Commit Mode
  5163 ** KEYWORDS: {autocommit mode}
  5164 **
  5165 ** ^The sqlite3_get_autocommit() interface returns non-zero or
  5166 ** zero if the given database connection is or is not in autocommit mode,
  5167 ** respectively.  ^Autocommit mode is on by default.
  5168 ** ^Autocommit mode is disabled by a [BEGIN] statement.
  5169 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  5170 **
  5171 ** If certain kinds of errors occur on a statement within a multi-statement
  5172 ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
  5173 ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
  5174 ** transaction might be rolled back automatically.  The only way to
  5175 ** find out whether SQLite automatically rolled back the transaction after
  5176 ** an error is to use this function.
  5177 **
  5178 ** If another thread changes the autocommit status of the database
  5179 ** connection while this routine is running, then the return value
  5180 ** is undefined.
  5181 */
  5182 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
  5184 /*
  5185 ** CAPI3REF: Find The Database Handle Of A Prepared Statement
  5186 **
  5187 ** ^The sqlite3_db_handle interface returns the [database connection] handle
  5188 ** to which a [prepared statement] belongs.  ^The [database connection]
  5189 ** returned by sqlite3_db_handle is the same [database connection]
  5190 ** that was the first argument
  5191 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  5192 ** create the statement in the first place.
  5193 */
  5194 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  5196 /*
  5197 ** CAPI3REF: Return The Filename For A Database Connection
  5198 **
  5199 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  5200 ** associated with database N of connection D.  ^The main database file
  5201 ** has the name "main".  If there is no attached database N on the database
  5202 ** connection D, or if database N is a temporary or in-memory database, then
  5203 ** a NULL pointer is returned.
  5204 **
  5205 ** ^The filename returned by this function is the output of the
  5206 ** xFullPathname method of the [VFS].  ^In other words, the filename
  5207 ** will be an absolute pathname, even if the filename used
  5208 ** to open the database originally was a URI or relative pathname.
  5209 */
  5210 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  5212 /*
  5213 ** CAPI3REF: Determine if a database is read-only
  5214 **
  5215 ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
  5216 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
  5217 ** the name of a database on connection D.
  5218 */
  5219 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
  5221 /*
  5222 ** CAPI3REF: Find the next prepared statement
  5223 **
  5224 ** ^This interface returns a pointer to the next [prepared statement] after
  5225 ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
  5226 ** then this interface returns a pointer to the first prepared statement
  5227 ** associated with the database connection pDb.  ^If no prepared statement
  5228 ** satisfies the conditions of this routine, it returns NULL.
  5229 **
  5230 ** The [database connection] pointer D in a call to
  5231 ** [sqlite3_next_stmt(D,S)] must refer to an open database
  5232 ** connection and in particular must not be a NULL pointer.
  5233 */
  5234 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  5236 /*
  5237 ** CAPI3REF: Commit And Rollback Notification Callbacks
  5238 **
  5239 ** ^The sqlite3_commit_hook() interface registers a callback
  5240 ** function to be invoked whenever a transaction is [COMMIT | committed].
  5241 ** ^Any callback set by a previous call to sqlite3_commit_hook()
  5242 ** for the same database connection is overridden.
  5243 ** ^The sqlite3_rollback_hook() interface registers a callback
  5244 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
  5245 ** ^Any callback set by a previous call to sqlite3_rollback_hook()
  5246 ** for the same database connection is overridden.
  5247 ** ^The pArg argument is passed through to the callback.
  5248 ** ^If the callback on a commit hook function returns non-zero,
  5249 ** then the commit is converted into a rollback.
  5250 **
  5251 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
  5252 ** return the P argument from the previous call of the same function
  5253 ** on the same [database connection] D, or NULL for
  5254 ** the first call for each function on D.
  5255 **
  5256 ** The commit and rollback hook callbacks are not reentrant.
  5257 ** The callback implementation must not do anything that will modify
  5258 ** the database connection that invoked the callback.  Any actions
  5259 ** to modify the database connection must be deferred until after the
  5260 ** completion of the [sqlite3_step()] call that triggered the commit
  5261 ** or rollback hook in the first place.
  5262 ** Note that running any other SQL statements, including SELECT statements,
  5263 ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
  5264 ** the database connections for the meaning of "modify" in this paragraph.
  5265 **
  5266 ** ^Registering a NULL function disables the callback.
  5267 **
  5268 ** ^When the commit hook callback routine returns zero, the [COMMIT]
  5269 ** operation is allowed to continue normally.  ^If the commit hook
  5270 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
  5271 ** ^The rollback hook is invoked on a rollback that results from a commit
  5272 ** hook returning non-zero, just as it would be with any other rollback.
  5273 **
  5274 ** ^For the purposes of this API, a transaction is said to have been
  5275 ** rolled back if an explicit "ROLLBACK" statement is executed, or
  5276 ** an error or constraint causes an implicit rollback to occur.
  5277 ** ^The rollback callback is not invoked if a transaction is
  5278 ** automatically rolled back because the database connection is closed.
  5279 **
  5280 ** See also the [sqlite3_update_hook()] interface.
  5281 */
  5282 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  5283 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  5285 /*
  5286 ** CAPI3REF: Data Change Notification Callbacks
  5287 **
  5288 ** ^The sqlite3_update_hook() interface registers a callback function
  5289 ** with the [database connection] identified by the first argument
  5290 ** to be invoked whenever a row is updated, inserted or deleted.
  5291 ** ^Any callback set by a previous call to this function
  5292 ** for the same database connection is overridden.
  5293 **
  5294 ** ^The second argument is a pointer to the function to invoke when a
  5295 ** row is updated, inserted or deleted.
  5296 ** ^The first argument to the callback is a copy of the third argument
  5297 ** to sqlite3_update_hook().
  5298 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
  5299 ** or [SQLITE_UPDATE], depending on the operation that caused the callback
  5300 ** to be invoked.
  5301 ** ^The third and fourth arguments to the callback contain pointers to the
  5302 ** database and table name containing the affected row.
  5303 ** ^The final callback parameter is the [rowid] of the row.
  5304 ** ^In the case of an update, this is the [rowid] after the update takes place.
  5305 **
  5306 ** ^(The update hook is not invoked when internal system tables are
  5307 ** modified (i.e. sqlite_master and sqlite_sequence).)^
  5308 **
  5309 ** ^In the current implementation, the update hook
  5310 ** is not invoked when duplication rows are deleted because of an
  5311 ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  5312 ** invoked when rows are deleted using the [truncate optimization].
  5313 ** The exceptions defined in this paragraph might change in a future
  5314 ** release of SQLite.
  5315 **
  5316 ** The update hook implementation must not do anything that will modify
  5317 ** the database connection that invoked the update hook.  Any actions
  5318 ** to modify the database connection must be deferred until after the
  5319 ** completion of the [sqlite3_step()] call that triggered the update hook.
  5320 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  5321 ** database connections for the meaning of "modify" in this paragraph.
  5322 **
  5323 ** ^The sqlite3_update_hook(D,C,P) function
  5324 ** returns the P argument from the previous call
  5325 ** on the same [database connection] D, or NULL for
  5326 ** the first call on D.
  5327 **
  5328 ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
  5329 ** interfaces.
  5330 */
  5331 SQLITE_API void *sqlite3_update_hook(
  5332   sqlite3*, 
  5333   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
  5334   void*
  5335 );
  5337 /*
  5338 ** CAPI3REF: Enable Or Disable Shared Pager Cache
  5339 **
  5340 ** ^(This routine enables or disables the sharing of the database cache
  5341 ** and schema data structures between [database connection | connections]
  5342 ** to the same database. Sharing is enabled if the argument is true
  5343 ** and disabled if the argument is false.)^
  5344 **
  5345 ** ^Cache sharing is enabled and disabled for an entire process.
  5346 ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
  5347 ** sharing was enabled or disabled for each thread separately.
  5348 **
  5349 ** ^(The cache sharing mode set by this interface effects all subsequent
  5350 ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
  5351 ** Existing database connections continue use the sharing mode
  5352 ** that was in effect at the time they were opened.)^
  5353 **
  5354 ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
  5355 ** successfully.  An [error code] is returned otherwise.)^
  5356 **
  5357 ** ^Shared cache is disabled by default. But this might change in
  5358 ** future releases of SQLite.  Applications that care about shared
  5359 ** cache setting should set it explicitly.
  5360 **
  5361 ** This interface is threadsafe on processors where writing a
  5362 ** 32-bit integer is atomic.
  5363 **
  5364 ** See Also:  [SQLite Shared-Cache Mode]
  5365 */
  5366 SQLITE_API int sqlite3_enable_shared_cache(int);
  5368 /*
  5369 ** CAPI3REF: Attempt To Free Heap Memory
  5370 **
  5371 ** ^The sqlite3_release_memory() interface attempts to free N bytes
  5372 ** of heap memory by deallocating non-essential memory allocations
  5373 ** held by the database library.   Memory used to cache database
  5374 ** pages to improve performance is an example of non-essential memory.
  5375 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
  5376 ** which might be more or less than the amount requested.
  5377 ** ^The sqlite3_release_memory() routine is a no-op returning zero
  5378 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  5379 **
  5380 ** See also: [sqlite3_db_release_memory()]
  5381 */
  5382 SQLITE_API int sqlite3_release_memory(int);
  5384 /*
  5385 ** CAPI3REF: Free Memory Used By A Database Connection
  5386 **
  5387 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  5388 ** memory as possible from database connection D. Unlike the
  5389 ** [sqlite3_release_memory()] interface, this interface is effect even
  5390 ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  5391 ** omitted.
  5392 **
  5393 ** See also: [sqlite3_release_memory()]
  5394 */
  5395 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
  5397 /*
  5398 ** CAPI3REF: Impose A Limit On Heap Size
  5399 **
  5400 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
  5401 ** soft limit on the amount of heap memory that may be allocated by SQLite.
  5402 ** ^SQLite strives to keep heap memory utilization below the soft heap
  5403 ** limit by reducing the number of pages held in the page cache
  5404 ** as heap memory usages approaches the limit.
  5405 ** ^The soft heap limit is "soft" because even though SQLite strives to stay
  5406 ** below the limit, it will exceed the limit rather than generate
  5407 ** an [SQLITE_NOMEM] error.  In other words, the soft heap limit 
  5408 ** is advisory only.
  5409 **
  5410 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
  5411 ** the soft heap limit prior to the call, or negative in the case of an
  5412 ** error.  ^If the argument N is negative
  5413 ** then no change is made to the soft heap limit.  Hence, the current
  5414 ** size of the soft heap limit can be determined by invoking
  5415 ** sqlite3_soft_heap_limit64() with a negative argument.
  5416 **
  5417 ** ^If the argument N is zero then the soft heap limit is disabled.
  5418 **
  5419 ** ^(The soft heap limit is not enforced in the current implementation
  5420 ** if one or more of following conditions are true:
  5421 **
  5422 ** <ul>
  5423 ** <li> The soft heap limit is set to zero.
  5424 ** <li> Memory accounting is disabled using a combination of the
  5425 **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
  5426 **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
  5427 ** <li> An alternative page cache implementation is specified using
  5428 **      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
  5429 ** <li> The page cache allocates from its own memory pool supplied
  5430 **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
  5431 **      from the heap.
  5432 ** </ul>)^
  5433 **
  5434 ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
  5435 ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
  5436 ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
  5437 ** the soft heap limit is enforced on every memory allocation.  Without
  5438 ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
  5439 ** when memory is allocated by the page cache.  Testing suggests that because
  5440 ** the page cache is the predominate memory user in SQLite, most
  5441 ** applications will achieve adequate soft heap limit enforcement without
  5442 ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  5443 **
  5444 ** The circumstances under which SQLite will enforce the soft heap limit may
  5445 ** changes in future releases of SQLite.
  5446 */
  5447 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
  5449 /*
  5450 ** CAPI3REF: Deprecated Soft Heap Limit Interface
  5451 ** DEPRECATED
  5452 **
  5453 ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
  5454 ** interface.  This routine is provided for historical compatibility
  5455 ** only.  All new applications should use the
  5456 ** [sqlite3_soft_heap_limit64()] interface rather than this one.
  5457 */
  5458 SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
  5461 /*
  5462 ** CAPI3REF: Extract Metadata About A Column Of A Table
  5463 **
  5464 ** ^This routine returns metadata about a specific column of a specific
  5465 ** database table accessible using the [database connection] handle
  5466 ** passed as the first function argument.
  5467 **
  5468 ** ^The column is identified by the second, third and fourth parameters to
  5469 ** this function. ^The second parameter is either the name of the database
  5470 ** (i.e. "main", "temp", or an attached database) containing the specified
  5471 ** table or NULL. ^If it is NULL, then all attached databases are searched
  5472 ** for the table using the same algorithm used by the database engine to
  5473 ** resolve unqualified table references.
  5474 **
  5475 ** ^The third and fourth parameters to this function are the table and column
  5476 ** name of the desired column, respectively. Neither of these parameters
  5477 ** may be NULL.
  5478 **
  5479 ** ^Metadata is returned by writing to the memory locations passed as the 5th
  5480 ** and subsequent parameters to this function. ^Any of these arguments may be
  5481 ** NULL, in which case the corresponding element of metadata is omitted.
  5482 **
  5483 ** ^(<blockquote>
  5484 ** <table border="1">
  5485 ** <tr><th> Parameter <th> Output<br>Type <th>  Description
  5486 **
  5487 ** <tr><td> 5th <td> const char* <td> Data type
  5488 ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
  5489 ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
  5490 ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
  5491 ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
  5492 ** </table>
  5493 ** </blockquote>)^
  5494 **
  5495 ** ^The memory pointed to by the character pointers returned for the
  5496 ** declaration type and collation sequence is valid only until the next
  5497 ** call to any SQLite API function.
  5498 **
  5499 ** ^If the specified table is actually a view, an [error code] is returned.
  5500 **
  5501 ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
  5502 ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
  5503 ** parameters are set for the explicitly declared column. ^(If there is no
  5504 ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
  5505 ** parameters are set as follows:
  5506 **
  5507 ** <pre>
  5508 **     data type: "INTEGER"
  5509 **     collation sequence: "BINARY"
  5510 **     not null: 0
  5511 **     primary key: 1
  5512 **     auto increment: 0
  5513 ** </pre>)^
  5514 **
  5515 ** ^(This function may load one or more schemas from database files. If an
  5516 ** error occurs during this process, or if the requested table or column
  5517 ** cannot be found, an [error code] is returned and an error message left
  5518 ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
  5519 **
  5520 ** ^This API is only available if the library was compiled with the
  5521 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
  5522 */
  5523 SQLITE_API int sqlite3_table_column_metadata(
  5524   sqlite3 *db,                /* Connection handle */
  5525   const char *zDbName,        /* Database name or NULL */
  5526   const char *zTableName,     /* Table name */
  5527   const char *zColumnName,    /* Column name */
  5528   char const **pzDataType,    /* OUTPUT: Declared data type */
  5529   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
  5530   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  5531   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  5532   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  5533 );
  5535 /*
  5536 ** CAPI3REF: Load An Extension
  5537 **
  5538 ** ^This interface loads an SQLite extension library from the named file.
  5539 **
  5540 ** ^The sqlite3_load_extension() interface attempts to load an
  5541 ** SQLite extension library contained in the file zFile.
  5542 **
  5543 ** ^The entry point is zProc.
  5544 ** ^zProc may be 0, in which case the name of the entry point
  5545 ** defaults to "sqlite3_extension_init".
  5546 ** ^The sqlite3_load_extension() interface returns
  5547 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
  5548 ** ^If an error occurs and pzErrMsg is not 0, then the
  5549 ** [sqlite3_load_extension()] interface shall attempt to
  5550 ** fill *pzErrMsg with error message text stored in memory
  5551 ** obtained from [sqlite3_malloc()]. The calling function
  5552 ** should free this memory by calling [sqlite3_free()].
  5553 **
  5554 ** ^Extension loading must be enabled using
  5555 ** [sqlite3_enable_load_extension()] prior to calling this API,
  5556 ** otherwise an error will be returned.
  5557 **
  5558 ** See also the [load_extension() SQL function].
  5559 */
  5560 SQLITE_API int sqlite3_load_extension(
  5561   sqlite3 *db,          /* Load the extension into this database connection */
  5562   const char *zFile,    /* Name of the shared library containing extension */
  5563   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
  5564   char **pzErrMsg       /* Put error message here if not 0 */
  5565 );
  5567 /*
  5568 ** CAPI3REF: Enable Or Disable Extension Loading
  5569 **
  5570 ** ^So as not to open security holes in older applications that are
  5571 ** unprepared to deal with extension loading, and as a means of disabling
  5572 ** extension loading while evaluating user-entered SQL, the following API
  5573 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  5574 **
  5575 ** ^Extension loading is off by default. See ticket #1863.
  5576 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
  5577 ** to turn extension loading on and call it with onoff==0 to turn
  5578 ** it back off again.
  5579 */
  5580 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
  5582 /*
  5583 ** CAPI3REF: Automatically Load Statically Linked Extensions
  5584 **
  5585 ** ^This interface causes the xEntryPoint() function to be invoked for
  5586 ** each new [database connection] that is created.  The idea here is that
  5587 ** xEntryPoint() is the entry point for a statically linked SQLite extension
  5588 ** that is to be automatically loaded into all new database connections.
  5589 **
  5590 ** ^(Even though the function prototype shows that xEntryPoint() takes
  5591 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
  5592 ** arguments and expects and integer result as if the signature of the
  5593 ** entry point where as follows:
  5594 **
  5595 ** <blockquote><pre>
  5596 ** &nbsp;  int xEntryPoint(
  5597 ** &nbsp;    sqlite3 *db,
  5598 ** &nbsp;    const char **pzErrMsg,
  5599 ** &nbsp;    const struct sqlite3_api_routines *pThunk
  5600 ** &nbsp;  );
  5601 ** </pre></blockquote>)^
  5602 **
  5603 ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
  5604 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
  5605 ** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
  5606 ** is NULL before calling the xEntryPoint().  ^SQLite will invoke
  5607 ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
  5608 ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
  5609 ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
  5610 **
  5611 ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
  5612 ** on the list of automatic extensions is a harmless no-op. ^No entry point
  5613 ** will be called more than once for each database connection that is opened.
  5614 **
  5615 ** See also: [sqlite3_reset_auto_extension()].
  5616 */
  5617 SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
  5619 /*
  5620 ** CAPI3REF: Reset Automatic Extension Loading
  5621 **
  5622 ** ^This interface disables all automatic extensions previously
  5623 ** registered using [sqlite3_auto_extension()].
  5624 */
  5625 SQLITE_API void sqlite3_reset_auto_extension(void);
  5627 /*
  5628 ** The interface to the virtual-table mechanism is currently considered
  5629 ** to be experimental.  The interface might change in incompatible ways.
  5630 ** If this is a problem for you, do not use the interface at this time.
  5631 **
  5632 ** When the virtual-table mechanism stabilizes, we will declare the
  5633 ** interface fixed, support it indefinitely, and remove this comment.
  5634 */
  5636 /*
  5637 ** Structures used by the virtual table interface
  5638 */
  5639 typedef struct sqlite3_vtab sqlite3_vtab;
  5640 typedef struct sqlite3_index_info sqlite3_index_info;
  5641 typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
  5642 typedef struct sqlite3_module sqlite3_module;
  5644 /*
  5645 ** CAPI3REF: Virtual Table Object
  5646 ** KEYWORDS: sqlite3_module {virtual table module}
  5647 **
  5648 ** This structure, sometimes called a "virtual table module", 
  5649 ** defines the implementation of a [virtual tables].  
  5650 ** This structure consists mostly of methods for the module.
  5651 **
  5652 ** ^A virtual table module is created by filling in a persistent
  5653 ** instance of this structure and passing a pointer to that instance
  5654 ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
  5655 ** ^The registration remains valid until it is replaced by a different
  5656 ** module or until the [database connection] closes.  The content
  5657 ** of this structure must not change while it is registered with
  5658 ** any database connection.
  5659 */
  5660 struct sqlite3_module {
  5661   int iVersion;
  5662   int (*xCreate)(sqlite3*, void *pAux,
  5663                int argc, const char *const*argv,
  5664                sqlite3_vtab **ppVTab, char**);
  5665   int (*xConnect)(sqlite3*, void *pAux,
  5666                int argc, const char *const*argv,
  5667                sqlite3_vtab **ppVTab, char**);
  5668   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
  5669   int (*xDisconnect)(sqlite3_vtab *pVTab);
  5670   int (*xDestroy)(sqlite3_vtab *pVTab);
  5671   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
  5672   int (*xClose)(sqlite3_vtab_cursor*);
  5673   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
  5674                 int argc, sqlite3_value **argv);
  5675   int (*xNext)(sqlite3_vtab_cursor*);
  5676   int (*xEof)(sqlite3_vtab_cursor*);
  5677   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
  5678   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
  5679   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
  5680   int (*xBegin)(sqlite3_vtab *pVTab);
  5681   int (*xSync)(sqlite3_vtab *pVTab);
  5682   int (*xCommit)(sqlite3_vtab *pVTab);
  5683   int (*xRollback)(sqlite3_vtab *pVTab);
  5684   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
  5685                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
  5686                        void **ppArg);
  5687   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  5688   /* The methods above are in version 1 of the sqlite_module object. Those 
  5689   ** below are for version 2 and greater. */
  5690   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  5691   int (*xRelease)(sqlite3_vtab *pVTab, int);
  5692   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
  5693 };
  5695 /*
  5696 ** CAPI3REF: Virtual Table Indexing Information
  5697 ** KEYWORDS: sqlite3_index_info
  5698 **
  5699 ** The sqlite3_index_info structure and its substructures is used as part
  5700 ** of the [virtual table] interface to
  5701 ** pass information into and receive the reply from the [xBestIndex]
  5702 ** method of a [virtual table module].  The fields under **Inputs** are the
  5703 ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
  5704 ** results into the **Outputs** fields.
  5705 **
  5706 ** ^(The aConstraint[] array records WHERE clause constraints of the form:
  5707 **
  5708 ** <blockquote>column OP expr</blockquote>
  5709 **
  5710 ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
  5711 ** stored in aConstraint[].op using one of the
  5712 ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
  5713 ** ^(The index of the column is stored in
  5714 ** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
  5715 ** expr on the right-hand side can be evaluated (and thus the constraint
  5716 ** is usable) and false if it cannot.)^
  5717 **
  5718 ** ^The optimizer automatically inverts terms of the form "expr OP column"
  5719 ** and makes other simplifications to the WHERE clause in an attempt to
  5720 ** get as many WHERE clause terms into the form shown above as possible.
  5721 ** ^The aConstraint[] array only reports WHERE clause terms that are
  5722 ** relevant to the particular virtual table being queried.
  5723 **
  5724 ** ^Information about the ORDER BY clause is stored in aOrderBy[].
  5725 ** ^Each term of aOrderBy records a column of the ORDER BY clause.
  5726 **
  5727 ** The [xBestIndex] method must fill aConstraintUsage[] with information
  5728 ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
  5729 ** the right-hand side of the corresponding aConstraint[] is evaluated
  5730 ** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
  5731 ** is true, then the constraint is assumed to be fully handled by the
  5732 ** virtual table and is not checked again by SQLite.)^
  5733 **
  5734 ** ^The idxNum and idxPtr values are recorded and passed into the
  5735 ** [xFilter] method.
  5736 ** ^[sqlite3_free()] is used to free idxPtr if and only if
  5737 ** needToFreeIdxPtr is true.
  5738 **
  5739 ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
  5740 ** the correct order to satisfy the ORDER BY clause so that no separate
  5741 ** sorting step is required.
  5742 **
  5743 ** ^The estimatedCost value is an estimate of the cost of doing the
  5744 ** particular lookup.  A full scan of a table with N entries should have
  5745 ** a cost of N.  A binary search of a table of N entries should have a
  5746 ** cost of approximately log(N).
  5747 */
  5748 struct sqlite3_index_info {
  5749   /* Inputs */
  5750   int nConstraint;           /* Number of entries in aConstraint */
  5751   struct sqlite3_index_constraint {
  5752      int iColumn;              /* Column on left-hand side of constraint */
  5753      unsigned char op;         /* Constraint operator */
  5754      unsigned char usable;     /* True if this constraint is usable */
  5755      int iTermOffset;          /* Used internally - xBestIndex should ignore */
  5756   } *aConstraint;            /* Table of WHERE clause constraints */
  5757   int nOrderBy;              /* Number of terms in the ORDER BY clause */
  5758   struct sqlite3_index_orderby {
  5759      int iColumn;              /* Column number */
  5760      unsigned char desc;       /* True for DESC.  False for ASC. */
  5761   } *aOrderBy;               /* The ORDER BY clause */
  5762   /* Outputs */
  5763   struct sqlite3_index_constraint_usage {
  5764     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
  5765     unsigned char omit;      /* Do not code a test for this constraint */
  5766   } *aConstraintUsage;
  5767   int idxNum;                /* Number used to identify the index */
  5768   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
  5769   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
  5770   int orderByConsumed;       /* True if output is already ordered */
  5771   double estimatedCost;      /* Estimated cost of using this index */
  5772 };
  5774 /*
  5775 ** CAPI3REF: Virtual Table Constraint Operator Codes
  5776 **
  5777 ** These macros defined the allowed values for the
  5778 ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
  5779 ** an operator that is part of a constraint term in the wHERE clause of
  5780 ** a query that uses a [virtual table].
  5781 */
  5782 #define SQLITE_INDEX_CONSTRAINT_EQ    2
  5783 #define SQLITE_INDEX_CONSTRAINT_GT    4
  5784 #define SQLITE_INDEX_CONSTRAINT_LE    8
  5785 #define SQLITE_INDEX_CONSTRAINT_LT    16
  5786 #define SQLITE_INDEX_CONSTRAINT_GE    32
  5787 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
  5789 /*
  5790 ** CAPI3REF: Register A Virtual Table Implementation
  5791 **
  5792 ** ^These routines are used to register a new [virtual table module] name.
  5793 ** ^Module names must be registered before
  5794 ** creating a new [virtual table] using the module and before using a
  5795 ** preexisting [virtual table] for the module.
  5796 **
  5797 ** ^The module name is registered on the [database connection] specified
  5798 ** by the first parameter.  ^The name of the module is given by the 
  5799 ** second parameter.  ^The third parameter is a pointer to
  5800 ** the implementation of the [virtual table module].   ^The fourth
  5801 ** parameter is an arbitrary client data pointer that is passed through
  5802 ** into the [xCreate] and [xConnect] methods of the virtual table module
  5803 ** when a new virtual table is be being created or reinitialized.
  5804 **
  5805 ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
  5806 ** is a pointer to a destructor for the pClientData.  ^SQLite will
  5807 ** invoke the destructor function (if it is not NULL) when SQLite
  5808 ** no longer needs the pClientData pointer.  ^The destructor will also
  5809 ** be invoked if the call to sqlite3_create_module_v2() fails.
  5810 ** ^The sqlite3_create_module()
  5811 ** interface is equivalent to sqlite3_create_module_v2() with a NULL
  5812 ** destructor.
  5813 */
  5814 SQLITE_API int sqlite3_create_module(
  5815   sqlite3 *db,               /* SQLite connection to register module with */
  5816   const char *zName,         /* Name of the module */
  5817   const sqlite3_module *p,   /* Methods for the module */
  5818   void *pClientData          /* Client data for xCreate/xConnect */
  5819 );
  5820 SQLITE_API int sqlite3_create_module_v2(
  5821   sqlite3 *db,               /* SQLite connection to register module with */
  5822   const char *zName,         /* Name of the module */
  5823   const sqlite3_module *p,   /* Methods for the module */
  5824   void *pClientData,         /* Client data for xCreate/xConnect */
  5825   void(*xDestroy)(void*)     /* Module destructor function */
  5826 );
  5828 /*
  5829 ** CAPI3REF: Virtual Table Instance Object
  5830 ** KEYWORDS: sqlite3_vtab
  5831 **
  5832 ** Every [virtual table module] implementation uses a subclass
  5833 ** of this object to describe a particular instance
  5834 ** of the [virtual table].  Each subclass will
  5835 ** be tailored to the specific needs of the module implementation.
  5836 ** The purpose of this superclass is to define certain fields that are
  5837 ** common to all module implementations.
  5838 **
  5839 ** ^Virtual tables methods can set an error message by assigning a
  5840 ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
  5841 ** take care that any prior string is freed by a call to [sqlite3_free()]
  5842 ** prior to assigning a new string to zErrMsg.  ^After the error message
  5843 ** is delivered up to the client application, the string will be automatically
  5844 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
  5845 */
  5846 struct sqlite3_vtab {
  5847   const sqlite3_module *pModule;  /* The module for this virtual table */
  5848   int nRef;                       /* NO LONGER USED */
  5849   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
  5850   /* Virtual table implementations will typically add additional fields */
  5851 };
  5853 /*
  5854 ** CAPI3REF: Virtual Table Cursor Object
  5855 ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
  5856 **
  5857 ** Every [virtual table module] implementation uses a subclass of the
  5858 ** following structure to describe cursors that point into the
  5859 ** [virtual table] and are used
  5860 ** to loop through the virtual table.  Cursors are created using the
  5861 ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
  5862 ** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
  5863 ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
  5864 ** of the module.  Each module implementation will define
  5865 ** the content of a cursor structure to suit its own needs.
  5866 **
  5867 ** This superclass exists in order to define fields of the cursor that
  5868 ** are common to all implementations.
  5869 */
  5870 struct sqlite3_vtab_cursor {
  5871   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
  5872   /* Virtual table implementations will typically add additional fields */
  5873 };
  5875 /*
  5876 ** CAPI3REF: Declare The Schema Of A Virtual Table
  5877 **
  5878 ** ^The [xCreate] and [xConnect] methods of a
  5879 ** [virtual table module] call this interface
  5880 ** to declare the format (the names and datatypes of the columns) of
  5881 ** the virtual tables they implement.
  5882 */
  5883 SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  5885 /*
  5886 ** CAPI3REF: Overload A Function For A Virtual Table
  5887 **
  5888 ** ^(Virtual tables can provide alternative implementations of functions
  5889 ** using the [xFindFunction] method of the [virtual table module].  
  5890 ** But global versions of those functions
  5891 ** must exist in order to be overloaded.)^
  5892 **
  5893 ** ^(This API makes sure a global version of a function with a particular
  5894 ** name and number of parameters exists.  If no such function exists
  5895 ** before this API is called, a new function is created.)^  ^The implementation
  5896 ** of the new function always causes an exception to be thrown.  So
  5897 ** the new function is not good for anything by itself.  Its only
  5898 ** purpose is to be a placeholder function that can be overloaded
  5899 ** by a [virtual table].
  5900 */
  5901 SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
  5903 /*
  5904 ** The interface to the virtual-table mechanism defined above (back up
  5905 ** to a comment remarkably similar to this one) is currently considered
  5906 ** to be experimental.  The interface might change in incompatible ways.
  5907 ** If this is a problem for you, do not use the interface at this time.
  5908 **
  5909 ** When the virtual-table mechanism stabilizes, we will declare the
  5910 ** interface fixed, support it indefinitely, and remove this comment.
  5911 */
  5913 /*
  5914 ** CAPI3REF: A Handle To An Open BLOB
  5915 ** KEYWORDS: {BLOB handle} {BLOB handles}
  5916 **
  5917 ** An instance of this object represents an open BLOB on which
  5918 ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
  5919 ** ^Objects of this type are created by [sqlite3_blob_open()]
  5920 ** and destroyed by [sqlite3_blob_close()].
  5921 ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
  5922 ** can be used to read or write small subsections of the BLOB.
  5923 ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  5924 */
  5925 typedef struct sqlite3_blob sqlite3_blob;
  5927 /*
  5928 ** CAPI3REF: Open A BLOB For Incremental I/O
  5929 **
  5930 ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  5931 ** in row iRow, column zColumn, table zTable in database zDb;
  5932 ** in other words, the same BLOB that would be selected by:
  5933 **
  5934 ** <pre>
  5935 **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
  5936 ** </pre>)^
  5937 **
  5938 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
  5939 ** and write access. ^If it is zero, the BLOB is opened for read access.
  5940 ** ^It is not possible to open a column that is part of an index or primary 
  5941 ** key for writing. ^If [foreign key constraints] are enabled, it is 
  5942 ** not possible to open a column that is part of a [child key] for writing.
  5943 **
  5944 ** ^Note that the database name is not the filename that contains
  5945 ** the database but rather the symbolic name of the database that
  5946 ** appears after the AS keyword when the database is connected using [ATTACH].
  5947 ** ^For the main database file, the database name is "main".
  5948 ** ^For TEMP tables, the database name is "temp".
  5949 **
  5950 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
  5951 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
  5952 ** to be a null pointer.)^
  5953 ** ^This function sets the [database connection] error code and message
  5954 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
  5955 ** functions. ^Note that the *ppBlob variable is always initialized in a
  5956 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
  5957 ** regardless of the success or failure of this routine.
  5958 **
  5959 ** ^(If the row that a BLOB handle points to is modified by an
  5960 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  5961 ** then the BLOB handle is marked as "expired".
  5962 ** This is true if any column of the row is changed, even a column
  5963 ** other than the one the BLOB handle is open on.)^
  5964 ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
  5965 ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
  5966 ** ^(Changes written into a BLOB prior to the BLOB expiring are not
  5967 ** rolled back by the expiration of the BLOB.  Such changes will eventually
  5968 ** commit if the transaction continues to completion.)^
  5969 **
  5970 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
  5971 ** the opened blob.  ^The size of a blob may not be changed by this
  5972 ** interface.  Use the [UPDATE] SQL command to change the size of a
  5973 ** blob.
  5974 **
  5975 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  5976 ** and the built-in [zeroblob] SQL function can be used, if desired,
  5977 ** to create an empty, zero-filled blob in which to read or write using
  5978 ** this interface.
  5979 **
  5980 ** To avoid a resource leak, every open [BLOB handle] should eventually
  5981 ** be released by a call to [sqlite3_blob_close()].
  5982 */
  5983 SQLITE_API int sqlite3_blob_open(
  5984   sqlite3*,
  5985   const char *zDb,
  5986   const char *zTable,
  5987   const char *zColumn,
  5988   sqlite3_int64 iRow,
  5989   int flags,
  5990   sqlite3_blob **ppBlob
  5991 );
  5993 /*
  5994 ** CAPI3REF: Move a BLOB Handle to a New Row
  5995 **
  5996 ** ^This function is used to move an existing blob handle so that it points
  5997 ** to a different row of the same database table. ^The new row is identified
  5998 ** by the rowid value passed as the second argument. Only the row can be
  5999 ** changed. ^The database, table and column on which the blob handle is open
  6000 ** remain the same. Moving an existing blob handle to a new row can be
  6001 ** faster than closing the existing handle and opening a new one.
  6002 **
  6003 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  6004 ** it must exist and there must be either a blob or text value stored in
  6005 ** the nominated column.)^ ^If the new row is not present in the table, or if
  6006 ** it does not contain a blob or text value, or if another error occurs, an
  6007 ** SQLite error code is returned and the blob handle is considered aborted.
  6008 ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
  6009 ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
  6010 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
  6011 ** always returns zero.
  6012 **
  6013 ** ^This function sets the database handle error code and message.
  6014 */
  6015 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  6017 /*
  6018 ** CAPI3REF: Close A BLOB Handle
  6019 **
  6020 ** ^Closes an open [BLOB handle].
  6021 **
  6022 ** ^Closing a BLOB shall cause the current transaction to commit
  6023 ** if there are no other BLOBs, no pending prepared statements, and the
  6024 ** database connection is in [autocommit mode].
  6025 ** ^If any writes were made to the BLOB, they might be held in cache
  6026 ** until the close operation if they will fit.
  6027 **
  6028 ** ^(Closing the BLOB often forces the changes
  6029 ** out to disk and so if any I/O errors occur, they will likely occur
  6030 ** at the time when the BLOB is closed.  Any errors that occur during
  6031 ** closing are reported as a non-zero return value.)^
  6032 **
  6033 ** ^(The BLOB is closed unconditionally.  Even if this routine returns
  6034 ** an error code, the BLOB is still closed.)^
  6035 **
  6036 ** ^Calling this routine with a null pointer (such as would be returned
  6037 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
  6038 */
  6039 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
  6041 /*
  6042 ** CAPI3REF: Return The Size Of An Open BLOB
  6043 **
  6044 ** ^Returns the size in bytes of the BLOB accessible via the 
  6045 ** successfully opened [BLOB handle] in its only argument.  ^The
  6046 ** incremental blob I/O routines can only read or overwriting existing
  6047 ** blob content; they cannot change the size of a blob.
  6048 **
  6049 ** This routine only works on a [BLOB handle] which has been created
  6050 ** by a prior successful call to [sqlite3_blob_open()] and which has not
  6051 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  6052 ** to this routine results in undefined and probably undesirable behavior.
  6053 */
  6054 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
  6056 /*
  6057 ** CAPI3REF: Read Data From A BLOB Incrementally
  6058 **
  6059 ** ^(This function is used to read data from an open [BLOB handle] into a
  6060 ** caller-supplied buffer. N bytes of data are copied into buffer Z
  6061 ** from the open BLOB, starting at offset iOffset.)^
  6062 **
  6063 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  6064 ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
  6065 ** less than zero, [SQLITE_ERROR] is returned and no data is read.
  6066 ** ^The size of the blob (and hence the maximum value of N+iOffset)
  6067 ** can be determined using the [sqlite3_blob_bytes()] interface.
  6068 **
  6069 ** ^An attempt to read from an expired [BLOB handle] fails with an
  6070 ** error code of [SQLITE_ABORT].
  6071 **
  6072 ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
  6073 ** Otherwise, an [error code] or an [extended error code] is returned.)^
  6074 **
  6075 ** This routine only works on a [BLOB handle] which has been created
  6076 ** by a prior successful call to [sqlite3_blob_open()] and which has not
  6077 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  6078 ** to this routine results in undefined and probably undesirable behavior.
  6079 **
  6080 ** See also: [sqlite3_blob_write()].
  6081 */
  6082 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  6084 /*
  6085 ** CAPI3REF: Write Data Into A BLOB Incrementally
  6086 **
  6087 ** ^This function is used to write data into an open [BLOB handle] from a
  6088 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
  6089 ** into the open BLOB, starting at offset iOffset.
  6090 **
  6091 ** ^If the [BLOB handle] passed as the first argument was not opened for
  6092 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
  6093 ** this function returns [SQLITE_READONLY].
  6094 **
  6095 ** ^This function may only modify the contents of the BLOB; it is
  6096 ** not possible to increase the size of a BLOB using this API.
  6097 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  6098 ** [SQLITE_ERROR] is returned and no data is written.  ^If N is
  6099 ** less than zero [SQLITE_ERROR] is returned and no data is written.
  6100 ** The size of the BLOB (and hence the maximum value of N+iOffset)
  6101 ** can be determined using the [sqlite3_blob_bytes()] interface.
  6102 **
  6103 ** ^An attempt to write to an expired [BLOB handle] fails with an
  6104 ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
  6105 ** before the [BLOB handle] expired are not rolled back by the
  6106 ** expiration of the handle, though of course those changes might
  6107 ** have been overwritten by the statement that expired the BLOB handle
  6108 ** or by other independent statements.
  6109 **
  6110 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  6111 ** Otherwise, an  [error code] or an [extended error code] is returned.)^
  6112 **
  6113 ** This routine only works on a [BLOB handle] which has been created
  6114 ** by a prior successful call to [sqlite3_blob_open()] and which has not
  6115 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  6116 ** to this routine results in undefined and probably undesirable behavior.
  6117 **
  6118 ** See also: [sqlite3_blob_read()].
  6119 */
  6120 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
  6122 /*
  6123 ** CAPI3REF: Virtual File System Objects
  6124 **
  6125 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
  6126 ** that SQLite uses to interact
  6127 ** with the underlying operating system.  Most SQLite builds come with a
  6128 ** single default VFS that is appropriate for the host computer.
  6129 ** New VFSes can be registered and existing VFSes can be unregistered.
  6130 ** The following interfaces are provided.
  6131 **
  6132 ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
  6133 ** ^Names are case sensitive.
  6134 ** ^Names are zero-terminated UTF-8 strings.
  6135 ** ^If there is no match, a NULL pointer is returned.
  6136 ** ^If zVfsName is NULL then the default VFS is returned.
  6137 **
  6138 ** ^New VFSes are registered with sqlite3_vfs_register().
  6139 ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
  6140 ** ^The same VFS can be registered multiple times without injury.
  6141 ** ^To make an existing VFS into the default VFS, register it again
  6142 ** with the makeDflt flag set.  If two different VFSes with the
  6143 ** same name are registered, the behavior is undefined.  If a
  6144 ** VFS is registered with a name that is NULL or an empty string,
  6145 ** then the behavior is undefined.
  6146 **
  6147 ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
  6148 ** ^(If the default VFS is unregistered, another VFS is chosen as
  6149 ** the default.  The choice for the new VFS is arbitrary.)^
  6150 */
  6151 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
  6152 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
  6153 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
  6155 /*
  6156 ** CAPI3REF: Mutexes
  6157 **
  6158 ** The SQLite core uses these routines for thread
  6159 ** synchronization. Though they are intended for internal
  6160 ** use by SQLite, code that links against SQLite is
  6161 ** permitted to use any of these routines.
  6162 **
  6163 ** The SQLite source code contains multiple implementations
  6164 ** of these mutex routines.  An appropriate implementation
  6165 ** is selected automatically at compile-time.  ^(The following
  6166 ** implementations are available in the SQLite core:
  6167 **
  6168 ** <ul>
  6169 ** <li>   SQLITE_MUTEX_PTHREADS
  6170 ** <li>   SQLITE_MUTEX_W32
  6171 ** <li>   SQLITE_MUTEX_NOOP
  6172 ** </ul>)^
  6173 **
  6174 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  6175 ** that does no real locking and is appropriate for use in
  6176 ** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
  6177 ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
  6178 ** and Windows.
  6179 **
  6180 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  6181 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  6182 ** implementation is included with the library. In this case the
  6183 ** application must supply a custom mutex implementation using the
  6184 ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
  6185 ** before calling sqlite3_initialize() or any other public sqlite3_
  6186 ** function that calls sqlite3_initialize().)^
  6187 **
  6188 ** ^The sqlite3_mutex_alloc() routine allocates a new
  6189 ** mutex and returns a pointer to it. ^If it returns NULL
  6190 ** that means that a mutex could not be allocated.  ^SQLite
  6191 ** will unwind its stack and return an error.  ^(The argument
  6192 ** to sqlite3_mutex_alloc() is one of these integer constants:
  6193 **
  6194 ** <ul>
  6195 ** <li>  SQLITE_MUTEX_FAST
  6196 ** <li>  SQLITE_MUTEX_RECURSIVE
  6197 ** <li>  SQLITE_MUTEX_STATIC_MASTER
  6198 ** <li>  SQLITE_MUTEX_STATIC_MEM
  6199 ** <li>  SQLITE_MUTEX_STATIC_MEM2
  6200 ** <li>  SQLITE_MUTEX_STATIC_PRNG
  6201 ** <li>  SQLITE_MUTEX_STATIC_LRU
  6202 ** <li>  SQLITE_MUTEX_STATIC_LRU2
  6203 ** </ul>)^
  6204 **
  6205 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
  6206 ** cause sqlite3_mutex_alloc() to create
  6207 ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  6208 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  6209 ** The mutex implementation does not need to make a distinction
  6210 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  6211 ** not want to.  ^SQLite will only request a recursive mutex in
  6212 ** cases where it really needs one.  ^If a faster non-recursive mutex
  6213 ** implementation is available on the host platform, the mutex subsystem
  6214 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  6215 **
  6216 ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
  6217 ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
  6218 ** a pointer to a static preexisting mutex.  ^Six static mutexes are
  6219 ** used by the current version of SQLite.  Future versions of SQLite
  6220 ** may add additional static mutexes.  Static mutexes are for internal
  6221 ** use by SQLite only.  Applications that use SQLite mutexes should
  6222 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  6223 ** SQLITE_MUTEX_RECURSIVE.
  6224 **
  6225 ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  6226 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  6227 ** returns a different mutex on every call.  ^But for the static
  6228 ** mutex types, the same mutex is returned on every call that has
  6229 ** the same type number.
  6230 **
  6231 ** ^The sqlite3_mutex_free() routine deallocates a previously
  6232 ** allocated dynamic mutex.  ^SQLite is careful to deallocate every
  6233 ** dynamic mutex that it allocates.  The dynamic mutexes must not be in
  6234 ** use when they are deallocated.  Attempting to deallocate a static
  6235 ** mutex results in undefined behavior.  ^SQLite never deallocates
  6236 ** a static mutex.
  6237 **
  6238 ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  6239 ** to enter a mutex.  ^If another thread is already within the mutex,
  6240 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  6241 ** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
  6242 ** upon successful entry.  ^(Mutexes created using
  6243 ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
  6244 ** In such cases the,
  6245 ** mutex must be exited an equal number of times before another thread
  6246 ** can enter.)^  ^(If the same thread tries to enter any other
  6247 ** kind of mutex more than once, the behavior is undefined.
  6248 ** SQLite will never exhibit
  6249 ** such behavior in its own use of mutexes.)^
  6250 **
  6251 ** ^(Some systems (for example, Windows 95) do not support the operation
  6252 ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
  6253 ** will always return SQLITE_BUSY.  The SQLite core only ever uses
  6254 ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
  6255 **
  6256 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
  6257 ** previously entered by the same thread.   ^(The behavior
  6258 ** is undefined if the mutex is not currently entered by the
  6259 ** calling thread or is not currently allocated.  SQLite will
  6260 ** never do either.)^
  6261 **
  6262 ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
  6263 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
  6264 ** behave as no-ops.
  6265 **
  6266 ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
  6267 */
  6268 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
  6269 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
  6270 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
  6271 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
  6272 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
  6274 /*
  6275 ** CAPI3REF: Mutex Methods Object
  6276 **
  6277 ** An instance of this structure defines the low-level routines
  6278 ** used to allocate and use mutexes.
  6279 **
  6280 ** Usually, the default mutex implementations provided by SQLite are
  6281 ** sufficient, however the user has the option of substituting a custom
  6282 ** implementation for specialized deployments or systems for which SQLite
  6283 ** does not provide a suitable implementation. In this case, the user
  6284 ** creates and populates an instance of this structure to pass
  6285 ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
  6286 ** Additionally, an instance of this structure can be used as an
  6287 ** output variable when querying the system for the current mutex
  6288 ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
  6289 **
  6290 ** ^The xMutexInit method defined by this structure is invoked as
  6291 ** part of system initialization by the sqlite3_initialize() function.
  6292 ** ^The xMutexInit routine is called by SQLite exactly once for each
  6293 ** effective call to [sqlite3_initialize()].
  6294 **
  6295 ** ^The xMutexEnd method defined by this structure is invoked as
  6296 ** part of system shutdown by the sqlite3_shutdown() function. The
  6297 ** implementation of this method is expected to release all outstanding
  6298 ** resources obtained by the mutex methods implementation, especially
  6299 ** those obtained by the xMutexInit method.  ^The xMutexEnd()
  6300 ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
  6301 **
  6302 ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
  6303 ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
  6304 ** xMutexNotheld) implement the following interfaces (respectively):
  6305 **
  6306 ** <ul>
  6307 **   <li>  [sqlite3_mutex_alloc()] </li>
  6308 **   <li>  [sqlite3_mutex_free()] </li>
  6309 **   <li>  [sqlite3_mutex_enter()] </li>
  6310 **   <li>  [sqlite3_mutex_try()] </li>
  6311 **   <li>  [sqlite3_mutex_leave()] </li>
  6312 **   <li>  [sqlite3_mutex_held()] </li>
  6313 **   <li>  [sqlite3_mutex_notheld()] </li>
  6314 ** </ul>)^
  6315 **
  6316 ** The only difference is that the public sqlite3_XXX functions enumerated
  6317 ** above silently ignore any invocations that pass a NULL pointer instead
  6318 ** of a valid mutex handle. The implementations of the methods defined
  6319 ** by this structure are not required to handle this case, the results
  6320 ** of passing a NULL pointer instead of a valid mutex handle are undefined
  6321 ** (i.e. it is acceptable to provide an implementation that segfaults if
  6322 ** it is passed a NULL pointer).
  6323 **
  6324 ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
  6325 ** invoke xMutexInit() multiple times within the same process and without
  6326 ** intervening calls to xMutexEnd().  Second and subsequent calls to
  6327 ** xMutexInit() must be no-ops.
  6328 **
  6329 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
  6330 ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
  6331 ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
  6332 ** memory allocation for a fast or recursive mutex.
  6333 **
  6334 ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
  6335 ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
  6336 ** If xMutexInit fails in any way, it is expected to clean up after itself
  6337 ** prior to returning.
  6338 */
  6339 typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
  6340 struct sqlite3_mutex_methods {
  6341   int (*xMutexInit)(void);
  6342   int (*xMutexEnd)(void);
  6343   sqlite3_mutex *(*xMutexAlloc)(int);
  6344   void (*xMutexFree)(sqlite3_mutex *);
  6345   void (*xMutexEnter)(sqlite3_mutex *);
  6346   int (*xMutexTry)(sqlite3_mutex *);
  6347   void (*xMutexLeave)(sqlite3_mutex *);
  6348   int (*xMutexHeld)(sqlite3_mutex *);
  6349   int (*xMutexNotheld)(sqlite3_mutex *);
  6350 };
  6352 /*
  6353 ** CAPI3REF: Mutex Verification Routines
  6354 **
  6355 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
  6356 ** are intended for use inside assert() statements.  ^The SQLite core
  6357 ** never uses these routines except inside an assert() and applications
  6358 ** are advised to follow the lead of the core.  ^The SQLite core only
  6359 ** provides implementations for these routines when it is compiled
  6360 ** with the SQLITE_DEBUG flag.  ^External mutex implementations
  6361 ** are only required to provide these routines if SQLITE_DEBUG is
  6362 ** defined and if NDEBUG is not defined.
  6363 **
  6364 ** ^These routines should return true if the mutex in their argument
  6365 ** is held or not held, respectively, by the calling thread.
  6366 **
  6367 ** ^The implementation is not required to provide versions of these
  6368 ** routines that actually work. If the implementation does not provide working
  6369 ** versions of these routines, it should at least provide stubs that always
  6370 ** return true so that one does not get spurious assertion failures.
  6371 **
  6372 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
  6373 ** the routine should return 1.   This seems counter-intuitive since
  6374 ** clearly the mutex cannot be held if it does not exist.  But
  6375 ** the reason the mutex does not exist is because the build is not
  6376 ** using mutexes.  And we do not want the assert() containing the
  6377 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
  6378 ** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
  6379 ** interface should also return 1 when given a NULL pointer.
  6380 */
  6381 #ifndef NDEBUG
  6382 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
  6383 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
  6384 #endif
  6386 /*
  6387 ** CAPI3REF: Mutex Types
  6388 **
  6389 ** The [sqlite3_mutex_alloc()] interface takes a single argument
  6390 ** which is one of these integer constants.
  6391 **
  6392 ** The set of static mutexes may change from one SQLite release to the
  6393 ** next.  Applications that override the built-in mutex logic must be
  6394 ** prepared to accommodate additional static mutexes.
  6395 */
  6396 #define SQLITE_MUTEX_FAST             0
  6397 #define SQLITE_MUTEX_RECURSIVE        1
  6398 #define SQLITE_MUTEX_STATIC_MASTER    2
  6399 #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  6400 #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  6401 #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  6402 #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
  6403 #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  6404 #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
  6405 #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  6407 /*
  6408 ** CAPI3REF: Retrieve the mutex for a database connection
  6409 **
  6410 ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  6411 ** serializes access to the [database connection] given in the argument
  6412 ** when the [threading mode] is Serialized.
  6413 ** ^If the [threading mode] is Single-thread or Multi-thread then this
  6414 ** routine returns a NULL pointer.
  6415 */
  6416 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
  6418 /*
  6419 ** CAPI3REF: Low-Level Control Of Database Files
  6420 **
  6421 ** ^The [sqlite3_file_control()] interface makes a direct call to the
  6422 ** xFileControl method for the [sqlite3_io_methods] object associated
  6423 ** with a particular database identified by the second argument. ^The
  6424 ** name of the database is "main" for the main database or "temp" for the
  6425 ** TEMP database, or the name that appears after the AS keyword for
  6426 ** databases that are added using the [ATTACH] SQL command.
  6427 ** ^A NULL pointer can be used in place of "main" to refer to the
  6428 ** main database file.
  6429 ** ^The third and fourth parameters to this routine
  6430 ** are passed directly through to the second and third parameters of
  6431 ** the xFileControl method.  ^The return value of the xFileControl
  6432 ** method becomes the return value of this routine.
  6433 **
  6434 ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
  6435 ** a pointer to the underlying [sqlite3_file] object to be written into
  6436 ** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
  6437 ** case is a short-circuit path which does not actually invoke the
  6438 ** underlying sqlite3_io_methods.xFileControl method.
  6439 **
  6440 ** ^If the second parameter (zDbName) does not match the name of any
  6441 ** open database file, then SQLITE_ERROR is returned.  ^This error
  6442 ** code is not remembered and will not be recalled by [sqlite3_errcode()]
  6443 ** or [sqlite3_errmsg()].  The underlying xFileControl method might
  6444 ** also return SQLITE_ERROR.  There is no way to distinguish between
  6445 ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
  6446 ** xFileControl method.
  6447 **
  6448 ** See also: [SQLITE_FCNTL_LOCKSTATE]
  6449 */
  6450 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
  6452 /*
  6453 ** CAPI3REF: Testing Interface
  6454 **
  6455 ** ^The sqlite3_test_control() interface is used to read out internal
  6456 ** state of SQLite and to inject faults into SQLite for testing
  6457 ** purposes.  ^The first parameter is an operation code that determines
  6458 ** the number, meaning, and operation of all subsequent parameters.
  6459 **
  6460 ** This interface is not for use by applications.  It exists solely
  6461 ** for verifying the correct operation of the SQLite library.  Depending
  6462 ** on how the SQLite library is compiled, this interface might not exist.
  6463 **
  6464 ** The details of the operation codes, their meanings, the parameters
  6465 ** they take, and what they do are all subject to change without notice.
  6466 ** Unlike most of the SQLite API, this function is not guaranteed to
  6467 ** operate consistently from one release to the next.
  6468 */
  6469 SQLITE_API int sqlite3_test_control(int op, ...);
  6471 /*
  6472 ** CAPI3REF: Testing Interface Operation Codes
  6473 **
  6474 ** These constants are the valid operation code parameters used
  6475 ** as the first argument to [sqlite3_test_control()].
  6476 **
  6477 ** These parameters and their meanings are subject to change
  6478 ** without notice.  These values are for testing purposes only.
  6479 ** Applications should not use any of these parameters or the
  6480 ** [sqlite3_test_control()] interface.
  6481 */
  6482 #define SQLITE_TESTCTRL_FIRST                    5
  6483 #define SQLITE_TESTCTRL_PRNG_SAVE                5
  6484 #define SQLITE_TESTCTRL_PRNG_RESTORE             6
  6485 #define SQLITE_TESTCTRL_PRNG_RESET               7
  6486 #define SQLITE_TESTCTRL_BITVEC_TEST              8
  6487 #define SQLITE_TESTCTRL_FAULT_INSTALL            9
  6488 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  6489 #define SQLITE_TESTCTRL_PENDING_BYTE            11
  6490 #define SQLITE_TESTCTRL_ASSERT                  12
  6491 #define SQLITE_TESTCTRL_ALWAYS                  13
  6492 #define SQLITE_TESTCTRL_RESERVE                 14
  6493 #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6494 #define SQLITE_TESTCTRL_ISKEYWORD               16
  6495 #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6496 #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6497 #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
  6498 #define SQLITE_TESTCTRL_LAST                    19
  6500 /*
  6501 ** CAPI3REF: SQLite Runtime Status
  6502 **
  6503 ** ^This interface is used to retrieve runtime status information
  6504 ** about the performance of SQLite, and optionally to reset various
  6505 ** highwater marks.  ^The first argument is an integer code for
  6506 ** the specific parameter to measure.  ^(Recognized integer codes
  6507 ** are of the form [status parameters | SQLITE_STATUS_...].)^
  6508 ** ^The current value of the parameter is returned into *pCurrent.
  6509 ** ^The highest recorded value is returned in *pHighwater.  ^If the
  6510 ** resetFlag is true, then the highest record value is reset after
  6511 ** *pHighwater is written.  ^(Some parameters do not record the highest
  6512 ** value.  For those parameters
  6513 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
  6514 ** ^(Other parameters record only the highwater mark and not the current
  6515 ** value.  For these latter parameters nothing is written into *pCurrent.)^
  6516 **
  6517 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
  6518 ** non-zero [error code] on failure.
  6519 **
  6520 ** This routine is threadsafe but is not atomic.  This routine can be
  6521 ** called while other threads are running the same or different SQLite
  6522 ** interfaces.  However the values returned in *pCurrent and
  6523 ** *pHighwater reflect the status of SQLite at different points in time
  6524 ** and it is possible that another thread might change the parameter
  6525 ** in between the times when *pCurrent and *pHighwater are written.
  6526 **
  6527 ** See also: [sqlite3_db_status()]
  6528 */
  6529 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
  6532 /*
  6533 ** CAPI3REF: Status Parameters
  6534 ** KEYWORDS: {status parameters}
  6535 **
  6536 ** These integer constants designate various run-time status parameters
  6537 ** that can be returned by [sqlite3_status()].
  6538 **
  6539 ** <dl>
  6540 ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
  6541 ** <dd>This parameter is the current amount of memory checked out
  6542 ** using [sqlite3_malloc()], either directly or indirectly.  The
  6543 ** figure includes calls made to [sqlite3_malloc()] by the application
  6544 ** and internal memory usage by the SQLite library.  Scratch memory
  6545 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
  6546 ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
  6547 ** this parameter.  The amount returned is the sum of the allocation
  6548 ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
  6549 **
  6550 ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
  6551 ** <dd>This parameter records the largest memory allocation request
  6552 ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
  6553 ** internal equivalents).  Only the value returned in the
  6554 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  6555 ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6556 **
  6557 ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
  6558 ** <dd>This parameter records the number of separate memory allocations
  6559 ** currently checked out.</dd>)^
  6560 **
  6561 ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
  6562 ** <dd>This parameter returns the number of pages used out of the
  6563 ** [pagecache memory allocator] that was configured using 
  6564 ** [SQLITE_CONFIG_PAGECACHE].  The
  6565 ** value returned is in pages, not in bytes.</dd>)^
  6566 **
  6567 ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] 
  6568 ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
  6569 ** <dd>This parameter returns the number of bytes of page cache
  6570 ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
  6571 ** buffer and where forced to overflow to [sqlite3_malloc()].  The
  6572 ** returned value includes allocations that overflowed because they
  6573 ** where too large (they were larger than the "sz" parameter to
  6574 ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
  6575 ** no space was left in the page cache.</dd>)^
  6576 **
  6577 ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
  6578 ** <dd>This parameter records the largest memory allocation request
  6579 ** handed to [pagecache memory allocator].  Only the value returned in the
  6580 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  6581 ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6582 **
  6583 ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
  6584 ** <dd>This parameter returns the number of allocations used out of the
  6585 ** [scratch memory allocator] configured using
  6586 ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
  6587 ** in bytes.  Since a single thread may only have one scratch allocation
  6588 ** outstanding at time, this parameter also reports the number of threads
  6589 ** using scratch memory at the same time.</dd>)^
  6590 **
  6591 ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
  6592 ** <dd>This parameter returns the number of bytes of scratch memory
  6593 ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
  6594 ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
  6595 ** returned include overflows because the requested allocation was too
  6596 ** larger (that is, because the requested allocation was larger than the
  6597 ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
  6598 ** slots were available.
  6599 ** </dd>)^
  6600 **
  6601 ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
  6602 ** <dd>This parameter records the largest memory allocation request
  6603 ** handed to [scratch memory allocator].  Only the value returned in the
  6604 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  6605 ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6606 **
  6607 ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
  6608 ** <dd>This parameter records the deepest parser stack.  It is only
  6609 ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
  6610 ** </dl>
  6611 **
  6612 ** New status parameters may be added from time to time.
  6613 */
  6614 #define SQLITE_STATUS_MEMORY_USED          0
  6615 #define SQLITE_STATUS_PAGECACHE_USED       1
  6616 #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
  6617 #define SQLITE_STATUS_SCRATCH_USED         3
  6618 #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
  6619 #define SQLITE_STATUS_MALLOC_SIZE          5
  6620 #define SQLITE_STATUS_PARSER_STACK         6
  6621 #define SQLITE_STATUS_PAGECACHE_SIZE       7
  6622 #define SQLITE_STATUS_SCRATCH_SIZE         8
  6623 #define SQLITE_STATUS_MALLOC_COUNT         9
  6625 /*
  6626 ** CAPI3REF: Database Connection Status
  6627 **
  6628 ** ^This interface is used to retrieve runtime status information 
  6629 ** about a single [database connection].  ^The first argument is the
  6630 ** database connection object to be interrogated.  ^The second argument
  6631 ** is an integer constant, taken from the set of
  6632 ** [SQLITE_DBSTATUS options], that
  6633 ** determines the parameter to interrogate.  The set of 
  6634 ** [SQLITE_DBSTATUS options] is likely
  6635 ** to grow in future releases of SQLite.
  6636 **
  6637 ** ^The current value of the requested parameter is written into *pCur
  6638 ** and the highest instantaneous value is written into *pHiwtr.  ^If
  6639 ** the resetFlg is true, then the highest instantaneous value is
  6640 ** reset back down to the current value.
  6641 **
  6642 ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
  6643 ** non-zero [error code] on failure.
  6644 **
  6645 ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
  6646 */
  6647 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
  6649 /*
  6650 ** CAPI3REF: Status Parameters for database connections
  6651 ** KEYWORDS: {SQLITE_DBSTATUS options}
  6652 **
  6653 ** These constants are the available integer "verbs" that can be passed as
  6654 ** the second argument to the [sqlite3_db_status()] interface.
  6655 **
  6656 ** New verbs may be added in future releases of SQLite. Existing verbs
  6657 ** might be discontinued. Applications should check the return code from
  6658 ** [sqlite3_db_status()] to make sure that the call worked.
  6659 ** The [sqlite3_db_status()] interface will return a non-zero error code
  6660 ** if a discontinued or unsupported verb is invoked.
  6661 **
  6662 ** <dl>
  6663 ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
  6664 ** <dd>This parameter returns the number of lookaside memory slots currently
  6665 ** checked out.</dd>)^
  6666 **
  6667 ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
  6668 ** <dd>This parameter returns the number malloc attempts that were 
  6669 ** satisfied using lookaside memory. Only the high-water value is meaningful;
  6670 ** the current value is always zero.)^
  6671 **
  6672 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
  6673 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
  6674 ** <dd>This parameter returns the number malloc attempts that might have
  6675 ** been satisfied using lookaside memory but failed due to the amount of
  6676 ** memory requested being larger than the lookaside slot size.
  6677 ** Only the high-water value is meaningful;
  6678 ** the current value is always zero.)^
  6679 **
  6680 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
  6681 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
  6682 ** <dd>This parameter returns the number malloc attempts that might have
  6683 ** been satisfied using lookaside memory but failed due to all lookaside
  6684 ** memory already being in use.
  6685 ** Only the high-water value is meaningful;
  6686 ** the current value is always zero.)^
  6687 **
  6688 ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  6689 ** <dd>This parameter returns the approximate number of of bytes of heap
  6690 ** memory used by all pager caches associated with the database connection.)^
  6691 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  6692 **
  6693 ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
  6694 ** <dd>This parameter returns the approximate number of of bytes of heap
  6695 ** memory used to store the schema for all databases associated
  6696 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 
  6697 ** ^The full amount of memory used by the schemas is reported, even if the
  6698 ** schema memory is shared with other database connections due to
  6699 ** [shared cache mode] being enabled.
  6700 ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
  6701 **
  6702 ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  6703 ** <dd>This parameter returns the approximate number of of bytes of heap
  6704 ** and lookaside memory used by all prepared statements associated with
  6705 ** the database connection.)^
  6706 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  6707 ** </dd>
  6708 **
  6709 ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
  6710 ** <dd>This parameter returns the number of pager cache hits that have
  6711 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 
  6712 ** is always 0.
  6713 ** </dd>
  6714 **
  6715 ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
  6716 ** <dd>This parameter returns the number of pager cache misses that have
  6717 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
  6718 ** is always 0.
  6719 ** </dd>
  6720 **
  6721 ** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
  6722 ** <dd>This parameter returns the number of dirty cache entries that have
  6723 ** been written to disk. Specifically, the number of pages written to the
  6724 ** wal file in wal mode databases, or the number of pages written to the
  6725 ** database file in rollback mode databases. Any pages written as part of
  6726 ** transaction rollback or database recovery operations are not included.
  6727 ** If an IO or other error occurs while writing a page to disk, the effect
  6728 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  6729 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  6730 ** </dd>
  6731 ** </dl>
  6732 */
  6733 #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  6734 #define SQLITE_DBSTATUS_CACHE_USED           1
  6735 #define SQLITE_DBSTATUS_SCHEMA_USED          2
  6736 #define SQLITE_DBSTATUS_STMT_USED            3
  6737 #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  6738 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  6739 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  6740 #define SQLITE_DBSTATUS_CACHE_HIT            7
  6741 #define SQLITE_DBSTATUS_CACHE_MISS           8
  6742 #define SQLITE_DBSTATUS_CACHE_WRITE          9
  6743 #define SQLITE_DBSTATUS_MAX                  9   /* Largest defined DBSTATUS */
  6746 /*
  6747 ** CAPI3REF: Prepared Statement Status
  6748 **
  6749 ** ^(Each prepared statement maintains various
  6750 ** [SQLITE_STMTSTATUS counters] that measure the number
  6751 ** of times it has performed specific operations.)^  These counters can
  6752 ** be used to monitor the performance characteristics of the prepared
  6753 ** statements.  For example, if the number of table steps greatly exceeds
  6754 ** the number of table searches or result rows, that would tend to indicate
  6755 ** that the prepared statement is using a full table scan rather than
  6756 ** an index.  
  6757 **
  6758 ** ^(This interface is used to retrieve and reset counter values from
  6759 ** a [prepared statement].  The first argument is the prepared statement
  6760 ** object to be interrogated.  The second argument
  6761 ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
  6762 ** to be interrogated.)^
  6763 ** ^The current value of the requested counter is returned.
  6764 ** ^If the resetFlg is true, then the counter is reset to zero after this
  6765 ** interface call returns.
  6766 **
  6767 ** See also: [sqlite3_status()] and [sqlite3_db_status()].
  6768 */
  6769 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
  6771 /*
  6772 ** CAPI3REF: Status Parameters for prepared statements
  6773 ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
  6774 **
  6775 ** These preprocessor macros define integer codes that name counter
  6776 ** values associated with the [sqlite3_stmt_status()] interface.
  6777 ** The meanings of the various counters are as follows:
  6778 **
  6779 ** <dl>
  6780 ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
  6781 ** <dd>^This is the number of times that SQLite has stepped forward in
  6782 ** a table as part of a full table scan.  Large numbers for this counter
  6783 ** may indicate opportunities for performance improvement through 
  6784 ** careful use of indices.</dd>
  6785 **
  6786 ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
  6787 ** <dd>^This is the number of sort operations that have occurred.
  6788 ** A non-zero value in this counter may indicate an opportunity to
  6789 ** improvement performance through careful use of indices.</dd>
  6790 **
  6791 ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
  6792 ** <dd>^This is the number of rows inserted into transient indices that
  6793 ** were created automatically in order to help joins run faster.
  6794 ** A non-zero value in this counter may indicate an opportunity to
  6795 ** improvement performance by adding permanent indices that do not
  6796 ** need to be reinitialized each time the statement is run.</dd>
  6797 ** </dl>
  6798 */
  6799 #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
  6800 #define SQLITE_STMTSTATUS_SORT              2
  6801 #define SQLITE_STMTSTATUS_AUTOINDEX         3
  6803 /*
  6804 ** CAPI3REF: Custom Page Cache Object
  6805 **
  6806 ** The sqlite3_pcache type is opaque.  It is implemented by
  6807 ** the pluggable module.  The SQLite core has no knowledge of
  6808 ** its size or internal structure and never deals with the
  6809 ** sqlite3_pcache object except by holding and passing pointers
  6810 ** to the object.
  6811 **
  6812 ** See [sqlite3_pcache_methods2] for additional information.
  6813 */
  6814 typedef struct sqlite3_pcache sqlite3_pcache;
  6816 /*
  6817 ** CAPI3REF: Custom Page Cache Object
  6818 **
  6819 ** The sqlite3_pcache_page object represents a single page in the
  6820 ** page cache.  The page cache will allocate instances of this
  6821 ** object.  Various methods of the page cache use pointers to instances
  6822 ** of this object as parameters or as their return value.
  6823 **
  6824 ** See [sqlite3_pcache_methods2] for additional information.
  6825 */
  6826 typedef struct sqlite3_pcache_page sqlite3_pcache_page;
  6827 struct sqlite3_pcache_page {
  6828   void *pBuf;        /* The content of the page */
  6829   void *pExtra;      /* Extra information associated with the page */
  6830 };
  6832 /*
  6833 ** CAPI3REF: Application Defined Page Cache.
  6834 ** KEYWORDS: {page cache}
  6835 **
  6836 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
  6837 ** register an alternative page cache implementation by passing in an 
  6838 ** instance of the sqlite3_pcache_methods2 structure.)^
  6839 ** In many applications, most of the heap memory allocated by 
  6840 ** SQLite is used for the page cache.
  6841 ** By implementing a 
  6842 ** custom page cache using this API, an application can better control
  6843 ** the amount of memory consumed by SQLite, the way in which 
  6844 ** that memory is allocated and released, and the policies used to 
  6845 ** determine exactly which parts of a database file are cached and for 
  6846 ** how long.
  6847 **
  6848 ** The alternative page cache mechanism is an
  6849 ** extreme measure that is only needed by the most demanding applications.
  6850 ** The built-in page cache is recommended for most uses.
  6851 **
  6852 ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
  6853 ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
  6854 ** the application may discard the parameter after the call to
  6855 ** [sqlite3_config()] returns.)^
  6856 **
  6857 ** [[the xInit() page cache method]]
  6858 ** ^(The xInit() method is called once for each effective 
  6859 ** call to [sqlite3_initialize()])^
  6860 ** (usually only once during the lifetime of the process). ^(The xInit()
  6861 ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
  6862 ** The intent of the xInit() method is to set up global data structures 
  6863 ** required by the custom page cache implementation. 
  6864 ** ^(If the xInit() method is NULL, then the 
  6865 ** built-in default page cache is used instead of the application defined
  6866 ** page cache.)^
  6867 **
  6868 ** [[the xShutdown() page cache method]]
  6869 ** ^The xShutdown() method is called by [sqlite3_shutdown()].
  6870 ** It can be used to clean up 
  6871 ** any outstanding resources before process shutdown, if required.
  6872 ** ^The xShutdown() method may be NULL.
  6873 **
  6874 ** ^SQLite automatically serializes calls to the xInit method,
  6875 ** so the xInit method need not be threadsafe.  ^The
  6876 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  6877 ** not need to be threadsafe either.  All other methods must be threadsafe
  6878 ** in multithreaded applications.
  6879 **
  6880 ** ^SQLite will never invoke xInit() more than once without an intervening
  6881 ** call to xShutdown().
  6882 **
  6883 ** [[the xCreate() page cache methods]]
  6884 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
  6885 ** SQLite will typically create one cache instance for each open database file,
  6886 ** though this is not guaranteed. ^The
  6887 ** first parameter, szPage, is the size in bytes of the pages that must
  6888 ** be allocated by the cache.  ^szPage will always a power of two.  ^The
  6889 ** second parameter szExtra is a number of bytes of extra storage 
  6890 ** associated with each page cache entry.  ^The szExtra parameter will
  6891 ** a number less than 250.  SQLite will use the
  6892 ** extra szExtra bytes on each page to store metadata about the underlying
  6893 ** database page on disk.  The value passed into szExtra depends
  6894 ** on the SQLite version, the target platform, and how SQLite was compiled.
  6895 ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
  6896 ** created will be used to cache database pages of a file stored on disk, or
  6897 ** false if it is used for an in-memory database. The cache implementation
  6898 ** does not have to do anything special based with the value of bPurgeable;
  6899 ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
  6900 ** never invoke xUnpin() except to deliberately delete a page.
  6901 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
  6902 ** false will always have the "discard" flag set to true.  
  6903 ** ^Hence, a cache created with bPurgeable false will
  6904 ** never contain any unpinned pages.
  6905 **
  6906 ** [[the xCachesize() page cache method]]
  6907 ** ^(The xCachesize() method may be called at any time by SQLite to set the
  6908 ** suggested maximum cache-size (number of pages stored by) the cache
  6909 ** instance passed as the first argument. This is the value configured using
  6910 ** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
  6911 ** parameter, the implementation is not required to do anything with this
  6912 ** value; it is advisory only.
  6913 **
  6914 ** [[the xPagecount() page cache methods]]
  6915 ** The xPagecount() method must return the number of pages currently
  6916 ** stored in the cache, both pinned and unpinned.
  6917 ** 
  6918 ** [[the xFetch() page cache methods]]
  6919 ** The xFetch() method locates a page in the cache and returns a pointer to 
  6920 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
  6921 ** The pBuf element of the returned sqlite3_pcache_page object will be a
  6922 ** pointer to a buffer of szPage bytes used to store the content of a 
  6923 ** single database page.  The pExtra element of sqlite3_pcache_page will be
  6924 ** a pointer to the szExtra bytes of extra storage that SQLite has requested
  6925 ** for each entry in the page cache.
  6926 **
  6927 ** The page to be fetched is determined by the key. ^The minimum key value
  6928 ** is 1.  After it has been retrieved using xFetch, the page is considered
  6929 ** to be "pinned".
  6930 **
  6931 ** If the requested page is already in the page cache, then the page cache
  6932 ** implementation must return a pointer to the page buffer with its content
  6933 ** intact.  If the requested page is not already in the cache, then the
  6934 ** cache implementation should use the value of the createFlag
  6935 ** parameter to help it determined what action to take:
  6936 **
  6937 ** <table border=1 width=85% align=center>
  6938 ** <tr><th> createFlag <th> Behaviour when page is not already in cache
  6939 ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
  6940 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
  6941 **                 Otherwise return NULL.
  6942 ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
  6943 **                 NULL if allocating a new page is effectively impossible.
  6944 ** </table>
  6945 **
  6946 ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
  6947 ** will only use a createFlag of 2 after a prior call with a createFlag of 1
  6948 ** failed.)^  In between the to xFetch() calls, SQLite may
  6949 ** attempt to unpin one or more cache pages by spilling the content of
  6950 ** pinned pages to disk and synching the operating system disk cache.
  6951 **
  6952 ** [[the xUnpin() page cache method]]
  6953 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
  6954 ** as its second argument.  If the third parameter, discard, is non-zero,
  6955 ** then the page must be evicted from the cache.
  6956 ** ^If the discard parameter is
  6957 ** zero, then the page may be discarded or retained at the discretion of
  6958 ** page cache implementation. ^The page cache implementation
  6959 ** may choose to evict unpinned pages at any time.
  6960 **
  6961 ** The cache must not perform any reference counting. A single 
  6962 ** call to xUnpin() unpins the page regardless of the number of prior calls 
  6963 ** to xFetch().
  6964 **
  6965 ** [[the xRekey() page cache methods]]
  6966 ** The xRekey() method is used to change the key value associated with the
  6967 ** page passed as the second argument. If the cache
  6968 ** previously contains an entry associated with newKey, it must be
  6969 ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
  6970 ** to be pinned.
  6971 **
  6972 ** When SQLite calls the xTruncate() method, the cache must discard all
  6973 ** existing cache entries with page numbers (keys) greater than or equal
  6974 ** to the value of the iLimit parameter passed to xTruncate(). If any
  6975 ** of these pages are pinned, they are implicitly unpinned, meaning that
  6976 ** they can be safely discarded.
  6977 **
  6978 ** [[the xDestroy() page cache method]]
  6979 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
  6980 ** All resources associated with the specified cache should be freed. ^After
  6981 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
  6982 ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
  6983 ** functions.
  6984 **
  6985 ** [[the xShrink() page cache method]]
  6986 ** ^SQLite invokes the xShrink() method when it wants the page cache to
  6987 ** free up as much of heap memory as possible.  The page cache implementation
  6988 ** is not obligated to free any memory, but well-behaved implementations should
  6989 ** do their best.
  6990 */
  6991 typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
  6992 struct sqlite3_pcache_methods2 {
  6993   int iVersion;
  6994   void *pArg;
  6995   int (*xInit)(void*);
  6996   void (*xShutdown)(void*);
  6997   sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
  6998   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  6999   int (*xPagecount)(sqlite3_pcache*);
  7000   sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  7001   void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
  7002   void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, 
  7003       unsigned oldKey, unsigned newKey);
  7004   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  7005   void (*xDestroy)(sqlite3_pcache*);
  7006   void (*xShrink)(sqlite3_pcache*);
  7007 };
  7009 /*
  7010 ** This is the obsolete pcache_methods object that has now been replaced
  7011 ** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
  7012 ** retained in the header file for backwards compatibility only.
  7013 */
  7014 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
  7015 struct sqlite3_pcache_methods {
  7016   void *pArg;
  7017   int (*xInit)(void*);
  7018   void (*xShutdown)(void*);
  7019   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
  7020   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  7021   int (*xPagecount)(sqlite3_pcache*);
  7022   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  7023   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
  7024   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
  7025   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  7026   void (*xDestroy)(sqlite3_pcache*);
  7027 };
  7030 /*
  7031 ** CAPI3REF: Online Backup Object
  7032 **
  7033 ** The sqlite3_backup object records state information about an ongoing
  7034 ** online backup operation.  ^The sqlite3_backup object is created by
  7035 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
  7036 ** [sqlite3_backup_finish()].
  7037 **
  7038 ** See Also: [Using the SQLite Online Backup API]
  7039 */
  7040 typedef struct sqlite3_backup sqlite3_backup;
  7042 /*
  7043 ** CAPI3REF: Online Backup API.
  7044 **
  7045 ** The backup API copies the content of one database into another.
  7046 ** It is useful either for creating backups of databases or
  7047 ** for copying in-memory databases to or from persistent files. 
  7048 **
  7049 ** See Also: [Using the SQLite Online Backup API]
  7050 **
  7051 ** ^SQLite holds a write transaction open on the destination database file
  7052 ** for the duration of the backup operation.
  7053 ** ^The source database is read-locked only while it is being read;
  7054 ** it is not locked continuously for the entire backup operation.
  7055 ** ^Thus, the backup may be performed on a live source database without
  7056 ** preventing other database connections from
  7057 ** reading or writing to the source database while the backup is underway.
  7058 ** 
  7059 ** ^(To perform a backup operation: 
  7060 **   <ol>
  7061 **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
  7062 **         backup, 
  7063 **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 
  7064 **         the data between the two databases, and finally
  7065 **     <li><b>sqlite3_backup_finish()</b> is called to release all resources 
  7066 **         associated with the backup operation. 
  7067 **   </ol>)^
  7068 ** There should be exactly one call to sqlite3_backup_finish() for each
  7069 ** successful call to sqlite3_backup_init().
  7070 **
  7071 ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
  7072 **
  7073 ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the 
  7074 ** [database connection] associated with the destination database 
  7075 ** and the database name, respectively.
  7076 ** ^The database name is "main" for the main database, "temp" for the
  7077 ** temporary database, or the name specified after the AS keyword in
  7078 ** an [ATTACH] statement for an attached database.
  7079 ** ^The S and M arguments passed to 
  7080 ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
  7081 ** and database name of the source database, respectively.
  7082 ** ^The source and destination [database connections] (parameters S and D)
  7083 ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
  7084 ** an error.
  7085 **
  7086 ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
  7087 ** returned and an error code and error message are stored in the
  7088 ** destination [database connection] D.
  7089 ** ^The error code and message for the failed call to sqlite3_backup_init()
  7090 ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
  7091 ** [sqlite3_errmsg16()] functions.
  7092 ** ^A successful call to sqlite3_backup_init() returns a pointer to an
  7093 ** [sqlite3_backup] object.
  7094 ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
  7095 ** sqlite3_backup_finish() functions to perform the specified backup 
  7096 ** operation.
  7097 **
  7098 ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
  7099 **
  7100 ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between 
  7101 ** the source and destination databases specified by [sqlite3_backup] object B.
  7102 ** ^If N is negative, all remaining source pages are copied. 
  7103 ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
  7104 ** are still more pages to be copied, then the function returns [SQLITE_OK].
  7105 ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
  7106 ** from source to destination, then it returns [SQLITE_DONE].
  7107 ** ^If an error occurs while running sqlite3_backup_step(B,N),
  7108 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
  7109 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
  7110 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
  7111 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
  7112 **
  7113 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
  7114 ** <ol>
  7115 ** <li> the destination database was opened read-only, or
  7116 ** <li> the destination database is using write-ahead-log journaling
  7117 ** and the destination and source page sizes differ, or
  7118 ** <li> the destination database is an in-memory database and the
  7119 ** destination and source page sizes differ.
  7120 ** </ol>)^
  7121 **
  7122 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
  7123 ** the [sqlite3_busy_handler | busy-handler function]
  7124 ** is invoked (if one is specified). ^If the 
  7125 ** busy-handler returns non-zero before the lock is available, then 
  7126 ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
  7127 ** sqlite3_backup_step() can be retried later. ^If the source
  7128 ** [database connection]
  7129 ** is being used to write to the source database when sqlite3_backup_step()
  7130 ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
  7131 ** case the call to sqlite3_backup_step() can be retried later on. ^(If
  7132 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
  7133 ** [SQLITE_READONLY] is returned, then 
  7134 ** there is no point in retrying the call to sqlite3_backup_step(). These 
  7135 ** errors are considered fatal.)^  The application must accept 
  7136 ** that the backup operation has failed and pass the backup operation handle 
  7137 ** to the sqlite3_backup_finish() to release associated resources.
  7138 **
  7139 ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
  7140 ** on the destination file. ^The exclusive lock is not released until either 
  7141 ** sqlite3_backup_finish() is called or the backup operation is complete 
  7142 ** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
  7143 ** sqlite3_backup_step() obtains a [shared lock] on the source database that
  7144 ** lasts for the duration of the sqlite3_backup_step() call.
  7145 ** ^Because the source database is not locked between calls to
  7146 ** sqlite3_backup_step(), the source database may be modified mid-way
  7147 ** through the backup process.  ^If the source database is modified by an
  7148 ** external process or via a database connection other than the one being
  7149 ** used by the backup operation, then the backup will be automatically
  7150 ** restarted by the next call to sqlite3_backup_step(). ^If the source 
  7151 ** database is modified by the using the same database connection as is used
  7152 ** by the backup operation, then the backup database is automatically
  7153 ** updated at the same time.
  7154 **
  7155 ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
  7156 **
  7157 ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the 
  7158 ** application wishes to abandon the backup operation, the application
  7159 ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
  7160 ** ^The sqlite3_backup_finish() interfaces releases all
  7161 ** resources associated with the [sqlite3_backup] object. 
  7162 ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
  7163 ** active write-transaction on the destination database is rolled back.
  7164 ** The [sqlite3_backup] object is invalid
  7165 ** and may not be used following a call to sqlite3_backup_finish().
  7166 **
  7167 ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
  7168 ** sqlite3_backup_step() errors occurred, regardless or whether or not
  7169 ** sqlite3_backup_step() completed.
  7170 ** ^If an out-of-memory condition or IO error occurred during any prior
  7171 ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
  7172 ** sqlite3_backup_finish() returns the corresponding [error code].
  7173 **
  7174 ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
  7175 ** is not a permanent error and does not affect the return value of
  7176 ** sqlite3_backup_finish().
  7177 **
  7178 ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
  7179 ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
  7180 **
  7181 ** ^Each call to sqlite3_backup_step() sets two values inside
  7182 ** the [sqlite3_backup] object: the number of pages still to be backed
  7183 ** up and the total number of pages in the source database file.
  7184 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
  7185 ** retrieve these two values, respectively.
  7186 **
  7187 ** ^The values returned by these functions are only updated by
  7188 ** sqlite3_backup_step(). ^If the source database is modified during a backup
  7189 ** operation, then the values are not updated to account for any extra
  7190 ** pages that need to be updated or the size of the source database file
  7191 ** changing.
  7192 **
  7193 ** <b>Concurrent Usage of Database Handles</b>
  7194 **
  7195 ** ^The source [database connection] may be used by the application for other
  7196 ** purposes while a backup operation is underway or being initialized.
  7197 ** ^If SQLite is compiled and configured to support threadsafe database
  7198 ** connections, then the source database connection may be used concurrently
  7199 ** from within other threads.
  7200 **
  7201 ** However, the application must guarantee that the destination 
  7202 ** [database connection] is not passed to any other API (by any thread) after 
  7203 ** sqlite3_backup_init() is called and before the corresponding call to
  7204 ** sqlite3_backup_finish().  SQLite does not currently check to see
  7205 ** if the application incorrectly accesses the destination [database connection]
  7206 ** and so no error code is reported, but the operations may malfunction
  7207 ** nevertheless.  Use of the destination database connection while a
  7208 ** backup is in progress might also also cause a mutex deadlock.
  7209 **
  7210 ** If running in [shared cache mode], the application must
  7211 ** guarantee that the shared cache used by the destination database
  7212 ** is not accessed while the backup is running. In practice this means
  7213 ** that the application must guarantee that the disk file being 
  7214 ** backed up to is not accessed by any connection within the process,
  7215 ** not just the specific connection that was passed to sqlite3_backup_init().
  7216 **
  7217 ** The [sqlite3_backup] object itself is partially threadsafe. Multiple 
  7218 ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
  7219 ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
  7220 ** APIs are not strictly speaking threadsafe. If they are invoked at the
  7221 ** same time as another thread is invoking sqlite3_backup_step() it is
  7222 ** possible that they return invalid values.
  7223 */
  7224 SQLITE_API sqlite3_backup *sqlite3_backup_init(
  7225   sqlite3 *pDest,                        /* Destination database handle */
  7226   const char *zDestName,                 /* Destination database name */
  7227   sqlite3 *pSource,                      /* Source database handle */
  7228   const char *zSourceName                /* Source database name */
  7229 );
  7230 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
  7231 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
  7232 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
  7233 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
  7235 /*
  7236 ** CAPI3REF: Unlock Notification
  7237 **
  7238 ** ^When running in shared-cache mode, a database operation may fail with
  7239 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  7240 ** individual tables within the shared-cache cannot be obtained. See
  7241 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
  7242 ** ^This API may be used to register a callback that SQLite will invoke 
  7243 ** when the connection currently holding the required lock relinquishes it.
  7244 ** ^This API is only available if the library was compiled with the
  7245 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
  7246 **
  7247 ** See Also: [Using the SQLite Unlock Notification Feature].
  7248 **
  7249 ** ^Shared-cache locks are released when a database connection concludes
  7250 ** its current transaction, either by committing it or rolling it back. 
  7251 **
  7252 ** ^When a connection (known as the blocked connection) fails to obtain a
  7253 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
  7254 ** identity of the database connection (the blocking connection) that
  7255 ** has locked the required resource is stored internally. ^After an 
  7256 ** application receives an SQLITE_LOCKED error, it may call the
  7257 ** sqlite3_unlock_notify() method with the blocked connection handle as 
  7258 ** the first argument to register for a callback that will be invoked
  7259 ** when the blocking connections current transaction is concluded. ^The
  7260 ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
  7261 ** call that concludes the blocking connections transaction.
  7262 **
  7263 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
  7264 ** there is a chance that the blocking connection will have already
  7265 ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
  7266 ** If this happens, then the specified callback is invoked immediately,
  7267 ** from within the call to sqlite3_unlock_notify().)^
  7268 **
  7269 ** ^If the blocked connection is attempting to obtain a write-lock on a
  7270 ** shared-cache table, and more than one other connection currently holds
  7271 ** a read-lock on the same table, then SQLite arbitrarily selects one of 
  7272 ** the other connections to use as the blocking connection.
  7273 **
  7274 ** ^(There may be at most one unlock-notify callback registered by a 
  7275 ** blocked connection. If sqlite3_unlock_notify() is called when the
  7276 ** blocked connection already has a registered unlock-notify callback,
  7277 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
  7278 ** called with a NULL pointer as its second argument, then any existing
  7279 ** unlock-notify callback is canceled. ^The blocked connections 
  7280 ** unlock-notify callback may also be canceled by closing the blocked
  7281 ** connection using [sqlite3_close()].
  7282 **
  7283 ** The unlock-notify callback is not reentrant. If an application invokes
  7284 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
  7285 ** crash or deadlock may be the result.
  7286 **
  7287 ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
  7288 ** returns SQLITE_OK.
  7289 **
  7290 ** <b>Callback Invocation Details</b>
  7291 **
  7292 ** When an unlock-notify callback is registered, the application provides a 
  7293 ** single void* pointer that is passed to the callback when it is invoked.
  7294 ** However, the signature of the callback function allows SQLite to pass
  7295 ** it an array of void* context pointers. The first argument passed to
  7296 ** an unlock-notify callback is a pointer to an array of void* pointers,
  7297 ** and the second is the number of entries in the array.
  7298 **
  7299 ** When a blocking connections transaction is concluded, there may be
  7300 ** more than one blocked connection that has registered for an unlock-notify
  7301 ** callback. ^If two or more such blocked connections have specified the
  7302 ** same callback function, then instead of invoking the callback function
  7303 ** multiple times, it is invoked once with the set of void* context pointers
  7304 ** specified by the blocked connections bundled together into an array.
  7305 ** This gives the application an opportunity to prioritize any actions 
  7306 ** related to the set of unblocked database connections.
  7307 **
  7308 ** <b>Deadlock Detection</b>
  7309 **
  7310 ** Assuming that after registering for an unlock-notify callback a 
  7311 ** database waits for the callback to be issued before taking any further
  7312 ** action (a reasonable assumption), then using this API may cause the
  7313 ** application to deadlock. For example, if connection X is waiting for
  7314 ** connection Y's transaction to be concluded, and similarly connection
  7315 ** Y is waiting on connection X's transaction, then neither connection
  7316 ** will proceed and the system may remain deadlocked indefinitely.
  7317 **
  7318 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
  7319 ** detection. ^If a given call to sqlite3_unlock_notify() would put the
  7320 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
  7321 ** unlock-notify callback is registered. The system is said to be in
  7322 ** a deadlocked state if connection A has registered for an unlock-notify
  7323 ** callback on the conclusion of connection B's transaction, and connection
  7324 ** B has itself registered for an unlock-notify callback when connection
  7325 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
  7326 ** the system is also considered to be deadlocked if connection B has
  7327 ** registered for an unlock-notify callback on the conclusion of connection
  7328 ** C's transaction, where connection C is waiting on connection A. ^Any
  7329 ** number of levels of indirection are allowed.
  7330 **
  7331 ** <b>The "DROP TABLE" Exception</b>
  7332 **
  7333 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost 
  7334 ** always appropriate to call sqlite3_unlock_notify(). There is however,
  7335 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
  7336 ** SQLite checks if there are any currently executing SELECT statements
  7337 ** that belong to the same connection. If there are, SQLITE_LOCKED is
  7338 ** returned. In this case there is no "blocking connection", so invoking
  7339 ** sqlite3_unlock_notify() results in the unlock-notify callback being
  7340 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
  7341 ** or "DROP INDEX" query, an infinite loop might be the result.
  7342 **
  7343 ** One way around this problem is to check the extended error code returned
  7344 ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
  7345 ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
  7346 ** the special "DROP TABLE/INDEX" case, the extended error code is just 
  7347 ** SQLITE_LOCKED.)^
  7348 */
  7349 SQLITE_API int sqlite3_unlock_notify(
  7350   sqlite3 *pBlocked,                          /* Waiting connection */
  7351   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
  7352   void *pNotifyArg                            /* Argument to pass to xNotify */
  7353 );
  7356 /*
  7357 ** CAPI3REF: String Comparison
  7358 **
  7359 ** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
  7360 ** and extensions to compare the contents of two buffers containing UTF-8
  7361 ** strings in a case-independent fashion, using the same definition of "case
  7362 ** independence" that SQLite uses internally when comparing identifiers.
  7363 */
  7364 SQLITE_API int sqlite3_stricmp(const char *, const char *);
  7365 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
  7367 /*
  7368 ** CAPI3REF: Error Logging Interface
  7369 **
  7370 ** ^The [sqlite3_log()] interface writes a message into the error log
  7371 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
  7372 ** ^If logging is enabled, the zFormat string and subsequent arguments are
  7373 ** used with [sqlite3_snprintf()] to generate the final output string.
  7374 **
  7375 ** The sqlite3_log() interface is intended for use by extensions such as
  7376 ** virtual tables, collating functions, and SQL functions.  While there is
  7377 ** nothing to prevent an application from calling sqlite3_log(), doing so
  7378 ** is considered bad form.
  7379 **
  7380 ** The zFormat string must not be NULL.
  7381 **
  7382 ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
  7383 ** will not use dynamically allocated memory.  The log message is stored in
  7384 ** a fixed-length buffer on the stack.  If the log message is longer than
  7385 ** a few hundred characters, it will be truncated to the length of the
  7386 ** buffer.
  7387 */
  7388 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
  7390 /*
  7391 ** CAPI3REF: Write-Ahead Log Commit Hook
  7392 **
  7393 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
  7394 ** will be invoked each time a database connection commits data to a
  7395 ** [write-ahead log] (i.e. whenever a transaction is committed in
  7396 ** [journal_mode | journal_mode=WAL mode]). 
  7397 **
  7398 ** ^The callback is invoked by SQLite after the commit has taken place and 
  7399 ** the associated write-lock on the database released, so the implementation 
  7400 ** may read, write or [checkpoint] the database as required.
  7401 **
  7402 ** ^The first parameter passed to the callback function when it is invoked
  7403 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
  7404 ** registering the callback. ^The second is a copy of the database handle.
  7405 ** ^The third parameter is the name of the database that was written to -
  7406 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
  7407 ** is the number of pages currently in the write-ahead log file,
  7408 ** including those that were just committed.
  7409 **
  7410 ** The callback function should normally return [SQLITE_OK].  ^If an error
  7411 ** code is returned, that error will propagate back up through the
  7412 ** SQLite code base to cause the statement that provoked the callback
  7413 ** to report an error, though the commit will have still occurred. If the
  7414 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
  7415 ** that does not correspond to any valid SQLite error code, the results
  7416 ** are undefined.
  7417 **
  7418 ** A single database handle may have at most a single write-ahead log callback 
  7419 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
  7420 ** previously registered write-ahead log callback. ^Note that the
  7421 ** [sqlite3_wal_autocheckpoint()] interface and the
  7422 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
  7423 ** those overwrite any prior [sqlite3_wal_hook()] settings.
  7424 */
  7425 SQLITE_API void *sqlite3_wal_hook(
  7426   sqlite3*, 
  7427   int(*)(void *,sqlite3*,const char*,int),
  7428   void*
  7429 );
  7431 /*
  7432 ** CAPI3REF: Configure an auto-checkpoint
  7433 **
  7434 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
  7435 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
  7436 ** to automatically [checkpoint]
  7437 ** after committing a transaction if there are N or
  7438 ** more frames in the [write-ahead log] file.  ^Passing zero or 
  7439 ** a negative value as the nFrame parameter disables automatic
  7440 ** checkpoints entirely.
  7441 **
  7442 ** ^The callback registered by this function replaces any existing callback
  7443 ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
  7444 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
  7445 ** configured by this function.
  7446 **
  7447 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
  7448 ** from SQL.
  7449 **
  7450 ** ^Every new [database connection] defaults to having the auto-checkpoint
  7451 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
  7452 ** pages.  The use of this interface
  7453 ** is only necessary if the default setting is found to be suboptimal
  7454 ** for a particular application.
  7455 */
  7456 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  7458 /*
  7459 ** CAPI3REF: Checkpoint a database
  7460 **
  7461 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
  7462 ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
  7463 ** empty string, then a checkpoint is run on all databases of
  7464 ** connection D.  ^If the database connection D is not in
  7465 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
  7466 **
  7467 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
  7468 ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
  7469 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
  7470 ** run whenever the WAL reaches a certain size threshold.
  7471 **
  7472 ** See also: [sqlite3_wal_checkpoint_v2()]
  7473 */
  7474 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  7476 /*
  7477 ** CAPI3REF: Checkpoint a database
  7478 **
  7479 ** Run a checkpoint operation on WAL database zDb attached to database 
  7480 ** handle db. The specific operation is determined by the value of the 
  7481 ** eMode parameter:
  7482 **
  7483 ** <dl>
  7484 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
  7485 **   Checkpoint as many frames as possible without waiting for any database 
  7486 **   readers or writers to finish. Sync the db file if all frames in the log
  7487 **   are checkpointed. This mode is the same as calling 
  7488 **   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
  7489 **
  7490 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
  7491 **   This mode blocks (calls the busy-handler callback) until there is no
  7492 **   database writer and all readers are reading from the most recent database
  7493 **   snapshot. It then checkpoints all frames in the log file and syncs the
  7494 **   database file. This call blocks database writers while it is running,
  7495 **   but not database readers.
  7496 **
  7497 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
  7498 **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
  7499 **   checkpointing the log file it blocks (calls the busy-handler callback)
  7500 **   until all readers are reading from the database file only. This ensures 
  7501 **   that the next client to write to the database file restarts the log file 
  7502 **   from the beginning. This call blocks database writers while it is running,
  7503 **   but not database readers.
  7504 ** </dl>
  7505 **
  7506 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
  7507 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
  7508 ** the total number of checkpointed frames (including any that were already
  7509 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
  7510 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
  7511 ** If no values are available because of an error, they are both set to -1
  7512 ** before returning to communicate this to the caller.
  7513 **
  7514 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
  7515 ** any other process is running a checkpoint operation at the same time, the 
  7516 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a 
  7517 ** busy-handler configured, it will not be invoked in this case.
  7518 **
  7519 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive 
  7520 ** "writer" lock on the database file. If the writer lock cannot be obtained
  7521 ** immediately, and a busy-handler is configured, it is invoked and the writer
  7522 ** lock retried until either the busy-handler returns 0 or the lock is
  7523 ** successfully obtained. The busy-handler is also invoked while waiting for
  7524 ** database readers as described above. If the busy-handler returns 0 before
  7525 ** the writer lock is obtained or while waiting for database readers, the
  7526 ** checkpoint operation proceeds from that point in the same way as 
  7527 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 
  7528 ** without blocking any further. SQLITE_BUSY is returned in this case.
  7529 **
  7530 ** If parameter zDb is NULL or points to a zero length string, then the
  7531 ** specified operation is attempted on all WAL databases. In this case the
  7532 ** values written to output parameters *pnLog and *pnCkpt are undefined. If 
  7533 ** an SQLITE_BUSY error is encountered when processing one or more of the 
  7534 ** attached WAL databases, the operation is still attempted on any remaining 
  7535 ** attached databases and SQLITE_BUSY is returned to the caller. If any other 
  7536 ** error occurs while processing an attached database, processing is abandoned 
  7537 ** and the error code returned to the caller immediately. If no error 
  7538 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached 
  7539 ** databases, SQLITE_OK is returned.
  7540 **
  7541 ** If database zDb is the name of an attached database that is not in WAL
  7542 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
  7543 ** zDb is not NULL (or a zero length string) and is not the name of any
  7544 ** attached database, SQLITE_ERROR is returned to the caller.
  7545 */
  7546 SQLITE_API int sqlite3_wal_checkpoint_v2(
  7547   sqlite3 *db,                    /* Database handle */
  7548   const char *zDb,                /* Name of attached database (or NULL) */
  7549   int eMode,                      /* SQLITE_CHECKPOINT_* value */
  7550   int *pnLog,                     /* OUT: Size of WAL log in frames */
  7551   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
  7552 );
  7554 /*
  7555 ** CAPI3REF: Checkpoint operation parameters
  7556 **
  7557 ** These constants can be used as the 3rd parameter to
  7558 ** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
  7559 ** documentation for additional information about the meaning and use of
  7560 ** each of these values.
  7561 */
  7562 #define SQLITE_CHECKPOINT_PASSIVE 0
  7563 #define SQLITE_CHECKPOINT_FULL    1
  7564 #define SQLITE_CHECKPOINT_RESTART 2
  7566 /*
  7567 ** CAPI3REF: Virtual Table Interface Configuration
  7568 **
  7569 ** This function may be called by either the [xConnect] or [xCreate] method
  7570 ** of a [virtual table] implementation to configure
  7571 ** various facets of the virtual table interface.
  7572 **
  7573 ** If this interface is invoked outside the context of an xConnect or
  7574 ** xCreate virtual table method then the behavior is undefined.
  7575 **
  7576 ** At present, there is only one option that may be configured using
  7577 ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
  7578 ** may be added in the future.
  7579 */
  7580 SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
  7582 /*
  7583 ** CAPI3REF: Virtual Table Configuration Options
  7584 **
  7585 ** These macros define the various options to the
  7586 ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  7587 ** can use to customize and optimize their behavior.
  7588 **
  7589 ** <dl>
  7590 ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  7591 ** <dd>Calls of the form
  7592 ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  7593 ** where X is an integer.  If X is zero, then the [virtual table] whose
  7594 ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  7595 ** support constraints.  In this configuration (which is the default) if
  7596 ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
  7597 ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
  7598 ** specified as part of the users SQL statement, regardless of the actual
  7599 ** ON CONFLICT mode specified.
  7600 **
  7601 ** If X is non-zero, then the virtual table implementation guarantees
  7602 ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
  7603 ** any modifications to internal or persistent data structures have been made.
  7604 ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite 
  7605 ** is able to roll back a statement or database transaction, and abandon
  7606 ** or continue processing the current SQL statement as appropriate. 
  7607 ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
  7608 ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
  7609 ** had been ABORT.
  7610 **
  7611 ** Virtual table implementations that are required to handle OR REPLACE
  7612 ** must do so within the [xUpdate] method. If a call to the 
  7613 ** [sqlite3_vtab_on_conflict()] function indicates that the current ON 
  7614 ** CONFLICT policy is REPLACE, the virtual table implementation should 
  7615 ** silently replace the appropriate rows within the xUpdate callback and
  7616 ** return SQLITE_OK. Or, if this is not possible, it may return
  7617 ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT 
  7618 ** constraint handling.
  7619 ** </dl>
  7620 */
  7621 #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
  7623 /*
  7624 ** CAPI3REF: Determine The Virtual Table Conflict Policy
  7625 **
  7626 ** This function may only be called from within a call to the [xUpdate] method
  7627 ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
  7628 ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
  7629 ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
  7630 ** of the SQL statement that triggered the call to the [xUpdate] method of the
  7631 ** [virtual table].
  7632 */
  7633 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
  7635 /*
  7636 ** CAPI3REF: Conflict resolution modes
  7637 **
  7638 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
  7639 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
  7640 ** is for the SQL statement being evaluated.
  7641 **
  7642 ** Note that the [SQLITE_IGNORE] constant is also used as a potential
  7643 ** return value from the [sqlite3_set_authorizer()] callback and that
  7644 ** [SQLITE_ABORT] is also a [result code].
  7645 */
  7646 #define SQLITE_ROLLBACK 1
  7647 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
  7648 #define SQLITE_FAIL     3
  7649 /* #define SQLITE_ABORT 4  // Also an error code */
  7650 #define SQLITE_REPLACE  5
  7654 /*
  7655 ** Undo the hack that converts floating point types to integer for
  7656 ** builds on processors without floating point support.
  7657 */
  7658 #ifdef SQLITE_OMIT_FLOATING_POINT
  7659 # undef double
  7660 #endif
  7662 #if 0
  7663 }  /* End of the 'extern "C"' block */
  7664 #endif
  7665 #endif
  7667 /*
  7668 ** 2010 August 30
  7669 **
  7670 ** The author disclaims copyright to this source code.  In place of
  7671 ** a legal notice, here is a blessing:
  7672 **
  7673 **    May you do good and not evil.
  7674 **    May you find forgiveness for yourself and forgive others.
  7675 **    May you share freely, never taking more than you give.
  7676 **
  7677 *************************************************************************
  7678 */
  7680 #ifndef _SQLITE3RTREE_H_
  7681 #define _SQLITE3RTREE_H_
  7684 #if 0
  7685 extern "C" {
  7686 #endif
  7688 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
  7690 /*
  7691 ** Register a geometry callback named zGeom that can be used as part of an
  7692 ** R-Tree geometry query as follows:
  7693 **
  7694 **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
  7695 */
  7696 SQLITE_API int sqlite3_rtree_geometry_callback(
  7697   sqlite3 *db,
  7698   const char *zGeom,
  7699 #ifdef SQLITE_RTREE_INT_ONLY
  7700   int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
  7701 #else
  7702   int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
  7703 #endif
  7704   void *pContext
  7705 );
  7708 /*
  7709 ** A pointer to a structure of the following type is passed as the first
  7710 ** argument to callbacks registered using rtree_geometry_callback().
  7711 */
  7712 struct sqlite3_rtree_geometry {
  7713   void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
  7714   int nParam;                     /* Size of array aParam[] */
  7715   double *aParam;                 /* Parameters passed to SQL geom function */
  7716   void *pUser;                    /* Callback implementation user data */
  7717   void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
  7718 };
  7721 #if 0
  7722 }  /* end of the 'extern "C"' block */
  7723 #endif
  7725 #endif  /* ifndef _SQLITE3RTREE_H_ */
  7728 /************** End of sqlite3.h *********************************************/
  7729 /************** Continuing where we left off in sqliteInt.h ******************/
  7730 /************** Include hash.h in the middle of sqliteInt.h ******************/
  7731 /************** Begin file hash.h ********************************************/
  7732 /*
  7733 ** 2001 September 22
  7734 **
  7735 ** The author disclaims copyright to this source code.  In place of
  7736 ** a legal notice, here is a blessing:
  7737 **
  7738 **    May you do good and not evil.
  7739 **    May you find forgiveness for yourself and forgive others.
  7740 **    May you share freely, never taking more than you give.
  7741 **
  7742 *************************************************************************
  7743 ** This is the header file for the generic hash-table implemenation
  7744 ** used in SQLite.
  7745 */
  7746 #ifndef _SQLITE_HASH_H_
  7747 #define _SQLITE_HASH_H_
  7749 /* Forward declarations of structures. */
  7750 typedef struct Hash Hash;
  7751 typedef struct HashElem HashElem;
  7753 /* A complete hash table is an instance of the following structure.
  7754 ** The internals of this structure are intended to be opaque -- client
  7755 ** code should not attempt to access or modify the fields of this structure
  7756 ** directly.  Change this structure only by using the routines below.
  7757 ** However, some of the "procedures" and "functions" for modifying and
  7758 ** accessing this structure are really macros, so we can't really make
  7759 ** this structure opaque.
  7760 **
  7761 ** All elements of the hash table are on a single doubly-linked list.
  7762 ** Hash.first points to the head of this list.
  7763 **
  7764 ** There are Hash.htsize buckets.  Each bucket points to a spot in
  7765 ** the global doubly-linked list.  The contents of the bucket are the
  7766 ** element pointed to plus the next _ht.count-1 elements in the list.
  7767 **
  7768 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
  7769 ** by a linear search of the global list.  For small tables, the 
  7770 ** Hash.ht table is never allocated because if there are few elements
  7771 ** in the table, it is faster to do a linear search than to manage
  7772 ** the hash table.
  7773 */
  7774 struct Hash {
  7775   unsigned int htsize;      /* Number of buckets in the hash table */
  7776   unsigned int count;       /* Number of entries in this table */
  7777   HashElem *first;          /* The first element of the array */
  7778   struct _ht {              /* the hash table */
  7779     int count;                 /* Number of entries with this hash */
  7780     HashElem *chain;           /* Pointer to first entry with this hash */
  7781   } *ht;
  7782 };
  7784 /* Each element in the hash table is an instance of the following 
  7785 ** structure.  All elements are stored on a single doubly-linked list.
  7786 **
  7787 ** Again, this structure is intended to be opaque, but it can't really
  7788 ** be opaque because it is used by macros.
  7789 */
  7790 struct HashElem {
  7791   HashElem *next, *prev;       /* Next and previous elements in the table */
  7792   void *data;                  /* Data associated with this element */
  7793   const char *pKey; int nKey;  /* Key associated with this element */
  7794 };
  7796 /*
  7797 ** Access routines.  To delete, insert a NULL pointer.
  7798 */
  7799 SQLITE_PRIVATE void sqlite3HashInit(Hash*);
  7800 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
  7801 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
  7802 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
  7804 /*
  7805 ** Macros for looping over all elements of a hash table.  The idiom is
  7806 ** like this:
  7807 **
  7808 **   Hash h;
  7809 **   HashElem *p;
  7810 **   ...
  7811 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
  7812 **     SomeStructure *pData = sqliteHashData(p);
  7813 **     // do something with pData
  7814 **   }
  7815 */
  7816 #define sqliteHashFirst(H)  ((H)->first)
  7817 #define sqliteHashNext(E)   ((E)->next)
  7818 #define sqliteHashData(E)   ((E)->data)
  7819 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
  7820 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
  7822 /*
  7823 ** Number of entries in a hash table
  7824 */
  7825 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
  7827 #endif /* _SQLITE_HASH_H_ */
  7829 /************** End of hash.h ************************************************/
  7830 /************** Continuing where we left off in sqliteInt.h ******************/
  7831 /************** Include parse.h in the middle of sqliteInt.h *****************/
  7832 /************** Begin file parse.h *******************************************/
  7833 #define TK_SEMI                            1
  7834 #define TK_EXPLAIN                         2
  7835 #define TK_QUERY                           3
  7836 #define TK_PLAN                            4
  7837 #define TK_BEGIN                           5
  7838 #define TK_TRANSACTION                     6
  7839 #define TK_DEFERRED                        7
  7840 #define TK_IMMEDIATE                       8
  7841 #define TK_EXCLUSIVE                       9
  7842 #define TK_COMMIT                         10
  7843 #define TK_END                            11
  7844 #define TK_ROLLBACK                       12
  7845 #define TK_SAVEPOINT                      13
  7846 #define TK_RELEASE                        14
  7847 #define TK_TO                             15
  7848 #define TK_TABLE                          16
  7849 #define TK_CREATE                         17
  7850 #define TK_IF                             18
  7851 #define TK_NOT                            19
  7852 #define TK_EXISTS                         20
  7853 #define TK_TEMP                           21
  7854 #define TK_LP                             22
  7855 #define TK_RP                             23
  7856 #define TK_AS                             24
  7857 #define TK_COMMA                          25
  7858 #define TK_ID                             26
  7859 #define TK_INDEXED                        27
  7860 #define TK_ABORT                          28
  7861 #define TK_ACTION                         29
  7862 #define TK_AFTER                          30
  7863 #define TK_ANALYZE                        31
  7864 #define TK_ASC                            32
  7865 #define TK_ATTACH                         33
  7866 #define TK_BEFORE                         34
  7867 #define TK_BY                             35
  7868 #define TK_CASCADE                        36
  7869 #define TK_CAST                           37
  7870 #define TK_COLUMNKW                       38
  7871 #define TK_CONFLICT                       39
  7872 #define TK_DATABASE                       40
  7873 #define TK_DESC                           41
  7874 #define TK_DETACH                         42
  7875 #define TK_EACH                           43
  7876 #define TK_FAIL                           44
  7877 #define TK_FOR                            45
  7878 #define TK_IGNORE                         46
  7879 #define TK_INITIALLY                      47
  7880 #define TK_INSTEAD                        48
  7881 #define TK_LIKE_KW                        49
  7882 #define TK_MATCH                          50
  7883 #define TK_NO                             51
  7884 #define TK_KEY                            52
  7885 #define TK_OF                             53
  7886 #define TK_OFFSET                         54
  7887 #define TK_PRAGMA                         55
  7888 #define TK_RAISE                          56
  7889 #define TK_REPLACE                        57
  7890 #define TK_RESTRICT                       58
  7891 #define TK_ROW                            59
  7892 #define TK_TRIGGER                        60
  7893 #define TK_VACUUM                         61
  7894 #define TK_VIEW                           62
  7895 #define TK_VIRTUAL                        63
  7896 #define TK_REINDEX                        64
  7897 #define TK_RENAME                         65
  7898 #define TK_CTIME_KW                       66
  7899 #define TK_ANY                            67
  7900 #define TK_OR                             68
  7901 #define TK_AND                            69
  7902 #define TK_IS                             70
  7903 #define TK_BETWEEN                        71
  7904 #define TK_IN                             72
  7905 #define TK_ISNULL                         73
  7906 #define TK_NOTNULL                        74
  7907 #define TK_NE                             75
  7908 #define TK_EQ                             76
  7909 #define TK_GT                             77
  7910 #define TK_LE                             78
  7911 #define TK_LT                             79
  7912 #define TK_GE                             80
  7913 #define TK_ESCAPE                         81
  7914 #define TK_BITAND                         82
  7915 #define TK_BITOR                          83
  7916 #define TK_LSHIFT                         84
  7917 #define TK_RSHIFT                         85
  7918 #define TK_PLUS                           86
  7919 #define TK_MINUS                          87
  7920 #define TK_STAR                           88
  7921 #define TK_SLASH                          89
  7922 #define TK_REM                            90
  7923 #define TK_CONCAT                         91
  7924 #define TK_COLLATE                        92
  7925 #define TK_BITNOT                         93
  7926 #define TK_STRING                         94
  7927 #define TK_JOIN_KW                        95
  7928 #define TK_CONSTRAINT                     96
  7929 #define TK_DEFAULT                        97
  7930 #define TK_NULL                           98
  7931 #define TK_PRIMARY                        99
  7932 #define TK_UNIQUE                         100
  7933 #define TK_CHECK                          101
  7934 #define TK_REFERENCES                     102
  7935 #define TK_AUTOINCR                       103
  7936 #define TK_ON                             104
  7937 #define TK_INSERT                         105
  7938 #define TK_DELETE                         106
  7939 #define TK_UPDATE                         107
  7940 #define TK_SET                            108
  7941 #define TK_DEFERRABLE                     109
  7942 #define TK_FOREIGN                        110
  7943 #define TK_DROP                           111
  7944 #define TK_UNION                          112
  7945 #define TK_ALL                            113
  7946 #define TK_EXCEPT                         114
  7947 #define TK_INTERSECT                      115
  7948 #define TK_SELECT                         116
  7949 #define TK_DISTINCT                       117
  7950 #define TK_DOT                            118
  7951 #define TK_FROM                           119
  7952 #define TK_JOIN                           120
  7953 #define TK_USING                          121
  7954 #define TK_ORDER                          122
  7955 #define TK_GROUP                          123
  7956 #define TK_HAVING                         124
  7957 #define TK_LIMIT                          125
  7958 #define TK_WHERE                          126
  7959 #define TK_INTO                           127
  7960 #define TK_VALUES                         128
  7961 #define TK_INTEGER                        129
  7962 #define TK_FLOAT                          130
  7963 #define TK_BLOB                           131
  7964 #define TK_REGISTER                       132
  7965 #define TK_VARIABLE                       133
  7966 #define TK_CASE                           134
  7967 #define TK_WHEN                           135
  7968 #define TK_THEN                           136
  7969 #define TK_ELSE                           137
  7970 #define TK_INDEX                          138
  7971 #define TK_ALTER                          139
  7972 #define TK_ADD                            140
  7973 #define TK_TO_TEXT                        141
  7974 #define TK_TO_BLOB                        142
  7975 #define TK_TO_NUMERIC                     143
  7976 #define TK_TO_INT                         144
  7977 #define TK_TO_REAL                        145
  7978 #define TK_ISNOT                          146
  7979 #define TK_END_OF_FILE                    147
  7980 #define TK_ILLEGAL                        148
  7981 #define TK_SPACE                          149
  7982 #define TK_UNCLOSED_STRING                150
  7983 #define TK_FUNCTION                       151
  7984 #define TK_COLUMN                         152
  7985 #define TK_AGG_FUNCTION                   153
  7986 #define TK_AGG_COLUMN                     154
  7987 #define TK_CONST_FUNC                     155
  7988 #define TK_UMINUS                         156
  7989 #define TK_UPLUS                          157
  7991 /************** End of parse.h ***********************************************/
  7992 /************** Continuing where we left off in sqliteInt.h ******************/
  7993 #include <stdio.h>
  7994 #include <stdlib.h>
  7995 #include <string.h>
  7996 #include <assert.h>
  7997 #include <stddef.h>
  7999 /*
  8000 ** If compiling for a processor that lacks floating point support,
  8001 ** substitute integer for floating-point
  8002 */
  8003 #ifdef SQLITE_OMIT_FLOATING_POINT
  8004 # define double sqlite_int64
  8005 # define float sqlite_int64
  8006 # define LONGDOUBLE_TYPE sqlite_int64
  8007 # ifndef SQLITE_BIG_DBL
  8008 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
  8009 # endif
  8010 # define SQLITE_OMIT_DATETIME_FUNCS 1
  8011 # define SQLITE_OMIT_TRACE 1
  8012 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  8013 # undef SQLITE_HAVE_ISNAN
  8014 #endif
  8015 #ifndef SQLITE_BIG_DBL
  8016 # define SQLITE_BIG_DBL (1e99)
  8017 #endif
  8019 /*
  8020 ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  8021 ** afterward. Having this macro allows us to cause the C compiler 
  8022 ** to omit code used by TEMP tables without messy #ifndef statements.
  8023 */
  8024 #ifdef SQLITE_OMIT_TEMPDB
  8025 #define OMIT_TEMPDB 1
  8026 #else
  8027 #define OMIT_TEMPDB 0
  8028 #endif
  8030 /*
  8031 ** The "file format" number is an integer that is incremented whenever
  8032 ** the VDBE-level file format changes.  The following macros define the
  8033 ** the default file format for new databases and the maximum file format
  8034 ** that the library can read.
  8035 */
  8036 #define SQLITE_MAX_FILE_FORMAT 4
  8037 #ifndef SQLITE_DEFAULT_FILE_FORMAT
  8038 # define SQLITE_DEFAULT_FILE_FORMAT 4
  8039 #endif
  8041 /*
  8042 ** Determine whether triggers are recursive by default.  This can be
  8043 ** changed at run-time using a pragma.
  8044 */
  8045 #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  8046 # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
  8047 #endif
  8049 /*
  8050 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
  8051 ** on the command-line
  8052 */
  8053 #ifndef SQLITE_TEMP_STORE
  8054 # define SQLITE_TEMP_STORE 1
  8055 #endif
  8057 /*
  8058 ** GCC does not define the offsetof() macro so we'll have to do it
  8059 ** ourselves.
  8060 */
  8061 #ifndef offsetof
  8062 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
  8063 #endif
  8065 /*
  8066 ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
  8067 ** not, there are still machines out there that use EBCDIC.)
  8068 */
  8069 #if 'A' == '\301'
  8070 # define SQLITE_EBCDIC 1
  8071 #else
  8072 # define SQLITE_ASCII 1
  8073 #endif
  8075 /*
  8076 ** Integers of known sizes.  These typedefs might change for architectures
  8077 ** where the sizes very.  Preprocessor macros are available so that the
  8078 ** types can be conveniently redefined at compile-type.  Like this:
  8079 **
  8080 **         cc '-DUINTPTR_TYPE=long long int' ...
  8081 */
  8082 #ifndef UINT32_TYPE
  8083 # ifdef HAVE_UINT32_T
  8084 #  define UINT32_TYPE uint32_t
  8085 # else
  8086 #  define UINT32_TYPE unsigned int
  8087 # endif
  8088 #endif
  8089 #ifndef UINT16_TYPE
  8090 # ifdef HAVE_UINT16_T
  8091 #  define UINT16_TYPE uint16_t
  8092 # else
  8093 #  define UINT16_TYPE unsigned short int
  8094 # endif
  8095 #endif
  8096 #ifndef INT16_TYPE
  8097 # ifdef HAVE_INT16_T
  8098 #  define INT16_TYPE int16_t
  8099 # else
  8100 #  define INT16_TYPE short int
  8101 # endif
  8102 #endif
  8103 #ifndef UINT8_TYPE
  8104 # ifdef HAVE_UINT8_T
  8105 #  define UINT8_TYPE uint8_t
  8106 # else
  8107 #  define UINT8_TYPE unsigned char
  8108 # endif
  8109 #endif
  8110 #ifndef INT8_TYPE
  8111 # ifdef HAVE_INT8_T
  8112 #  define INT8_TYPE int8_t
  8113 # else
  8114 #  define INT8_TYPE signed char
  8115 # endif
  8116 #endif
  8117 #ifndef LONGDOUBLE_TYPE
  8118 # define LONGDOUBLE_TYPE long double
  8119 #endif
  8120 typedef sqlite_int64 i64;          /* 8-byte signed integer */
  8121 typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
  8122 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
  8123 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
  8124 typedef INT16_TYPE i16;            /* 2-byte signed integer */
  8125 typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
  8126 typedef INT8_TYPE i8;              /* 1-byte signed integer */
  8128 /*
  8129 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  8130 ** that can be stored in a u32 without loss of data.  The value
  8131 ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
  8132 ** have to specify the value in the less intuitive manner shown:
  8133 */
  8134 #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
  8136 /*
  8137 ** The datatype used to store estimates of the number of rows in a
  8138 ** table or index.  This is an unsigned integer type.  For 99.9% of
  8139 ** the world, a 32-bit integer is sufficient.  But a 64-bit integer
  8140 ** can be used at compile-time if desired.
  8141 */
  8142 #ifdef SQLITE_64BIT_STATS
  8143  typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
  8144 #else
  8145  typedef u32 tRowcnt;    /* 32-bit is the default */
  8146 #endif
  8148 /*
  8149 ** Macros to determine whether the machine is big or little endian,
  8150 ** evaluated at runtime.
  8151 */
  8152 #ifdef SQLITE_AMALGAMATION
  8153 SQLITE_PRIVATE const int sqlite3one = 1;
  8154 #else
  8155 SQLITE_PRIVATE const int sqlite3one;
  8156 #endif
  8157 #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  8158                              || defined(__x86_64) || defined(__x86_64__)
  8159 # define SQLITE_BIGENDIAN    0
  8160 # define SQLITE_LITTLEENDIAN 1
  8161 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
  8162 #else
  8163 # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
  8164 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  8165 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  8166 #endif
  8168 /*
  8169 ** Constants for the largest and smallest possible 64-bit signed integers.
  8170 ** These macros are designed to work correctly on both 32-bit and 64-bit
  8171 ** compilers.
  8172 */
  8173 #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
  8174 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  8176 /* 
  8177 ** Round up a number to the next larger multiple of 8.  This is used
  8178 ** to force 8-byte alignment on 64-bit architectures.
  8179 */
  8180 #define ROUND8(x)     (((x)+7)&~7)
  8182 /*
  8183 ** Round down to the nearest multiple of 8
  8184 */
  8185 #define ROUNDDOWN8(x) ((x)&~7)
  8187 /*
  8188 ** Assert that the pointer X is aligned to an 8-byte boundary.  This
  8189 ** macro is used only within assert() to verify that the code gets
  8190 ** all alignment restrictions correct.
  8191 **
  8192 ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
  8193 ** underlying malloc() implemention might return us 4-byte aligned
  8194 ** pointers.  In that case, only verify 4-byte alignment.
  8195 */
  8196 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
  8197 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
  8198 #else
  8199 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
  8200 #endif
  8203 /*
  8204 ** An instance of the following structure is used to store the busy-handler
  8205 ** callback for a given sqlite handle. 
  8206 **
  8207 ** The sqlite.busyHandler member of the sqlite struct contains the busy
  8208 ** callback for the database handle. Each pager opened via the sqlite
  8209 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
  8210 ** callback is currently invoked only from within pager.c.
  8211 */
  8212 typedef struct BusyHandler BusyHandler;
  8213 struct BusyHandler {
  8214   int (*xFunc)(void *,int);  /* The busy callback */
  8215   void *pArg;                /* First arg to busy callback */
  8216   int nBusy;                 /* Incremented with each busy call */
  8217 };
  8219 /*
  8220 ** Name of the master database table.  The master database table
  8221 ** is a special table that holds the names and attributes of all
  8222 ** user tables and indices.
  8223 */
  8224 #define MASTER_NAME       "sqlite_master"
  8225 #define TEMP_MASTER_NAME  "sqlite_temp_master"
  8227 /*
  8228 ** The root-page of the master database table.
  8229 */
  8230 #define MASTER_ROOT       1
  8232 /*
  8233 ** The name of the schema table.
  8234 */
  8235 #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
  8237 /*
  8238 ** A convenience macro that returns the number of elements in
  8239 ** an array.
  8240 */
  8241 #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
  8243 /*
  8244 ** The following value as a destructor means to use sqlite3DbFree().
  8245 ** The sqlite3DbFree() routine requires two parameters instead of the 
  8246 ** one parameter that destructors normally want.  So we have to introduce 
  8247 ** this magic value that the code knows to handle differently.  Any 
  8248 ** pointer will work here as long as it is distinct from SQLITE_STATIC
  8249 ** and SQLITE_TRANSIENT.
  8250 */
  8251 #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3MallocSize)
  8253 /*
  8254 ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
  8255 ** not support Writable Static Data (WSD) such as global and static variables.
  8256 ** All variables must either be on the stack or dynamically allocated from
  8257 ** the heap.  When WSD is unsupported, the variable declarations scattered
  8258 ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
  8259 ** macro is used for this purpose.  And instead of referencing the variable
  8260 ** directly, we use its constant as a key to lookup the run-time allocated
  8261 ** buffer that holds real variable.  The constant is also the initializer
  8262 ** for the run-time allocated buffer.
  8263 **
  8264 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
  8265 ** macros become no-ops and have zero performance impact.
  8266 */
  8267 #ifdef SQLITE_OMIT_WSD
  8268   #define SQLITE_WSD const
  8269   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
  8270   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
  8271 SQLITE_API   int sqlite3_wsd_init(int N, int J);
  8272 SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
  8273 #else
  8274   #define SQLITE_WSD 
  8275   #define GLOBAL(t,v) v
  8276   #define sqlite3GlobalConfig sqlite3Config
  8277 #endif
  8279 /*
  8280 ** The following macros are used to suppress compiler warnings and to
  8281 ** make it clear to human readers when a function parameter is deliberately 
  8282 ** left unused within the body of a function. This usually happens when
  8283 ** a function is called via a function pointer. For example the 
  8284 ** implementation of an SQL aggregate step callback may not use the
  8285 ** parameter indicating the number of arguments passed to the aggregate,
  8286 ** if it knows that this is enforced elsewhere.
  8287 **
  8288 ** When a function parameter is not used at all within the body of a function,
  8289 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
  8290 ** However, these macros may also be used to suppress warnings related to
  8291 ** parameters that may or may not be used depending on compilation options.
  8292 ** For example those parameters only used in assert() statements. In these
  8293 ** cases the parameters are named as per the usual conventions.
  8294 */
  8295 #define UNUSED_PARAMETER(x) (void)(x)
  8296 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
  8298 /*
  8299 ** Forward references to structures
  8300 */
  8301 typedef struct AggInfo AggInfo;
  8302 typedef struct AuthContext AuthContext;
  8303 typedef struct AutoincInfo AutoincInfo;
  8304 typedef struct Bitvec Bitvec;
  8305 typedef struct CollSeq CollSeq;
  8306 typedef struct Column Column;
  8307 typedef struct Db Db;
  8308 typedef struct Schema Schema;
  8309 typedef struct Expr Expr;
  8310 typedef struct ExprList ExprList;
  8311 typedef struct ExprSpan ExprSpan;
  8312 typedef struct FKey FKey;
  8313 typedef struct FuncDestructor FuncDestructor;
  8314 typedef struct FuncDef FuncDef;
  8315 typedef struct FuncDefHash FuncDefHash;
  8316 typedef struct IdList IdList;
  8317 typedef struct Index Index;
  8318 typedef struct IndexSample IndexSample;
  8319 typedef struct KeyClass KeyClass;
  8320 typedef struct KeyInfo KeyInfo;
  8321 typedef struct Lookaside Lookaside;
  8322 typedef struct LookasideSlot LookasideSlot;
  8323 typedef struct Module Module;
  8324 typedef struct NameContext NameContext;
  8325 typedef struct Parse Parse;
  8326 typedef struct RowSet RowSet;
  8327 typedef struct Savepoint Savepoint;
  8328 typedef struct Select Select;
  8329 typedef struct SelectDest SelectDest;
  8330 typedef struct SrcList SrcList;
  8331 typedef struct StrAccum StrAccum;
  8332 typedef struct Table Table;
  8333 typedef struct TableLock TableLock;
  8334 typedef struct Token Token;
  8335 typedef struct Trigger Trigger;
  8336 typedef struct TriggerPrg TriggerPrg;
  8337 typedef struct TriggerStep TriggerStep;
  8338 typedef struct UnpackedRecord UnpackedRecord;
  8339 typedef struct VTable VTable;
  8340 typedef struct VtabCtx VtabCtx;
  8341 typedef struct Walker Walker;
  8342 typedef struct WherePlan WherePlan;
  8343 typedef struct WhereInfo WhereInfo;
  8344 typedef struct WhereLevel WhereLevel;
  8346 /*
  8347 ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  8348 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  8349 ** pointer types (i.e. FuncDef) defined above.
  8350 */
  8351 /************** Include btree.h in the middle of sqliteInt.h *****************/
  8352 /************** Begin file btree.h *******************************************/
  8353 /*
  8354 ** 2001 September 15
  8355 **
  8356 ** The author disclaims copyright to this source code.  In place of
  8357 ** a legal notice, here is a blessing:
  8358 **
  8359 **    May you do good and not evil.
  8360 **    May you find forgiveness for yourself and forgive others.
  8361 **    May you share freely, never taking more than you give.
  8362 **
  8363 *************************************************************************
  8364 ** This header file defines the interface that the sqlite B-Tree file
  8365 ** subsystem.  See comments in the source code for a detailed description
  8366 ** of what each interface routine does.
  8367 */
  8368 #ifndef _BTREE_H_
  8369 #define _BTREE_H_
  8371 /* TODO: This definition is just included so other modules compile. It
  8372 ** needs to be revisited.
  8373 */
  8374 #define SQLITE_N_BTREE_META 10
  8376 /*
  8377 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
  8378 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
  8379 */
  8380 #ifndef SQLITE_DEFAULT_AUTOVACUUM
  8381   #define SQLITE_DEFAULT_AUTOVACUUM 0
  8382 #endif
  8384 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
  8385 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
  8386 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
  8388 /*
  8389 ** Forward declarations of structure
  8390 */
  8391 typedef struct Btree Btree;
  8392 typedef struct BtCursor BtCursor;
  8393 typedef struct BtShared BtShared;
  8396 SQLITE_PRIVATE int sqlite3BtreeOpen(
  8397   sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
  8398   const char *zFilename,   /* Name of database file to open */
  8399   sqlite3 *db,             /* Associated database connection */
  8400   Btree **ppBtree,         /* Return open Btree* here */
  8401   int flags,               /* Flags */
  8402   int vfsFlags             /* Flags passed through to VFS open */
  8403 );
  8405 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
  8406 ** following values.
  8407 **
  8408 ** NOTE:  These values must match the corresponding PAGER_ values in
  8409 ** pager.h.
  8410 */
  8411 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
  8412 #define BTREE_MEMORY        2  /* This is an in-memory DB */
  8413 #define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
  8414 #define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
  8416 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  8417 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  8418 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
  8419 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  8420 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  8421 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  8422 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  8423 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
  8424 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
  8425 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
  8426 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
  8427 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
  8428 #endif
  8429 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
  8430 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
  8431 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
  8432 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
  8433 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
  8434 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
  8435 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int);
  8436 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
  8437 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
  8438 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
  8439 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
  8440 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
  8441 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
  8442 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
  8443 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
  8444 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
  8446 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
  8447 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
  8448 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
  8450 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
  8452 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
  8453 ** of the flags shown below.
  8454 **
  8455 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
  8456 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
  8457 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
  8458 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
  8459 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
  8460 ** indices.)
  8461 */
  8462 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
  8463 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
  8465 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  8466 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
  8467 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  8469 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  8470 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
  8472 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
  8474 /*
  8475 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
  8476 ** should be one of the following values. The integer values are assigned 
  8477 ** to constants so that the offset of the corresponding field in an
  8478 ** SQLite database header may be found using the following formula:
  8479 **
  8480 **   offset = 36 + (idx * 4)
  8481 **
  8482 ** For example, the free-page-count field is located at byte offset 36 of
  8483 ** the database file header. The incr-vacuum-flag field is located at
  8484 ** byte offset 64 (== 36+4*7).
  8485 */
  8486 #define BTREE_FREE_PAGE_COUNT     0
  8487 #define BTREE_SCHEMA_VERSION      1
  8488 #define BTREE_FILE_FORMAT         2
  8489 #define BTREE_DEFAULT_CACHE_SIZE  3
  8490 #define BTREE_LARGEST_ROOT_PAGE   4
  8491 #define BTREE_TEXT_ENCODING       5
  8492 #define BTREE_USER_VERSION        6
  8493 #define BTREE_INCR_VACUUM         7
  8495 /*
  8496 ** Values that may be OR'd together to form the second argument of an
  8497 ** sqlite3BtreeCursorHints() call.
  8498 */
  8499 #define BTREE_BULKLOAD 0x00000001
  8501 SQLITE_PRIVATE int sqlite3BtreeCursor(
  8502   Btree*,                              /* BTree containing table to open */
  8503   int iTable,                          /* Index of root page */
  8504   int wrFlag,                          /* 1 for writing.  0 for read-only */
  8505   struct KeyInfo*,                     /* First argument to compare function */
  8506   BtCursor *pCursor                    /* Space to write cursor structure */
  8507 );
  8508 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
  8509 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
  8511 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
  8512 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
  8513   BtCursor*,
  8514   UnpackedRecord *pUnKey,
  8515   i64 intKey,
  8516   int bias,
  8517   int *pRes
  8518 );
  8519 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
  8520 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
  8521 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
  8522                                   const void *pData, int nData,
  8523                                   int nZero, int bias, int seekResult);
  8524 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
  8525 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
  8526 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
  8527 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
  8528 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
  8529 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
  8530 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
  8531 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
  8532 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
  8533 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  8534 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  8535 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  8536 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  8538 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  8539 SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  8541 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  8542 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  8543 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  8544 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  8545 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
  8547 #ifndef NDEBUG
  8548 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  8549 #endif
  8551 #ifndef SQLITE_OMIT_BTREECOUNT
  8552 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
  8553 #endif
  8555 #ifdef SQLITE_TEST
  8556 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
  8557 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
  8558 #endif
  8560 #ifndef SQLITE_OMIT_WAL
  8561 SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
  8562 #endif
  8564 /*
  8565 ** If we are not using shared cache, then there is no need to
  8566 ** use mutexes to access the BtShared structures.  So make the
  8567 ** Enter and Leave procedures no-ops.
  8568 */
  8569 #ifndef SQLITE_OMIT_SHARED_CACHE
  8570 SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
  8571 SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
  8572 #else
  8573 # define sqlite3BtreeEnter(X) 
  8574 # define sqlite3BtreeEnterAll(X)
  8575 #endif
  8577 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  8578 SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
  8579 SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
  8580 SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
  8581 SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
  8582 SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
  8583 #ifndef NDEBUG
  8584   /* These routines are used inside assert() statements only. */
  8585 SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
  8586 SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
  8587 SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
  8588 #endif
  8589 #else
  8591 # define sqlite3BtreeSharable(X) 0
  8592 # define sqlite3BtreeLeave(X)
  8593 # define sqlite3BtreeEnterCursor(X)
  8594 # define sqlite3BtreeLeaveCursor(X)
  8595 # define sqlite3BtreeLeaveAll(X)
  8597 # define sqlite3BtreeHoldsMutex(X) 1
  8598 # define sqlite3BtreeHoldsAllMutexes(X) 1
  8599 # define sqlite3SchemaMutexHeld(X,Y,Z) 1
  8600 #endif
  8603 #endif /* _BTREE_H_ */
  8605 /************** End of btree.h ***********************************************/
  8606 /************** Continuing where we left off in sqliteInt.h ******************/
  8607 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
  8608 /************** Begin file vdbe.h ********************************************/
  8609 /*
  8610 ** 2001 September 15
  8611 **
  8612 ** The author disclaims copyright to this source code.  In place of
  8613 ** a legal notice, here is a blessing:
  8614 **
  8615 **    May you do good and not evil.
  8616 **    May you find forgiveness for yourself and forgive others.
  8617 **    May you share freely, never taking more than you give.
  8618 **
  8619 *************************************************************************
  8620 ** Header file for the Virtual DataBase Engine (VDBE)
  8621 **
  8622 ** This header defines the interface to the virtual database engine
  8623 ** or VDBE.  The VDBE implements an abstract machine that runs a
  8624 ** simple program to access and modify the underlying database.
  8625 */
  8626 #ifndef _SQLITE_VDBE_H_
  8627 #define _SQLITE_VDBE_H_
  8628 /* #include <stdio.h> */
  8630 /*
  8631 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
  8632 ** in the source file sqliteVdbe.c are allowed to see the insides
  8633 ** of this structure.
  8634 */
  8635 typedef struct Vdbe Vdbe;
  8637 /*
  8638 ** The names of the following types declared in vdbeInt.h are required
  8639 ** for the VdbeOp definition.
  8640 */
  8641 typedef struct VdbeFunc VdbeFunc;
  8642 typedef struct Mem Mem;
  8643 typedef struct SubProgram SubProgram;
  8645 /*
  8646 ** A single instruction of the virtual machine has an opcode
  8647 ** and as many as three operands.  The instruction is recorded
  8648 ** as an instance of the following structure:
  8649 */
  8650 struct VdbeOp {
  8651   u8 opcode;          /* What operation to perform */
  8652   signed char p4type; /* One of the P4_xxx constants for p4 */
  8653   u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
  8654   u8 p5;              /* Fifth parameter is an unsigned character */
  8655   int p1;             /* First operand */
  8656   int p2;             /* Second parameter (often the jump destination) */
  8657   int p3;             /* The third parameter */
  8658   union {             /* fourth parameter */
  8659     int i;                 /* Integer value if p4type==P4_INT32 */
  8660     void *p;               /* Generic pointer */
  8661     char *z;               /* Pointer to data for string (char array) types */
  8662     i64 *pI64;             /* Used when p4type is P4_INT64 */
  8663     double *pReal;         /* Used when p4type is P4_REAL */
  8664     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
  8665     VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
  8666     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
  8667     Mem *pMem;             /* Used when p4type is P4_MEM */
  8668     VTable *pVtab;         /* Used when p4type is P4_VTAB */
  8669     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
  8670     int *ai;               /* Used when p4type is P4_INTARRAY */
  8671     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
  8672     int (*xAdvance)(BtCursor *, int *);
  8673   } p4;
  8674 #ifdef SQLITE_DEBUG
  8675   char *zComment;          /* Comment to improve readability */
  8676 #endif
  8677 #ifdef VDBE_PROFILE
  8678   int cnt;                 /* Number of times this instruction was executed */
  8679   u64 cycles;              /* Total time spent executing this instruction */
  8680 #endif
  8681 };
  8682 typedef struct VdbeOp VdbeOp;
  8685 /*
  8686 ** A sub-routine used to implement a trigger program.
  8687 */
  8688 struct SubProgram {
  8689   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
  8690   int nOp;                      /* Elements in aOp[] */
  8691   int nMem;                     /* Number of memory cells required */
  8692   int nCsr;                     /* Number of cursors required */
  8693   int nOnce;                    /* Number of OP_Once instructions */
  8694   void *token;                  /* id that may be used to recursive triggers */
  8695   SubProgram *pNext;            /* Next sub-program already visited */
  8696 };
  8698 /*
  8699 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
  8700 ** it takes up less space.
  8701 */
  8702 struct VdbeOpList {
  8703   u8 opcode;          /* What operation to perform */
  8704   signed char p1;     /* First operand */
  8705   signed char p2;     /* Second parameter (often the jump destination) */
  8706   signed char p3;     /* Third parameter */
  8707 };
  8708 typedef struct VdbeOpList VdbeOpList;
  8710 /*
  8711 ** Allowed values of VdbeOp.p4type
  8712 */
  8713 #define P4_NOTUSED    0   /* The P4 parameter is not used */
  8714 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
  8715 #define P4_STATIC   (-2)  /* Pointer to a static string */
  8716 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
  8717 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
  8718 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
  8719 #define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
  8720 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
  8721 #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
  8722 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
  8723 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
  8724 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
  8725 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
  8726 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
  8727 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  8728 #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
  8729 #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
  8731 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
  8732 ** is made.  That copy is freed when the Vdbe is finalized.  But if the
  8733 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
  8734 ** gets freed when the Vdbe is finalized so it still should be obtained
  8735 ** from a single sqliteMalloc().  But no copy is made and the calling
  8736 ** function should *not* try to free the KeyInfo.
  8737 */
  8738 #define P4_KEYINFO_HANDOFF (-16)
  8739 #define P4_KEYINFO_STATIC  (-17)
  8741 /*
  8742 ** The Vdbe.aColName array contains 5n Mem structures, where n is the 
  8743 ** number of columns of data returned by the statement.
  8744 */
  8745 #define COLNAME_NAME     0
  8746 #define COLNAME_DECLTYPE 1
  8747 #define COLNAME_DATABASE 2
  8748 #define COLNAME_TABLE    3
  8749 #define COLNAME_COLUMN   4
  8750 #ifdef SQLITE_ENABLE_COLUMN_METADATA
  8751 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
  8752 #else
  8753 # ifdef SQLITE_OMIT_DECLTYPE
  8754 #   define COLNAME_N      1      /* Store only the name */
  8755 # else
  8756 #   define COLNAME_N      2      /* Store the name and decltype */
  8757 # endif
  8758 #endif
  8760 /*
  8761 ** The following macro converts a relative address in the p2 field
  8762 ** of a VdbeOp structure into a negative number so that 
  8763 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
  8764 ** the macro again restores the address.
  8765 */
  8766 #define ADDR(X)  (-1-(X))
  8768 /*
  8769 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
  8770 ** header file that defines a number for each opcode used by the VDBE.
  8771 */
  8772 /************** Include opcodes.h in the middle of vdbe.h ********************/
  8773 /************** Begin file opcodes.h *****************************************/
  8774 /* Automatically generated.  Do not edit */
  8775 /* See the mkopcodeh.awk script for details */
  8776 #define OP_Goto                                 1
  8777 #define OP_Gosub                                2
  8778 #define OP_Return                               3
  8779 #define OP_Yield                                4
  8780 #define OP_HaltIfNull                           5
  8781 #define OP_Halt                                 6
  8782 #define OP_Integer                              7
  8783 #define OP_Int64                                8
  8784 #define OP_Real                               130   /* same as TK_FLOAT    */
  8785 #define OP_String8                             94   /* same as TK_STRING   */
  8786 #define OP_String                               9
  8787 #define OP_Null                                10
  8788 #define OP_Blob                                11
  8789 #define OP_Variable                            12
  8790 #define OP_Move                                13
  8791 #define OP_Copy                                14
  8792 #define OP_SCopy                               15
  8793 #define OP_ResultRow                           16
  8794 #define OP_Concat                              91   /* same as TK_CONCAT   */
  8795 #define OP_Add                                 86   /* same as TK_PLUS     */
  8796 #define OP_Subtract                            87   /* same as TK_MINUS    */
  8797 #define OP_Multiply                            88   /* same as TK_STAR     */
  8798 #define OP_Divide                              89   /* same as TK_SLASH    */
  8799 #define OP_Remainder                           90   /* same as TK_REM      */
  8800 #define OP_CollSeq                             17
  8801 #define OP_Function                            18
  8802 #define OP_BitAnd                              82   /* same as TK_BITAND   */
  8803 #define OP_BitOr                               83   /* same as TK_BITOR    */
  8804 #define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
  8805 #define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
  8806 #define OP_AddImm                              20
  8807 #define OP_MustBeInt                           21
  8808 #define OP_RealAffinity                        22
  8809 #define OP_ToText                             141   /* same as TK_TO_TEXT  */
  8810 #define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
  8811 #define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
  8812 #define OP_ToInt                              144   /* same as TK_TO_INT   */
  8813 #define OP_ToReal                             145   /* same as TK_TO_REAL  */
  8814 #define OP_Eq                                  76   /* same as TK_EQ       */
  8815 #define OP_Ne                                  75   /* same as TK_NE       */
  8816 #define OP_Lt                                  79   /* same as TK_LT       */
  8817 #define OP_Le                                  78   /* same as TK_LE       */
  8818 #define OP_Gt                                  77   /* same as TK_GT       */
  8819 #define OP_Ge                                  80   /* same as TK_GE       */
  8820 #define OP_Permutation                         23
  8821 #define OP_Compare                             24
  8822 #define OP_Jump                                25
  8823 #define OP_And                                 69   /* same as TK_AND      */
  8824 #define OP_Or                                  68   /* same as TK_OR       */
  8825 #define OP_Not                                 19   /* same as TK_NOT      */
  8826 #define OP_BitNot                              93   /* same as TK_BITNOT   */
  8827 #define OP_Once                                26
  8828 #define OP_If                                  27
  8829 #define OP_IfNot                               28
  8830 #define OP_IsNull                              73   /* same as TK_ISNULL   */
  8831 #define OP_NotNull                             74   /* same as TK_NOTNULL  */
  8832 #define OP_Column                              29
  8833 #define OP_Affinity                            30
  8834 #define OP_MakeRecord                          31
  8835 #define OP_Count                               32
  8836 #define OP_Savepoint                           33
  8837 #define OP_AutoCommit                          34
  8838 #define OP_Transaction                         35
  8839 #define OP_ReadCookie                          36
  8840 #define OP_SetCookie                           37
  8841 #define OP_VerifyCookie                        38
  8842 #define OP_OpenRead                            39
  8843 #define OP_OpenWrite                           40
  8844 #define OP_OpenAutoindex                       41
  8845 #define OP_OpenEphemeral                       42
  8846 #define OP_SorterOpen                          43
  8847 #define OP_OpenPseudo                          44
  8848 #define OP_Close                               45
  8849 #define OP_SeekLt                              46
  8850 #define OP_SeekLe                              47
  8851 #define OP_SeekGe                              48
  8852 #define OP_SeekGt                              49
  8853 #define OP_Seek                                50
  8854 #define OP_NotFound                            51
  8855 #define OP_Found                               52
  8856 #define OP_IsUnique                            53
  8857 #define OP_NotExists                           54
  8858 #define OP_Sequence                            55
  8859 #define OP_NewRowid                            56
  8860 #define OP_Insert                              57
  8861 #define OP_InsertInt                           58
  8862 #define OP_Delete                              59
  8863 #define OP_ResetCount                          60
  8864 #define OP_SorterCompare                       61
  8865 #define OP_SorterData                          62
  8866 #define OP_RowKey                              63
  8867 #define OP_RowData                             64
  8868 #define OP_Rowid                               65
  8869 #define OP_NullRow                             66
  8870 #define OP_Last                                67
  8871 #define OP_SorterSort                          70
  8872 #define OP_Sort                                71
  8873 #define OP_Rewind                              72
  8874 #define OP_SorterNext                          81
  8875 #define OP_Prev                                92
  8876 #define OP_Next                                95
  8877 #define OP_SorterInsert                        96
  8878 #define OP_IdxInsert                           97
  8879 #define OP_IdxDelete                           98
  8880 #define OP_IdxRowid                            99
  8881 #define OP_IdxLT                              100
  8882 #define OP_IdxGE                              101
  8883 #define OP_Destroy                            102
  8884 #define OP_Clear                              103
  8885 #define OP_CreateIndex                        104
  8886 #define OP_CreateTable                        105
  8887 #define OP_ParseSchema                        106
  8888 #define OP_LoadAnalysis                       107
  8889 #define OP_DropTable                          108
  8890 #define OP_DropIndex                          109
  8891 #define OP_DropTrigger                        110
  8892 #define OP_IntegrityCk                        111
  8893 #define OP_RowSetAdd                          112
  8894 #define OP_RowSetRead                         113
  8895 #define OP_RowSetTest                         114
  8896 #define OP_Program                            115
  8897 #define OP_Param                              116
  8898 #define OP_FkCounter                          117
  8899 #define OP_FkIfZero                           118
  8900 #define OP_MemMax                             119
  8901 #define OP_IfPos                              120
  8902 #define OP_IfNeg                              121
  8903 #define OP_IfZero                             122
  8904 #define OP_AggStep                            123
  8905 #define OP_AggFinal                           124
  8906 #define OP_Checkpoint                         125
  8907 #define OP_JournalMode                        126
  8908 #define OP_Vacuum                             127
  8909 #define OP_IncrVacuum                         128
  8910 #define OP_Expire                             129
  8911 #define OP_TableLock                          131
  8912 #define OP_VBegin                             132
  8913 #define OP_VCreate                            133
  8914 #define OP_VDestroy                           134
  8915 #define OP_VOpen                              135
  8916 #define OP_VFilter                            136
  8917 #define OP_VColumn                            137
  8918 #define OP_VNext                              138
  8919 #define OP_VRename                            139
  8920 #define OP_VUpdate                            140
  8921 #define OP_Pagecount                          146
  8922 #define OP_MaxPgcnt                           147
  8923 #define OP_Trace                              148
  8924 #define OP_Noop                               149
  8925 #define OP_Explain                            150
  8928 /* Properties such as "out2" or "jump" that are specified in
  8929 ** comments following the "case" for each opcode in the vdbe.c
  8930 ** are encoded into bitvectors as follows:
  8931 */
  8932 #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
  8933 #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
  8934 #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
  8935 #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
  8936 #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
  8937 #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
  8938 #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
  8939 #define OPFLG_INITIALIZER {\
  8940 /*   0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
  8941 /*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24,\
  8942 /*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
  8943 /*  24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
  8944 /*  32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
  8945 /*  40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
  8946 /*  48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
  8947 /*  56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  8948 /*  64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
  8949 /*  72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
  8950 /*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
  8951 /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
  8952 /*  96 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
  8953 /* 104 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  8954 /* 112 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
  8955 /* 120 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00,\
  8956 /* 128 */ 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
  8957 /* 136 */ 0x01, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x04,\
  8958 /* 144 */ 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00,}
  8960 /************** End of opcodes.h *********************************************/
  8961 /************** Continuing where we left off in vdbe.h ***********************/
  8963 /*
  8964 ** Prototypes for the VDBE interface.  See comments on the implementation
  8965 ** for a description of what each of these routines does.
  8966 */
  8967 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
  8968 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  8969 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  8970 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  8971 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  8972 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  8973 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  8974 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  8975 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  8976 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  8977 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  8978 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
  8979 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  8980 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  8981 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  8982 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  8983 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  8984 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  8985 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  8986 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
  8987 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  8988 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
  8989 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
  8990 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
  8991 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
  8992 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
  8993 #ifdef SQLITE_DEBUG
  8994 SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
  8995 SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
  8996 #endif
  8997 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
  8998 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
  8999 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
  9000 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
  9001 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
  9002 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
  9003 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
  9004 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
  9005 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
  9006 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
  9007 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
  9008 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  9009 #ifndef SQLITE_OMIT_TRACE
  9010 SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  9011 #endif
  9013 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  9014 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
  9015 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  9017 #ifndef SQLITE_OMIT_TRIGGER
  9018 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  9019 #endif
  9022 #ifndef NDEBUG
  9023 SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
  9024 # define VdbeComment(X)  sqlite3VdbeComment X
  9025 SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
  9026 # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
  9027 #else
  9028 # define VdbeComment(X)
  9029 # define VdbeNoopComment(X)
  9030 #endif
  9032 #endif
  9034 /************** End of vdbe.h ************************************************/
  9035 /************** Continuing where we left off in sqliteInt.h ******************/
  9036 /************** Include pager.h in the middle of sqliteInt.h *****************/
  9037 /************** Begin file pager.h *******************************************/
  9038 /*
  9039 ** 2001 September 15
  9040 **
  9041 ** The author disclaims copyright to this source code.  In place of
  9042 ** a legal notice, here is a blessing:
  9043 **
  9044 **    May you do good and not evil.
  9045 **    May you find forgiveness for yourself and forgive others.
  9046 **    May you share freely, never taking more than you give.
  9047 **
  9048 *************************************************************************
  9049 ** This header file defines the interface that the sqlite page cache
  9050 ** subsystem.  The page cache subsystem reads and writes a file a page
  9051 ** at a time and provides a journal for rollback.
  9052 */
  9054 #ifndef _PAGER_H_
  9055 #define _PAGER_H_
  9057 /*
  9058 ** Default maximum size for persistent journal files. A negative 
  9059 ** value means no limit. This value may be overridden using the 
  9060 ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
  9061 */
  9062 #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
  9063   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
  9064 #endif
  9066 /*
  9067 ** The type used to represent a page number.  The first page in a file
  9068 ** is called page 1.  0 is used to represent "not a page".
  9069 */
  9070 typedef u32 Pgno;
  9072 /*
  9073 ** Each open file is managed by a separate instance of the "Pager" structure.
  9074 */
  9075 typedef struct Pager Pager;
  9077 /*
  9078 ** Handle type for pages.
  9079 */
  9080 typedef struct PgHdr DbPage;
  9082 /*
  9083 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
  9084 ** reserved for working around a windows/posix incompatibility). It is
  9085 ** used in the journal to signify that the remainder of the journal file 
  9086 ** is devoted to storing a master journal name - there are no more pages to
  9087 ** roll back. See comments for function writeMasterJournal() in pager.c 
  9088 ** for details.
  9089 */
  9090 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
  9092 /*
  9093 ** Allowed values for the flags parameter to sqlite3PagerOpen().
  9094 **
  9095 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
  9096 */
  9097 #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
  9098 #define PAGER_MEMORY        0x0002    /* In-memory database */
  9100 /*
  9101 ** Valid values for the second argument to sqlite3PagerLockingMode().
  9102 */
  9103 #define PAGER_LOCKINGMODE_QUERY      -1
  9104 #define PAGER_LOCKINGMODE_NORMAL      0
  9105 #define PAGER_LOCKINGMODE_EXCLUSIVE   1
  9107 /*
  9108 ** Numeric constants that encode the journalmode.  
  9109 */
  9110 #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
  9111 #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
  9112 #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
  9113 #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
  9114 #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
  9115 #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
  9116 #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
  9118 /*
  9119 ** The remainder of this file contains the declarations of the functions
  9120 ** that make up the Pager sub-system API. See source code comments for 
  9121 ** a detailed description of each routine.
  9122 */
  9124 /* Open and close a Pager connection. */ 
  9125 SQLITE_PRIVATE int sqlite3PagerOpen(
  9126   sqlite3_vfs*,
  9127   Pager **ppPager,
  9128   const char*,
  9129   int,
  9130   int,
  9131   int,
  9132   void(*)(DbPage*)
  9133 );
  9134 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
  9135 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
  9137 /* Functions used to configure a Pager object. */
  9138 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
  9139 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
  9140 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
  9141 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
  9142 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
  9143 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
  9144 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
  9145 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
  9146 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
  9147 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
  9148 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
  9149 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
  9151 /* Functions used to obtain and release page references. */ 
  9152 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
  9153 #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
  9154 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
  9155 SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
  9156 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
  9158 /* Operations on page references. */
  9159 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
  9160 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
  9161 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
  9162 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
  9163 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); 
  9164 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); 
  9166 /* Functions used to manage pager transactions and savepoints. */
  9167 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
  9168 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
  9169 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
  9170 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
  9171 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
  9172 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  9173 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  9174 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  9175 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
  9176 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
  9178 #ifndef SQLITE_OMIT_WAL
  9179 SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
  9180 SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
  9181 SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
  9182 SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
  9183 SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
  9184 #endif
  9186 #ifdef SQLITE_ENABLE_ZIPVFS
  9187 SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
  9188 #endif
  9190 /* Functions used to query pager state and configuration. */
  9191 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
  9192 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
  9193 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
  9194 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
  9195 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
  9196 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  9197 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  9198 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  9199 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  9200 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  9201 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
  9202 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
  9203 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
  9205 /* Functions used to truncate the database file. */
  9206 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
  9208 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
  9209 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
  9210 #endif
  9212 /* Functions to support testing and debugging. */
  9213 #if !defined(NDEBUG) || defined(SQLITE_TEST)
  9214 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
  9215 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
  9216 #endif
  9217 #ifdef SQLITE_TEST
  9218 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
  9219 SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
  9220   void disable_simulated_io_errors(void);
  9221   void enable_simulated_io_errors(void);
  9222 #else
  9223 # define disable_simulated_io_errors()
  9224 # define enable_simulated_io_errors()
  9225 #endif
  9227 #endif /* _PAGER_H_ */
  9229 /************** End of pager.h ***********************************************/
  9230 /************** Continuing where we left off in sqliteInt.h ******************/
  9231 /************** Include pcache.h in the middle of sqliteInt.h ****************/
  9232 /************** Begin file pcache.h ******************************************/
  9233 /*
  9234 ** 2008 August 05
  9235 **
  9236 ** The author disclaims copyright to this source code.  In place of
  9237 ** a legal notice, here is a blessing:
  9238 **
  9239 **    May you do good and not evil.
  9240 **    May you find forgiveness for yourself and forgive others.
  9241 **    May you share freely, never taking more than you give.
  9242 **
  9243 *************************************************************************
  9244 ** This header file defines the interface that the sqlite page cache
  9245 ** subsystem. 
  9246 */
  9248 #ifndef _PCACHE_H_
  9250 typedef struct PgHdr PgHdr;
  9251 typedef struct PCache PCache;
  9253 /*
  9254 ** Every page in the cache is controlled by an instance of the following
  9255 ** structure.
  9256 */
  9257 struct PgHdr {
  9258   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
  9259   void *pData;                   /* Page data */
  9260   void *pExtra;                  /* Extra content */
  9261   PgHdr *pDirty;                 /* Transient list of dirty pages */
  9262   Pager *pPager;                 /* The pager this page is part of */
  9263   Pgno pgno;                     /* Page number for this page */
  9264 #ifdef SQLITE_CHECK_PAGES
  9265   u32 pageHash;                  /* Hash of page content */
  9266 #endif
  9267   u16 flags;                     /* PGHDR flags defined below */
  9269   /**********************************************************************
  9270   ** Elements above are public.  All that follows is private to pcache.c
  9271   ** and should not be accessed by other modules.
  9272   */
  9273   i16 nRef;                      /* Number of users of this page */
  9274   PCache *pCache;                /* Cache that owns this page */
  9276   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
  9277   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
  9278 };
  9280 /* Bit values for PgHdr.flags */
  9281 #define PGHDR_DIRTY             0x002  /* Page has changed */
  9282 #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
  9283                                        ** writing this page to the database */
  9284 #define PGHDR_NEED_READ         0x008  /* Content is unread */
  9285 #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
  9286 #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
  9288 /* Initialize and shutdown the page cache subsystem */
  9289 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
  9290 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
  9292 /* Page cache buffer management:
  9293 ** These routines implement SQLITE_CONFIG_PAGECACHE.
  9294 */
  9295 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
  9297 /* Create a new pager cache.
  9298 ** Under memory stress, invoke xStress to try to make pages clean.
  9299 ** Only clean and unpinned pages can be reclaimed.
  9300 */
  9301 SQLITE_PRIVATE void sqlite3PcacheOpen(
  9302   int szPage,                    /* Size of every page */
  9303   int szExtra,                   /* Extra space associated with each page */
  9304   int bPurgeable,                /* True if pages are on backing store */
  9305   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
  9306   void *pStress,                 /* Argument to xStress */
  9307   PCache *pToInit                /* Preallocated space for the PCache */
  9308 );
  9310 /* Modify the page-size after the cache has been created. */
  9311 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
  9313 /* Return the size in bytes of a PCache object.  Used to preallocate
  9314 ** storage space.
  9315 */
  9316 SQLITE_PRIVATE int sqlite3PcacheSize(void);
  9318 /* One release per successful fetch.  Page is pinned until released.
  9319 ** Reference counted. 
  9320 */
  9321 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
  9322 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
  9324 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
  9325 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
  9326 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
  9327 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
  9329 /* Change a page number.  Used by incr-vacuum. */
  9330 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
  9332 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
  9333 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
  9335 /* Get a list of all dirty pages in the cache, sorted by page number */
  9336 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
  9338 /* Reset and close the cache object */
  9339 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
  9341 /* Clear flags from pages of the page cache */
  9342 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
  9344 /* Discard the contents of the cache */
  9345 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
  9347 /* Return the total number of outstanding page references */
  9348 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
  9350 /* Increment the reference count of an existing page */
  9351 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
  9353 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
  9355 /* Return the total number of pages stored in the cache */
  9356 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
  9358 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  9359 /* Iterate through all dirty pages currently stored in the cache. This
  9360 ** interface is only available if SQLITE_CHECK_PAGES is defined when the 
  9361 ** library is built.
  9362 */
  9363 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  9364 #endif
  9366 /* Set and get the suggested cache-size for the specified pager-cache.
  9367 **
  9368 ** If no global maximum is configured, then the system attempts to limit
  9369 ** the total number of pages cached by purgeable pager-caches to the sum
  9370 ** of the suggested cache-sizes.
  9371 */
  9372 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
  9373 #ifdef SQLITE_TEST
  9374 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
  9375 #endif
  9377 /* Free up as much memory as possible from the page cache */
  9378 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
  9380 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  9381 /* Try to return memory used by the pcache module to the main memory heap */
  9382 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
  9383 #endif
  9385 #ifdef SQLITE_TEST
  9386 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
  9387 #endif
  9389 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
  9391 #endif /* _PCACHE_H_ */
  9393 /************** End of pcache.h **********************************************/
  9394 /************** Continuing where we left off in sqliteInt.h ******************/
  9396 /************** Include os.h in the middle of sqliteInt.h ********************/
  9397 /************** Begin file os.h **********************************************/
  9398 /*
  9399 ** 2001 September 16
  9400 **
  9401 ** The author disclaims copyright to this source code.  In place of
  9402 ** a legal notice, here is a blessing:
  9403 **
  9404 **    May you do good and not evil.
  9405 **    May you find forgiveness for yourself and forgive others.
  9406 **    May you share freely, never taking more than you give.
  9407 **
  9408 ******************************************************************************
  9409 **
  9410 ** This header file (together with is companion C source-code file
  9411 ** "os.c") attempt to abstract the underlying operating system so that
  9412 ** the SQLite library will work on both POSIX and windows systems.
  9413 **
  9414 ** This header file is #include-ed by sqliteInt.h and thus ends up
  9415 ** being included by every source file.
  9416 */
  9417 #ifndef _SQLITE_OS_H_
  9418 #define _SQLITE_OS_H_
  9420 /*
  9421 ** Figure out if we are dealing with Unix, Windows, or some other
  9422 ** operating system.  After the following block of preprocess macros,
  9423 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
  9424 ** will defined to either 1 or 0.  One of the four will be 1.  The other 
  9425 ** three will be 0.
  9426 */
  9427 #if defined(SQLITE_OS_OTHER)
  9428 # if SQLITE_OS_OTHER==1
  9429 #   undef SQLITE_OS_UNIX
  9430 #   define SQLITE_OS_UNIX 0
  9431 #   undef SQLITE_OS_WIN
  9432 #   define SQLITE_OS_WIN 0
  9433 # else
  9434 #   undef SQLITE_OS_OTHER
  9435 # endif
  9436 #endif
  9437 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  9438 # define SQLITE_OS_OTHER 0
  9439 # ifndef SQLITE_OS_WIN
  9440 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  9441 #     define SQLITE_OS_WIN 1
  9442 #     define SQLITE_OS_UNIX 0
  9443 #   else
  9444 #     define SQLITE_OS_WIN 0
  9445 #     define SQLITE_OS_UNIX 1
  9446 #  endif
  9447 # else
  9448 #  define SQLITE_OS_UNIX 0
  9449 # endif
  9450 #else
  9451 # ifndef SQLITE_OS_WIN
  9452 #  define SQLITE_OS_WIN 0
  9453 # endif
  9454 #endif
  9456 #if SQLITE_OS_WIN
  9457 # include <windows.h>
  9458 #endif
  9460 /*
  9461 ** Determine if we are dealing with Windows NT.
  9462 **
  9463 ** We ought to be able to determine if we are compiling for win98 or winNT
  9464 ** using the _WIN32_WINNT macro as follows:
  9465 **
  9466 ** #if defined(_WIN32_WINNT)
  9467 ** # define SQLITE_OS_WINNT 1
  9468 ** #else
  9469 ** # define SQLITE_OS_WINNT 0
  9470 ** #endif
  9471 **
  9472 ** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
  9473 ** so the above test does not work.  We'll just assume that everything is
  9474 ** winNT unless the programmer explicitly says otherwise by setting
  9475 ** SQLITE_OS_WINNT to 0.
  9476 */
  9477 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  9478 # define SQLITE_OS_WINNT 1
  9479 #endif
  9481 /*
  9482 ** Determine if we are dealing with WindowsCE - which has a much
  9483 ** reduced API.
  9484 */
  9485 #if defined(_WIN32_WCE)
  9486 # define SQLITE_OS_WINCE 1
  9487 #else
  9488 # define SQLITE_OS_WINCE 0
  9489 #endif
  9491 /*
  9492 ** Determine if we are dealing with WinRT, which provides only a subset of
  9493 ** the full Win32 API.
  9494 */
  9495 #if !defined(SQLITE_OS_WINRT)
  9496 # define SQLITE_OS_WINRT 0
  9497 #endif
  9499 /*
  9500 ** When compiled for WinCE or WinRT, there is no concept of the current
  9501 ** directory.
  9502  */
  9503 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
  9504 # define SQLITE_CURDIR 1
  9505 #endif
  9507 /* If the SET_FULLSYNC macro is not defined above, then make it
  9508 ** a no-op
  9509 */
  9510 #ifndef SET_FULLSYNC
  9511 # define SET_FULLSYNC(x,y)
  9512 #endif
  9514 /*
  9515 ** The default size of a disk sector
  9516 */
  9517 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
  9518 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
  9519 #endif
  9521 /*
  9522 ** Temporary files are named starting with this prefix followed by 16 random
  9523 ** alphanumeric characters, and no file extension. They are stored in the
  9524 ** OS's standard temporary file directory, and are deleted prior to exit.
  9525 ** If sqlite is being embedded in another program, you may wish to change the
  9526 ** prefix to reflect your program's name, so that if your program exits
  9527 ** prematurely, old temporary files can be easily identified. This can be done
  9528 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
  9529 **
  9530 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
  9531 ** Mcafee started using SQLite in their anti-virus product and it
  9532 ** started putting files with the "sqlite" name in the c:/temp folder.
  9533 ** This annoyed many windows users.  Those users would then do a 
  9534 ** Google search for "sqlite", find the telephone numbers of the
  9535 ** developers and call to wake them up at night and complain.
  9536 ** For this reason, the default name prefix is changed to be "sqlite" 
  9537 ** spelled backwards.  So the temp files are still identified, but
  9538 ** anybody smart enough to figure out the code is also likely smart
  9539 ** enough to know that calling the developer will not help get rid
  9540 ** of the file.
  9541 */
  9542 #ifndef SQLITE_TEMP_FILE_PREFIX
  9543 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
  9544 #endif
  9546 /*
  9547 ** The following values may be passed as the second argument to
  9548 ** sqlite3OsLock(). The various locks exhibit the following semantics:
  9549 **
  9550 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
  9551 ** RESERVED:  A single process may hold a RESERVED lock on a file at
  9552 **            any time. Other processes may hold and obtain new SHARED locks.
  9553 ** PENDING:   A single process may hold a PENDING lock on a file at
  9554 **            any one time. Existing SHARED locks may persist, but no new
  9555 **            SHARED locks may be obtained by other processes.
  9556 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
  9557 **
  9558 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
  9559 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
  9560 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
  9561 ** sqlite3OsLock().
  9562 */
  9563 #define NO_LOCK         0
  9564 #define SHARED_LOCK     1
  9565 #define RESERVED_LOCK   2
  9566 #define PENDING_LOCK    3
  9567 #define EXCLUSIVE_LOCK  4
  9569 /*
  9570 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
  9571 **
  9572 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
  9573 ** those functions are not available.  So we use only LockFile() and
  9574 ** UnlockFile().
  9575 **
  9576 ** LockFile() prevents not just writing but also reading by other processes.
  9577 ** A SHARED_LOCK is obtained by locking a single randomly-chosen 
  9578 ** byte out of a specific range of bytes. The lock byte is obtained at 
  9579 ** random so two separate readers can probably access the file at the 
  9580 ** same time, unless they are unlucky and choose the same lock byte.
  9581 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
  9582 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
  9583 ** a single byte of the file that is designated as the reserved lock byte.
  9584 ** A PENDING_LOCK is obtained by locking a designated byte different from
  9585 ** the RESERVED_LOCK byte.
  9586 **
  9587 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
  9588 ** which means we can use reader/writer locks.  When reader/writer locks
  9589 ** are used, the lock is placed on the same range of bytes that is used
  9590 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
  9591 ** will support two or more Win95 readers or two or more WinNT readers.
  9592 ** But a single Win95 reader will lock out all WinNT readers and a single
  9593 ** WinNT reader will lock out all other Win95 readers.
  9594 **
  9595 ** The following #defines specify the range of bytes used for locking.
  9596 ** SHARED_SIZE is the number of bytes available in the pool from which
  9597 ** a random byte is selected for a shared lock.  The pool of bytes for
  9598 ** shared locks begins at SHARED_FIRST. 
  9599 **
  9600 ** The same locking strategy and
  9601 ** byte ranges are used for Unix.  This leaves open the possiblity of having
  9602 ** clients on win95, winNT, and unix all talking to the same shared file
  9603 ** and all locking correctly.  To do so would require that samba (or whatever
  9604 ** tool is being used for file sharing) implements locks correctly between
  9605 ** windows and unix.  I'm guessing that isn't likely to happen, but by
  9606 ** using the same locking range we are at least open to the possibility.
  9607 **
  9608 ** Locking in windows is manditory.  For this reason, we cannot store
  9609 ** actual data in the bytes used for locking.  The pager never allocates
  9610 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
  9611 ** that all locks will fit on a single page even at the minimum page size.
  9612 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
  9613 ** is set high so that we don't have to allocate an unused page except
  9614 ** for very large databases.  But one should test the page skipping logic 
  9615 ** by setting PENDING_BYTE low and running the entire regression suite.
  9616 **
  9617 ** Changing the value of PENDING_BYTE results in a subtly incompatible
  9618 ** file format.  Depending on how it is changed, you might not notice
  9619 ** the incompatibility right away, even running a full regression test.
  9620 ** The default location of PENDING_BYTE is the first byte past the
  9621 ** 1GB boundary.
  9622 **
  9623 */
  9624 #ifdef SQLITE_OMIT_WSD
  9625 # define PENDING_BYTE     (0x40000000)
  9626 #else
  9627 # define PENDING_BYTE      sqlite3PendingByte
  9628 #endif
  9629 #define RESERVED_BYTE     (PENDING_BYTE+1)
  9630 #define SHARED_FIRST      (PENDING_BYTE+2)
  9631 #define SHARED_SIZE       510
  9633 /*
  9634 ** Wrapper around OS specific sqlite3_os_init() function.
  9635 */
  9636 SQLITE_PRIVATE int sqlite3OsInit(void);
  9638 /* 
  9639 ** Functions for accessing sqlite3_file methods 
  9640 */
  9641 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
  9642 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
  9643 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
  9644 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
  9645 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
  9646 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
  9647 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
  9648 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
  9649 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
  9650 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
  9651 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
  9652 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
  9653 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
  9654 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
  9655 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
  9656 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
  9657 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
  9658 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
  9661 /* 
  9662 ** Functions for accessing sqlite3_vfs methods 
  9663 */
  9664 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
  9665 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
  9666 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
  9667 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
  9668 #ifndef SQLITE_OMIT_LOAD_EXTENSION
  9669 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
  9670 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
  9671 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
  9672 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
  9673 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  9674 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
  9675 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
  9676 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
  9678 /*
  9679 ** Convenience functions for opening and closing files using 
  9680 ** sqlite3_malloc() to obtain space for the file-handle structure.
  9681 */
  9682 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
  9683 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
  9685 #endif /* _SQLITE_OS_H_ */
  9687 /************** End of os.h **************************************************/
  9688 /************** Continuing where we left off in sqliteInt.h ******************/
  9689 /************** Include mutex.h in the middle of sqliteInt.h *****************/
  9690 /************** Begin file mutex.h *******************************************/
  9691 /*
  9692 ** 2007 August 28
  9693 **
  9694 ** The author disclaims copyright to this source code.  In place of
  9695 ** a legal notice, here is a blessing:
  9696 **
  9697 **    May you do good and not evil.
  9698 **    May you find forgiveness for yourself and forgive others.
  9699 **    May you share freely, never taking more than you give.
  9700 **
  9701 *************************************************************************
  9702 **
  9703 ** This file contains the common header for all mutex implementations.
  9704 ** The sqliteInt.h header #includes this file so that it is available
  9705 ** to all source files.  We break it out in an effort to keep the code
  9706 ** better organized.
  9707 **
  9708 ** NOTE:  source files should *not* #include this header file directly.
  9709 ** Source files should #include the sqliteInt.h file and let that file
  9710 ** include this one indirectly.
  9711 */
  9714 /*
  9715 ** Figure out what version of the code to use.  The choices are
  9716 **
  9717 **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
  9718 **                             mutexes implemention cannot be overridden
  9719 **                             at start-time.
  9720 **
  9721 **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
  9722 **                             mutual exclusion is provided.  But this
  9723 **                             implementation can be overridden at
  9724 **                             start-time.
  9725 **
  9726 **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
  9727 **
  9728 **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
  9729 */
  9730 #if !SQLITE_THREADSAFE
  9731 # define SQLITE_MUTEX_OMIT
  9732 #endif
  9733 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
  9734 #  if SQLITE_OS_UNIX
  9735 #    define SQLITE_MUTEX_PTHREADS
  9736 #  elif SQLITE_OS_WIN
  9737 #    define SQLITE_MUTEX_W32
  9738 #  else
  9739 #    define SQLITE_MUTEX_NOOP
  9740 #  endif
  9741 #endif
  9743 #ifdef SQLITE_MUTEX_OMIT
  9744 /*
  9745 ** If this is a no-op implementation, implement everything as macros.
  9746 */
  9747 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
  9748 #define sqlite3_mutex_free(X)
  9749 #define sqlite3_mutex_enter(X)    
  9750 #define sqlite3_mutex_try(X)      SQLITE_OK
  9751 #define sqlite3_mutex_leave(X)    
  9752 #define sqlite3_mutex_held(X)     ((void)(X),1)
  9753 #define sqlite3_mutex_notheld(X)  ((void)(X),1)
  9754 #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
  9755 #define sqlite3MutexInit()        SQLITE_OK
  9756 #define sqlite3MutexEnd()
  9757 #define MUTEX_LOGIC(X)
  9758 #else
  9759 #define MUTEX_LOGIC(X)            X
  9760 #endif /* defined(SQLITE_MUTEX_OMIT) */
  9762 /************** End of mutex.h ***********************************************/
  9763 /************** Continuing where we left off in sqliteInt.h ******************/
  9766 /*
  9767 ** Each database file to be accessed by the system is an instance
  9768 ** of the following structure.  There are normally two of these structures
  9769 ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
  9770 ** aDb[1] is the database file used to hold temporary tables.  Additional
  9771 ** databases may be attached.
  9772 */
  9773 struct Db {
  9774   char *zName;         /* Name of this database */
  9775   Btree *pBt;          /* The B*Tree structure for this database file */
  9776   u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
  9777   u8 safety_level;     /* How aggressive at syncing data to disk */
  9778   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
  9779 };
  9781 /*
  9782 ** An instance of the following structure stores a database schema.
  9783 **
  9784 ** Most Schema objects are associated with a Btree.  The exception is
  9785 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
  9786 ** In shared cache mode, a single Schema object can be shared by multiple
  9787 ** Btrees that refer to the same underlying BtShared object.
  9788 ** 
  9789 ** Schema objects are automatically deallocated when the last Btree that
  9790 ** references them is destroyed.   The TEMP Schema is manually freed by
  9791 ** sqlite3_close().
  9793 ** A thread must be holding a mutex on the corresponding Btree in order
  9794 ** to access Schema content.  This implies that the thread must also be
  9795 ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
  9796 ** For a TEMP Schema, only the connection mutex is required.
  9797 */
  9798 struct Schema {
  9799   int schema_cookie;   /* Database schema version number for this file */
  9800   int iGeneration;     /* Generation counter.  Incremented with each change */
  9801   Hash tblHash;        /* All tables indexed by name */
  9802   Hash idxHash;        /* All (named) indices indexed by name */
  9803   Hash trigHash;       /* All triggers indexed by name */
  9804   Hash fkeyHash;       /* All foreign keys by referenced table name */
  9805   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  9806   u8 file_format;      /* Schema format version for this file */
  9807   u8 enc;              /* Text encoding used by this database */
  9808   u16 flags;           /* Flags associated with this schema */
  9809   int cache_size;      /* Number of pages to use in the cache */
  9810 };
  9812 /*
  9813 ** These macros can be used to test, set, or clear bits in the 
  9814 ** Db.pSchema->flags field.
  9815 */
  9816 #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
  9817 #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
  9818 #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
  9819 #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
  9821 /*
  9822 ** Allowed values for the DB.pSchema->flags field.
  9823 **
  9824 ** The DB_SchemaLoaded flag is set after the database schema has been
  9825 ** read into internal hash tables.
  9826 **
  9827 ** DB_UnresetViews means that one or more views have column names that
  9828 ** have been filled out.  If the schema changes, these column names might
  9829 ** changes and so the view will need to be reset.
  9830 */
  9831 #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
  9832 #define DB_UnresetViews    0x0002  /* Some views have defined column names */
  9833 #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
  9835 /*
  9836 ** The number of different kinds of things that can be limited
  9837 ** using the sqlite3_limit() interface.
  9838 */
  9839 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
  9841 /*
  9842 ** Lookaside malloc is a set of fixed-size buffers that can be used
  9843 ** to satisfy small transient memory allocation requests for objects
  9844 ** associated with a particular database connection.  The use of
  9845 ** lookaside malloc provides a significant performance enhancement
  9846 ** (approx 10%) by avoiding numerous malloc/free requests while parsing
  9847 ** SQL statements.
  9848 **
  9849 ** The Lookaside structure holds configuration information about the
  9850 ** lookaside malloc subsystem.  Each available memory allocation in
  9851 ** the lookaside subsystem is stored on a linked list of LookasideSlot
  9852 ** objects.
  9853 **
  9854 ** Lookaside allocations are only allowed for objects that are associated
  9855 ** with a particular database connection.  Hence, schema information cannot
  9856 ** be stored in lookaside because in shared cache mode the schema information
  9857 ** is shared by multiple database connections.  Therefore, while parsing
  9858 ** schema information, the Lookaside.bEnabled flag is cleared so that
  9859 ** lookaside allocations are not used to construct the schema objects.
  9860 */
  9861 struct Lookaside {
  9862   u16 sz;                 /* Size of each buffer in bytes */
  9863   u8 bEnabled;            /* False to disable new lookaside allocations */
  9864   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
  9865   int nOut;               /* Number of buffers currently checked out */
  9866   int mxOut;              /* Highwater mark for nOut */
  9867   int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
  9868   LookasideSlot *pFree;   /* List of available buffers */
  9869   void *pStart;           /* First byte of available memory space */
  9870   void *pEnd;             /* First byte past end of available space */
  9871 };
  9872 struct LookasideSlot {
  9873   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
  9874 };
  9876 /*
  9877 ** A hash table for function definitions.
  9878 **
  9879 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  9880 ** Collisions are on the FuncDef.pHash chain.
  9881 */
  9882 struct FuncDefHash {
  9883   FuncDef *a[23];       /* Hash table for functions */
  9884 };
  9886 /*
  9887 ** Each database connection is an instance of the following structure.
  9888 */
  9889 struct sqlite3 {
  9890   sqlite3_vfs *pVfs;            /* OS Interface */
  9891   struct Vdbe *pVdbe;           /* List of active virtual machines */
  9892   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
  9893   sqlite3_mutex *mutex;         /* Connection mutex */
  9894   Db *aDb;                      /* All backends */
  9895   int nDb;                      /* Number of backends currently in use */
  9896   int flags;                    /* Miscellaneous flags. See below */
  9897   i64 lastRowid;                /* ROWID of most recent insert (see above) */
  9898   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
  9899   int errCode;                  /* Most recent error code (SQLITE_*) */
  9900   int errMask;                  /* & result codes with this before returning */
  9901   u16 dbOptFlags;               /* Flags to enable/disable optimizations */
  9902   u8 autoCommit;                /* The auto-commit flag. */
  9903   u8 temp_store;                /* 1: file 2: memory 0: default */
  9904   u8 mallocFailed;              /* True if we have seen a malloc failure */
  9905   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
  9906   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
  9907   u8 suppressErr;               /* Do not issue error messages if true */
  9908   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
  9909   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
  9910   int nextPagesize;             /* Pagesize after VACUUM if >0 */
  9911   u32 magic;                    /* Magic number for detect library misuse */
  9912   int nChange;                  /* Value returned by sqlite3_changes() */
  9913   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
  9914   int aLimit[SQLITE_N_LIMIT];   /* Limits */
  9915   struct sqlite3InitInfo {      /* Information used during initialization */
  9916     int newTnum;                /* Rootpage of table being initialized */
  9917     u8 iDb;                     /* Which db file is being initialized */
  9918     u8 busy;                    /* TRUE if currently initializing */
  9919     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
  9920   } init;
  9921   int activeVdbeCnt;            /* Number of VDBEs currently executing */
  9922   int writeVdbeCnt;             /* Number of active VDBEs that are writing */
  9923   int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
  9924   int nExtension;               /* Number of loaded extensions */
  9925   void **aExtension;            /* Array of shared library handles */
  9926   void (*xTrace)(void*,const char*);        /* Trace function */
  9927   void *pTraceArg;                          /* Argument to the trace function */
  9928   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
  9929   void *pProfileArg;                        /* Argument to profile function */
  9930   void *pCommitArg;                 /* Argument to xCommitCallback() */   
  9931   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
  9932   void *pRollbackArg;               /* Argument to xRollbackCallback() */   
  9933   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
  9934   void *pUpdateArg;
  9935   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
  9936 #ifndef SQLITE_OMIT_WAL
  9937   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
  9938   void *pWalArg;
  9939 #endif
  9940   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
  9941   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
  9942   void *pCollNeededArg;
  9943   sqlite3_value *pErr;          /* Most recent error message */
  9944   char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
  9945   char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
  9946   union {
  9947     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
  9948     double notUsed1;            /* Spacer */
  9949   } u1;
  9950   Lookaside lookaside;          /* Lookaside malloc configuration */
  9951 #ifndef SQLITE_OMIT_AUTHORIZATION
  9952   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
  9953                                 /* Access authorization function */
  9954   void *pAuthArg;               /* 1st argument to the access auth function */
  9955 #endif
  9956 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  9957   int (*xProgress)(void *);     /* The progress callback */
  9958   void *pProgressArg;           /* Argument to the progress callback */
  9959   int nProgressOps;             /* Number of opcodes for progress callback */
  9960 #endif
  9961 #ifndef SQLITE_OMIT_VIRTUALTABLE
  9962   int nVTrans;                  /* Allocated size of aVTrans */
  9963   Hash aModule;                 /* populated by sqlite3_create_module() */
  9964   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
  9965   VTable **aVTrans;             /* Virtual tables with open transactions */
  9966   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
  9967 #endif
  9968   FuncDefHash aFunc;            /* Hash table of connection functions */
  9969   Hash aCollSeq;                /* All collating sequences */
  9970   BusyHandler busyHandler;      /* Busy callback */
  9971   Db aDbStatic[2];              /* Static space for the 2 default backends */
  9972   Savepoint *pSavepoint;        /* List of active savepoints */
  9973   int busyTimeout;              /* Busy handler timeout, in msec */
  9974   int nSavepoint;               /* Number of non-transaction savepoints */
  9975   int nStatement;               /* Number of nested statement-transactions  */
  9976   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
  9977   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
  9979 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  9980   /* The following variables are all protected by the STATIC_MASTER 
  9981   ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
  9982   **
  9983   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  9984   ** unlock so that it can proceed.
  9985   **
  9986   ** When X.pBlockingConnection==Y, that means that something that X tried
  9987   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
  9988   ** held by Y.
  9989   */
  9990   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
  9991   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
  9992   void *pUnlockArg;                     /* Argument to xUnlockNotify */
  9993   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
  9994   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
  9995 #endif
  9996 };
  9998 /*
  9999 ** A macro to discover the encoding of a database.
 10000 */
 10001 #define ENC(db) ((db)->aDb[0].pSchema->enc)
 10003 /*
 10004 ** Possible values for the sqlite3.flags.
 10005 */
 10006 #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
 10007 #define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
 10008 #define SQLITE_FullColNames   0x00000004  /* Show full column names on SELECT */
 10009 #define SQLITE_ShortColNames  0x00000008  /* Show short columns names */
 10010 #define SQLITE_CountRows      0x00000010  /* Count rows changed by INSERT, */
 10011                                           /*   DELETE, or UPDATE and return */
 10012                                           /*   the count using a callback. */
 10013 #define SQLITE_NullCallback   0x00000020  /* Invoke the callback once if the */
 10014                                           /*   result set is empty */
 10015 #define SQLITE_SqlTrace       0x00000040  /* Debug print SQL as it executes */
 10016 #define SQLITE_VdbeListing    0x00000080  /* Debug listings of VDBE programs */
 10017 #define SQLITE_WriteSchema    0x00000100  /* OK to update SQLITE_MASTER */
 10018                          /*   0x00000200  Unused */
 10019 #define SQLITE_IgnoreChecks   0x00000400  /* Do not enforce check constraints */
 10020 #define SQLITE_ReadUncommitted 0x0000800  /* For shared-cache mode */
 10021 #define SQLITE_LegacyFileFmt  0x00001000  /* Create new databases in format 1 */
 10022 #define SQLITE_FullFSync      0x00002000  /* Use full fsync on the backend */
 10023 #define SQLITE_CkptFullFSync  0x00004000  /* Use full fsync for checkpoint */
 10024 #define SQLITE_RecoveryMode   0x00008000  /* Ignore schema errors */
 10025 #define SQLITE_ReverseOrder   0x00010000  /* Reverse unordered SELECTs */
 10026 #define SQLITE_RecTriggers    0x00020000  /* Enable recursive triggers */
 10027 #define SQLITE_ForeignKeys    0x00040000  /* Enforce foreign key constraints  */
 10028 #define SQLITE_AutoIndex      0x00080000  /* Enable automatic indexes */
 10029 #define SQLITE_PreferBuiltin  0x00100000  /* Preference to built-in funcs */
 10030 #define SQLITE_LoadExtension  0x00200000  /* Enable load_extension */
 10031 #define SQLITE_EnableTrigger  0x00400000  /* True to enable triggers */
 10033 /*
 10034 ** Bits of the sqlite3.dbOptFlags field that are used by the
 10035 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 10036 ** selectively disable various optimizations.
 10037 */
 10038 #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 10039 #define SQLITE_ColumnCache    0x0002   /* Column cache */
 10040 #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 10041 #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 10042 #define SQLITE_IdxRealAsInt   0x0010   /* Store REAL as INT in indices */
 10043 #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
 10044 #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
 10045 #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
 10046 #define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
 10047 #define SQLITE_AllOpts        0xffff   /* All optimizations */
 10049 /*
 10050 ** Macros for testing whether or not optimizations are enabled or disabled.
 10051 */
 10052 #ifndef SQLITE_OMIT_BUILTIN_TEST
 10053 #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 10054 #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
 10055 #else
 10056 #define OptimizationDisabled(db, mask)  0
 10057 #define OptimizationEnabled(db, mask)   1
 10058 #endif
 10060 /*
 10061 ** Possible values for the sqlite.magic field.
 10062 ** The numbers are obtained at random and have no special meaning, other
 10063 ** than being distinct from one another.
 10064 */
 10065 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
 10066 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
 10067 #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
 10068 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
 10069 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
 10070 #define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
 10072 /*
 10073 ** Each SQL function is defined by an instance of the following
 10074 ** structure.  A pointer to this structure is stored in the sqlite.aFunc
 10075 ** hash table.  When multiple functions have the same name, the hash table
 10076 ** points to a linked list of these structures.
 10077 */
 10078 struct FuncDef {
 10079   i16 nArg;            /* Number of arguments.  -1 means unlimited */
 10080   u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
 10081   u8 flags;            /* Some combination of SQLITE_FUNC_* */
 10082   void *pUserData;     /* User data parameter */
 10083   FuncDef *pNext;      /* Next function with same name */
 10084   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
 10085   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
 10086   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
 10087   char *zName;         /* SQL name of the function. */
 10088   FuncDef *pHash;      /* Next with a different name but the same hash */
 10089   FuncDestructor *pDestructor;   /* Reference counted destructor function */
 10090 };
 10092 /*
 10093 ** This structure encapsulates a user-function destructor callback (as
 10094 ** configured using create_function_v2()) and a reference counter. When
 10095 ** create_function_v2() is called to create a function with a destructor,
 10096 ** a single object of this type is allocated. FuncDestructor.nRef is set to 
 10097 ** the number of FuncDef objects created (either 1 or 3, depending on whether
 10098 ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
 10099 ** member of each of the new FuncDef objects is set to point to the allocated
 10100 ** FuncDestructor.
 10101 **
 10102 ** Thereafter, when one of the FuncDef objects is deleted, the reference
 10103 ** count on this object is decremented. When it reaches 0, the destructor
 10104 ** is invoked and the FuncDestructor structure freed.
 10105 */
 10106 struct FuncDestructor {
 10107   int nRef;
 10108   void (*xDestroy)(void *);
 10109   void *pUserData;
 10110 };
 10112 /*
 10113 ** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
 10114 ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
 10115 ** are assert() statements in the code to verify this.
 10116 */
 10117 #define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
 10118 #define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
 10119 #define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
 10120 #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
 10121 #define SQLITE_FUNC_COUNT    0x10 /* Built-in count(*) aggregate */
 10122 #define SQLITE_FUNC_COALESCE 0x20 /* Built-in coalesce() or ifnull() function */
 10123 #define SQLITE_FUNC_LENGTH   0x40 /* Built-in length() function */
 10124 #define SQLITE_FUNC_TYPEOF   0x80 /* Built-in typeof() function */
 10126 /*
 10127 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 10128 ** used to create the initializers for the FuncDef structures.
 10129 **
 10130 **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 10131 **     Used to create a scalar function definition of a function zName 
 10132 **     implemented by C function xFunc that accepts nArg arguments. The
 10133 **     value passed as iArg is cast to a (void*) and made available
 10134 **     as the user-data (sqlite3_user_data()) for the function. If 
 10135 **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
 10136 **
 10137 **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
 10138 **     Used to create an aggregate function definition implemented by
 10139 **     the C functions xStep and xFinal. The first four parameters
 10140 **     are interpreted in the same way as the first 4 parameters to
 10141 **     FUNCTION().
 10142 **
 10143 **   LIKEFUNC(zName, nArg, pArg, flags)
 10144 **     Used to create a scalar function definition of a function zName 
 10145 **     that accepts nArg arguments and is implemented by a call to C 
 10146 **     function likeFunc. Argument pArg is cast to a (void *) and made
 10147 **     available as the function user-data (sqlite3_user_data()). The
 10148 **     FuncDef.flags variable is set to the value passed as the flags
 10149 **     parameter.
 10150 */
 10151 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
 10152   {nArg, SQLITE_UTF8, (bNC*SQLITE_FUNC_NEEDCOLL), \
 10153    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
 10154 #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
 10155   {nArg, SQLITE_UTF8, (bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
 10156    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
 10157 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
 10158   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
 10159    pArg, 0, xFunc, 0, 0, #zName, 0, 0}
 10160 #define LIKEFUNC(zName, nArg, arg, flags) \
 10161   {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
 10162 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
 10163   {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
 10164    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
 10166 /*
 10167 ** All current savepoints are stored in a linked list starting at
 10168 ** sqlite3.pSavepoint. The first element in the list is the most recently
 10169 ** opened savepoint. Savepoints are added to the list by the vdbe
 10170 ** OP_Savepoint instruction.
 10171 */
 10172 struct Savepoint {
 10173   char *zName;                        /* Savepoint name (nul-terminated) */
 10174   i64 nDeferredCons;                  /* Number of deferred fk violations */
 10175   Savepoint *pNext;                   /* Parent savepoint (if any) */
 10176 };
 10178 /*
 10179 ** The following are used as the second parameter to sqlite3Savepoint(),
 10180 ** and as the P1 argument to the OP_Savepoint instruction.
 10181 */
 10182 #define SAVEPOINT_BEGIN      0
 10183 #define SAVEPOINT_RELEASE    1
 10184 #define SAVEPOINT_ROLLBACK   2
 10187 /*
 10188 ** Each SQLite module (virtual table definition) is defined by an
 10189 ** instance of the following structure, stored in the sqlite3.aModule
 10190 ** hash table.
 10191 */
 10192 struct Module {
 10193   const sqlite3_module *pModule;       /* Callback pointers */
 10194   const char *zName;                   /* Name passed to create_module() */
 10195   void *pAux;                          /* pAux passed to create_module() */
 10196   void (*xDestroy)(void *);            /* Module destructor function */
 10197 };
 10199 /*
 10200 ** information about each column of an SQL table is held in an instance
 10201 ** of this structure.
 10202 */
 10203 struct Column {
 10204   char *zName;     /* Name of this column */
 10205   Expr *pDflt;     /* Default value of this column */
 10206   char *zDflt;     /* Original text of the default value */
 10207   char *zType;     /* Data type for this column */
 10208   char *zColl;     /* Collating sequence.  If NULL, use the default */
 10209   u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
 10210   char affinity;   /* One of the SQLITE_AFF_... values */
 10211   u16 colFlags;    /* Boolean properties.  See COLFLAG_ defines below */
 10212 };
 10214 /* Allowed values for Column.colFlags:
 10215 */
 10216 #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 10217 #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 10219 /*
 10220 ** A "Collating Sequence" is defined by an instance of the following
 10221 ** structure. Conceptually, a collating sequence consists of a name and
 10222 ** a comparison routine that defines the order of that sequence.
 10223 **
 10224 ** If CollSeq.xCmp is NULL, it means that the
 10225 ** collating sequence is undefined.  Indices built on an undefined
 10226 ** collating sequence may not be read or written.
 10227 */
 10228 struct CollSeq {
 10229   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
 10230   u8 enc;               /* Text encoding handled by xCmp() */
 10231   void *pUser;          /* First argument to xCmp() */
 10232   int (*xCmp)(void*,int, const void*, int, const void*);
 10233   void (*xDel)(void*);  /* Destructor for pUser */
 10234 };
 10236 /*
 10237 ** A sort order can be either ASC or DESC.
 10238 */
 10239 #define SQLITE_SO_ASC       0  /* Sort in ascending order */
 10240 #define SQLITE_SO_DESC      1  /* Sort in ascending order */
 10242 /*
 10243 ** Column affinity types.
 10244 **
 10245 ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
 10246 ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
 10247 ** the speed a little by numbering the values consecutively.  
 10248 **
 10249 ** But rather than start with 0 or 1, we begin with 'a'.  That way,
 10250 ** when multiple affinity types are concatenated into a string and
 10251 ** used as the P4 operand, they will be more readable.
 10252 **
 10253 ** Note also that the numeric types are grouped together so that testing
 10254 ** for a numeric type is a single comparison.
 10255 */
 10256 #define SQLITE_AFF_TEXT     'a'
 10257 #define SQLITE_AFF_NONE     'b'
 10258 #define SQLITE_AFF_NUMERIC  'c'
 10259 #define SQLITE_AFF_INTEGER  'd'
 10260 #define SQLITE_AFF_REAL     'e'
 10262 #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
 10264 /*
 10265 ** The SQLITE_AFF_MASK values masks off the significant bits of an
 10266 ** affinity value. 
 10267 */
 10268 #define SQLITE_AFF_MASK     0x67
 10270 /*
 10271 ** Additional bit values that can be ORed with an affinity without
 10272 ** changing the affinity.
 10273 */
 10274 #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
 10275 #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
 10276 #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
 10278 /*
 10279 ** An object of this type is created for each virtual table present in
 10280 ** the database schema. 
 10281 **
 10282 ** If the database schema is shared, then there is one instance of this
 10283 ** structure for each database connection (sqlite3*) that uses the shared
 10284 ** schema. This is because each database connection requires its own unique
 10285 ** instance of the sqlite3_vtab* handle used to access the virtual table 
 10286 ** implementation. sqlite3_vtab* handles can not be shared between 
 10287 ** database connections, even when the rest of the in-memory database 
 10288 ** schema is shared, as the implementation often stores the database
 10289 ** connection handle passed to it via the xConnect() or xCreate() method
 10290 ** during initialization internally. This database connection handle may
 10291 ** then be used by the virtual table implementation to access real tables 
 10292 ** within the database. So that they appear as part of the callers 
 10293 ** transaction, these accesses need to be made via the same database 
 10294 ** connection as that used to execute SQL operations on the virtual table.
 10295 **
 10296 ** All VTable objects that correspond to a single table in a shared
 10297 ** database schema are initially stored in a linked-list pointed to by
 10298 ** the Table.pVTable member variable of the corresponding Table object.
 10299 ** When an sqlite3_prepare() operation is required to access the virtual
 10300 ** table, it searches the list for the VTable that corresponds to the
 10301 ** database connection doing the preparing so as to use the correct
 10302 ** sqlite3_vtab* handle in the compiled query.
 10303 **
 10304 ** When an in-memory Table object is deleted (for example when the
 10305 ** schema is being reloaded for some reason), the VTable objects are not 
 10306 ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
 10307 ** immediately. Instead, they are moved from the Table.pVTable list to
 10308 ** another linked list headed by the sqlite3.pDisconnect member of the
 10309 ** corresponding sqlite3 structure. They are then deleted/xDisconnected 
 10310 ** next time a statement is prepared using said sqlite3*. This is done
 10311 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
 10312 ** Refer to comments above function sqlite3VtabUnlockList() for an
 10313 ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
 10314 ** list without holding the corresponding sqlite3.mutex mutex.
 10315 **
 10316 ** The memory for objects of this type is always allocated by 
 10317 ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
 10318 ** the first argument.
 10319 */
 10320 struct VTable {
 10321   sqlite3 *db;              /* Database connection associated with this table */
 10322   Module *pMod;             /* Pointer to module implementation */
 10323   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
 10324   int nRef;                 /* Number of pointers to this structure */
 10325   u8 bConstraint;           /* True if constraints are supported */
 10326   int iSavepoint;           /* Depth of the SAVEPOINT stack */
 10327   VTable *pNext;            /* Next in linked list (see above) */
 10328 };
 10330 /*
 10331 ** Each SQL table is represented in memory by an instance of the
 10332 ** following structure.
 10333 **
 10334 ** Table.zName is the name of the table.  The case of the original
 10335 ** CREATE TABLE statement is stored, but case is not significant for
 10336 ** comparisons.
 10337 **
 10338 ** Table.nCol is the number of columns in this table.  Table.aCol is a
 10339 ** pointer to an array of Column structures, one for each column.
 10340 **
 10341 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
 10342 ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
 10343 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
 10344 ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
 10345 ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
 10346 ** is generated for each row of the table.  TF_HasPrimaryKey is set if
 10347 ** the table has any PRIMARY KEY, INTEGER or otherwise.
 10348 **
 10349 ** Table.tnum is the page number for the root BTree page of the table in the
 10350 ** database file.  If Table.iDb is the index of the database table backend
 10351 ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
 10352 ** holds temporary tables and indices.  If TF_Ephemeral is set
 10353 ** then the table is stored in a file that is automatically deleted
 10354 ** when the VDBE cursor to the table is closed.  In this case Table.tnum 
 10355 ** refers VDBE cursor number that holds the table open, not to the root
 10356 ** page number.  Transient tables are used to hold the results of a
 10357 ** sub-query that appears instead of a real table name in the FROM clause 
 10358 ** of a SELECT statement.
 10359 */
 10360 struct Table {
 10361   char *zName;         /* Name of the table or view */
 10362   Column *aCol;        /* Information about each column */
 10363   Index *pIndex;       /* List of SQL indexes on this table. */
 10364   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 10365   FKey *pFKey;         /* Linked list of all foreign keys in this table */
 10366   char *zColAff;       /* String defining the affinity of each column */
 10367 #ifndef SQLITE_OMIT_CHECK
 10368   ExprList *pCheck;    /* All CHECK constraints */
 10369 #endif
 10370   tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
 10371   int tnum;            /* Root BTree node for this table (see note above) */
 10372   i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
 10373   i16 nCol;            /* Number of columns in this table */
 10374   u16 nRef;            /* Number of pointers to this Table */
 10375   u8 tabFlags;         /* Mask of TF_* values */
 10376   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
 10377 #ifndef SQLITE_OMIT_ALTERTABLE
 10378   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
 10379 #endif
 10380 #ifndef SQLITE_OMIT_VIRTUALTABLE
 10381   int nModuleArg;      /* Number of arguments to the module */
 10382   char **azModuleArg;  /* Text of all module args. [0] is module name */
 10383   VTable *pVTable;     /* List of VTable objects. */
 10384 #endif
 10385   Trigger *pTrigger;   /* List of triggers stored in pSchema */
 10386   Schema *pSchema;     /* Schema that contains this table */
 10387   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
 10388 };
 10390 /*
 10391 ** Allowed values for Tabe.tabFlags.
 10392 */
 10393 #define TF_Readonly        0x01    /* Read-only system table */
 10394 #define TF_Ephemeral       0x02    /* An ephemeral table */
 10395 #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
 10396 #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
 10397 #define TF_Virtual         0x10    /* Is a virtual table */
 10400 /*
 10401 ** Test to see whether or not a table is a virtual table.  This is
 10402 ** done as a macro so that it will be optimized out when virtual
 10403 ** table support is omitted from the build.
 10404 */
 10405 #ifndef SQLITE_OMIT_VIRTUALTABLE
 10406 #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
 10407 #  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
 10408 #else
 10409 #  define IsVirtual(X)      0
 10410 #  define IsHiddenColumn(X) 0
 10411 #endif
 10413 /*
 10414 ** Each foreign key constraint is an instance of the following structure.
 10415 **
 10416 ** A foreign key is associated with two tables.  The "from" table is
 10417 ** the table that contains the REFERENCES clause that creates the foreign
 10418 ** key.  The "to" table is the table that is named in the REFERENCES clause.
 10419 ** Consider this example:
 10420 **
 10421 **     CREATE TABLE ex1(
 10422 **       a INTEGER PRIMARY KEY,
 10423 **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
 10424 **     );
 10425 **
 10426 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
 10427 **
 10428 ** Each REFERENCES clause generates an instance of the following structure
 10429 ** which is attached to the from-table.  The to-table need not exist when
 10430 ** the from-table is created.  The existence of the to-table is not checked.
 10431 */
 10432 struct FKey {
 10433   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
 10434   FKey *pNextFrom;  /* Next foreign key in pFrom */
 10435   char *zTo;        /* Name of table that the key points to (aka: Parent) */
 10436   FKey *pNextTo;    /* Next foreign key on table named zTo */
 10437   FKey *pPrevTo;    /* Previous foreign key on table named zTo */
 10438   int nCol;         /* Number of columns in this key */
 10439   /* EV: R-30323-21917 */
 10440   u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
 10441   u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
 10442   Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
 10443   struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
 10444     int iFrom;         /* Index of column in pFrom */
 10445     char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
 10446   } aCol[1];        /* One entry for each of nCol column s */
 10447 };
 10449 /*
 10450 ** SQLite supports many different ways to resolve a constraint
 10451 ** error.  ROLLBACK processing means that a constraint violation
 10452 ** causes the operation in process to fail and for the current transaction
 10453 ** to be rolled back.  ABORT processing means the operation in process
 10454 ** fails and any prior changes from that one operation are backed out,
 10455 ** but the transaction is not rolled back.  FAIL processing means that
 10456 ** the operation in progress stops and returns an error code.  But prior
 10457 ** changes due to the same operation are not backed out and no rollback
 10458 ** occurs.  IGNORE means that the particular row that caused the constraint
 10459 ** error is not inserted or updated.  Processing continues and no error
 10460 ** is returned.  REPLACE means that preexisting database rows that caused
 10461 ** a UNIQUE constraint violation are removed so that the new insert or
 10462 ** update can proceed.  Processing continues and no error is reported.
 10463 **
 10464 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
 10465 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
 10466 ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
 10467 ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
 10468 ** referenced table row is propagated into the row that holds the
 10469 ** foreign key.
 10470 ** 
 10471 ** The following symbolic values are used to record which type
 10472 ** of action to take.
 10473 */
 10474 #define OE_None     0   /* There is no constraint to check */
 10475 #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
 10476 #define OE_Abort    2   /* Back out changes but do no rollback transaction */
 10477 #define OE_Fail     3   /* Stop the operation but leave all prior changes */
 10478 #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
 10479 #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
 10481 #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
 10482 #define OE_SetNull  7   /* Set the foreign key value to NULL */
 10483 #define OE_SetDflt  8   /* Set the foreign key value to its default */
 10484 #define OE_Cascade  9   /* Cascade the changes */
 10486 #define OE_Default  99  /* Do whatever the default action is */
 10489 /*
 10490 ** An instance of the following structure is passed as the first
 10491 ** argument to sqlite3VdbeKeyCompare and is used to control the 
 10492 ** comparison of the two index keys.
 10493 */
 10494 struct KeyInfo {
 10495   sqlite3 *db;        /* The database connection */
 10496   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
 10497   u16 nField;         /* Number of entries in aColl[] */
 10498   u8 *aSortOrder;     /* Sort order for each column.  May be NULL */
 10499   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
 10500 };
 10502 /*
 10503 ** An instance of the following structure holds information about a
 10504 ** single index record that has already been parsed out into individual
 10505 ** values.
 10506 **
 10507 ** A record is an object that contains one or more fields of data.
 10508 ** Records are used to store the content of a table row and to store
 10509 ** the key of an index.  A blob encoding of a record is created by
 10510 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
 10511 ** OP_Column opcode.
 10512 **
 10513 ** This structure holds a record that has already been disassembled
 10514 ** into its constituent fields.
 10515 */
 10516 struct UnpackedRecord {
 10517   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
 10518   u16 nField;         /* Number of entries in apMem[] */
 10519   u8 flags;           /* Boolean settings.  UNPACKED_... below */
 10520   i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
 10521   Mem *aMem;          /* Values */
 10522 };
 10524 /*
 10525 ** Allowed values of UnpackedRecord.flags
 10526 */
 10527 #define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
 10528 #define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
 10529 #define UNPACKED_PREFIX_SEARCH 0x04  /* Ignore final (rowid) field */
 10531 /*
 10532 ** Each SQL index is represented in memory by an
 10533 ** instance of the following structure.
 10534 **
 10535 ** The columns of the table that are to be indexed are described
 10536 ** by the aiColumn[] field of this structure.  For example, suppose
 10537 ** we have the following table and index:
 10538 **
 10539 **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
 10540 **     CREATE INDEX Ex2 ON Ex1(c3,c1);
 10541 **
 10542 ** In the Table structure describing Ex1, nCol==3 because there are
 10543 ** three columns in the table.  In the Index structure describing
 10544 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
 10545 ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
 10546 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
 10547 ** The second column to be indexed (c1) has an index of 0 in
 10548 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
 10549 **
 10550 ** The Index.onError field determines whether or not the indexed columns
 10551 ** must be unique and what to do if they are not.  When Index.onError=OE_None,
 10552 ** it means this is not a unique index.  Otherwise it is a unique index
 10553 ** and the value of Index.onError indicate the which conflict resolution 
 10554 ** algorithm to employ whenever an attempt is made to insert a non-unique
 10555 ** element.
 10556 */
 10557 struct Index {
 10558   char *zName;     /* Name of this index */
 10559   int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
 10560   tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
 10561   Table *pTable;   /* The SQL table being indexed */
 10562   char *zColAff;   /* String defining the affinity of each column */
 10563   Index *pNext;    /* The next index associated with the same table */
 10564   Schema *pSchema; /* Schema containing this index */
 10565   u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
 10566   char **azColl;   /* Array of collation sequence names for index */
 10567   int nColumn;     /* Number of columns in the table used by this index */
 10568   int tnum;        /* Page containing root of this index in database file */
 10569   u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 10570   u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
 10571   u8 bUnordered;   /* Use this index for == or IN queries only */
 10572 #ifdef SQLITE_ENABLE_STAT3
 10573   int nSample;             /* Number of elements in aSample[] */
 10574   tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
 10575   IndexSample *aSample;    /* Samples of the left-most key */
 10576 #endif
 10577 };
 10579 /*
 10580 ** Each sample stored in the sqlite_stat3 table is represented in memory 
 10581 ** using a structure of this type.  See documentation at the top of the
 10582 ** analyze.c source file for additional information.
 10583 */
 10584 struct IndexSample {
 10585   union {
 10586     char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
 10587     double r;       /* Value if eType is SQLITE_FLOAT */
 10588     i64 i;          /* Value if eType is SQLITE_INTEGER */
 10589   } u;
 10590   u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
 10591   int nByte;        /* Size in byte of text or blob. */
 10592   tRowcnt nEq;      /* Est. number of rows where the key equals this sample */
 10593   tRowcnt nLt;      /* Est. number of rows where key is less than this sample */
 10594   tRowcnt nDLt;     /* Est. number of distinct keys less than this sample */
 10595 };
 10597 /*
 10598 ** Each token coming out of the lexer is an instance of
 10599 ** this structure.  Tokens are also used as part of an expression.
 10600 **
 10601 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
 10602 ** may contain random values.  Do not make any assumptions about Token.dyn
 10603 ** and Token.n when Token.z==0.
 10604 */
 10605 struct Token {
 10606   const char *z;     /* Text of the token.  Not NULL-terminated! */
 10607   unsigned int n;    /* Number of characters in this token */
 10608 };
 10610 /*
 10611 ** An instance of this structure contains information needed to generate
 10612 ** code for a SELECT that contains aggregate functions.
 10613 **
 10614 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
 10615 ** pointer to this structure.  The Expr.iColumn field is the index in
 10616 ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
 10617 ** code for that node.
 10618 **
 10619 ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
 10620 ** original Select structure that describes the SELECT statement.  These
 10621 ** fields do not need to be freed when deallocating the AggInfo structure.
 10622 */
 10623 struct AggInfo {
 10624   u8 directMode;          /* Direct rendering mode means take data directly
 10625                           ** from source tables rather than from accumulators */
 10626   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
 10627                           ** than the source table */
 10628   int sortingIdx;         /* Cursor number of the sorting index */
 10629   int sortingIdxPTab;     /* Cursor number of pseudo-table */
 10630   int nSortingColumn;     /* Number of columns in the sorting index */
 10631   ExprList *pGroupBy;     /* The group by clause */
 10632   struct AggInfo_col {    /* For each column used in source tables */
 10633     Table *pTab;             /* Source table */
 10634     int iTable;              /* Cursor number of the source table */
 10635     int iColumn;             /* Column number within the source table */
 10636     int iSorterColumn;       /* Column number in the sorting index */
 10637     int iMem;                /* Memory location that acts as accumulator */
 10638     Expr *pExpr;             /* The original expression */
 10639   } *aCol;
 10640   int nColumn;            /* Number of used entries in aCol[] */
 10641   int nAccumulator;       /* Number of columns that show through to the output.
 10642                           ** Additional columns are used only as parameters to
 10643                           ** aggregate functions */
 10644   struct AggInfo_func {   /* For each aggregate function */
 10645     Expr *pExpr;             /* Expression encoding the function */
 10646     FuncDef *pFunc;          /* The aggregate function implementation */
 10647     int iMem;                /* Memory location that acts as accumulator */
 10648     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
 10649   } *aFunc;
 10650   int nFunc;              /* Number of entries in aFunc[] */
 10651 };
 10653 /*
 10654 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
 10655 ** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
 10656 ** than 32767 we have to make it 32-bit.  16-bit is preferred because
 10657 ** it uses less memory in the Expr object, which is a big memory user
 10658 ** in systems with lots of prepared statements.  And few applications
 10659 ** need more than about 10 or 20 variables.  But some extreme users want
 10660 ** to have prepared statements with over 32767 variables, and for them
 10661 ** the option is available (at compile-time).
 10662 */
 10663 #if SQLITE_MAX_VARIABLE_NUMBER<=32767
 10664 typedef i16 ynVar;
 10665 #else
 10666 typedef int ynVar;
 10667 #endif
 10669 /*
 10670 ** Each node of an expression in the parse tree is an instance
 10671 ** of this structure.
 10672 **
 10673 ** Expr.op is the opcode. The integer parser token codes are reused
 10674 ** as opcodes here. For example, the parser defines TK_GE to be an integer
 10675 ** code representing the ">=" operator. This same integer code is reused
 10676 ** to represent the greater-than-or-equal-to operator in the expression
 10677 ** tree.
 10678 **
 10679 ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
 10680 ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
 10681 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
 10682 ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
 10683 ** then Expr.token contains the name of the function.
 10684 **
 10685 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
 10686 ** binary operator. Either or both may be NULL.
 10687 **
 10688 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
 10689 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
 10690 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
 10691 ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
 10692 ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
 10693 ** valid.
 10694 **
 10695 ** An expression of the form ID or ID.ID refers to a column in a table.
 10696 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
 10697 ** the integer cursor number of a VDBE cursor pointing to that table and
 10698 ** Expr.iColumn is the column number for the specific column.  If the
 10699 ** expression is used as a result in an aggregate SELECT, then the
 10700 ** value is also stored in the Expr.iAgg column in the aggregate so that
 10701 ** it can be accessed after all aggregates are computed.
 10702 **
 10703 ** If the expression is an unbound variable marker (a question mark 
 10704 ** character '?' in the original SQL) then the Expr.iTable holds the index 
 10705 ** number for that variable.
 10706 **
 10707 ** If the expression is a subquery then Expr.iColumn holds an integer
 10708 ** register number containing the result of the subquery.  If the
 10709 ** subquery gives a constant result, then iTable is -1.  If the subquery
 10710 ** gives a different answer at different times during statement processing
 10711 ** then iTable is the address of a subroutine that computes the subquery.
 10712 **
 10713 ** If the Expr is of type OP_Column, and the table it is selecting from
 10714 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
 10715 ** corresponding table definition.
 10716 **
 10717 ** ALLOCATION NOTES:
 10718 **
 10719 ** Expr objects can use a lot of memory space in database schema.  To
 10720 ** help reduce memory requirements, sometimes an Expr object will be
 10721 ** truncated.  And to reduce the number of memory allocations, sometimes
 10722 ** two or more Expr objects will be stored in a single memory allocation,
 10723 ** together with Expr.zToken strings.
 10724 **
 10725 ** If the EP_Reduced and EP_TokenOnly flags are set when
 10726 ** an Expr object is truncated.  When EP_Reduced is set, then all
 10727 ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
 10728 ** are contained within the same memory allocation.  Note, however, that
 10729 ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
 10730 ** allocated, regardless of whether or not EP_Reduced is set.
 10731 */
 10732 struct Expr {
 10733   u8 op;                 /* Operation performed by this node */
 10734   char affinity;         /* The affinity of the column or 0 if not a column */
 10735   u16 flags;             /* Various flags.  EP_* See below */
 10736   union {
 10737     char *zToken;          /* Token value. Zero terminated and dequoted */
 10738     int iValue;            /* Non-negative integer value if EP_IntValue */
 10739   } u;
 10741   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
 10742   ** space is allocated for the fields below this point. An attempt to
 10743   ** access them will result in a segfault or malfunction. 
 10744   *********************************************************************/
 10746   Expr *pLeft;           /* Left subnode */
 10747   Expr *pRight;          /* Right subnode */
 10748   union {
 10749     ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
 10750     Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
 10751   } x;
 10753   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
 10754   ** space is allocated for the fields below this point. An attempt to
 10755   ** access them will result in a segfault or malfunction.
 10756   *********************************************************************/
 10758 #if SQLITE_MAX_EXPR_DEPTH>0
 10759   int nHeight;           /* Height of the tree headed by this node */
 10760 #endif
 10761   int iTable;            /* TK_COLUMN: cursor number of table holding column
 10762                          ** TK_REGISTER: register number
 10763                          ** TK_TRIGGER: 1 -> new, 0 -> old */
 10764   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
 10765                          ** TK_VARIABLE: variable number (always >= 1). */
 10766   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 10767   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 10768   u8 flags2;             /* Second set of flags.  EP2_... */
 10769   u8 op2;                /* TK_REGISTER: original value of Expr.op
 10770                          ** TK_COLUMN: the value of p5 for OP_Column
 10771                          ** TK_AGG_FUNCTION: nesting depth */
 10772   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 10773   Table *pTab;           /* Table for TK_COLUMN expressions. */
 10774 };
 10776 /*
 10777 ** The following are the meanings of bits in the Expr.flags field.
 10778 */
 10779 #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
 10780 #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
 10781 #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
 10782 #define EP_Error      0x0008  /* Expression contains one or more errors */
 10783 #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
 10784 #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
 10785 #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
 10786 #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
 10787 #define EP_Collate    0x0100  /* Tree contains a TK_COLLATE opeartor */
 10788 #define EP_FixedDest  0x0200  /* Result needed in a specific register */
 10789 #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
 10790 #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
 10791 #define EP_Hint       0x1000  /* Not used */
 10792 #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
 10793 #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
 10794 #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
 10796 /*
 10797 ** The following are the meanings of bits in the Expr.flags2 field.
 10798 */
 10799 #define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
 10800 #define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
 10802 /*
 10803 ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
 10804 ** flag on an expression structure.  This flag is used for VV&A only.  The
 10805 ** routine is implemented as a macro that only works when in debugging mode,
 10806 ** so as not to burden production code.
 10807 */
 10808 #ifdef SQLITE_DEBUG
 10809 # define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
 10810 #else
 10811 # define ExprSetIrreducible(X)
 10812 #endif
 10814 /*
 10815 ** These macros can be used to test, set, or clear bits in the 
 10816 ** Expr.flags field.
 10817 */
 10818 #define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
 10819 #define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
 10820 #define ExprSetProperty(E,P)     (E)->flags|=(P)
 10821 #define ExprClearProperty(E,P)   (E)->flags&=~(P)
 10823 /*
 10824 ** Macros to determine the number of bytes required by a normal Expr 
 10825 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
 10826 ** and an Expr struct with the EP_TokenOnly flag set.
 10827 */
 10828 #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
 10829 #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
 10830 #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
 10832 /*
 10833 ** Flags passed to the sqlite3ExprDup() function. See the header comment 
 10834 ** above sqlite3ExprDup() for details.
 10835 */
 10836 #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
 10838 /*
 10839 ** A list of expressions.  Each expression may optionally have a
 10840 ** name.  An expr/name combination can be used in several ways, such
 10841 ** as the list of "expr AS ID" fields following a "SELECT" or in the
 10842 ** list of "ID = expr" items in an UPDATE.  A list of expressions can
 10843 ** also be used as the argument to a function, in which case the a.zName
 10844 ** field is not used.
 10845 */
 10846 struct ExprList {
 10847   int nExpr;             /* Number of expressions on the list */
 10848   int iECursor;          /* VDBE Cursor associated with this ExprList */
 10849   struct ExprList_item { /* For each expression in the list */
 10850     Expr *pExpr;           /* The list of expressions */
 10851     char *zName;           /* Token associated with this expression */
 10852     char *zSpan;           /* Original text of the expression */
 10853     u8 sortOrder;          /* 1 for DESC or 0 for ASC */
 10854     u8 done;               /* A flag to indicate when processing is finished */
 10855     u16 iOrderByCol;       /* For ORDER BY, column number in result set */
 10856     u16 iAlias;            /* Index into Parse.aAlias[] for zName */
 10857   } *a;                  /* Alloc a power of two greater or equal to nExpr */
 10858 };
 10860 /*
 10861 ** An instance of this structure is used by the parser to record both
 10862 ** the parse tree for an expression and the span of input text for an
 10863 ** expression.
 10864 */
 10865 struct ExprSpan {
 10866   Expr *pExpr;          /* The expression parse tree */
 10867   const char *zStart;   /* First character of input text */
 10868   const char *zEnd;     /* One character past the end of input text */
 10869 };
 10871 /*
 10872 ** An instance of this structure can hold a simple list of identifiers,
 10873 ** such as the list "a,b,c" in the following statements:
 10874 **
 10875 **      INSERT INTO t(a,b,c) VALUES ...;
 10876 **      CREATE INDEX idx ON t(a,b,c);
 10877 **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
 10878 **
 10879 ** The IdList.a.idx field is used when the IdList represents the list of
 10880 ** column names after a table name in an INSERT statement.  In the statement
 10881 **
 10882 **     INSERT INTO t(a,b,c) ...
 10883 **
 10884 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
 10885 */
 10886 struct IdList {
 10887   struct IdList_item {
 10888     char *zName;      /* Name of the identifier */
 10889     int idx;          /* Index in some Table.aCol[] of a column named zName */
 10890   } *a;
 10891   int nId;         /* Number of identifiers on the list */
 10892 };
 10894 /*
 10895 ** The bitmask datatype defined below is used for various optimizations.
 10896 **
 10897 ** Changing this from a 64-bit to a 32-bit type limits the number of
 10898 ** tables in a join to 32 instead of 64.  But it also reduces the size
 10899 ** of the library by 738 bytes on ix86.
 10900 */
 10901 typedef u64 Bitmask;
 10903 /*
 10904 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
 10905 */
 10906 #define BMS  ((int)(sizeof(Bitmask)*8))
 10908 /*
 10909 ** The following structure describes the FROM clause of a SELECT statement.
 10910 ** Each table or subquery in the FROM clause is a separate element of
 10911 ** the SrcList.a[] array.
 10912 **
 10913 ** With the addition of multiple database support, the following structure
 10914 ** can also be used to describe a particular table such as the table that
 10915 ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
 10916 ** such a table must be a simple name: ID.  But in SQLite, the table can
 10917 ** now be identified by a database name, a dot, then the table name: ID.ID.
 10918 **
 10919 ** The jointype starts out showing the join type between the current table
 10920 ** and the next table on the list.  The parser builds the list this way.
 10921 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
 10922 ** jointype expresses the join between the table and the previous table.
 10923 **
 10924 ** In the colUsed field, the high-order bit (bit 63) is set if the table
 10925 ** contains more than 63 columns and the 64-th or later column is used.
 10926 */
 10927 struct SrcList {
 10928   i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
 10929   i16 nAlloc;      /* Number of entries allocated in a[] below */
 10930   struct SrcList_item {
 10931     Schema *pSchema;  /* Schema to which this item is fixed */
 10932     char *zDatabase;  /* Name of database holding this table */
 10933     char *zName;      /* Name of the table */
 10934     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
 10935     Table *pTab;      /* An SQL table corresponding to zName */
 10936     Select *pSelect;  /* A SELECT statement used in place of a table name */
 10937     int addrFillSub;  /* Address of subroutine to manifest a subquery */
 10938     int regReturn;    /* Register holding return address of addrFillSub */
 10939     u8 jointype;      /* Type of join between this able and the previous */
 10940     unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
 10941     unsigned isCorrelated :1;  /* True if sub-query is correlated */
 10942     unsigned viaCoroutine :1;  /* Implemented as a co-routine */
 10943 #ifndef SQLITE_OMIT_EXPLAIN
 10944     u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
 10945 #endif
 10946     int iCursor;      /* The VDBE cursor number used to access this table */
 10947     Expr *pOn;        /* The ON clause of a join */
 10948     IdList *pUsing;   /* The USING clause of a join */
 10949     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
 10950     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
 10951     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
 10952   } a[1];             /* One entry for each identifier on the list */
 10953 };
 10955 /*
 10956 ** Permitted values of the SrcList.a.jointype field
 10957 */
 10958 #define JT_INNER     0x0001    /* Any kind of inner or cross join */
 10959 #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
 10960 #define JT_NATURAL   0x0004    /* True for a "natural" join */
 10961 #define JT_LEFT      0x0008    /* Left outer join */
 10962 #define JT_RIGHT     0x0010    /* Right outer join */
 10963 #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
 10964 #define JT_ERROR     0x0040    /* unknown or unsupported join type */
 10967 /*
 10968 ** A WherePlan object holds information that describes a lookup
 10969 ** strategy.
 10970 **
 10971 ** This object is intended to be opaque outside of the where.c module.
 10972 ** It is included here only so that that compiler will know how big it
 10973 ** is.  None of the fields in this object should be used outside of
 10974 ** the where.c module.
 10975 **
 10976 ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
 10977 ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
 10978 ** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
 10979 ** case that more than one of these conditions is true.
 10980 */
 10981 struct WherePlan {
 10982   u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
 10983   u16 nEq;                       /* Number of == constraints */
 10984   u16 nOBSat;                    /* Number of ORDER BY terms satisfied */
 10985   double nRow;                   /* Estimated number of rows (for EQP) */
 10986   union {
 10987     Index *pIdx;                   /* Index when WHERE_INDEXED is true */
 10988     struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
 10989     sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
 10990   } u;
 10991 };
 10993 /*
 10994 ** For each nested loop in a WHERE clause implementation, the WhereInfo
 10995 ** structure contains a single instance of this structure.  This structure
 10996 ** is intended to be private to the where.c module and should not be
 10997 ** access or modified by other modules.
 10998 **
 10999 ** The pIdxInfo field is used to help pick the best index on a
 11000 ** virtual table.  The pIdxInfo pointer contains indexing
 11001 ** information for the i-th table in the FROM clause before reordering.
 11002 ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
 11003 ** All other information in the i-th WhereLevel object for the i-th table
 11004 ** after FROM clause ordering.
 11005 */
 11006 struct WhereLevel {
 11007   WherePlan plan;       /* query plan for this element of the FROM clause */
 11008   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
 11009   int iTabCur;          /* The VDBE cursor used to access the table */
 11010   int iIdxCur;          /* The VDBE cursor used to access pIdx */
 11011   int addrBrk;          /* Jump here to break out of the loop */
 11012   int addrNxt;          /* Jump here to start the next IN combination */
 11013   int addrCont;         /* Jump here to continue with the next loop cycle */
 11014   int addrFirst;        /* First instruction of interior of the loop */
 11015   u8 iFrom;             /* Which entry in the FROM clause */
 11016   u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
 11017   int p1, p2;           /* Operands of the opcode used to ends the loop */
 11018   union {               /* Information that depends on plan.wsFlags */
 11019     struct {
 11020       int nIn;              /* Number of entries in aInLoop[] */
 11021       struct InLoop {
 11022         int iCur;              /* The VDBE cursor used by this IN operator */
 11023         int addrInTop;         /* Top of the IN loop */
 11024       } *aInLoop;           /* Information about each nested IN operator */
 11025     } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
 11026     Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
 11027   } u;
 11028   double rOptCost;      /* "Optimal" cost for this level */
 11030   /* The following field is really not part of the current level.  But
 11031   ** we need a place to cache virtual table index information for each
 11032   ** virtual table in the FROM clause and the WhereLevel structure is
 11033   ** a convenient place since there is one WhereLevel for each FROM clause
 11034   ** element.
 11035   */
 11036   sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
 11037 };
 11039 /*
 11040 ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
 11041 ** and the WhereInfo.wctrlFlags member.
 11042 */
 11043 #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
 11044 #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
 11045 #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
 11046 #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
 11047 #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
 11048 #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
 11049 #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
 11050 #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
 11051 #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
 11053 /*
 11054 ** The WHERE clause processing routine has two halves.  The
 11055 ** first part does the start of the WHERE loop and the second
 11056 ** half does the tail of the WHERE loop.  An instance of
 11057 ** this structure is returned by the first half and passed
 11058 ** into the second half to give some continuity.
 11059 */
 11060 struct WhereInfo {
 11061   Parse *pParse;            /* Parsing and code generating context */
 11062   SrcList *pTabList;        /* List of tables in the join */
 11063   u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
 11064   u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
 11065   u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
 11066   u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
 11067   u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
 11068   int iTop;                 /* The very beginning of the WHERE loop */
 11069   int iContinue;            /* Jump here to continue with next record */
 11070   int iBreak;               /* Jump here to break out of the loop */
 11071   int nLevel;               /* Number of nested loop */
 11072   struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
 11073   double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
 11074   double nRowOut;           /* Estimated number of output rows */
 11075   WhereLevel a[1];          /* Information about each nest loop in WHERE */
 11076 };
 11078 /* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
 11079 #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
 11080 #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
 11081 #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
 11082 #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
 11084 /*
 11085 ** A NameContext defines a context in which to resolve table and column
 11086 ** names.  The context consists of a list of tables (the pSrcList) field and
 11087 ** a list of named expression (pEList).  The named expression list may
 11088 ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
 11089 ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
 11090 ** pEList corresponds to the result set of a SELECT and is NULL for
 11091 ** other statements.
 11092 **
 11093 ** NameContexts can be nested.  When resolving names, the inner-most 
 11094 ** context is searched first.  If no match is found, the next outer
 11095 ** context is checked.  If there is still no match, the next context
 11096 ** is checked.  This process continues until either a match is found
 11097 ** or all contexts are check.  When a match is found, the nRef member of
 11098 ** the context containing the match is incremented. 
 11099 **
 11100 ** Each subquery gets a new NameContext.  The pNext field points to the
 11101 ** NameContext in the parent query.  Thus the process of scanning the
 11102 ** NameContext list corresponds to searching through successively outer
 11103 ** subqueries looking for a match.
 11104 */
 11105 struct NameContext {
 11106   Parse *pParse;       /* The parser */
 11107   SrcList *pSrcList;   /* One or more tables used to resolve names */
 11108   ExprList *pEList;    /* Optional list of named expressions */
 11109   AggInfo *pAggInfo;   /* Information about aggregates at this level */
 11110   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
 11111   int nRef;            /* Number of names resolved by this context */
 11112   int nErr;            /* Number of errors encountered while resolving names */
 11113   u8 ncFlags;          /* Zero or more NC_* flags defined below */
 11114 };
 11116 /*
 11117 ** Allowed values for the NameContext, ncFlags field.
 11118 */
 11119 #define NC_AllowAgg  0x01    /* Aggregate functions are allowed here */
 11120 #define NC_HasAgg    0x02    /* One or more aggregate functions seen */
 11121 #define NC_IsCheck   0x04    /* True if resolving names in a CHECK constraint */
 11122 #define NC_InAggFunc 0x08    /* True if analyzing arguments to an agg func */
 11124 /*
 11125 ** An instance of the following structure contains all information
 11126 ** needed to generate code for a single SELECT statement.
 11127 **
 11128 ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
 11129 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
 11130 ** limit and nOffset to the value of the offset (or 0 if there is not
 11131 ** offset).  But later on, nLimit and nOffset become the memory locations
 11132 ** in the VDBE that record the limit and offset counters.
 11133 **
 11134 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
 11135 ** These addresses must be stored so that we can go back and fill in
 11136 ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
 11137 ** the number of columns in P2 can be computed at the same time
 11138 ** as the OP_OpenEphm instruction is coded because not
 11139 ** enough information about the compound query is known at that point.
 11140 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
 11141 ** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
 11142 ** sequences for the ORDER BY clause.
 11143 */
 11144 struct Select {
 11145   ExprList *pEList;      /* The fields of the result */
 11146   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
 11147   u16 selFlags;          /* Various SF_* values */
 11148   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
 11149   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
 11150   double nSelectRow;     /* Estimated number of result rows */
 11151   SrcList *pSrc;         /* The FROM clause */
 11152   Expr *pWhere;          /* The WHERE clause */
 11153   ExprList *pGroupBy;    /* The GROUP BY clause */
 11154   Expr *pHaving;         /* The HAVING clause */
 11155   ExprList *pOrderBy;    /* The ORDER BY clause */
 11156   Select *pPrior;        /* Prior select in a compound select statement */
 11157   Select *pNext;         /* Next select to the left in a compound */
 11158   Select *pRightmost;    /* Right-most select in a compound select statement */
 11159   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
 11160   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
 11161 };
 11163 /*
 11164 ** Allowed values for Select.selFlags.  The "SF" prefix stands for
 11165 ** "Select Flag".
 11166 */
 11167 #define SF_Distinct        0x0001  /* Output should be DISTINCT */
 11168 #define SF_Resolved        0x0002  /* Identifiers have been resolved */
 11169 #define SF_Aggregate       0x0004  /* Contains aggregate functions */
 11170 #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 11171 #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 11172 #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
 11173 #define SF_UseSorter       0x0040  /* Sort using a sorter */
 11174 #define SF_Values          0x0080  /* Synthesized from VALUES clause */
 11175 #define SF_Materialize     0x0100  /* Force materialization of views */
 11178 /*
 11179 ** The results of a select can be distributed in several ways.  The
 11180 ** "SRT" prefix means "SELECT Result Type".
 11181 */
 11182 #define SRT_Union        1  /* Store result as keys in an index */
 11183 #define SRT_Except       2  /* Remove result from a UNION index */
 11184 #define SRT_Exists       3  /* Store 1 if the result is not empty */
 11185 #define SRT_Discard      4  /* Do not save the results anywhere */
 11187 /* The ORDER BY clause is ignored for all of the above */
 11188 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
 11190 #define SRT_Output       5  /* Output each row of result */
 11191 #define SRT_Mem          6  /* Store result in a memory cell */
 11192 #define SRT_Set          7  /* Store results as keys in an index */
 11193 #define SRT_Table        8  /* Store result as data with an automatic rowid */
 11194 #define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
 11195 #define SRT_Coroutine   10  /* Generate a single row of result */
 11197 /*
 11198 ** An instance of this object describes where to put of the results of
 11199 ** a SELECT statement.
 11200 */
 11201 struct SelectDest {
 11202   u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
 11203   char affSdst;     /* Affinity used when eDest==SRT_Set */
 11204   int iSDParm;      /* A parameter used by the eDest disposal method */
 11205   int iSdst;        /* Base register where results are written */
 11206   int nSdst;        /* Number of registers allocated */
 11207 };
 11209 /*
 11210 ** During code generation of statements that do inserts into AUTOINCREMENT 
 11211 ** tables, the following information is attached to the Table.u.autoInc.p
 11212 ** pointer of each autoincrement table to record some side information that
 11213 ** the code generator needs.  We have to keep per-table autoincrement
 11214 ** information in case inserts are down within triggers.  Triggers do not
 11215 ** normally coordinate their activities, but we do need to coordinate the
 11216 ** loading and saving of autoincrement information.
 11217 */
 11218 struct AutoincInfo {
 11219   AutoincInfo *pNext;   /* Next info block in a list of them all */
 11220   Table *pTab;          /* Table this info block refers to */
 11221   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
 11222   int regCtr;           /* Memory register holding the rowid counter */
 11223 };
 11225 /*
 11226 ** Size of the column cache
 11227 */
 11228 #ifndef SQLITE_N_COLCACHE
 11229 # define SQLITE_N_COLCACHE 10
 11230 #endif
 11232 /*
 11233 ** At least one instance of the following structure is created for each 
 11234 ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
 11235 ** statement. All such objects are stored in the linked list headed at
 11236 ** Parse.pTriggerPrg and deleted once statement compilation has been
 11237 ** completed.
 11238 **
 11239 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
 11240 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
 11241 ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
 11242 ** The Parse.pTriggerPrg list never contains two entries with the same
 11243 ** values for both pTrigger and orconf.
 11244 **
 11245 ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
 11246 ** accessed (or set to 0 for triggers fired as a result of INSERT 
 11247 ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
 11248 ** a mask of new.* columns used by the program.
 11249 */
 11250 struct TriggerPrg {
 11251   Trigger *pTrigger;      /* Trigger this program was coded from */
 11252   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
 11253   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
 11254   int orconf;             /* Default ON CONFLICT policy */
 11255   u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
 11256 };
 11258 /*
 11259 ** The yDbMask datatype for the bitmask of all attached databases.
 11260 */
 11261 #if SQLITE_MAX_ATTACHED>30
 11262   typedef sqlite3_uint64 yDbMask;
 11263 #else
 11264   typedef unsigned int yDbMask;
 11265 #endif
 11267 /*
 11268 ** An SQL parser context.  A copy of this structure is passed through
 11269 ** the parser and down into all the parser action routine in order to
 11270 ** carry around information that is global to the entire parse.
 11271 **
 11272 ** The structure is divided into two parts.  When the parser and code
 11273 ** generate call themselves recursively, the first part of the structure
 11274 ** is constant but the second part is reset at the beginning and end of
 11275 ** each recursion.
 11276 **
 11277 ** The nTableLock and aTableLock variables are only used if the shared-cache 
 11278 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
 11279 ** used to store the set of table-locks required by the statement being
 11280 ** compiled. Function sqlite3TableLock() is used to add entries to the
 11281 ** list.
 11282 */
 11283 struct Parse {
 11284   sqlite3 *db;         /* The main database structure */
 11285   char *zErrMsg;       /* An error message */
 11286   Vdbe *pVdbe;         /* An engine for executing database bytecode */
 11287   int rc;              /* Return code from execution */
 11288   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
 11289   u8 checkSchema;      /* Causes schema cookie check after an error */
 11290   u8 nested;           /* Number of nested calls to the parser/code generator */
 11291   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
 11292   u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
 11293   u8 nColCache;        /* Number of entries in aColCache[] */
 11294   u8 iColCache;        /* Next entry in aColCache[] to replace */
 11295   u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
 11296   u8 mayAbort;         /* True if statement may throw an ABORT exception */
 11297   int aTempReg[8];     /* Holding area for temporary registers */
 11298   int nRangeReg;       /* Size of the temporary register block */
 11299   int iRangeReg;       /* First register in temporary register block */
 11300   int nErr;            /* Number of errors seen */
 11301   int nTab;            /* Number of previously allocated VDBE cursors */
 11302   int nMem;            /* Number of memory cells used so far */
 11303   int nSet;            /* Number of sets used so far */
 11304   int nOnce;           /* Number of OP_Once instructions so far */
 11305   int ckBase;          /* Base register of data during check constraints */
 11306   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
 11307   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
 11308   struct yColCache {
 11309     int iTable;           /* Table cursor number */
 11310     int iColumn;          /* Table column number */
 11311     u8 tempReg;           /* iReg is a temp register that needs to be freed */
 11312     int iLevel;           /* Nesting level */
 11313     int iReg;             /* Reg with value of this column. 0 means none. */
 11314     int lru;              /* Least recently used entry has the smallest value */
 11315   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
 11316   yDbMask writeMask;   /* Start a write transaction on these databases */
 11317   yDbMask cookieMask;  /* Bitmask of schema verified databases */
 11318   int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
 11319   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
 11320   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 11321   int regRoot;         /* Register holding root page number for new objects */
 11322   int nMaxArg;         /* Max args passed to user function by sub-program */
 11323   Token constraintName;/* Name of the constraint currently being parsed */
 11324 #ifndef SQLITE_OMIT_SHARED_CACHE
 11325   int nTableLock;        /* Number of locks in aTableLock */
 11326   TableLock *aTableLock; /* Required table locks for shared-cache mode */
 11327 #endif
 11328   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 11330   /* Information used while coding trigger programs. */
 11331   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 11332   Table *pTriggerTab;  /* Table triggers are being coded for */
 11333   double nQueryLoop;   /* Estimated number of iterations of a query */
 11334   u32 oldmask;         /* Mask of old.* columns referenced */
 11335   u32 newmask;         /* Mask of new.* columns referenced */
 11336   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 11337   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 11338   u8 disableTriggers;  /* True to disable triggers */
 11340   /* Above is constant between recursions.  Below is reset before and after
 11341   ** each recursion */
 11343   int nVar;                 /* Number of '?' variables seen in the SQL so far */
 11344   int nzVar;                /* Number of available slots in azVar[] */
 11345   u8 explain;               /* True if the EXPLAIN flag is found on the query */
 11346 #ifndef SQLITE_OMIT_VIRTUALTABLE
 11347   u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
 11348   int nVtabLock;            /* Number of virtual tables to lock */
 11349 #endif
 11350   int nAlias;               /* Number of aliased result set columns */
 11351   int nHeight;              /* Expression tree height of current sub-select */
 11352 #ifndef SQLITE_OMIT_EXPLAIN
 11353   int iSelectId;            /* ID of current select for EXPLAIN output */
 11354   int iNextSelectId;        /* Next available select ID for EXPLAIN output */
 11355 #endif
 11356   char **azVar;             /* Pointers to names of parameters */
 11357   Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
 11358   int *aAlias;              /* Register used to hold aliased result */
 11359   const char *zTail;        /* All SQL text past the last semicolon parsed */
 11360   Table *pNewTable;         /* A table being constructed by CREATE TABLE */
 11361   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
 11362   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
 11363   Token sNameToken;         /* Token with unqualified schema object name */
 11364   Token sLastToken;         /* The last token parsed */
 11365 #ifndef SQLITE_OMIT_VIRTUALTABLE
 11366   Token sArg;               /* Complete text of a module argument */
 11367   Table **apVtabLock;       /* Pointer to virtual tables needing locking */
 11368 #endif
 11369   Table *pZombieTab;        /* List of Table objects to delete after code gen */
 11370   TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
 11371 };
 11373 /*
 11374 ** Return true if currently inside an sqlite3_declare_vtab() call.
 11375 */
 11376 #ifdef SQLITE_OMIT_VIRTUALTABLE
 11377   #define IN_DECLARE_VTAB 0
 11378 #else
 11379   #define IN_DECLARE_VTAB (pParse->declareVtab)
 11380 #endif
 11382 /*
 11383 ** An instance of the following structure can be declared on a stack and used
 11384 ** to save the Parse.zAuthContext value so that it can be restored later.
 11385 */
 11386 struct AuthContext {
 11387   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
 11388   Parse *pParse;              /* The Parse structure */
 11389 };
 11391 /*
 11392 ** Bitfield flags for P5 value in various opcodes.
 11393 */
 11394 #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
 11395 #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
 11396 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 11397 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 11398 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 11399 #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
 11400 #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 11401 #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 11402 #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 11403 #define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
 11404 #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
 11406 /*
 11407  * Each trigger present in the database schema is stored as an instance of
 11408  * struct Trigger. 
 11410  * Pointers to instances of struct Trigger are stored in two ways.
 11411  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
 11412  *    database). This allows Trigger structures to be retrieved by name.
 11413  * 2. All triggers associated with a single table form a linked list, using the
 11414  *    pNext member of struct Trigger. A pointer to the first element of the
 11415  *    linked list is stored as the "pTrigger" member of the associated
 11416  *    struct Table.
 11418  * The "step_list" member points to the first element of a linked list
 11419  * containing the SQL statements specified as the trigger program.
 11420  */
 11421 struct Trigger {
 11422   char *zName;            /* The name of the trigger                        */
 11423   char *table;            /* The table or view to which the trigger applies */
 11424   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
 11425   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
 11426   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
 11427   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
 11428                              the <column-list> is stored here */
 11429   Schema *pSchema;        /* Schema containing the trigger */
 11430   Schema *pTabSchema;     /* Schema containing the table */
 11431   TriggerStep *step_list; /* Link list of trigger program steps             */
 11432   Trigger *pNext;         /* Next trigger associated with the table */
 11433 };
 11435 /*
 11436 ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
 11437 ** determine which. 
 11438 **
 11439 ** If there are multiple triggers, you might of some BEFORE and some AFTER.
 11440 ** In that cases, the constants below can be ORed together.
 11441 */
 11442 #define TRIGGER_BEFORE  1
 11443 #define TRIGGER_AFTER   2
 11445 /*
 11446  * An instance of struct TriggerStep is used to store a single SQL statement
 11447  * that is a part of a trigger-program. 
 11449  * Instances of struct TriggerStep are stored in a singly linked list (linked
 11450  * using the "pNext" member) referenced by the "step_list" member of the 
 11451  * associated struct Trigger instance. The first element of the linked list is
 11452  * the first step of the trigger-program.
 11454  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
 11455  * "SELECT" statement. The meanings of the other members is determined by the 
 11456  * value of "op" as follows:
 11458  * (op == TK_INSERT)
 11459  * orconf    -> stores the ON CONFLICT algorithm
 11460  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
 11461  *              this stores a pointer to the SELECT statement. Otherwise NULL.
 11462  * target    -> A token holding the quoted name of the table to insert into.
 11463  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
 11464  *              this stores values to be inserted. Otherwise NULL.
 11465  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
 11466  *              statement, then this stores the column-names to be
 11467  *              inserted into.
 11469  * (op == TK_DELETE)
 11470  * target    -> A token holding the quoted name of the table to delete from.
 11471  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
 11472  *              Otherwise NULL.
 11474  * (op == TK_UPDATE)
 11475  * target    -> A token holding the quoted name of the table to update rows of.
 11476  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
 11477  *              Otherwise NULL.
 11478  * pExprList -> A list of the columns to update and the expressions to update
 11479  *              them to. See sqlite3Update() documentation of "pChanges"
 11480  *              argument.
 11482  */
 11483 struct TriggerStep {
 11484   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
 11485   u8 orconf;           /* OE_Rollback etc. */
 11486   Trigger *pTrig;      /* The trigger that this step is a part of */
 11487   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
 11488   Token target;        /* Target table for DELETE, UPDATE, INSERT */
 11489   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
 11490   ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
 11491   IdList *pIdList;     /* Column names for INSERT */
 11492   TriggerStep *pNext;  /* Next in the link-list */
 11493   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 11494 };
 11496 /*
 11497 ** The following structure contains information used by the sqliteFix...
 11498 ** routines as they walk the parse tree to make database references
 11499 ** explicit.  
 11500 */
 11501 typedef struct DbFixer DbFixer;
 11502 struct DbFixer {
 11503   Parse *pParse;      /* The parsing context.  Error messages written here */
 11504   Schema *pSchema;    /* Fix items to this schema */
 11505   const char *zDb;    /* Make sure all objects are contained in this database */
 11506   const char *zType;  /* Type of the container - used for error messages */
 11507   const Token *pName; /* Name of the container - used for error messages */
 11508 };
 11510 /*
 11511 ** An objected used to accumulate the text of a string where we
 11512 ** do not necessarily know how big the string will be in the end.
 11513 */
 11514 struct StrAccum {
 11515   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
 11516   char *zBase;         /* A base allocation.  Not from malloc. */
 11517   char *zText;         /* The string collected so far */
 11518   int  nChar;          /* Length of the string so far */
 11519   int  nAlloc;         /* Amount of space allocated in zText */
 11520   int  mxAlloc;        /* Maximum allowed string length */
 11521   u8   mallocFailed;   /* Becomes true if any memory allocation fails */
 11522   u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
 11523   u8   tooBig;         /* Becomes true if string size exceeds limits */
 11524 };
 11526 /*
 11527 ** A pointer to this structure is used to communicate information
 11528 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
 11529 */
 11530 typedef struct {
 11531   sqlite3 *db;        /* The database being initialized */
 11532   char **pzErrMsg;    /* Error message stored here */
 11533   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
 11534   int rc;             /* Result code stored here */
 11535 } InitData;
 11537 /*
 11538 ** Structure containing global configuration data for the SQLite library.
 11539 **
 11540 ** This structure also contains some state information.
 11541 */
 11542 struct Sqlite3Config {
 11543   int bMemstat;                     /* True to enable memory status */
 11544   int bCoreMutex;                   /* True to enable core mutexing */
 11545   int bFullMutex;                   /* True to enable full mutexing */
 11546   int bOpenUri;                     /* True to interpret filenames as URIs */
 11547   int bUseCis;                      /* Use covering indices for full-scans */
 11548   int mxStrlen;                     /* Maximum string length */
 11549   int szLookaside;                  /* Default lookaside buffer size */
 11550   int nLookaside;                   /* Default lookaside buffer count */
 11551   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
 11552   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
 11553   sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
 11554   void *pHeap;                      /* Heap storage space */
 11555   int nHeap;                        /* Size of pHeap[] */
 11556   int mnReq, mxReq;                 /* Min and max heap requests sizes */
 11557   void *pScratch;                   /* Scratch memory */
 11558   int szScratch;                    /* Size of each scratch buffer */
 11559   int nScratch;                     /* Number of scratch buffers */
 11560   void *pPage;                      /* Page cache memory */
 11561   int szPage;                       /* Size of each page in pPage[] */
 11562   int nPage;                        /* Number of pages in pPage[] */
 11563   int mxParserStack;                /* maximum depth of the parser stack */
 11564   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
 11565   /* The above might be initialized to non-zero.  The following need to always
 11566   ** initially be zero, however. */
 11567   int isInit;                       /* True after initialization has finished */
 11568   int inProgress;                   /* True while initialization in progress */
 11569   int isMutexInit;                  /* True after mutexes are initialized */
 11570   int isMallocInit;                 /* True after malloc is initialized */
 11571   int isPCacheInit;                 /* True after malloc is initialized */
 11572   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
 11573   int nRefInitMutex;                /* Number of users of pInitMutex */
 11574   void (*xLog)(void*,int,const char*); /* Function for logging */
 11575   void *pLogArg;                       /* First argument to xLog() */
 11576   int bLocaltimeFault;              /* True to fail localtime() calls */
 11577 #ifdef SQLITE_ENABLE_SQLLOG
 11578   void(*xSqllog)(void*,sqlite3*,const char*, int);
 11579   void *pSqllogArg;
 11580 #endif
 11581 };
 11583 /*
 11584 ** Context pointer passed down through the tree-walk.
 11585 */
 11586 struct Walker {
 11587   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
 11588   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
 11589   Parse *pParse;                            /* Parser context.  */
 11590   int walkerDepth;                          /* Number of subqueries */
 11591   union {                                   /* Extra data for callback */
 11592     NameContext *pNC;                          /* Naming context */
 11593     int i;                                     /* Integer value */
 11594     SrcList *pSrcList;                         /* FROM clause */
 11595     struct SrcCount *pSrcCount;                /* Counting column references */
 11596   } u;
 11597 };
 11599 /* Forward declarations */
 11600 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 11601 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 11602 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
 11603 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
 11604 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
 11606 /*
 11607 ** Return code from the parse-tree walking primitives and their
 11608 ** callbacks.
 11609 */
 11610 #define WRC_Continue    0   /* Continue down into children */
 11611 #define WRC_Prune       1   /* Omit children but continue walking siblings */
 11612 #define WRC_Abort       2   /* Abandon the tree walk */
 11614 /*
 11615 ** Assuming zIn points to the first byte of a UTF-8 character,
 11616 ** advance zIn to point to the first byte of the next UTF-8 character.
 11617 */
 11618 #define SQLITE_SKIP_UTF8(zIn) {                        \
 11619   if( (*(zIn++))>=0xc0 ){                              \
 11620     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
 11621   }                                                    \
 11624 /*
 11625 ** The SQLITE_*_BKPT macros are substitutes for the error codes with
 11626 ** the same name but without the _BKPT suffix.  These macros invoke
 11627 ** routines that report the line-number on which the error originated
 11628 ** using sqlite3_log().  The routines also provide a convenient place
 11629 ** to set a debugger breakpoint.
 11630 */
 11631 SQLITE_PRIVATE int sqlite3CorruptError(int);
 11632 SQLITE_PRIVATE int sqlite3MisuseError(int);
 11633 SQLITE_PRIVATE int sqlite3CantopenError(int);
 11634 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
 11635 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
 11636 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
 11639 /*
 11640 ** FTS4 is really an extension for FTS3.  It is enabled using the
 11641 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
 11642 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
 11643 */
 11644 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
 11645 # define SQLITE_ENABLE_FTS3
 11646 #endif
 11648 /*
 11649 ** The ctype.h header is needed for non-ASCII systems.  It is also
 11650 ** needed by FTS3 when FTS3 is included in the amalgamation.
 11651 */
 11652 #if !defined(SQLITE_ASCII) || \
 11653     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
 11654 # include <ctype.h>
 11655 #endif
 11657 /*
 11658 ** The following macros mimic the standard library functions toupper(),
 11659 ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
 11660 ** sqlite versions only work for ASCII characters, regardless of locale.
 11661 */
 11662 #ifdef SQLITE_ASCII
 11663 # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
 11664 # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
 11665 # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
 11666 # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
 11667 # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
 11668 # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
 11669 # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
 11670 #else
 11671 # define sqlite3Toupper(x)   toupper((unsigned char)(x))
 11672 # define sqlite3Isspace(x)   isspace((unsigned char)(x))
 11673 # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
 11674 # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
 11675 # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
 11676 # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
 11677 # define sqlite3Tolower(x)   tolower((unsigned char)(x))
 11678 #endif
 11680 /*
 11681 ** Internal function prototypes
 11682 */
 11683 #define sqlite3StrICmp sqlite3_stricmp
 11684 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
 11685 #define sqlite3StrNICmp sqlite3_strnicmp
 11687 SQLITE_PRIVATE int sqlite3MallocInit(void);
 11688 SQLITE_PRIVATE void sqlite3MallocEnd(void);
 11689 SQLITE_PRIVATE void *sqlite3Malloc(int);
 11690 SQLITE_PRIVATE void *sqlite3MallocZero(int);
 11691 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
 11692 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
 11693 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
 11694 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
 11695 SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
 11696 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
 11697 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
 11698 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
 11699 SQLITE_PRIVATE int sqlite3MallocSize(void*);
 11700 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
 11701 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
 11702 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
 11703 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
 11704 SQLITE_PRIVATE void sqlite3PageFree(void*);
 11705 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
 11706 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 11707 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
 11709 /*
 11710 ** On systems with ample stack space and that support alloca(), make
 11711 ** use of alloca() to obtain space for large automatic objects.  By default,
 11712 ** obtain space from malloc().
 11713 **
 11714 ** The alloca() routine never returns NULL.  This will cause code paths
 11715 ** that deal with sqlite3StackAlloc() failures to be unreachable.
 11716 */
 11717 #ifdef SQLITE_USE_ALLOCA
 11718 # define sqlite3StackAllocRaw(D,N)   alloca(N)
 11719 # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
 11720 # define sqlite3StackFree(D,P)       
 11721 #else
 11722 # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
 11723 # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
 11724 # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
 11725 #endif
 11727 #ifdef SQLITE_ENABLE_MEMSYS3
 11728 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
 11729 #endif
 11730 #ifdef SQLITE_ENABLE_MEMSYS5
 11731 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
 11732 #endif
 11735 #ifndef SQLITE_MUTEX_OMIT
 11736 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
 11737 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
 11738 SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
 11739 SQLITE_PRIVATE   int sqlite3MutexInit(void);
 11740 SQLITE_PRIVATE   int sqlite3MutexEnd(void);
 11741 #endif
 11743 SQLITE_PRIVATE int sqlite3StatusValue(int);
 11744 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
 11745 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
 11747 #ifndef SQLITE_OMIT_FLOATING_POINT
 11748 SQLITE_PRIVATE   int sqlite3IsNaN(double);
 11749 #else
 11750 # define sqlite3IsNaN(X)  0
 11751 #endif
 11753 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
 11754 #ifndef SQLITE_OMIT_TRACE
 11755 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
 11756 #endif
 11757 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
 11758 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 11759 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
 11760 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
 11761 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 11762 #endif
 11763 #if defined(SQLITE_TEST)
 11764 SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 11765 #endif
 11767 /* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
 11768 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 11769 SQLITE_PRIVATE   void sqlite3ExplainBegin(Vdbe*);
 11770 SQLITE_PRIVATE   void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
 11771 SQLITE_PRIVATE   void sqlite3ExplainNL(Vdbe*);
 11772 SQLITE_PRIVATE   void sqlite3ExplainPush(Vdbe*);
 11773 SQLITE_PRIVATE   void sqlite3ExplainPop(Vdbe*);
 11774 SQLITE_PRIVATE   void sqlite3ExplainFinish(Vdbe*);
 11775 SQLITE_PRIVATE   void sqlite3ExplainSelect(Vdbe*, Select*);
 11776 SQLITE_PRIVATE   void sqlite3ExplainExpr(Vdbe*, Expr*);
 11777 SQLITE_PRIVATE   void sqlite3ExplainExprList(Vdbe*, ExprList*);
 11778 SQLITE_PRIVATE   const char *sqlite3VdbeExplanation(Vdbe*);
 11779 #else
 11780 # define sqlite3ExplainBegin(X)
 11781 # define sqlite3ExplainSelect(A,B)
 11782 # define sqlite3ExplainExpr(A,B)
 11783 # define sqlite3ExplainExprList(A,B)
 11784 # define sqlite3ExplainFinish(X)
 11785 # define sqlite3VdbeExplanation(X) 0
 11786 #endif
 11789 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
 11790 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
 11791 SQLITE_PRIVATE int sqlite3Dequote(char*);
 11792 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
 11793 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
 11794 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
 11795 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
 11796 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
 11797 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
 11798 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
 11799 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
 11800 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
 11801 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
 11802 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
 11803 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
 11804 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
 11805 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
 11806 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
 11807 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
 11808 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
 11809 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
 11810 SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
 11811 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
 11812 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
 11813 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
 11814 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
 11815 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
 11816 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
 11817 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
 11818 SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
 11819 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
 11820 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
 11821 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
 11822 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
 11823 SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
 11824 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
 11825 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
 11826 SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
 11827 SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
 11828 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
 11829 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
 11830 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
 11831 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
 11832                     sqlite3_vfs**,char**,char **);
 11833 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
 11834 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
 11836 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
 11837 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
 11838 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
 11839 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
 11840 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
 11841 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
 11842 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
 11844 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
 11845 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
 11846 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
 11847 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
 11848 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
 11850 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
 11852 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 11853 SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
 11854 #else
 11855 # define sqlite3ViewGetColumnNames(A,B) 0
 11856 #endif
 11858 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 11859 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
 11860 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
 11861 #ifndef SQLITE_OMIT_AUTOINCREMENT
 11862 SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
 11863 SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
 11864 #else
 11865 # define sqlite3AutoincrementBegin(X)
 11866 # define sqlite3AutoincrementEnd(X)
 11867 #endif
 11868 SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
 11869 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
 11870 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
 11871 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
 11872 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
 11873 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
 11874 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
 11875 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
 11876                                       Token*, Select*, Expr*, IdList*);
 11877 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
 11878 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
 11879 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
 11880 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
 11881 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
 11882 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 11883 SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 11884                         Token*, int, int);
 11885 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 11886 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 11887 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 11888                          Expr*,ExprList*,int,Expr*,Expr*);
 11889 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 11890 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 11891 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 11892 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 11893 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 11894 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
 11895 #endif
 11896 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 11897 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
 11898 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 11899 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 11900 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
 11901 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
 11902 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 11903 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
 11904 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
 11905 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
 11906 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
 11907 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
 11908 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
 11909 SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
 11910 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
 11911 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
 11912 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
 11913 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
 11914 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
 11915 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
 11916 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
 11917 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
 11918 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
 11919 SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
 11920 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
 11921 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 11922 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 11923 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
 11924 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
 11925 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 11926 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
 11927 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
 11928 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 11929 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 11930 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 11931 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 11932 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
 11933 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
 11934 SQLITE_PRIVATE void sqlite3PrngResetState(void);
 11935 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
 11936 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 11937 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 11938 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 11939 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
 11940 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
 11941 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 11942 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 11943 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
 11944 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 11945 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 11946 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
 11947 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 11948 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 11949 SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
 11950 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 11951 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
 11952 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
 11953 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
 11954 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
 11955 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
 11956                                      int*,int,int,int,int,int*);
 11957 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
 11958 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
 11959 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
 11960 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
 11961 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
 11962 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
 11963 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 11964 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 11965 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 11966 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 11967 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
 11968 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
 11969 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
 11970 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
 11971 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 11972 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
 11973 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 11974 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
 11975 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
 11977 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 11978 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
 11979 #endif
 11981 #ifndef SQLITE_OMIT_TRIGGER
 11982 SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
 11983                            Expr*,int, int);
 11984 SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
 11985 SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
 11986 SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
 11987 SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
 11988 SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
 11989 SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
 11990                             int, int, int);
 11991 SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
 11992   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 11993 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
 11994 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
 11995 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
 11996                                         ExprList*,Select*,u8);
 11997 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
 11998 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
 11999 SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 12000 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 12001 SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
 12002 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
 12003 #else
 12004 # define sqlite3TriggersExist(B,C,D,E,F) 0
 12005 # define sqlite3DeleteTrigger(A,B)
 12006 # define sqlite3DropTriggerPtr(A,B)
 12007 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
 12008 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
 12009 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
 12010 # define sqlite3TriggerList(X, Y) 0
 12011 # define sqlite3ParseToplevel(p) p
 12012 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
 12013 #endif
 12015 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
 12016 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
 12017 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
 12018 #ifndef SQLITE_OMIT_AUTHORIZATION
 12019 SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
 12020 SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
 12021 SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
 12022 SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
 12023 SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
 12024 #else
 12025 # define sqlite3AuthRead(a,b,c,d)
 12026 # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
 12027 # define sqlite3AuthContextPush(a,b,c)
 12028 # define sqlite3AuthContextPop(a)  ((void)(a))
 12029 #endif
 12030 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
 12031 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
 12032 SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
 12033 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
 12034 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
 12035 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
 12036 SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
 12037 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
 12038 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
 12039 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
 12040 SQLITE_PRIVATE int sqlite3Atoi(const char*);
 12041 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
 12042 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
 12043 SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
 12045 /*
 12046 ** Routines to read and write variable-length integers.  These used to
 12047 ** be defined locally, but now we use the varint routines in the util.c
 12048 ** file.  Code should use the MACRO forms below, as the Varint32 versions
 12049 ** are coded to assume the single byte case is already handled (which 
 12050 ** the MACRO form does).
 12051 */
 12052 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
 12053 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
 12054 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
 12055 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
 12056 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
 12058 /*
 12059 ** The header of a record consists of a sequence variable-length integers.
 12060 ** These integers are almost always small and are encoded as a single byte.
 12061 ** The following macros take advantage this fact to provide a fast encode
 12062 ** and decode of the integers in a record header.  It is faster for the common
 12063 ** case where the integer is a single byte.  It is a little slower when the
 12064 ** integer is two or more bytes.  But overall it is faster.
 12065 **
 12066 ** The following expressions are equivalent:
 12067 **
 12068 **     x = sqlite3GetVarint32( A, &B );
 12069 **     x = sqlite3PutVarint32( A, B );
 12070 **
 12071 **     x = getVarint32( A, B );
 12072 **     x = putVarint32( A, B );
 12073 **
 12074 */
 12075 #define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
 12076 #define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
 12077 #define getVarint    sqlite3GetVarint
 12078 #define putVarint    sqlite3PutVarint
 12081 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
 12082 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
 12083 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
 12084 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
 12085 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
 12086 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
 12087 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
 12088 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 12089 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 12090 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 12091 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 12092 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 12093 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 12094 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 12095 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 12096 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
 12097 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 12098 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 12099 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 12100 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 12101 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 12102 SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 12103 SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
 12104 SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
 12105 SQLITE_PRIVATE int sqlite3AbsInt32(int);
 12106 #ifdef SQLITE_ENABLE_8_3_NAMES
 12107 SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
 12108 #else
 12109 # define sqlite3FileSuffix3(X,Y)
 12110 #endif
 12111 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
 12113 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
 12114 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
 12115 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
 12116                         void(*)(void*));
 12117 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
 12118 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
 12119 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
 12120 #ifdef SQLITE_ENABLE_STAT3
 12121 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
 12122 #endif
 12123 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
 12124 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
 12125 #ifndef SQLITE_AMALGAMATION
 12126 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
 12127 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
 12128 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
 12129 SQLITE_PRIVATE const Token sqlite3IntTokens[];
 12130 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 12131 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 12132 #ifndef SQLITE_OMIT_WSD
 12133 SQLITE_PRIVATE int sqlite3PendingByte;
 12134 #endif
 12135 #endif
 12136 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 12137 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 12138 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 12139 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 12140 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 12141 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 12142 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
 12143 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
 12144 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 12145 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 12146 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 12147 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 12148 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 12149 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 12150 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 12151 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 12152 SQLITE_PRIVATE char sqlite3AffinityType(const char*);
 12153 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 12154 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
 12155 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 12156 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 12157 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 12158 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 12159 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 12160 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 12161 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
 12162 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
 12163 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
 12164 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
 12165 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
 12166 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
 12167 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
 12168   void (*)(sqlite3_context*,int,sqlite3_value **),
 12169   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
 12170   FuncDestructor *pDestructor
 12171 );
 12172 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
 12173 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
 12175 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
 12176 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 12177 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
 12178 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 12179 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 12180 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 12181 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 12183 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
 12184 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
 12186 /*
 12187 ** The interface to the LEMON-generated parser
 12188 */
 12189 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
 12190 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
 12191 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
 12192 #ifdef YYTRACKMAXSTACKDEPTH
 12193 SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
 12194 #endif
 12196 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
 12197 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 12198 SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
 12199 #else
 12200 # define sqlite3CloseExtensions(X)
 12201 #endif
 12203 #ifndef SQLITE_OMIT_SHARED_CACHE
 12204 SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
 12205 #else
 12206   #define sqlite3TableLock(v,w,x,y,z)
 12207 #endif
 12209 #ifdef SQLITE_TEST
 12210 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
 12211 #endif
 12213 #ifdef SQLITE_OMIT_VIRTUALTABLE
 12214 #  define sqlite3VtabClear(Y)
 12215 #  define sqlite3VtabSync(X,Y) SQLITE_OK
 12216 #  define sqlite3VtabRollback(X)
 12217 #  define sqlite3VtabCommit(X)
 12218 #  define sqlite3VtabInSync(db) 0
 12219 #  define sqlite3VtabLock(X) 
 12220 #  define sqlite3VtabUnlock(X)
 12221 #  define sqlite3VtabUnlockList(X)
 12222 #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
 12223 #  define sqlite3GetVTable(X,Y)  ((VTable*)0)
 12224 #else
 12225 SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
 12226 SQLITE_PRIVATE    void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
 12227 SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
 12228 SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
 12229 SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
 12230 SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
 12231 SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
 12232 SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
 12233 SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
 12234 SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
 12235 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
 12236 #endif
 12237 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
 12238 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
 12239 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
 12240 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
 12241 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
 12242 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
 12243 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
 12244 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 12245 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 12246 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 12247 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
 12248 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 12249 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 12250 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 12251 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 12252 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 12253 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 12254 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 12255 #ifndef SQLITE_OMIT_WAL
 12256 SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
 12257 SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
 12258 #endif
 12260 /* Declarations for functions in fkey.c. All of these are replaced by
 12261 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
 12262 ** key functionality is available. If OMIT_TRIGGER is defined but
 12263 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
 12264 ** this case foreign keys are parsed, but no other functionality is 
 12265 ** provided (enforcement of FK constraints requires the triggers sub-system).
 12266 */
 12267 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 12268 SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
 12269 SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
 12270 SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
 12271 SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
 12272 SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
 12273 SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
 12274 #else
 12275   #define sqlite3FkActions(a,b,c,d)
 12276   #define sqlite3FkCheck(a,b,c,d)
 12277   #define sqlite3FkDropTable(a,b,c)
 12278   #define sqlite3FkOldmask(a,b)      0
 12279   #define sqlite3FkRequired(a,b,c,d) 0
 12280 #endif
 12281 #ifndef SQLITE_OMIT_FOREIGN_KEY
 12282 SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
 12283 #else
 12284   #define sqlite3FkDelete(a,b)
 12285 #endif
 12288 /*
 12289 ** Available fault injectors.  Should be numbered beginning with 0.
 12290 */
 12291 #define SQLITE_FAULTINJECTOR_MALLOC     0
 12292 #define SQLITE_FAULTINJECTOR_COUNT      1
 12294 /*
 12295 ** The interface to the code in fault.c used for identifying "benign"
 12296 ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
 12297 ** is not defined.
 12298 */
 12299 #ifndef SQLITE_OMIT_BUILTIN_TEST
 12300 SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
 12301 SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
 12302 #else
 12303   #define sqlite3BeginBenignMalloc()
 12304   #define sqlite3EndBenignMalloc()
 12305 #endif
 12307 #define IN_INDEX_ROWID           1
 12308 #define IN_INDEX_EPH             2
 12309 #define IN_INDEX_INDEX           3
 12310 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
 12312 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 12313 SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
 12314 SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
 12315 SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
 12316 SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
 12317 #else
 12318   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
 12319   #define sqlite3JournalExists(p) 1
 12320 #endif
 12322 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
 12323 SQLITE_PRIVATE int sqlite3MemJournalSize(void);
 12324 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
 12326 #if SQLITE_MAX_EXPR_DEPTH>0
 12327 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
 12328 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
 12329 SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
 12330 #else
 12331   #define sqlite3ExprSetHeight(x,y)
 12332   #define sqlite3SelectExprHeight(x) 0
 12333   #define sqlite3ExprCheckHeight(x,y)
 12334 #endif
 12336 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
 12337 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
 12339 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 12340 SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
 12341 SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
 12342 SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
 12343 #else
 12344   #define sqlite3ConnectionBlocked(x,y)
 12345   #define sqlite3ConnectionUnlocked(x)
 12346   #define sqlite3ConnectionClosed(x)
 12347 #endif
 12349 #ifdef SQLITE_DEBUG
 12350 SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
 12351 #endif
 12353 /*
 12354 ** If the SQLITE_ENABLE IOTRACE exists then the global variable
 12355 ** sqlite3IoTrace is a pointer to a printf-like routine used to
 12356 ** print I/O tracing messages. 
 12357 */
 12358 #ifdef SQLITE_ENABLE_IOTRACE
 12359 # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
 12360 SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
 12361 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
 12362 #else
 12363 # define IOTRACE(A)
 12364 # define sqlite3VdbeIOTraceSql(X)
 12365 #endif
 12367 /*
 12368 ** These routines are available for the mem2.c debugging memory allocator
 12369 ** only.  They are used to verify that different "types" of memory
 12370 ** allocations are properly tracked by the system.
 12371 **
 12372 ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
 12373 ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
 12374 ** a single bit set.
 12375 **
 12376 ** sqlite3MemdebugHasType() returns true if any of the bits in its second
 12377 ** argument match the type set by the previous sqlite3MemdebugSetType().
 12378 ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
 12379 **
 12380 ** sqlite3MemdebugNoType() returns true if none of the bits in its second
 12381 ** argument match the type set by the previous sqlite3MemdebugSetType().
 12382 **
 12383 ** Perhaps the most important point is the difference between MEMTYPE_HEAP
 12384 ** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
 12385 ** it might have been allocated by lookaside, except the allocation was
 12386 ** too large or lookaside was already full.  It is important to verify
 12387 ** that allocations that might have been satisfied by lookaside are not
 12388 ** passed back to non-lookaside free() routines.  Asserts such as the
 12389 ** example above are placed on the non-lookaside free() routines to verify
 12390 ** this constraint. 
 12391 **
 12392 ** All of this is no-op for a production build.  It only comes into
 12393 ** play when the SQLITE_MEMDEBUG compile-time option is used.
 12394 */
 12395 #ifdef SQLITE_MEMDEBUG
 12396 SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
 12397 SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
 12398 SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
 12399 #else
 12400 # define sqlite3MemdebugSetType(X,Y)  /* no-op */
 12401 # define sqlite3MemdebugHasType(X,Y)  1
 12402 # define sqlite3MemdebugNoType(X,Y)   1
 12403 #endif
 12404 #define MEMTYPE_HEAP       0x01  /* General heap allocations */
 12405 #define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
 12406 #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
 12407 #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
 12408 #define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
 12410 #endif /* _SQLITEINT_H_ */
 12412 /************** End of sqliteInt.h *******************************************/
 12413 /************** Begin file global.c ******************************************/
 12414 /*
 12415 ** 2008 June 13
 12416 **
 12417 ** The author disclaims copyright to this source code.  In place of
 12418 ** a legal notice, here is a blessing:
 12419 **
 12420 **    May you do good and not evil.
 12421 **    May you find forgiveness for yourself and forgive others.
 12422 **    May you share freely, never taking more than you give.
 12423 **
 12424 *************************************************************************
 12425 **
 12426 ** This file contains definitions of global variables and contants.
 12427 */
 12429 /* An array to map all upper-case characters into their corresponding
 12430 ** lower-case character. 
 12431 **
 12432 ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
 12433 ** handle case conversions for the UTF character set since the tables
 12434 ** involved are nearly as big or bigger than SQLite itself.
 12435 */
 12436 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
 12437 #ifdef SQLITE_ASCII
 12438       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
 12439      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
 12440      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
 12441      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
 12442     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
 12443     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
 12444     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
 12445     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 12446     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
 12447     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
 12448     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
 12449     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
 12450     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
 12451     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
 12452     252,253,254,255
 12453 #endif
 12454 #ifdef SQLITE_EBCDIC
 12455       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
 12456      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
 12457      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
 12458      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
 12459      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
 12460      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
 12461      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
 12462     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
 12463     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
 12464     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
 12465     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
 12466     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
 12467     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
 12468     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
 12469     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
 12470     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
 12471 #endif
 12472 };
 12474 /*
 12475 ** The following 256 byte lookup table is used to support SQLites built-in
 12476 ** equivalents to the following standard library functions:
 12477 **
 12478 **   isspace()                        0x01
 12479 **   isalpha()                        0x02
 12480 **   isdigit()                        0x04
 12481 **   isalnum()                        0x06
 12482 **   isxdigit()                       0x08
 12483 **   toupper()                        0x20
 12484 **   SQLite identifier character      0x40
 12485 **
 12486 ** Bit 0x20 is set if the mapped character requires translation to upper
 12487 ** case. i.e. if the character is a lower-case ASCII character.
 12488 ** If x is a lower-case ASCII character, then its upper-case equivalent
 12489 ** is (x - 0x20). Therefore toupper() can be implemented as:
 12490 **
 12491 **   (x & ~(map[x]&0x20))
 12492 **
 12493 ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
 12494 ** array. tolower() is used more often than toupper() by SQLite.
 12495 **
 12496 ** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
 12497 ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
 12498 ** non-ASCII UTF character. Hence the test for whether or not a character is
 12499 ** part of an identifier is 0x46.
 12500 **
 12501 ** SQLite's versions are identical to the standard versions assuming a
 12502 ** locale of "C". They are implemented as macros in sqliteInt.h.
 12503 */
 12504 #ifdef SQLITE_ASCII
 12505 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
 12506   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
 12507   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
 12508   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
 12509   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
 12510   0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
 12511   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
 12512   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
 12513   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
 12515   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
 12516   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
 12517   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
 12518   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
 12519   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
 12520   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
 12521   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
 12522   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
 12524   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
 12525   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
 12526   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
 12527   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
 12528   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
 12529   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
 12530   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
 12531   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
 12533   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
 12534   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
 12535   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
 12536   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
 12537   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
 12538   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
 12539   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
 12540   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
 12541 };
 12542 #endif
 12544 #ifndef SQLITE_USE_URI
 12545 # define  SQLITE_USE_URI 0
 12546 #endif
 12548 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
 12549 # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
 12550 #endif
 12552 /*
 12553 ** The following singleton contains the global configuration for
 12554 ** the SQLite library.
 12555 */
 12556 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
 12557    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
 12558    1,                         /* bCoreMutex */
 12559    SQLITE_THREADSAFE==1,      /* bFullMutex */
 12560    SQLITE_USE_URI,            /* bOpenUri */
 12561    SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
 12562    0x7ffffffe,                /* mxStrlen */
 12563    128,                       /* szLookaside */
 12564    500,                       /* nLookaside */
 12565    {0,0,0,0,0,0,0,0},         /* m */
 12566    {0,0,0,0,0,0,0,0,0},       /* mutex */
 12567    {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
 12568    (void*)0,                  /* pHeap */
 12569    0,                         /* nHeap */
 12570    0, 0,                      /* mnHeap, mxHeap */
 12571    (void*)0,                  /* pScratch */
 12572    0,                         /* szScratch */
 12573    0,                         /* nScratch */
 12574    (void*)0,                  /* pPage */
 12575    0,                         /* szPage */
 12576    0,                         /* nPage */
 12577    0,                         /* mxParserStack */
 12578    0,                         /* sharedCacheEnabled */
 12579    /* All the rest should always be initialized to zero */
 12580    0,                         /* isInit */
 12581    0,                         /* inProgress */
 12582    0,                         /* isMutexInit */
 12583    0,                         /* isMallocInit */
 12584    0,                         /* isPCacheInit */
 12585    0,                         /* pInitMutex */
 12586    0,                         /* nRefInitMutex */
 12587    0,                         /* xLog */
 12588    0,                         /* pLogArg */
 12589    0,                         /* bLocaltimeFault */
 12590 #ifdef SQLITE_ENABLE_SQLLOG
 12591    0,                         /* xSqllog */
 12592    0                          /* pSqllogArg */
 12593 #endif
 12594 };
 12597 /*
 12598 ** Hash table for global functions - functions common to all
 12599 ** database connections.  After initialization, this table is
 12600 ** read-only.
 12601 */
 12602 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 12604 /*
 12605 ** Constant tokens for values 0 and 1.
 12606 */
 12607 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
 12608    { "0", 1 },
 12609    { "1", 1 }
 12610 };
 12613 /*
 12614 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
 12615 ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 12616 ** the database page that contains the pending byte.  It never attempts
 12617 ** to read or write that page.  The pending byte page is set assign
 12618 ** for use by the VFS layers as space for managing file locks.
 12619 **
 12620 ** During testing, it is often desirable to move the pending byte to
 12621 ** a different position in the file.  This allows code that has to
 12622 ** deal with the pending byte to run on files that are much smaller
 12623 ** than 1 GiB.  The sqlite3_test_control() interface can be used to
 12624 ** move the pending byte.
 12625 **
 12626 ** IMPORTANT:  Changing the pending byte to any value other than
 12627 ** 0x40000000 results in an incompatible database file format!
 12628 ** Changing the pending byte during operating results in undefined
 12629 ** and dileterious behavior.
 12630 */
 12631 #ifndef SQLITE_OMIT_WSD
 12632 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
 12633 #endif
 12635 /*
 12636 ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
 12637 ** created by mkopcodeh.awk during compilation.  Data is obtained
 12638 ** from the comments following the "case OP_xxxx:" statements in
 12639 ** the vdbe.c file.  
 12640 */
 12641 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
 12643 /************** End of global.c **********************************************/
 12644 /************** Begin file ctime.c *******************************************/
 12645 /*
 12646 ** 2010 February 23
 12647 **
 12648 ** The author disclaims copyright to this source code.  In place of
 12649 ** a legal notice, here is a blessing:
 12650 **
 12651 **    May you do good and not evil.
 12652 **    May you find forgiveness for yourself and forgive others.
 12653 **    May you share freely, never taking more than you give.
 12654 **
 12655 *************************************************************************
 12656 **
 12657 ** This file implements routines used to report what compile-time options
 12658 ** SQLite was built with.
 12659 */
 12661 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 12664 /*
 12665 ** An array of names of all compile-time options.  This array should 
 12666 ** be sorted A-Z.
 12667 **
 12668 ** This array looks large, but in a typical installation actually uses
 12669 ** only a handful of compile-time options, so most times this array is usually
 12670 ** rather short and uses little memory space.
 12671 */
 12672 static const char * const azCompileOpt[] = {
 12674 /* These macros are provided to "stringify" the value of the define
 12675 ** for those options in which the value is meaningful. */
 12676 #define CTIMEOPT_VAL_(opt) #opt
 12677 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
 12679 #ifdef SQLITE_32BIT_ROWID
 12680   "32BIT_ROWID",
 12681 #endif
 12682 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
 12683   "4_BYTE_ALIGNED_MALLOC",
 12684 #endif
 12685 #ifdef SQLITE_CASE_SENSITIVE_LIKE
 12686   "CASE_SENSITIVE_LIKE",
 12687 #endif
 12688 #ifdef SQLITE_CHECK_PAGES
 12689   "CHECK_PAGES",
 12690 #endif
 12691 #ifdef SQLITE_COVERAGE_TEST
 12692   "COVERAGE_TEST",
 12693 #endif
 12694 #ifdef SQLITE_CURDIR
 12695   "CURDIR",
 12696 #endif
 12697 #ifdef SQLITE_DEBUG
 12698   "DEBUG",
 12699 #endif
 12700 #ifdef SQLITE_DEFAULT_LOCKING_MODE
 12701   "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
 12702 #endif
 12703 #ifdef SQLITE_DISABLE_DIRSYNC
 12704   "DISABLE_DIRSYNC",
 12705 #endif
 12706 #ifdef SQLITE_DISABLE_LFS
 12707   "DISABLE_LFS",
 12708 #endif
 12709 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 12710   "ENABLE_ATOMIC_WRITE",
 12711 #endif
 12712 #ifdef SQLITE_ENABLE_CEROD
 12713   "ENABLE_CEROD",
 12714 #endif
 12715 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 12716   "ENABLE_COLUMN_METADATA",
 12717 #endif
 12718 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 12719   "ENABLE_EXPENSIVE_ASSERT",
 12720 #endif
 12721 #ifdef SQLITE_ENABLE_FTS1
 12722   "ENABLE_FTS1",
 12723 #endif
 12724 #ifdef SQLITE_ENABLE_FTS2
 12725   "ENABLE_FTS2",
 12726 #endif
 12727 #ifdef SQLITE_ENABLE_FTS3
 12728   "ENABLE_FTS3",
 12729 #endif
 12730 #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
 12731   "ENABLE_FTS3_PARENTHESIS",
 12732 #endif
 12733 #ifdef SQLITE_ENABLE_FTS4
 12734   "ENABLE_FTS4",
 12735 #endif
 12736 #ifdef SQLITE_ENABLE_ICU
 12737   "ENABLE_ICU",
 12738 #endif
 12739 #ifdef SQLITE_ENABLE_IOTRACE
 12740   "ENABLE_IOTRACE",
 12741 #endif
 12742 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
 12743   "ENABLE_LOAD_EXTENSION",
 12744 #endif
 12745 #ifdef SQLITE_ENABLE_LOCKING_STYLE
 12746   "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
 12747 #endif
 12748 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 12749   "ENABLE_MEMORY_MANAGEMENT",
 12750 #endif
 12751 #ifdef SQLITE_ENABLE_MEMSYS3
 12752   "ENABLE_MEMSYS3",
 12753 #endif
 12754 #ifdef SQLITE_ENABLE_MEMSYS5
 12755   "ENABLE_MEMSYS5",
 12756 #endif
 12757 #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
 12758   "ENABLE_OVERSIZE_CELL_CHECK",
 12759 #endif
 12760 #ifdef SQLITE_ENABLE_RTREE
 12761   "ENABLE_RTREE",
 12762 #endif
 12763 #ifdef SQLITE_ENABLE_STAT3
 12764   "ENABLE_STAT3",
 12765 #endif
 12766 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 12767   "ENABLE_UNLOCK_NOTIFY",
 12768 #endif
 12769 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
 12770   "ENABLE_UPDATE_DELETE_LIMIT",
 12771 #endif
 12772 #ifdef SQLITE_HAS_CODEC
 12773   "HAS_CODEC",
 12774 #endif
 12775 #ifdef SQLITE_HAVE_ISNAN
 12776   "HAVE_ISNAN",
 12777 #endif
 12778 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 12779   "HOMEGROWN_RECURSIVE_MUTEX",
 12780 #endif
 12781 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
 12782   "IGNORE_AFP_LOCK_ERRORS",
 12783 #endif
 12784 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 12785   "IGNORE_FLOCK_LOCK_ERRORS",
 12786 #endif
 12787 #ifdef SQLITE_INT64_TYPE
 12788   "INT64_TYPE",
 12789 #endif
 12790 #ifdef SQLITE_LOCK_TRACE
 12791   "LOCK_TRACE",
 12792 #endif
 12793 #ifdef SQLITE_MAX_SCHEMA_RETRY
 12794   "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
 12795 #endif
 12796 #ifdef SQLITE_MEMDEBUG
 12797   "MEMDEBUG",
 12798 #endif
 12799 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 12800   "MIXED_ENDIAN_64BIT_FLOAT",
 12801 #endif
 12802 #ifdef SQLITE_NO_SYNC
 12803   "NO_SYNC",
 12804 #endif
 12805 #ifdef SQLITE_OMIT_ALTERTABLE
 12806   "OMIT_ALTERTABLE",
 12807 #endif
 12808 #ifdef SQLITE_OMIT_ANALYZE
 12809   "OMIT_ANALYZE",
 12810 #endif
 12811 #ifdef SQLITE_OMIT_ATTACH
 12812   "OMIT_ATTACH",
 12813 #endif
 12814 #ifdef SQLITE_OMIT_AUTHORIZATION
 12815   "OMIT_AUTHORIZATION",
 12816 #endif
 12817 #ifdef SQLITE_OMIT_AUTOINCREMENT
 12818   "OMIT_AUTOINCREMENT",
 12819 #endif
 12820 #ifdef SQLITE_OMIT_AUTOINIT
 12821   "OMIT_AUTOINIT",
 12822 #endif
 12823 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
 12824   "OMIT_AUTOMATIC_INDEX",
 12825 #endif
 12826 #ifdef SQLITE_OMIT_AUTORESET
 12827   "OMIT_AUTORESET",
 12828 #endif
 12829 #ifdef SQLITE_OMIT_AUTOVACUUM
 12830   "OMIT_AUTOVACUUM",
 12831 #endif
 12832 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
 12833   "OMIT_BETWEEN_OPTIMIZATION",
 12834 #endif
 12835 #ifdef SQLITE_OMIT_BLOB_LITERAL
 12836   "OMIT_BLOB_LITERAL",
 12837 #endif
 12838 #ifdef SQLITE_OMIT_BTREECOUNT
 12839   "OMIT_BTREECOUNT",
 12840 #endif
 12841 #ifdef SQLITE_OMIT_BUILTIN_TEST
 12842   "OMIT_BUILTIN_TEST",
 12843 #endif
 12844 #ifdef SQLITE_OMIT_CAST
 12845   "OMIT_CAST",
 12846 #endif
 12847 #ifdef SQLITE_OMIT_CHECK
 12848   "OMIT_CHECK",
 12849 #endif
 12850 /* // redundant
 12851 ** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
 12852 **   "OMIT_COMPILEOPTION_DIAGS",
 12853 ** #endif
 12854 */
 12855 #ifdef SQLITE_OMIT_COMPLETE
 12856   "OMIT_COMPLETE",
 12857 #endif
 12858 #ifdef SQLITE_OMIT_COMPOUND_SELECT
 12859   "OMIT_COMPOUND_SELECT",
 12860 #endif
 12861 #ifdef SQLITE_OMIT_DATETIME_FUNCS
 12862   "OMIT_DATETIME_FUNCS",
 12863 #endif
 12864 #ifdef SQLITE_OMIT_DECLTYPE
 12865   "OMIT_DECLTYPE",
 12866 #endif
 12867 #ifdef SQLITE_OMIT_DEPRECATED
 12868   "OMIT_DEPRECATED",
 12869 #endif
 12870 #ifdef SQLITE_OMIT_DISKIO
 12871   "OMIT_DISKIO",
 12872 #endif
 12873 #ifdef SQLITE_OMIT_EXPLAIN
 12874   "OMIT_EXPLAIN",
 12875 #endif
 12876 #ifdef SQLITE_OMIT_FLAG_PRAGMAS
 12877   "OMIT_FLAG_PRAGMAS",
 12878 #endif
 12879 #ifdef SQLITE_OMIT_FLOATING_POINT
 12880   "OMIT_FLOATING_POINT",
 12881 #endif
 12882 #ifdef SQLITE_OMIT_FOREIGN_KEY
 12883   "OMIT_FOREIGN_KEY",
 12884 #endif
 12885 #ifdef SQLITE_OMIT_GET_TABLE
 12886   "OMIT_GET_TABLE",
 12887 #endif
 12888 #ifdef SQLITE_OMIT_INCRBLOB
 12889   "OMIT_INCRBLOB",
 12890 #endif
 12891 #ifdef SQLITE_OMIT_INTEGRITY_CHECK
 12892   "OMIT_INTEGRITY_CHECK",
 12893 #endif
 12894 #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
 12895   "OMIT_LIKE_OPTIMIZATION",
 12896 #endif
 12897 #ifdef SQLITE_OMIT_LOAD_EXTENSION
 12898   "OMIT_LOAD_EXTENSION",
 12899 #endif
 12900 #ifdef SQLITE_OMIT_LOCALTIME
 12901   "OMIT_LOCALTIME",
 12902 #endif
 12903 #ifdef SQLITE_OMIT_LOOKASIDE
 12904   "OMIT_LOOKASIDE",
 12905 #endif
 12906 #ifdef SQLITE_OMIT_MEMORYDB
 12907   "OMIT_MEMORYDB",
 12908 #endif
 12909 #ifdef SQLITE_OMIT_MERGE_SORT
 12910   "OMIT_MERGE_SORT",
 12911 #endif
 12912 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
 12913   "OMIT_OR_OPTIMIZATION",
 12914 #endif
 12915 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
 12916   "OMIT_PAGER_PRAGMAS",
 12917 #endif
 12918 #ifdef SQLITE_OMIT_PRAGMA
 12919   "OMIT_PRAGMA",
 12920 #endif
 12921 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
 12922   "OMIT_PROGRESS_CALLBACK",
 12923 #endif
 12924 #ifdef SQLITE_OMIT_QUICKBALANCE
 12925   "OMIT_QUICKBALANCE",
 12926 #endif
 12927 #ifdef SQLITE_OMIT_REINDEX
 12928   "OMIT_REINDEX",
 12929 #endif
 12930 #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
 12931   "OMIT_SCHEMA_PRAGMAS",
 12932 #endif
 12933 #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
 12934   "OMIT_SCHEMA_VERSION_PRAGMAS",
 12935 #endif
 12936 #ifdef SQLITE_OMIT_SHARED_CACHE
 12937   "OMIT_SHARED_CACHE",
 12938 #endif
 12939 #ifdef SQLITE_OMIT_SUBQUERY
 12940   "OMIT_SUBQUERY",
 12941 #endif
 12942 #ifdef SQLITE_OMIT_TCL_VARIABLE
 12943   "OMIT_TCL_VARIABLE",
 12944 #endif
 12945 #ifdef SQLITE_OMIT_TEMPDB
 12946   "OMIT_TEMPDB",
 12947 #endif
 12948 #ifdef SQLITE_OMIT_TRACE
 12949   "OMIT_TRACE",
 12950 #endif
 12951 #ifdef SQLITE_OMIT_TRIGGER
 12952   "OMIT_TRIGGER",
 12953 #endif
 12954 #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 12955   "OMIT_TRUNCATE_OPTIMIZATION",
 12956 #endif
 12957 #ifdef SQLITE_OMIT_UTF16
 12958   "OMIT_UTF16",
 12959 #endif
 12960 #ifdef SQLITE_OMIT_VACUUM
 12961   "OMIT_VACUUM",
 12962 #endif
 12963 #ifdef SQLITE_OMIT_VIEW
 12964   "OMIT_VIEW",
 12965 #endif
 12966 #ifdef SQLITE_OMIT_VIRTUALTABLE
 12967   "OMIT_VIRTUALTABLE",
 12968 #endif
 12969 #ifdef SQLITE_OMIT_WAL
 12970   "OMIT_WAL",
 12971 #endif
 12972 #ifdef SQLITE_OMIT_WSD
 12973   "OMIT_WSD",
 12974 #endif
 12975 #ifdef SQLITE_OMIT_XFER_OPT
 12976   "OMIT_XFER_OPT",
 12977 #endif
 12978 #ifdef SQLITE_PERFORMANCE_TRACE
 12979   "PERFORMANCE_TRACE",
 12980 #endif
 12981 #ifdef SQLITE_PROXY_DEBUG
 12982   "PROXY_DEBUG",
 12983 #endif
 12984 #ifdef SQLITE_RTREE_INT_ONLY
 12985   "RTREE_INT_ONLY",
 12986 #endif
 12987 #ifdef SQLITE_SECURE_DELETE
 12988   "SECURE_DELETE",
 12989 #endif
 12990 #ifdef SQLITE_SMALL_STACK
 12991   "SMALL_STACK",
 12992 #endif
 12993 #ifdef SQLITE_SOUNDEX
 12994   "SOUNDEX",
 12995 #endif
 12996 #ifdef SQLITE_TCL
 12997   "TCL",
 12998 #endif
 12999 #ifdef SQLITE_TEMP_STORE
 13000   "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
 13001 #endif
 13002 #ifdef SQLITE_TEST
 13003   "TEST",
 13004 #endif
 13005 #ifdef SQLITE_THREADSAFE
 13006   "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
 13007 #endif
 13008 #ifdef SQLITE_USE_ALLOCA
 13009   "USE_ALLOCA",
 13010 #endif
 13011 #ifdef SQLITE_ZERO_MALLOC
 13012   "ZERO_MALLOC"
 13013 #endif
 13014 };
 13016 /*
 13017 ** Given the name of a compile-time option, return true if that option
 13018 ** was used and false if not.
 13019 **
 13020 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
 13021 ** is not required for a match.
 13022 */
 13023 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
 13024   int i, n;
 13025   if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
 13026   n = sqlite3Strlen30(zOptName);
 13028   /* Since ArraySize(azCompileOpt) is normally in single digits, a
 13029   ** linear search is adequate.  No need for a binary search. */
 13030   for(i=0; i<ArraySize(azCompileOpt); i++){
 13031     if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
 13032        && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
 13034   return 0;
 13037 /*
 13038 ** Return the N-th compile-time option string.  If N is out of range,
 13039 ** return a NULL pointer.
 13040 */
 13041 SQLITE_API const char *sqlite3_compileoption_get(int N){
 13042   if( N>=0 && N<ArraySize(azCompileOpt) ){
 13043     return azCompileOpt[N];
 13045   return 0;
 13048 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 13050 /************** End of ctime.c ***********************************************/
 13051 /************** Begin file status.c ******************************************/
 13052 /*
 13053 ** 2008 June 18
 13054 **
 13055 ** The author disclaims copyright to this source code.  In place of
 13056 ** a legal notice, here is a blessing:
 13057 **
 13058 **    May you do good and not evil.
 13059 **    May you find forgiveness for yourself and forgive others.
 13060 **    May you share freely, never taking more than you give.
 13061 **
 13062 *************************************************************************
 13063 **
 13064 ** This module implements the sqlite3_status() interface and related
 13065 ** functionality.
 13066 */
 13067 /************** Include vdbeInt.h in the middle of status.c ******************/
 13068 /************** Begin file vdbeInt.h *****************************************/
 13069 /*
 13070 ** 2003 September 6
 13071 **
 13072 ** The author disclaims copyright to this source code.  In place of
 13073 ** a legal notice, here is a blessing:
 13074 **
 13075 **    May you do good and not evil.
 13076 **    May you find forgiveness for yourself and forgive others.
 13077 **    May you share freely, never taking more than you give.
 13078 **
 13079 *************************************************************************
 13080 ** This is the header file for information that is private to the
 13081 ** VDBE.  This information used to all be at the top of the single
 13082 ** source code file "vdbe.c".  When that file became too big (over
 13083 ** 6000 lines long) it was split up into several smaller files and
 13084 ** this header information was factored out.
 13085 */
 13086 #ifndef _VDBEINT_H_
 13087 #define _VDBEINT_H_
 13089 /*
 13090 ** SQL is translated into a sequence of instructions to be
 13091 ** executed by a virtual machine.  Each instruction is an instance
 13092 ** of the following structure.
 13093 */
 13094 typedef struct VdbeOp Op;
 13096 /*
 13097 ** Boolean values
 13098 */
 13099 typedef unsigned char Bool;
 13101 /* Opaque type used by code in vdbesort.c */
 13102 typedef struct VdbeSorter VdbeSorter;
 13104 /* Opaque type used by the explainer */
 13105 typedef struct Explain Explain;
 13107 /*
 13108 ** A cursor is a pointer into a single BTree within a database file.
 13109 ** The cursor can seek to a BTree entry with a particular key, or
 13110 ** loop over all entries of the Btree.  You can also insert new BTree
 13111 ** entries or retrieve the key or data from the entry that the cursor
 13112 ** is currently pointing to.
 13113 ** 
 13114 ** Every cursor that the virtual machine has open is represented by an
 13115 ** instance of the following structure.
 13116 */
 13117 struct VdbeCursor {
 13118   BtCursor *pCursor;    /* The cursor structure of the backend */
 13119   Btree *pBt;           /* Separate file holding temporary table */
 13120   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
 13121   int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
 13122   int pseudoTableReg;   /* Register holding pseudotable content. */
 13123   int nField;           /* Number of fields in the header */
 13124   Bool zeroed;          /* True if zeroed out and ready for reuse */
 13125   Bool rowidIsValid;    /* True if lastRowid is valid */
 13126   Bool atFirst;         /* True if pointing to first entry */
 13127   Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
 13128   Bool nullRow;         /* True if pointing to a row with no data */
 13129   Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
 13130   Bool isTable;         /* True if a table requiring integer keys */
 13131   Bool isIndex;         /* True if an index containing keys only - no data */
 13132   Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
 13133   Bool isSorter;        /* True if a new-style sorter */
 13134   Bool multiPseudo;     /* Multi-register pseudo-cursor */
 13135   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
 13136   const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
 13137   i64 seqCount;         /* Sequence counter */
 13138   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
 13139   i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
 13140   VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
 13142   /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
 13143   ** OP_IsUnique opcode on this cursor. */
 13144   int seekResult;
 13146   /* Cached information about the header for the data record that the
 13147   ** cursor is currently pointing to.  Only valid if cacheStatus matches
 13148   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
 13149   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
 13150   ** the cache is out of date.
 13151   **
 13152   ** aRow might point to (ephemeral) data for the current row, or it might
 13153   ** be NULL.
 13154   */
 13155   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
 13156   int payloadSize;      /* Total number of bytes in the record */
 13157   u32 *aType;           /* Type values for all entries in the record */
 13158   u32 *aOffset;         /* Cached offsets to the start of each columns data */
 13159   u8 *aRow;             /* Data for the current row, if all on one page */
 13160 };
 13161 typedef struct VdbeCursor VdbeCursor;
 13163 /*
 13164 ** When a sub-program is executed (OP_Program), a structure of this type
 13165 ** is allocated to store the current value of the program counter, as
 13166 ** well as the current memory cell array and various other frame specific
 13167 ** values stored in the Vdbe struct. When the sub-program is finished, 
 13168 ** these values are copied back to the Vdbe from the VdbeFrame structure,
 13169 ** restoring the state of the VM to as it was before the sub-program
 13170 ** began executing.
 13171 **
 13172 ** The memory for a VdbeFrame object is allocated and managed by a memory
 13173 ** cell in the parent (calling) frame. When the memory cell is deleted or
 13174 ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
 13175 ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
 13176 ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
 13177 ** this instead of deleting the VdbeFrame immediately is to avoid recursive
 13178 ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
 13179 ** child frame are released.
 13180 **
 13181 ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
 13182 ** set to NULL if the currently executing frame is the main program.
 13183 */
 13184 typedef struct VdbeFrame VdbeFrame;
 13185 struct VdbeFrame {
 13186   Vdbe *v;                /* VM this frame belongs to */
 13187   VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
 13188   Op *aOp;                /* Program instructions for parent frame */
 13189   Mem *aMem;              /* Array of memory cells for parent frame */
 13190   u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
 13191   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
 13192   void *token;            /* Copy of SubProgram.token */
 13193   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
 13194   u16 nCursor;            /* Number of entries in apCsr */
 13195   int pc;                 /* Program Counter in parent (calling) frame */
 13196   int nOp;                /* Size of aOp array */
 13197   int nMem;               /* Number of entries in aMem */
 13198   int nOnceFlag;          /* Number of entries in aOnceFlag */
 13199   int nChildMem;          /* Number of memory cells for child frame */
 13200   int nChildCsr;          /* Number of cursors for child frame */
 13201   int nChange;            /* Statement changes (Vdbe.nChanges)     */
 13202 };
 13204 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
 13206 /*
 13207 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
 13208 */
 13209 #define CACHE_STALE 0
 13211 /*
 13212 ** Internally, the vdbe manipulates nearly all SQL values as Mem
 13213 ** structures. Each Mem struct may cache multiple representations (string,
 13214 ** integer etc.) of the same value.
 13215 */
 13216 struct Mem {
 13217   sqlite3 *db;        /* The associated database connection */
 13218   char *z;            /* String or BLOB value */
 13219   double r;           /* Real value */
 13220   union {
 13221     i64 i;              /* Integer value used when MEM_Int is set in flags */
 13222     int nZero;          /* Used when bit MEM_Zero is set in flags */
 13223     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
 13224     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
 13225     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
 13226   } u;
 13227   int n;              /* Number of characters in string value, excluding '\0' */
 13228   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
 13229   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
 13230   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
 13231 #ifdef SQLITE_DEBUG
 13232   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
 13233   void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
 13234 #endif
 13235   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
 13236   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
 13237 };
 13239 /* One or more of the following flags are set to indicate the validOK
 13240 ** representations of the value stored in the Mem struct.
 13241 **
 13242 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
 13243 ** No other flags may be set in this case.
 13244 **
 13245 ** If the MEM_Str flag is set then Mem.z points at a string representation.
 13246 ** Usually this is encoded in the same unicode encoding as the main
 13247 ** database (see below for exceptions). If the MEM_Term flag is also
 13248 ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 13249 ** flags may coexist with the MEM_Str flag.
 13250 */
 13251 #define MEM_Null      0x0001   /* Value is NULL */
 13252 #define MEM_Str       0x0002   /* Value is a string */
 13253 #define MEM_Int       0x0004   /* Value is an integer */
 13254 #define MEM_Real      0x0008   /* Value is a real number */
 13255 #define MEM_Blob      0x0010   /* Value is a BLOB */
 13256 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
 13257 #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
 13258 #define MEM_Invalid   0x0080   /* Value is undefined */
 13259 #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
 13260 #define MEM_TypeMask  0x01ff   /* Mask of type bits */
 13263 /* Whenever Mem contains a valid string or blob representation, one of
 13264 ** the following flags must be set to determine the memory management
 13265 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
 13266 ** string is \000 or \u0000 terminated
 13267 */
 13268 #define MEM_Term      0x0200   /* String rep is nul terminated */
 13269 #define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
 13270 #define MEM_Static    0x0800   /* Mem.z points to a static string */
 13271 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
 13272 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
 13273 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
 13274 #ifdef SQLITE_OMIT_INCRBLOB
 13275   #undef MEM_Zero
 13276   #define MEM_Zero 0x0000
 13277 #endif
 13279 /*
 13280 ** Clear any existing type flags from a Mem and replace them with f
 13281 */
 13282 #define MemSetTypeFlag(p, f) \
 13283    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
 13285 /*
 13286 ** Return true if a memory cell is not marked as invalid.  This macro
 13287 ** is for use inside assert() statements only.
 13288 */
 13289 #ifdef SQLITE_DEBUG
 13290 #define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
 13291 #endif
 13294 /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
 13295 ** additional information about auxiliary information bound to arguments
 13296 ** of the function.  This is used to implement the sqlite3_get_auxdata()
 13297 ** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
 13298 ** that can be associated with a constant argument to a function.  This
 13299 ** allows functions such as "regexp" to compile their constant regular
 13300 ** expression argument once and reused the compiled code for multiple
 13301 ** invocations.
 13302 */
 13303 struct VdbeFunc {
 13304   FuncDef *pFunc;               /* The definition of the function */
 13305   int nAux;                     /* Number of entries allocated for apAux[] */
 13306   struct AuxData {
 13307     void *pAux;                   /* Aux data for the i-th argument */
 13308     void (*xDelete)(void *);      /* Destructor for the aux data */
 13309   } apAux[1];                   /* One slot for each function argument */
 13310 };
 13312 /*
 13313 ** The "context" argument for a installable function.  A pointer to an
 13314 ** instance of this structure is the first argument to the routines used
 13315 ** implement the SQL functions.
 13316 **
 13317 ** There is a typedef for this structure in sqlite.h.  So all routines,
 13318 ** even the public interface to SQLite, can use a pointer to this structure.
 13319 ** But this file is the only place where the internal details of this
 13320 ** structure are known.
 13321 **
 13322 ** This structure is defined inside of vdbeInt.h because it uses substructures
 13323 ** (Mem) which are only defined there.
 13324 */
 13325 struct sqlite3_context {
 13326   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
 13327   VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
 13328   Mem s;                /* The return value is stored here */
 13329   Mem *pMem;            /* Memory cell used to store aggregate context */
 13330   CollSeq *pColl;       /* Collating sequence */
 13331   int isError;          /* Error code returned by the function. */
 13332   int skipFlag;         /* Skip skip accumulator loading if true */
 13333 };
 13335 /*
 13336 ** An Explain object accumulates indented output which is helpful
 13337 ** in describing recursive data structures.
 13338 */
 13339 struct Explain {
 13340   Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
 13341   StrAccum str;      /* The string being accumulated */
 13342   int nIndent;       /* Number of elements in aIndent */
 13343   u16 aIndent[100];  /* Levels of indentation */
 13344   char zBase[100];   /* Initial space */
 13345 };
 13347 /* A bitfield type for use inside of structures.  Always follow with :N where
 13348 ** N is the number of bits.
 13349 */
 13350 typedef unsigned bft;  /* Bit Field Type */
 13352 /*
 13353 ** An instance of the virtual machine.  This structure contains the complete
 13354 ** state of the virtual machine.
 13355 **
 13356 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
 13357 ** is really a pointer to an instance of this structure.
 13358 **
 13359 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
 13360 ** any virtual table method invocations made by the vdbe program. It is
 13361 ** set to 2 for xDestroy method calls and 1 for all other methods. This
 13362 ** variable is used for two purposes: to allow xDestroy methods to execute
 13363 ** "DROP TABLE" statements and to prevent some nasty side effects of
 13364 ** malloc failure when SQLite is invoked recursively by a virtual table 
 13365 ** method function.
 13366 */
 13367 struct Vdbe {
 13368   sqlite3 *db;            /* The database connection that owns this statement */
 13369   Op *aOp;                /* Space to hold the virtual machine's program */
 13370   Mem *aMem;              /* The memory locations */
 13371   Mem **apArg;            /* Arguments to currently executing user function */
 13372   Mem *aColName;          /* Column names to return */
 13373   Mem *pResultSet;        /* Pointer to an array of results */
 13374   int nMem;               /* Number of memory locations currently allocated */
 13375   int nOp;                /* Number of instructions in the program */
 13376   int nOpAlloc;           /* Number of slots allocated for aOp[] */
 13377   int nLabel;             /* Number of labels used */
 13378   int *aLabel;            /* Space to hold the labels */
 13379   u16 nResColumn;         /* Number of columns in one row of the result set */
 13380   u16 nCursor;            /* Number of slots in apCsr[] */
 13381   u32 magic;              /* Magic number for sanity checking */
 13382   char *zErrMsg;          /* Error message written here */
 13383   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
 13384   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
 13385   Mem *aVar;              /* Values for the OP_Variable opcode. */
 13386   char **azVar;           /* Name of variables */
 13387   ynVar nVar;             /* Number of entries in aVar[] */
 13388   ynVar nzVar;            /* Number of entries in azVar[] */
 13389   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
 13390   int pc;                 /* The program counter */
 13391   int rc;                 /* Value to return */
 13392   u8 errorAction;         /* Recovery action to do in case of an error */
 13393   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 13394   bft explain:2;          /* True if EXPLAIN present on SQL command */
 13395   bft inVtabMethod:2;     /* See comments above */
 13396   bft changeCntOn:1;      /* True to update the change-counter */
 13397   bft expired:1;          /* True if the VM needs to be recompiled */
 13398   bft runOnlyOnce:1;      /* Automatically expire on reset */
 13399   bft usesStmtJournal:1;  /* True if uses a statement journal */
 13400   bft readOnly:1;         /* True for read-only statements */
 13401   bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
 13402   bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
 13403   int nChange;            /* Number of db changes made since last reset */
 13404   yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
 13405   yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 13406   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
 13407   int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
 13408 #ifndef SQLITE_OMIT_TRACE
 13409   i64 startTime;          /* Time when query started - used for profiling */
 13410 #endif
 13411   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
 13412   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
 13413   char *zSql;             /* Text of the SQL statement that generated this */
 13414   void *pFree;            /* Free this when deleting the vdbe */
 13415 #ifdef SQLITE_DEBUG
 13416   FILE *trace;            /* Write an execution trace here, if not NULL */
 13417 #endif
 13418 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
 13419   Explain *pExplain;      /* The explainer */
 13420   char *zExplain;         /* Explanation of data structures */
 13421 #endif
 13422   VdbeFrame *pFrame;      /* Parent frame */
 13423   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 13424   int nFrame;             /* Number of frames in pFrame list */
 13425   u32 expmask;            /* Binding to these vars invalidates VM */
 13426   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 13427   int nOnceFlag;          /* Size of array aOnceFlag[] */
 13428   u8 *aOnceFlag;          /* Flags for OP_Once */
 13429 };
 13431 /*
 13432 ** The following are allowed values for Vdbe.magic
 13433 */
 13434 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
 13435 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
 13436 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
 13437 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
 13439 /*
 13440 ** Function prototypes
 13441 */
 13442 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
 13443 void sqliteVdbePopStack(Vdbe*,int);
 13444 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
 13445 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 13446 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
 13447 #endif
 13448 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
 13449 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
 13450 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
 13451 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 13452 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
 13454 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 13455 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
 13456 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
 13457 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
 13458 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
 13459 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
 13460 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 13461 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 13462 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
 13463 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
 13464 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
 13465 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
 13466 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
 13467 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
 13468 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
 13469 #ifdef SQLITE_OMIT_FLOATING_POINT
 13470 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
 13471 #else
 13472 SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
 13473 #endif
 13474 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
 13475 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
 13476 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
 13477 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
 13478 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
 13479 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
 13480 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
 13481 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
 13482 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 13483 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 13484 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 13485 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
 13486 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 13487 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
 13488 #define VdbeMemRelease(X)  \
 13489   if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
 13490     sqlite3VdbeMemReleaseExternal(X);
 13491 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 13492 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
 13493 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 13494 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 13495 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 13496 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 13497 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
 13498 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
 13500 #ifdef SQLITE_OMIT_MERGE_SORT
 13501 # define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
 13502 # define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
 13503 # define sqlite3VdbeSorterClose(Y,Z)
 13504 # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
 13505 # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
 13506 # define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
 13507 # define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
 13508 #else
 13509 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
 13510 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 13511 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
 13512 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
 13513 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
 13514 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
 13515 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int *);
 13516 #endif
 13518 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 13519 SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
 13520 SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
 13521 #else
 13522 # define sqlite3VdbeEnter(X)
 13523 # define sqlite3VdbeLeave(X)
 13524 #endif
 13526 #ifdef SQLITE_DEBUG
 13527 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
 13528 #endif
 13530 #ifndef SQLITE_OMIT_FOREIGN_KEY
 13531 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
 13532 #else
 13533 # define sqlite3VdbeCheckFk(p,i) 0
 13534 #endif
 13536 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
 13537 #ifdef SQLITE_DEBUG
 13538 SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
 13539 SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
 13540 #endif
 13541 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
 13543 #ifndef SQLITE_OMIT_INCRBLOB
 13544 SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
 13545   #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
 13546 #else
 13547   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
 13548   #define ExpandBlob(P) SQLITE_OK
 13549 #endif
 13551 #endif /* !defined(_VDBEINT_H_) */
 13553 /************** End of vdbeInt.h *********************************************/
 13554 /************** Continuing where we left off in status.c *********************/
 13556 /*
 13557 ** Variables in which to record status information.
 13558 */
 13559 typedef struct sqlite3StatType sqlite3StatType;
 13560 static SQLITE_WSD struct sqlite3StatType {
 13561   int nowValue[10];         /* Current value */
 13562   int mxValue[10];          /* Maximum value */
 13563 } sqlite3Stat = { {0,}, {0,} };
 13566 /* The "wsdStat" macro will resolve to the status information
 13567 ** state vector.  If writable static data is unsupported on the target,
 13568 ** we have to locate the state vector at run-time.  In the more common
 13569 ** case where writable static data is supported, wsdStat can refer directly
 13570 ** to the "sqlite3Stat" state vector declared above.
 13571 */
 13572 #ifdef SQLITE_OMIT_WSD
 13573 # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
 13574 # define wsdStat x[0]
 13575 #else
 13576 # define wsdStatInit
 13577 # define wsdStat sqlite3Stat
 13578 #endif
 13580 /*
 13581 ** Return the current value of a status parameter.
 13582 */
 13583 SQLITE_PRIVATE int sqlite3StatusValue(int op){
 13584   wsdStatInit;
 13585   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 13586   return wsdStat.nowValue[op];
 13589 /*
 13590 ** Add N to the value of a status record.  It is assumed that the
 13591 ** caller holds appropriate locks.
 13592 */
 13593 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
 13594   wsdStatInit;
 13595   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 13596   wsdStat.nowValue[op] += N;
 13597   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
 13598     wsdStat.mxValue[op] = wsdStat.nowValue[op];
 13602 /*
 13603 ** Set the value of a status to X.
 13604 */
 13605 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
 13606   wsdStatInit;
 13607   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 13608   wsdStat.nowValue[op] = X;
 13609   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
 13610     wsdStat.mxValue[op] = wsdStat.nowValue[op];
 13614 /*
 13615 ** Query status information.
 13616 **
 13617 ** This implementation assumes that reading or writing an aligned
 13618 ** 32-bit integer is an atomic operation.  If that assumption is not true,
 13619 ** then this routine is not threadsafe.
 13620 */
 13621 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
 13622   wsdStatInit;
 13623   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
 13624     return SQLITE_MISUSE_BKPT;
 13626   *pCurrent = wsdStat.nowValue[op];
 13627   *pHighwater = wsdStat.mxValue[op];
 13628   if( resetFlag ){
 13629     wsdStat.mxValue[op] = wsdStat.nowValue[op];
 13631   return SQLITE_OK;
 13634 /*
 13635 ** Query status information for a single database connection
 13636 */
 13637 SQLITE_API int sqlite3_db_status(
 13638   sqlite3 *db,          /* The database connection whose status is desired */
 13639   int op,               /* Status verb */
 13640   int *pCurrent,        /* Write current value here */
 13641   int *pHighwater,      /* Write high-water mark here */
 13642   int resetFlag         /* Reset high-water mark if true */
 13643 ){
 13644   int rc = SQLITE_OK;   /* Return code */
 13645   sqlite3_mutex_enter(db->mutex);
 13646   switch( op ){
 13647     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
 13648       *pCurrent = db->lookaside.nOut;
 13649       *pHighwater = db->lookaside.mxOut;
 13650       if( resetFlag ){
 13651         db->lookaside.mxOut = db->lookaside.nOut;
 13653       break;
 13656     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
 13657     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
 13658     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
 13659       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
 13660       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
 13661       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
 13662       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
 13663       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
 13664       *pCurrent = 0;
 13665       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
 13666       if( resetFlag ){
 13667         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
 13669       break;
 13672     /* 
 13673     ** Return an approximation for the amount of memory currently used
 13674     ** by all pagers associated with the given database connection.  The
 13675     ** highwater mark is meaningless and is returned as zero.
 13676     */
 13677     case SQLITE_DBSTATUS_CACHE_USED: {
 13678       int totalUsed = 0;
 13679       int i;
 13680       sqlite3BtreeEnterAll(db);
 13681       for(i=0; i<db->nDb; i++){
 13682         Btree *pBt = db->aDb[i].pBt;
 13683         if( pBt ){
 13684           Pager *pPager = sqlite3BtreePager(pBt);
 13685           totalUsed += sqlite3PagerMemUsed(pPager);
 13688       sqlite3BtreeLeaveAll(db);
 13689       *pCurrent = totalUsed;
 13690       *pHighwater = 0;
 13691       break;
 13694     /*
 13695     ** *pCurrent gets an accurate estimate of the amount of memory used
 13696     ** to store the schema for all databases (main, temp, and any ATTACHed
 13697     ** databases.  *pHighwater is set to zero.
 13698     */
 13699     case SQLITE_DBSTATUS_SCHEMA_USED: {
 13700       int i;                      /* Used to iterate through schemas */
 13701       int nByte = 0;              /* Used to accumulate return value */
 13703       sqlite3BtreeEnterAll(db);
 13704       db->pnBytesFreed = &nByte;
 13705       for(i=0; i<db->nDb; i++){
 13706         Schema *pSchema = db->aDb[i].pSchema;
 13707         if( ALWAYS(pSchema!=0) ){
 13708           HashElem *p;
 13710           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
 13711               pSchema->tblHash.count 
 13712             + pSchema->trigHash.count
 13713             + pSchema->idxHash.count
 13714             + pSchema->fkeyHash.count
 13715           );
 13716           nByte += sqlite3MallocSize(pSchema->tblHash.ht);
 13717           nByte += sqlite3MallocSize(pSchema->trigHash.ht);
 13718           nByte += sqlite3MallocSize(pSchema->idxHash.ht);
 13719           nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
 13721           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
 13722             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
 13724           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
 13725             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
 13729       db->pnBytesFreed = 0;
 13730       sqlite3BtreeLeaveAll(db);
 13732       *pHighwater = 0;
 13733       *pCurrent = nByte;
 13734       break;
 13737     /*
 13738     ** *pCurrent gets an accurate estimate of the amount of memory used
 13739     ** to store all prepared statements.
 13740     ** *pHighwater is set to zero.
 13741     */
 13742     case SQLITE_DBSTATUS_STMT_USED: {
 13743       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
 13744       int nByte = 0;              /* Used to accumulate return value */
 13746       db->pnBytesFreed = &nByte;
 13747       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
 13748         sqlite3VdbeClearObject(db, pVdbe);
 13749         sqlite3DbFree(db, pVdbe);
 13751       db->pnBytesFreed = 0;
 13753       *pHighwater = 0;
 13754       *pCurrent = nByte;
 13756       break;
 13759     /*
 13760     ** Set *pCurrent to the total cache hits or misses encountered by all
 13761     ** pagers the database handle is connected to. *pHighwater is always set 
 13762     ** to zero.
 13763     */
 13764     case SQLITE_DBSTATUS_CACHE_HIT:
 13765     case SQLITE_DBSTATUS_CACHE_MISS:
 13766     case SQLITE_DBSTATUS_CACHE_WRITE:{
 13767       int i;
 13768       int nRet = 0;
 13769       assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 13770       assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
 13772       for(i=0; i<db->nDb; i++){
 13773         if( db->aDb[i].pBt ){
 13774           Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
 13775           sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
 13778       *pHighwater = 0;
 13779       *pCurrent = nRet;
 13780       break;
 13783     default: {
 13784       rc = SQLITE_ERROR;
 13787   sqlite3_mutex_leave(db->mutex);
 13788   return rc;
 13791 /************** End of status.c **********************************************/
 13792 /************** Begin file date.c ********************************************/
 13793 /*
 13794 ** 2003 October 31
 13795 **
 13796 ** The author disclaims copyright to this source code.  In place of
 13797 ** a legal notice, here is a blessing:
 13798 **
 13799 **    May you do good and not evil.
 13800 **    May you find forgiveness for yourself and forgive others.
 13801 **    May you share freely, never taking more than you give.
 13802 **
 13803 *************************************************************************
 13804 ** This file contains the C functions that implement date and time
 13805 ** functions for SQLite.  
 13806 **
 13807 ** There is only one exported symbol in this file - the function
 13808 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
 13809 ** All other code has file scope.
 13810 **
 13811 ** SQLite processes all times and dates as Julian Day numbers.  The
 13812 ** dates and times are stored as the number of days since noon
 13813 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
 13814 ** calendar system. 
 13815 **
 13816 ** 1970-01-01 00:00:00 is JD 2440587.5
 13817 ** 2000-01-01 00:00:00 is JD 2451544.5
 13818 **
 13819 ** This implemention requires years to be expressed as a 4-digit number
 13820 ** which means that only dates between 0000-01-01 and 9999-12-31 can
 13821 ** be represented, even though julian day numbers allow a much wider
 13822 ** range of dates.
 13823 **
 13824 ** The Gregorian calendar system is used for all dates and times,
 13825 ** even those that predate the Gregorian calendar.  Historians usually
 13826 ** use the Julian calendar for dates prior to 1582-10-15 and for some
 13827 ** dates afterwards, depending on locale.  Beware of this difference.
 13828 **
 13829 ** The conversion algorithms are implemented based on descriptions
 13830 ** in the following text:
 13831 **
 13832 **      Jean Meeus
 13833 **      Astronomical Algorithms, 2nd Edition, 1998
 13834 **      ISBM 0-943396-61-1
 13835 **      Willmann-Bell, Inc
 13836 **      Richmond, Virginia (USA)
 13837 */
 13838 /* #include <stdlib.h> */
 13839 /* #include <assert.h> */
 13840 #include <time.h>
 13842 #ifndef SQLITE_OMIT_DATETIME_FUNCS
 13845 /*
 13846 ** A structure for holding a single date and time.
 13847 */
 13848 typedef struct DateTime DateTime;
 13849 struct DateTime {
 13850   sqlite3_int64 iJD; /* The julian day number times 86400000 */
 13851   int Y, M, D;       /* Year, month, and day */
 13852   int h, m;          /* Hour and minutes */
 13853   int tz;            /* Timezone offset in minutes */
 13854   double s;          /* Seconds */
 13855   char validYMD;     /* True (1) if Y,M,D are valid */
 13856   char validHMS;     /* True (1) if h,m,s are valid */
 13857   char validJD;      /* True (1) if iJD is valid */
 13858   char validTZ;      /* True (1) if tz is valid */
 13859 };
 13862 /*
 13863 ** Convert zDate into one or more integers.  Additional arguments
 13864 ** come in groups of 5 as follows:
 13865 **
 13866 **       N       number of digits in the integer
 13867 **       min     minimum allowed value of the integer
 13868 **       max     maximum allowed value of the integer
 13869 **       nextC   first character after the integer
 13870 **       pVal    where to write the integers value.
 13871 **
 13872 ** Conversions continue until one with nextC==0 is encountered.
 13873 ** The function returns the number of successful conversions.
 13874 */
 13875 static int getDigits(const char *zDate, ...){
 13876   va_list ap;
 13877   int val;
 13878   int N;
 13879   int min;
 13880   int max;
 13881   int nextC;
 13882   int *pVal;
 13883   int cnt = 0;
 13884   va_start(ap, zDate);
 13885   do{
 13886     N = va_arg(ap, int);
 13887     min = va_arg(ap, int);
 13888     max = va_arg(ap, int);
 13889     nextC = va_arg(ap, int);
 13890     pVal = va_arg(ap, int*);
 13891     val = 0;
 13892     while( N-- ){
 13893       if( !sqlite3Isdigit(*zDate) ){
 13894         goto end_getDigits;
 13896       val = val*10 + *zDate - '0';
 13897       zDate++;
 13899     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
 13900       goto end_getDigits;
 13902     *pVal = val;
 13903     zDate++;
 13904     cnt++;
 13905   }while( nextC );
 13906 end_getDigits:
 13907   va_end(ap);
 13908   return cnt;
 13911 /*
 13912 ** Parse a timezone extension on the end of a date-time.
 13913 ** The extension is of the form:
 13914 **
 13915 **        (+/-)HH:MM
 13916 **
 13917 ** Or the "zulu" notation:
 13918 **
 13919 **        Z
 13920 **
 13921 ** If the parse is successful, write the number of minutes
 13922 ** of change in p->tz and return 0.  If a parser error occurs,
 13923 ** return non-zero.
 13924 **
 13925 ** A missing specifier is not considered an error.
 13926 */
 13927 static int parseTimezone(const char *zDate, DateTime *p){
 13928   int sgn = 0;
 13929   int nHr, nMn;
 13930   int c;
 13931   while( sqlite3Isspace(*zDate) ){ zDate++; }
 13932   p->tz = 0;
 13933   c = *zDate;
 13934   if( c=='-' ){
 13935     sgn = -1;
 13936   }else if( c=='+' ){
 13937     sgn = +1;
 13938   }else if( c=='Z' || c=='z' ){
 13939     zDate++;
 13940     goto zulu_time;
 13941   }else{
 13942     return c!=0;
 13944   zDate++;
 13945   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
 13946     return 1;
 13948   zDate += 5;
 13949   p->tz = sgn*(nMn + nHr*60);
 13950 zulu_time:
 13951   while( sqlite3Isspace(*zDate) ){ zDate++; }
 13952   return *zDate!=0;
 13955 /*
 13956 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
 13957 ** The HH, MM, and SS must each be exactly 2 digits.  The
 13958 ** fractional seconds FFFF can be one or more digits.
 13959 **
 13960 ** Return 1 if there is a parsing error and 0 on success.
 13961 */
 13962 static int parseHhMmSs(const char *zDate, DateTime *p){
 13963   int h, m, s;
 13964   double ms = 0.0;
 13965   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
 13966     return 1;
 13968   zDate += 5;
 13969   if( *zDate==':' ){
 13970     zDate++;
 13971     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
 13972       return 1;
 13974     zDate += 2;
 13975     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
 13976       double rScale = 1.0;
 13977       zDate++;
 13978       while( sqlite3Isdigit(*zDate) ){
 13979         ms = ms*10.0 + *zDate - '0';
 13980         rScale *= 10.0;
 13981         zDate++;
 13983       ms /= rScale;
 13985   }else{
 13986     s = 0;
 13988   p->validJD = 0;
 13989   p->validHMS = 1;
 13990   p->h = h;
 13991   p->m = m;
 13992   p->s = s + ms;
 13993   if( parseTimezone(zDate, p) ) return 1;
 13994   p->validTZ = (p->tz!=0)?1:0;
 13995   return 0;
 13998 /*
 13999 ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
 14000 ** that the YYYY-MM-DD is according to the Gregorian calendar.
 14001 **
 14002 ** Reference:  Meeus page 61
 14003 */
 14004 static void computeJD(DateTime *p){
 14005   int Y, M, D, A, B, X1, X2;
 14007   if( p->validJD ) return;
 14008   if( p->validYMD ){
 14009     Y = p->Y;
 14010     M = p->M;
 14011     D = p->D;
 14012   }else{
 14013     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
 14014     M = 1;
 14015     D = 1;
 14017   if( M<=2 ){
 14018     Y--;
 14019     M += 12;
 14021   A = Y/100;
 14022   B = 2 - A + (A/4);
 14023   X1 = 36525*(Y+4716)/100;
 14024   X2 = 306001*(M+1)/10000;
 14025   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
 14026   p->validJD = 1;
 14027   if( p->validHMS ){
 14028     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
 14029     if( p->validTZ ){
 14030       p->iJD -= p->tz*60000;
 14031       p->validYMD = 0;
 14032       p->validHMS = 0;
 14033       p->validTZ = 0;
 14038 /*
 14039 ** Parse dates of the form
 14040 **
 14041 **     YYYY-MM-DD HH:MM:SS.FFF
 14042 **     YYYY-MM-DD HH:MM:SS
 14043 **     YYYY-MM-DD HH:MM
 14044 **     YYYY-MM-DD
 14045 **
 14046 ** Write the result into the DateTime structure and return 0
 14047 ** on success and 1 if the input string is not a well-formed
 14048 ** date.
 14049 */
 14050 static int parseYyyyMmDd(const char *zDate, DateTime *p){
 14051   int Y, M, D, neg;
 14053   if( zDate[0]=='-' ){
 14054     zDate++;
 14055     neg = 1;
 14056   }else{
 14057     neg = 0;
 14059   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
 14060     return 1;
 14062   zDate += 10;
 14063   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
 14064   if( parseHhMmSs(zDate, p)==0 ){
 14065     /* We got the time */
 14066   }else if( *zDate==0 ){
 14067     p->validHMS = 0;
 14068   }else{
 14069     return 1;
 14071   p->validJD = 0;
 14072   p->validYMD = 1;
 14073   p->Y = neg ? -Y : Y;
 14074   p->M = M;
 14075   p->D = D;
 14076   if( p->validTZ ){
 14077     computeJD(p);
 14079   return 0;
 14082 /*
 14083 ** Set the time to the current time reported by the VFS.
 14084 **
 14085 ** Return the number of errors.
 14086 */
 14087 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
 14088   sqlite3 *db = sqlite3_context_db_handle(context);
 14089   if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){
 14090     p->validJD = 1;
 14091     return 0;
 14092   }else{
 14093     return 1;
 14097 /*
 14098 ** Attempt to parse the given string into a Julian Day Number.  Return
 14099 ** the number of errors.
 14100 **
 14101 ** The following are acceptable forms for the input string:
 14102 **
 14103 **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
 14104 **      DDDD.DD 
 14105 **      now
 14106 **
 14107 ** In the first form, the +/-HH:MM is always optional.  The fractional
 14108 ** seconds extension (the ".FFF") is optional.  The seconds portion
 14109 ** (":SS.FFF") is option.  The year and date can be omitted as long
 14110 ** as there is a time string.  The time string can be omitted as long
 14111 ** as there is a year and date.
 14112 */
 14113 static int parseDateOrTime(
 14114   sqlite3_context *context, 
 14115   const char *zDate, 
 14116   DateTime *p
 14117 ){
 14118   double r;
 14119   if( parseYyyyMmDd(zDate,p)==0 ){
 14120     return 0;
 14121   }else if( parseHhMmSs(zDate, p)==0 ){
 14122     return 0;
 14123   }else if( sqlite3StrICmp(zDate,"now")==0){
 14124     return setDateTimeToCurrent(context, p);
 14125   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
 14126     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
 14127     p->validJD = 1;
 14128     return 0;
 14130   return 1;
 14133 /*
 14134 ** Compute the Year, Month, and Day from the julian day number.
 14135 */
 14136 static void computeYMD(DateTime *p){
 14137   int Z, A, B, C, D, E, X1;
 14138   if( p->validYMD ) return;
 14139   if( !p->validJD ){
 14140     p->Y = 2000;
 14141     p->M = 1;
 14142     p->D = 1;
 14143   }else{
 14144     Z = (int)((p->iJD + 43200000)/86400000);
 14145     A = (int)((Z - 1867216.25)/36524.25);
 14146     A = Z + 1 + A - (A/4);
 14147     B = A + 1524;
 14148     C = (int)((B - 122.1)/365.25);
 14149     D = (36525*C)/100;
 14150     E = (int)((B-D)/30.6001);
 14151     X1 = (int)(30.6001*E);
 14152     p->D = B - D - X1;
 14153     p->M = E<14 ? E-1 : E-13;
 14154     p->Y = p->M>2 ? C - 4716 : C - 4715;
 14156   p->validYMD = 1;
 14159 /*
 14160 ** Compute the Hour, Minute, and Seconds from the julian day number.
 14161 */
 14162 static void computeHMS(DateTime *p){
 14163   int s;
 14164   if( p->validHMS ) return;
 14165   computeJD(p);
 14166   s = (int)((p->iJD + 43200000) % 86400000);
 14167   p->s = s/1000.0;
 14168   s = (int)p->s;
 14169   p->s -= s;
 14170   p->h = s/3600;
 14171   s -= p->h*3600;
 14172   p->m = s/60;
 14173   p->s += s - p->m*60;
 14174   p->validHMS = 1;
 14177 /*
 14178 ** Compute both YMD and HMS
 14179 */
 14180 static void computeYMD_HMS(DateTime *p){
 14181   computeYMD(p);
 14182   computeHMS(p);
 14185 /*
 14186 ** Clear the YMD and HMS and the TZ
 14187 */
 14188 static void clearYMD_HMS_TZ(DateTime *p){
 14189   p->validYMD = 0;
 14190   p->validHMS = 0;
 14191   p->validTZ = 0;
 14194 /*
 14195 ** On recent Windows platforms, the localtime_s() function is available
 14196 ** as part of the "Secure CRT". It is essentially equivalent to 
 14197 ** localtime_r() available under most POSIX platforms, except that the 
 14198 ** order of the parameters is reversed.
 14199 **
 14200 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
 14201 **
 14202 ** If the user has not indicated to use localtime_r() or localtime_s()
 14203 ** already, check for an MSVC build environment that provides 
 14204 ** localtime_s().
 14205 */
 14206 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
 14207      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
 14208 #define HAVE_LOCALTIME_S 1
 14209 #endif
 14211 #ifndef SQLITE_OMIT_LOCALTIME
 14212 /*
 14213 ** The following routine implements the rough equivalent of localtime_r()
 14214 ** using whatever operating-system specific localtime facility that
 14215 ** is available.  This routine returns 0 on success and
 14216 ** non-zero on any kind of error.
 14217 **
 14218 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
 14219 ** routine will always fail.
 14220 */
 14221 static int osLocaltime(time_t *t, struct tm *pTm){
 14222   int rc;
 14223 #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
 14224       && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
 14225   struct tm *pX;
 14226 #if SQLITE_THREADSAFE>0
 14227   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 14228 #endif
 14229   sqlite3_mutex_enter(mutex);
 14230   pX = localtime(t);
 14231 #ifndef SQLITE_OMIT_BUILTIN_TEST
 14232   if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
 14233 #endif
 14234   if( pX ) *pTm = *pX;
 14235   sqlite3_mutex_leave(mutex);
 14236   rc = pX==0;
 14237 #else
 14238 #ifndef SQLITE_OMIT_BUILTIN_TEST
 14239   if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
 14240 #endif
 14241 #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
 14242   rc = localtime_r(t, pTm)==0;
 14243 #else
 14244   rc = localtime_s(pTm, t);
 14245 #endif /* HAVE_LOCALTIME_R */
 14246 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
 14247   return rc;
 14249 #endif /* SQLITE_OMIT_LOCALTIME */
 14252 #ifndef SQLITE_OMIT_LOCALTIME
 14253 /*
 14254 ** Compute the difference (in milliseconds) between localtime and UTC
 14255 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
 14256 ** return this value and set *pRc to SQLITE_OK. 
 14257 **
 14258 ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
 14259 ** is undefined in this case.
 14260 */
 14261 static sqlite3_int64 localtimeOffset(
 14262   DateTime *p,                    /* Date at which to calculate offset */
 14263   sqlite3_context *pCtx,          /* Write error here if one occurs */
 14264   int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
 14265 ){
 14266   DateTime x, y;
 14267   time_t t;
 14268   struct tm sLocal;
 14270   /* Initialize the contents of sLocal to avoid a compiler warning. */
 14271   memset(&sLocal, 0, sizeof(sLocal));
 14273   x = *p;
 14274   computeYMD_HMS(&x);
 14275   if( x.Y<1971 || x.Y>=2038 ){
 14276     x.Y = 2000;
 14277     x.M = 1;
 14278     x.D = 1;
 14279     x.h = 0;
 14280     x.m = 0;
 14281     x.s = 0.0;
 14282   } else {
 14283     int s = (int)(x.s + 0.5);
 14284     x.s = s;
 14286   x.tz = 0;
 14287   x.validJD = 0;
 14288   computeJD(&x);
 14289   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
 14290   if( osLocaltime(&t, &sLocal) ){
 14291     sqlite3_result_error(pCtx, "local time unavailable", -1);
 14292     *pRc = SQLITE_ERROR;
 14293     return 0;
 14295   y.Y = sLocal.tm_year + 1900;
 14296   y.M = sLocal.tm_mon + 1;
 14297   y.D = sLocal.tm_mday;
 14298   y.h = sLocal.tm_hour;
 14299   y.m = sLocal.tm_min;
 14300   y.s = sLocal.tm_sec;
 14301   y.validYMD = 1;
 14302   y.validHMS = 1;
 14303   y.validJD = 0;
 14304   y.validTZ = 0;
 14305   computeJD(&y);
 14306   *pRc = SQLITE_OK;
 14307   return y.iJD - x.iJD;
 14309 #endif /* SQLITE_OMIT_LOCALTIME */
 14311 /*
 14312 ** Process a modifier to a date-time stamp.  The modifiers are
 14313 ** as follows:
 14314 **
 14315 **     NNN days
 14316 **     NNN hours
 14317 **     NNN minutes
 14318 **     NNN.NNNN seconds
 14319 **     NNN months
 14320 **     NNN years
 14321 **     start of month
 14322 **     start of year
 14323 **     start of week
 14324 **     start of day
 14325 **     weekday N
 14326 **     unixepoch
 14327 **     localtime
 14328 **     utc
 14329 **
 14330 ** Return 0 on success and 1 if there is any kind of error. If the error
 14331 ** is in a system call (i.e. localtime()), then an error message is written
 14332 ** to context pCtx. If the error is an unrecognized modifier, no error is
 14333 ** written to pCtx.
 14334 */
 14335 static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
 14336   int rc = 1;
 14337   int n;
 14338   double r;
 14339   char *z, zBuf[30];
 14340   z = zBuf;
 14341   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
 14342     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
 14344   z[n] = 0;
 14345   switch( z[0] ){
 14346 #ifndef SQLITE_OMIT_LOCALTIME
 14347     case 'l': {
 14348       /*    localtime
 14349       **
 14350       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
 14351       ** show local time.
 14352       */
 14353       if( strcmp(z, "localtime")==0 ){
 14354         computeJD(p);
 14355         p->iJD += localtimeOffset(p, pCtx, &rc);
 14356         clearYMD_HMS_TZ(p);
 14358       break;
 14360 #endif
 14361     case 'u': {
 14362       /*
 14363       **    unixepoch
 14364       **
 14365       ** Treat the current value of p->iJD as the number of
 14366       ** seconds since 1970.  Convert to a real julian day number.
 14367       */
 14368       if( strcmp(z, "unixepoch")==0 && p->validJD ){
 14369         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
 14370         clearYMD_HMS_TZ(p);
 14371         rc = 0;
 14373 #ifndef SQLITE_OMIT_LOCALTIME
 14374       else if( strcmp(z, "utc")==0 ){
 14375         sqlite3_int64 c1;
 14376         computeJD(p);
 14377         c1 = localtimeOffset(p, pCtx, &rc);
 14378         if( rc==SQLITE_OK ){
 14379           p->iJD -= c1;
 14380           clearYMD_HMS_TZ(p);
 14381           p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
 14384 #endif
 14385       break;
 14387     case 'w': {
 14388       /*
 14389       **    weekday N
 14390       **
 14391       ** Move the date to the same time on the next occurrence of
 14392       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
 14393       ** date is already on the appropriate weekday, this is a no-op.
 14394       */
 14395       if( strncmp(z, "weekday ", 8)==0
 14396                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
 14397                && (n=(int)r)==r && n>=0 && r<7 ){
 14398         sqlite3_int64 Z;
 14399         computeYMD_HMS(p);
 14400         p->validTZ = 0;
 14401         p->validJD = 0;
 14402         computeJD(p);
 14403         Z = ((p->iJD + 129600000)/86400000) % 7;
 14404         if( Z>n ) Z -= 7;
 14405         p->iJD += (n - Z)*86400000;
 14406         clearYMD_HMS_TZ(p);
 14407         rc = 0;
 14409       break;
 14411     case 's': {
 14412       /*
 14413       **    start of TTTTT
 14414       **
 14415       ** Move the date backwards to the beginning of the current day,
 14416       ** or month or year.
 14417       */
 14418       if( strncmp(z, "start of ", 9)!=0 ) break;
 14419       z += 9;
 14420       computeYMD(p);
 14421       p->validHMS = 1;
 14422       p->h = p->m = 0;
 14423       p->s = 0.0;
 14424       p->validTZ = 0;
 14425       p->validJD = 0;
 14426       if( strcmp(z,"month")==0 ){
 14427         p->D = 1;
 14428         rc = 0;
 14429       }else if( strcmp(z,"year")==0 ){
 14430         computeYMD(p);
 14431         p->M = 1;
 14432         p->D = 1;
 14433         rc = 0;
 14434       }else if( strcmp(z,"day")==0 ){
 14435         rc = 0;
 14437       break;
 14439     case '+':
 14440     case '-':
 14441     case '0':
 14442     case '1':
 14443     case '2':
 14444     case '3':
 14445     case '4':
 14446     case '5':
 14447     case '6':
 14448     case '7':
 14449     case '8':
 14450     case '9': {
 14451       double rRounder;
 14452       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
 14453       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
 14454         rc = 1;
 14455         break;
 14457       if( z[n]==':' ){
 14458         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
 14459         ** specified number of hours, minutes, seconds, and fractional seconds
 14460         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
 14461         ** omitted.
 14462         */
 14463         const char *z2 = z;
 14464         DateTime tx;
 14465         sqlite3_int64 day;
 14466         if( !sqlite3Isdigit(*z2) ) z2++;
 14467         memset(&tx, 0, sizeof(tx));
 14468         if( parseHhMmSs(z2, &tx) ) break;
 14469         computeJD(&tx);
 14470         tx.iJD -= 43200000;
 14471         day = tx.iJD/86400000;
 14472         tx.iJD -= day*86400000;
 14473         if( z[0]=='-' ) tx.iJD = -tx.iJD;
 14474         computeJD(p);
 14475         clearYMD_HMS_TZ(p);
 14476         p->iJD += tx.iJD;
 14477         rc = 0;
 14478         break;
 14480       z += n;
 14481       while( sqlite3Isspace(*z) ) z++;
 14482       n = sqlite3Strlen30(z);
 14483       if( n>10 || n<3 ) break;
 14484       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
 14485       computeJD(p);
 14486       rc = 0;
 14487       rRounder = r<0 ? -0.5 : +0.5;
 14488       if( n==3 && strcmp(z,"day")==0 ){
 14489         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
 14490       }else if( n==4 && strcmp(z,"hour")==0 ){
 14491         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
 14492       }else if( n==6 && strcmp(z,"minute")==0 ){
 14493         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
 14494       }else if( n==6 && strcmp(z,"second")==0 ){
 14495         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
 14496       }else if( n==5 && strcmp(z,"month")==0 ){
 14497         int x, y;
 14498         computeYMD_HMS(p);
 14499         p->M += (int)r;
 14500         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
 14501         p->Y += x;
 14502         p->M -= x*12;
 14503         p->validJD = 0;
 14504         computeJD(p);
 14505         y = (int)r;
 14506         if( y!=r ){
 14507           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
 14509       }else if( n==4 && strcmp(z,"year")==0 ){
 14510         int y = (int)r;
 14511         computeYMD_HMS(p);
 14512         p->Y += y;
 14513         p->validJD = 0;
 14514         computeJD(p);
 14515         if( y!=r ){
 14516           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
 14518       }else{
 14519         rc = 1;
 14521       clearYMD_HMS_TZ(p);
 14522       break;
 14524     default: {
 14525       break;
 14528   return rc;
 14531 /*
 14532 ** Process time function arguments.  argv[0] is a date-time stamp.
 14533 ** argv[1] and following are modifiers.  Parse them all and write
 14534 ** the resulting time into the DateTime structure p.  Return 0
 14535 ** on success and 1 if there are any errors.
 14536 **
 14537 ** If there are zero parameters (if even argv[0] is undefined)
 14538 ** then assume a default value of "now" for argv[0].
 14539 */
 14540 static int isDate(
 14541   sqlite3_context *context, 
 14542   int argc, 
 14543   sqlite3_value **argv, 
 14544   DateTime *p
 14545 ){
 14546   int i;
 14547   const unsigned char *z;
 14548   int eType;
 14549   memset(p, 0, sizeof(*p));
 14550   if( argc==0 ){
 14551     return setDateTimeToCurrent(context, p);
 14553   if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
 14554                    || eType==SQLITE_INTEGER ){
 14555     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
 14556     p->validJD = 1;
 14557   }else{
 14558     z = sqlite3_value_text(argv[0]);
 14559     if( !z || parseDateOrTime(context, (char*)z, p) ){
 14560       return 1;
 14563   for(i=1; i<argc; i++){
 14564     z = sqlite3_value_text(argv[i]);
 14565     if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
 14567   return 0;
 14571 /*
 14572 ** The following routines implement the various date and time functions
 14573 ** of SQLite.
 14574 */
 14576 /*
 14577 **    julianday( TIMESTRING, MOD, MOD, ...)
 14578 **
 14579 ** Return the julian day number of the date specified in the arguments
 14580 */
 14581 static void juliandayFunc(
 14582   sqlite3_context *context,
 14583   int argc,
 14584   sqlite3_value **argv
 14585 ){
 14586   DateTime x;
 14587   if( isDate(context, argc, argv, &x)==0 ){
 14588     computeJD(&x);
 14589     sqlite3_result_double(context, x.iJD/86400000.0);
 14593 /*
 14594 **    datetime( TIMESTRING, MOD, MOD, ...)
 14595 **
 14596 ** Return YYYY-MM-DD HH:MM:SS
 14597 */
 14598 static void datetimeFunc(
 14599   sqlite3_context *context,
 14600   int argc,
 14601   sqlite3_value **argv
 14602 ){
 14603   DateTime x;
 14604   if( isDate(context, argc, argv, &x)==0 ){
 14605     char zBuf[100];
 14606     computeYMD_HMS(&x);
 14607     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
 14608                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
 14609     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 14613 /*
 14614 **    time( TIMESTRING, MOD, MOD, ...)
 14615 **
 14616 ** Return HH:MM:SS
 14617 */
 14618 static void timeFunc(
 14619   sqlite3_context *context,
 14620   int argc,
 14621   sqlite3_value **argv
 14622 ){
 14623   DateTime x;
 14624   if( isDate(context, argc, argv, &x)==0 ){
 14625     char zBuf[100];
 14626     computeHMS(&x);
 14627     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
 14628     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 14632 /*
 14633 **    date( TIMESTRING, MOD, MOD, ...)
 14634 **
 14635 ** Return YYYY-MM-DD
 14636 */
 14637 static void dateFunc(
 14638   sqlite3_context *context,
 14639   int argc,
 14640   sqlite3_value **argv
 14641 ){
 14642   DateTime x;
 14643   if( isDate(context, argc, argv, &x)==0 ){
 14644     char zBuf[100];
 14645     computeYMD(&x);
 14646     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
 14647     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 14651 /*
 14652 **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
 14653 **
 14654 ** Return a string described by FORMAT.  Conversions as follows:
 14655 **
 14656 **   %d  day of month
 14657 **   %f  ** fractional seconds  SS.SSS
 14658 **   %H  hour 00-24
 14659 **   %j  day of year 000-366
 14660 **   %J  ** Julian day number
 14661 **   %m  month 01-12
 14662 **   %M  minute 00-59
 14663 **   %s  seconds since 1970-01-01
 14664 **   %S  seconds 00-59
 14665 **   %w  day of week 0-6  sunday==0
 14666 **   %W  week of year 00-53
 14667 **   %Y  year 0000-9999
 14668 **   %%  %
 14669 */
 14670 static void strftimeFunc(
 14671   sqlite3_context *context,
 14672   int argc,
 14673   sqlite3_value **argv
 14674 ){
 14675   DateTime x;
 14676   u64 n;
 14677   size_t i,j;
 14678   char *z;
 14679   sqlite3 *db;
 14680   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
 14681   char zBuf[100];
 14682   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
 14683   db = sqlite3_context_db_handle(context);
 14684   for(i=0, n=1; zFmt[i]; i++, n++){
 14685     if( zFmt[i]=='%' ){
 14686       switch( zFmt[i+1] ){
 14687         case 'd':
 14688         case 'H':
 14689         case 'm':
 14690         case 'M':
 14691         case 'S':
 14692         case 'W':
 14693           n++;
 14694           /* fall thru */
 14695         case 'w':
 14696         case '%':
 14697           break;
 14698         case 'f':
 14699           n += 8;
 14700           break;
 14701         case 'j':
 14702           n += 3;
 14703           break;
 14704         case 'Y':
 14705           n += 8;
 14706           break;
 14707         case 's':
 14708         case 'J':
 14709           n += 50;
 14710           break;
 14711         default:
 14712           return;  /* ERROR.  return a NULL */
 14714       i++;
 14717   testcase( n==sizeof(zBuf)-1 );
 14718   testcase( n==sizeof(zBuf) );
 14719   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 14720   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
 14721   if( n<sizeof(zBuf) ){
 14722     z = zBuf;
 14723   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 14724     sqlite3_result_error_toobig(context);
 14725     return;
 14726   }else{
 14727     z = sqlite3DbMallocRaw(db, (int)n);
 14728     if( z==0 ){
 14729       sqlite3_result_error_nomem(context);
 14730       return;
 14733   computeJD(&x);
 14734   computeYMD_HMS(&x);
 14735   for(i=j=0; zFmt[i]; i++){
 14736     if( zFmt[i]!='%' ){
 14737       z[j++] = zFmt[i];
 14738     }else{
 14739       i++;
 14740       switch( zFmt[i] ){
 14741         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
 14742         case 'f': {
 14743           double s = x.s;
 14744           if( s>59.999 ) s = 59.999;
 14745           sqlite3_snprintf(7, &z[j],"%06.3f", s);
 14746           j += sqlite3Strlen30(&z[j]);
 14747           break;
 14749         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
 14750         case 'W': /* Fall thru */
 14751         case 'j': {
 14752           int nDay;             /* Number of days since 1st day of year */
 14753           DateTime y = x;
 14754           y.validJD = 0;
 14755           y.M = 1;
 14756           y.D = 1;
 14757           computeJD(&y);
 14758           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
 14759           if( zFmt[i]=='W' ){
 14760             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
 14761             wd = (int)(((x.iJD+43200000)/86400000)%7);
 14762             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
 14763             j += 2;
 14764           }else{
 14765             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
 14766             j += 3;
 14768           break;
 14770         case 'J': {
 14771           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
 14772           j+=sqlite3Strlen30(&z[j]);
 14773           break;
 14775         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
 14776         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
 14777         case 's': {
 14778           sqlite3_snprintf(30,&z[j],"%lld",
 14779                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
 14780           j += sqlite3Strlen30(&z[j]);
 14781           break;
 14783         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
 14784         case 'w': {
 14785           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
 14786           break;
 14788         case 'Y': {
 14789           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
 14790           break;
 14792         default:   z[j++] = '%'; break;
 14796   z[j] = 0;
 14797   sqlite3_result_text(context, z, -1,
 14798                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
 14801 /*
 14802 ** current_time()
 14803 **
 14804 ** This function returns the same value as time('now').
 14805 */
 14806 static void ctimeFunc(
 14807   sqlite3_context *context,
 14808   int NotUsed,
 14809   sqlite3_value **NotUsed2
 14810 ){
 14811   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 14812   timeFunc(context, 0, 0);
 14815 /*
 14816 ** current_date()
 14817 **
 14818 ** This function returns the same value as date('now').
 14819 */
 14820 static void cdateFunc(
 14821   sqlite3_context *context,
 14822   int NotUsed,
 14823   sqlite3_value **NotUsed2
 14824 ){
 14825   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 14826   dateFunc(context, 0, 0);
 14829 /*
 14830 ** current_timestamp()
 14831 **
 14832 ** This function returns the same value as datetime('now').
 14833 */
 14834 static void ctimestampFunc(
 14835   sqlite3_context *context,
 14836   int NotUsed,
 14837   sqlite3_value **NotUsed2
 14838 ){
 14839   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 14840   datetimeFunc(context, 0, 0);
 14842 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
 14844 #ifdef SQLITE_OMIT_DATETIME_FUNCS
 14845 /*
 14846 ** If the library is compiled to omit the full-scale date and time
 14847 ** handling (to get a smaller binary), the following minimal version
 14848 ** of the functions current_time(), current_date() and current_timestamp()
 14849 ** are included instead. This is to support column declarations that
 14850 ** include "DEFAULT CURRENT_TIME" etc.
 14851 **
 14852 ** This function uses the C-library functions time(), gmtime()
 14853 ** and strftime(). The format string to pass to strftime() is supplied
 14854 ** as the user-data for the function.
 14855 */
 14856 static void currentTimeFunc(
 14857   sqlite3_context *context,
 14858   int argc,
 14859   sqlite3_value **argv
 14860 ){
 14861   time_t t;
 14862   char *zFormat = (char *)sqlite3_user_data(context);
 14863   sqlite3 *db;
 14864   sqlite3_int64 iT;
 14865   struct tm *pTm;
 14866   struct tm sNow;
 14867   char zBuf[20];
 14869   UNUSED_PARAMETER(argc);
 14870   UNUSED_PARAMETER(argv);
 14872   db = sqlite3_context_db_handle(context);
 14873   if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return;
 14874   t = iT/1000 - 10000*(sqlite3_int64)21086676;
 14875 #ifdef HAVE_GMTIME_R
 14876   pTm = gmtime_r(&t, &sNow);
 14877 #else
 14878   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 14879   pTm = gmtime(&t);
 14880   if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
 14881   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 14882 #endif
 14883   if( pTm ){
 14884     strftime(zBuf, 20, zFormat, &sNow);
 14885     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 14888 #endif
 14890 /*
 14891 ** This function registered all of the above C functions as SQL
 14892 ** functions.  This should be the only routine in this file with
 14893 ** external linkage.
 14894 */
 14895 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
 14896   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
 14897 #ifndef SQLITE_OMIT_DATETIME_FUNCS
 14898     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
 14899     FUNCTION(date,             -1, 0, 0, dateFunc      ),
 14900     FUNCTION(time,             -1, 0, 0, timeFunc      ),
 14901     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
 14902     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
 14903     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
 14904     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
 14905     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
 14906 #else
 14907     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
 14908     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
 14909     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
 14910 #endif
 14911   };
 14912   int i;
 14913   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 14914   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
 14916   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
 14917     sqlite3FuncDefInsert(pHash, &aFunc[i]);
 14921 /************** End of date.c ************************************************/
 14922 /************** Begin file os.c **********************************************/
 14923 /*
 14924 ** 2005 November 29
 14925 **
 14926 ** The author disclaims copyright to this source code.  In place of
 14927 ** a legal notice, here is a blessing:
 14928 **
 14929 **    May you do good and not evil.
 14930 **    May you find forgiveness for yourself and forgive others.
 14931 **    May you share freely, never taking more than you give.
 14932 **
 14933 ******************************************************************************
 14934 **
 14935 ** This file contains OS interface code that is common to all
 14936 ** architectures.
 14937 */
 14938 #define _SQLITE_OS_C_ 1
 14939 #undef _SQLITE_OS_C_
 14941 /*
 14942 ** The default SQLite sqlite3_vfs implementations do not allocate
 14943 ** memory (actually, os_unix.c allocates a small amount of memory
 14944 ** from within OsOpen()), but some third-party implementations may.
 14945 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
 14946 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
 14947 **
 14948 ** The following functions are instrumented for malloc() failure 
 14949 ** testing:
 14950 **
 14951 **     sqlite3OsRead()
 14952 **     sqlite3OsWrite()
 14953 **     sqlite3OsSync()
 14954 **     sqlite3OsFileSize()
 14955 **     sqlite3OsLock()
 14956 **     sqlite3OsCheckReservedLock()
 14957 **     sqlite3OsFileControl()
 14958 **     sqlite3OsShmMap()
 14959 **     sqlite3OsOpen()
 14960 **     sqlite3OsDelete()
 14961 **     sqlite3OsAccess()
 14962 **     sqlite3OsFullPathname()
 14963 **
 14964 */
 14965 #if defined(SQLITE_TEST)
 14966 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
 14967   #define DO_OS_MALLOC_TEST(x)                                       \
 14968   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
 14969     void *pTstAlloc = sqlite3Malloc(10);                             \
 14970     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
 14971     sqlite3_free(pTstAlloc);                                         \
 14973 #else
 14974   #define DO_OS_MALLOC_TEST(x)
 14975 #endif
 14977 /*
 14978 ** The following routines are convenience wrappers around methods
 14979 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
 14980 ** of this would be completely automatic if SQLite were coded using
 14981 ** C++ instead of plain old C.
 14982 */
 14983 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
 14984   int rc = SQLITE_OK;
 14985   if( pId->pMethods ){
 14986     rc = pId->pMethods->xClose(pId);
 14987     pId->pMethods = 0;
 14989   return rc;
 14991 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
 14992   DO_OS_MALLOC_TEST(id);
 14993   return id->pMethods->xRead(id, pBuf, amt, offset);
 14995 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
 14996   DO_OS_MALLOC_TEST(id);
 14997   return id->pMethods->xWrite(id, pBuf, amt, offset);
 14999 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
 15000   return id->pMethods->xTruncate(id, size);
 15002 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
 15003   DO_OS_MALLOC_TEST(id);
 15004   return id->pMethods->xSync(id, flags);
 15006 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
 15007   DO_OS_MALLOC_TEST(id);
 15008   return id->pMethods->xFileSize(id, pSize);
 15010 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
 15011   DO_OS_MALLOC_TEST(id);
 15012   return id->pMethods->xLock(id, lockType);
 15014 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
 15015   return id->pMethods->xUnlock(id, lockType);
 15017 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
 15018   DO_OS_MALLOC_TEST(id);
 15019   return id->pMethods->xCheckReservedLock(id, pResOut);
 15022 /*
 15023 ** Use sqlite3OsFileControl() when we are doing something that might fail
 15024 ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
 15025 ** when simply tossing information over the wall to the VFS and we do not
 15026 ** really care if the VFS receives and understands the information since it
 15027 ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
 15028 ** routine has no return value since the return value would be meaningless.
 15029 */
 15030 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
 15031   DO_OS_MALLOC_TEST(id);
 15032   return id->pMethods->xFileControl(id, op, pArg);
 15034 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
 15035   (void)id->pMethods->xFileControl(id, op, pArg);
 15038 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
 15039   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
 15040   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
 15042 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
 15043   return id->pMethods->xDeviceCharacteristics(id);
 15045 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
 15046   return id->pMethods->xShmLock(id, offset, n, flags);
 15048 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
 15049   id->pMethods->xShmBarrier(id);
 15051 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
 15052   return id->pMethods->xShmUnmap(id, deleteFlag);
 15054 SQLITE_PRIVATE int sqlite3OsShmMap(
 15055   sqlite3_file *id,               /* Database file handle */
 15056   int iPage,
 15057   int pgsz,
 15058   int bExtend,                    /* True to extend file if necessary */
 15059   void volatile **pp              /* OUT: Pointer to mapping */
 15060 ){
 15061   DO_OS_MALLOC_TEST(id);
 15062   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
 15065 /*
 15066 ** The next group of routines are convenience wrappers around the
 15067 ** VFS methods.
 15068 */
 15069 SQLITE_PRIVATE int sqlite3OsOpen(
 15070   sqlite3_vfs *pVfs, 
 15071   const char *zPath, 
 15072   sqlite3_file *pFile, 
 15073   int flags, 
 15074   int *pFlagsOut
 15075 ){
 15076   int rc;
 15077   DO_OS_MALLOC_TEST(0);
 15078   /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
 15079   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
 15080   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
 15081   ** reaching the VFS. */
 15082   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
 15083   assert( rc==SQLITE_OK || pFile->pMethods==0 );
 15084   return rc;
 15086 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
 15087   DO_OS_MALLOC_TEST(0);
 15088   assert( dirSync==0 || dirSync==1 );
 15089   return pVfs->xDelete(pVfs, zPath, dirSync);
 15091 SQLITE_PRIVATE int sqlite3OsAccess(
 15092   sqlite3_vfs *pVfs, 
 15093   const char *zPath, 
 15094   int flags, 
 15095   int *pResOut
 15096 ){
 15097   DO_OS_MALLOC_TEST(0);
 15098   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
 15100 SQLITE_PRIVATE int sqlite3OsFullPathname(
 15101   sqlite3_vfs *pVfs, 
 15102   const char *zPath, 
 15103   int nPathOut, 
 15104   char *zPathOut
 15105 ){
 15106   DO_OS_MALLOC_TEST(0);
 15107   zPathOut[0] = 0;
 15108   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
 15110 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 15111 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
 15112   return pVfs->xDlOpen(pVfs, zPath);
 15114 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
 15115   pVfs->xDlError(pVfs, nByte, zBufOut);
 15117 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
 15118   return pVfs->xDlSym(pVfs, pHdle, zSym);
 15120 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
 15121   pVfs->xDlClose(pVfs, pHandle);
 15123 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
 15124 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
 15125   return pVfs->xRandomness(pVfs, nByte, zBufOut);
 15127 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
 15128   return pVfs->xSleep(pVfs, nMicro);
 15130 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
 15131   int rc;
 15132   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
 15133   ** method to get the current date and time if that method is available
 15134   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
 15135   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
 15136   ** unavailable.
 15137   */
 15138   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
 15139     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
 15140   }else{
 15141     double r;
 15142     rc = pVfs->xCurrentTime(pVfs, &r);
 15143     *pTimeOut = (sqlite3_int64)(r*86400000.0);
 15145   return rc;
 15148 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
 15149   sqlite3_vfs *pVfs, 
 15150   const char *zFile, 
 15151   sqlite3_file **ppFile, 
 15152   int flags,
 15153   int *pOutFlags
 15154 ){
 15155   int rc = SQLITE_NOMEM;
 15156   sqlite3_file *pFile;
 15157   pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
 15158   if( pFile ){
 15159     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
 15160     if( rc!=SQLITE_OK ){
 15161       sqlite3_free(pFile);
 15162     }else{
 15163       *ppFile = pFile;
 15166   return rc;
 15168 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
 15169   int rc = SQLITE_OK;
 15170   assert( pFile );
 15171   rc = sqlite3OsClose(pFile);
 15172   sqlite3_free(pFile);
 15173   return rc;
 15176 /*
 15177 ** This function is a wrapper around the OS specific implementation of
 15178 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
 15179 ** ability to simulate a malloc failure, so that the handling of an
 15180 ** error in sqlite3_os_init() by the upper layers can be tested.
 15181 */
 15182 SQLITE_PRIVATE int sqlite3OsInit(void){
 15183   void *p = sqlite3_malloc(10);
 15184   if( p==0 ) return SQLITE_NOMEM;
 15185   sqlite3_free(p);
 15186   return sqlite3_os_init();
 15189 /*
 15190 ** The list of all registered VFS implementations.
 15191 */
 15192 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
 15193 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
 15195 /*
 15196 ** Locate a VFS by name.  If no name is given, simply return the
 15197 ** first VFS on the list.
 15198 */
 15199 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
 15200   sqlite3_vfs *pVfs = 0;
 15201 #if SQLITE_THREADSAFE
 15202   sqlite3_mutex *mutex;
 15203 #endif
 15204 #ifndef SQLITE_OMIT_AUTOINIT
 15205   int rc = sqlite3_initialize();
 15206   if( rc ) return 0;
 15207 #endif
 15208 #if SQLITE_THREADSAFE
 15209   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 15210 #endif
 15211   sqlite3_mutex_enter(mutex);
 15212   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
 15213     if( zVfs==0 ) break;
 15214     if( strcmp(zVfs, pVfs->zName)==0 ) break;
 15216   sqlite3_mutex_leave(mutex);
 15217   return pVfs;
 15220 /*
 15221 ** Unlink a VFS from the linked list
 15222 */
 15223 static void vfsUnlink(sqlite3_vfs *pVfs){
 15224   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
 15225   if( pVfs==0 ){
 15226     /* No-op */
 15227   }else if( vfsList==pVfs ){
 15228     vfsList = pVfs->pNext;
 15229   }else if( vfsList ){
 15230     sqlite3_vfs *p = vfsList;
 15231     while( p->pNext && p->pNext!=pVfs ){
 15232       p = p->pNext;
 15234     if( p->pNext==pVfs ){
 15235       p->pNext = pVfs->pNext;
 15240 /*
 15241 ** Register a VFS with the system.  It is harmless to register the same
 15242 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
 15243 ** true.
 15244 */
 15245 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
 15246   MUTEX_LOGIC(sqlite3_mutex *mutex;)
 15247 #ifndef SQLITE_OMIT_AUTOINIT
 15248   int rc = sqlite3_initialize();
 15249   if( rc ) return rc;
 15250 #endif
 15251   MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 15252   sqlite3_mutex_enter(mutex);
 15253   vfsUnlink(pVfs);
 15254   if( makeDflt || vfsList==0 ){
 15255     pVfs->pNext = vfsList;
 15256     vfsList = pVfs;
 15257   }else{
 15258     pVfs->pNext = vfsList->pNext;
 15259     vfsList->pNext = pVfs;
 15261   assert(vfsList);
 15262   sqlite3_mutex_leave(mutex);
 15263   return SQLITE_OK;
 15266 /*
 15267 ** Unregister a VFS so that it is no longer accessible.
 15268 */
 15269 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
 15270 #if SQLITE_THREADSAFE
 15271   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 15272 #endif
 15273   sqlite3_mutex_enter(mutex);
 15274   vfsUnlink(pVfs);
 15275   sqlite3_mutex_leave(mutex);
 15276   return SQLITE_OK;
 15279 /************** End of os.c **************************************************/
 15280 /************** Begin file fault.c *******************************************/
 15281 /*
 15282 ** 2008 Jan 22
 15283 **
 15284 ** The author disclaims copyright to this source code.  In place of
 15285 ** a legal notice, here is a blessing:
 15286 **
 15287 **    May you do good and not evil.
 15288 **    May you find forgiveness for yourself and forgive others.
 15289 **    May you share freely, never taking more than you give.
 15290 **
 15291 *************************************************************************
 15292 **
 15293 ** This file contains code to support the concept of "benign" 
 15294 ** malloc failures (when the xMalloc() or xRealloc() method of the
 15295 ** sqlite3_mem_methods structure fails to allocate a block of memory
 15296 ** and returns 0). 
 15297 **
 15298 ** Most malloc failures are non-benign. After they occur, SQLite
 15299 ** abandons the current operation and returns an error code (usually
 15300 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
 15301 ** fatal. For example, if a malloc fails while resizing a hash table, this 
 15302 ** is completely recoverable simply by not carrying out the resize. The 
 15303 ** hash table will continue to function normally.  So a malloc failure 
 15304 ** during a hash table resize is a benign fault.
 15305 */
 15308 #ifndef SQLITE_OMIT_BUILTIN_TEST
 15310 /*
 15311 ** Global variables.
 15312 */
 15313 typedef struct BenignMallocHooks BenignMallocHooks;
 15314 static SQLITE_WSD struct BenignMallocHooks {
 15315   void (*xBenignBegin)(void);
 15316   void (*xBenignEnd)(void);
 15317 } sqlite3Hooks = { 0, 0 };
 15319 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
 15320 ** structure.  If writable static data is unsupported on the target,
 15321 ** we have to locate the state vector at run-time.  In the more common
 15322 ** case where writable static data is supported, wsdHooks can refer directly
 15323 ** to the "sqlite3Hooks" state vector declared above.
 15324 */
 15325 #ifdef SQLITE_OMIT_WSD
 15326 # define wsdHooksInit \
 15327   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
 15328 # define wsdHooks x[0]
 15329 #else
 15330 # define wsdHooksInit
 15331 # define wsdHooks sqlite3Hooks
 15332 #endif
 15335 /*
 15336 ** Register hooks to call when sqlite3BeginBenignMalloc() and
 15337 ** sqlite3EndBenignMalloc() are called, respectively.
 15338 */
 15339 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
 15340   void (*xBenignBegin)(void),
 15341   void (*xBenignEnd)(void)
 15342 ){
 15343   wsdHooksInit;
 15344   wsdHooks.xBenignBegin = xBenignBegin;
 15345   wsdHooks.xBenignEnd = xBenignEnd;
 15348 /*
 15349 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
 15350 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
 15351 ** indicates that subsequent malloc failures are non-benign.
 15352 */
 15353 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
 15354   wsdHooksInit;
 15355   if( wsdHooks.xBenignBegin ){
 15356     wsdHooks.xBenignBegin();
 15359 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
 15360   wsdHooksInit;
 15361   if( wsdHooks.xBenignEnd ){
 15362     wsdHooks.xBenignEnd();
 15366 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
 15368 /************** End of fault.c ***********************************************/
 15369 /************** Begin file mem0.c ********************************************/
 15370 /*
 15371 ** 2008 October 28
 15372 **
 15373 ** The author disclaims copyright to this source code.  In place of
 15374 ** a legal notice, here is a blessing:
 15375 **
 15376 **    May you do good and not evil.
 15377 **    May you find forgiveness for yourself and forgive others.
 15378 **    May you share freely, never taking more than you give.
 15379 **
 15380 *************************************************************************
 15381 **
 15382 ** This file contains a no-op memory allocation drivers for use when
 15383 ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
 15384 ** here always fail.  SQLite will not operate with these drivers.  These
 15385 ** are merely placeholders.  Real drivers must be substituted using
 15386 ** sqlite3_config() before SQLite will operate.
 15387 */
 15389 /*
 15390 ** This version of the memory allocator is the default.  It is
 15391 ** used when no other memory allocator is specified using compile-time
 15392 ** macros.
 15393 */
 15394 #ifdef SQLITE_ZERO_MALLOC
 15396 /*
 15397 ** No-op versions of all memory allocation routines
 15398 */
 15399 static void *sqlite3MemMalloc(int nByte){ return 0; }
 15400 static void sqlite3MemFree(void *pPrior){ return; }
 15401 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
 15402 static int sqlite3MemSize(void *pPrior){ return 0; }
 15403 static int sqlite3MemRoundup(int n){ return n; }
 15404 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
 15405 static void sqlite3MemShutdown(void *NotUsed){ return; }
 15407 /*
 15408 ** This routine is the only routine in this file with external linkage.
 15409 **
 15410 ** Populate the low-level memory allocation function pointers in
 15411 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
 15412 */
 15413 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 15414   static const sqlite3_mem_methods defaultMethods = {
 15415      sqlite3MemMalloc,
 15416      sqlite3MemFree,
 15417      sqlite3MemRealloc,
 15418      sqlite3MemSize,
 15419      sqlite3MemRoundup,
 15420      sqlite3MemInit,
 15421      sqlite3MemShutdown,
 15423   };
 15424   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 15427 #endif /* SQLITE_ZERO_MALLOC */
 15429 /************** End of mem0.c ************************************************/
 15430 /************** Begin file mem1.c ********************************************/
 15431 /*
 15432 ** 2007 August 14
 15433 **
 15434 ** The author disclaims copyright to this source code.  In place of
 15435 ** a legal notice, here is a blessing:
 15436 **
 15437 **    May you do good and not evil.
 15438 **    May you find forgiveness for yourself and forgive others.
 15439 **    May you share freely, never taking more than you give.
 15440 **
 15441 *************************************************************************
 15442 **
 15443 ** This file contains low-level memory allocation drivers for when
 15444 ** SQLite will use the standard C-library malloc/realloc/free interface
 15445 ** to obtain the memory it needs.
 15446 **
 15447 ** This file contains implementations of the low-level memory allocation
 15448 ** routines specified in the sqlite3_mem_methods object.  The content of
 15449 ** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
 15450 ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
 15451 ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
 15452 ** default configuration is to use memory allocation routines in this
 15453 ** file.
 15454 **
 15455 ** C-preprocessor macro summary:
 15456 **
 15457 **    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
 15458 **                                the malloc_usable_size() interface exists
 15459 **                                on the target platform.  Or, this symbol
 15460 **                                can be set manually, if desired.
 15461 **                                If an equivalent interface exists by
 15462 **                                a different name, using a separate -D
 15463 **                                option to rename it.
 15464 **
 15465 **    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
 15466 **                                memory allocator.  Set this symbol to enable
 15467 **                                building on older macs.
 15468 **
 15469 **    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
 15470 **                                _msize() on windows systems.  This might
 15471 **                                be necessary when compiling for Delphi,
 15472 **                                for example.
 15473 */
 15475 /*
 15476 ** This version of the memory allocator is the default.  It is
 15477 ** used when no other memory allocator is specified using compile-time
 15478 ** macros.
 15479 */
 15480 #ifdef SQLITE_SYSTEM_MALLOC
 15482 /*
 15483 ** The MSVCRT has malloc_usable_size() but it is called _msize().
 15484 ** The use of _msize() is automatic, but can be disabled by compiling
 15485 ** with -DSQLITE_WITHOUT_MSIZE
 15486 */
 15487 #if defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
 15488 # define SQLITE_MALLOCSIZE _msize
 15489 #endif
 15491 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
 15493 /*
 15494 ** Use the zone allocator available on apple products unless the
 15495 ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
 15496 */
 15497 #include <sys/sysctl.h>
 15498 #include <malloc/malloc.h>
 15499 #include <libkern/OSAtomic.h>
 15500 static malloc_zone_t* _sqliteZone_;
 15501 #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
 15502 #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
 15503 #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
 15504 #define SQLITE_MALLOCSIZE(x) \
 15505         (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
 15507 #else /* if not __APPLE__ */
 15509 /*
 15510 ** Use standard C library malloc and free on non-Apple systems.  
 15511 ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
 15512 */
 15513 #define SQLITE_MALLOC(x)    malloc(x)
 15514 #define SQLITE_FREE(x)      free(x)
 15515 #define SQLITE_REALLOC(x,y) realloc((x),(y))
 15517 #if (defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)) \
 15518       || (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE))
 15519 # include <malloc.h>    /* Needed for malloc_usable_size on linux */
 15520 #endif
 15521 #ifdef HAVE_MALLOC_USABLE_SIZE
 15522 # ifndef SQLITE_MALLOCSIZE
 15523 #  define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
 15524 # endif
 15525 #else
 15526 # undef SQLITE_MALLOCSIZE
 15527 #endif
 15529 #endif /* __APPLE__ or not __APPLE__ */
 15531 /*
 15532 ** Like malloc(), but remember the size of the allocation
 15533 ** so that we can find it later using sqlite3MemSize().
 15534 **
 15535 ** For this low-level routine, we are guaranteed that nByte>0 because
 15536 ** cases of nByte<=0 will be intercepted and dealt with by higher level
 15537 ** routines.
 15538 */
 15539 static void *sqlite3MemMalloc(int nByte){
 15540 #ifdef SQLITE_MALLOCSIZE
 15541   void *p = SQLITE_MALLOC( nByte );
 15542   if( p==0 ){
 15543     testcase( sqlite3GlobalConfig.xLog!=0 );
 15544     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 15546   return p;
 15547 #else
 15548   sqlite3_int64 *p;
 15549   assert( nByte>0 );
 15550   nByte = ROUND8(nByte);
 15551   p = SQLITE_MALLOC( nByte+8 );
 15552   if( p ){
 15553     p[0] = nByte;
 15554     p++;
 15555   }else{
 15556     testcase( sqlite3GlobalConfig.xLog!=0 );
 15557     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 15559   return (void *)p;
 15560 #endif
 15563 /*
 15564 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
 15565 ** or sqlite3MemRealloc().
 15566 **
 15567 ** For this low-level routine, we already know that pPrior!=0 since
 15568 ** cases where pPrior==0 will have been intecepted and dealt with
 15569 ** by higher-level routines.
 15570 */
 15571 static void sqlite3MemFree(void *pPrior){
 15572 #ifdef SQLITE_MALLOCSIZE
 15573   SQLITE_FREE(pPrior);
 15574 #else
 15575   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
 15576   assert( pPrior!=0 );
 15577   p--;
 15578   SQLITE_FREE(p);
 15579 #endif
 15582 /*
 15583 ** Report the allocated size of a prior return from xMalloc()
 15584 ** or xRealloc().
 15585 */
 15586 static int sqlite3MemSize(void *pPrior){
 15587 #ifdef SQLITE_MALLOCSIZE
 15588   return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
 15589 #else
 15590   sqlite3_int64 *p;
 15591   if( pPrior==0 ) return 0;
 15592   p = (sqlite3_int64*)pPrior;
 15593   p--;
 15594   return (int)p[0];
 15595 #endif
 15598 /*
 15599 ** Like realloc().  Resize an allocation previously obtained from
 15600 ** sqlite3MemMalloc().
 15601 **
 15602 ** For this low-level interface, we know that pPrior!=0.  Cases where
 15603 ** pPrior==0 while have been intercepted by higher-level routine and
 15604 ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
 15605 ** cases where nByte<=0 will have been intercepted by higher-level
 15606 ** routines and redirected to xFree.
 15607 */
 15608 static void *sqlite3MemRealloc(void *pPrior, int nByte){
 15609 #ifdef SQLITE_MALLOCSIZE
 15610   void *p = SQLITE_REALLOC(pPrior, nByte);
 15611   if( p==0 ){
 15612     testcase( sqlite3GlobalConfig.xLog!=0 );
 15613     sqlite3_log(SQLITE_NOMEM,
 15614       "failed memory resize %u to %u bytes",
 15615       SQLITE_MALLOCSIZE(pPrior), nByte);
 15617   return p;
 15618 #else
 15619   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
 15620   assert( pPrior!=0 && nByte>0 );
 15621   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
 15622   p--;
 15623   p = SQLITE_REALLOC(p, nByte+8 );
 15624   if( p ){
 15625     p[0] = nByte;
 15626     p++;
 15627   }else{
 15628     testcase( sqlite3GlobalConfig.xLog!=0 );
 15629     sqlite3_log(SQLITE_NOMEM,
 15630       "failed memory resize %u to %u bytes",
 15631       sqlite3MemSize(pPrior), nByte);
 15633   return (void*)p;
 15634 #endif
 15637 /*
 15638 ** Round up a request size to the next valid allocation size.
 15639 */
 15640 static int sqlite3MemRoundup(int n){
 15641   return ROUND8(n);
 15644 /*
 15645 ** Initialize this module.
 15646 */
 15647 static int sqlite3MemInit(void *NotUsed){
 15648 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
 15649   int cpuCount;
 15650   size_t len;
 15651   if( _sqliteZone_ ){
 15652     return SQLITE_OK;
 15654   len = sizeof(cpuCount);
 15655   /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
 15656   sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
 15657   if( cpuCount>1 ){
 15658     /* defer MT decisions to system malloc */
 15659     _sqliteZone_ = malloc_default_zone();
 15660   }else{
 15661     /* only 1 core, use our own zone to contention over global locks, 
 15662     ** e.g. we have our own dedicated locks */
 15663     bool success;
 15664     malloc_zone_t* newzone = malloc_create_zone(4096, 0);
 15665     malloc_set_zone_name(newzone, "Sqlite_Heap");
 15666     do{
 15667       success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
 15668                                  (void * volatile *)&_sqliteZone_);
 15669     }while(!_sqliteZone_);
 15670     if( !success ){
 15671       /* somebody registered a zone first */
 15672       malloc_destroy_zone(newzone);
 15675 #endif
 15676   UNUSED_PARAMETER(NotUsed);
 15677   return SQLITE_OK;
 15680 /*
 15681 ** Deinitialize this module.
 15682 */
 15683 static void sqlite3MemShutdown(void *NotUsed){
 15684   UNUSED_PARAMETER(NotUsed);
 15685   return;
 15688 /*
 15689 ** This routine is the only routine in this file with external linkage.
 15690 **
 15691 ** Populate the low-level memory allocation function pointers in
 15692 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
 15693 */
 15694 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 15695   static const sqlite3_mem_methods defaultMethods = {
 15696      sqlite3MemMalloc,
 15697      sqlite3MemFree,
 15698      sqlite3MemRealloc,
 15699      sqlite3MemSize,
 15700      sqlite3MemRoundup,
 15701      sqlite3MemInit,
 15702      sqlite3MemShutdown,
 15704   };
 15705   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 15708 #endif /* SQLITE_SYSTEM_MALLOC */
 15710 /************** End of mem1.c ************************************************/
 15711 /************** Begin file mem2.c ********************************************/
 15712 /*
 15713 ** 2007 August 15
 15714 **
 15715 ** The author disclaims copyright to this source code.  In place of
 15716 ** a legal notice, here is a blessing:
 15717 **
 15718 **    May you do good and not evil.
 15719 **    May you find forgiveness for yourself and forgive others.
 15720 **    May you share freely, never taking more than you give.
 15721 **
 15722 *************************************************************************
 15723 **
 15724 ** This file contains low-level memory allocation drivers for when
 15725 ** SQLite will use the standard C-library malloc/realloc/free interface
 15726 ** to obtain the memory it needs while adding lots of additional debugging
 15727 ** information to each allocation in order to help detect and fix memory
 15728 ** leaks and memory usage errors.
 15729 **
 15730 ** This file contains implementations of the low-level memory allocation
 15731 ** routines specified in the sqlite3_mem_methods object.
 15732 */
 15734 /*
 15735 ** This version of the memory allocator is used only if the
 15736 ** SQLITE_MEMDEBUG macro is defined
 15737 */
 15738 #ifdef SQLITE_MEMDEBUG
 15740 /*
 15741 ** The backtrace functionality is only available with GLIBC
 15742 */
 15743 #ifdef __GLIBC__
 15744   extern int backtrace(void**,int);
 15745   extern void backtrace_symbols_fd(void*const*,int,int);
 15746 #else
 15747 # define backtrace(A,B) 1
 15748 # define backtrace_symbols_fd(A,B,C)
 15749 #endif
 15750 /* #include <stdio.h> */
 15752 /*
 15753 ** Each memory allocation looks like this:
 15754 **
 15755 **  ------------------------------------------------------------------------
 15756 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
 15757 **  ------------------------------------------------------------------------
 15758 **
 15759 ** The application code sees only a pointer to the allocation.  We have
 15760 ** to back up from the allocation pointer to find the MemBlockHdr.  The
 15761 ** MemBlockHdr tells us the size of the allocation and the number of
 15762 ** backtrace pointers.  There is also a guard word at the end of the
 15763 ** MemBlockHdr.
 15764 */
 15765 struct MemBlockHdr {
 15766   i64 iSize;                          /* Size of this allocation */
 15767   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
 15768   char nBacktrace;                    /* Number of backtraces on this alloc */
 15769   char nBacktraceSlots;               /* Available backtrace slots */
 15770   u8 nTitle;                          /* Bytes of title; includes '\0' */
 15771   u8 eType;                           /* Allocation type code */
 15772   int iForeGuard;                     /* Guard word for sanity */
 15773 };
 15775 /*
 15776 ** Guard words
 15777 */
 15778 #define FOREGUARD 0x80F5E153
 15779 #define REARGUARD 0xE4676B53
 15781 /*
 15782 ** Number of malloc size increments to track.
 15783 */
 15784 #define NCSIZE  1000
 15786 /*
 15787 ** All of the static variables used by this module are collected
 15788 ** into a single structure named "mem".  This is to keep the
 15789 ** static variables organized and to reduce namespace pollution
 15790 ** when this module is combined with other in the amalgamation.
 15791 */
 15792 static struct {
 15794   /*
 15795   ** Mutex to control access to the memory allocation subsystem.
 15796   */
 15797   sqlite3_mutex *mutex;
 15799   /*
 15800   ** Head and tail of a linked list of all outstanding allocations
 15801   */
 15802   struct MemBlockHdr *pFirst;
 15803   struct MemBlockHdr *pLast;
 15805   /*
 15806   ** The number of levels of backtrace to save in new allocations.
 15807   */
 15808   int nBacktrace;
 15809   void (*xBacktrace)(int, int, void **);
 15811   /*
 15812   ** Title text to insert in front of each block
 15813   */
 15814   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
 15815   char zTitle[100];  /* The title text */
 15817   /* 
 15818   ** sqlite3MallocDisallow() increments the following counter.
 15819   ** sqlite3MallocAllow() decrements it.
 15820   */
 15821   int disallow; /* Do not allow memory allocation */
 15823   /*
 15824   ** Gather statistics on the sizes of memory allocations.
 15825   ** nAlloc[i] is the number of allocation attempts of i*8
 15826   ** bytes.  i==NCSIZE is the number of allocation attempts for
 15827   ** sizes more than NCSIZE*8 bytes.
 15828   */
 15829   int nAlloc[NCSIZE];      /* Total number of allocations */
 15830   int nCurrent[NCSIZE];    /* Current number of allocations */
 15831   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
 15833 } mem;
 15836 /*
 15837 ** Adjust memory usage statistics
 15838 */
 15839 static void adjustStats(int iSize, int increment){
 15840   int i = ROUND8(iSize)/8;
 15841   if( i>NCSIZE-1 ){
 15842     i = NCSIZE - 1;
 15844   if( increment>0 ){
 15845     mem.nAlloc[i]++;
 15846     mem.nCurrent[i]++;
 15847     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
 15848       mem.mxCurrent[i] = mem.nCurrent[i];
 15850   }else{
 15851     mem.nCurrent[i]--;
 15852     assert( mem.nCurrent[i]>=0 );
 15856 /*
 15857 ** Given an allocation, find the MemBlockHdr for that allocation.
 15858 **
 15859 ** This routine checks the guards at either end of the allocation and
 15860 ** if they are incorrect it asserts.
 15861 */
 15862 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
 15863   struct MemBlockHdr *p;
 15864   int *pInt;
 15865   u8 *pU8;
 15866   int nReserve;
 15868   p = (struct MemBlockHdr*)pAllocation;
 15869   p--;
 15870   assert( p->iForeGuard==(int)FOREGUARD );
 15871   nReserve = ROUND8(p->iSize);
 15872   pInt = (int*)pAllocation;
 15873   pU8 = (u8*)pAllocation;
 15874   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
 15875   /* This checks any of the "extra" bytes allocated due
 15876   ** to rounding up to an 8 byte boundary to ensure 
 15877   ** they haven't been overwritten.
 15878   */
 15879   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
 15880   return p;
 15883 /*
 15884 ** Return the number of bytes currently allocated at address p.
 15885 */
 15886 static int sqlite3MemSize(void *p){
 15887   struct MemBlockHdr *pHdr;
 15888   if( !p ){
 15889     return 0;
 15891   pHdr = sqlite3MemsysGetHeader(p);
 15892   return pHdr->iSize;
 15895 /*
 15896 ** Initialize the memory allocation subsystem.
 15897 */
 15898 static int sqlite3MemInit(void *NotUsed){
 15899   UNUSED_PARAMETER(NotUsed);
 15900   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
 15901   if( !sqlite3GlobalConfig.bMemstat ){
 15902     /* If memory status is enabled, then the malloc.c wrapper will already
 15903     ** hold the STATIC_MEM mutex when the routines here are invoked. */
 15904     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 15906   return SQLITE_OK;
 15909 /*
 15910 ** Deinitialize the memory allocation subsystem.
 15911 */
 15912 static void sqlite3MemShutdown(void *NotUsed){
 15913   UNUSED_PARAMETER(NotUsed);
 15914   mem.mutex = 0;
 15917 /*
 15918 ** Round up a request size to the next valid allocation size.
 15919 */
 15920 static int sqlite3MemRoundup(int n){
 15921   return ROUND8(n);
 15924 /*
 15925 ** Fill a buffer with pseudo-random bytes.  This is used to preset
 15926 ** the content of a new memory allocation to unpredictable values and
 15927 ** to clear the content of a freed allocation to unpredictable values.
 15928 */
 15929 static void randomFill(char *pBuf, int nByte){
 15930   unsigned int x, y, r;
 15931   x = SQLITE_PTR_TO_INT(pBuf);
 15932   y = nByte | 1;
 15933   while( nByte >= 4 ){
 15934     x = (x>>1) ^ (-(x&1) & 0xd0000001);
 15935     y = y*1103515245 + 12345;
 15936     r = x ^ y;
 15937     *(int*)pBuf = r;
 15938     pBuf += 4;
 15939     nByte -= 4;
 15941   while( nByte-- > 0 ){
 15942     x = (x>>1) ^ (-(x&1) & 0xd0000001);
 15943     y = y*1103515245 + 12345;
 15944     r = x ^ y;
 15945     *(pBuf++) = r & 0xff;
 15949 /*
 15950 ** Allocate nByte bytes of memory.
 15951 */
 15952 static void *sqlite3MemMalloc(int nByte){
 15953   struct MemBlockHdr *pHdr;
 15954   void **pBt;
 15955   char *z;
 15956   int *pInt;
 15957   void *p = 0;
 15958   int totalSize;
 15959   int nReserve;
 15960   sqlite3_mutex_enter(mem.mutex);
 15961   assert( mem.disallow==0 );
 15962   nReserve = ROUND8(nByte);
 15963   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
 15964                mem.nBacktrace*sizeof(void*) + mem.nTitle;
 15965   p = malloc(totalSize);
 15966   if( p ){
 15967     z = p;
 15968     pBt = (void**)&z[mem.nTitle];
 15969     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
 15970     pHdr->pNext = 0;
 15971     pHdr->pPrev = mem.pLast;
 15972     if( mem.pLast ){
 15973       mem.pLast->pNext = pHdr;
 15974     }else{
 15975       mem.pFirst = pHdr;
 15977     mem.pLast = pHdr;
 15978     pHdr->iForeGuard = FOREGUARD;
 15979     pHdr->eType = MEMTYPE_HEAP;
 15980     pHdr->nBacktraceSlots = mem.nBacktrace;
 15981     pHdr->nTitle = mem.nTitle;
 15982     if( mem.nBacktrace ){
 15983       void *aAddr[40];
 15984       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
 15985       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
 15986       assert(pBt[0]);
 15987       if( mem.xBacktrace ){
 15988         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
 15990     }else{
 15991       pHdr->nBacktrace = 0;
 15993     if( mem.nTitle ){
 15994       memcpy(z, mem.zTitle, mem.nTitle);
 15996     pHdr->iSize = nByte;
 15997     adjustStats(nByte, +1);
 15998     pInt = (int*)&pHdr[1];
 15999     pInt[nReserve/sizeof(int)] = REARGUARD;
 16000     randomFill((char*)pInt, nByte);
 16001     memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
 16002     p = (void*)pInt;
 16004   sqlite3_mutex_leave(mem.mutex);
 16005   return p; 
 16008 /*
 16009 ** Free memory.
 16010 */
 16011 static void sqlite3MemFree(void *pPrior){
 16012   struct MemBlockHdr *pHdr;
 16013   void **pBt;
 16014   char *z;
 16015   assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 
 16016        || mem.mutex!=0 );
 16017   pHdr = sqlite3MemsysGetHeader(pPrior);
 16018   pBt = (void**)pHdr;
 16019   pBt -= pHdr->nBacktraceSlots;
 16020   sqlite3_mutex_enter(mem.mutex);
 16021   if( pHdr->pPrev ){
 16022     assert( pHdr->pPrev->pNext==pHdr );
 16023     pHdr->pPrev->pNext = pHdr->pNext;
 16024   }else{
 16025     assert( mem.pFirst==pHdr );
 16026     mem.pFirst = pHdr->pNext;
 16028   if( pHdr->pNext ){
 16029     assert( pHdr->pNext->pPrev==pHdr );
 16030     pHdr->pNext->pPrev = pHdr->pPrev;
 16031   }else{
 16032     assert( mem.pLast==pHdr );
 16033     mem.pLast = pHdr->pPrev;
 16035   z = (char*)pBt;
 16036   z -= pHdr->nTitle;
 16037   adjustStats(pHdr->iSize, -1);
 16038   randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
 16039                 pHdr->iSize + sizeof(int) + pHdr->nTitle);
 16040   free(z);
 16041   sqlite3_mutex_leave(mem.mutex);  
 16044 /*
 16045 ** Change the size of an existing memory allocation.
 16046 **
 16047 ** For this debugging implementation, we *always* make a copy of the
 16048 ** allocation into a new place in memory.  In this way, if the 
 16049 ** higher level code is using pointer to the old allocation, it is 
 16050 ** much more likely to break and we are much more liking to find
 16051 ** the error.
 16052 */
 16053 static void *sqlite3MemRealloc(void *pPrior, int nByte){
 16054   struct MemBlockHdr *pOldHdr;
 16055   void *pNew;
 16056   assert( mem.disallow==0 );
 16057   assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
 16058   pOldHdr = sqlite3MemsysGetHeader(pPrior);
 16059   pNew = sqlite3MemMalloc(nByte);
 16060   if( pNew ){
 16061     memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
 16062     if( nByte>pOldHdr->iSize ){
 16063       randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
 16065     sqlite3MemFree(pPrior);
 16067   return pNew;
 16070 /*
 16071 ** Populate the low-level memory allocation function pointers in
 16072 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
 16073 */
 16074 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 16075   static const sqlite3_mem_methods defaultMethods = {
 16076      sqlite3MemMalloc,
 16077      sqlite3MemFree,
 16078      sqlite3MemRealloc,
 16079      sqlite3MemSize,
 16080      sqlite3MemRoundup,
 16081      sqlite3MemInit,
 16082      sqlite3MemShutdown,
 16084   };
 16085   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 16088 /*
 16089 ** Set the "type" of an allocation.
 16090 */
 16091 SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
 16092   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 16093     struct MemBlockHdr *pHdr;
 16094     pHdr = sqlite3MemsysGetHeader(p);
 16095     assert( pHdr->iForeGuard==FOREGUARD );
 16096     pHdr->eType = eType;
 16100 /*
 16101 ** Return TRUE if the mask of type in eType matches the type of the
 16102 ** allocation p.  Also return true if p==NULL.
 16103 **
 16104 ** This routine is designed for use within an assert() statement, to
 16105 ** verify the type of an allocation.  For example:
 16106 **
 16107 **     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 16108 */
 16109 SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
 16110   int rc = 1;
 16111   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 16112     struct MemBlockHdr *pHdr;
 16113     pHdr = sqlite3MemsysGetHeader(p);
 16114     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
 16115     if( (pHdr->eType&eType)==0 ){
 16116       rc = 0;
 16119   return rc;
 16122 /*
 16123 ** Return TRUE if the mask of type in eType matches no bits of the type of the
 16124 ** allocation p.  Also return true if p==NULL.
 16125 **
 16126 ** This routine is designed for use within an assert() statement, to
 16127 ** verify the type of an allocation.  For example:
 16128 **
 16129 **     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 16130 */
 16131 SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
 16132   int rc = 1;
 16133   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 16134     struct MemBlockHdr *pHdr;
 16135     pHdr = sqlite3MemsysGetHeader(p);
 16136     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
 16137     if( (pHdr->eType&eType)!=0 ){
 16138       rc = 0;
 16141   return rc;
 16144 /*
 16145 ** Set the number of backtrace levels kept for each allocation.
 16146 ** A value of zero turns off backtracing.  The number is always rounded
 16147 ** up to a multiple of 2.
 16148 */
 16149 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
 16150   if( depth<0 ){ depth = 0; }
 16151   if( depth>20 ){ depth = 20; }
 16152   depth = (depth+1)&0xfe;
 16153   mem.nBacktrace = depth;
 16156 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
 16157   mem.xBacktrace = xBacktrace;
 16160 /*
 16161 ** Set the title string for subsequent allocations.
 16162 */
 16163 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
 16164   unsigned int n = sqlite3Strlen30(zTitle) + 1;
 16165   sqlite3_mutex_enter(mem.mutex);
 16166   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
 16167   memcpy(mem.zTitle, zTitle, n);
 16168   mem.zTitle[n] = 0;
 16169   mem.nTitle = ROUND8(n);
 16170   sqlite3_mutex_leave(mem.mutex);
 16173 SQLITE_PRIVATE void sqlite3MemdebugSync(){
 16174   struct MemBlockHdr *pHdr;
 16175   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
 16176     void **pBt = (void**)pHdr;
 16177     pBt -= pHdr->nBacktraceSlots;
 16178     mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
 16182 /*
 16183 ** Open the file indicated and write a log of all unfreed memory 
 16184 ** allocations into that log.
 16185 */
 16186 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
 16187   FILE *out;
 16188   struct MemBlockHdr *pHdr;
 16189   void **pBt;
 16190   int i;
 16191   out = fopen(zFilename, "w");
 16192   if( out==0 ){
 16193     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 16194                     zFilename);
 16195     return;
 16197   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
 16198     char *z = (char*)pHdr;
 16199     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
 16200     fprintf(out, "**** %lld bytes at %p from %s ****\n", 
 16201             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
 16202     if( pHdr->nBacktrace ){
 16203       fflush(out);
 16204       pBt = (void**)pHdr;
 16205       pBt -= pHdr->nBacktraceSlots;
 16206       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
 16207       fprintf(out, "\n");
 16210   fprintf(out, "COUNTS:\n");
 16211   for(i=0; i<NCSIZE-1; i++){
 16212     if( mem.nAlloc[i] ){
 16213       fprintf(out, "   %5d: %10d %10d %10d\n", 
 16214             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
 16217   if( mem.nAlloc[NCSIZE-1] ){
 16218     fprintf(out, "   %5d: %10d %10d %10d\n",
 16219              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
 16220              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
 16222   fclose(out);
 16225 /*
 16226 ** Return the number of times sqlite3MemMalloc() has been called.
 16227 */
 16228 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
 16229   int i;
 16230   int nTotal = 0;
 16231   for(i=0; i<NCSIZE; i++){
 16232     nTotal += mem.nAlloc[i];
 16234   return nTotal;
 16238 #endif /* SQLITE_MEMDEBUG */
 16240 /************** End of mem2.c ************************************************/
 16241 /************** Begin file mem3.c ********************************************/
 16242 /*
 16243 ** 2007 October 14
 16244 **
 16245 ** The author disclaims copyright to this source code.  In place of
 16246 ** a legal notice, here is a blessing:
 16247 **
 16248 **    May you do good and not evil.
 16249 **    May you find forgiveness for yourself and forgive others.
 16250 **    May you share freely, never taking more than you give.
 16251 **
 16252 *************************************************************************
 16253 ** This file contains the C functions that implement a memory
 16254 ** allocation subsystem for use by SQLite. 
 16255 **
 16256 ** This version of the memory allocation subsystem omits all
 16257 ** use of malloc(). The SQLite user supplies a block of memory
 16258 ** before calling sqlite3_initialize() from which allocations
 16259 ** are made and returned by the xMalloc() and xRealloc() 
 16260 ** implementations. Once sqlite3_initialize() has been called,
 16261 ** the amount of memory available to SQLite is fixed and cannot
 16262 ** be changed.
 16263 **
 16264 ** This version of the memory allocation subsystem is included
 16265 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
 16266 */
 16268 /*
 16269 ** This version of the memory allocator is only built into the library
 16270 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
 16271 ** mean that the library will use a memory-pool by default, just that
 16272 ** it is available. The mempool allocator is activated by calling
 16273 ** sqlite3_config().
 16274 */
 16275 #ifdef SQLITE_ENABLE_MEMSYS3
 16277 /*
 16278 ** Maximum size (in Mem3Blocks) of a "small" chunk.
 16279 */
 16280 #define MX_SMALL 10
 16283 /*
 16284 ** Number of freelist hash slots
 16285 */
 16286 #define N_HASH  61
 16288 /*
 16289 ** A memory allocation (also called a "chunk") consists of two or 
 16290 ** more blocks where each block is 8 bytes.  The first 8 bytes are 
 16291 ** a header that is not returned to the user.
 16292 **
 16293 ** A chunk is two or more blocks that is either checked out or
 16294 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
 16295 ** size of the allocation in blocks if the allocation is free.
 16296 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
 16297 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
 16298 ** is true if the previous chunk is checked out and false if the
 16299 ** previous chunk is free.  The u.hdr.prevSize field is the size of
 16300 ** the previous chunk in blocks if the previous chunk is on the
 16301 ** freelist. If the previous chunk is checked out, then
 16302 ** u.hdr.prevSize can be part of the data for that chunk and should
 16303 ** not be read or written.
 16304 **
 16305 ** We often identify a chunk by its index in mem3.aPool[].  When
 16306 ** this is done, the chunk index refers to the second block of
 16307 ** the chunk.  In this way, the first chunk has an index of 1.
 16308 ** A chunk index of 0 means "no such chunk" and is the equivalent
 16309 ** of a NULL pointer.
 16310 **
 16311 ** The second block of free chunks is of the form u.list.  The
 16312 ** two fields form a double-linked list of chunks of related sizes.
 16313 ** Pointers to the head of the list are stored in mem3.aiSmall[] 
 16314 ** for smaller chunks and mem3.aiHash[] for larger chunks.
 16315 **
 16316 ** The second block of a chunk is user data if the chunk is checked 
 16317 ** out.  If a chunk is checked out, the user data may extend into
 16318 ** the u.hdr.prevSize value of the following chunk.
 16319 */
 16320 typedef struct Mem3Block Mem3Block;
 16321 struct Mem3Block {
 16322   union {
 16323     struct {
 16324       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
 16325       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
 16326     } hdr;
 16327     struct {
 16328       u32 next;       /* Index in mem3.aPool[] of next free chunk */
 16329       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
 16330     } list;
 16331   } u;
 16332 };
 16334 /*
 16335 ** All of the static variables used by this module are collected
 16336 ** into a single structure named "mem3".  This is to keep the
 16337 ** static variables organized and to reduce namespace pollution
 16338 ** when this module is combined with other in the amalgamation.
 16339 */
 16340 static SQLITE_WSD struct Mem3Global {
 16341   /*
 16342   ** Memory available for allocation. nPool is the size of the array
 16343   ** (in Mem3Blocks) pointed to by aPool less 2.
 16344   */
 16345   u32 nPool;
 16346   Mem3Block *aPool;
 16348   /*
 16349   ** True if we are evaluating an out-of-memory callback.
 16350   */
 16351   int alarmBusy;
 16353   /*
 16354   ** Mutex to control access to the memory allocation subsystem.
 16355   */
 16356   sqlite3_mutex *mutex;
 16358   /*
 16359   ** The minimum amount of free space that we have seen.
 16360   */
 16361   u32 mnMaster;
 16363   /*
 16364   ** iMaster is the index of the master chunk.  Most new allocations
 16365   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
 16366   ** of the current master.  iMaster is 0 if there is not master chunk.
 16367   ** The master chunk is not in either the aiHash[] or aiSmall[].
 16368   */
 16369   u32 iMaster;
 16370   u32 szMaster;
 16372   /*
 16373   ** Array of lists of free blocks according to the block size 
 16374   ** for smaller chunks, or a hash on the block size for larger
 16375   ** chunks.
 16376   */
 16377   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
 16378   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
 16379 } mem3 = { 97535575 };
 16381 #define mem3 GLOBAL(struct Mem3Global, mem3)
 16383 /*
 16384 ** Unlink the chunk at mem3.aPool[i] from list it is currently
 16385 ** on.  *pRoot is the list that i is a member of.
 16386 */
 16387 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
 16388   u32 next = mem3.aPool[i].u.list.next;
 16389   u32 prev = mem3.aPool[i].u.list.prev;
 16390   assert( sqlite3_mutex_held(mem3.mutex) );
 16391   if( prev==0 ){
 16392     *pRoot = next;
 16393   }else{
 16394     mem3.aPool[prev].u.list.next = next;
 16396   if( next ){
 16397     mem3.aPool[next].u.list.prev = prev;
 16399   mem3.aPool[i].u.list.next = 0;
 16400   mem3.aPool[i].u.list.prev = 0;
 16403 /*
 16404 ** Unlink the chunk at index i from 
 16405 ** whatever list is currently a member of.
 16406 */
 16407 static void memsys3Unlink(u32 i){
 16408   u32 size, hash;
 16409   assert( sqlite3_mutex_held(mem3.mutex) );
 16410   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
 16411   assert( i>=1 );
 16412   size = mem3.aPool[i-1].u.hdr.size4x/4;
 16413   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
 16414   assert( size>=2 );
 16415   if( size <= MX_SMALL ){
 16416     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
 16417   }else{
 16418     hash = size % N_HASH;
 16419     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
 16423 /*
 16424 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
 16425 ** at *pRoot.
 16426 */
 16427 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
 16428   assert( sqlite3_mutex_held(mem3.mutex) );
 16429   mem3.aPool[i].u.list.next = *pRoot;
 16430   mem3.aPool[i].u.list.prev = 0;
 16431   if( *pRoot ){
 16432     mem3.aPool[*pRoot].u.list.prev = i;
 16434   *pRoot = i;
 16437 /*
 16438 ** Link the chunk at index i into either the appropriate
 16439 ** small chunk list, or into the large chunk hash table.
 16440 */
 16441 static void memsys3Link(u32 i){
 16442   u32 size, hash;
 16443   assert( sqlite3_mutex_held(mem3.mutex) );
 16444   assert( i>=1 );
 16445   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
 16446   size = mem3.aPool[i-1].u.hdr.size4x/4;
 16447   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
 16448   assert( size>=2 );
 16449   if( size <= MX_SMALL ){
 16450     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
 16451   }else{
 16452     hash = size % N_HASH;
 16453     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
 16457 /*
 16458 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
 16459 ** will already be held (obtained by code in malloc.c) if
 16460 ** sqlite3GlobalConfig.bMemStat is true.
 16461 */
 16462 static void memsys3Enter(void){
 16463   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
 16464     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 16466   sqlite3_mutex_enter(mem3.mutex);
 16468 static void memsys3Leave(void){
 16469   sqlite3_mutex_leave(mem3.mutex);
 16472 /*
 16473 ** Called when we are unable to satisfy an allocation of nBytes.
 16474 */
 16475 static void memsys3OutOfMemory(int nByte){
 16476   if( !mem3.alarmBusy ){
 16477     mem3.alarmBusy = 1;
 16478     assert( sqlite3_mutex_held(mem3.mutex) );
 16479     sqlite3_mutex_leave(mem3.mutex);
 16480     sqlite3_release_memory(nByte);
 16481     sqlite3_mutex_enter(mem3.mutex);
 16482     mem3.alarmBusy = 0;
 16487 /*
 16488 ** Chunk i is a free chunk that has been unlinked.  Adjust its 
 16489 ** size parameters for check-out and return a pointer to the 
 16490 ** user portion of the chunk.
 16491 */
 16492 static void *memsys3Checkout(u32 i, u32 nBlock){
 16493   u32 x;
 16494   assert( sqlite3_mutex_held(mem3.mutex) );
 16495   assert( i>=1 );
 16496   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
 16497   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
 16498   x = mem3.aPool[i-1].u.hdr.size4x;
 16499   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
 16500   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
 16501   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
 16502   return &mem3.aPool[i];
 16505 /*
 16506 ** Carve a piece off of the end of the mem3.iMaster free chunk.
 16507 ** Return a pointer to the new allocation.  Or, if the master chunk
 16508 ** is not large enough, return 0.
 16509 */
 16510 static void *memsys3FromMaster(u32 nBlock){
 16511   assert( sqlite3_mutex_held(mem3.mutex) );
 16512   assert( mem3.szMaster>=nBlock );
 16513   if( nBlock>=mem3.szMaster-1 ){
 16514     /* Use the entire master */
 16515     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
 16516     mem3.iMaster = 0;
 16517     mem3.szMaster = 0;
 16518     mem3.mnMaster = 0;
 16519     return p;
 16520   }else{
 16521     /* Split the master block.  Return the tail. */
 16522     u32 newi, x;
 16523     newi = mem3.iMaster + mem3.szMaster - nBlock;
 16524     assert( newi > mem3.iMaster+1 );
 16525     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
 16526     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
 16527     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
 16528     mem3.szMaster -= nBlock;
 16529     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
 16530     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 16531     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 16532     if( mem3.szMaster < mem3.mnMaster ){
 16533       mem3.mnMaster = mem3.szMaster;
 16535     return (void*)&mem3.aPool[newi];
 16539 /*
 16540 ** *pRoot is the head of a list of free chunks of the same size
 16541 ** or same size hash.  In other words, *pRoot is an entry in either
 16542 ** mem3.aiSmall[] or mem3.aiHash[].  
 16543 **
 16544 ** This routine examines all entries on the given list and tries
 16545 ** to coalesce each entries with adjacent free chunks.  
 16546 **
 16547 ** If it sees a chunk that is larger than mem3.iMaster, it replaces 
 16548 ** the current mem3.iMaster with the new larger chunk.  In order for
 16549 ** this mem3.iMaster replacement to work, the master chunk must be
 16550 ** linked into the hash tables.  That is not the normal state of
 16551 ** affairs, of course.  The calling routine must link the master
 16552 ** chunk before invoking this routine, then must unlink the (possibly
 16553 ** changed) master chunk once this routine has finished.
 16554 */
 16555 static void memsys3Merge(u32 *pRoot){
 16556   u32 iNext, prev, size, i, x;
 16558   assert( sqlite3_mutex_held(mem3.mutex) );
 16559   for(i=*pRoot; i>0; i=iNext){
 16560     iNext = mem3.aPool[i].u.list.next;
 16561     size = mem3.aPool[i-1].u.hdr.size4x;
 16562     assert( (size&1)==0 );
 16563     if( (size&2)==0 ){
 16564       memsys3UnlinkFromList(i, pRoot);
 16565       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
 16566       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
 16567       if( prev==iNext ){
 16568         iNext = mem3.aPool[prev].u.list.next;
 16570       memsys3Unlink(prev);
 16571       size = i + size/4 - prev;
 16572       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
 16573       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
 16574       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
 16575       memsys3Link(prev);
 16576       i = prev;
 16577     }else{
 16578       size /= 4;
 16580     if( size>mem3.szMaster ){
 16581       mem3.iMaster = i;
 16582       mem3.szMaster = size;
 16587 /*
 16588 ** Return a block of memory of at least nBytes in size.
 16589 ** Return NULL if unable.
 16590 **
 16591 ** This function assumes that the necessary mutexes, if any, are
 16592 ** already held by the caller. Hence "Unsafe".
 16593 */
 16594 static void *memsys3MallocUnsafe(int nByte){
 16595   u32 i;
 16596   u32 nBlock;
 16597   u32 toFree;
 16599   assert( sqlite3_mutex_held(mem3.mutex) );
 16600   assert( sizeof(Mem3Block)==8 );
 16601   if( nByte<=12 ){
 16602     nBlock = 2;
 16603   }else{
 16604     nBlock = (nByte + 11)/8;
 16606   assert( nBlock>=2 );
 16608   /* STEP 1:
 16609   ** Look for an entry of the correct size in either the small
 16610   ** chunk table or in the large chunk hash table.  This is
 16611   ** successful most of the time (about 9 times out of 10).
 16612   */
 16613   if( nBlock <= MX_SMALL ){
 16614     i = mem3.aiSmall[nBlock-2];
 16615     if( i>0 ){
 16616       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
 16617       return memsys3Checkout(i, nBlock);
 16619   }else{
 16620     int hash = nBlock % N_HASH;
 16621     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
 16622       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
 16623         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
 16624         return memsys3Checkout(i, nBlock);
 16629   /* STEP 2:
 16630   ** Try to satisfy the allocation by carving a piece off of the end
 16631   ** of the master chunk.  This step usually works if step 1 fails.
 16632   */
 16633   if( mem3.szMaster>=nBlock ){
 16634     return memsys3FromMaster(nBlock);
 16638   /* STEP 3:  
 16639   ** Loop through the entire memory pool.  Coalesce adjacent free
 16640   ** chunks.  Recompute the master chunk as the largest free chunk.
 16641   ** Then try again to satisfy the allocation by carving a piece off
 16642   ** of the end of the master chunk.  This step happens very
 16643   ** rarely (we hope!)
 16644   */
 16645   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
 16646     memsys3OutOfMemory(toFree);
 16647     if( mem3.iMaster ){
 16648       memsys3Link(mem3.iMaster);
 16649       mem3.iMaster = 0;
 16650       mem3.szMaster = 0;
 16652     for(i=0; i<N_HASH; i++){
 16653       memsys3Merge(&mem3.aiHash[i]);
 16655     for(i=0; i<MX_SMALL-1; i++){
 16656       memsys3Merge(&mem3.aiSmall[i]);
 16658     if( mem3.szMaster ){
 16659       memsys3Unlink(mem3.iMaster);
 16660       if( mem3.szMaster>=nBlock ){
 16661         return memsys3FromMaster(nBlock);
 16666   /* If none of the above worked, then we fail. */
 16667   return 0;
 16670 /*
 16671 ** Free an outstanding memory allocation.
 16672 **
 16673 ** This function assumes that the necessary mutexes, if any, are
 16674 ** already held by the caller. Hence "Unsafe".
 16675 */
 16676 static void memsys3FreeUnsafe(void *pOld){
 16677   Mem3Block *p = (Mem3Block*)pOld;
 16678   int i;
 16679   u32 size, x;
 16680   assert( sqlite3_mutex_held(mem3.mutex) );
 16681   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
 16682   i = p - mem3.aPool;
 16683   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
 16684   size = mem3.aPool[i-1].u.hdr.size4x/4;
 16685   assert( i+size<=mem3.nPool+1 );
 16686   mem3.aPool[i-1].u.hdr.size4x &= ~1;
 16687   mem3.aPool[i+size-1].u.hdr.prevSize = size;
 16688   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
 16689   memsys3Link(i);
 16691   /* Try to expand the master using the newly freed chunk */
 16692   if( mem3.iMaster ){
 16693     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
 16694       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
 16695       mem3.iMaster -= size;
 16696       mem3.szMaster += size;
 16697       memsys3Unlink(mem3.iMaster);
 16698       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 16699       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 16700       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
 16702     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 16703     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
 16704       memsys3Unlink(mem3.iMaster+mem3.szMaster);
 16705       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
 16706       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 16707       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
 16712 /*
 16713 ** Return the size of an outstanding allocation, in bytes.  The
 16714 ** size returned omits the 8-byte header overhead.  This only
 16715 ** works for chunks that are currently checked out.
 16716 */
 16717 static int memsys3Size(void *p){
 16718   Mem3Block *pBlock;
 16719   if( p==0 ) return 0;
 16720   pBlock = (Mem3Block*)p;
 16721   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
 16722   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
 16725 /*
 16726 ** Round up a request size to the next valid allocation size.
 16727 */
 16728 static int memsys3Roundup(int n){
 16729   if( n<=12 ){
 16730     return 12;
 16731   }else{
 16732     return ((n+11)&~7) - 4;
 16736 /*
 16737 ** Allocate nBytes of memory.
 16738 */
 16739 static void *memsys3Malloc(int nBytes){
 16740   sqlite3_int64 *p;
 16741   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
 16742   memsys3Enter();
 16743   p = memsys3MallocUnsafe(nBytes);
 16744   memsys3Leave();
 16745   return (void*)p; 
 16748 /*
 16749 ** Free memory.
 16750 */
 16751 static void memsys3Free(void *pPrior){
 16752   assert( pPrior );
 16753   memsys3Enter();
 16754   memsys3FreeUnsafe(pPrior);
 16755   memsys3Leave();
 16758 /*
 16759 ** Change the size of an existing memory allocation
 16760 */
 16761 static void *memsys3Realloc(void *pPrior, int nBytes){
 16762   int nOld;
 16763   void *p;
 16764   if( pPrior==0 ){
 16765     return sqlite3_malloc(nBytes);
 16767   if( nBytes<=0 ){
 16768     sqlite3_free(pPrior);
 16769     return 0;
 16771   nOld = memsys3Size(pPrior);
 16772   if( nBytes<=nOld && nBytes>=nOld-128 ){
 16773     return pPrior;
 16775   memsys3Enter();
 16776   p = memsys3MallocUnsafe(nBytes);
 16777   if( p ){
 16778     if( nOld<nBytes ){
 16779       memcpy(p, pPrior, nOld);
 16780     }else{
 16781       memcpy(p, pPrior, nBytes);
 16783     memsys3FreeUnsafe(pPrior);
 16785   memsys3Leave();
 16786   return p;
 16789 /*
 16790 ** Initialize this module.
 16791 */
 16792 static int memsys3Init(void *NotUsed){
 16793   UNUSED_PARAMETER(NotUsed);
 16794   if( !sqlite3GlobalConfig.pHeap ){
 16795     return SQLITE_ERROR;
 16798   /* Store a pointer to the memory block in global structure mem3. */
 16799   assert( sizeof(Mem3Block)==8 );
 16800   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
 16801   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
 16803   /* Initialize the master block. */
 16804   mem3.szMaster = mem3.nPool;
 16805   mem3.mnMaster = mem3.szMaster;
 16806   mem3.iMaster = 1;
 16807   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
 16808   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
 16809   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
 16811   return SQLITE_OK;
 16814 /*
 16815 ** Deinitialize this module.
 16816 */
 16817 static void memsys3Shutdown(void *NotUsed){
 16818   UNUSED_PARAMETER(NotUsed);
 16819   mem3.mutex = 0;
 16820   return;
 16825 /*
 16826 ** Open the file indicated and write a log of all unfreed memory 
 16827 ** allocations into that log.
 16828 */
 16829 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
 16830 #ifdef SQLITE_DEBUG
 16831   FILE *out;
 16832   u32 i, j;
 16833   u32 size;
 16834   if( zFilename==0 || zFilename[0]==0 ){
 16835     out = stdout;
 16836   }else{
 16837     out = fopen(zFilename, "w");
 16838     if( out==0 ){
 16839       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 16840                       zFilename);
 16841       return;
 16844   memsys3Enter();
 16845   fprintf(out, "CHUNKS:\n");
 16846   for(i=1; i<=mem3.nPool; i+=size/4){
 16847     size = mem3.aPool[i-1].u.hdr.size4x;
 16848     if( size/4<=1 ){
 16849       fprintf(out, "%p size error\n", &mem3.aPool[i]);
 16850       assert( 0 );
 16851       break;
 16853     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
 16854       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
 16855       assert( 0 );
 16856       break;
 16858     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
 16859       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
 16860       assert( 0 );
 16861       break;
 16863     if( size&1 ){
 16864       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
 16865     }else{
 16866       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
 16867                   i==mem3.iMaster ? " **master**" : "");
 16870   for(i=0; i<MX_SMALL-1; i++){
 16871     if( mem3.aiSmall[i]==0 ) continue;
 16872     fprintf(out, "small(%2d):", i);
 16873     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
 16874       fprintf(out, " %p(%d)", &mem3.aPool[j],
 16875               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
 16877     fprintf(out, "\n"); 
 16879   for(i=0; i<N_HASH; i++){
 16880     if( mem3.aiHash[i]==0 ) continue;
 16881     fprintf(out, "hash(%2d):", i);
 16882     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
 16883       fprintf(out, " %p(%d)", &mem3.aPool[j],
 16884               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
 16886     fprintf(out, "\n"); 
 16888   fprintf(out, "master=%d\n", mem3.iMaster);
 16889   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
 16890   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
 16891   sqlite3_mutex_leave(mem3.mutex);
 16892   if( out==stdout ){
 16893     fflush(stdout);
 16894   }else{
 16895     fclose(out);
 16897 #else
 16898   UNUSED_PARAMETER(zFilename);
 16899 #endif
 16902 /*
 16903 ** This routine is the only routine in this file with external 
 16904 ** linkage.
 16905 **
 16906 ** Populate the low-level memory allocation function pointers in
 16907 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
 16908 ** arguments specify the block of memory to manage.
 16909 **
 16910 ** This routine is only called by sqlite3_config(), and therefore
 16911 ** is not required to be threadsafe (it is not).
 16912 */
 16913 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
 16914   static const sqlite3_mem_methods mempoolMethods = {
 16915      memsys3Malloc,
 16916      memsys3Free,
 16917      memsys3Realloc,
 16918      memsys3Size,
 16919      memsys3Roundup,
 16920      memsys3Init,
 16921      memsys3Shutdown,
 16923   };
 16924   return &mempoolMethods;
 16927 #endif /* SQLITE_ENABLE_MEMSYS3 */
 16929 /************** End of mem3.c ************************************************/
 16930 /************** Begin file mem5.c ********************************************/
 16931 /*
 16932 ** 2007 October 14
 16933 **
 16934 ** The author disclaims copyright to this source code.  In place of
 16935 ** a legal notice, here is a blessing:
 16936 **
 16937 **    May you do good and not evil.
 16938 **    May you find forgiveness for yourself and forgive others.
 16939 **    May you share freely, never taking more than you give.
 16940 **
 16941 *************************************************************************
 16942 ** This file contains the C functions that implement a memory
 16943 ** allocation subsystem for use by SQLite. 
 16944 **
 16945 ** This version of the memory allocation subsystem omits all
 16946 ** use of malloc(). The application gives SQLite a block of memory
 16947 ** before calling sqlite3_initialize() from which allocations
 16948 ** are made and returned by the xMalloc() and xRealloc() 
 16949 ** implementations. Once sqlite3_initialize() has been called,
 16950 ** the amount of memory available to SQLite is fixed and cannot
 16951 ** be changed.
 16952 **
 16953 ** This version of the memory allocation subsystem is included
 16954 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
 16955 **
 16956 ** This memory allocator uses the following algorithm:
 16957 **
 16958 **   1.  All memory allocations sizes are rounded up to a power of 2.
 16959 **
 16960 **   2.  If two adjacent free blocks are the halves of a larger block,
 16961 **       then the two blocks are coalesed into the single larger block.
 16962 **
 16963 **   3.  New memory is allocated from the first available free block.
 16964 **
 16965 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
 16966 ** Concerning Dynamic Storage Allocation". Journal of the Association for
 16967 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
 16968 ** 
 16969 ** Let n be the size of the largest allocation divided by the minimum
 16970 ** allocation size (after rounding all sizes up to a power of 2.)  Let M
 16971 ** be the maximum amount of memory ever outstanding at one time.  Let
 16972 ** N be the total amount of memory available for allocation.  Robson
 16973 ** proved that this memory allocator will never breakdown due to 
 16974 ** fragmentation as long as the following constraint holds:
 16975 **
 16976 **      N >=  M*(1 + log2(n)/2) - n + 1
 16977 **
 16978 ** The sqlite3_status() logic tracks the maximum values of n and M so
 16979 ** that an application can, at any time, verify this constraint.
 16980 */
 16982 /*
 16983 ** This version of the memory allocator is used only when 
 16984 ** SQLITE_ENABLE_MEMSYS5 is defined.
 16985 */
 16986 #ifdef SQLITE_ENABLE_MEMSYS5
 16988 /*
 16989 ** A minimum allocation is an instance of the following structure.
 16990 ** Larger allocations are an array of these structures where the
 16991 ** size of the array is a power of 2.
 16992 **
 16993 ** The size of this object must be a power of two.  That fact is
 16994 ** verified in memsys5Init().
 16995 */
 16996 typedef struct Mem5Link Mem5Link;
 16997 struct Mem5Link {
 16998   int next;       /* Index of next free chunk */
 16999   int prev;       /* Index of previous free chunk */
 17000 };
 17002 /*
 17003 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
 17004 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
 17005 ** it is not actually possible to reach this limit.
 17006 */
 17007 #define LOGMAX 30
 17009 /*
 17010 ** Masks used for mem5.aCtrl[] elements.
 17011 */
 17012 #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
 17013 #define CTRL_FREE     0x20    /* True if not checked out */
 17015 /*
 17016 ** All of the static variables used by this module are collected
 17017 ** into a single structure named "mem5".  This is to keep the
 17018 ** static variables organized and to reduce namespace pollution
 17019 ** when this module is combined with other in the amalgamation.
 17020 */
 17021 static SQLITE_WSD struct Mem5Global {
 17022   /*
 17023   ** Memory available for allocation
 17024   */
 17025   int szAtom;      /* Smallest possible allocation in bytes */
 17026   int nBlock;      /* Number of szAtom sized blocks in zPool */
 17027   u8 *zPool;       /* Memory available to be allocated */
 17029   /*
 17030   ** Mutex to control access to the memory allocation subsystem.
 17031   */
 17032   sqlite3_mutex *mutex;
 17034   /*
 17035   ** Performance statistics
 17036   */
 17037   u64 nAlloc;         /* Total number of calls to malloc */
 17038   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
 17039   u64 totalExcess;    /* Total internal fragmentation */
 17040   u32 currentOut;     /* Current checkout, including internal fragmentation */
 17041   u32 currentCount;   /* Current number of distinct checkouts */
 17042   u32 maxOut;         /* Maximum instantaneous currentOut */
 17043   u32 maxCount;       /* Maximum instantaneous currentCount */
 17044   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
 17046   /*
 17047   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
 17048   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
 17049   ** and so forth.
 17050   */
 17051   int aiFreelist[LOGMAX+1];
 17053   /*
 17054   ** Space for tracking which blocks are checked out and the size
 17055   ** of each block.  One byte per block.
 17056   */
 17057   u8 *aCtrl;
 17059 } mem5;
 17061 /*
 17062 ** Access the static variable through a macro for SQLITE_OMIT_WSD
 17063 */
 17064 #define mem5 GLOBAL(struct Mem5Global, mem5)
 17066 /*
 17067 ** Assuming mem5.zPool is divided up into an array of Mem5Link
 17068 ** structures, return a pointer to the idx-th such lik.
 17069 */
 17070 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
 17072 /*
 17073 ** Unlink the chunk at mem5.aPool[i] from list it is currently
 17074 ** on.  It should be found on mem5.aiFreelist[iLogsize].
 17075 */
 17076 static void memsys5Unlink(int i, int iLogsize){
 17077   int next, prev;
 17078   assert( i>=0 && i<mem5.nBlock );
 17079   assert( iLogsize>=0 && iLogsize<=LOGMAX );
 17080   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 17082   next = MEM5LINK(i)->next;
 17083   prev = MEM5LINK(i)->prev;
 17084   if( prev<0 ){
 17085     mem5.aiFreelist[iLogsize] = next;
 17086   }else{
 17087     MEM5LINK(prev)->next = next;
 17089   if( next>=0 ){
 17090     MEM5LINK(next)->prev = prev;
 17094 /*
 17095 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
 17096 ** free list.
 17097 */
 17098 static void memsys5Link(int i, int iLogsize){
 17099   int x;
 17100   assert( sqlite3_mutex_held(mem5.mutex) );
 17101   assert( i>=0 && i<mem5.nBlock );
 17102   assert( iLogsize>=0 && iLogsize<=LOGMAX );
 17103   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 17105   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
 17106   MEM5LINK(i)->prev = -1;
 17107   if( x>=0 ){
 17108     assert( x<mem5.nBlock );
 17109     MEM5LINK(x)->prev = i;
 17111   mem5.aiFreelist[iLogsize] = i;
 17114 /*
 17115 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
 17116 ** will already be held (obtained by code in malloc.c) if
 17117 ** sqlite3GlobalConfig.bMemStat is true.
 17118 */
 17119 static void memsys5Enter(void){
 17120   sqlite3_mutex_enter(mem5.mutex);
 17122 static void memsys5Leave(void){
 17123   sqlite3_mutex_leave(mem5.mutex);
 17126 /*
 17127 ** Return the size of an outstanding allocation, in bytes.  The
 17128 ** size returned omits the 8-byte header overhead.  This only
 17129 ** works for chunks that are currently checked out.
 17130 */
 17131 static int memsys5Size(void *p){
 17132   int iSize = 0;
 17133   if( p ){
 17134     int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
 17135     assert( i>=0 && i<mem5.nBlock );
 17136     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
 17138   return iSize;
 17141 /*
 17142 ** Find the first entry on the freelist iLogsize.  Unlink that
 17143 ** entry and return its index. 
 17144 */
 17145 static int memsys5UnlinkFirst(int iLogsize){
 17146   int i;
 17147   int iFirst;
 17149   assert( iLogsize>=0 && iLogsize<=LOGMAX );
 17150   i = iFirst = mem5.aiFreelist[iLogsize];
 17151   assert( iFirst>=0 );
 17152   while( i>0 ){
 17153     if( i<iFirst ) iFirst = i;
 17154     i = MEM5LINK(i)->next;
 17156   memsys5Unlink(iFirst, iLogsize);
 17157   return iFirst;
 17160 /*
 17161 ** Return a block of memory of at least nBytes in size.
 17162 ** Return NULL if unable.  Return NULL if nBytes==0.
 17163 **
 17164 ** The caller guarantees that nByte positive.
 17165 **
 17166 ** The caller has obtained a mutex prior to invoking this
 17167 ** routine so there is never any chance that two or more
 17168 ** threads can be in this routine at the same time.
 17169 */
 17170 static void *memsys5MallocUnsafe(int nByte){
 17171   int i;           /* Index of a mem5.aPool[] slot */
 17172   int iBin;        /* Index into mem5.aiFreelist[] */
 17173   int iFullSz;     /* Size of allocation rounded up to power of 2 */
 17174   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
 17176   /* nByte must be a positive */
 17177   assert( nByte>0 );
 17179   /* Keep track of the maximum allocation request.  Even unfulfilled
 17180   ** requests are counted */
 17181   if( (u32)nByte>mem5.maxRequest ){
 17182     mem5.maxRequest = nByte;
 17185   /* Abort if the requested allocation size is larger than the largest
 17186   ** power of two that we can represent using 32-bit signed integers.
 17187   */
 17188   if( nByte > 0x40000000 ){
 17189     return 0;
 17192   /* Round nByte up to the next valid power of two */
 17193   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
 17195   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
 17196   ** block.  If not, then split a block of the next larger power of
 17197   ** two in order to create a new free block of size iLogsize.
 17198   */
 17199   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
 17200   if( iBin>LOGMAX ){
 17201     testcase( sqlite3GlobalConfig.xLog!=0 );
 17202     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
 17203     return 0;
 17205   i = memsys5UnlinkFirst(iBin);
 17206   while( iBin>iLogsize ){
 17207     int newSize;
 17209     iBin--;
 17210     newSize = 1 << iBin;
 17211     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
 17212     memsys5Link(i+newSize, iBin);
 17214   mem5.aCtrl[i] = iLogsize;
 17216   /* Update allocator performance statistics. */
 17217   mem5.nAlloc++;
 17218   mem5.totalAlloc += iFullSz;
 17219   mem5.totalExcess += iFullSz - nByte;
 17220   mem5.currentCount++;
 17221   mem5.currentOut += iFullSz;
 17222   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
 17223   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
 17225   /* Return a pointer to the allocated memory. */
 17226   return (void*)&mem5.zPool[i*mem5.szAtom];
 17229 /*
 17230 ** Free an outstanding memory allocation.
 17231 */
 17232 static void memsys5FreeUnsafe(void *pOld){
 17233   u32 size, iLogsize;
 17234   int iBlock;
 17236   /* Set iBlock to the index of the block pointed to by pOld in 
 17237   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
 17238   */
 17239   iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
 17241   /* Check that the pointer pOld points to a valid, non-free block. */
 17242   assert( iBlock>=0 && iBlock<mem5.nBlock );
 17243   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
 17244   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
 17246   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
 17247   size = 1<<iLogsize;
 17248   assert( iBlock+size-1<(u32)mem5.nBlock );
 17250   mem5.aCtrl[iBlock] |= CTRL_FREE;
 17251   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
 17252   assert( mem5.currentCount>0 );
 17253   assert( mem5.currentOut>=(size*mem5.szAtom) );
 17254   mem5.currentCount--;
 17255   mem5.currentOut -= size*mem5.szAtom;
 17256   assert( mem5.currentOut>0 || mem5.currentCount==0 );
 17257   assert( mem5.currentCount>0 || mem5.currentOut==0 );
 17259   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
 17260   while( ALWAYS(iLogsize<LOGMAX) ){
 17261     int iBuddy;
 17262     if( (iBlock>>iLogsize) & 1 ){
 17263       iBuddy = iBlock - size;
 17264     }else{
 17265       iBuddy = iBlock + size;
 17267     assert( iBuddy>=0 );
 17268     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
 17269     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
 17270     memsys5Unlink(iBuddy, iLogsize);
 17271     iLogsize++;
 17272     if( iBuddy<iBlock ){
 17273       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
 17274       mem5.aCtrl[iBlock] = 0;
 17275       iBlock = iBuddy;
 17276     }else{
 17277       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
 17278       mem5.aCtrl[iBuddy] = 0;
 17280     size *= 2;
 17282   memsys5Link(iBlock, iLogsize);
 17285 /*
 17286 ** Allocate nBytes of memory
 17287 */
 17288 static void *memsys5Malloc(int nBytes){
 17289   sqlite3_int64 *p = 0;
 17290   if( nBytes>0 ){
 17291     memsys5Enter();
 17292     p = memsys5MallocUnsafe(nBytes);
 17293     memsys5Leave();
 17295   return (void*)p; 
 17298 /*
 17299 ** Free memory.
 17300 **
 17301 ** The outer layer memory allocator prevents this routine from
 17302 ** being called with pPrior==0.
 17303 */
 17304 static void memsys5Free(void *pPrior){
 17305   assert( pPrior!=0 );
 17306   memsys5Enter();
 17307   memsys5FreeUnsafe(pPrior);
 17308   memsys5Leave();  
 17311 /*
 17312 ** Change the size of an existing memory allocation.
 17313 **
 17314 ** The outer layer memory allocator prevents this routine from
 17315 ** being called with pPrior==0.  
 17316 **
 17317 ** nBytes is always a value obtained from a prior call to
 17318 ** memsys5Round().  Hence nBytes is always a non-negative power
 17319 ** of two.  If nBytes==0 that means that an oversize allocation
 17320 ** (an allocation larger than 0x40000000) was requested and this
 17321 ** routine should return 0 without freeing pPrior.
 17322 */
 17323 static void *memsys5Realloc(void *pPrior, int nBytes){
 17324   int nOld;
 17325   void *p;
 17326   assert( pPrior!=0 );
 17327   assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
 17328   assert( nBytes>=0 );
 17329   if( nBytes==0 ){
 17330     return 0;
 17332   nOld = memsys5Size(pPrior);
 17333   if( nBytes<=nOld ){
 17334     return pPrior;
 17336   memsys5Enter();
 17337   p = memsys5MallocUnsafe(nBytes);
 17338   if( p ){
 17339     memcpy(p, pPrior, nOld);
 17340     memsys5FreeUnsafe(pPrior);
 17342   memsys5Leave();
 17343   return p;
 17346 /*
 17347 ** Round up a request size to the next valid allocation size.  If
 17348 ** the allocation is too large to be handled by this allocation system,
 17349 ** return 0.
 17350 **
 17351 ** All allocations must be a power of two and must be expressed by a
 17352 ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
 17353 ** or 1073741824 bytes.
 17354 */
 17355 static int memsys5Roundup(int n){
 17356   int iFullSz;
 17357   if( n > 0x40000000 ) return 0;
 17358   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
 17359   return iFullSz;
 17362 /*
 17363 ** Return the ceiling of the logarithm base 2 of iValue.
 17364 **
 17365 ** Examples:   memsys5Log(1) -> 0
 17366 **             memsys5Log(2) -> 1
 17367 **             memsys5Log(4) -> 2
 17368 **             memsys5Log(5) -> 3
 17369 **             memsys5Log(8) -> 3
 17370 **             memsys5Log(9) -> 4
 17371 */
 17372 static int memsys5Log(int iValue){
 17373   int iLog;
 17374   for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
 17375   return iLog;
 17378 /*
 17379 ** Initialize the memory allocator.
 17380 **
 17381 ** This routine is not threadsafe.  The caller must be holding a mutex
 17382 ** to prevent multiple threads from entering at the same time.
 17383 */
 17384 static int memsys5Init(void *NotUsed){
 17385   int ii;            /* Loop counter */
 17386   int nByte;         /* Number of bytes of memory available to this allocator */
 17387   u8 *zByte;         /* Memory usable by this allocator */
 17388   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
 17389   int iOffset;       /* An offset into mem5.aCtrl[] */
 17391   UNUSED_PARAMETER(NotUsed);
 17393   /* For the purposes of this routine, disable the mutex */
 17394   mem5.mutex = 0;
 17396   /* The size of a Mem5Link object must be a power of two.  Verify that
 17397   ** this is case.
 17398   */
 17399   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
 17401   nByte = sqlite3GlobalConfig.nHeap;
 17402   zByte = (u8*)sqlite3GlobalConfig.pHeap;
 17403   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
 17405   /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
 17406   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
 17407   mem5.szAtom = (1<<nMinLog);
 17408   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
 17409     mem5.szAtom = mem5.szAtom << 1;
 17412   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
 17413   mem5.zPool = zByte;
 17414   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
 17416   for(ii=0; ii<=LOGMAX; ii++){
 17417     mem5.aiFreelist[ii] = -1;
 17420   iOffset = 0;
 17421   for(ii=LOGMAX; ii>=0; ii--){
 17422     int nAlloc = (1<<ii);
 17423     if( (iOffset+nAlloc)<=mem5.nBlock ){
 17424       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
 17425       memsys5Link(iOffset, ii);
 17426       iOffset += nAlloc;
 17428     assert((iOffset+nAlloc)>mem5.nBlock);
 17431   /* If a mutex is required for normal operation, allocate one */
 17432   if( sqlite3GlobalConfig.bMemstat==0 ){
 17433     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 17436   return SQLITE_OK;
 17439 /*
 17440 ** Deinitialize this module.
 17441 */
 17442 static void memsys5Shutdown(void *NotUsed){
 17443   UNUSED_PARAMETER(NotUsed);
 17444   mem5.mutex = 0;
 17445   return;
 17448 #ifdef SQLITE_TEST
 17449 /*
 17450 ** Open the file indicated and write a log of all unfreed memory 
 17451 ** allocations into that log.
 17452 */
 17453 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
 17454   FILE *out;
 17455   int i, j, n;
 17456   int nMinLog;
 17458   if( zFilename==0 || zFilename[0]==0 ){
 17459     out = stdout;
 17460   }else{
 17461     out = fopen(zFilename, "w");
 17462     if( out==0 ){
 17463       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 17464                       zFilename);
 17465       return;
 17468   memsys5Enter();
 17469   nMinLog = memsys5Log(mem5.szAtom);
 17470   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
 17471     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
 17472     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
 17474   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
 17475   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
 17476   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
 17477   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
 17478   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
 17479   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
 17480   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
 17481   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
 17482   memsys5Leave();
 17483   if( out==stdout ){
 17484     fflush(stdout);
 17485   }else{
 17486     fclose(out);
 17489 #endif
 17491 /*
 17492 ** This routine is the only routine in this file with external 
 17493 ** linkage. It returns a pointer to a static sqlite3_mem_methods
 17494 ** struct populated with the memsys5 methods.
 17495 */
 17496 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
 17497   static const sqlite3_mem_methods memsys5Methods = {
 17498      memsys5Malloc,
 17499      memsys5Free,
 17500      memsys5Realloc,
 17501      memsys5Size,
 17502      memsys5Roundup,
 17503      memsys5Init,
 17504      memsys5Shutdown,
 17506   };
 17507   return &memsys5Methods;
 17510 #endif /* SQLITE_ENABLE_MEMSYS5 */
 17512 /************** End of mem5.c ************************************************/
 17513 /************** Begin file mutex.c *******************************************/
 17514 /*
 17515 ** 2007 August 14
 17516 **
 17517 ** The author disclaims copyright to this source code.  In place of
 17518 ** a legal notice, here is a blessing:
 17519 **
 17520 **    May you do good and not evil.
 17521 **    May you find forgiveness for yourself and forgive others.
 17522 **    May you share freely, never taking more than you give.
 17523 **
 17524 *************************************************************************
 17525 ** This file contains the C functions that implement mutexes.
 17526 **
 17527 ** This file contains code that is common across all mutex implementations.
 17528 */
 17530 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
 17531 /*
 17532 ** For debugging purposes, record when the mutex subsystem is initialized
 17533 ** and uninitialized so that we can assert() if there is an attempt to
 17534 ** allocate a mutex while the system is uninitialized.
 17535 */
 17536 static SQLITE_WSD int mutexIsInit = 0;
 17537 #endif /* SQLITE_DEBUG */
 17540 #ifndef SQLITE_MUTEX_OMIT
 17541 /*
 17542 ** Initialize the mutex system.
 17543 */
 17544 SQLITE_PRIVATE int sqlite3MutexInit(void){ 
 17545   int rc = SQLITE_OK;
 17546   if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
 17547     /* If the xMutexAlloc method has not been set, then the user did not
 17548     ** install a mutex implementation via sqlite3_config() prior to 
 17549     ** sqlite3_initialize() being called. This block copies pointers to
 17550     ** the default implementation into the sqlite3GlobalConfig structure.
 17551     */
 17552     sqlite3_mutex_methods const *pFrom;
 17553     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
 17555     if( sqlite3GlobalConfig.bCoreMutex ){
 17556       pFrom = sqlite3DefaultMutex();
 17557     }else{
 17558       pFrom = sqlite3NoopMutex();
 17560     memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
 17561     memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
 17562            sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
 17563     pTo->xMutexAlloc = pFrom->xMutexAlloc;
 17565   rc = sqlite3GlobalConfig.mutex.xMutexInit();
 17567 #ifdef SQLITE_DEBUG
 17568   GLOBAL(int, mutexIsInit) = 1;
 17569 #endif
 17571   return rc;
 17574 /*
 17575 ** Shutdown the mutex system. This call frees resources allocated by
 17576 ** sqlite3MutexInit().
 17577 */
 17578 SQLITE_PRIVATE int sqlite3MutexEnd(void){
 17579   int rc = SQLITE_OK;
 17580   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
 17581     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
 17584 #ifdef SQLITE_DEBUG
 17585   GLOBAL(int, mutexIsInit) = 0;
 17586 #endif
 17588   return rc;
 17591 /*
 17592 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
 17593 */
 17594 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
 17595 #ifndef SQLITE_OMIT_AUTOINIT
 17596   if( sqlite3_initialize() ) return 0;
 17597 #endif
 17598   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
 17601 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
 17602   if( !sqlite3GlobalConfig.bCoreMutex ){
 17603     return 0;
 17605   assert( GLOBAL(int, mutexIsInit) );
 17606   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
 17609 /*
 17610 ** Free a dynamic mutex.
 17611 */
 17612 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
 17613   if( p ){
 17614     sqlite3GlobalConfig.mutex.xMutexFree(p);
 17618 /*
 17619 ** Obtain the mutex p. If some other thread already has the mutex, block
 17620 ** until it can be obtained.
 17621 */
 17622 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
 17623   if( p ){
 17624     sqlite3GlobalConfig.mutex.xMutexEnter(p);
 17628 /*
 17629 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
 17630 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
 17631 */
 17632 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
 17633   int rc = SQLITE_OK;
 17634   if( p ){
 17635     return sqlite3GlobalConfig.mutex.xMutexTry(p);
 17637   return rc;
 17640 /*
 17641 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
 17642 ** entered by the same thread.  The behavior is undefined if the mutex 
 17643 ** is not currently entered. If a NULL pointer is passed as an argument
 17644 ** this function is a no-op.
 17645 */
 17646 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
 17647   if( p ){
 17648     sqlite3GlobalConfig.mutex.xMutexLeave(p);
 17652 #ifndef NDEBUG
 17653 /*
 17654 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 17655 ** intended for use inside assert() statements.
 17656 */
 17657 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
 17658   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
 17660 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
 17661   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 17663 #endif
 17665 #endif /* !defined(SQLITE_MUTEX_OMIT) */
 17667 /************** End of mutex.c ***********************************************/
 17668 /************** Begin file mutex_noop.c **************************************/
 17669 /*
 17670 ** 2008 October 07
 17671 **
 17672 ** The author disclaims copyright to this source code.  In place of
 17673 ** a legal notice, here is a blessing:
 17674 **
 17675 **    May you do good and not evil.
 17676 **    May you find forgiveness for yourself and forgive others.
 17677 **    May you share freely, never taking more than you give.
 17678 **
 17679 *************************************************************************
 17680 ** This file contains the C functions that implement mutexes.
 17681 **
 17682 ** This implementation in this file does not provide any mutual
 17683 ** exclusion and is thus suitable for use only in applications
 17684 ** that use SQLite in a single thread.  The routines defined
 17685 ** here are place-holders.  Applications can substitute working
 17686 ** mutex routines at start-time using the
 17687 **
 17688 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
 17689 **
 17690 ** interface.
 17691 **
 17692 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
 17693 ** that does error checking on mutexes to make sure they are being
 17694 ** called correctly.
 17695 */
 17697 #ifndef SQLITE_MUTEX_OMIT
 17699 #ifndef SQLITE_DEBUG
 17700 /*
 17701 ** Stub routines for all mutex methods.
 17702 **
 17703 ** This routines provide no mutual exclusion or error checking.
 17704 */
 17705 static int noopMutexInit(void){ return SQLITE_OK; }
 17706 static int noopMutexEnd(void){ return SQLITE_OK; }
 17707 static sqlite3_mutex *noopMutexAlloc(int id){ 
 17708   UNUSED_PARAMETER(id);
 17709   return (sqlite3_mutex*)8; 
 17711 static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 17712 static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 17713 static int noopMutexTry(sqlite3_mutex *p){
 17714   UNUSED_PARAMETER(p);
 17715   return SQLITE_OK;
 17717 static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 17719 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
 17720   static const sqlite3_mutex_methods sMutex = {
 17721     noopMutexInit,
 17722     noopMutexEnd,
 17723     noopMutexAlloc,
 17724     noopMutexFree,
 17725     noopMutexEnter,
 17726     noopMutexTry,
 17727     noopMutexLeave,
 17729     0,
 17730     0,
 17731   };
 17733   return &sMutex;
 17735 #endif /* !SQLITE_DEBUG */
 17737 #ifdef SQLITE_DEBUG
 17738 /*
 17739 ** In this implementation, error checking is provided for testing
 17740 ** and debugging purposes.  The mutexes still do not provide any
 17741 ** mutual exclusion.
 17742 */
 17744 /*
 17745 ** The mutex object
 17746 */
 17747 typedef struct sqlite3_debug_mutex {
 17748   int id;     /* The mutex type */
 17749   int cnt;    /* Number of entries without a matching leave */
 17750 } sqlite3_debug_mutex;
 17752 /*
 17753 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 17754 ** intended for use inside assert() statements.
 17755 */
 17756 static int debugMutexHeld(sqlite3_mutex *pX){
 17757   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 17758   return p==0 || p->cnt>0;
 17760 static int debugMutexNotheld(sqlite3_mutex *pX){
 17761   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 17762   return p==0 || p->cnt==0;
 17765 /*
 17766 ** Initialize and deinitialize the mutex subsystem.
 17767 */
 17768 static int debugMutexInit(void){ return SQLITE_OK; }
 17769 static int debugMutexEnd(void){ return SQLITE_OK; }
 17771 /*
 17772 ** The sqlite3_mutex_alloc() routine allocates a new
 17773 ** mutex and returns a pointer to it.  If it returns NULL
 17774 ** that means that a mutex could not be allocated. 
 17775 */
 17776 static sqlite3_mutex *debugMutexAlloc(int id){
 17777   static sqlite3_debug_mutex aStatic[6];
 17778   sqlite3_debug_mutex *pNew = 0;
 17779   switch( id ){
 17780     case SQLITE_MUTEX_FAST:
 17781     case SQLITE_MUTEX_RECURSIVE: {
 17782       pNew = sqlite3Malloc(sizeof(*pNew));
 17783       if( pNew ){
 17784         pNew->id = id;
 17785         pNew->cnt = 0;
 17787       break;
 17789     default: {
 17790       assert( id-2 >= 0 );
 17791       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
 17792       pNew = &aStatic[id-2];
 17793       pNew->id = id;
 17794       break;
 17797   return (sqlite3_mutex*)pNew;
 17800 /*
 17801 ** This routine deallocates a previously allocated mutex.
 17802 */
 17803 static void debugMutexFree(sqlite3_mutex *pX){
 17804   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 17805   assert( p->cnt==0 );
 17806   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 17807   sqlite3_free(p);
 17810 /*
 17811 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 17812 ** to enter a mutex.  If another thread is already within the mutex,
 17813 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 17814 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 17815 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 17816 ** be entered multiple times by the same thread.  In such cases the,
 17817 ** mutex must be exited an equal number of times before another thread
 17818 ** can enter.  If the same thread tries to enter any other kind of mutex
 17819 ** more than once, the behavior is undefined.
 17820 */
 17821 static void debugMutexEnter(sqlite3_mutex *pX){
 17822   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 17823   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 17824   p->cnt++;
 17826 static int debugMutexTry(sqlite3_mutex *pX){
 17827   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 17828   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 17829   p->cnt++;
 17830   return SQLITE_OK;
 17833 /*
 17834 ** The sqlite3_mutex_leave() routine exits a mutex that was
 17835 ** previously entered by the same thread.  The behavior
 17836 ** is undefined if the mutex is not currently entered or
 17837 ** is not currently allocated.  SQLite will never do either.
 17838 */
 17839 static void debugMutexLeave(sqlite3_mutex *pX){
 17840   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 17841   assert( debugMutexHeld(pX) );
 17842   p->cnt--;
 17843   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 17846 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
 17847   static const sqlite3_mutex_methods sMutex = {
 17848     debugMutexInit,
 17849     debugMutexEnd,
 17850     debugMutexAlloc,
 17851     debugMutexFree,
 17852     debugMutexEnter,
 17853     debugMutexTry,
 17854     debugMutexLeave,
 17856     debugMutexHeld,
 17857     debugMutexNotheld
 17858   };
 17860   return &sMutex;
 17862 #endif /* SQLITE_DEBUG */
 17864 /*
 17865 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
 17866 ** is used regardless of the run-time threadsafety setting.
 17867 */
 17868 #ifdef SQLITE_MUTEX_NOOP
 17869 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 17870   return sqlite3NoopMutex();
 17872 #endif /* defined(SQLITE_MUTEX_NOOP) */
 17873 #endif /* !defined(SQLITE_MUTEX_OMIT) */
 17875 /************** End of mutex_noop.c ******************************************/
 17876 /************** Begin file mutex_unix.c **************************************/
 17877 /*
 17878 ** 2007 August 28
 17879 **
 17880 ** The author disclaims copyright to this source code.  In place of
 17881 ** a legal notice, here is a blessing:
 17882 **
 17883 **    May you do good and not evil.
 17884 **    May you find forgiveness for yourself and forgive others.
 17885 **    May you share freely, never taking more than you give.
 17886 **
 17887 *************************************************************************
 17888 ** This file contains the C functions that implement mutexes for pthreads
 17889 */
 17891 /*
 17892 ** The code in this file is only used if we are compiling threadsafe
 17893 ** under unix with pthreads.
 17894 **
 17895 ** Note that this implementation requires a version of pthreads that
 17896 ** supports recursive mutexes.
 17897 */
 17898 #ifdef SQLITE_MUTEX_PTHREADS
 17900 #include <pthread.h>
 17902 /*
 17903 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
 17904 ** are necessary under two condidtions:  (1) Debug builds and (2) using
 17905 ** home-grown mutexes.  Encapsulate these conditions into a single #define.
 17906 */
 17907 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
 17908 # define SQLITE_MUTEX_NREF 1
 17909 #else
 17910 # define SQLITE_MUTEX_NREF 0
 17911 #endif
 17913 /*
 17914 ** Each recursive mutex is an instance of the following structure.
 17915 */
 17916 struct sqlite3_mutex {
 17917   pthread_mutex_t mutex;     /* Mutex controlling the lock */
 17918 #if SQLITE_MUTEX_NREF
 17919   int id;                    /* Mutex type */
 17920   volatile int nRef;         /* Number of entrances */
 17921   volatile pthread_t owner;  /* Thread that is within this mutex */
 17922   int trace;                 /* True to trace changes */
 17923 #endif
 17924 };
 17925 #if SQLITE_MUTEX_NREF
 17926 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
 17927 #else
 17928 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
 17929 #endif
 17931 /*
 17932 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 17933 ** intended for use only inside assert() statements.  On some platforms,
 17934 ** there might be race conditions that can cause these routines to
 17935 ** deliver incorrect results.  In particular, if pthread_equal() is
 17936 ** not an atomic operation, then these routines might delivery
 17937 ** incorrect results.  On most platforms, pthread_equal() is a 
 17938 ** comparison of two integers and is therefore atomic.  But we are
 17939 ** told that HPUX is not such a platform.  If so, then these routines
 17940 ** will not always work correctly on HPUX.
 17941 **
 17942 ** On those platforms where pthread_equal() is not atomic, SQLite
 17943 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
 17944 ** make sure no assert() statements are evaluated and hence these
 17945 ** routines are never called.
 17946 */
 17947 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
 17948 static int pthreadMutexHeld(sqlite3_mutex *p){
 17949   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
 17951 static int pthreadMutexNotheld(sqlite3_mutex *p){
 17952   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
 17954 #endif
 17956 /*
 17957 ** Initialize and deinitialize the mutex subsystem.
 17958 */
 17959 static int pthreadMutexInit(void){ return SQLITE_OK; }
 17960 static int pthreadMutexEnd(void){ return SQLITE_OK; }
 17962 /*
 17963 ** The sqlite3_mutex_alloc() routine allocates a new
 17964 ** mutex and returns a pointer to it.  If it returns NULL
 17965 ** that means that a mutex could not be allocated.  SQLite
 17966 ** will unwind its stack and return an error.  The argument
 17967 ** to sqlite3_mutex_alloc() is one of these integer constants:
 17968 **
 17969 ** <ul>
 17970 ** <li>  SQLITE_MUTEX_FAST
 17971 ** <li>  SQLITE_MUTEX_RECURSIVE
 17972 ** <li>  SQLITE_MUTEX_STATIC_MASTER
 17973 ** <li>  SQLITE_MUTEX_STATIC_MEM
 17974 ** <li>  SQLITE_MUTEX_STATIC_MEM2
 17975 ** <li>  SQLITE_MUTEX_STATIC_PRNG
 17976 ** <li>  SQLITE_MUTEX_STATIC_LRU
 17977 ** <li>  SQLITE_MUTEX_STATIC_PMEM
 17978 ** </ul>
 17979 **
 17980 ** The first two constants cause sqlite3_mutex_alloc() to create
 17981 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 17982 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 17983 ** The mutex implementation does not need to make a distinction
 17984 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 17985 ** not want to.  But SQLite will only request a recursive mutex in
 17986 ** cases where it really needs one.  If a faster non-recursive mutex
 17987 ** implementation is available on the host platform, the mutex subsystem
 17988 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
 17989 **
 17990 ** The other allowed parameters to sqlite3_mutex_alloc() each return
 17991 ** a pointer to a static preexisting mutex.  Six static mutexes are
 17992 ** used by the current version of SQLite.  Future versions of SQLite
 17993 ** may add additional static mutexes.  Static mutexes are for internal
 17994 ** use by SQLite only.  Applications that use SQLite mutexes should
 17995 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 17996 ** SQLITE_MUTEX_RECURSIVE.
 17997 **
 17998 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
 17999 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 18000 ** returns a different mutex on every call.  But for the static 
 18001 ** mutex types, the same mutex is returned on every call that has
 18002 ** the same type number.
 18003 */
 18004 static sqlite3_mutex *pthreadMutexAlloc(int iType){
 18005   static sqlite3_mutex staticMutexes[] = {
 18006     SQLITE3_MUTEX_INITIALIZER,
 18007     SQLITE3_MUTEX_INITIALIZER,
 18008     SQLITE3_MUTEX_INITIALIZER,
 18009     SQLITE3_MUTEX_INITIALIZER,
 18010     SQLITE3_MUTEX_INITIALIZER,
 18011     SQLITE3_MUTEX_INITIALIZER
 18012   };
 18013   sqlite3_mutex *p;
 18014   switch( iType ){
 18015     case SQLITE_MUTEX_RECURSIVE: {
 18016       p = sqlite3MallocZero( sizeof(*p) );
 18017       if( p ){
 18018 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18019         /* If recursive mutexes are not available, we will have to
 18020         ** build our own.  See below. */
 18021         pthread_mutex_init(&p->mutex, 0);
 18022 #else
 18023         /* Use a recursive mutex if it is available */
 18024         pthread_mutexattr_t recursiveAttr;
 18025         pthread_mutexattr_init(&recursiveAttr);
 18026         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
 18027         pthread_mutex_init(&p->mutex, &recursiveAttr);
 18028         pthread_mutexattr_destroy(&recursiveAttr);
 18029 #endif
 18030 #if SQLITE_MUTEX_NREF
 18031         p->id = iType;
 18032 #endif
 18034       break;
 18036     case SQLITE_MUTEX_FAST: {
 18037       p = sqlite3MallocZero( sizeof(*p) );
 18038       if( p ){
 18039 #if SQLITE_MUTEX_NREF
 18040         p->id = iType;
 18041 #endif
 18042         pthread_mutex_init(&p->mutex, 0);
 18044       break;
 18046     default: {
 18047       assert( iType-2 >= 0 );
 18048       assert( iType-2 < ArraySize(staticMutexes) );
 18049       p = &staticMutexes[iType-2];
 18050 #if SQLITE_MUTEX_NREF
 18051       p->id = iType;
 18052 #endif
 18053       break;
 18056   return p;
 18060 /*
 18061 ** This routine deallocates a previously
 18062 ** allocated mutex.  SQLite is careful to deallocate every
 18063 ** mutex that it allocates.
 18064 */
 18065 static void pthreadMutexFree(sqlite3_mutex *p){
 18066   assert( p->nRef==0 );
 18067   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 18068   pthread_mutex_destroy(&p->mutex);
 18069   sqlite3_free(p);
 18072 /*
 18073 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 18074 ** to enter a mutex.  If another thread is already within the mutex,
 18075 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 18076 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 18077 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 18078 ** be entered multiple times by the same thread.  In such cases the,
 18079 ** mutex must be exited an equal number of times before another thread
 18080 ** can enter.  If the same thread tries to enter any other kind of mutex
 18081 ** more than once, the behavior is undefined.
 18082 */
 18083 static void pthreadMutexEnter(sqlite3_mutex *p){
 18084   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 18086 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18087   /* If recursive mutexes are not available, then we have to grow
 18088   ** our own.  This implementation assumes that pthread_equal()
 18089   ** is atomic - that it cannot be deceived into thinking self
 18090   ** and p->owner are equal if p->owner changes between two values
 18091   ** that are not equal to self while the comparison is taking place.
 18092   ** This implementation also assumes a coherent cache - that 
 18093   ** separate processes cannot read different values from the same
 18094   ** address at the same time.  If either of these two conditions
 18095   ** are not met, then the mutexes will fail and problems will result.
 18096   */
 18098     pthread_t self = pthread_self();
 18099     if( p->nRef>0 && pthread_equal(p->owner, self) ){
 18100       p->nRef++;
 18101     }else{
 18102       pthread_mutex_lock(&p->mutex);
 18103       assert( p->nRef==0 );
 18104       p->owner = self;
 18105       p->nRef = 1;
 18108 #else
 18109   /* Use the built-in recursive mutexes if they are available.
 18110   */
 18111   pthread_mutex_lock(&p->mutex);
 18112 #if SQLITE_MUTEX_NREF
 18113   assert( p->nRef>0 || p->owner==0 );
 18114   p->owner = pthread_self();
 18115   p->nRef++;
 18116 #endif
 18117 #endif
 18119 #ifdef SQLITE_DEBUG
 18120   if( p->trace ){
 18121     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18123 #endif
 18125 static int pthreadMutexTry(sqlite3_mutex *p){
 18126   int rc;
 18127   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 18129 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18130   /* If recursive mutexes are not available, then we have to grow
 18131   ** our own.  This implementation assumes that pthread_equal()
 18132   ** is atomic - that it cannot be deceived into thinking self
 18133   ** and p->owner are equal if p->owner changes between two values
 18134   ** that are not equal to self while the comparison is taking place.
 18135   ** This implementation also assumes a coherent cache - that 
 18136   ** separate processes cannot read different values from the same
 18137   ** address at the same time.  If either of these two conditions
 18138   ** are not met, then the mutexes will fail and problems will result.
 18139   */
 18141     pthread_t self = pthread_self();
 18142     if( p->nRef>0 && pthread_equal(p->owner, self) ){
 18143       p->nRef++;
 18144       rc = SQLITE_OK;
 18145     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
 18146       assert( p->nRef==0 );
 18147       p->owner = self;
 18148       p->nRef = 1;
 18149       rc = SQLITE_OK;
 18150     }else{
 18151       rc = SQLITE_BUSY;
 18154 #else
 18155   /* Use the built-in recursive mutexes if they are available.
 18156   */
 18157   if( pthread_mutex_trylock(&p->mutex)==0 ){
 18158 #if SQLITE_MUTEX_NREF
 18159     p->owner = pthread_self();
 18160     p->nRef++;
 18161 #endif
 18162     rc = SQLITE_OK;
 18163   }else{
 18164     rc = SQLITE_BUSY;
 18166 #endif
 18168 #ifdef SQLITE_DEBUG
 18169   if( rc==SQLITE_OK && p->trace ){
 18170     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18172 #endif
 18173   return rc;
 18176 /*
 18177 ** The sqlite3_mutex_leave() routine exits a mutex that was
 18178 ** previously entered by the same thread.  The behavior
 18179 ** is undefined if the mutex is not currently entered or
 18180 ** is not currently allocated.  SQLite will never do either.
 18181 */
 18182 static void pthreadMutexLeave(sqlite3_mutex *p){
 18183   assert( pthreadMutexHeld(p) );
 18184 #if SQLITE_MUTEX_NREF
 18185   p->nRef--;
 18186   if( p->nRef==0 ) p->owner = 0;
 18187 #endif
 18188   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 18190 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18191   if( p->nRef==0 ){
 18192     pthread_mutex_unlock(&p->mutex);
 18194 #else
 18195   pthread_mutex_unlock(&p->mutex);
 18196 #endif
 18198 #ifdef SQLITE_DEBUG
 18199   if( p->trace ){
 18200     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18202 #endif
 18205 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 18206   static const sqlite3_mutex_methods sMutex = {
 18207     pthreadMutexInit,
 18208     pthreadMutexEnd,
 18209     pthreadMutexAlloc,
 18210     pthreadMutexFree,
 18211     pthreadMutexEnter,
 18212     pthreadMutexTry,
 18213     pthreadMutexLeave,
 18214 #ifdef SQLITE_DEBUG
 18215     pthreadMutexHeld,
 18216     pthreadMutexNotheld
 18217 #else
 18218     0,
 18220 #endif
 18221   };
 18223   return &sMutex;
 18226 #endif /* SQLITE_MUTEX_PTHREADS */
 18228 /************** End of mutex_unix.c ******************************************/
 18229 /************** Begin file mutex_w32.c ***************************************/
 18230 /*
 18231 ** 2007 August 14
 18232 **
 18233 ** The author disclaims copyright to this source code.  In place of
 18234 ** a legal notice, here is a blessing:
 18235 **
 18236 **    May you do good and not evil.
 18237 **    May you find forgiveness for yourself and forgive others.
 18238 **    May you share freely, never taking more than you give.
 18239 **
 18240 *************************************************************************
 18241 ** This file contains the C functions that implement mutexes for win32
 18242 */
 18244 /*
 18245 ** The code in this file is only used if we are compiling multithreaded
 18246 ** on a win32 system.
 18247 */
 18248 #ifdef SQLITE_MUTEX_W32
 18250 /*
 18251 ** Each recursive mutex is an instance of the following structure.
 18252 */
 18253 struct sqlite3_mutex {
 18254   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
 18255   int id;                    /* Mutex type */
 18256 #ifdef SQLITE_DEBUG
 18257   volatile int nRef;         /* Number of enterances */
 18258   volatile DWORD owner;      /* Thread holding this mutex */
 18259   int trace;                 /* True to trace changes */
 18260 #endif
 18261 };
 18262 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 18263 #ifdef SQLITE_DEBUG
 18264 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
 18265 #else
 18266 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
 18267 #endif
 18269 /*
 18270 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
 18271 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
 18272 **
 18273 ** Here is an interesting observation:  Win95, Win98, and WinME lack
 18274 ** the LockFileEx() API.  But we can still statically link against that
 18275 ** API as long as we don't call it win running Win95/98/ME.  A call to
 18276 ** this routine is used to determine if the host is Win95/98/ME or
 18277 ** WinNT/2K/XP so that we will know whether or not we can safely call
 18278 ** the LockFileEx() API.
 18279 **
 18280 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
 18281 ** which is only available if your application was compiled with 
 18282 ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
 18283 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
 18284 ** this out as well.
 18285 */
 18286 #if 0
 18287 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 18288 # define mutexIsNT()  (1)
 18289 #else
 18290   static int mutexIsNT(void){
 18291     static int osType = 0;
 18292     if( osType==0 ){
 18293       OSVERSIONINFO sInfo;
 18294       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
 18295       GetVersionEx(&sInfo);
 18296       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
 18298     return osType==2;
 18300 #endif /* SQLITE_OS_WINCE */
 18301 #endif
 18303 #ifdef SQLITE_DEBUG
 18304 /*
 18305 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 18306 ** intended for use only inside assert() statements.
 18307 */
 18308 static int winMutexHeld(sqlite3_mutex *p){
 18309   return p->nRef!=0 && p->owner==GetCurrentThreadId();
 18311 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
 18312   return p->nRef==0 || p->owner!=tid;
 18314 static int winMutexNotheld(sqlite3_mutex *p){
 18315   DWORD tid = GetCurrentThreadId(); 
 18316   return winMutexNotheld2(p, tid);
 18318 #endif
 18321 /*
 18322 ** Initialize and deinitialize the mutex subsystem.
 18323 */
 18324 static sqlite3_mutex winMutex_staticMutexes[6] = {
 18325   SQLITE3_MUTEX_INITIALIZER,
 18326   SQLITE3_MUTEX_INITIALIZER,
 18327   SQLITE3_MUTEX_INITIALIZER,
 18328   SQLITE3_MUTEX_INITIALIZER,
 18329   SQLITE3_MUTEX_INITIALIZER,
 18330   SQLITE3_MUTEX_INITIALIZER
 18331 };
 18332 static int winMutex_isInit = 0;
 18333 /* As winMutexInit() and winMutexEnd() are called as part
 18334 ** of the sqlite3_initialize and sqlite3_shutdown()
 18335 ** processing, the "interlocked" magic is probably not
 18336 ** strictly necessary.
 18337 */
 18338 static long winMutex_lock = 0;
 18340 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
 18342 static int winMutexInit(void){ 
 18343   /* The first to increment to 1 does actual initialization */
 18344   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
 18345     int i;
 18346     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
 18347 #if SQLITE_OS_WINRT
 18348       InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
 18349 #else
 18350       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
 18351 #endif
 18353     winMutex_isInit = 1;
 18354   }else{
 18355     /* Someone else is in the process of initing the static mutexes */
 18356     while( !winMutex_isInit ){
 18357       sqlite3_win32_sleep(1);
 18360   return SQLITE_OK; 
 18363 static int winMutexEnd(void){ 
 18364   /* The first to decrement to 0 does actual shutdown 
 18365   ** (which should be the last to shutdown.) */
 18366   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
 18367     if( winMutex_isInit==1 ){
 18368       int i;
 18369       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
 18370         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
 18372       winMutex_isInit = 0;
 18375   return SQLITE_OK; 
 18378 /*
 18379 ** The sqlite3_mutex_alloc() routine allocates a new
 18380 ** mutex and returns a pointer to it.  If it returns NULL
 18381 ** that means that a mutex could not be allocated.  SQLite
 18382 ** will unwind its stack and return an error.  The argument
 18383 ** to sqlite3_mutex_alloc() is one of these integer constants:
 18384 **
 18385 ** <ul>
 18386 ** <li>  SQLITE_MUTEX_FAST
 18387 ** <li>  SQLITE_MUTEX_RECURSIVE
 18388 ** <li>  SQLITE_MUTEX_STATIC_MASTER
 18389 ** <li>  SQLITE_MUTEX_STATIC_MEM
 18390 ** <li>  SQLITE_MUTEX_STATIC_MEM2
 18391 ** <li>  SQLITE_MUTEX_STATIC_PRNG
 18392 ** <li>  SQLITE_MUTEX_STATIC_LRU
 18393 ** <li>  SQLITE_MUTEX_STATIC_PMEM
 18394 ** </ul>
 18395 **
 18396 ** The first two constants cause sqlite3_mutex_alloc() to create
 18397 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 18398 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 18399 ** The mutex implementation does not need to make a distinction
 18400 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 18401 ** not want to.  But SQLite will only request a recursive mutex in
 18402 ** cases where it really needs one.  If a faster non-recursive mutex
 18403 ** implementation is available on the host platform, the mutex subsystem
 18404 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
 18405 **
 18406 ** The other allowed parameters to sqlite3_mutex_alloc() each return
 18407 ** a pointer to a static preexisting mutex.  Six static mutexes are
 18408 ** used by the current version of SQLite.  Future versions of SQLite
 18409 ** may add additional static mutexes.  Static mutexes are for internal
 18410 ** use by SQLite only.  Applications that use SQLite mutexes should
 18411 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 18412 ** SQLITE_MUTEX_RECURSIVE.
 18413 **
 18414 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
 18415 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 18416 ** returns a different mutex on every call.  But for the static 
 18417 ** mutex types, the same mutex is returned on every call that has
 18418 ** the same type number.
 18419 */
 18420 static sqlite3_mutex *winMutexAlloc(int iType){
 18421   sqlite3_mutex *p;
 18423   switch( iType ){
 18424     case SQLITE_MUTEX_FAST:
 18425     case SQLITE_MUTEX_RECURSIVE: {
 18426       p = sqlite3MallocZero( sizeof(*p) );
 18427       if( p ){  
 18428 #ifdef SQLITE_DEBUG
 18429         p->id = iType;
 18430 #endif
 18431 #if SQLITE_OS_WINRT
 18432         InitializeCriticalSectionEx(&p->mutex, 0, 0);
 18433 #else
 18434         InitializeCriticalSection(&p->mutex);
 18435 #endif
 18437       break;
 18439     default: {
 18440       assert( winMutex_isInit==1 );
 18441       assert( iType-2 >= 0 );
 18442       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
 18443       p = &winMutex_staticMutexes[iType-2];
 18444 #ifdef SQLITE_DEBUG
 18445       p->id = iType;
 18446 #endif
 18447       break;
 18450   return p;
 18454 /*
 18455 ** This routine deallocates a previously
 18456 ** allocated mutex.  SQLite is careful to deallocate every
 18457 ** mutex that it allocates.
 18458 */
 18459 static void winMutexFree(sqlite3_mutex *p){
 18460   assert( p );
 18461   assert( p->nRef==0 && p->owner==0 );
 18462   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 18463   DeleteCriticalSection(&p->mutex);
 18464   sqlite3_free(p);
 18467 /*
 18468 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 18469 ** to enter a mutex.  If another thread is already within the mutex,
 18470 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 18471 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 18472 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 18473 ** be entered multiple times by the same thread.  In such cases the,
 18474 ** mutex must be exited an equal number of times before another thread
 18475 ** can enter.  If the same thread tries to enter any other kind of mutex
 18476 ** more than once, the behavior is undefined.
 18477 */
 18478 static void winMutexEnter(sqlite3_mutex *p){
 18479 #ifdef SQLITE_DEBUG
 18480   DWORD tid = GetCurrentThreadId(); 
 18481   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 18482 #endif
 18483   EnterCriticalSection(&p->mutex);
 18484 #ifdef SQLITE_DEBUG
 18485   assert( p->nRef>0 || p->owner==0 );
 18486   p->owner = tid; 
 18487   p->nRef++;
 18488   if( p->trace ){
 18489     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18491 #endif
 18493 static int winMutexTry(sqlite3_mutex *p){
 18494 #ifndef NDEBUG
 18495   DWORD tid = GetCurrentThreadId(); 
 18496 #endif
 18497   int rc = SQLITE_BUSY;
 18498   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 18499   /*
 18500   ** The sqlite3_mutex_try() routine is very rarely used, and when it
 18501   ** is used it is merely an optimization.  So it is OK for it to always
 18502   ** fail.  
 18503   **
 18504   ** The TryEnterCriticalSection() interface is only available on WinNT.
 18505   ** And some windows compilers complain if you try to use it without
 18506   ** first doing some #defines that prevent SQLite from building on Win98.
 18507   ** For that reason, we will omit this optimization for now.  See
 18508   ** ticket #2685.
 18509   */
 18510 #if 0
 18511   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
 18512     p->owner = tid;
 18513     p->nRef++;
 18514     rc = SQLITE_OK;
 18516 #else
 18517   UNUSED_PARAMETER(p);
 18518 #endif
 18519 #ifdef SQLITE_DEBUG
 18520   if( rc==SQLITE_OK && p->trace ){
 18521     printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18523 #endif
 18524   return rc;
 18527 /*
 18528 ** The sqlite3_mutex_leave() routine exits a mutex that was
 18529 ** previously entered by the same thread.  The behavior
 18530 ** is undefined if the mutex is not currently entered or
 18531 ** is not currently allocated.  SQLite will never do either.
 18532 */
 18533 static void winMutexLeave(sqlite3_mutex *p){
 18534 #ifndef NDEBUG
 18535   DWORD tid = GetCurrentThreadId();
 18536   assert( p->nRef>0 );
 18537   assert( p->owner==tid );
 18538   p->nRef--;
 18539   if( p->nRef==0 ) p->owner = 0;
 18540   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 18541 #endif
 18542   LeaveCriticalSection(&p->mutex);
 18543 #ifdef SQLITE_DEBUG
 18544   if( p->trace ){
 18545     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18547 #endif
 18550 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 18551   static const sqlite3_mutex_methods sMutex = {
 18552     winMutexInit,
 18553     winMutexEnd,
 18554     winMutexAlloc,
 18555     winMutexFree,
 18556     winMutexEnter,
 18557     winMutexTry,
 18558     winMutexLeave,
 18559 #ifdef SQLITE_DEBUG
 18560     winMutexHeld,
 18561     winMutexNotheld
 18562 #else
 18563     0,
 18565 #endif
 18566   };
 18568   return &sMutex;
 18570 #endif /* SQLITE_MUTEX_W32 */
 18572 /************** End of mutex_w32.c *******************************************/
 18573 /************** Begin file malloc.c ******************************************/
 18574 /*
 18575 ** 2001 September 15
 18576 **
 18577 ** The author disclaims copyright to this source code.  In place of
 18578 ** a legal notice, here is a blessing:
 18579 **
 18580 **    May you do good and not evil.
 18581 **    May you find forgiveness for yourself and forgive others.
 18582 **    May you share freely, never taking more than you give.
 18583 **
 18584 *************************************************************************
 18585 **
 18586 ** Memory allocation functions used throughout sqlite.
 18587 */
 18588 /* #include <stdarg.h> */
 18590 /*
 18591 ** Attempt to release up to n bytes of non-essential memory currently
 18592 ** held by SQLite. An example of non-essential memory is memory used to
 18593 ** cache database pages that are not currently in use.
 18594 */
 18595 SQLITE_API int sqlite3_release_memory(int n){
 18596 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 18597   return sqlite3PcacheReleaseMemory(n);
 18598 #else
 18599   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
 18600   ** is a no-op returning zero if SQLite is not compiled with
 18601   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
 18602   UNUSED_PARAMETER(n);
 18603   return 0;
 18604 #endif
 18607 /*
 18608 ** An instance of the following object records the location of
 18609 ** each unused scratch buffer.
 18610 */
 18611 typedef struct ScratchFreeslot {
 18612   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
 18613 } ScratchFreeslot;
 18615 /*
 18616 ** State information local to the memory allocation subsystem.
 18617 */
 18618 static SQLITE_WSD struct Mem0Global {
 18619   sqlite3_mutex *mutex;         /* Mutex to serialize access */
 18621   /*
 18622   ** The alarm callback and its arguments.  The mem0.mutex lock will
 18623   ** be held while the callback is running.  Recursive calls into
 18624   ** the memory subsystem are allowed, but no new callbacks will be
 18625   ** issued.
 18626   */
 18627   sqlite3_int64 alarmThreshold;
 18628   void (*alarmCallback)(void*, sqlite3_int64,int);
 18629   void *alarmArg;
 18631   /*
 18632   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
 18633   ** (so that a range test can be used to determine if an allocation
 18634   ** being freed came from pScratch) and a pointer to the list of
 18635   ** unused scratch allocations.
 18636   */
 18637   void *pScratchEnd;
 18638   ScratchFreeslot *pScratchFree;
 18639   u32 nScratchFree;
 18641   /*
 18642   ** True if heap is nearly "full" where "full" is defined by the
 18643   ** sqlite3_soft_heap_limit() setting.
 18644   */
 18645   int nearlyFull;
 18646 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
 18648 #define mem0 GLOBAL(struct Mem0Global, mem0)
 18650 /*
 18651 ** This routine runs when the memory allocator sees that the
 18652 ** total memory allocation is about to exceed the soft heap
 18653 ** limit.
 18654 */
 18655 static void softHeapLimitEnforcer(
 18656   void *NotUsed, 
 18657   sqlite3_int64 NotUsed2,
 18658   int allocSize
 18659 ){
 18660   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 18661   sqlite3_release_memory(allocSize);
 18664 /*
 18665 ** Change the alarm callback
 18666 */
 18667 static int sqlite3MemoryAlarm(
 18668   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
 18669   void *pArg,
 18670   sqlite3_int64 iThreshold
 18671 ){
 18672   int nUsed;
 18673   sqlite3_mutex_enter(mem0.mutex);
 18674   mem0.alarmCallback = xCallback;
 18675   mem0.alarmArg = pArg;
 18676   mem0.alarmThreshold = iThreshold;
 18677   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 18678   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
 18679   sqlite3_mutex_leave(mem0.mutex);
 18680   return SQLITE_OK;
 18683 #ifndef SQLITE_OMIT_DEPRECATED
 18684 /*
 18685 ** Deprecated external interface.  Internal/core SQLite code
 18686 ** should call sqlite3MemoryAlarm.
 18687 */
 18688 SQLITE_API int sqlite3_memory_alarm(
 18689   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
 18690   void *pArg,
 18691   sqlite3_int64 iThreshold
 18692 ){
 18693   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
 18695 #endif
 18697 /*
 18698 ** Set the soft heap-size limit for the library. Passing a zero or 
 18699 ** negative value indicates no limit.
 18700 */
 18701 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
 18702   sqlite3_int64 priorLimit;
 18703   sqlite3_int64 excess;
 18704 #ifndef SQLITE_OMIT_AUTOINIT
 18705   int rc = sqlite3_initialize();
 18706   if( rc ) return -1;
 18707 #endif
 18708   sqlite3_mutex_enter(mem0.mutex);
 18709   priorLimit = mem0.alarmThreshold;
 18710   sqlite3_mutex_leave(mem0.mutex);
 18711   if( n<0 ) return priorLimit;
 18712   if( n>0 ){
 18713     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
 18714   }else{
 18715     sqlite3MemoryAlarm(0, 0, 0);
 18717   excess = sqlite3_memory_used() - n;
 18718   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
 18719   return priorLimit;
 18721 SQLITE_API void sqlite3_soft_heap_limit(int n){
 18722   if( n<0 ) n = 0;
 18723   sqlite3_soft_heap_limit64(n);
 18726 /*
 18727 ** Initialize the memory allocation subsystem.
 18728 */
 18729 SQLITE_PRIVATE int sqlite3MallocInit(void){
 18730   if( sqlite3GlobalConfig.m.xMalloc==0 ){
 18731     sqlite3MemSetDefault();
 18733   memset(&mem0, 0, sizeof(mem0));
 18734   if( sqlite3GlobalConfig.bCoreMutex ){
 18735     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 18737   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
 18738       && sqlite3GlobalConfig.nScratch>0 ){
 18739     int i, n, sz;
 18740     ScratchFreeslot *pSlot;
 18741     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
 18742     sqlite3GlobalConfig.szScratch = sz;
 18743     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
 18744     n = sqlite3GlobalConfig.nScratch;
 18745     mem0.pScratchFree = pSlot;
 18746     mem0.nScratchFree = n;
 18747     for(i=0; i<n-1; i++){
 18748       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
 18749       pSlot = pSlot->pNext;
 18751     pSlot->pNext = 0;
 18752     mem0.pScratchEnd = (void*)&pSlot[1];
 18753   }else{
 18754     mem0.pScratchEnd = 0;
 18755     sqlite3GlobalConfig.pScratch = 0;
 18756     sqlite3GlobalConfig.szScratch = 0;
 18757     sqlite3GlobalConfig.nScratch = 0;
 18759   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
 18760       || sqlite3GlobalConfig.nPage<1 ){
 18761     sqlite3GlobalConfig.pPage = 0;
 18762     sqlite3GlobalConfig.szPage = 0;
 18763     sqlite3GlobalConfig.nPage = 0;
 18765   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
 18768 /*
 18769 ** Return true if the heap is currently under memory pressure - in other
 18770 ** words if the amount of heap used is close to the limit set by
 18771 ** sqlite3_soft_heap_limit().
 18772 */
 18773 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
 18774   return mem0.nearlyFull;
 18777 /*
 18778 ** Deinitialize the memory allocation subsystem.
 18779 */
 18780 SQLITE_PRIVATE void sqlite3MallocEnd(void){
 18781   if( sqlite3GlobalConfig.m.xShutdown ){
 18782     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
 18784   memset(&mem0, 0, sizeof(mem0));
 18787 /*
 18788 ** Return the amount of memory currently checked out.
 18789 */
 18790 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
 18791   int n, mx;
 18792   sqlite3_int64 res;
 18793   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
 18794   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
 18795   return res;
 18798 /*
 18799 ** Return the maximum amount of memory that has ever been
 18800 ** checked out since either the beginning of this process
 18801 ** or since the most recent reset.
 18802 */
 18803 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
 18804   int n, mx;
 18805   sqlite3_int64 res;
 18806   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
 18807   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
 18808   return res;
 18811 /*
 18812 ** Trigger the alarm 
 18813 */
 18814 static void sqlite3MallocAlarm(int nByte){
 18815   void (*xCallback)(void*,sqlite3_int64,int);
 18816   sqlite3_int64 nowUsed;
 18817   void *pArg;
 18818   if( mem0.alarmCallback==0 ) return;
 18819   xCallback = mem0.alarmCallback;
 18820   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 18821   pArg = mem0.alarmArg;
 18822   mem0.alarmCallback = 0;
 18823   sqlite3_mutex_leave(mem0.mutex);
 18824   xCallback(pArg, nowUsed, nByte);
 18825   sqlite3_mutex_enter(mem0.mutex);
 18826   mem0.alarmCallback = xCallback;
 18827   mem0.alarmArg = pArg;
 18830 /*
 18831 ** Do a memory allocation with statistics and alarms.  Assume the
 18832 ** lock is already held.
 18833 */
 18834 static int mallocWithAlarm(int n, void **pp){
 18835   int nFull;
 18836   void *p;
 18837   assert( sqlite3_mutex_held(mem0.mutex) );
 18838   nFull = sqlite3GlobalConfig.m.xRoundup(n);
 18839   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
 18840   if( mem0.alarmCallback!=0 ){
 18841     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 18842     if( nUsed >= mem0.alarmThreshold - nFull ){
 18843       mem0.nearlyFull = 1;
 18844       sqlite3MallocAlarm(nFull);
 18845     }else{
 18846       mem0.nearlyFull = 0;
 18849   p = sqlite3GlobalConfig.m.xMalloc(nFull);
 18850 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 18851   if( p==0 && mem0.alarmCallback ){
 18852     sqlite3MallocAlarm(nFull);
 18853     p = sqlite3GlobalConfig.m.xMalloc(nFull);
 18855 #endif
 18856   if( p ){
 18857     nFull = sqlite3MallocSize(p);
 18858     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
 18859     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
 18861   *pp = p;
 18862   return nFull;
 18865 /*
 18866 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
 18867 ** assumes the memory subsystem has already been initialized.
 18868 */
 18869 SQLITE_PRIVATE void *sqlite3Malloc(int n){
 18870   void *p;
 18871   if( n<=0               /* IMP: R-65312-04917 */ 
 18872    || n>=0x7fffff00
 18873   ){
 18874     /* A memory allocation of a number of bytes which is near the maximum
 18875     ** signed integer value might cause an integer overflow inside of the
 18876     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
 18877     ** 255 bytes of overhead.  SQLite itself will never use anything near
 18878     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
 18879     p = 0;
 18880   }else if( sqlite3GlobalConfig.bMemstat ){
 18881     sqlite3_mutex_enter(mem0.mutex);
 18882     mallocWithAlarm(n, &p);
 18883     sqlite3_mutex_leave(mem0.mutex);
 18884   }else{
 18885     p = sqlite3GlobalConfig.m.xMalloc(n);
 18887   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
 18888   return p;
 18891 /*
 18892 ** This version of the memory allocation is for use by the application.
 18893 ** First make sure the memory subsystem is initialized, then do the
 18894 ** allocation.
 18895 */
 18896 SQLITE_API void *sqlite3_malloc(int n){
 18897 #ifndef SQLITE_OMIT_AUTOINIT
 18898   if( sqlite3_initialize() ) return 0;
 18899 #endif
 18900   return sqlite3Malloc(n);
 18903 /*
 18904 ** Each thread may only have a single outstanding allocation from
 18905 ** xScratchMalloc().  We verify this constraint in the single-threaded
 18906 ** case by setting scratchAllocOut to 1 when an allocation
 18907 ** is outstanding clearing it when the allocation is freed.
 18908 */
 18909 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 18910 static int scratchAllocOut = 0;
 18911 #endif
 18914 /*
 18915 ** Allocate memory that is to be used and released right away.
 18916 ** This routine is similar to alloca() in that it is not intended
 18917 ** for situations where the memory might be held long-term.  This
 18918 ** routine is intended to get memory to old large transient data
 18919 ** structures that would not normally fit on the stack of an
 18920 ** embedded processor.
 18921 */
 18922 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
 18923   void *p;
 18924   assert( n>0 );
 18926   sqlite3_mutex_enter(mem0.mutex);
 18927   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
 18928     p = mem0.pScratchFree;
 18929     mem0.pScratchFree = mem0.pScratchFree->pNext;
 18930     mem0.nScratchFree--;
 18931     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
 18932     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
 18933     sqlite3_mutex_leave(mem0.mutex);
 18934   }else{
 18935     if( sqlite3GlobalConfig.bMemstat ){
 18936       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
 18937       n = mallocWithAlarm(n, &p);
 18938       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
 18939       sqlite3_mutex_leave(mem0.mutex);
 18940     }else{
 18941       sqlite3_mutex_leave(mem0.mutex);
 18942       p = sqlite3GlobalConfig.m.xMalloc(n);
 18944     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
 18946   assert( sqlite3_mutex_notheld(mem0.mutex) );
 18949 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 18950   /* Verify that no more than two scratch allocations per thread
 18951   ** are outstanding at one time.  (This is only checked in the
 18952   ** single-threaded case since checking in the multi-threaded case
 18953   ** would be much more complicated.) */
 18954   assert( scratchAllocOut<=1 );
 18955   if( p ) scratchAllocOut++;
 18956 #endif
 18958   return p;
 18960 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
 18961   if( p ){
 18963 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 18964     /* Verify that no more than two scratch allocation per thread
 18965     ** is outstanding at one time.  (This is only checked in the
 18966     ** single-threaded case since checking in the multi-threaded case
 18967     ** would be much more complicated.) */
 18968     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
 18969     scratchAllocOut--;
 18970 #endif
 18972     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
 18973       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
 18974       ScratchFreeslot *pSlot;
 18975       pSlot = (ScratchFreeslot*)p;
 18976       sqlite3_mutex_enter(mem0.mutex);
 18977       pSlot->pNext = mem0.pScratchFree;
 18978       mem0.pScratchFree = pSlot;
 18979       mem0.nScratchFree++;
 18980       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
 18981       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
 18982       sqlite3_mutex_leave(mem0.mutex);
 18983     }else{
 18984       /* Release memory back to the heap */
 18985       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
 18986       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
 18987       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 18988       if( sqlite3GlobalConfig.bMemstat ){
 18989         int iSize = sqlite3MallocSize(p);
 18990         sqlite3_mutex_enter(mem0.mutex);
 18991         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
 18992         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
 18993         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
 18994         sqlite3GlobalConfig.m.xFree(p);
 18995         sqlite3_mutex_leave(mem0.mutex);
 18996       }else{
 18997         sqlite3GlobalConfig.m.xFree(p);
 19003 /*
 19004 ** TRUE if p is a lookaside memory allocation from db
 19005 */
 19006 #ifndef SQLITE_OMIT_LOOKASIDE
 19007 static int isLookaside(sqlite3 *db, void *p){
 19008   return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
 19010 #else
 19011 #define isLookaside(A,B) 0
 19012 #endif
 19014 /*
 19015 ** Return the size of a memory allocation previously obtained from
 19016 ** sqlite3Malloc() or sqlite3_malloc().
 19017 */
 19018 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
 19019   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 19020   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 19021   return sqlite3GlobalConfig.m.xSize(p);
 19023 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
 19024   assert( db==0 || sqlite3_mutex_held(db->mutex) );
 19025   if( db && isLookaside(db, p) ){
 19026     return db->lookaside.sz;
 19027   }else{
 19028     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 19029     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 19030     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
 19031     return sqlite3GlobalConfig.m.xSize(p);
 19035 /*
 19036 ** Free memory previously obtained from sqlite3Malloc().
 19037 */
 19038 SQLITE_API void sqlite3_free(void *p){
 19039   if( p==0 ) return;  /* IMP: R-49053-54554 */
 19040   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 19041   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 19042   if( sqlite3GlobalConfig.bMemstat ){
 19043     sqlite3_mutex_enter(mem0.mutex);
 19044     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
 19045     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
 19046     sqlite3GlobalConfig.m.xFree(p);
 19047     sqlite3_mutex_leave(mem0.mutex);
 19048   }else{
 19049     sqlite3GlobalConfig.m.xFree(p);
 19053 /*
 19054 ** Free memory that might be associated with a particular database
 19055 ** connection.
 19056 */
 19057 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
 19058   assert( db==0 || sqlite3_mutex_held(db->mutex) );
 19059   if( db ){
 19060     if( db->pnBytesFreed ){
 19061       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
 19062       return;
 19064     if( isLookaside(db, p) ){
 19065       LookasideSlot *pBuf = (LookasideSlot*)p;
 19066 #if SQLITE_DEBUG
 19067       /* Trash all content in the buffer being freed */
 19068       memset(p, 0xaa, db->lookaside.sz);
 19069 #endif
 19070       pBuf->pNext = db->lookaside.pFree;
 19071       db->lookaside.pFree = pBuf;
 19072       db->lookaside.nOut--;
 19073       return;
 19076   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 19077   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 19078   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
 19079   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 19080   sqlite3_free(p);
 19083 /*
 19084 ** Change the size of an existing memory allocation
 19085 */
 19086 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
 19087   int nOld, nNew, nDiff;
 19088   void *pNew;
 19089   if( pOld==0 ){
 19090     return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
 19092   if( nBytes<=0 ){
 19093     sqlite3_free(pOld); /* IMP: R-31593-10574 */
 19094     return 0;
 19096   if( nBytes>=0x7fffff00 ){
 19097     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
 19098     return 0;
 19100   nOld = sqlite3MallocSize(pOld);
 19101   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
 19102   ** argument to xRealloc is always a value returned by a prior call to
 19103   ** xRoundup. */
 19104   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
 19105   if( nOld==nNew ){
 19106     pNew = pOld;
 19107   }else if( sqlite3GlobalConfig.bMemstat ){
 19108     sqlite3_mutex_enter(mem0.mutex);
 19109     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
 19110     nDiff = nNew - nOld;
 19111     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
 19112           mem0.alarmThreshold-nDiff ){
 19113       sqlite3MallocAlarm(nDiff);
 19115     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
 19116     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
 19117     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 19118     if( pNew==0 && mem0.alarmCallback ){
 19119       sqlite3MallocAlarm(nBytes);
 19120       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 19122     if( pNew ){
 19123       nNew = sqlite3MallocSize(pNew);
 19124       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
 19126     sqlite3_mutex_leave(mem0.mutex);
 19127   }else{
 19128     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 19130   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
 19131   return pNew;
 19134 /*
 19135 ** The public interface to sqlite3Realloc.  Make sure that the memory
 19136 ** subsystem is initialized prior to invoking sqliteRealloc.
 19137 */
 19138 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
 19139 #ifndef SQLITE_OMIT_AUTOINIT
 19140   if( sqlite3_initialize() ) return 0;
 19141 #endif
 19142   return sqlite3Realloc(pOld, n);
 19146 /*
 19147 ** Allocate and zero memory.
 19148 */ 
 19149 SQLITE_PRIVATE void *sqlite3MallocZero(int n){
 19150   void *p = sqlite3Malloc(n);
 19151   if( p ){
 19152     memset(p, 0, n);
 19154   return p;
 19157 /*
 19158 ** Allocate and zero memory.  If the allocation fails, make
 19159 ** the mallocFailed flag in the connection pointer.
 19160 */
 19161 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
 19162   void *p = sqlite3DbMallocRaw(db, n);
 19163   if( p ){
 19164     memset(p, 0, n);
 19166   return p;
 19169 /*
 19170 ** Allocate and zero memory.  If the allocation fails, make
 19171 ** the mallocFailed flag in the connection pointer.
 19172 **
 19173 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
 19174 ** failure on the same database connection) then always return 0.
 19175 ** Hence for a particular database connection, once malloc starts
 19176 ** failing, it fails consistently until mallocFailed is reset.
 19177 ** This is an important assumption.  There are many places in the
 19178 ** code that do things like this:
 19179 **
 19180 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
 19181 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
 19182 **         if( b ) a[10] = 9;
 19183 **
 19184 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
 19185 ** that all prior mallocs (ex: "a") worked too.
 19186 */
 19187 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
 19188   void *p;
 19189   assert( db==0 || sqlite3_mutex_held(db->mutex) );
 19190   assert( db==0 || db->pnBytesFreed==0 );
 19191 #ifndef SQLITE_OMIT_LOOKASIDE
 19192   if( db ){
 19193     LookasideSlot *pBuf;
 19194     if( db->mallocFailed ){
 19195       return 0;
 19197     if( db->lookaside.bEnabled ){
 19198       if( n>db->lookaside.sz ){
 19199         db->lookaside.anStat[1]++;
 19200       }else if( (pBuf = db->lookaside.pFree)==0 ){
 19201         db->lookaside.anStat[2]++;
 19202       }else{
 19203         db->lookaside.pFree = pBuf->pNext;
 19204         db->lookaside.nOut++;
 19205         db->lookaside.anStat[0]++;
 19206         if( db->lookaside.nOut>db->lookaside.mxOut ){
 19207           db->lookaside.mxOut = db->lookaside.nOut;
 19209         return (void*)pBuf;
 19213 #else
 19214   if( db && db->mallocFailed ){
 19215     return 0;
 19217 #endif
 19218   p = sqlite3Malloc(n);
 19219   if( !p && db ){
 19220     db->mallocFailed = 1;
 19222   sqlite3MemdebugSetType(p, MEMTYPE_DB |
 19223          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
 19224   return p;
 19227 /*
 19228 ** Resize the block of memory pointed to by p to n bytes. If the
 19229 ** resize fails, set the mallocFailed flag in the connection object.
 19230 */
 19231 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
 19232   void *pNew = 0;
 19233   assert( db!=0 );
 19234   assert( sqlite3_mutex_held(db->mutex) );
 19235   if( db->mallocFailed==0 ){
 19236     if( p==0 ){
 19237       return sqlite3DbMallocRaw(db, n);
 19239     if( isLookaside(db, p) ){
 19240       if( n<=db->lookaside.sz ){
 19241         return p;
 19243       pNew = sqlite3DbMallocRaw(db, n);
 19244       if( pNew ){
 19245         memcpy(pNew, p, db->lookaside.sz);
 19246         sqlite3DbFree(db, p);
 19248     }else{
 19249       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 19250       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 19251       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 19252       pNew = sqlite3_realloc(p, n);
 19253       if( !pNew ){
 19254         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
 19255         db->mallocFailed = 1;
 19257       sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
 19258             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
 19261   return pNew;
 19264 /*
 19265 ** Attempt to reallocate p.  If the reallocation fails, then free p
 19266 ** and set the mallocFailed flag in the database connection.
 19267 */
 19268 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
 19269   void *pNew;
 19270   pNew = sqlite3DbRealloc(db, p, n);
 19271   if( !pNew ){
 19272     sqlite3DbFree(db, p);
 19274   return pNew;
 19277 /*
 19278 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
 19279 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
 19280 ** is because when memory debugging is turned on, these two functions are 
 19281 ** called via macros that record the current file and line number in the
 19282 ** ThreadData structure.
 19283 */
 19284 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
 19285   char *zNew;
 19286   size_t n;
 19287   if( z==0 ){
 19288     return 0;
 19290   n = sqlite3Strlen30(z) + 1;
 19291   assert( (n&0x7fffffff)==n );
 19292   zNew = sqlite3DbMallocRaw(db, (int)n);
 19293   if( zNew ){
 19294     memcpy(zNew, z, n);
 19296   return zNew;
 19298 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
 19299   char *zNew;
 19300   if( z==0 ){
 19301     return 0;
 19303   assert( (n&0x7fffffff)==n );
 19304   zNew = sqlite3DbMallocRaw(db, n+1);
 19305   if( zNew ){
 19306     memcpy(zNew, z, n);
 19307     zNew[n] = 0;
 19309   return zNew;
 19312 /*
 19313 ** Create a string from the zFromat argument and the va_list that follows.
 19314 ** Store the string in memory obtained from sqliteMalloc() and make *pz
 19315 ** point to that string.
 19316 */
 19317 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
 19318   va_list ap;
 19319   char *z;
 19321   va_start(ap, zFormat);
 19322   z = sqlite3VMPrintf(db, zFormat, ap);
 19323   va_end(ap);
 19324   sqlite3DbFree(db, *pz);
 19325   *pz = z;
 19329 /*
 19330 ** This function must be called before exiting any API function (i.e. 
 19331 ** returning control to the user) that has called sqlite3_malloc or
 19332 ** sqlite3_realloc.
 19333 **
 19334 ** The returned value is normally a copy of the second argument to this
 19335 ** function. However, if a malloc() failure has occurred since the previous
 19336 ** invocation SQLITE_NOMEM is returned instead. 
 19337 **
 19338 ** If the first argument, db, is not NULL and a malloc() error has occurred,
 19339 ** then the connection error-code (the value returned by sqlite3_errcode())
 19340 ** is set to SQLITE_NOMEM.
 19341 */
 19342 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
 19343   /* If the db handle is not NULL, then we must hold the connection handle
 19344   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
 19345   ** is unsafe, as is the call to sqlite3Error().
 19346   */
 19347   assert( !db || sqlite3_mutex_held(db->mutex) );
 19348   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
 19349     sqlite3Error(db, SQLITE_NOMEM, 0);
 19350     db->mallocFailed = 0;
 19351     rc = SQLITE_NOMEM;
 19353   return rc & (db ? db->errMask : 0xff);
 19356 /************** End of malloc.c **********************************************/
 19357 /************** Begin file printf.c ******************************************/
 19358 /*
 19359 ** The "printf" code that follows dates from the 1980's.  It is in
 19360 ** the public domain.  The original comments are included here for
 19361 ** completeness.  They are very out-of-date but might be useful as
 19362 ** an historical reference.  Most of the "enhancements" have been backed
 19363 ** out so that the functionality is now the same as standard printf().
 19364 **
 19365 **************************************************************************
 19366 **
 19367 ** This file contains code for a set of "printf"-like routines.  These
 19368 ** routines format strings much like the printf() from the standard C
 19369 ** library, though the implementation here has enhancements to support
 19370 ** SQLlite.
 19371 */
 19373 /*
 19374 ** Conversion types fall into various categories as defined by the
 19375 ** following enumeration.
 19376 */
 19377 #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
 19378 #define etFLOAT       2 /* Floating point.  %f */
 19379 #define etEXP         3 /* Exponentional notation. %e and %E */
 19380 #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
 19381 #define etSIZE        5 /* Return number of characters processed so far. %n */
 19382 #define etSTRING      6 /* Strings. %s */
 19383 #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
 19384 #define etPERCENT     8 /* Percent symbol. %% */
 19385 #define etCHARX       9 /* Characters. %c */
 19386 /* The rest are extensions, not normally found in printf() */
 19387 #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
 19388 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
 19389                           NULL pointers replaced by SQL NULL.  %Q */
 19390 #define etTOKEN      12 /* a pointer to a Token structure */
 19391 #define etSRCLIST    13 /* a pointer to a SrcList */
 19392 #define etPOINTER    14 /* The %p conversion */
 19393 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
 19394 #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
 19396 #define etINVALID     0 /* Any unrecognized conversion type */
 19399 /*
 19400 ** An "etByte" is an 8-bit unsigned value.
 19401 */
 19402 typedef unsigned char etByte;
 19404 /*
 19405 ** Each builtin conversion character (ex: the 'd' in "%d") is described
 19406 ** by an instance of the following structure
 19407 */
 19408 typedef struct et_info {   /* Information about each format field */
 19409   char fmttype;            /* The format field code letter */
 19410   etByte base;             /* The base for radix conversion */
 19411   etByte flags;            /* One or more of FLAG_ constants below */
 19412   etByte type;             /* Conversion paradigm */
 19413   etByte charset;          /* Offset into aDigits[] of the digits string */
 19414   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
 19415 } et_info;
 19417 /*
 19418 ** Allowed values for et_info.flags
 19419 */
 19420 #define FLAG_SIGNED  1     /* True if the value to convert is signed */
 19421 #define FLAG_INTERN  2     /* True if for internal use only */
 19422 #define FLAG_STRING  4     /* Allow infinity precision */
 19425 /*
 19426 ** The following table is searched linearly, so it is good to put the
 19427 ** most frequently used conversion types first.
 19428 */
 19429 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
 19430 static const char aPrefix[] = "-x0\000X0";
 19431 static const et_info fmtinfo[] = {
 19432   {  'd', 10, 1, etRADIX,      0,  0 },
 19433   {  's',  0, 4, etSTRING,     0,  0 },
 19434   {  'g',  0, 1, etGENERIC,    30, 0 },
 19435   {  'z',  0, 4, etDYNSTRING,  0,  0 },
 19436   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
 19437   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
 19438   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
 19439   {  'c',  0, 0, etCHARX,      0,  0 },
 19440   {  'o',  8, 0, etRADIX,      0,  2 },
 19441   {  'u', 10, 0, etRADIX,      0,  0 },
 19442   {  'x', 16, 0, etRADIX,      16, 1 },
 19443   {  'X', 16, 0, etRADIX,      0,  4 },
 19444 #ifndef SQLITE_OMIT_FLOATING_POINT
 19445   {  'f',  0, 1, etFLOAT,      0,  0 },
 19446   {  'e',  0, 1, etEXP,        30, 0 },
 19447   {  'E',  0, 1, etEXP,        14, 0 },
 19448   {  'G',  0, 1, etGENERIC,    14, 0 },
 19449 #endif
 19450   {  'i', 10, 1, etRADIX,      0,  0 },
 19451   {  'n',  0, 0, etSIZE,       0,  0 },
 19452   {  '%',  0, 0, etPERCENT,    0,  0 },
 19453   {  'p', 16, 0, etPOINTER,    0,  1 },
 19455 /* All the rest have the FLAG_INTERN bit set and are thus for internal
 19456 ** use only */
 19457   {  'T',  0, 2, etTOKEN,      0,  0 },
 19458   {  'S',  0, 2, etSRCLIST,    0,  0 },
 19459   {  'r', 10, 3, etORDINAL,    0,  0 },
 19460 };
 19462 /*
 19463 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
 19464 ** conversions will work.
 19465 */
 19466 #ifndef SQLITE_OMIT_FLOATING_POINT
 19467 /*
 19468 ** "*val" is a double such that 0.1 <= *val < 10.0
 19469 ** Return the ascii code for the leading digit of *val, then
 19470 ** multiply "*val" by 10.0 to renormalize.
 19471 **
 19472 ** Example:
 19473 **     input:     *val = 3.14159
 19474 **     output:    *val = 1.4159    function return = '3'
 19475 **
 19476 ** The counter *cnt is incremented each time.  After counter exceeds
 19477 ** 16 (the number of significant digits in a 64-bit float) '0' is
 19478 ** always returned.
 19479 */
 19480 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
 19481   int digit;
 19482   LONGDOUBLE_TYPE d;
 19483   if( (*cnt)<=0 ) return '0';
 19484   (*cnt)--;
 19485   digit = (int)*val;
 19486   d = digit;
 19487   digit += '0';
 19488   *val = (*val - d)*10.0;
 19489   return (char)digit;
 19491 #endif /* SQLITE_OMIT_FLOATING_POINT */
 19493 /*
 19494 ** Append N space characters to the given string buffer.
 19495 */
 19496 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
 19497   static const char zSpaces[] = "                             ";
 19498   while( N>=(int)sizeof(zSpaces)-1 ){
 19499     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
 19500     N -= sizeof(zSpaces)-1;
 19502   if( N>0 ){
 19503     sqlite3StrAccumAppend(pAccum, zSpaces, N);
 19507 /*
 19508 ** On machines with a small stack size, you can redefine the
 19509 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
 19510 */
 19511 #ifndef SQLITE_PRINT_BUF_SIZE
 19512 # define SQLITE_PRINT_BUF_SIZE 70
 19513 #endif
 19514 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
 19516 /*
 19517 ** Render a string given by "fmt" into the StrAccum object.
 19518 */
 19519 SQLITE_PRIVATE void sqlite3VXPrintf(
 19520   StrAccum *pAccum,                  /* Accumulate results here */
 19521   int useExtended,                   /* Allow extended %-conversions */
 19522   const char *fmt,                   /* Format string */
 19523   va_list ap                         /* arguments */
 19524 ){
 19525   int c;                     /* Next character in the format string */
 19526   char *bufpt;               /* Pointer to the conversion buffer */
 19527   int precision;             /* Precision of the current field */
 19528   int length;                /* Length of the field */
 19529   int idx;                   /* A general purpose loop counter */
 19530   int width;                 /* Width of the current field */
 19531   etByte flag_leftjustify;   /* True if "-" flag is present */
 19532   etByte flag_plussign;      /* True if "+" flag is present */
 19533   etByte flag_blanksign;     /* True if " " flag is present */
 19534   etByte flag_alternateform; /* True if "#" flag is present */
 19535   etByte flag_altform2;      /* True if "!" flag is present */
 19536   etByte flag_zeropad;       /* True if field width constant starts with zero */
 19537   etByte flag_long;          /* True if "l" flag is present */
 19538   etByte flag_longlong;      /* True if the "ll" flag is present */
 19539   etByte done;               /* Loop termination flag */
 19540   etByte xtype = 0;          /* Conversion paradigm */
 19541   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
 19542   sqlite_uint64 longvalue;   /* Value for integer types */
 19543   LONGDOUBLE_TYPE realvalue; /* Value for real types */
 19544   const et_info *infop;      /* Pointer to the appropriate info structure */
 19545   char *zOut;                /* Rendering buffer */
 19546   int nOut;                  /* Size of the rendering buffer */
 19547   char *zExtra;              /* Malloced memory used by some conversion */
 19548 #ifndef SQLITE_OMIT_FLOATING_POINT
 19549   int  exp, e2;              /* exponent of real numbers */
 19550   int nsd;                   /* Number of significant digits returned */
 19551   double rounder;            /* Used for rounding floating point values */
 19552   etByte flag_dp;            /* True if decimal point should be shown */
 19553   etByte flag_rtz;           /* True if trailing zeros should be removed */
 19554 #endif
 19555   char buf[etBUFSIZE];       /* Conversion buffer */
 19557   bufpt = 0;
 19558   for(; (c=(*fmt))!=0; ++fmt){
 19559     if( c!='%' ){
 19560       int amt;
 19561       bufpt = (char *)fmt;
 19562       amt = 1;
 19563       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
 19564       sqlite3StrAccumAppend(pAccum, bufpt, amt);
 19565       if( c==0 ) break;
 19567     if( (c=(*++fmt))==0 ){
 19568       sqlite3StrAccumAppend(pAccum, "%", 1);
 19569       break;
 19571     /* Find out what flags are present */
 19572     flag_leftjustify = flag_plussign = flag_blanksign = 
 19573      flag_alternateform = flag_altform2 = flag_zeropad = 0;
 19574     done = 0;
 19575     do{
 19576       switch( c ){
 19577         case '-':   flag_leftjustify = 1;     break;
 19578         case '+':   flag_plussign = 1;        break;
 19579         case ' ':   flag_blanksign = 1;       break;
 19580         case '#':   flag_alternateform = 1;   break;
 19581         case '!':   flag_altform2 = 1;        break;
 19582         case '0':   flag_zeropad = 1;         break;
 19583         default:    done = 1;                 break;
 19585     }while( !done && (c=(*++fmt))!=0 );
 19586     /* Get the field width */
 19587     width = 0;
 19588     if( c=='*' ){
 19589       width = va_arg(ap,int);
 19590       if( width<0 ){
 19591         flag_leftjustify = 1;
 19592         width = -width;
 19594       c = *++fmt;
 19595     }else{
 19596       while( c>='0' && c<='9' ){
 19597         width = width*10 + c - '0';
 19598         c = *++fmt;
 19601     /* Get the precision */
 19602     if( c=='.' ){
 19603       precision = 0;
 19604       c = *++fmt;
 19605       if( c=='*' ){
 19606         precision = va_arg(ap,int);
 19607         if( precision<0 ) precision = -precision;
 19608         c = *++fmt;
 19609       }else{
 19610         while( c>='0' && c<='9' ){
 19611           precision = precision*10 + c - '0';
 19612           c = *++fmt;
 19615     }else{
 19616       precision = -1;
 19618     /* Get the conversion type modifier */
 19619     if( c=='l' ){
 19620       flag_long = 1;
 19621       c = *++fmt;
 19622       if( c=='l' ){
 19623         flag_longlong = 1;
 19624         c = *++fmt;
 19625       }else{
 19626         flag_longlong = 0;
 19628     }else{
 19629       flag_long = flag_longlong = 0;
 19631     /* Fetch the info entry for the field */
 19632     infop = &fmtinfo[0];
 19633     xtype = etINVALID;
 19634     for(idx=0; idx<ArraySize(fmtinfo); idx++){
 19635       if( c==fmtinfo[idx].fmttype ){
 19636         infop = &fmtinfo[idx];
 19637         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
 19638           xtype = infop->type;
 19639         }else{
 19640           return;
 19642         break;
 19645     zExtra = 0;
 19647     /*
 19648     ** At this point, variables are initialized as follows:
 19649     **
 19650     **   flag_alternateform          TRUE if a '#' is present.
 19651     **   flag_altform2               TRUE if a '!' is present.
 19652     **   flag_plussign               TRUE if a '+' is present.
 19653     **   flag_leftjustify            TRUE if a '-' is present or if the
 19654     **                               field width was negative.
 19655     **   flag_zeropad                TRUE if the width began with 0.
 19656     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
 19657     **                               the conversion character.
 19658     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
 19659     **                               the conversion character.
 19660     **   flag_blanksign              TRUE if a ' ' is present.
 19661     **   width                       The specified field width.  This is
 19662     **                               always non-negative.  Zero is the default.
 19663     **   precision                   The specified precision.  The default
 19664     **                               is -1.
 19665     **   xtype                       The class of the conversion.
 19666     **   infop                       Pointer to the appropriate info struct.
 19667     */
 19668     switch( xtype ){
 19669       case etPOINTER:
 19670         flag_longlong = sizeof(char*)==sizeof(i64);
 19671         flag_long = sizeof(char*)==sizeof(long int);
 19672         /* Fall through into the next case */
 19673       case etORDINAL:
 19674       case etRADIX:
 19675         if( infop->flags & FLAG_SIGNED ){
 19676           i64 v;
 19677           if( flag_longlong ){
 19678             v = va_arg(ap,i64);
 19679           }else if( flag_long ){
 19680             v = va_arg(ap,long int);
 19681           }else{
 19682             v = va_arg(ap,int);
 19684           if( v<0 ){
 19685             if( v==SMALLEST_INT64 ){
 19686               longvalue = ((u64)1)<<63;
 19687             }else{
 19688               longvalue = -v;
 19690             prefix = '-';
 19691           }else{
 19692             longvalue = v;
 19693             if( flag_plussign )        prefix = '+';
 19694             else if( flag_blanksign )  prefix = ' ';
 19695             else                       prefix = 0;
 19697         }else{
 19698           if( flag_longlong ){
 19699             longvalue = va_arg(ap,u64);
 19700           }else if( flag_long ){
 19701             longvalue = va_arg(ap,unsigned long int);
 19702           }else{
 19703             longvalue = va_arg(ap,unsigned int);
 19705           prefix = 0;
 19707         if( longvalue==0 ) flag_alternateform = 0;
 19708         if( flag_zeropad && precision<width-(prefix!=0) ){
 19709           precision = width-(prefix!=0);
 19711         if( precision<etBUFSIZE-10 ){
 19712           nOut = etBUFSIZE;
 19713           zOut = buf;
 19714         }else{
 19715           nOut = precision + 10;
 19716           zOut = zExtra = sqlite3Malloc( nOut );
 19717           if( zOut==0 ){
 19718             pAccum->mallocFailed = 1;
 19719             return;
 19722         bufpt = &zOut[nOut-1];
 19723         if( xtype==etORDINAL ){
 19724           static const char zOrd[] = "thstndrd";
 19725           int x = (int)(longvalue % 10);
 19726           if( x>=4 || (longvalue/10)%10==1 ){
 19727             x = 0;
 19729           *(--bufpt) = zOrd[x*2+1];
 19730           *(--bufpt) = zOrd[x*2];
 19733           register const char *cset;      /* Use registers for speed */
 19734           register int base;
 19735           cset = &aDigits[infop->charset];
 19736           base = infop->base;
 19737           do{                                           /* Convert to ascii */
 19738             *(--bufpt) = cset[longvalue%base];
 19739             longvalue = longvalue/base;
 19740           }while( longvalue>0 );
 19742         length = (int)(&zOut[nOut-1]-bufpt);
 19743         for(idx=precision-length; idx>0; idx--){
 19744           *(--bufpt) = '0';                             /* Zero pad */
 19746         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
 19747         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
 19748           const char *pre;
 19749           char x;
 19750           pre = &aPrefix[infop->prefix];
 19751           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
 19753         length = (int)(&zOut[nOut-1]-bufpt);
 19754         break;
 19755       case etFLOAT:
 19756       case etEXP:
 19757       case etGENERIC:
 19758         realvalue = va_arg(ap,double);
 19759 #ifdef SQLITE_OMIT_FLOATING_POINT
 19760         length = 0;
 19761 #else
 19762         if( precision<0 ) precision = 6;         /* Set default precision */
 19763         if( realvalue<0.0 ){
 19764           realvalue = -realvalue;
 19765           prefix = '-';
 19766         }else{
 19767           if( flag_plussign )          prefix = '+';
 19768           else if( flag_blanksign )    prefix = ' ';
 19769           else                         prefix = 0;
 19771         if( xtype==etGENERIC && precision>0 ) precision--;
 19772 #if 0
 19773         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
 19774         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
 19775 #else
 19776         /* It makes more sense to use 0.5 */
 19777         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
 19778 #endif
 19779         if( xtype==etFLOAT ) realvalue += rounder;
 19780         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
 19781         exp = 0;
 19782         if( sqlite3IsNaN((double)realvalue) ){
 19783           bufpt = "NaN";
 19784           length = 3;
 19785           break;
 19787         if( realvalue>0.0 ){
 19788           LONGDOUBLE_TYPE scale = 1.0;
 19789           while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
 19790           while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
 19791           while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
 19792           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
 19793           realvalue /= scale;
 19794           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
 19795           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
 19796           if( exp>350 ){
 19797             if( prefix=='-' ){
 19798               bufpt = "-Inf";
 19799             }else if( prefix=='+' ){
 19800               bufpt = "+Inf";
 19801             }else{
 19802               bufpt = "Inf";
 19804             length = sqlite3Strlen30(bufpt);
 19805             break;
 19808         bufpt = buf;
 19809         /*
 19810         ** If the field type is etGENERIC, then convert to either etEXP
 19811         ** or etFLOAT, as appropriate.
 19812         */
 19813         if( xtype!=etFLOAT ){
 19814           realvalue += rounder;
 19815           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
 19817         if( xtype==etGENERIC ){
 19818           flag_rtz = !flag_alternateform;
 19819           if( exp<-4 || exp>precision ){
 19820             xtype = etEXP;
 19821           }else{
 19822             precision = precision - exp;
 19823             xtype = etFLOAT;
 19825         }else{
 19826           flag_rtz = flag_altform2;
 19828         if( xtype==etEXP ){
 19829           e2 = 0;
 19830         }else{
 19831           e2 = exp;
 19833         if( e2+precision+width > etBUFSIZE - 15 ){
 19834           bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
 19835           if( bufpt==0 ){
 19836             pAccum->mallocFailed = 1;
 19837             return;
 19840         zOut = bufpt;
 19841         nsd = 16 + flag_altform2*10;
 19842         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
 19843         /* The sign in front of the number */
 19844         if( prefix ){
 19845           *(bufpt++) = prefix;
 19847         /* Digits prior to the decimal point */
 19848         if( e2<0 ){
 19849           *(bufpt++) = '0';
 19850         }else{
 19851           for(; e2>=0; e2--){
 19852             *(bufpt++) = et_getdigit(&realvalue,&nsd);
 19855         /* The decimal point */
 19856         if( flag_dp ){
 19857           *(bufpt++) = '.';
 19859         /* "0" digits after the decimal point but before the first
 19860         ** significant digit of the number */
 19861         for(e2++; e2<0; precision--, e2++){
 19862           assert( precision>0 );
 19863           *(bufpt++) = '0';
 19865         /* Significant digits after the decimal point */
 19866         while( (precision--)>0 ){
 19867           *(bufpt++) = et_getdigit(&realvalue,&nsd);
 19869         /* Remove trailing zeros and the "." if no digits follow the "." */
 19870         if( flag_rtz && flag_dp ){
 19871           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
 19872           assert( bufpt>zOut );
 19873           if( bufpt[-1]=='.' ){
 19874             if( flag_altform2 ){
 19875               *(bufpt++) = '0';
 19876             }else{
 19877               *(--bufpt) = 0;
 19881         /* Add the "eNNN" suffix */
 19882         if( xtype==etEXP ){
 19883           *(bufpt++) = aDigits[infop->charset];
 19884           if( exp<0 ){
 19885             *(bufpt++) = '-'; exp = -exp;
 19886           }else{
 19887             *(bufpt++) = '+';
 19889           if( exp>=100 ){
 19890             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
 19891             exp %= 100;
 19893           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
 19894           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
 19896         *bufpt = 0;
 19898         /* The converted number is in buf[] and zero terminated. Output it.
 19899         ** Note that the number is in the usual order, not reversed as with
 19900         ** integer conversions. */
 19901         length = (int)(bufpt-zOut);
 19902         bufpt = zOut;
 19904         /* Special case:  Add leading zeros if the flag_zeropad flag is
 19905         ** set and we are not left justified */
 19906         if( flag_zeropad && !flag_leftjustify && length < width){
 19907           int i;
 19908           int nPad = width - length;
 19909           for(i=width; i>=nPad; i--){
 19910             bufpt[i] = bufpt[i-nPad];
 19912           i = prefix!=0;
 19913           while( nPad-- ) bufpt[i++] = '0';
 19914           length = width;
 19916 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
 19917         break;
 19918       case etSIZE:
 19919         *(va_arg(ap,int*)) = pAccum->nChar;
 19920         length = width = 0;
 19921         break;
 19922       case etPERCENT:
 19923         buf[0] = '%';
 19924         bufpt = buf;
 19925         length = 1;
 19926         break;
 19927       case etCHARX:
 19928         c = va_arg(ap,int);
 19929         buf[0] = (char)c;
 19930         if( precision>=0 ){
 19931           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
 19932           length = precision;
 19933         }else{
 19934           length =1;
 19936         bufpt = buf;
 19937         break;
 19938       case etSTRING:
 19939       case etDYNSTRING:
 19940         bufpt = va_arg(ap,char*);
 19941         if( bufpt==0 ){
 19942           bufpt = "";
 19943         }else if( xtype==etDYNSTRING ){
 19944           zExtra = bufpt;
 19946         if( precision>=0 ){
 19947           for(length=0; length<precision && bufpt[length]; length++){}
 19948         }else{
 19949           length = sqlite3Strlen30(bufpt);
 19951         break;
 19952       case etSQLESCAPE:
 19953       case etSQLESCAPE2:
 19954       case etSQLESCAPE3: {
 19955         int i, j, k, n, isnull;
 19956         int needQuote;
 19957         char ch;
 19958         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
 19959         char *escarg = va_arg(ap,char*);
 19960         isnull = escarg==0;
 19961         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
 19962         k = precision;
 19963         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
 19964           if( ch==q )  n++;
 19966         needQuote = !isnull && xtype==etSQLESCAPE2;
 19967         n += i + 1 + needQuote*2;
 19968         if( n>etBUFSIZE ){
 19969           bufpt = zExtra = sqlite3Malloc( n );
 19970           if( bufpt==0 ){
 19971             pAccum->mallocFailed = 1;
 19972             return;
 19974         }else{
 19975           bufpt = buf;
 19977         j = 0;
 19978         if( needQuote ) bufpt[j++] = q;
 19979         k = i;
 19980         for(i=0; i<k; i++){
 19981           bufpt[j++] = ch = escarg[i];
 19982           if( ch==q ) bufpt[j++] = ch;
 19984         if( needQuote ) bufpt[j++] = q;
 19985         bufpt[j] = 0;
 19986         length = j;
 19987         /* The precision in %q and %Q means how many input characters to
 19988         ** consume, not the length of the output...
 19989         ** if( precision>=0 && precision<length ) length = precision; */
 19990         break;
 19992       case etTOKEN: {
 19993         Token *pToken = va_arg(ap, Token*);
 19994         if( pToken ){
 19995           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
 19997         length = width = 0;
 19998         break;
 20000       case etSRCLIST: {
 20001         SrcList *pSrc = va_arg(ap, SrcList*);
 20002         int k = va_arg(ap, int);
 20003         struct SrcList_item *pItem = &pSrc->a[k];
 20004         assert( k>=0 && k<pSrc->nSrc );
 20005         if( pItem->zDatabase ){
 20006           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
 20007           sqlite3StrAccumAppend(pAccum, ".", 1);
 20009         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
 20010         length = width = 0;
 20011         break;
 20013       default: {
 20014         assert( xtype==etINVALID );
 20015         return;
 20017     }/* End switch over the format type */
 20018     /*
 20019     ** The text of the conversion is pointed to by "bufpt" and is
 20020     ** "length" characters long.  The field width is "width".  Do
 20021     ** the output.
 20022     */
 20023     if( !flag_leftjustify ){
 20024       register int nspace;
 20025       nspace = width-length;
 20026       if( nspace>0 ){
 20027         sqlite3AppendSpace(pAccum, nspace);
 20030     if( length>0 ){
 20031       sqlite3StrAccumAppend(pAccum, bufpt, length);
 20033     if( flag_leftjustify ){
 20034       register int nspace;
 20035       nspace = width-length;
 20036       if( nspace>0 ){
 20037         sqlite3AppendSpace(pAccum, nspace);
 20040     sqlite3_free(zExtra);
 20041   }/* End for loop over the format string */
 20042 } /* End of function */
 20044 /*
 20045 ** Append N bytes of text from z to the StrAccum object.
 20046 */
 20047 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
 20048   assert( z!=0 || N==0 );
 20049   if( p->tooBig | p->mallocFailed ){
 20050     testcase(p->tooBig);
 20051     testcase(p->mallocFailed);
 20052     return;
 20054   assert( p->zText!=0 || p->nChar==0 );
 20055   if( N<0 ){
 20056     N = sqlite3Strlen30(z);
 20058   if( N==0 || NEVER(z==0) ){
 20059     return;
 20061   if( p->nChar+N >= p->nAlloc ){
 20062     char *zNew;
 20063     if( !p->useMalloc ){
 20064       p->tooBig = 1;
 20065       N = p->nAlloc - p->nChar - 1;
 20066       if( N<=0 ){
 20067         return;
 20069     }else{
 20070       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
 20071       i64 szNew = p->nChar;
 20072       szNew += N + 1;
 20073       if( szNew > p->mxAlloc ){
 20074         sqlite3StrAccumReset(p);
 20075         p->tooBig = 1;
 20076         return;
 20077       }else{
 20078         p->nAlloc = (int)szNew;
 20080       if( p->useMalloc==1 ){
 20081         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 20082       }else{
 20083         zNew = sqlite3_realloc(zOld, p->nAlloc);
 20085       if( zNew ){
 20086         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 20087         p->zText = zNew;
 20088       }else{
 20089         p->mallocFailed = 1;
 20090         sqlite3StrAccumReset(p);
 20091         return;
 20095   assert( p->zText );
 20096   memcpy(&p->zText[p->nChar], z, N);
 20097   p->nChar += N;
 20100 /*
 20101 ** Finish off a string by making sure it is zero-terminated.
 20102 ** Return a pointer to the resulting string.  Return a NULL
 20103 ** pointer if any kind of error was encountered.
 20104 */
 20105 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
 20106   if( p->zText ){
 20107     p->zText[p->nChar] = 0;
 20108     if( p->useMalloc && p->zText==p->zBase ){
 20109       if( p->useMalloc==1 ){
 20110         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 20111       }else{
 20112         p->zText = sqlite3_malloc(p->nChar+1);
 20114       if( p->zText ){
 20115         memcpy(p->zText, p->zBase, p->nChar+1);
 20116       }else{
 20117         p->mallocFailed = 1;
 20121   return p->zText;
 20124 /*
 20125 ** Reset an StrAccum string.  Reclaim all malloced memory.
 20126 */
 20127 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
 20128   if( p->zText!=p->zBase ){
 20129     if( p->useMalloc==1 ){
 20130       sqlite3DbFree(p->db, p->zText);
 20131     }else{
 20132       sqlite3_free(p->zText);
 20135   p->zText = 0;
 20138 /*
 20139 ** Initialize a string accumulator
 20140 */
 20141 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
 20142   p->zText = p->zBase = zBase;
 20143   p->db = 0;
 20144   p->nChar = 0;
 20145   p->nAlloc = n;
 20146   p->mxAlloc = mx;
 20147   p->useMalloc = 1;
 20148   p->tooBig = 0;
 20149   p->mallocFailed = 0;
 20152 /*
 20153 ** Print into memory obtained from sqliteMalloc().  Use the internal
 20154 ** %-conversion extensions.
 20155 */
 20156 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
 20157   char *z;
 20158   char zBase[SQLITE_PRINT_BUF_SIZE];
 20159   StrAccum acc;
 20160   assert( db!=0 );
 20161   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
 20162                       db->aLimit[SQLITE_LIMIT_LENGTH]);
 20163   acc.db = db;
 20164   sqlite3VXPrintf(&acc, 1, zFormat, ap);
 20165   z = sqlite3StrAccumFinish(&acc);
 20166   if( acc.mallocFailed ){
 20167     db->mallocFailed = 1;
 20169   return z;
 20172 /*
 20173 ** Print into memory obtained from sqliteMalloc().  Use the internal
 20174 ** %-conversion extensions.
 20175 */
 20176 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
 20177   va_list ap;
 20178   char *z;
 20179   va_start(ap, zFormat);
 20180   z = sqlite3VMPrintf(db, zFormat, ap);
 20181   va_end(ap);
 20182   return z;
 20185 /*
 20186 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
 20187 ** the string and before returnning.  This routine is intended to be used
 20188 ** to modify an existing string.  For example:
 20189 **
 20190 **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
 20191 **
 20192 */
 20193 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
 20194   va_list ap;
 20195   char *z;
 20196   va_start(ap, zFormat);
 20197   z = sqlite3VMPrintf(db, zFormat, ap);
 20198   va_end(ap);
 20199   sqlite3DbFree(db, zStr);
 20200   return z;
 20203 /*
 20204 ** Print into memory obtained from sqlite3_malloc().  Omit the internal
 20205 ** %-conversion extensions.
 20206 */
 20207 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
 20208   char *z;
 20209   char zBase[SQLITE_PRINT_BUF_SIZE];
 20210   StrAccum acc;
 20211 #ifndef SQLITE_OMIT_AUTOINIT
 20212   if( sqlite3_initialize() ) return 0;
 20213 #endif
 20214   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
 20215   acc.useMalloc = 2;
 20216   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 20217   z = sqlite3StrAccumFinish(&acc);
 20218   return z;
 20221 /*
 20222 ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
 20223 ** %-conversion extensions.
 20224 */
 20225 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
 20226   va_list ap;
 20227   char *z;
 20228 #ifndef SQLITE_OMIT_AUTOINIT
 20229   if( sqlite3_initialize() ) return 0;
 20230 #endif
 20231   va_start(ap, zFormat);
 20232   z = sqlite3_vmprintf(zFormat, ap);
 20233   va_end(ap);
 20234   return z;
 20237 /*
 20238 ** sqlite3_snprintf() works like snprintf() except that it ignores the
 20239 ** current locale settings.  This is important for SQLite because we
 20240 ** are not able to use a "," as the decimal point in place of "." as
 20241 ** specified by some locales.
 20242 **
 20243 ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
 20244 ** from the snprintf() standard.  Unfortunately, it is too late to change
 20245 ** this without breaking compatibility, so we just have to live with the
 20246 ** mistake.
 20247 **
 20248 ** sqlite3_vsnprintf() is the varargs version.
 20249 */
 20250 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
 20251   StrAccum acc;
 20252   if( n<=0 ) return zBuf;
 20253   sqlite3StrAccumInit(&acc, zBuf, n, 0);
 20254   acc.useMalloc = 0;
 20255   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 20256   return sqlite3StrAccumFinish(&acc);
 20258 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
 20259   char *z;
 20260   va_list ap;
 20261   va_start(ap,zFormat);
 20262   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
 20263   va_end(ap);
 20264   return z;
 20267 /*
 20268 ** This is the routine that actually formats the sqlite3_log() message.
 20269 ** We house it in a separate routine from sqlite3_log() to avoid using
 20270 ** stack space on small-stack systems when logging is disabled.
 20271 **
 20272 ** sqlite3_log() must render into a static buffer.  It cannot dynamically
 20273 ** allocate memory because it might be called while the memory allocator
 20274 ** mutex is held.
 20275 */
 20276 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
 20277   StrAccum acc;                          /* String accumulator */
 20278   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
 20280   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
 20281   acc.useMalloc = 0;
 20282   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 20283   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
 20284                            sqlite3StrAccumFinish(&acc));
 20287 /*
 20288 ** Format and write a message to the log if logging is enabled.
 20289 */
 20290 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
 20291   va_list ap;                             /* Vararg list */
 20292   if( sqlite3GlobalConfig.xLog ){
 20293     va_start(ap, zFormat);
 20294     renderLogMsg(iErrCode, zFormat, ap);
 20295     va_end(ap);
 20299 #if defined(SQLITE_DEBUG)
 20300 /*
 20301 ** A version of printf() that understands %lld.  Used for debugging.
 20302 ** The printf() built into some versions of windows does not understand %lld
 20303 ** and segfaults if you give it a long long int.
 20304 */
 20305 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
 20306   va_list ap;
 20307   StrAccum acc;
 20308   char zBuf[500];
 20309   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
 20310   acc.useMalloc = 0;
 20311   va_start(ap,zFormat);
 20312   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 20313   va_end(ap);
 20314   sqlite3StrAccumFinish(&acc);
 20315   fprintf(stdout,"%s", zBuf);
 20316   fflush(stdout);
 20318 #endif
 20320 #ifndef SQLITE_OMIT_TRACE
 20321 /*
 20322 ** variable-argument wrapper around sqlite3VXPrintf().
 20323 */
 20324 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
 20325   va_list ap;
 20326   va_start(ap,zFormat);
 20327   sqlite3VXPrintf(p, 1, zFormat, ap);
 20328   va_end(ap);
 20330 #endif
 20332 /************** End of printf.c **********************************************/
 20333 /************** Begin file random.c ******************************************/
 20334 /*
 20335 ** 2001 September 15
 20336 **
 20337 ** The author disclaims copyright to this source code.  In place of
 20338 ** a legal notice, here is a blessing:
 20339 **
 20340 **    May you do good and not evil.
 20341 **    May you find forgiveness for yourself and forgive others.
 20342 **    May you share freely, never taking more than you give.
 20343 **
 20344 *************************************************************************
 20345 ** This file contains code to implement a pseudo-random number
 20346 ** generator (PRNG) for SQLite.
 20347 **
 20348 ** Random numbers are used by some of the database backends in order
 20349 ** to generate random integer keys for tables or random filenames.
 20350 */
 20353 /* All threads share a single random number generator.
 20354 ** This structure is the current state of the generator.
 20355 */
 20356 static SQLITE_WSD struct sqlite3PrngType {
 20357   unsigned char isInit;          /* True if initialized */
 20358   unsigned char i, j;            /* State variables */
 20359   unsigned char s[256];          /* State variables */
 20360 } sqlite3Prng;
 20362 /*
 20363 ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
 20364 ** must be held while executing this routine.
 20365 **
 20366 ** Why not just use a library random generator like lrand48() for this?
 20367 ** Because the OP_NewRowid opcode in the VDBE depends on having a very
 20368 ** good source of random numbers.  The lrand48() library function may
 20369 ** well be good enough.  But maybe not.  Or maybe lrand48() has some
 20370 ** subtle problems on some systems that could cause problems.  It is hard
 20371 ** to know.  To minimize the risk of problems due to bad lrand48()
 20372 ** implementations, SQLite uses this random number generator based
 20373 ** on RC4, which we know works very well.
 20374 **
 20375 ** (Later):  Actually, OP_NewRowid does not depend on a good source of
 20376 ** randomness any more.  But we will leave this code in all the same.
 20377 */
 20378 static u8 randomByte(void){
 20379   unsigned char t;
 20382   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
 20383   ** state vector.  If writable static data is unsupported on the target,
 20384   ** we have to locate the state vector at run-time.  In the more common
 20385   ** case where writable static data is supported, wsdPrng can refer directly
 20386   ** to the "sqlite3Prng" state vector declared above.
 20387   */
 20388 #ifdef SQLITE_OMIT_WSD
 20389   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
 20390 # define wsdPrng p[0]
 20391 #else
 20392 # define wsdPrng sqlite3Prng
 20393 #endif
 20396   /* Initialize the state of the random number generator once,
 20397   ** the first time this routine is called.  The seed value does
 20398   ** not need to contain a lot of randomness since we are not
 20399   ** trying to do secure encryption or anything like that...
 20400   **
 20401   ** Nothing in this file or anywhere else in SQLite does any kind of
 20402   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
 20403   ** number generator) not as an encryption device.
 20404   */
 20405   if( !wsdPrng.isInit ){
 20406     int i;
 20407     char k[256];
 20408     wsdPrng.j = 0;
 20409     wsdPrng.i = 0;
 20410     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
 20411     for(i=0; i<256; i++){
 20412       wsdPrng.s[i] = (u8)i;
 20414     for(i=0; i<256; i++){
 20415       wsdPrng.j += wsdPrng.s[i] + k[i];
 20416       t = wsdPrng.s[wsdPrng.j];
 20417       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
 20418       wsdPrng.s[i] = t;
 20420     wsdPrng.isInit = 1;
 20423   /* Generate and return single random byte
 20424   */
 20425   wsdPrng.i++;
 20426   t = wsdPrng.s[wsdPrng.i];
 20427   wsdPrng.j += t;
 20428   wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
 20429   wsdPrng.s[wsdPrng.j] = t;
 20430   t += wsdPrng.s[wsdPrng.i];
 20431   return wsdPrng.s[t];
 20434 /*
 20435 ** Return N random bytes.
 20436 */
 20437 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
 20438   unsigned char *zBuf = pBuf;
 20439 #if SQLITE_THREADSAFE
 20440   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
 20441 #endif
 20442   sqlite3_mutex_enter(mutex);
 20443   while( N-- ){
 20444     *(zBuf++) = randomByte();
 20446   sqlite3_mutex_leave(mutex);
 20449 #ifndef SQLITE_OMIT_BUILTIN_TEST
 20450 /*
 20451 ** For testing purposes, we sometimes want to preserve the state of
 20452 ** PRNG and restore the PRNG to its saved state at a later time, or
 20453 ** to reset the PRNG to its initial state.  These routines accomplish
 20454 ** those tasks.
 20455 **
 20456 ** The sqlite3_test_control() interface calls these routines to
 20457 ** control the PRNG.
 20458 */
 20459 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
 20460 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
 20461   memcpy(
 20462     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
 20463     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
 20464     sizeof(sqlite3Prng)
 20465   );
 20467 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
 20468   memcpy(
 20469     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
 20470     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
 20471     sizeof(sqlite3Prng)
 20472   );
 20474 SQLITE_PRIVATE void sqlite3PrngResetState(void){
 20475   GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
 20477 #endif /* SQLITE_OMIT_BUILTIN_TEST */
 20479 /************** End of random.c **********************************************/
 20480 /************** Begin file utf.c *********************************************/
 20481 /*
 20482 ** 2004 April 13
 20483 **
 20484 ** The author disclaims copyright to this source code.  In place of
 20485 ** a legal notice, here is a blessing:
 20486 **
 20487 **    May you do good and not evil.
 20488 **    May you find forgiveness for yourself and forgive others.
 20489 **    May you share freely, never taking more than you give.
 20490 **
 20491 *************************************************************************
 20492 ** This file contains routines used to translate between UTF-8, 
 20493 ** UTF-16, UTF-16BE, and UTF-16LE.
 20494 **
 20495 ** Notes on UTF-8:
 20496 **
 20497 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
 20498 **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
 20499 **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
 20500 **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
 20501 **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
 20502 **
 20503 **
 20504 ** Notes on UTF-16:  (with wwww+1==uuuuu)
 20505 **
 20506 **      Word-0               Word-1          Value
 20507 **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
 20508 **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
 20509 **
 20510 **
 20511 ** BOM or Byte Order Mark:
 20512 **     0xff 0xfe   little-endian utf-16 follows
 20513 **     0xfe 0xff   big-endian utf-16 follows
 20514 **
 20515 */
 20516 /* #include <assert.h> */
 20518 #ifndef SQLITE_AMALGAMATION
 20519 /*
 20520 ** The following constant value is used by the SQLITE_BIGENDIAN and
 20521 ** SQLITE_LITTLEENDIAN macros.
 20522 */
 20523 SQLITE_PRIVATE const int sqlite3one = 1;
 20524 #endif /* SQLITE_AMALGAMATION */
 20526 /*
 20527 ** This lookup table is used to help decode the first byte of
 20528 ** a multi-byte UTF8 character.
 20529 */
 20530 static const unsigned char sqlite3Utf8Trans1[] = {
 20531   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 20532   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 20533   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 20534   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
 20535   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 20536   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 20537   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 20538   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
 20539 };
 20542 #define WRITE_UTF8(zOut, c) {                          \
 20543   if( c<0x00080 ){                                     \
 20544     *zOut++ = (u8)(c&0xFF);                            \
 20545   }                                                    \
 20546   else if( c<0x00800 ){                                \
 20547     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
 20548     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 20549   }                                                    \
 20550   else if( c<0x10000 ){                                \
 20551     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
 20552     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 20553     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 20554   }else{                                               \
 20555     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
 20556     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
 20557     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 20558     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 20559   }                                                    \
 20562 #define WRITE_UTF16LE(zOut, c) {                                    \
 20563   if( c<=0xFFFF ){                                                  \
 20564     *zOut++ = (u8)(c&0x00FF);                                       \
 20565     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
 20566   }else{                                                            \
 20567     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
 20568     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
 20569     *zOut++ = (u8)(c&0x00FF);                                       \
 20570     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
 20571   }                                                                 \
 20574 #define WRITE_UTF16BE(zOut, c) {                                    \
 20575   if( c<=0xFFFF ){                                                  \
 20576     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
 20577     *zOut++ = (u8)(c&0x00FF);                                       \
 20578   }else{                                                            \
 20579     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
 20580     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
 20581     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
 20582     *zOut++ = (u8)(c&0x00FF);                                       \
 20583   }                                                                 \
 20586 #define READ_UTF16LE(zIn, TERM, c){                                   \
 20587   c = (*zIn++);                                                       \
 20588   c += ((*zIn++)<<8);                                                 \
 20589   if( c>=0xD800 && c<0xE000 && TERM ){                                \
 20590     int c2 = (*zIn++);                                                \
 20591     c2 += ((*zIn++)<<8);                                              \
 20592     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
 20593   }                                                                   \
 20596 #define READ_UTF16BE(zIn, TERM, c){                                   \
 20597   c = ((*zIn++)<<8);                                                  \
 20598   c += (*zIn++);                                                      \
 20599   if( c>=0xD800 && c<0xE000 && TERM ){                                \
 20600     int c2 = ((*zIn++)<<8);                                           \
 20601     c2 += (*zIn++);                                                   \
 20602     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
 20603   }                                                                   \
 20606 /*
 20607 ** Translate a single UTF-8 character.  Return the unicode value.
 20608 **
 20609 ** During translation, assume that the byte that zTerm points
 20610 ** is a 0x00.
 20611 **
 20612 ** Write a pointer to the next unread byte back into *pzNext.
 20613 **
 20614 ** Notes On Invalid UTF-8:
 20615 **
 20616 **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
 20617 **     be encoded as a multi-byte character.  Any multi-byte character that
 20618 **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
 20619 **
 20620 **  *  This routine never allows a UTF16 surrogate value to be encoded.
 20621 **     If a multi-byte character attempts to encode a value between
 20622 **     0xd800 and 0xe000 then it is rendered as 0xfffd.
 20623 **
 20624 **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
 20625 **     byte of a character are interpreted as single-byte characters
 20626 **     and rendered as themselves even though they are technically
 20627 **     invalid characters.
 20628 **
 20629 **  *  This routine accepts an infinite number of different UTF8 encodings
 20630 **     for unicode values 0x80 and greater.  It do not change over-length
 20631 **     encodings to 0xfffd as some systems recommend.
 20632 */
 20633 #define READ_UTF8(zIn, zTerm, c)                           \
 20634   c = *(zIn++);                                            \
 20635   if( c>=0xc0 ){                                           \
 20636     c = sqlite3Utf8Trans1[c-0xc0];                         \
 20637     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
 20638       c = (c<<6) + (0x3f & *(zIn++));                      \
 20639     }                                                      \
 20640     if( c<0x80                                             \
 20641         || (c&0xFFFFF800)==0xD800                          \
 20642         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
 20644 SQLITE_PRIVATE u32 sqlite3Utf8Read(
 20645   const unsigned char **pz    /* Pointer to string from which to read char */
 20646 ){
 20647   unsigned int c;
 20649   /* Same as READ_UTF8() above but without the zTerm parameter.
 20650   ** For this routine, we assume the UTF8 string is always zero-terminated.
 20651   */
 20652   c = *((*pz)++);
 20653   if( c>=0xc0 ){
 20654     c = sqlite3Utf8Trans1[c-0xc0];
 20655     while( (*(*pz) & 0xc0)==0x80 ){
 20656       c = (c<<6) + (0x3f & *((*pz)++));
 20658     if( c<0x80
 20659         || (c&0xFFFFF800)==0xD800
 20660         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
 20662   return c;
 20668 /*
 20669 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
 20670 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
 20671 */ 
 20672 /* #define TRANSLATE_TRACE 1 */
 20674 #ifndef SQLITE_OMIT_UTF16
 20675 /*
 20676 ** This routine transforms the internal text encoding used by pMem to
 20677 ** desiredEnc. It is an error if the string is already of the desired
 20678 ** encoding, or if *pMem does not contain a string value.
 20679 */
 20680 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
 20681   int len;                    /* Maximum length of output string in bytes */
 20682   unsigned char *zOut;                  /* Output buffer */
 20683   unsigned char *zIn;                   /* Input iterator */
 20684   unsigned char *zTerm;                 /* End of input */
 20685   unsigned char *z;                     /* Output iterator */
 20686   unsigned int c;
 20688   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 20689   assert( pMem->flags&MEM_Str );
 20690   assert( pMem->enc!=desiredEnc );
 20691   assert( pMem->enc!=0 );
 20692   assert( pMem->n>=0 );
 20694 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
 20696     char zBuf[100];
 20697     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
 20698     fprintf(stderr, "INPUT:  %s\n", zBuf);
 20700 #endif
 20702   /* If the translation is between UTF-16 little and big endian, then 
 20703   ** all that is required is to swap the byte order. This case is handled
 20704   ** differently from the others.
 20705   */
 20706   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
 20707     u8 temp;
 20708     int rc;
 20709     rc = sqlite3VdbeMemMakeWriteable(pMem);
 20710     if( rc!=SQLITE_OK ){
 20711       assert( rc==SQLITE_NOMEM );
 20712       return SQLITE_NOMEM;
 20714     zIn = (u8*)pMem->z;
 20715     zTerm = &zIn[pMem->n&~1];
 20716     while( zIn<zTerm ){
 20717       temp = *zIn;
 20718       *zIn = *(zIn+1);
 20719       zIn++;
 20720       *zIn++ = temp;
 20722     pMem->enc = desiredEnc;
 20723     goto translate_out;
 20726   /* Set len to the maximum number of bytes required in the output buffer. */
 20727   if( desiredEnc==SQLITE_UTF8 ){
 20728     /* When converting from UTF-16, the maximum growth results from
 20729     ** translating a 2-byte character to a 4-byte UTF-8 character.
 20730     ** A single byte is required for the output string
 20731     ** nul-terminator.
 20732     */
 20733     pMem->n &= ~1;
 20734     len = pMem->n * 2 + 1;
 20735   }else{
 20736     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
 20737     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
 20738     ** character. Two bytes are required in the output buffer for the
 20739     ** nul-terminator.
 20740     */
 20741     len = pMem->n * 2 + 2;
 20744   /* Set zIn to point at the start of the input buffer and zTerm to point 1
 20745   ** byte past the end.
 20746   **
 20747   ** Variable zOut is set to point at the output buffer, space obtained
 20748   ** from sqlite3_malloc().
 20749   */
 20750   zIn = (u8*)pMem->z;
 20751   zTerm = &zIn[pMem->n];
 20752   zOut = sqlite3DbMallocRaw(pMem->db, len);
 20753   if( !zOut ){
 20754     return SQLITE_NOMEM;
 20756   z = zOut;
 20758   if( pMem->enc==SQLITE_UTF8 ){
 20759     if( desiredEnc==SQLITE_UTF16LE ){
 20760       /* UTF-8 -> UTF-16 Little-endian */
 20761       while( zIn<zTerm ){
 20762         READ_UTF8(zIn, zTerm, c);
 20763         WRITE_UTF16LE(z, c);
 20765     }else{
 20766       assert( desiredEnc==SQLITE_UTF16BE );
 20767       /* UTF-8 -> UTF-16 Big-endian */
 20768       while( zIn<zTerm ){
 20769         READ_UTF8(zIn, zTerm, c);
 20770         WRITE_UTF16BE(z, c);
 20773     pMem->n = (int)(z - zOut);
 20774     *z++ = 0;
 20775   }else{
 20776     assert( desiredEnc==SQLITE_UTF8 );
 20777     if( pMem->enc==SQLITE_UTF16LE ){
 20778       /* UTF-16 Little-endian -> UTF-8 */
 20779       while( zIn<zTerm ){
 20780         READ_UTF16LE(zIn, zIn<zTerm, c); 
 20781         WRITE_UTF8(z, c);
 20783     }else{
 20784       /* UTF-16 Big-endian -> UTF-8 */
 20785       while( zIn<zTerm ){
 20786         READ_UTF16BE(zIn, zIn<zTerm, c); 
 20787         WRITE_UTF8(z, c);
 20790     pMem->n = (int)(z - zOut);
 20792   *z = 0;
 20793   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
 20795   sqlite3VdbeMemRelease(pMem);
 20796   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
 20797   pMem->enc = desiredEnc;
 20798   pMem->flags |= (MEM_Term|MEM_Dyn);
 20799   pMem->z = (char*)zOut;
 20800   pMem->zMalloc = pMem->z;
 20802 translate_out:
 20803 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
 20805     char zBuf[100];
 20806     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
 20807     fprintf(stderr, "OUTPUT: %s\n", zBuf);
 20809 #endif
 20810   return SQLITE_OK;
 20813 /*
 20814 ** This routine checks for a byte-order mark at the beginning of the 
 20815 ** UTF-16 string stored in *pMem. If one is present, it is removed and
 20816 ** the encoding of the Mem adjusted. This routine does not do any
 20817 ** byte-swapping, it just sets Mem.enc appropriately.
 20818 **
 20819 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
 20820 ** changed by this function.
 20821 */
 20822 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
 20823   int rc = SQLITE_OK;
 20824   u8 bom = 0;
 20826   assert( pMem->n>=0 );
 20827   if( pMem->n>1 ){
 20828     u8 b1 = *(u8 *)pMem->z;
 20829     u8 b2 = *(((u8 *)pMem->z) + 1);
 20830     if( b1==0xFE && b2==0xFF ){
 20831       bom = SQLITE_UTF16BE;
 20833     if( b1==0xFF && b2==0xFE ){
 20834       bom = SQLITE_UTF16LE;
 20838   if( bom ){
 20839     rc = sqlite3VdbeMemMakeWriteable(pMem);
 20840     if( rc==SQLITE_OK ){
 20841       pMem->n -= 2;
 20842       memmove(pMem->z, &pMem->z[2], pMem->n);
 20843       pMem->z[pMem->n] = '\0';
 20844       pMem->z[pMem->n+1] = '\0';
 20845       pMem->flags |= MEM_Term;
 20846       pMem->enc = bom;
 20849   return rc;
 20851 #endif /* SQLITE_OMIT_UTF16 */
 20853 /*
 20854 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
 20855 ** return the number of unicode characters in pZ up to (but not including)
 20856 ** the first 0x00 byte. If nByte is not less than zero, return the
 20857 ** number of unicode characters in the first nByte of pZ (or up to 
 20858 ** the first 0x00, whichever comes first).
 20859 */
 20860 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
 20861   int r = 0;
 20862   const u8 *z = (const u8*)zIn;
 20863   const u8 *zTerm;
 20864   if( nByte>=0 ){
 20865     zTerm = &z[nByte];
 20866   }else{
 20867     zTerm = (const u8*)(-1);
 20869   assert( z<=zTerm );
 20870   while( *z!=0 && z<zTerm ){
 20871     SQLITE_SKIP_UTF8(z);
 20872     r++;
 20874   return r;
 20877 /* This test function is not currently used by the automated test-suite. 
 20878 ** Hence it is only available in debug builds.
 20879 */
 20880 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 20881 /*
 20882 ** Translate UTF-8 to UTF-8.
 20883 **
 20884 ** This has the effect of making sure that the string is well-formed
 20885 ** UTF-8.  Miscoded characters are removed.
 20886 **
 20887 ** The translation is done in-place and aborted if the output
 20888 ** overruns the input.
 20889 */
 20890 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
 20891   unsigned char *zOut = zIn;
 20892   unsigned char *zStart = zIn;
 20893   u32 c;
 20895   while( zIn[0] && zOut<=zIn ){
 20896     c = sqlite3Utf8Read((const u8**)&zIn);
 20897     if( c!=0xfffd ){
 20898       WRITE_UTF8(zOut, c);
 20901   *zOut = 0;
 20902   return (int)(zOut - zStart);
 20904 #endif
 20906 #ifndef SQLITE_OMIT_UTF16
 20907 /*
 20908 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
 20909 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
 20910 ** be freed by the calling function.
 20911 **
 20912 ** NULL is returned if there is an allocation error.
 20913 */
 20914 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
 20915   Mem m;
 20916   memset(&m, 0, sizeof(m));
 20917   m.db = db;
 20918   sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
 20919   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
 20920   if( db->mallocFailed ){
 20921     sqlite3VdbeMemRelease(&m);
 20922     m.z = 0;
 20924   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
 20925   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
 20926   assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
 20927   assert( m.z || db->mallocFailed );
 20928   return m.z;
 20931 /*
 20932 ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
 20933 ** enc. A pointer to the new string is returned, and the value of *pnOut
 20934 ** is set to the length of the returned string in bytes. The call should
 20935 ** arrange to call sqlite3DbFree() on the returned pointer when it is
 20936 ** no longer required.
 20937 ** 
 20938 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
 20939 ** flag set.
 20940 */
 20941 #ifdef SQLITE_ENABLE_STAT3
 20942 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
 20943   Mem m;
 20944   memset(&m, 0, sizeof(m));
 20945   m.db = db;
 20946   sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
 20947   if( sqlite3VdbeMemTranslate(&m, enc) ){
 20948     assert( db->mallocFailed );
 20949     return 0;
 20951   assert( m.z==m.zMalloc );
 20952   *pnOut = m.n;
 20953   return m.z;
 20955 #endif
 20957 /*
 20958 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
 20959 ** Return the number of bytes in the first nChar unicode characters
 20960 ** in pZ.  nChar must be non-negative.
 20961 */
 20962 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
 20963   int c;
 20964   unsigned char const *z = zIn;
 20965   int n = 0;
 20967   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
 20968     while( n<nChar ){
 20969       READ_UTF16BE(z, 1, c);
 20970       n++;
 20972   }else{
 20973     while( n<nChar ){
 20974       READ_UTF16LE(z, 1, c);
 20975       n++;
 20978   return (int)(z-(unsigned char const *)zIn);
 20981 #if defined(SQLITE_TEST)
 20982 /*
 20983 ** This routine is called from the TCL test function "translate_selftest".
 20984 ** It checks that the primitives for serializing and deserializing
 20985 ** characters in each encoding are inverses of each other.
 20986 */
 20987 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
 20988   unsigned int i, t;
 20989   unsigned char zBuf[20];
 20990   unsigned char *z;
 20991   int n;
 20992   unsigned int c;
 20994   for(i=0; i<0x00110000; i++){
 20995     z = zBuf;
 20996     WRITE_UTF8(z, i);
 20997     n = (int)(z-zBuf);
 20998     assert( n>0 && n<=4 );
 20999     z[0] = 0;
 21000     z = zBuf;
 21001     c = sqlite3Utf8Read((const u8**)&z);
 21002     t = i;
 21003     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
 21004     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
 21005     assert( c==t );
 21006     assert( (z-zBuf)==n );
 21008   for(i=0; i<0x00110000; i++){
 21009     if( i>=0xD800 && i<0xE000 ) continue;
 21010     z = zBuf;
 21011     WRITE_UTF16LE(z, i);
 21012     n = (int)(z-zBuf);
 21013     assert( n>0 && n<=4 );
 21014     z[0] = 0;
 21015     z = zBuf;
 21016     READ_UTF16LE(z, 1, c);
 21017     assert( c==i );
 21018     assert( (z-zBuf)==n );
 21020   for(i=0; i<0x00110000; i++){
 21021     if( i>=0xD800 && i<0xE000 ) continue;
 21022     z = zBuf;
 21023     WRITE_UTF16BE(z, i);
 21024     n = (int)(z-zBuf);
 21025     assert( n>0 && n<=4 );
 21026     z[0] = 0;
 21027     z = zBuf;
 21028     READ_UTF16BE(z, 1, c);
 21029     assert( c==i );
 21030     assert( (z-zBuf)==n );
 21033 #endif /* SQLITE_TEST */
 21034 #endif /* SQLITE_OMIT_UTF16 */
 21036 /************** End of utf.c *************************************************/
 21037 /************** Begin file util.c ********************************************/
 21038 /*
 21039 ** 2001 September 15
 21040 **
 21041 ** The author disclaims copyright to this source code.  In place of
 21042 ** a legal notice, here is a blessing:
 21043 **
 21044 **    May you do good and not evil.
 21045 **    May you find forgiveness for yourself and forgive others.
 21046 **    May you share freely, never taking more than you give.
 21047 **
 21048 *************************************************************************
 21049 ** Utility functions used throughout sqlite.
 21050 **
 21051 ** This file contains functions for allocating memory, comparing
 21052 ** strings, and stuff like that.
 21053 **
 21054 */
 21055 /* #include <stdarg.h> */
 21056 #ifdef SQLITE_HAVE_ISNAN
 21057 # include <math.h>
 21058 #endif
 21060 /*
 21061 ** Routine needed to support the testcase() macro.
 21062 */
 21063 #ifdef SQLITE_COVERAGE_TEST
 21064 SQLITE_PRIVATE void sqlite3Coverage(int x){
 21065   static unsigned dummy = 0;
 21066   dummy += (unsigned)x;
 21068 #endif
 21070 #ifndef SQLITE_OMIT_FLOATING_POINT
 21071 /*
 21072 ** Return true if the floating point value is Not a Number (NaN).
 21073 **
 21074 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
 21075 ** Otherwise, we have our own implementation that works on most systems.
 21076 */
 21077 SQLITE_PRIVATE int sqlite3IsNaN(double x){
 21078   int rc;   /* The value return */
 21079 #if !defined(SQLITE_HAVE_ISNAN)
 21080   /*
 21081   ** Systems that support the isnan() library function should probably
 21082   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
 21083   ** found that many systems do not have a working isnan() function so
 21084   ** this implementation is provided as an alternative.
 21085   **
 21086   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
 21087   ** On the other hand, the use of -ffast-math comes with the following
 21088   ** warning:
 21089   **
 21090   **      This option [-ffast-math] should never be turned on by any
 21091   **      -O option since it can result in incorrect output for programs
 21092   **      which depend on an exact implementation of IEEE or ISO 
 21093   **      rules/specifications for math functions.
 21094   **
 21095   ** Under MSVC, this NaN test may fail if compiled with a floating-
 21096   ** point precision mode other than /fp:precise.  From the MSDN 
 21097   ** documentation:
 21098   **
 21099   **      The compiler [with /fp:precise] will properly handle comparisons 
 21100   **      involving NaN. For example, x != x evaluates to true if x is NaN 
 21101   **      ...
 21102   */
 21103 #ifdef __FAST_MATH__
 21104 # error SQLite will not work correctly with the -ffast-math option of GCC.
 21105 #endif
 21106   volatile double y = x;
 21107   volatile double z = y;
 21108   rc = (y!=z);
 21109 #else  /* if defined(SQLITE_HAVE_ISNAN) */
 21110   rc = isnan(x);
 21111 #endif /* SQLITE_HAVE_ISNAN */
 21112   testcase( rc );
 21113   return rc;
 21115 #endif /* SQLITE_OMIT_FLOATING_POINT */
 21117 /*
 21118 ** Compute a string length that is limited to what can be stored in
 21119 ** lower 30 bits of a 32-bit signed integer.
 21120 **
 21121 ** The value returned will never be negative.  Nor will it ever be greater
 21122 ** than the actual length of the string.  For very long strings (greater
 21123 ** than 1GiB) the value returned might be less than the true string length.
 21124 */
 21125 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
 21126   const char *z2 = z;
 21127   if( z==0 ) return 0;
 21128   while( *z2 ){ z2++; }
 21129   return 0x3fffffff & (int)(z2 - z);
 21132 /*
 21133 ** Set the most recent error code and error string for the sqlite
 21134 ** handle "db". The error code is set to "err_code".
 21135 **
 21136 ** If it is not NULL, string zFormat specifies the format of the
 21137 ** error string in the style of the printf functions: The following
 21138 ** format characters are allowed:
 21139 **
 21140 **      %s      Insert a string
 21141 **      %z      A string that should be freed after use
 21142 **      %d      Insert an integer
 21143 **      %T      Insert a token
 21144 **      %S      Insert the first element of a SrcList
 21145 **
 21146 ** zFormat and any string tokens that follow it are assumed to be
 21147 ** encoded in UTF-8.
 21148 **
 21149 ** To clear the most recent error for sqlite handle "db", sqlite3Error
 21150 ** should be called with err_code set to SQLITE_OK and zFormat set
 21151 ** to NULL.
 21152 */
 21153 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
 21154   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
 21155     db->errCode = err_code;
 21156     if( zFormat ){
 21157       char *z;
 21158       va_list ap;
 21159       va_start(ap, zFormat);
 21160       z = sqlite3VMPrintf(db, zFormat, ap);
 21161       va_end(ap);
 21162       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
 21163     }else{
 21164       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
 21169 /*
 21170 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
 21171 ** The following formatting characters are allowed:
 21172 **
 21173 **      %s      Insert a string
 21174 **      %z      A string that should be freed after use
 21175 **      %d      Insert an integer
 21176 **      %T      Insert a token
 21177 **      %S      Insert the first element of a SrcList
 21178 **
 21179 ** This function should be used to report any error that occurs whilst
 21180 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
 21181 ** last thing the sqlite3_prepare() function does is copy the error
 21182 ** stored by this function into the database handle using sqlite3Error().
 21183 ** Function sqlite3Error() should be used during statement execution
 21184 ** (sqlite3_step() etc.).
 21185 */
 21186 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
 21187   char *zMsg;
 21188   va_list ap;
 21189   sqlite3 *db = pParse->db;
 21190   va_start(ap, zFormat);
 21191   zMsg = sqlite3VMPrintf(db, zFormat, ap);
 21192   va_end(ap);
 21193   if( db->suppressErr ){
 21194     sqlite3DbFree(db, zMsg);
 21195   }else{
 21196     pParse->nErr++;
 21197     sqlite3DbFree(db, pParse->zErrMsg);
 21198     pParse->zErrMsg = zMsg;
 21199     pParse->rc = SQLITE_ERROR;
 21203 /*
 21204 ** Convert an SQL-style quoted string into a normal string by removing
 21205 ** the quote characters.  The conversion is done in-place.  If the
 21206 ** input does not begin with a quote character, then this routine
 21207 ** is a no-op.
 21208 **
 21209 ** The input string must be zero-terminated.  A new zero-terminator
 21210 ** is added to the dequoted string.
 21211 **
 21212 ** The return value is -1 if no dequoting occurs or the length of the
 21213 ** dequoted string, exclusive of the zero terminator, if dequoting does
 21214 ** occur.
 21215 **
 21216 ** 2002-Feb-14: This routine is extended to remove MS-Access style
 21217 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
 21218 ** "a-b-c".
 21219 */
 21220 SQLITE_PRIVATE int sqlite3Dequote(char *z){
 21221   char quote;
 21222   int i, j;
 21223   if( z==0 ) return -1;
 21224   quote = z[0];
 21225   switch( quote ){
 21226     case '\'':  break;
 21227     case '"':   break;
 21228     case '`':   break;                /* For MySQL compatibility */
 21229     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
 21230     default:    return -1;
 21232   for(i=1, j=0; ALWAYS(z[i]); i++){
 21233     if( z[i]==quote ){
 21234       if( z[i+1]==quote ){
 21235         z[j++] = quote;
 21236         i++;
 21237       }else{
 21238         break;
 21240     }else{
 21241       z[j++] = z[i];
 21244   z[j] = 0;
 21245   return j;
 21248 /* Convenient short-hand */
 21249 #define UpperToLower sqlite3UpperToLower
 21251 /*
 21252 ** Some systems have stricmp().  Others have strcasecmp().  Because
 21253 ** there is no consistency, we will define our own.
 21254 **
 21255 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
 21256 ** sqlite3_strnicmp() APIs allow applications and extensions to compare
 21257 ** the contents of two buffers containing UTF-8 strings in a
 21258 ** case-independent fashion, using the same definition of "case
 21259 ** independence" that SQLite uses internally when comparing identifiers.
 21260 */
 21261 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
 21262   register unsigned char *a, *b;
 21263   a = (unsigned char *)zLeft;
 21264   b = (unsigned char *)zRight;
 21265   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 21266   return UpperToLower[*a] - UpperToLower[*b];
 21268 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
 21269   register unsigned char *a, *b;
 21270   a = (unsigned char *)zLeft;
 21271   b = (unsigned char *)zRight;
 21272   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 21273   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
 21276 /*
 21277 ** The string z[] is an text representation of a real number.
 21278 ** Convert this string to a double and write it into *pResult.
 21279 **
 21280 ** The string z[] is length bytes in length (bytes, not characters) and
 21281 ** uses the encoding enc.  The string is not necessarily zero-terminated.
 21282 **
 21283 ** Return TRUE if the result is a valid real number (or integer) and FALSE
 21284 ** if the string is empty or contains extraneous text.  Valid numbers
 21285 ** are in one of these formats:
 21286 **
 21287 **    [+-]digits[E[+-]digits]
 21288 **    [+-]digits.[digits][E[+-]digits]
 21289 **    [+-].digits[E[+-]digits]
 21290 **
 21291 ** Leading and trailing whitespace is ignored for the purpose of determining
 21292 ** validity.
 21293 **
 21294 ** If some prefix of the input string is a valid number, this routine
 21295 ** returns FALSE but it still converts the prefix and writes the result
 21296 ** into *pResult.
 21297 */
 21298 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
 21299 #ifndef SQLITE_OMIT_FLOATING_POINT
 21300   int incr = (enc==SQLITE_UTF8?1:2);
 21301   const char *zEnd = z + length;
 21302   /* sign * significand * (10 ^ (esign * exponent)) */
 21303   int sign = 1;    /* sign of significand */
 21304   i64 s = 0;       /* significand */
 21305   int d = 0;       /* adjust exponent for shifting decimal point */
 21306   int esign = 1;   /* sign of exponent */
 21307   int e = 0;       /* exponent */
 21308   int eValid = 1;  /* True exponent is either not used or is well-formed */
 21309   double result;
 21310   int nDigits = 0;
 21312   *pResult = 0.0;   /* Default return value, in case of an error */
 21314   if( enc==SQLITE_UTF16BE ) z++;
 21316   /* skip leading spaces */
 21317   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
 21318   if( z>=zEnd ) return 0;
 21320   /* get sign of significand */
 21321   if( *z=='-' ){
 21322     sign = -1;
 21323     z+=incr;
 21324   }else if( *z=='+' ){
 21325     z+=incr;
 21328   /* skip leading zeroes */
 21329   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
 21331   /* copy max significant digits to significand */
 21332   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
 21333     s = s*10 + (*z - '0');
 21334     z+=incr, nDigits++;
 21337   /* skip non-significant significand digits
 21338   ** (increase exponent by d to shift decimal left) */
 21339   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
 21340   if( z>=zEnd ) goto do_atof_calc;
 21342   /* if decimal point is present */
 21343   if( *z=='.' ){
 21344     z+=incr;
 21345     /* copy digits from after decimal to significand
 21346     ** (decrease exponent by d to shift decimal right) */
 21347     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
 21348       s = s*10 + (*z - '0');
 21349       z+=incr, nDigits++, d--;
 21351     /* skip non-significant digits */
 21352     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
 21354   if( z>=zEnd ) goto do_atof_calc;
 21356   /* if exponent is present */
 21357   if( *z=='e' || *z=='E' ){
 21358     z+=incr;
 21359     eValid = 0;
 21360     if( z>=zEnd ) goto do_atof_calc;
 21361     /* get sign of exponent */
 21362     if( *z=='-' ){
 21363       esign = -1;
 21364       z+=incr;
 21365     }else if( *z=='+' ){
 21366       z+=incr;
 21368     /* copy digits to exponent */
 21369     while( z<zEnd && sqlite3Isdigit(*z) ){
 21370       e = e<10000 ? (e*10 + (*z - '0')) : 10000;
 21371       z+=incr;
 21372       eValid = 1;
 21376   /* skip trailing spaces */
 21377   if( nDigits && eValid ){
 21378     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
 21381 do_atof_calc:
 21382   /* adjust exponent by d, and update sign */
 21383   e = (e*esign) + d;
 21384   if( e<0 ) {
 21385     esign = -1;
 21386     e *= -1;
 21387   } else {
 21388     esign = 1;
 21391   /* if 0 significand */
 21392   if( !s ) {
 21393     /* In the IEEE 754 standard, zero is signed.
 21394     ** Add the sign if we've seen at least one digit */
 21395     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
 21396   } else {
 21397     /* attempt to reduce exponent */
 21398     if( esign>0 ){
 21399       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
 21400     }else{
 21401       while( !(s%10) && e>0 ) e--,s/=10;
 21404     /* adjust the sign of significand */
 21405     s = sign<0 ? -s : s;
 21407     /* if exponent, scale significand as appropriate
 21408     ** and store in result. */
 21409     if( e ){
 21410       LONGDOUBLE_TYPE scale = 1.0;
 21411       /* attempt to handle extremely small/large numbers better */
 21412       if( e>307 && e<342 ){
 21413         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
 21414         if( esign<0 ){
 21415           result = s / scale;
 21416           result /= 1.0e+308;
 21417         }else{
 21418           result = s * scale;
 21419           result *= 1.0e+308;
 21421       }else if( e>=342 ){
 21422         if( esign<0 ){
 21423           result = 0.0*s;
 21424         }else{
 21425           result = 1e308*1e308*s;  /* Infinity */
 21427       }else{
 21428         /* 1.0e+22 is the largest power of 10 than can be 
 21429         ** represented exactly. */
 21430         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
 21431         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
 21432         if( esign<0 ){
 21433           result = s / scale;
 21434         }else{
 21435           result = s * scale;
 21438     } else {
 21439       result = (double)s;
 21443   /* store the result */
 21444   *pResult = result;
 21446   /* return true if number and no extra non-whitespace chracters after */
 21447   return z>=zEnd && nDigits>0 && eValid;
 21448 #else
 21449   return !sqlite3Atoi64(z, pResult, length, enc);
 21450 #endif /* SQLITE_OMIT_FLOATING_POINT */
 21453 /*
 21454 ** Compare the 19-character string zNum against the text representation
 21455 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
 21456 ** if zNum is less than, equal to, or greater than the string.
 21457 ** Note that zNum must contain exactly 19 characters.
 21458 **
 21459 ** Unlike memcmp() this routine is guaranteed to return the difference
 21460 ** in the values of the last digit if the only difference is in the
 21461 ** last digit.  So, for example,
 21462 **
 21463 **      compare2pow63("9223372036854775800", 1)
 21464 **
 21465 ** will return -8.
 21466 */
 21467 static int compare2pow63(const char *zNum, int incr){
 21468   int c = 0;
 21469   int i;
 21470                     /* 012345678901234567 */
 21471   const char *pow63 = "922337203685477580";
 21472   for(i=0; c==0 && i<18; i++){
 21473     c = (zNum[i*incr]-pow63[i])*10;
 21475   if( c==0 ){
 21476     c = zNum[18*incr] - '8';
 21477     testcase( c==(-1) );
 21478     testcase( c==0 );
 21479     testcase( c==(+1) );
 21481   return c;
 21485 /*
 21486 ** Convert zNum to a 64-bit signed integer.
 21487 **
 21488 ** If the zNum value is representable as a 64-bit twos-complement 
 21489 ** integer, then write that value into *pNum and return 0.
 21490 **
 21491 ** If zNum is exactly 9223372036854665808, return 2.  This special
 21492 ** case is broken out because while 9223372036854665808 cannot be a 
 21493 ** signed 64-bit integer, its negative -9223372036854665808 can be.
 21494 **
 21495 ** If zNum is too big for a 64-bit integer and is not
 21496 ** 9223372036854665808 then return 1.
 21497 **
 21498 ** length is the number of bytes in the string (bytes, not characters).
 21499 ** The string is not necessarily zero-terminated.  The encoding is
 21500 ** given by enc.
 21501 */
 21502 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
 21503   int incr = (enc==SQLITE_UTF8?1:2);
 21504   u64 u = 0;
 21505   int neg = 0; /* assume positive */
 21506   int i;
 21507   int c = 0;
 21508   const char *zStart;
 21509   const char *zEnd = zNum + length;
 21510   if( enc==SQLITE_UTF16BE ) zNum++;
 21511   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
 21512   if( zNum<zEnd ){
 21513     if( *zNum=='-' ){
 21514       neg = 1;
 21515       zNum+=incr;
 21516     }else if( *zNum=='+' ){
 21517       zNum+=incr;
 21520   zStart = zNum;
 21521   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 21522   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 21523     u = u*10 + c - '0';
 21525   if( u>LARGEST_INT64 ){
 21526     *pNum = SMALLEST_INT64;
 21527   }else if( neg ){
 21528     *pNum = -(i64)u;
 21529   }else{
 21530     *pNum = (i64)u;
 21532   testcase( i==18 );
 21533   testcase( i==19 );
 21534   testcase( i==20 );
 21535   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
 21536     /* zNum is empty or contains non-numeric text or is longer
 21537     ** than 19 digits (thus guaranteeing that it is too large) */
 21538     return 1;
 21539   }else if( i<19*incr ){
 21540     /* Less than 19 digits, so we know that it fits in 64 bits */
 21541     assert( u<=LARGEST_INT64 );
 21542     return 0;
 21543   }else{
 21544     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 21545     c = compare2pow63(zNum, incr);
 21546     if( c<0 ){
 21547       /* zNum is less than 9223372036854775808 so it fits */
 21548       assert( u<=LARGEST_INT64 );
 21549       return 0;
 21550     }else if( c>0 ){
 21551       /* zNum is greater than 9223372036854775808 so it overflows */
 21552       return 1;
 21553     }else{
 21554       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 21555       ** special case 2 overflow if positive */
 21556       assert( u-1==LARGEST_INT64 );
 21557       assert( (*pNum)==SMALLEST_INT64 );
 21558       return neg ? 0 : 2;
 21563 /*
 21564 ** If zNum represents an integer that will fit in 32-bits, then set
 21565 ** *pValue to that integer and return true.  Otherwise return false.
 21566 **
 21567 ** Any non-numeric characters that following zNum are ignored.
 21568 ** This is different from sqlite3Atoi64() which requires the
 21569 ** input number to be zero-terminated.
 21570 */
 21571 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
 21572   sqlite_int64 v = 0;
 21573   int i, c;
 21574   int neg = 0;
 21575   if( zNum[0]=='-' ){
 21576     neg = 1;
 21577     zNum++;
 21578   }else if( zNum[0]=='+' ){
 21579     zNum++;
 21581   while( zNum[0]=='0' ) zNum++;
 21582   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
 21583     v = v*10 + c;
 21586   /* The longest decimal representation of a 32 bit integer is 10 digits:
 21587   **
 21588   **             1234567890
 21589   **     2^31 -> 2147483648
 21590   */
 21591   testcase( i==10 );
 21592   if( i>10 ){
 21593     return 0;
 21595   testcase( v-neg==2147483647 );
 21596   if( v-neg>2147483647 ){
 21597     return 0;
 21599   if( neg ){
 21600     v = -v;
 21602   *pValue = (int)v;
 21603   return 1;
 21606 /*
 21607 ** Return a 32-bit integer value extracted from a string.  If the
 21608 ** string is not an integer, just return 0.
 21609 */
 21610 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
 21611   int x = 0;
 21612   if( z ) sqlite3GetInt32(z, &x);
 21613   return x;
 21616 /*
 21617 ** The variable-length integer encoding is as follows:
 21618 **
 21619 ** KEY:
 21620 **         A = 0xxxxxxx    7 bits of data and one flag bit
 21621 **         B = 1xxxxxxx    7 bits of data and one flag bit
 21622 **         C = xxxxxxxx    8 bits of data
 21623 **
 21624 **  7 bits - A
 21625 ** 14 bits - BA
 21626 ** 21 bits - BBA
 21627 ** 28 bits - BBBA
 21628 ** 35 bits - BBBBA
 21629 ** 42 bits - BBBBBA
 21630 ** 49 bits - BBBBBBA
 21631 ** 56 bits - BBBBBBBA
 21632 ** 64 bits - BBBBBBBBC
 21633 */
 21635 /*
 21636 ** Write a 64-bit variable-length integer to memory starting at p[0].
 21637 ** The length of data write will be between 1 and 9 bytes.  The number
 21638 ** of bytes written is returned.
 21639 **
 21640 ** A variable-length integer consists of the lower 7 bits of each byte
 21641 ** for all bytes that have the 8th bit set and one byte with the 8th
 21642 ** bit clear.  Except, if we get to the 9th byte, it stores the full
 21643 ** 8 bits and is the last byte.
 21644 */
 21645 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
 21646   int i, j, n;
 21647   u8 buf[10];
 21648   if( v & (((u64)0xff000000)<<32) ){
 21649     p[8] = (u8)v;
 21650     v >>= 8;
 21651     for(i=7; i>=0; i--){
 21652       p[i] = (u8)((v & 0x7f) | 0x80);
 21653       v >>= 7;
 21655     return 9;
 21657   n = 0;
 21658   do{
 21659     buf[n++] = (u8)((v & 0x7f) | 0x80);
 21660     v >>= 7;
 21661   }while( v!=0 );
 21662   buf[0] &= 0x7f;
 21663   assert( n<=9 );
 21664   for(i=0, j=n-1; j>=0; j--, i++){
 21665     p[i] = buf[j];
 21667   return n;
 21670 /*
 21671 ** This routine is a faster version of sqlite3PutVarint() that only
 21672 ** works for 32-bit positive integers and which is optimized for
 21673 ** the common case of small integers.  A MACRO version, putVarint32,
 21674 ** is provided which inlines the single-byte case.  All code should use
 21675 ** the MACRO version as this function assumes the single-byte case has
 21676 ** already been handled.
 21677 */
 21678 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
 21679 #ifndef putVarint32
 21680   if( (v & ~0x7f)==0 ){
 21681     p[0] = v;
 21682     return 1;
 21684 #endif
 21685   if( (v & ~0x3fff)==0 ){
 21686     p[0] = (u8)((v>>7) | 0x80);
 21687     p[1] = (u8)(v & 0x7f);
 21688     return 2;
 21690   return sqlite3PutVarint(p, v);
 21693 /*
 21694 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
 21695 ** are defined here rather than simply putting the constant expressions
 21696 ** inline in order to work around bugs in the RVT compiler.
 21697 **
 21698 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
 21699 **
 21700 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
 21701 */
 21702 #define SLOT_2_0     0x001fc07f
 21703 #define SLOT_4_2_0   0xf01fc07f
 21706 /*
 21707 ** Read a 64-bit variable-length integer from memory starting at p[0].
 21708 ** Return the number of bytes read.  The value is stored in *v.
 21709 */
 21710 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
 21711   u32 a,b,s;
 21713   a = *p;
 21714   /* a: p0 (unmasked) */
 21715   if (!(a&0x80))
 21717     *v = a;
 21718     return 1;
 21721   p++;
 21722   b = *p;
 21723   /* b: p1 (unmasked) */
 21724   if (!(b&0x80))
 21726     a &= 0x7f;
 21727     a = a<<7;
 21728     a |= b;
 21729     *v = a;
 21730     return 2;
 21733   /* Verify that constants are precomputed correctly */
 21734   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
 21735   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
 21737   p++;
 21738   a = a<<14;
 21739   a |= *p;
 21740   /* a: p0<<14 | p2 (unmasked) */
 21741   if (!(a&0x80))
 21743     a &= SLOT_2_0;
 21744     b &= 0x7f;
 21745     b = b<<7;
 21746     a |= b;
 21747     *v = a;
 21748     return 3;
 21751   /* CSE1 from below */
 21752   a &= SLOT_2_0;
 21753   p++;
 21754   b = b<<14;
 21755   b |= *p;
 21756   /* b: p1<<14 | p3 (unmasked) */
 21757   if (!(b&0x80))
 21759     b &= SLOT_2_0;
 21760     /* moved CSE1 up */
 21761     /* a &= (0x7f<<14)|(0x7f); */
 21762     a = a<<7;
 21763     a |= b;
 21764     *v = a;
 21765     return 4;
 21768   /* a: p0<<14 | p2 (masked) */
 21769   /* b: p1<<14 | p3 (unmasked) */
 21770   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 21771   /* moved CSE1 up */
 21772   /* a &= (0x7f<<14)|(0x7f); */
 21773   b &= SLOT_2_0;
 21774   s = a;
 21775   /* s: p0<<14 | p2 (masked) */
 21777   p++;
 21778   a = a<<14;
 21779   a |= *p;
 21780   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
 21781   if (!(a&0x80))
 21783     /* we can skip these cause they were (effectively) done above in calc'ing s */
 21784     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
 21785     /* b &= (0x7f<<14)|(0x7f); */
 21786     b = b<<7;
 21787     a |= b;
 21788     s = s>>18;
 21789     *v = ((u64)s)<<32 | a;
 21790     return 5;
 21793   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 21794   s = s<<7;
 21795   s |= b;
 21796   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 21798   p++;
 21799   b = b<<14;
 21800   b |= *p;
 21801   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
 21802   if (!(b&0x80))
 21804     /* we can skip this cause it was (effectively) done above in calc'ing s */
 21805     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
 21806     a &= SLOT_2_0;
 21807     a = a<<7;
 21808     a |= b;
 21809     s = s>>18;
 21810     *v = ((u64)s)<<32 | a;
 21811     return 6;
 21814   p++;
 21815   a = a<<14;
 21816   a |= *p;
 21817   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
 21818   if (!(a&0x80))
 21820     a &= SLOT_4_2_0;
 21821     b &= SLOT_2_0;
 21822     b = b<<7;
 21823     a |= b;
 21824     s = s>>11;
 21825     *v = ((u64)s)<<32 | a;
 21826     return 7;
 21829   /* CSE2 from below */
 21830   a &= SLOT_2_0;
 21831   p++;
 21832   b = b<<14;
 21833   b |= *p;
 21834   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
 21835   if (!(b&0x80))
 21837     b &= SLOT_4_2_0;
 21838     /* moved CSE2 up */
 21839     /* a &= (0x7f<<14)|(0x7f); */
 21840     a = a<<7;
 21841     a |= b;
 21842     s = s>>4;
 21843     *v = ((u64)s)<<32 | a;
 21844     return 8;
 21847   p++;
 21848   a = a<<15;
 21849   a |= *p;
 21850   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
 21852   /* moved CSE2 up */
 21853   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
 21854   b &= SLOT_2_0;
 21855   b = b<<8;
 21856   a |= b;
 21858   s = s<<4;
 21859   b = p[-4];
 21860   b &= 0x7f;
 21861   b = b>>3;
 21862   s |= b;
 21864   *v = ((u64)s)<<32 | a;
 21866   return 9;
 21869 /*
 21870 ** Read a 32-bit variable-length integer from memory starting at p[0].
 21871 ** Return the number of bytes read.  The value is stored in *v.
 21872 **
 21873 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
 21874 ** integer, then set *v to 0xffffffff.
 21875 **
 21876 ** A MACRO version, getVarint32, is provided which inlines the 
 21877 ** single-byte case.  All code should use the MACRO version as 
 21878 ** this function assumes the single-byte case has already been handled.
 21879 */
 21880 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
 21881   u32 a,b;
 21883   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
 21884   ** by the getVarin32() macro */
 21885   a = *p;
 21886   /* a: p0 (unmasked) */
 21887 #ifndef getVarint32
 21888   if (!(a&0x80))
 21890     /* Values between 0 and 127 */
 21891     *v = a;
 21892     return 1;
 21894 #endif
 21896   /* The 2-byte case */
 21897   p++;
 21898   b = *p;
 21899   /* b: p1 (unmasked) */
 21900   if (!(b&0x80))
 21902     /* Values between 128 and 16383 */
 21903     a &= 0x7f;
 21904     a = a<<7;
 21905     *v = a | b;
 21906     return 2;
 21909   /* The 3-byte case */
 21910   p++;
 21911   a = a<<14;
 21912   a |= *p;
 21913   /* a: p0<<14 | p2 (unmasked) */
 21914   if (!(a&0x80))
 21916     /* Values between 16384 and 2097151 */
 21917     a &= (0x7f<<14)|(0x7f);
 21918     b &= 0x7f;
 21919     b = b<<7;
 21920     *v = a | b;
 21921     return 3;
 21924   /* A 32-bit varint is used to store size information in btrees.
 21925   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
 21926   ** A 3-byte varint is sufficient, for example, to record the size
 21927   ** of a 1048569-byte BLOB or string.
 21928   **
 21929   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
 21930   ** rare larger cases can be handled by the slower 64-bit varint
 21931   ** routine.
 21932   */
 21933 #if 1
 21935     u64 v64;
 21936     u8 n;
 21938     p -= 2;
 21939     n = sqlite3GetVarint(p, &v64);
 21940     assert( n>3 && n<=9 );
 21941     if( (v64 & SQLITE_MAX_U32)!=v64 ){
 21942       *v = 0xffffffff;
 21943     }else{
 21944       *v = (u32)v64;
 21946     return n;
 21949 #else
 21950   /* For following code (kept for historical record only) shows an
 21951   ** unrolling for the 3- and 4-byte varint cases.  This code is
 21952   ** slightly faster, but it is also larger and much harder to test.
 21953   */
 21954   p++;
 21955   b = b<<14;
 21956   b |= *p;
 21957   /* b: p1<<14 | p3 (unmasked) */
 21958   if (!(b&0x80))
 21960     /* Values between 2097152 and 268435455 */
 21961     b &= (0x7f<<14)|(0x7f);
 21962     a &= (0x7f<<14)|(0x7f);
 21963     a = a<<7;
 21964     *v = a | b;
 21965     return 4;
 21968   p++;
 21969   a = a<<14;
 21970   a |= *p;
 21971   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
 21972   if (!(a&0x80))
 21974     /* Values  between 268435456 and 34359738367 */
 21975     a &= SLOT_4_2_0;
 21976     b &= SLOT_4_2_0;
 21977     b = b<<7;
 21978     *v = a | b;
 21979     return 5;
 21982   /* We can only reach this point when reading a corrupt database
 21983   ** file.  In that case we are not in any hurry.  Use the (relatively
 21984   ** slow) general-purpose sqlite3GetVarint() routine to extract the
 21985   ** value. */
 21987     u64 v64;
 21988     u8 n;
 21990     p -= 4;
 21991     n = sqlite3GetVarint(p, &v64);
 21992     assert( n>5 && n<=9 );
 21993     *v = (u32)v64;
 21994     return n;
 21996 #endif
 21999 /*
 22000 ** Return the number of bytes that will be needed to store the given
 22001 ** 64-bit integer.
 22002 */
 22003 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
 22004   int i = 0;
 22005   do{
 22006     i++;
 22007     v >>= 7;
 22008   }while( v!=0 && ALWAYS(i<9) );
 22009   return i;
 22013 /*
 22014 ** Read or write a four-byte big-endian integer value.
 22015 */
 22016 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
 22017   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 22019 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
 22020   p[0] = (u8)(v>>24);
 22021   p[1] = (u8)(v>>16);
 22022   p[2] = (u8)(v>>8);
 22023   p[3] = (u8)v;
 22028 /*
 22029 ** Translate a single byte of Hex into an integer.
 22030 ** This routine only works if h really is a valid hexadecimal
 22031 ** character:  0..9a..fA..F
 22032 */
 22033 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
 22034   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
 22035 #ifdef SQLITE_ASCII
 22036   h += 9*(1&(h>>6));
 22037 #endif
 22038 #ifdef SQLITE_EBCDIC
 22039   h += 9*(1&~(h>>4));
 22040 #endif
 22041   return (u8)(h & 0xf);
 22044 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
 22045 /*
 22046 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
 22047 ** value.  Return a pointer to its binary value.  Space to hold the
 22048 ** binary value has been obtained from malloc and must be freed by
 22049 ** the calling routine.
 22050 */
 22051 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
 22052   char *zBlob;
 22053   int i;
 22055   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
 22056   n--;
 22057   if( zBlob ){
 22058     for(i=0; i<n; i+=2){
 22059       zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
 22061     zBlob[i/2] = 0;
 22063   return zBlob;
 22065 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
 22067 /*
 22068 ** Log an error that is an API call on a connection pointer that should
 22069 ** not have been used.  The "type" of connection pointer is given as the
 22070 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
 22071 */
 22072 static void logBadConnection(const char *zType){
 22073   sqlite3_log(SQLITE_MISUSE, 
 22074      "API call with %s database connection pointer",
 22075      zType
 22076   );
 22079 /*
 22080 ** Check to make sure we have a valid db pointer.  This test is not
 22081 ** foolproof but it does provide some measure of protection against
 22082 ** misuse of the interface such as passing in db pointers that are
 22083 ** NULL or which have been previously closed.  If this routine returns
 22084 ** 1 it means that the db pointer is valid and 0 if it should not be
 22085 ** dereferenced for any reason.  The calling function should invoke
 22086 ** SQLITE_MISUSE immediately.
 22087 **
 22088 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
 22089 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
 22090 ** open properly and is not fit for general use but which can be
 22091 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
 22092 */
 22093 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
 22094   u32 magic;
 22095   if( db==0 ){
 22096     logBadConnection("NULL");
 22097     return 0;
 22099   magic = db->magic;
 22100   if( magic!=SQLITE_MAGIC_OPEN ){
 22101     if( sqlite3SafetyCheckSickOrOk(db) ){
 22102       testcase( sqlite3GlobalConfig.xLog!=0 );
 22103       logBadConnection("unopened");
 22105     return 0;
 22106   }else{
 22107     return 1;
 22110 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
 22111   u32 magic;
 22112   magic = db->magic;
 22113   if( magic!=SQLITE_MAGIC_SICK &&
 22114       magic!=SQLITE_MAGIC_OPEN &&
 22115       magic!=SQLITE_MAGIC_BUSY ){
 22116     testcase( sqlite3GlobalConfig.xLog!=0 );
 22117     logBadConnection("invalid");
 22118     return 0;
 22119   }else{
 22120     return 1;
 22124 /*
 22125 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
 22126 ** the other 64-bit signed integer at *pA and store the result in *pA.
 22127 ** Return 0 on success.  Or if the operation would have resulted in an
 22128 ** overflow, leave *pA unchanged and return 1.
 22129 */
 22130 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
 22131   i64 iA = *pA;
 22132   testcase( iA==0 ); testcase( iA==1 );
 22133   testcase( iB==-1 ); testcase( iB==0 );
 22134   if( iB>=0 ){
 22135     testcase( iA>0 && LARGEST_INT64 - iA == iB );
 22136     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
 22137     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
 22138     *pA += iB;
 22139   }else{
 22140     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
 22141     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
 22142     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 22143     *pA += iB;
 22145   return 0; 
 22147 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 22148   testcase( iB==SMALLEST_INT64+1 );
 22149   if( iB==SMALLEST_INT64 ){
 22150     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 22151     if( (*pA)>=0 ) return 1;
 22152     *pA -= iB;
 22153     return 0;
 22154   }else{
 22155     return sqlite3AddInt64(pA, -iB);
 22158 #define TWOPOWER32 (((i64)1)<<32)
 22159 #define TWOPOWER31 (((i64)1)<<31)
 22160 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 22161   i64 iA = *pA;
 22162   i64 iA1, iA0, iB1, iB0, r;
 22164   iA1 = iA/TWOPOWER32;
 22165   iA0 = iA % TWOPOWER32;
 22166   iB1 = iB/TWOPOWER32;
 22167   iB0 = iB % TWOPOWER32;
 22168   if( iA1*iB1 != 0 ) return 1;
 22169   assert( iA1*iB0==0 || iA0*iB1==0 );
 22170   r = iA1*iB0 + iA0*iB1;
 22171   testcase( r==(-TWOPOWER31)-1 );
 22172   testcase( r==(-TWOPOWER31) );
 22173   testcase( r==TWOPOWER31 );
 22174   testcase( r==TWOPOWER31-1 );
 22175   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
 22176   r *= TWOPOWER32;
 22177   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
 22178   *pA = r;
 22179   return 0;
 22182 /*
 22183 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
 22184 ** if the integer has a value of -2147483648, return +2147483647
 22185 */
 22186 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
 22187   if( x>=0 ) return x;
 22188   if( x==(int)0x80000000 ) return 0x7fffffff;
 22189   return -x;
 22192 #ifdef SQLITE_ENABLE_8_3_NAMES
 22193 /*
 22194 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
 22195 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
 22196 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
 22197 ** three characters, then shorten the suffix on z[] to be the last three
 22198 ** characters of the original suffix.
 22199 **
 22200 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
 22201 ** do the suffix shortening regardless of URI parameter.
 22202 **
 22203 ** Examples:
 22204 **
 22205 **     test.db-journal    =>   test.nal
 22206 **     test.db-wal        =>   test.wal
 22207 **     test.db-shm        =>   test.shm
 22208 **     test.db-mj7f3319fa =>   test.9fa
 22209 */
 22210 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
 22211 #if SQLITE_ENABLE_8_3_NAMES<2
 22212   if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
 22213 #endif
 22215     int i, sz;
 22216     sz = sqlite3Strlen30(z);
 22217     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
 22218     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
 22221 #endif
 22223 /************** End of util.c ************************************************/
 22224 /************** Begin file hash.c ********************************************/
 22225 /*
 22226 ** 2001 September 22
 22227 **
 22228 ** The author disclaims copyright to this source code.  In place of
 22229 ** a legal notice, here is a blessing:
 22230 **
 22231 **    May you do good and not evil.
 22232 **    May you find forgiveness for yourself and forgive others.
 22233 **    May you share freely, never taking more than you give.
 22234 **
 22235 *************************************************************************
 22236 ** This is the implementation of generic hash-tables
 22237 ** used in SQLite.
 22238 */
 22239 /* #include <assert.h> */
 22241 /* Turn bulk memory into a hash table object by initializing the
 22242 ** fields of the Hash structure.
 22243 **
 22244 ** "pNew" is a pointer to the hash table that is to be initialized.
 22245 */
 22246 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
 22247   assert( pNew!=0 );
 22248   pNew->first = 0;
 22249   pNew->count = 0;
 22250   pNew->htsize = 0;
 22251   pNew->ht = 0;
 22254 /* Remove all entries from a hash table.  Reclaim all memory.
 22255 ** Call this routine to delete a hash table or to reset a hash table
 22256 ** to the empty state.
 22257 */
 22258 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
 22259   HashElem *elem;         /* For looping over all elements of the table */
 22261   assert( pH!=0 );
 22262   elem = pH->first;
 22263   pH->first = 0;
 22264   sqlite3_free(pH->ht);
 22265   pH->ht = 0;
 22266   pH->htsize = 0;
 22267   while( elem ){
 22268     HashElem *next_elem = elem->next;
 22269     sqlite3_free(elem);
 22270     elem = next_elem;
 22272   pH->count = 0;
 22275 /*
 22276 ** The hashing function.
 22277 */
 22278 static unsigned int strHash(const char *z, int nKey){
 22279   int h = 0;
 22280   assert( nKey>=0 );
 22281   while( nKey > 0  ){
 22282     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
 22283     nKey--;
 22285   return h;
 22289 /* Link pNew element into the hash table pH.  If pEntry!=0 then also
 22290 ** insert pNew into the pEntry hash bucket.
 22291 */
 22292 static void insertElement(
 22293   Hash *pH,              /* The complete hash table */
 22294   struct _ht *pEntry,    /* The entry into which pNew is inserted */
 22295   HashElem *pNew         /* The element to be inserted */
 22296 ){
 22297   HashElem *pHead;       /* First element already in pEntry */
 22298   if( pEntry ){
 22299     pHead = pEntry->count ? pEntry->chain : 0;
 22300     pEntry->count++;
 22301     pEntry->chain = pNew;
 22302   }else{
 22303     pHead = 0;
 22305   if( pHead ){
 22306     pNew->next = pHead;
 22307     pNew->prev = pHead->prev;
 22308     if( pHead->prev ){ pHead->prev->next = pNew; }
 22309     else             { pH->first = pNew; }
 22310     pHead->prev = pNew;
 22311   }else{
 22312     pNew->next = pH->first;
 22313     if( pH->first ){ pH->first->prev = pNew; }
 22314     pNew->prev = 0;
 22315     pH->first = pNew;
 22320 /* Resize the hash table so that it cantains "new_size" buckets.
 22321 **
 22322 ** The hash table might fail to resize if sqlite3_malloc() fails or
 22323 ** if the new size is the same as the prior size.
 22324 ** Return TRUE if the resize occurs and false if not.
 22325 */
 22326 static int rehash(Hash *pH, unsigned int new_size){
 22327   struct _ht *new_ht;            /* The new hash table */
 22328   HashElem *elem, *next_elem;    /* For looping over existing elements */
 22330 #if SQLITE_MALLOC_SOFT_LIMIT>0
 22331   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
 22332     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
 22334   if( new_size==pH->htsize ) return 0;
 22335 #endif
 22337   /* The inability to allocates space for a larger hash table is
 22338   ** a performance hit but it is not a fatal error.  So mark the
 22339   ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of 
 22340   ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
 22341   ** only zeroes the requested number of bytes whereas this module will
 22342   ** use the actual amount of space allocated for the hash table (which
 22343   ** may be larger than the requested amount).
 22344   */
 22345   sqlite3BeginBenignMalloc();
 22346   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
 22347   sqlite3EndBenignMalloc();
 22349   if( new_ht==0 ) return 0;
 22350   sqlite3_free(pH->ht);
 22351   pH->ht = new_ht;
 22352   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
 22353   memset(new_ht, 0, new_size*sizeof(struct _ht));
 22354   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
 22355     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
 22356     next_elem = elem->next;
 22357     insertElement(pH, &new_ht[h], elem);
 22359   return 1;
 22362 /* This function (for internal use only) locates an element in an
 22363 ** hash table that matches the given key.  The hash for this key has
 22364 ** already been computed and is passed as the 4th parameter.
 22365 */
 22366 static HashElem *findElementGivenHash(
 22367   const Hash *pH,     /* The pH to be searched */
 22368   const char *pKey,   /* The key we are searching for */
 22369   int nKey,           /* Bytes in key (not counting zero terminator) */
 22370   unsigned int h      /* The hash for this key. */
 22371 ){
 22372   HashElem *elem;                /* Used to loop thru the element list */
 22373   int count;                     /* Number of elements left to test */
 22375   if( pH->ht ){
 22376     struct _ht *pEntry = &pH->ht[h];
 22377     elem = pEntry->chain;
 22378     count = pEntry->count;
 22379   }else{
 22380     elem = pH->first;
 22381     count = pH->count;
 22383   while( count-- && ALWAYS(elem) ){
 22384     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
 22385       return elem;
 22387     elem = elem->next;
 22389   return 0;
 22392 /* Remove a single entry from the hash table given a pointer to that
 22393 ** element and a hash on the element's key.
 22394 */
 22395 static void removeElementGivenHash(
 22396   Hash *pH,         /* The pH containing "elem" */
 22397   HashElem* elem,   /* The element to be removed from the pH */
 22398   unsigned int h    /* Hash value for the element */
 22399 ){
 22400   struct _ht *pEntry;
 22401   if( elem->prev ){
 22402     elem->prev->next = elem->next; 
 22403   }else{
 22404     pH->first = elem->next;
 22406   if( elem->next ){
 22407     elem->next->prev = elem->prev;
 22409   if( pH->ht ){
 22410     pEntry = &pH->ht[h];
 22411     if( pEntry->chain==elem ){
 22412       pEntry->chain = elem->next;
 22414     pEntry->count--;
 22415     assert( pEntry->count>=0 );
 22417   sqlite3_free( elem );
 22418   pH->count--;
 22419   if( pH->count==0 ){
 22420     assert( pH->first==0 );
 22421     assert( pH->count==0 );
 22422     sqlite3HashClear(pH);
 22426 /* Attempt to locate an element of the hash table pH with a key
 22427 ** that matches pKey,nKey.  Return the data for this element if it is
 22428 ** found, or NULL if there is no match.
 22429 */
 22430 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
 22431   HashElem *elem;    /* The element that matches key */
 22432   unsigned int h;    /* A hash on key */
 22434   assert( pH!=0 );
 22435   assert( pKey!=0 );
 22436   assert( nKey>=0 );
 22437   if( pH->ht ){
 22438     h = strHash(pKey, nKey) % pH->htsize;
 22439   }else{
 22440     h = 0;
 22442   elem = findElementGivenHash(pH, pKey, nKey, h);
 22443   return elem ? elem->data : 0;
 22446 /* Insert an element into the hash table pH.  The key is pKey,nKey
 22447 ** and the data is "data".
 22448 **
 22449 ** If no element exists with a matching key, then a new
 22450 ** element is created and NULL is returned.
 22451 **
 22452 ** If another element already exists with the same key, then the
 22453 ** new data replaces the old data and the old data is returned.
 22454 ** The key is not copied in this instance.  If a malloc fails, then
 22455 ** the new data is returned and the hash table is unchanged.
 22456 **
 22457 ** If the "data" parameter to this function is NULL, then the
 22458 ** element corresponding to "key" is removed from the hash table.
 22459 */
 22460 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
 22461   unsigned int h;       /* the hash of the key modulo hash table size */
 22462   HashElem *elem;       /* Used to loop thru the element list */
 22463   HashElem *new_elem;   /* New element added to the pH */
 22465   assert( pH!=0 );
 22466   assert( pKey!=0 );
 22467   assert( nKey>=0 );
 22468   if( pH->htsize ){
 22469     h = strHash(pKey, nKey) % pH->htsize;
 22470   }else{
 22471     h = 0;
 22473   elem = findElementGivenHash(pH,pKey,nKey,h);
 22474   if( elem ){
 22475     void *old_data = elem->data;
 22476     if( data==0 ){
 22477       removeElementGivenHash(pH,elem,h);
 22478     }else{
 22479       elem->data = data;
 22480       elem->pKey = pKey;
 22481       assert(nKey==elem->nKey);
 22483     return old_data;
 22485   if( data==0 ) return 0;
 22486   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
 22487   if( new_elem==0 ) return data;
 22488   new_elem->pKey = pKey;
 22489   new_elem->nKey = nKey;
 22490   new_elem->data = data;
 22491   pH->count++;
 22492   if( pH->count>=10 && pH->count > 2*pH->htsize ){
 22493     if( rehash(pH, pH->count*2) ){
 22494       assert( pH->htsize>0 );
 22495       h = strHash(pKey, nKey) % pH->htsize;
 22498   if( pH->ht ){
 22499     insertElement(pH, &pH->ht[h], new_elem);
 22500   }else{
 22501     insertElement(pH, 0, new_elem);
 22503   return 0;
 22506 /************** End of hash.c ************************************************/
 22507 /************** Begin file opcodes.c *****************************************/
 22508 /* Automatically generated.  Do not edit */
 22509 /* See the mkopcodec.awk script for details. */
 22510 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 22511 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
 22512  static const char *const azName[] = { "?",
 22513      /*   1 */ "Goto",
 22514      /*   2 */ "Gosub",
 22515      /*   3 */ "Return",
 22516      /*   4 */ "Yield",
 22517      /*   5 */ "HaltIfNull",
 22518      /*   6 */ "Halt",
 22519      /*   7 */ "Integer",
 22520      /*   8 */ "Int64",
 22521      /*   9 */ "String",
 22522      /*  10 */ "Null",
 22523      /*  11 */ "Blob",
 22524      /*  12 */ "Variable",
 22525      /*  13 */ "Move",
 22526      /*  14 */ "Copy",
 22527      /*  15 */ "SCopy",
 22528      /*  16 */ "ResultRow",
 22529      /*  17 */ "CollSeq",
 22530      /*  18 */ "Function",
 22531      /*  19 */ "Not",
 22532      /*  20 */ "AddImm",
 22533      /*  21 */ "MustBeInt",
 22534      /*  22 */ "RealAffinity",
 22535      /*  23 */ "Permutation",
 22536      /*  24 */ "Compare",
 22537      /*  25 */ "Jump",
 22538      /*  26 */ "Once",
 22539      /*  27 */ "If",
 22540      /*  28 */ "IfNot",
 22541      /*  29 */ "Column",
 22542      /*  30 */ "Affinity",
 22543      /*  31 */ "MakeRecord",
 22544      /*  32 */ "Count",
 22545      /*  33 */ "Savepoint",
 22546      /*  34 */ "AutoCommit",
 22547      /*  35 */ "Transaction",
 22548      /*  36 */ "ReadCookie",
 22549      /*  37 */ "SetCookie",
 22550      /*  38 */ "VerifyCookie",
 22551      /*  39 */ "OpenRead",
 22552      /*  40 */ "OpenWrite",
 22553      /*  41 */ "OpenAutoindex",
 22554      /*  42 */ "OpenEphemeral",
 22555      /*  43 */ "SorterOpen",
 22556      /*  44 */ "OpenPseudo",
 22557      /*  45 */ "Close",
 22558      /*  46 */ "SeekLt",
 22559      /*  47 */ "SeekLe",
 22560      /*  48 */ "SeekGe",
 22561      /*  49 */ "SeekGt",
 22562      /*  50 */ "Seek",
 22563      /*  51 */ "NotFound",
 22564      /*  52 */ "Found",
 22565      /*  53 */ "IsUnique",
 22566      /*  54 */ "NotExists",
 22567      /*  55 */ "Sequence",
 22568      /*  56 */ "NewRowid",
 22569      /*  57 */ "Insert",
 22570      /*  58 */ "InsertInt",
 22571      /*  59 */ "Delete",
 22572      /*  60 */ "ResetCount",
 22573      /*  61 */ "SorterCompare",
 22574      /*  62 */ "SorterData",
 22575      /*  63 */ "RowKey",
 22576      /*  64 */ "RowData",
 22577      /*  65 */ "Rowid",
 22578      /*  66 */ "NullRow",
 22579      /*  67 */ "Last",
 22580      /*  68 */ "Or",
 22581      /*  69 */ "And",
 22582      /*  70 */ "SorterSort",
 22583      /*  71 */ "Sort",
 22584      /*  72 */ "Rewind",
 22585      /*  73 */ "IsNull",
 22586      /*  74 */ "NotNull",
 22587      /*  75 */ "Ne",
 22588      /*  76 */ "Eq",
 22589      /*  77 */ "Gt",
 22590      /*  78 */ "Le",
 22591      /*  79 */ "Lt",
 22592      /*  80 */ "Ge",
 22593      /*  81 */ "SorterNext",
 22594      /*  82 */ "BitAnd",
 22595      /*  83 */ "BitOr",
 22596      /*  84 */ "ShiftLeft",
 22597      /*  85 */ "ShiftRight",
 22598      /*  86 */ "Add",
 22599      /*  87 */ "Subtract",
 22600      /*  88 */ "Multiply",
 22601      /*  89 */ "Divide",
 22602      /*  90 */ "Remainder",
 22603      /*  91 */ "Concat",
 22604      /*  92 */ "Prev",
 22605      /*  93 */ "BitNot",
 22606      /*  94 */ "String8",
 22607      /*  95 */ "Next",
 22608      /*  96 */ "SorterInsert",
 22609      /*  97 */ "IdxInsert",
 22610      /*  98 */ "IdxDelete",
 22611      /*  99 */ "IdxRowid",
 22612      /* 100 */ "IdxLT",
 22613      /* 101 */ "IdxGE",
 22614      /* 102 */ "Destroy",
 22615      /* 103 */ "Clear",
 22616      /* 104 */ "CreateIndex",
 22617      /* 105 */ "CreateTable",
 22618      /* 106 */ "ParseSchema",
 22619      /* 107 */ "LoadAnalysis",
 22620      /* 108 */ "DropTable",
 22621      /* 109 */ "DropIndex",
 22622      /* 110 */ "DropTrigger",
 22623      /* 111 */ "IntegrityCk",
 22624      /* 112 */ "RowSetAdd",
 22625      /* 113 */ "RowSetRead",
 22626      /* 114 */ "RowSetTest",
 22627      /* 115 */ "Program",
 22628      /* 116 */ "Param",
 22629      /* 117 */ "FkCounter",
 22630      /* 118 */ "FkIfZero",
 22631      /* 119 */ "MemMax",
 22632      /* 120 */ "IfPos",
 22633      /* 121 */ "IfNeg",
 22634      /* 122 */ "IfZero",
 22635      /* 123 */ "AggStep",
 22636      /* 124 */ "AggFinal",
 22637      /* 125 */ "Checkpoint",
 22638      /* 126 */ "JournalMode",
 22639      /* 127 */ "Vacuum",
 22640      /* 128 */ "IncrVacuum",
 22641      /* 129 */ "Expire",
 22642      /* 130 */ "Real",
 22643      /* 131 */ "TableLock",
 22644      /* 132 */ "VBegin",
 22645      /* 133 */ "VCreate",
 22646      /* 134 */ "VDestroy",
 22647      /* 135 */ "VOpen",
 22648      /* 136 */ "VFilter",
 22649      /* 137 */ "VColumn",
 22650      /* 138 */ "VNext",
 22651      /* 139 */ "VRename",
 22652      /* 140 */ "VUpdate",
 22653      /* 141 */ "ToText",
 22654      /* 142 */ "ToBlob",
 22655      /* 143 */ "ToNumeric",
 22656      /* 144 */ "ToInt",
 22657      /* 145 */ "ToReal",
 22658      /* 146 */ "Pagecount",
 22659      /* 147 */ "MaxPgcnt",
 22660      /* 148 */ "Trace",
 22661      /* 149 */ "Noop",
 22662      /* 150 */ "Explain",
 22663   };
 22664   return azName[i];
 22666 #endif
 22668 /************** End of opcodes.c *********************************************/
 22669 /************** Begin file os_unix.c *****************************************/
 22670 /*
 22671 ** 2004 May 22
 22672 **
 22673 ** The author disclaims copyright to this source code.  In place of
 22674 ** a legal notice, here is a blessing:
 22675 **
 22676 **    May you do good and not evil.
 22677 **    May you find forgiveness for yourself and forgive others.
 22678 **    May you share freely, never taking more than you give.
 22679 **
 22680 ******************************************************************************
 22681 **
 22682 ** This file contains the VFS implementation for unix-like operating systems
 22683 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
 22684 **
 22685 ** There are actually several different VFS implementations in this file.
 22686 ** The differences are in the way that file locking is done.  The default
 22687 ** implementation uses Posix Advisory Locks.  Alternative implementations
 22688 ** use flock(), dot-files, various proprietary locking schemas, or simply
 22689 ** skip locking all together.
 22690 **
 22691 ** This source file is organized into divisions where the logic for various
 22692 ** subfunctions is contained within the appropriate division.  PLEASE
 22693 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
 22694 ** in the correct division and should be clearly labeled.
 22695 **
 22696 ** The layout of divisions is as follows:
 22697 **
 22698 **   *  General-purpose declarations and utility functions.
 22699 **   *  Unique file ID logic used by VxWorks.
 22700 **   *  Various locking primitive implementations (all except proxy locking):
 22701 **      + for Posix Advisory Locks
 22702 **      + for no-op locks
 22703 **      + for dot-file locks
 22704 **      + for flock() locking
 22705 **      + for named semaphore locks (VxWorks only)
 22706 **      + for AFP filesystem locks (MacOSX only)
 22707 **   *  sqlite3_file methods not associated with locking.
 22708 **   *  Definitions of sqlite3_io_methods objects for all locking
 22709 **      methods plus "finder" functions for each locking method.
 22710 **   *  sqlite3_vfs method implementations.
 22711 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
 22712 **   *  Definitions of sqlite3_vfs objects for all locking methods
 22713 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
 22714 */
 22715 #if SQLITE_OS_UNIX              /* This file is used on unix only */
 22717 /* Use posix_fallocate() if it is available
 22718 */
 22719 #if !defined(HAVE_POSIX_FALLOCATE) \
 22720       && (_XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L)
 22721 # define HAVE_POSIX_FALLOCATE 1
 22722 #endif
 22724 /*
 22725 ** There are various methods for file locking used for concurrency
 22726 ** control:
 22727 **
 22728 **   1. POSIX locking (the default),
 22729 **   2. No locking,
 22730 **   3. Dot-file locking,
 22731 **   4. flock() locking,
 22732 **   5. AFP locking (OSX only),
 22733 **   6. Named POSIX semaphores (VXWorks only),
 22734 **   7. proxy locking. (OSX only)
 22735 **
 22736 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
 22737 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
 22738 ** selection of the appropriate locking style based on the filesystem
 22739 ** where the database is located.  
 22740 */
 22741 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
 22742 #  if defined(__APPLE__)
 22743 #    define SQLITE_ENABLE_LOCKING_STYLE 1
 22744 #  else
 22745 #    define SQLITE_ENABLE_LOCKING_STYLE 0
 22746 #  endif
 22747 #endif
 22749 /*
 22750 ** Define the OS_VXWORKS pre-processor macro to 1 if building on 
 22751 ** vxworks, or 0 otherwise.
 22752 */
 22753 #ifndef OS_VXWORKS
 22754 #  if defined(__RTP__) || defined(_WRS_KERNEL)
 22755 #    define OS_VXWORKS 1
 22756 #  else
 22757 #    define OS_VXWORKS 0
 22758 #  endif
 22759 #endif
 22761 /*
 22762 ** These #defines should enable >2GB file support on Posix if the
 22763 ** underlying operating system supports it.  If the OS lacks
 22764 ** large file support, these should be no-ops.
 22765 **
 22766 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
 22767 ** on the compiler command line.  This is necessary if you are compiling
 22768 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
 22769 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
 22770 ** without this option, LFS is enable.  But LFS does not exist in the kernel
 22771 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
 22772 ** portability you should omit LFS.
 22773 **
 22774 ** The previous paragraph was written in 2005.  (This paragraph is written
 22775 ** on 2008-11-28.) These days, all Linux kernels support large files, so
 22776 ** you should probably leave LFS enabled.  But some embedded platforms might
 22777 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
 22778 */
 22779 #ifndef SQLITE_DISABLE_LFS
 22780 # define _LARGE_FILE       1
 22781 # ifndef _FILE_OFFSET_BITS
 22782 #   define _FILE_OFFSET_BITS 64
 22783 # endif
 22784 # define _LARGEFILE_SOURCE 1
 22785 #endif
 22787 /*
 22788 ** standard include files.
 22789 */
 22790 #include <sys/types.h>
 22791 #include <sys/stat.h>
 22792 #include <fcntl.h>
 22793 #include <unistd.h>
 22794 /* #include <time.h> */
 22795 #include <sys/time.h>
 22796 #include <errno.h>
 22797 #ifndef SQLITE_OMIT_WAL
 22798 #include <sys/mman.h>
 22799 #endif
 22802 #if SQLITE_ENABLE_LOCKING_STYLE
 22803 # include <sys/ioctl.h>
 22804 # if OS_VXWORKS
 22805 #  include <semaphore.h>
 22806 #  include <limits.h>
 22807 # else
 22808 #  include <sys/file.h>
 22809 #  include <sys/param.h>
 22810 # endif
 22811 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
 22813 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
 22814 # include <sys/mount.h>
 22815 #endif
 22817 #ifdef HAVE_UTIME
 22818 # include <utime.h>
 22819 #endif
 22821 /*
 22822 ** Allowed values of unixFile.fsFlags
 22823 */
 22824 #define SQLITE_FSFLAGS_IS_MSDOS     0x1
 22826 /*
 22827 ** If we are to be thread-safe, include the pthreads header and define
 22828 ** the SQLITE_UNIX_THREADS macro.
 22829 */
 22830 #if SQLITE_THREADSAFE
 22831 /* # include <pthread.h> */
 22832 # define SQLITE_UNIX_THREADS 1
 22833 #endif
 22835 /*
 22836 ** Default permissions when creating a new file
 22837 */
 22838 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 22839 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
 22840 #endif
 22842 /*
 22843 ** Default permissions when creating auto proxy dir
 22844 */
 22845 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 22846 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
 22847 #endif
 22849 /*
 22850 ** Maximum supported path-length.
 22851 */
 22852 #define MAX_PATHNAME 512
 22854 /*
 22855 ** Only set the lastErrno if the error code is a real error and not 
 22856 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
 22857 */
 22858 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
 22860 /* Forward references */
 22861 typedef struct unixShm unixShm;               /* Connection shared memory */
 22862 typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
 22863 typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
 22864 typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
 22866 /*
 22867 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
 22868 ** cannot be closed immediately. In these cases, instances of the following
 22869 ** structure are used to store the file descriptor while waiting for an
 22870 ** opportunity to either close or reuse it.
 22871 */
 22872 struct UnixUnusedFd {
 22873   int fd;                   /* File descriptor to close */
 22874   int flags;                /* Flags this file descriptor was opened with */
 22875   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
 22876 };
 22878 /*
 22879 ** The unixFile structure is subclass of sqlite3_file specific to the unix
 22880 ** VFS implementations.
 22881 */
 22882 typedef struct unixFile unixFile;
 22883 struct unixFile {
 22884   sqlite3_io_methods const *pMethod;  /* Always the first entry */
 22885   sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
 22886   unixInodeInfo *pInode;              /* Info about locks on this inode */
 22887   int h;                              /* The file descriptor */
 22888   unsigned char eFileLock;            /* The type of lock held on this fd */
 22889   unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
 22890   int lastErrno;                      /* The unix errno from last I/O error */
 22891   void *lockingContext;               /* Locking style specific state */
 22892   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
 22893   const char *zPath;                  /* Name of the file */
 22894   unixShm *pShm;                      /* Shared memory segment information */
 22895   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
 22896 #ifdef __QNXNTO__
 22897   int sectorSize;                     /* Device sector size */
 22898   int deviceCharacteristics;          /* Precomputed device characteristics */
 22899 #endif
 22900 #if SQLITE_ENABLE_LOCKING_STYLE
 22901   int openFlags;                      /* The flags specified at open() */
 22902 #endif
 22903 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 22904   unsigned fsFlags;                   /* cached details from statfs() */
 22905 #endif
 22906 #if OS_VXWORKS
 22907   struct vxworksFileId *pId;          /* Unique file ID */
 22908 #endif
 22909 #ifdef SQLITE_DEBUG
 22910   /* The next group of variables are used to track whether or not the
 22911   ** transaction counter in bytes 24-27 of database files are updated
 22912   ** whenever any part of the database changes.  An assertion fault will
 22913   ** occur if a file is updated without also updating the transaction
 22914   ** counter.  This test is made to avoid new problems similar to the
 22915   ** one described by ticket #3584. 
 22916   */
 22917   unsigned char transCntrChng;   /* True if the transaction counter changed */
 22918   unsigned char dbUpdate;        /* True if any part of database file changed */
 22919   unsigned char inNormalWrite;   /* True if in a normal write operation */
 22920 #endif
 22921 #ifdef SQLITE_TEST
 22922   /* In test mode, increase the size of this structure a bit so that 
 22923   ** it is larger than the struct CrashFile defined in test6.c.
 22924   */
 22925   char aPadding[32];
 22926 #endif
 22927 };
 22929 /*
 22930 ** Allowed values for the unixFile.ctrlFlags bitmask:
 22931 */
 22932 #define UNIXFILE_EXCL        0x01     /* Connections from one process only */
 22933 #define UNIXFILE_RDONLY      0x02     /* Connection is read only */
 22934 #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
 22935 #ifndef SQLITE_DISABLE_DIRSYNC
 22936 # define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
 22937 #else
 22938 # define UNIXFILE_DIRSYNC    0x00
 22939 #endif
 22940 #define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
 22941 #define UNIXFILE_DELETE      0x20     /* Delete on close */
 22942 #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
 22943 #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
 22945 /*
 22946 ** Include code that is common to all os_*.c files
 22947 */
 22948 /************** Include os_common.h in the middle of os_unix.c ***************/
 22949 /************** Begin file os_common.h ***************************************/
 22950 /*
 22951 ** 2004 May 22
 22952 **
 22953 ** The author disclaims copyright to this source code.  In place of
 22954 ** a legal notice, here is a blessing:
 22955 **
 22956 **    May you do good and not evil.
 22957 **    May you find forgiveness for yourself and forgive others.
 22958 **    May you share freely, never taking more than you give.
 22959 **
 22960 ******************************************************************************
 22961 **
 22962 ** This file contains macros and a little bit of code that is common to
 22963 ** all of the platform-specific files (os_*.c) and is #included into those
 22964 ** files.
 22965 **
 22966 ** This file should be #included by the os_*.c files only.  It is not a
 22967 ** general purpose header file.
 22968 */
 22969 #ifndef _OS_COMMON_H_
 22970 #define _OS_COMMON_H_
 22972 /*
 22973 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
 22974 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 22975 ** switch.  The following code should catch this problem at compile-time.
 22976 */
 22977 #ifdef MEMORY_DEBUG
 22978 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 22979 #endif
 22981 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 22982 # ifndef SQLITE_DEBUG_OS_TRACE
 22983 #   define SQLITE_DEBUG_OS_TRACE 0
 22984 # endif
 22985   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 22986 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 22987 #else
 22988 # define OSTRACE(X)
 22989 #endif
 22991 /*
 22992 ** Macros for performance tracing.  Normally turned off.  Only works
 22993 ** on i486 hardware.
 22994 */
 22995 #ifdef SQLITE_PERFORMANCE_TRACE
 22997 /* 
 22998 ** hwtime.h contains inline assembler code for implementing 
 22999 ** high-performance timing routines.
 23000 */
 23001 /************** Include hwtime.h in the middle of os_common.h ****************/
 23002 /************** Begin file hwtime.h ******************************************/
 23003 /*
 23004 ** 2008 May 27
 23005 **
 23006 ** The author disclaims copyright to this source code.  In place of
 23007 ** a legal notice, here is a blessing:
 23008 **
 23009 **    May you do good and not evil.
 23010 **    May you find forgiveness for yourself and forgive others.
 23011 **    May you share freely, never taking more than you give.
 23012 **
 23013 ******************************************************************************
 23014 **
 23015 ** This file contains inline asm code for retrieving "high-performance"
 23016 ** counters for x86 class CPUs.
 23017 */
 23018 #ifndef _HWTIME_H_
 23019 #define _HWTIME_H_
 23021 /*
 23022 ** The following routine only works on pentium-class (or newer) processors.
 23023 ** It uses the RDTSC opcode to read the cycle count value out of the
 23024 ** processor and returns that value.  This can be used for high-res
 23025 ** profiling.
 23026 */
 23027 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
 23028       (defined(i386) || defined(__i386__) || defined(_M_IX86))
 23030   #if defined(__GNUC__)
 23032   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 23033      unsigned int lo, hi;
 23034      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 23035      return (sqlite_uint64)hi << 32 | lo;
 23038   #elif defined(_MSC_VER)
 23040   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 23041      __asm {
 23042         rdtsc
 23043         ret       ; return value at EDX:EAX
 23047   #endif
 23049 #elif (defined(__GNUC__) && defined(__x86_64__))
 23051   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 23052       unsigned long val;
 23053       __asm__ __volatile__ ("rdtsc" : "=A" (val));
 23054       return val;
 23057 #elif (defined(__GNUC__) && defined(__ppc__))
 23059   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 23060       unsigned long long retval;
 23061       unsigned long junk;
 23062       __asm__ __volatile__ ("\n\
 23063           1:      mftbu   %1\n\
 23064                   mftb    %L0\n\
 23065                   mftbu   %0\n\
 23066                   cmpw    %0,%1\n\
 23067                   bne     1b"
 23068                   : "=r" (retval), "=r" (junk));
 23069       return retval;
 23072 #else
 23074   #error Need implementation of sqlite3Hwtime() for your platform.
 23076   /*
 23077   ** To compile without implementing sqlite3Hwtime() for your platform,
 23078   ** you can remove the above #error and use the following
 23079   ** stub function.  You will lose timing support for many
 23080   ** of the debugging and testing utilities, but it should at
 23081   ** least compile and run.
 23082   */
 23083 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 23085 #endif
 23087 #endif /* !defined(_HWTIME_H_) */
 23089 /************** End of hwtime.h **********************************************/
 23090 /************** Continuing where we left off in os_common.h ******************/
 23092 static sqlite_uint64 g_start;
 23093 static sqlite_uint64 g_elapsed;
 23094 #define TIMER_START       g_start=sqlite3Hwtime()
 23095 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
 23096 #define TIMER_ELAPSED     g_elapsed
 23097 #else
 23098 #define TIMER_START
 23099 #define TIMER_END
 23100 #define TIMER_ELAPSED     ((sqlite_uint64)0)
 23101 #endif
 23103 /*
 23104 ** If we compile with the SQLITE_TEST macro set, then the following block
 23105 ** of code will give us the ability to simulate a disk I/O error.  This
 23106 ** is used for testing the I/O recovery logic.
 23107 */
 23108 #ifdef SQLITE_TEST
 23109 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
 23110 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
 23111 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
 23112 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
 23113 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
 23114 SQLITE_API int sqlite3_diskfull_pending = 0;
 23115 SQLITE_API int sqlite3_diskfull = 0;
 23116 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
 23117 #define SimulateIOError(CODE)  \
 23118   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
 23119        || sqlite3_io_error_pending-- == 1 )  \
 23120               { local_ioerr(); CODE; }
 23121 static void local_ioerr(){
 23122   IOTRACE(("IOERR\n"));
 23123   sqlite3_io_error_hit++;
 23124   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
 23126 #define SimulateDiskfullError(CODE) \
 23127    if( sqlite3_diskfull_pending ){ \
 23128      if( sqlite3_diskfull_pending == 1 ){ \
 23129        local_ioerr(); \
 23130        sqlite3_diskfull = 1; \
 23131        sqlite3_io_error_hit = 1; \
 23132        CODE; \
 23133      }else{ \
 23134        sqlite3_diskfull_pending--; \
 23135      } \
 23137 #else
 23138 #define SimulateIOErrorBenign(X)
 23139 #define SimulateIOError(A)
 23140 #define SimulateDiskfullError(A)
 23141 #endif
 23143 /*
 23144 ** When testing, keep a count of the number of open files.
 23145 */
 23146 #ifdef SQLITE_TEST
 23147 SQLITE_API int sqlite3_open_file_count = 0;
 23148 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
 23149 #else
 23150 #define OpenCounter(X)
 23151 #endif
 23153 #endif /* !defined(_OS_COMMON_H_) */
 23155 /************** End of os_common.h *******************************************/
 23156 /************** Continuing where we left off in os_unix.c ********************/
 23158 /*
 23159 ** Define various macros that are missing from some systems.
 23160 */
 23161 #ifndef O_LARGEFILE
 23162 # define O_LARGEFILE 0
 23163 #endif
 23164 #ifdef SQLITE_DISABLE_LFS
 23165 # undef O_LARGEFILE
 23166 # define O_LARGEFILE 0
 23167 #endif
 23168 #ifndef O_NOFOLLOW
 23169 # define O_NOFOLLOW 0
 23170 #endif
 23171 #ifndef O_BINARY
 23172 # define O_BINARY 0
 23173 #endif
 23175 /*
 23176 ** The threadid macro resolves to the thread-id or to 0.  Used for
 23177 ** testing and debugging only.
 23178 */
 23179 #if SQLITE_THREADSAFE
 23180 #define threadid pthread_self()
 23181 #else
 23182 #define threadid 0
 23183 #endif
 23185 /*
 23186 ** Different Unix systems declare open() in different ways.  Same use
 23187 ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
 23188 ** The difference is important when using a pointer to the function.
 23189 **
 23190 ** The safest way to deal with the problem is to always use this wrapper
 23191 ** which always has the same well-defined interface.
 23192 */
 23193 static int posixOpen(const char *zFile, int flags, int mode){
 23194   return open(zFile, flags, mode);
 23197 /*
 23198 ** On some systems, calls to fchown() will trigger a message in a security
 23199 ** log if they come from non-root processes.  So avoid calling fchown() if
 23200 ** we are not running as root.
 23201 */
 23202 static int posixFchown(int fd, uid_t uid, gid_t gid){
 23203   return geteuid() ? 0 : fchown(fd,uid,gid);
 23206 /* Forward reference */
 23207 static int openDirectory(const char*, int*);
 23209 /*
 23210 ** Many system calls are accessed through pointer-to-functions so that
 23211 ** they may be overridden at runtime to facilitate fault injection during
 23212 ** testing and sandboxing.  The following array holds the names and pointers
 23213 ** to all overrideable system calls.
 23214 */
 23215 static struct unix_syscall {
 23216   const char *zName;            /* Name of the sytem call */
 23217   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
 23218   sqlite3_syscall_ptr pDefault; /* Default value */
 23219 } aSyscall[] = {
 23220   { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
 23221 #define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
 23223   { "close",        (sqlite3_syscall_ptr)close,      0  },
 23224 #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
 23226   { "access",       (sqlite3_syscall_ptr)access,     0  },
 23227 #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
 23229   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
 23230 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
 23232   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
 23233 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
 23235 /*
 23236 ** The DJGPP compiler environment looks mostly like Unix, but it
 23237 ** lacks the fcntl() system call.  So redefine fcntl() to be something
 23238 ** that always succeeds.  This means that locking does not occur under
 23239 ** DJGPP.  But it is DOS - what did you expect?
 23240 */
 23241 #ifdef __DJGPP__
 23242   { "fstat",        0,                 0  },
 23243 #define osFstat(a,b,c)    0
 23244 #else     
 23245   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
 23246 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
 23247 #endif
 23249   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
 23250 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
 23252   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
 23253 #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
 23255   { "read",         (sqlite3_syscall_ptr)read,       0  },
 23256 #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
 23258 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
 23259   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
 23260 #else
 23261   { "pread",        (sqlite3_syscall_ptr)0,          0  },
 23262 #endif
 23263 #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
 23265 #if defined(USE_PREAD64)
 23266   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
 23267 #else
 23268   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
 23269 #endif
 23270 #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
 23272   { "write",        (sqlite3_syscall_ptr)write,      0  },
 23273 #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
 23275 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
 23276   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
 23277 #else
 23278   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
 23279 #endif
 23280 #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
 23281                     aSyscall[12].pCurrent)
 23283 #if defined(USE_PREAD64)
 23284   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
 23285 #else
 23286   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
 23287 #endif
 23288 #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
 23289                     aSyscall[13].pCurrent)
 23291 #if SQLITE_ENABLE_LOCKING_STYLE
 23292   { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
 23293 #else
 23294   { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
 23295 #endif
 23296 #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
 23298 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 23299   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
 23300 #else
 23301   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
 23302 #endif
 23303 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
 23305   { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
 23306 #define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
 23308   { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
 23309 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
 23311   { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
 23312 #define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
 23314   { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
 23315 #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
 23317   { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
 23318 #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 23320   { "umask",        (sqlite3_syscall_ptr)umask,           0 },
 23321 #define osUmask     ((mode_t(*)(mode_t))aSyscall[21].pCurrent)
 23323 }; /* End of the overrideable system calls */
 23325 /*
 23326 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 23327 ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
 23328 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
 23329 ** system call named zName.
 23330 */
 23331 static int unixSetSystemCall(
 23332   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
 23333   const char *zName,            /* Name of system call to override */
 23334   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
 23335 ){
 23336   unsigned int i;
 23337   int rc = SQLITE_NOTFOUND;
 23339   UNUSED_PARAMETER(pNotUsed);
 23340   if( zName==0 ){
 23341     /* If no zName is given, restore all system calls to their default
 23342     ** settings and return NULL
 23343     */
 23344     rc = SQLITE_OK;
 23345     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 23346       if( aSyscall[i].pDefault ){
 23347         aSyscall[i].pCurrent = aSyscall[i].pDefault;
 23350   }else{
 23351     /* If zName is specified, operate on only the one system call
 23352     ** specified.
 23353     */
 23354     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 23355       if( strcmp(zName, aSyscall[i].zName)==0 ){
 23356         if( aSyscall[i].pDefault==0 ){
 23357           aSyscall[i].pDefault = aSyscall[i].pCurrent;
 23359         rc = SQLITE_OK;
 23360         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
 23361         aSyscall[i].pCurrent = pNewFunc;
 23362         break;
 23366   return rc;
 23369 /*
 23370 ** Return the value of a system call.  Return NULL if zName is not a
 23371 ** recognized system call name.  NULL is also returned if the system call
 23372 ** is currently undefined.
 23373 */
 23374 static sqlite3_syscall_ptr unixGetSystemCall(
 23375   sqlite3_vfs *pNotUsed,
 23376   const char *zName
 23377 ){
 23378   unsigned int i;
 23380   UNUSED_PARAMETER(pNotUsed);
 23381   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 23382     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
 23384   return 0;
 23387 /*
 23388 ** Return the name of the first system call after zName.  If zName==NULL
 23389 ** then return the name of the first system call.  Return NULL if zName
 23390 ** is the last system call or if zName is not the name of a valid
 23391 ** system call.
 23392 */
 23393 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
 23394   int i = -1;
 23396   UNUSED_PARAMETER(p);
 23397   if( zName ){
 23398     for(i=0; i<ArraySize(aSyscall)-1; i++){
 23399       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
 23402   for(i++; i<ArraySize(aSyscall); i++){
 23403     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
 23405   return 0;
 23408 /*
 23409 ** Invoke open().  Do so multiple times, until it either succeeds or
 23410 ** fails for some reason other than EINTR.
 23411 **
 23412 ** If the file creation mode "m" is 0 then set it to the default for
 23413 ** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
 23414 ** 0644) as modified by the system umask.  If m is not 0, then
 23415 ** make the file creation mode be exactly m ignoring the umask.
 23416 **
 23417 ** The m parameter will be non-zero only when creating -wal, -journal,
 23418 ** and -shm files.  We want those files to have *exactly* the same
 23419 ** permissions as their original database, unadulterated by the umask.
 23420 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
 23421 ** transaction crashes and leaves behind hot journals, then any
 23422 ** process that is able to write to the database will also be able to
 23423 ** recover the hot journals.
 23424 */
 23425 static int robust_open(const char *z, int f, mode_t m){
 23426   int fd;
 23427   mode_t m2;
 23428   mode_t origM = 0;
 23429   if( m==0 ){
 23430     m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
 23431   }else{
 23432     m2 = m;
 23433     origM = osUmask(0);
 23435   do{
 23436 #if defined(O_CLOEXEC)
 23437     fd = osOpen(z,f|O_CLOEXEC,m2);
 23438 #else
 23439     fd = osOpen(z,f,m2);
 23440 #endif
 23441   }while( fd<0 && errno==EINTR );
 23442   if( m ){
 23443     osUmask(origM);
 23445 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
 23446   if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 23447 #endif
 23448   return fd;
 23451 /*
 23452 ** Helper functions to obtain and relinquish the global mutex. The
 23453 ** global mutex is used to protect the unixInodeInfo and
 23454 ** vxworksFileId objects used by this file, all of which may be 
 23455 ** shared by multiple threads.
 23456 **
 23457 ** Function unixMutexHeld() is used to assert() that the global mutex 
 23458 ** is held when required. This function is only used as part of assert() 
 23459 ** statements. e.g.
 23460 **
 23461 **   unixEnterMutex()
 23462 **     assert( unixMutexHeld() );
 23463 **   unixEnterLeave()
 23464 */
 23465 static void unixEnterMutex(void){
 23466   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 23468 static void unixLeaveMutex(void){
 23469   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 23471 #ifdef SQLITE_DEBUG
 23472 static int unixMutexHeld(void) {
 23473   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 23475 #endif
 23478 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 23479 /*
 23480 ** Helper function for printing out trace information from debugging
 23481 ** binaries. This returns the string represetation of the supplied
 23482 ** integer lock-type.
 23483 */
 23484 static const char *azFileLock(int eFileLock){
 23485   switch( eFileLock ){
 23486     case NO_LOCK: return "NONE";
 23487     case SHARED_LOCK: return "SHARED";
 23488     case RESERVED_LOCK: return "RESERVED";
 23489     case PENDING_LOCK: return "PENDING";
 23490     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
 23492   return "ERROR";
 23494 #endif
 23496 #ifdef SQLITE_LOCK_TRACE
 23497 /*
 23498 ** Print out information about all locking operations.
 23499 **
 23500 ** This routine is used for troubleshooting locks on multithreaded
 23501 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
 23502 ** command-line option on the compiler.  This code is normally
 23503 ** turned off.
 23504 */
 23505 static int lockTrace(int fd, int op, struct flock *p){
 23506   char *zOpName, *zType;
 23507   int s;
 23508   int savedErrno;
 23509   if( op==F_GETLK ){
 23510     zOpName = "GETLK";
 23511   }else if( op==F_SETLK ){
 23512     zOpName = "SETLK";
 23513   }else{
 23514     s = osFcntl(fd, op, p);
 23515     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
 23516     return s;
 23518   if( p->l_type==F_RDLCK ){
 23519     zType = "RDLCK";
 23520   }else if( p->l_type==F_WRLCK ){
 23521     zType = "WRLCK";
 23522   }else if( p->l_type==F_UNLCK ){
 23523     zType = "UNLCK";
 23524   }else{
 23525     assert( 0 );
 23527   assert( p->l_whence==SEEK_SET );
 23528   s = osFcntl(fd, op, p);
 23529   savedErrno = errno;
 23530   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
 23531      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
 23532      (int)p->l_pid, s);
 23533   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
 23534     struct flock l2;
 23535     l2 = *p;
 23536     osFcntl(fd, F_GETLK, &l2);
 23537     if( l2.l_type==F_RDLCK ){
 23538       zType = "RDLCK";
 23539     }else if( l2.l_type==F_WRLCK ){
 23540       zType = "WRLCK";
 23541     }else if( l2.l_type==F_UNLCK ){
 23542       zType = "UNLCK";
 23543     }else{
 23544       assert( 0 );
 23546     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
 23547        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
 23549   errno = savedErrno;
 23550   return s;
 23552 #undef osFcntl
 23553 #define osFcntl lockTrace
 23554 #endif /* SQLITE_LOCK_TRACE */
 23556 /*
 23557 ** Retry ftruncate() calls that fail due to EINTR
 23558 */
 23559 static int robust_ftruncate(int h, sqlite3_int64 sz){
 23560   int rc;
 23561   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
 23562   return rc;
 23565 /*
 23566 ** This routine translates a standard POSIX errno code into something
 23567 ** useful to the clients of the sqlite3 functions.  Specifically, it is
 23568 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
 23569 ** and a variety of "please close the file descriptor NOW" errors into 
 23570 ** SQLITE_IOERR
 23571 ** 
 23572 ** Errors during initialization of locks, or file system support for locks,
 23573 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
 23574 */
 23575 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
 23576   switch (posixError) {
 23577 #if 0
 23578   /* At one point this code was not commented out. In theory, this branch
 23579   ** should never be hit, as this function should only be called after
 23580   ** a locking-related function (i.e. fcntl()) has returned non-zero with
 23581   ** the value of errno as the first argument. Since a system call has failed,
 23582   ** errno should be non-zero.
 23583   **
 23584   ** Despite this, if errno really is zero, we still don't want to return
 23585   ** SQLITE_OK. The system call failed, and *some* SQLite error should be
 23586   ** propagated back to the caller. Commenting this branch out means errno==0
 23587   ** will be handled by the "default:" case below.
 23588   */
 23589   case 0: 
 23590     return SQLITE_OK;
 23591 #endif
 23593   case EAGAIN:
 23594   case ETIMEDOUT:
 23595   case EBUSY:
 23596   case EINTR:
 23597   case ENOLCK:  
 23598     /* random NFS retry error, unless during file system support 
 23599      * introspection, in which it actually means what it says */
 23600     return SQLITE_BUSY;
 23602   case EACCES: 
 23603     /* EACCES is like EAGAIN during locking operations, but not any other time*/
 23604     if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
 23605         (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
 23606         (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
 23607         (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
 23608       return SQLITE_BUSY;
 23610     /* else fall through */
 23611   case EPERM: 
 23612     return SQLITE_PERM;
 23614   /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
 23615   ** this module never makes such a call. And the code in SQLite itself 
 23616   ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
 23617   ** this case is also commented out. If the system does set errno to EDEADLK,
 23618   ** the default SQLITE_IOERR_XXX code will be returned. */
 23619 #if 0
 23620   case EDEADLK:
 23621     return SQLITE_IOERR_BLOCKED;
 23622 #endif
 23624 #if EOPNOTSUPP!=ENOTSUP
 23625   case EOPNOTSUPP: 
 23626     /* something went terribly awry, unless during file system support 
 23627      * introspection, in which it actually means what it says */
 23628 #endif
 23629 #ifdef ENOTSUP
 23630   case ENOTSUP: 
 23631     /* invalid fd, unless during file system support introspection, in which 
 23632      * it actually means what it says */
 23633 #endif
 23634   case EIO:
 23635   case EBADF:
 23636   case EINVAL:
 23637   case ENOTCONN:
 23638   case ENODEV:
 23639   case ENXIO:
 23640   case ENOENT:
 23641 #ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
 23642   case ESTALE:
 23643 #endif
 23644   case ENOSYS:
 23645     /* these should force the client to close the file and reconnect */
 23647   default: 
 23648     return sqliteIOErr;
 23654 /******************************************************************************
 23655 ****************** Begin Unique File ID Utility Used By VxWorks ***************
 23656 **
 23657 ** On most versions of unix, we can get a unique ID for a file by concatenating
 23658 ** the device number and the inode number.  But this does not work on VxWorks.
 23659 ** On VxWorks, a unique file id must be based on the canonical filename.
 23660 **
 23661 ** A pointer to an instance of the following structure can be used as a
 23662 ** unique file ID in VxWorks.  Each instance of this structure contains
 23663 ** a copy of the canonical filename.  There is also a reference count.  
 23664 ** The structure is reclaimed when the number of pointers to it drops to
 23665 ** zero.
 23666 **
 23667 ** There are never very many files open at one time and lookups are not
 23668 ** a performance-critical path, so it is sufficient to put these
 23669 ** structures on a linked list.
 23670 */
 23671 struct vxworksFileId {
 23672   struct vxworksFileId *pNext;  /* Next in a list of them all */
 23673   int nRef;                     /* Number of references to this one */
 23674   int nName;                    /* Length of the zCanonicalName[] string */
 23675   char *zCanonicalName;         /* Canonical filename */
 23676 };
 23678 #if OS_VXWORKS
 23679 /* 
 23680 ** All unique filenames are held on a linked list headed by this
 23681 ** variable:
 23682 */
 23683 static struct vxworksFileId *vxworksFileList = 0;
 23685 /*
 23686 ** Simplify a filename into its canonical form
 23687 ** by making the following changes:
 23688 **
 23689 **  * removing any trailing and duplicate /
 23690 **  * convert /./ into just /
 23691 **  * convert /A/../ where A is any simple name into just /
 23692 **
 23693 ** Changes are made in-place.  Return the new name length.
 23694 **
 23695 ** The original filename is in z[0..n-1].  Return the number of
 23696 ** characters in the simplified name.
 23697 */
 23698 static int vxworksSimplifyName(char *z, int n){
 23699   int i, j;
 23700   while( n>1 && z[n-1]=='/' ){ n--; }
 23701   for(i=j=0; i<n; i++){
 23702     if( z[i]=='/' ){
 23703       if( z[i+1]=='/' ) continue;
 23704       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
 23705         i += 1;
 23706         continue;
 23708       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
 23709         while( j>0 && z[j-1]!='/' ){ j--; }
 23710         if( j>0 ){ j--; }
 23711         i += 2;
 23712         continue;
 23715     z[j++] = z[i];
 23717   z[j] = 0;
 23718   return j;
 23721 /*
 23722 ** Find a unique file ID for the given absolute pathname.  Return
 23723 ** a pointer to the vxworksFileId object.  This pointer is the unique
 23724 ** file ID.
 23725 **
 23726 ** The nRef field of the vxworksFileId object is incremented before
 23727 ** the object is returned.  A new vxworksFileId object is created
 23728 ** and added to the global list if necessary.
 23729 **
 23730 ** If a memory allocation error occurs, return NULL.
 23731 */
 23732 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
 23733   struct vxworksFileId *pNew;         /* search key and new file ID */
 23734   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
 23735   int n;                              /* Length of zAbsoluteName string */
 23737   assert( zAbsoluteName[0]=='/' );
 23738   n = (int)strlen(zAbsoluteName);
 23739   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
 23740   if( pNew==0 ) return 0;
 23741   pNew->zCanonicalName = (char*)&pNew[1];
 23742   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
 23743   n = vxworksSimplifyName(pNew->zCanonicalName, n);
 23745   /* Search for an existing entry that matching the canonical name.
 23746   ** If found, increment the reference count and return a pointer to
 23747   ** the existing file ID.
 23748   */
 23749   unixEnterMutex();
 23750   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
 23751     if( pCandidate->nName==n 
 23752      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
 23753     ){
 23754        sqlite3_free(pNew);
 23755        pCandidate->nRef++;
 23756        unixLeaveMutex();
 23757        return pCandidate;
 23761   /* No match was found.  We will make a new file ID */
 23762   pNew->nRef = 1;
 23763   pNew->nName = n;
 23764   pNew->pNext = vxworksFileList;
 23765   vxworksFileList = pNew;
 23766   unixLeaveMutex();
 23767   return pNew;
 23770 /*
 23771 ** Decrement the reference count on a vxworksFileId object.  Free
 23772 ** the object when the reference count reaches zero.
 23773 */
 23774 static void vxworksReleaseFileId(struct vxworksFileId *pId){
 23775   unixEnterMutex();
 23776   assert( pId->nRef>0 );
 23777   pId->nRef--;
 23778   if( pId->nRef==0 ){
 23779     struct vxworksFileId **pp;
 23780     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
 23781     assert( *pp==pId );
 23782     *pp = pId->pNext;
 23783     sqlite3_free(pId);
 23785   unixLeaveMutex();
 23787 #endif /* OS_VXWORKS */
 23788 /*************** End of Unique File ID Utility Used By VxWorks ****************
 23789 ******************************************************************************/
 23792 /******************************************************************************
 23793 *************************** Posix Advisory Locking ****************************
 23794 **
 23795 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
 23796 ** section 6.5.2.2 lines 483 through 490 specify that when a process
 23797 ** sets or clears a lock, that operation overrides any prior locks set
 23798 ** by the same process.  It does not explicitly say so, but this implies
 23799 ** that it overrides locks set by the same process using a different
 23800 ** file descriptor.  Consider this test case:
 23801 **
 23802 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
 23803 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
 23804 **
 23805 ** Suppose ./file1 and ./file2 are really the same file (because
 23806 ** one is a hard or symbolic link to the other) then if you set
 23807 ** an exclusive lock on fd1, then try to get an exclusive lock
 23808 ** on fd2, it works.  I would have expected the second lock to
 23809 ** fail since there was already a lock on the file due to fd1.
 23810 ** But not so.  Since both locks came from the same process, the
 23811 ** second overrides the first, even though they were on different
 23812 ** file descriptors opened on different file names.
 23813 **
 23814 ** This means that we cannot use POSIX locks to synchronize file access
 23815 ** among competing threads of the same process.  POSIX locks will work fine
 23816 ** to synchronize access for threads in separate processes, but not
 23817 ** threads within the same process.
 23818 **
 23819 ** To work around the problem, SQLite has to manage file locks internally
 23820 ** on its own.  Whenever a new database is opened, we have to find the
 23821 ** specific inode of the database file (the inode is determined by the
 23822 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
 23823 ** and check for locks already existing on that inode.  When locks are
 23824 ** created or removed, we have to look at our own internal record of the
 23825 ** locks to see if another thread has previously set a lock on that same
 23826 ** inode.
 23827 **
 23828 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
 23829 ** For VxWorks, we have to use the alternative unique ID system based on
 23830 ** canonical filename and implemented in the previous division.)
 23831 **
 23832 ** The sqlite3_file structure for POSIX is no longer just an integer file
 23833 ** descriptor.  It is now a structure that holds the integer file
 23834 ** descriptor and a pointer to a structure that describes the internal
 23835 ** locks on the corresponding inode.  There is one locking structure
 23836 ** per inode, so if the same inode is opened twice, both unixFile structures
 23837 ** point to the same locking structure.  The locking structure keeps
 23838 ** a reference count (so we will know when to delete it) and a "cnt"
 23839 ** field that tells us its internal lock status.  cnt==0 means the
 23840 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
 23841 ** cnt>0 means there are cnt shared locks on the file.
 23842 **
 23843 ** Any attempt to lock or unlock a file first checks the locking
 23844 ** structure.  The fcntl() system call is only invoked to set a 
 23845 ** POSIX lock if the internal lock structure transitions between
 23846 ** a locked and an unlocked state.
 23847 **
 23848 ** But wait:  there are yet more problems with POSIX advisory locks.
 23849 **
 23850 ** If you close a file descriptor that points to a file that has locks,
 23851 ** all locks on that file that are owned by the current process are
 23852 ** released.  To work around this problem, each unixInodeInfo object
 23853 ** maintains a count of the number of pending locks on tha inode.
 23854 ** When an attempt is made to close an unixFile, if there are
 23855 ** other unixFile open on the same inode that are holding locks, the call
 23856 ** to close() the file descriptor is deferred until all of the locks clear.
 23857 ** The unixInodeInfo structure keeps a list of file descriptors that need to
 23858 ** be closed and that list is walked (and cleared) when the last lock
 23859 ** clears.
 23860 **
 23861 ** Yet another problem:  LinuxThreads do not play well with posix locks.
 23862 **
 23863 ** Many older versions of linux use the LinuxThreads library which is
 23864 ** not posix compliant.  Under LinuxThreads, a lock created by thread
 23865 ** A cannot be modified or overridden by a different thread B.
 23866 ** Only thread A can modify the lock.  Locking behavior is correct
 23867 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
 23868 ** on linux - with NPTL a lock created by thread A can override locks
 23869 ** in thread B.  But there is no way to know at compile-time which
 23870 ** threading library is being used.  So there is no way to know at
 23871 ** compile-time whether or not thread A can override locks on thread B.
 23872 ** One has to do a run-time check to discover the behavior of the
 23873 ** current process.
 23874 **
 23875 ** SQLite used to support LinuxThreads.  But support for LinuxThreads
 23876 ** was dropped beginning with version 3.7.0.  SQLite will still work with
 23877 ** LinuxThreads provided that (1) there is no more than one connection 
 23878 ** per database file in the same process and (2) database connections
 23879 ** do not move across threads.
 23880 */
 23882 /*
 23883 ** An instance of the following structure serves as the key used
 23884 ** to locate a particular unixInodeInfo object.
 23885 */
 23886 struct unixFileId {
 23887   dev_t dev;                  /* Device number */
 23888 #if OS_VXWORKS
 23889   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
 23890 #else
 23891   ino_t ino;                  /* Inode number */
 23892 #endif
 23893 };
 23895 /*
 23896 ** An instance of the following structure is allocated for each open
 23897 ** inode.  Or, on LinuxThreads, there is one of these structures for
 23898 ** each inode opened by each thread.
 23899 **
 23900 ** A single inode can have multiple file descriptors, so each unixFile
 23901 ** structure contains a pointer to an instance of this object and this
 23902 ** object keeps a count of the number of unixFile pointing to it.
 23903 */
 23904 struct unixInodeInfo {
 23905   struct unixFileId fileId;       /* The lookup key */
 23906   int nShared;                    /* Number of SHARED locks held */
 23907   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
 23908   unsigned char bProcessLock;     /* An exclusive process lock is held */
 23909   int nRef;                       /* Number of pointers to this structure */
 23910   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
 23911   int nLock;                      /* Number of outstanding file locks */
 23912   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
 23913   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
 23914   unixInodeInfo *pPrev;           /*    .... doubly linked */
 23915 #if SQLITE_ENABLE_LOCKING_STYLE
 23916   unsigned long long sharedByte;  /* for AFP simulated shared lock */
 23917 #endif
 23918 #if OS_VXWORKS
 23919   sem_t *pSem;                    /* Named POSIX semaphore */
 23920   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
 23921 #endif
 23922 };
 23924 /*
 23925 ** A lists of all unixInodeInfo objects.
 23926 */
 23927 static unixInodeInfo *inodeList = 0;
 23929 /*
 23930 **
 23931 ** This function - unixLogError_x(), is only ever called via the macro
 23932 ** unixLogError().
 23933 **
 23934 ** It is invoked after an error occurs in an OS function and errno has been
 23935 ** set. It logs a message using sqlite3_log() containing the current value of
 23936 ** errno and, if possible, the human-readable equivalent from strerror() or
 23937 ** strerror_r().
 23938 **
 23939 ** The first argument passed to the macro should be the error code that
 23940 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
 23941 ** The two subsequent arguments should be the name of the OS function that
 23942 ** failed (e.g. "unlink", "open") and the associated file-system path,
 23943 ** if any.
 23944 */
 23945 #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
 23946 static int unixLogErrorAtLine(
 23947   int errcode,                    /* SQLite error code */
 23948   const char *zFunc,              /* Name of OS function that failed */
 23949   const char *zPath,              /* File path associated with error */
 23950   int iLine                       /* Source line number where error occurred */
 23951 ){
 23952   char *zErr;                     /* Message from strerror() or equivalent */
 23953   int iErrno = errno;             /* Saved syscall error number */
 23955   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
 23956   ** the strerror() function to obtain the human-readable error message
 23957   ** equivalent to errno. Otherwise, use strerror_r().
 23958   */ 
 23959 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
 23960   char aErr[80];
 23961   memset(aErr, 0, sizeof(aErr));
 23962   zErr = aErr;
 23964   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
 23965   ** assume that the system provides the GNU version of strerror_r() that
 23966   ** returns a pointer to a buffer containing the error message. That pointer 
 23967   ** may point to aErr[], or it may point to some static storage somewhere. 
 23968   ** Otherwise, assume that the system provides the POSIX version of 
 23969   ** strerror_r(), which always writes an error message into aErr[].
 23970   **
 23971   ** If the code incorrectly assumes that it is the POSIX version that is
 23972   ** available, the error message will often be an empty string. Not a
 23973   ** huge problem. Incorrectly concluding that the GNU version is available 
 23974   ** could lead to a segfault though.
 23975   */
 23976 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
 23977   zErr = 
 23978 # endif
 23979   strerror_r(iErrno, aErr, sizeof(aErr)-1);
 23981 #elif SQLITE_THREADSAFE
 23982   /* This is a threadsafe build, but strerror_r() is not available. */
 23983   zErr = "";
 23984 #else
 23985   /* Non-threadsafe build, use strerror(). */
 23986   zErr = strerror(iErrno);
 23987 #endif
 23989   assert( errcode!=SQLITE_OK );
 23990   if( zPath==0 ) zPath = "";
 23991   sqlite3_log(errcode,
 23992       "os_unix.c:%d: (%d) %s(%s) - %s",
 23993       iLine, iErrno, zFunc, zPath, zErr
 23994   );
 23996   return errcode;
 23999 /*
 24000 ** Close a file descriptor.
 24001 **
 24002 ** We assume that close() almost always works, since it is only in a
 24003 ** very sick application or on a very sick platform that it might fail.
 24004 ** If it does fail, simply leak the file descriptor, but do log the
 24005 ** error.
 24006 **
 24007 ** Note that it is not safe to retry close() after EINTR since the
 24008 ** file descriptor might have already been reused by another thread.
 24009 ** So we don't even try to recover from an EINTR.  Just log the error
 24010 ** and move on.
 24011 */
 24012 static void robust_close(unixFile *pFile, int h, int lineno){
 24013   if( osClose(h) ){
 24014     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
 24015                        pFile ? pFile->zPath : 0, lineno);
 24019 /*
 24020 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
 24021 */ 
 24022 static void closePendingFds(unixFile *pFile){
 24023   unixInodeInfo *pInode = pFile->pInode;
 24024   UnixUnusedFd *p;
 24025   UnixUnusedFd *pNext;
 24026   for(p=pInode->pUnused; p; p=pNext){
 24027     pNext = p->pNext;
 24028     robust_close(pFile, p->fd, __LINE__);
 24029     sqlite3_free(p);
 24031   pInode->pUnused = 0;
 24034 /*
 24035 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 24036 **
 24037 ** The mutex entered using the unixEnterMutex() function must be held
 24038 ** when this function is called.
 24039 */
 24040 static void releaseInodeInfo(unixFile *pFile){
 24041   unixInodeInfo *pInode = pFile->pInode;
 24042   assert( unixMutexHeld() );
 24043   if( ALWAYS(pInode) ){
 24044     pInode->nRef--;
 24045     if( pInode->nRef==0 ){
 24046       assert( pInode->pShmNode==0 );
 24047       closePendingFds(pFile);
 24048       if( pInode->pPrev ){
 24049         assert( pInode->pPrev->pNext==pInode );
 24050         pInode->pPrev->pNext = pInode->pNext;
 24051       }else{
 24052         assert( inodeList==pInode );
 24053         inodeList = pInode->pNext;
 24055       if( pInode->pNext ){
 24056         assert( pInode->pNext->pPrev==pInode );
 24057         pInode->pNext->pPrev = pInode->pPrev;
 24059       sqlite3_free(pInode);
 24064 /*
 24065 ** Given a file descriptor, locate the unixInodeInfo object that
 24066 ** describes that file descriptor.  Create a new one if necessary.  The
 24067 ** return value might be uninitialized if an error occurs.
 24068 **
 24069 ** The mutex entered using the unixEnterMutex() function must be held
 24070 ** when this function is called.
 24071 **
 24072 ** Return an appropriate error code.
 24073 */
 24074 static int findInodeInfo(
 24075   unixFile *pFile,               /* Unix file with file desc used in the key */
 24076   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
 24077 ){
 24078   int rc;                        /* System call return code */
 24079   int fd;                        /* The file descriptor for pFile */
 24080   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
 24081   struct stat statbuf;           /* Low-level file information */
 24082   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
 24084   assert( unixMutexHeld() );
 24086   /* Get low-level information about the file that we can used to
 24087   ** create a unique name for the file.
 24088   */
 24089   fd = pFile->h;
 24090   rc = osFstat(fd, &statbuf);
 24091   if( rc!=0 ){
 24092     pFile->lastErrno = errno;
 24093 #ifdef EOVERFLOW
 24094     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
 24095 #endif
 24096     return SQLITE_IOERR;
 24099 #ifdef __APPLE__
 24100   /* On OS X on an msdos filesystem, the inode number is reported
 24101   ** incorrectly for zero-size files.  See ticket #3260.  To work
 24102   ** around this problem (we consider it a bug in OS X, not SQLite)
 24103   ** we always increase the file size to 1 by writing a single byte
 24104   ** prior to accessing the inode number.  The one byte written is
 24105   ** an ASCII 'S' character which also happens to be the first byte
 24106   ** in the header of every SQLite database.  In this way, if there
 24107   ** is a race condition such that another thread has already populated
 24108   ** the first page of the database, no damage is done.
 24109   */
 24110   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
 24111     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
 24112     if( rc!=1 ){
 24113       pFile->lastErrno = errno;
 24114       return SQLITE_IOERR;
 24116     rc = osFstat(fd, &statbuf);
 24117     if( rc!=0 ){
 24118       pFile->lastErrno = errno;
 24119       return SQLITE_IOERR;
 24122 #endif
 24124   memset(&fileId, 0, sizeof(fileId));
 24125   fileId.dev = statbuf.st_dev;
 24126 #if OS_VXWORKS
 24127   fileId.pId = pFile->pId;
 24128 #else
 24129   fileId.ino = statbuf.st_ino;
 24130 #endif
 24131   pInode = inodeList;
 24132   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 24133     pInode = pInode->pNext;
 24135   if( pInode==0 ){
 24136     pInode = sqlite3_malloc( sizeof(*pInode) );
 24137     if( pInode==0 ){
 24138       return SQLITE_NOMEM;
 24140     memset(pInode, 0, sizeof(*pInode));
 24141     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
 24142     pInode->nRef = 1;
 24143     pInode->pNext = inodeList;
 24144     pInode->pPrev = 0;
 24145     if( inodeList ) inodeList->pPrev = pInode;
 24146     inodeList = pInode;
 24147   }else{
 24148     pInode->nRef++;
 24150   *ppInode = pInode;
 24151   return SQLITE_OK;
 24155 /*
 24156 ** This routine checks if there is a RESERVED lock held on the specified
 24157 ** file by this or any other process. If such a lock is held, set *pResOut
 24158 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 24159 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 24160 */
 24161 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
 24162   int rc = SQLITE_OK;
 24163   int reserved = 0;
 24164   unixFile *pFile = (unixFile*)id;
 24166   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 24168   assert( pFile );
 24169   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
 24171   /* Check if a thread in this process holds such a lock */
 24172   if( pFile->pInode->eFileLock>SHARED_LOCK ){
 24173     reserved = 1;
 24176   /* Otherwise see if some other process holds it.
 24177   */
 24178 #ifndef __DJGPP__
 24179   if( !reserved && !pFile->pInode->bProcessLock ){
 24180     struct flock lock;
 24181     lock.l_whence = SEEK_SET;
 24182     lock.l_start = RESERVED_BYTE;
 24183     lock.l_len = 1;
 24184     lock.l_type = F_WRLCK;
 24185     if( osFcntl(pFile->h, F_GETLK, &lock) ){
 24186       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
 24187       pFile->lastErrno = errno;
 24188     } else if( lock.l_type!=F_UNLCK ){
 24189       reserved = 1;
 24192 #endif
 24194   unixLeaveMutex();
 24195   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 24197   *pResOut = reserved;
 24198   return rc;
 24201 /*
 24202 ** Attempt to set a system-lock on the file pFile.  The lock is 
 24203 ** described by pLock.
 24204 **
 24205 ** If the pFile was opened read/write from unix-excl, then the only lock
 24206 ** ever obtained is an exclusive lock, and it is obtained exactly once
 24207 ** the first time any lock is attempted.  All subsequent system locking
 24208 ** operations become no-ops.  Locking operations still happen internally,
 24209 ** in order to coordinate access between separate database connections
 24210 ** within this process, but all of that is handled in memory and the
 24211 ** operating system does not participate.
 24212 **
 24213 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
 24214 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
 24215 ** and is read-only.
 24216 **
 24217 ** Zero is returned if the call completes successfully, or -1 if a call
 24218 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
 24219 */
 24220 static int unixFileLock(unixFile *pFile, struct flock *pLock){
 24221   int rc;
 24222   unixInodeInfo *pInode = pFile->pInode;
 24223   assert( unixMutexHeld() );
 24224   assert( pInode!=0 );
 24225   if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
 24226    && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
 24227   ){
 24228     if( pInode->bProcessLock==0 ){
 24229       struct flock lock;
 24230       assert( pInode->nLock==0 );
 24231       lock.l_whence = SEEK_SET;
 24232       lock.l_start = SHARED_FIRST;
 24233       lock.l_len = SHARED_SIZE;
 24234       lock.l_type = F_WRLCK;
 24235       rc = osFcntl(pFile->h, F_SETLK, &lock);
 24236       if( rc<0 ) return rc;
 24237       pInode->bProcessLock = 1;
 24238       pInode->nLock++;
 24239     }else{
 24240       rc = 0;
 24242   }else{
 24243     rc = osFcntl(pFile->h, F_SETLK, pLock);
 24245   return rc;
 24248 /*
 24249 ** Lock the file with the lock specified by parameter eFileLock - one
 24250 ** of the following:
 24251 **
 24252 **     (1) SHARED_LOCK
 24253 **     (2) RESERVED_LOCK
 24254 **     (3) PENDING_LOCK
 24255 **     (4) EXCLUSIVE_LOCK
 24256 **
 24257 ** Sometimes when requesting one lock state, additional lock states
 24258 ** are inserted in between.  The locking might fail on one of the later
 24259 ** transitions leaving the lock state different from what it started but
 24260 ** still short of its goal.  The following chart shows the allowed
 24261 ** transitions and the inserted intermediate states:
 24262 **
 24263 **    UNLOCKED -> SHARED
 24264 **    SHARED -> RESERVED
 24265 **    SHARED -> (PENDING) -> EXCLUSIVE
 24266 **    RESERVED -> (PENDING) -> EXCLUSIVE
 24267 **    PENDING -> EXCLUSIVE
 24268 **
 24269 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 24270 ** routine to lower a locking level.
 24271 */
 24272 static int unixLock(sqlite3_file *id, int eFileLock){
 24273   /* The following describes the implementation of the various locks and
 24274   ** lock transitions in terms of the POSIX advisory shared and exclusive
 24275   ** lock primitives (called read-locks and write-locks below, to avoid
 24276   ** confusion with SQLite lock names). The algorithms are complicated
 24277   ** slightly in order to be compatible with windows systems simultaneously
 24278   ** accessing the same database file, in case that is ever required.
 24279   **
 24280   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
 24281   ** byte', each single bytes at well known offsets, and the 'shared byte
 24282   ** range', a range of 510 bytes at a well known offset.
 24283   **
 24284   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
 24285   ** byte'.  If this is successful, a random byte from the 'shared byte
 24286   ** range' is read-locked and the lock on the 'pending byte' released.
 24287   **
 24288   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
 24289   ** A RESERVED lock is implemented by grabbing a write-lock on the
 24290   ** 'reserved byte'. 
 24291   **
 24292   ** A process may only obtain a PENDING lock after it has obtained a
 24293   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
 24294   ** on the 'pending byte'. This ensures that no new SHARED locks can be
 24295   ** obtained, but existing SHARED locks are allowed to persist. A process
 24296   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
 24297   ** This property is used by the algorithm for rolling back a journal file
 24298   ** after a crash.
 24299   **
 24300   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
 24301   ** implemented by obtaining a write-lock on the entire 'shared byte
 24302   ** range'. Since all other locks require a read-lock on one of the bytes
 24303   ** within this range, this ensures that no other locks are held on the
 24304   ** database. 
 24305   **
 24306   ** The reason a single byte cannot be used instead of the 'shared byte
 24307   ** range' is that some versions of windows do not support read-locks. By
 24308   ** locking a random byte from a range, concurrent SHARED locks may exist
 24309   ** even if the locking primitive used is always a write-lock.
 24310   */
 24311   int rc = SQLITE_OK;
 24312   unixFile *pFile = (unixFile*)id;
 24313   unixInodeInfo *pInode;
 24314   struct flock lock;
 24315   int tErrno = 0;
 24317   assert( pFile );
 24318   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
 24319       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 24320       azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
 24322   /* If there is already a lock of this type or more restrictive on the
 24323   ** unixFile, do nothing. Don't use the end_lock: exit path, as
 24324   ** unixEnterMutex() hasn't been called yet.
 24325   */
 24326   if( pFile->eFileLock>=eFileLock ){
 24327     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
 24328             azFileLock(eFileLock)));
 24329     return SQLITE_OK;
 24332   /* Make sure the locking sequence is correct.
 24333   **  (1) We never move from unlocked to anything higher than shared lock.
 24334   **  (2) SQLite never explicitly requests a pendig lock.
 24335   **  (3) A shared lock is always held when a reserve lock is requested.
 24336   */
 24337   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
 24338   assert( eFileLock!=PENDING_LOCK );
 24339   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
 24341   /* This mutex is needed because pFile->pInode is shared across threads
 24342   */
 24343   unixEnterMutex();
 24344   pInode = pFile->pInode;
 24346   /* If some thread using this PID has a lock via a different unixFile*
 24347   ** handle that precludes the requested lock, return BUSY.
 24348   */
 24349   if( (pFile->eFileLock!=pInode->eFileLock && 
 24350           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
 24351   ){
 24352     rc = SQLITE_BUSY;
 24353     goto end_lock;
 24356   /* If a SHARED lock is requested, and some thread using this PID already
 24357   ** has a SHARED or RESERVED lock, then increment reference counts and
 24358   ** return SQLITE_OK.
 24359   */
 24360   if( eFileLock==SHARED_LOCK && 
 24361       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
 24362     assert( eFileLock==SHARED_LOCK );
 24363     assert( pFile->eFileLock==0 );
 24364     assert( pInode->nShared>0 );
 24365     pFile->eFileLock = SHARED_LOCK;
 24366     pInode->nShared++;
 24367     pInode->nLock++;
 24368     goto end_lock;
 24372   /* A PENDING lock is needed before acquiring a SHARED lock and before
 24373   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
 24374   ** be released.
 24375   */
 24376   lock.l_len = 1L;
 24377   lock.l_whence = SEEK_SET;
 24378   if( eFileLock==SHARED_LOCK 
 24379       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
 24380   ){
 24381     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
 24382     lock.l_start = PENDING_BYTE;
 24383     if( unixFileLock(pFile, &lock) ){
 24384       tErrno = errno;
 24385       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24386       if( rc!=SQLITE_BUSY ){
 24387         pFile->lastErrno = tErrno;
 24389       goto end_lock;
 24394   /* If control gets to this point, then actually go ahead and make
 24395   ** operating system calls for the specified lock.
 24396   */
 24397   if( eFileLock==SHARED_LOCK ){
 24398     assert( pInode->nShared==0 );
 24399     assert( pInode->eFileLock==0 );
 24400     assert( rc==SQLITE_OK );
 24402     /* Now get the read-lock */
 24403     lock.l_start = SHARED_FIRST;
 24404     lock.l_len = SHARED_SIZE;
 24405     if( unixFileLock(pFile, &lock) ){
 24406       tErrno = errno;
 24407       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24410     /* Drop the temporary PENDING lock */
 24411     lock.l_start = PENDING_BYTE;
 24412     lock.l_len = 1L;
 24413     lock.l_type = F_UNLCK;
 24414     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
 24415       /* This could happen with a network mount */
 24416       tErrno = errno;
 24417       rc = SQLITE_IOERR_UNLOCK; 
 24420     if( rc ){
 24421       if( rc!=SQLITE_BUSY ){
 24422         pFile->lastErrno = tErrno;
 24424       goto end_lock;
 24425     }else{
 24426       pFile->eFileLock = SHARED_LOCK;
 24427       pInode->nLock++;
 24428       pInode->nShared = 1;
 24430   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
 24431     /* We are trying for an exclusive lock but another thread in this
 24432     ** same process is still holding a shared lock. */
 24433     rc = SQLITE_BUSY;
 24434   }else{
 24435     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
 24436     ** assumed that there is a SHARED or greater lock on the file
 24437     ** already.
 24438     */
 24439     assert( 0!=pFile->eFileLock );
 24440     lock.l_type = F_WRLCK;
 24442     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
 24443     if( eFileLock==RESERVED_LOCK ){
 24444       lock.l_start = RESERVED_BYTE;
 24445       lock.l_len = 1L;
 24446     }else{
 24447       lock.l_start = SHARED_FIRST;
 24448       lock.l_len = SHARED_SIZE;
 24451     if( unixFileLock(pFile, &lock) ){
 24452       tErrno = errno;
 24453       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24454       if( rc!=SQLITE_BUSY ){
 24455         pFile->lastErrno = tErrno;
 24461 #ifdef SQLITE_DEBUG
 24462   /* Set up the transaction-counter change checking flags when
 24463   ** transitioning from a SHARED to a RESERVED lock.  The change
 24464   ** from SHARED to RESERVED marks the beginning of a normal
 24465   ** write operation (not a hot journal rollback).
 24466   */
 24467   if( rc==SQLITE_OK
 24468    && pFile->eFileLock<=SHARED_LOCK
 24469    && eFileLock==RESERVED_LOCK
 24470   ){
 24471     pFile->transCntrChng = 0;
 24472     pFile->dbUpdate = 0;
 24473     pFile->inNormalWrite = 1;
 24475 #endif
 24478   if( rc==SQLITE_OK ){
 24479     pFile->eFileLock = eFileLock;
 24480     pInode->eFileLock = eFileLock;
 24481   }else if( eFileLock==EXCLUSIVE_LOCK ){
 24482     pFile->eFileLock = PENDING_LOCK;
 24483     pInode->eFileLock = PENDING_LOCK;
 24486 end_lock:
 24487   unixLeaveMutex();
 24488   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), 
 24489       rc==SQLITE_OK ? "ok" : "failed"));
 24490   return rc;
 24493 /*
 24494 ** Add the file descriptor used by file handle pFile to the corresponding
 24495 ** pUnused list.
 24496 */
 24497 static void setPendingFd(unixFile *pFile){
 24498   unixInodeInfo *pInode = pFile->pInode;
 24499   UnixUnusedFd *p = pFile->pUnused;
 24500   p->pNext = pInode->pUnused;
 24501   pInode->pUnused = p;
 24502   pFile->h = -1;
 24503   pFile->pUnused = 0;
 24506 /*
 24507 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 24508 ** must be either NO_LOCK or SHARED_LOCK.
 24509 **
 24510 ** If the locking level of the file descriptor is already at or below
 24511 ** the requested locking level, this routine is a no-op.
 24512 ** 
 24513 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
 24514 ** the byte range is divided into 2 parts and the first part is unlocked then
 24515 ** set to a read lock, then the other part is simply unlocked.  This works 
 24516 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to 
 24517 ** remove the write lock on a region when a read lock is set.
 24518 */
 24519 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
 24520   unixFile *pFile = (unixFile*)id;
 24521   unixInodeInfo *pInode;
 24522   struct flock lock;
 24523   int rc = SQLITE_OK;
 24525   assert( pFile );
 24526   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
 24527       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 24528       getpid()));
 24530   assert( eFileLock<=SHARED_LOCK );
 24531   if( pFile->eFileLock<=eFileLock ){
 24532     return SQLITE_OK;
 24534   unixEnterMutex();
 24535   pInode = pFile->pInode;
 24536   assert( pInode->nShared!=0 );
 24537   if( pFile->eFileLock>SHARED_LOCK ){
 24538     assert( pInode->eFileLock==pFile->eFileLock );
 24540 #ifdef SQLITE_DEBUG
 24541     /* When reducing a lock such that other processes can start
 24542     ** reading the database file again, make sure that the
 24543     ** transaction counter was updated if any part of the database
 24544     ** file changed.  If the transaction counter is not updated,
 24545     ** other connections to the same file might not realize that
 24546     ** the file has changed and hence might not know to flush their
 24547     ** cache.  The use of a stale cache can lead to database corruption.
 24548     */
 24549     pFile->inNormalWrite = 0;
 24550 #endif
 24552     /* downgrading to a shared lock on NFS involves clearing the write lock
 24553     ** before establishing the readlock - to avoid a race condition we downgrade
 24554     ** the lock in 2 blocks, so that part of the range will be covered by a 
 24555     ** write lock until the rest is covered by a read lock:
 24556     **  1:   [WWWWW]
 24557     **  2:   [....W]
 24558     **  3:   [RRRRW]
 24559     **  4:   [RRRR.]
 24560     */
 24561     if( eFileLock==SHARED_LOCK ){
 24563 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
 24564       (void)handleNFSUnlock;
 24565       assert( handleNFSUnlock==0 );
 24566 #endif
 24567 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 24568       if( handleNFSUnlock ){
 24569         int tErrno;               /* Error code from system call errors */
 24570         off_t divSize = SHARED_SIZE - 1;
 24572         lock.l_type = F_UNLCK;
 24573         lock.l_whence = SEEK_SET;
 24574         lock.l_start = SHARED_FIRST;
 24575         lock.l_len = divSize;
 24576         if( unixFileLock(pFile, &lock)==(-1) ){
 24577           tErrno = errno;
 24578           rc = SQLITE_IOERR_UNLOCK;
 24579           if( IS_LOCK_ERROR(rc) ){
 24580             pFile->lastErrno = tErrno;
 24582           goto end_unlock;
 24584         lock.l_type = F_RDLCK;
 24585         lock.l_whence = SEEK_SET;
 24586         lock.l_start = SHARED_FIRST;
 24587         lock.l_len = divSize;
 24588         if( unixFileLock(pFile, &lock)==(-1) ){
 24589           tErrno = errno;
 24590           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
 24591           if( IS_LOCK_ERROR(rc) ){
 24592             pFile->lastErrno = tErrno;
 24594           goto end_unlock;
 24596         lock.l_type = F_UNLCK;
 24597         lock.l_whence = SEEK_SET;
 24598         lock.l_start = SHARED_FIRST+divSize;
 24599         lock.l_len = SHARED_SIZE-divSize;
 24600         if( unixFileLock(pFile, &lock)==(-1) ){
 24601           tErrno = errno;
 24602           rc = SQLITE_IOERR_UNLOCK;
 24603           if( IS_LOCK_ERROR(rc) ){
 24604             pFile->lastErrno = tErrno;
 24606           goto end_unlock;
 24608       }else
 24609 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 24611         lock.l_type = F_RDLCK;
 24612         lock.l_whence = SEEK_SET;
 24613         lock.l_start = SHARED_FIRST;
 24614         lock.l_len = SHARED_SIZE;
 24615         if( unixFileLock(pFile, &lock) ){
 24616           /* In theory, the call to unixFileLock() cannot fail because another
 24617           ** process is holding an incompatible lock. If it does, this 
 24618           ** indicates that the other process is not following the locking
 24619           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
 24620           ** SQLITE_BUSY would confuse the upper layer (in practice it causes 
 24621           ** an assert to fail). */ 
 24622           rc = SQLITE_IOERR_RDLOCK;
 24623           pFile->lastErrno = errno;
 24624           goto end_unlock;
 24628     lock.l_type = F_UNLCK;
 24629     lock.l_whence = SEEK_SET;
 24630     lock.l_start = PENDING_BYTE;
 24631     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
 24632     if( unixFileLock(pFile, &lock)==0 ){
 24633       pInode->eFileLock = SHARED_LOCK;
 24634     }else{
 24635       rc = SQLITE_IOERR_UNLOCK;
 24636       pFile->lastErrno = errno;
 24637       goto end_unlock;
 24640   if( eFileLock==NO_LOCK ){
 24641     /* Decrement the shared lock counter.  Release the lock using an
 24642     ** OS call only when all threads in this same process have released
 24643     ** the lock.
 24644     */
 24645     pInode->nShared--;
 24646     if( pInode->nShared==0 ){
 24647       lock.l_type = F_UNLCK;
 24648       lock.l_whence = SEEK_SET;
 24649       lock.l_start = lock.l_len = 0L;
 24650       if( unixFileLock(pFile, &lock)==0 ){
 24651         pInode->eFileLock = NO_LOCK;
 24652       }else{
 24653         rc = SQLITE_IOERR_UNLOCK;
 24654         pFile->lastErrno = errno;
 24655         pInode->eFileLock = NO_LOCK;
 24656         pFile->eFileLock = NO_LOCK;
 24660     /* Decrement the count of locks against this same file.  When the
 24661     ** count reaches zero, close any other file descriptors whose close
 24662     ** was deferred because of outstanding locks.
 24663     */
 24664     pInode->nLock--;
 24665     assert( pInode->nLock>=0 );
 24666     if( pInode->nLock==0 ){
 24667       closePendingFds(pFile);
 24671 end_unlock:
 24672   unixLeaveMutex();
 24673   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 24674   return rc;
 24677 /*
 24678 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 24679 ** must be either NO_LOCK or SHARED_LOCK.
 24680 **
 24681 ** If the locking level of the file descriptor is already at or below
 24682 ** the requested locking level, this routine is a no-op.
 24683 */
 24684 static int unixUnlock(sqlite3_file *id, int eFileLock){
 24685   return posixUnlock(id, eFileLock, 0);
 24688 /*
 24689 ** This function performs the parts of the "close file" operation 
 24690 ** common to all locking schemes. It closes the directory and file
 24691 ** handles, if they are valid, and sets all fields of the unixFile
 24692 ** structure to 0.
 24693 **
 24694 ** It is *not* necessary to hold the mutex when this routine is called,
 24695 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
 24696 ** vxworksReleaseFileId() routine.
 24697 */
 24698 static int closeUnixFile(sqlite3_file *id){
 24699   unixFile *pFile = (unixFile*)id;
 24700   if( pFile->h>=0 ){
 24701     robust_close(pFile, pFile->h, __LINE__);
 24702     pFile->h = -1;
 24704 #if OS_VXWORKS
 24705   if( pFile->pId ){
 24706     if( pFile->ctrlFlags & UNIXFILE_DELETE ){
 24707       osUnlink(pFile->pId->zCanonicalName);
 24709     vxworksReleaseFileId(pFile->pId);
 24710     pFile->pId = 0;
 24712 #endif
 24713   OSTRACE(("CLOSE   %-3d\n", pFile->h));
 24714   OpenCounter(-1);
 24715   sqlite3_free(pFile->pUnused);
 24716   memset(pFile, 0, sizeof(unixFile));
 24717   return SQLITE_OK;
 24720 /*
 24721 ** Close a file.
 24722 */
 24723 static int unixClose(sqlite3_file *id){
 24724   int rc = SQLITE_OK;
 24725   unixFile *pFile = (unixFile *)id;
 24726   unixUnlock(id, NO_LOCK);
 24727   unixEnterMutex();
 24729   /* unixFile.pInode is always valid here. Otherwise, a different close
 24730   ** routine (e.g. nolockClose()) would be called instead.
 24731   */
 24732   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
 24733   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
 24734     /* If there are outstanding locks, do not actually close the file just
 24735     ** yet because that would clear those locks.  Instead, add the file
 24736     ** descriptor to pInode->pUnused list.  It will be automatically closed 
 24737     ** when the last lock is cleared.
 24738     */
 24739     setPendingFd(pFile);
 24741   releaseInodeInfo(pFile);
 24742   rc = closeUnixFile(id);
 24743   unixLeaveMutex();
 24744   return rc;
 24747 /************** End of the posix advisory lock implementation *****************
 24748 ******************************************************************************/
 24750 /******************************************************************************
 24751 ****************************** No-op Locking **********************************
 24752 **
 24753 ** Of the various locking implementations available, this is by far the
 24754 ** simplest:  locking is ignored.  No attempt is made to lock the database
 24755 ** file for reading or writing.
 24756 **
 24757 ** This locking mode is appropriate for use on read-only databases
 24758 ** (ex: databases that are burned into CD-ROM, for example.)  It can
 24759 ** also be used if the application employs some external mechanism to
 24760 ** prevent simultaneous access of the same database by two or more
 24761 ** database connections.  But there is a serious risk of database
 24762 ** corruption if this locking mode is used in situations where multiple
 24763 ** database connections are accessing the same database file at the same
 24764 ** time and one or more of those connections are writing.
 24765 */
 24767 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
 24768   UNUSED_PARAMETER(NotUsed);
 24769   *pResOut = 0;
 24770   return SQLITE_OK;
 24772 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
 24773   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 24774   return SQLITE_OK;
 24776 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
 24777   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 24778   return SQLITE_OK;
 24781 /*
 24782 ** Close the file.
 24783 */
 24784 static int nolockClose(sqlite3_file *id) {
 24785   return closeUnixFile(id);
 24788 /******************* End of the no-op lock implementation *********************
 24789 ******************************************************************************/
 24791 /******************************************************************************
 24792 ************************* Begin dot-file Locking ******************************
 24793 **
 24794 ** The dotfile locking implementation uses the existance of separate lock
 24795 ** files (really a directory) to control access to the database.  This works
 24796 ** on just about every filesystem imaginable.  But there are serious downsides:
 24797 **
 24798 **    (1)  There is zero concurrency.  A single reader blocks all other
 24799 **         connections from reading or writing the database.
 24800 **
 24801 **    (2)  An application crash or power loss can leave stale lock files
 24802 **         sitting around that need to be cleared manually.
 24803 **
 24804 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
 24805 ** other locking strategy is available.
 24806 **
 24807 ** Dotfile locking works by creating a subdirectory in the same directory as
 24808 ** the database and with the same name but with a ".lock" extension added.
 24809 ** The existance of a lock directory implies an EXCLUSIVE lock.  All other
 24810 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
 24811 */
 24813 /*
 24814 ** The file suffix added to the data base filename in order to create the
 24815 ** lock directory.
 24816 */
 24817 #define DOTLOCK_SUFFIX ".lock"
 24819 /*
 24820 ** This routine checks if there is a RESERVED lock held on the specified
 24821 ** file by this or any other process. If such a lock is held, set *pResOut
 24822 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 24823 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 24824 **
 24825 ** In dotfile locking, either a lock exists or it does not.  So in this
 24826 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
 24827 ** is held on the file and false if the file is unlocked.
 24828 */
 24829 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
 24830   int rc = SQLITE_OK;
 24831   int reserved = 0;
 24832   unixFile *pFile = (unixFile*)id;
 24834   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 24836   assert( pFile );
 24838   /* Check if a thread in this process holds such a lock */
 24839   if( pFile->eFileLock>SHARED_LOCK ){
 24840     /* Either this connection or some other connection in the same process
 24841     ** holds a lock on the file.  No need to check further. */
 24842     reserved = 1;
 24843   }else{
 24844     /* The lock is held if and only if the lockfile exists */
 24845     const char *zLockFile = (const char*)pFile->lockingContext;
 24846     reserved = osAccess(zLockFile, 0)==0;
 24848   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
 24849   *pResOut = reserved;
 24850   return rc;
 24853 /*
 24854 ** Lock the file with the lock specified by parameter eFileLock - one
 24855 ** of the following:
 24856 **
 24857 **     (1) SHARED_LOCK
 24858 **     (2) RESERVED_LOCK
 24859 **     (3) PENDING_LOCK
 24860 **     (4) EXCLUSIVE_LOCK
 24861 **
 24862 ** Sometimes when requesting one lock state, additional lock states
 24863 ** are inserted in between.  The locking might fail on one of the later
 24864 ** transitions leaving the lock state different from what it started but
 24865 ** still short of its goal.  The following chart shows the allowed
 24866 ** transitions and the inserted intermediate states:
 24867 **
 24868 **    UNLOCKED -> SHARED
 24869 **    SHARED -> RESERVED
 24870 **    SHARED -> (PENDING) -> EXCLUSIVE
 24871 **    RESERVED -> (PENDING) -> EXCLUSIVE
 24872 **    PENDING -> EXCLUSIVE
 24873 **
 24874 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 24875 ** routine to lower a locking level.
 24876 **
 24877 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
 24878 ** But we track the other locking levels internally.
 24879 */
 24880 static int dotlockLock(sqlite3_file *id, int eFileLock) {
 24881   unixFile *pFile = (unixFile*)id;
 24882   char *zLockFile = (char *)pFile->lockingContext;
 24883   int rc = SQLITE_OK;
 24886   /* If we have any lock, then the lock file already exists.  All we have
 24887   ** to do is adjust our internal record of the lock level.
 24888   */
 24889   if( pFile->eFileLock > NO_LOCK ){
 24890     pFile->eFileLock = eFileLock;
 24891     /* Always update the timestamp on the old file */
 24892 #ifdef HAVE_UTIME
 24893     utime(zLockFile, NULL);
 24894 #else
 24895     utimes(zLockFile, NULL);
 24896 #endif
 24897     return SQLITE_OK;
 24900   /* grab an exclusive lock */
 24901   rc = osMkdir(zLockFile, 0777);
 24902   if( rc<0 ){
 24903     /* failed to open/create the lock directory */
 24904     int tErrno = errno;
 24905     if( EEXIST == tErrno ){
 24906       rc = SQLITE_BUSY;
 24907     } else {
 24908       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24909       if( IS_LOCK_ERROR(rc) ){
 24910         pFile->lastErrno = tErrno;
 24913     return rc;
 24916   /* got it, set the type and return ok */
 24917   pFile->eFileLock = eFileLock;
 24918   return rc;
 24921 /*
 24922 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 24923 ** must be either NO_LOCK or SHARED_LOCK.
 24924 **
 24925 ** If the locking level of the file descriptor is already at or below
 24926 ** the requested locking level, this routine is a no-op.
 24927 **
 24928 ** When the locking level reaches NO_LOCK, delete the lock file.
 24929 */
 24930 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
 24931   unixFile *pFile = (unixFile*)id;
 24932   char *zLockFile = (char *)pFile->lockingContext;
 24933   int rc;
 24935   assert( pFile );
 24936   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
 24937            pFile->eFileLock, getpid()));
 24938   assert( eFileLock<=SHARED_LOCK );
 24940   /* no-op if possible */
 24941   if( pFile->eFileLock==eFileLock ){
 24942     return SQLITE_OK;
 24945   /* To downgrade to shared, simply update our internal notion of the
 24946   ** lock state.  No need to mess with the file on disk.
 24947   */
 24948   if( eFileLock==SHARED_LOCK ){
 24949     pFile->eFileLock = SHARED_LOCK;
 24950     return SQLITE_OK;
 24953   /* To fully unlock the database, delete the lock file */
 24954   assert( eFileLock==NO_LOCK );
 24955   rc = osRmdir(zLockFile);
 24956   if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
 24957   if( rc<0 ){
 24958     int tErrno = errno;
 24959     rc = 0;
 24960     if( ENOENT != tErrno ){
 24961       rc = SQLITE_IOERR_UNLOCK;
 24963     if( IS_LOCK_ERROR(rc) ){
 24964       pFile->lastErrno = tErrno;
 24966     return rc; 
 24968   pFile->eFileLock = NO_LOCK;
 24969   return SQLITE_OK;
 24972 /*
 24973 ** Close a file.  Make sure the lock has been released before closing.
 24974 */
 24975 static int dotlockClose(sqlite3_file *id) {
 24976   int rc = SQLITE_OK;
 24977   if( id ){
 24978     unixFile *pFile = (unixFile*)id;
 24979     dotlockUnlock(id, NO_LOCK);
 24980     sqlite3_free(pFile->lockingContext);
 24981     rc = closeUnixFile(id);
 24983   return rc;
 24985 /****************** End of the dot-file lock implementation *******************
 24986 ******************************************************************************/
 24988 /******************************************************************************
 24989 ************************** Begin flock Locking ********************************
 24990 **
 24991 ** Use the flock() system call to do file locking.
 24992 **
 24993 ** flock() locking is like dot-file locking in that the various
 24994 ** fine-grain locking levels supported by SQLite are collapsed into
 24995 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
 24996 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
 24997 ** still works when you do this, but concurrency is reduced since
 24998 ** only a single process can be reading the database at a time.
 24999 **
 25000 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
 25001 ** compiling for VXWORKS.
 25002 */
 25003 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
 25005 /*
 25006 ** Retry flock() calls that fail with EINTR
 25007 */
 25008 #ifdef EINTR
 25009 static int robust_flock(int fd, int op){
 25010   int rc;
 25011   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
 25012   return rc;
 25014 #else
 25015 # define robust_flock(a,b) flock(a,b)
 25016 #endif
 25019 /*
 25020 ** This routine checks if there is a RESERVED lock held on the specified
 25021 ** file by this or any other process. If such a lock is held, set *pResOut
 25022 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 25023 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 25024 */
 25025 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
 25026   int rc = SQLITE_OK;
 25027   int reserved = 0;
 25028   unixFile *pFile = (unixFile*)id;
 25030   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 25032   assert( pFile );
 25034   /* Check if a thread in this process holds such a lock */
 25035   if( pFile->eFileLock>SHARED_LOCK ){
 25036     reserved = 1;
 25039   /* Otherwise see if some other process holds it. */
 25040   if( !reserved ){
 25041     /* attempt to get the lock */
 25042     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
 25043     if( !lrc ){
 25044       /* got the lock, unlock it */
 25045       lrc = robust_flock(pFile->h, LOCK_UN);
 25046       if ( lrc ) {
 25047         int tErrno = errno;
 25048         /* unlock failed with an error */
 25049         lrc = SQLITE_IOERR_UNLOCK; 
 25050         if( IS_LOCK_ERROR(lrc) ){
 25051           pFile->lastErrno = tErrno;
 25052           rc = lrc;
 25055     } else {
 25056       int tErrno = errno;
 25057       reserved = 1;
 25058       /* someone else might have it reserved */
 25059       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 
 25060       if( IS_LOCK_ERROR(lrc) ){
 25061         pFile->lastErrno = tErrno;
 25062         rc = lrc;
 25066   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
 25068 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 25069   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
 25070     rc = SQLITE_OK;
 25071     reserved=1;
 25073 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 25074   *pResOut = reserved;
 25075   return rc;
 25078 /*
 25079 ** Lock the file with the lock specified by parameter eFileLock - one
 25080 ** of the following:
 25081 **
 25082 **     (1) SHARED_LOCK
 25083 **     (2) RESERVED_LOCK
 25084 **     (3) PENDING_LOCK
 25085 **     (4) EXCLUSIVE_LOCK
 25086 **
 25087 ** Sometimes when requesting one lock state, additional lock states
 25088 ** are inserted in between.  The locking might fail on one of the later
 25089 ** transitions leaving the lock state different from what it started but
 25090 ** still short of its goal.  The following chart shows the allowed
 25091 ** transitions and the inserted intermediate states:
 25092 **
 25093 **    UNLOCKED -> SHARED
 25094 **    SHARED -> RESERVED
 25095 **    SHARED -> (PENDING) -> EXCLUSIVE
 25096 **    RESERVED -> (PENDING) -> EXCLUSIVE
 25097 **    PENDING -> EXCLUSIVE
 25098 **
 25099 ** flock() only really support EXCLUSIVE locks.  We track intermediate
 25100 ** lock states in the sqlite3_file structure, but all locks SHARED or
 25101 ** above are really EXCLUSIVE locks and exclude all other processes from
 25102 ** access the file.
 25103 **
 25104 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 25105 ** routine to lower a locking level.
 25106 */
 25107 static int flockLock(sqlite3_file *id, int eFileLock) {
 25108   int rc = SQLITE_OK;
 25109   unixFile *pFile = (unixFile*)id;
 25111   assert( pFile );
 25113   /* if we already have a lock, it is exclusive.  
 25114   ** Just adjust level and punt on outta here. */
 25115   if (pFile->eFileLock > NO_LOCK) {
 25116     pFile->eFileLock = eFileLock;
 25117     return SQLITE_OK;
 25120   /* grab an exclusive lock */
 25122   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
 25123     int tErrno = errno;
 25124     /* didn't get, must be busy */
 25125     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 25126     if( IS_LOCK_ERROR(rc) ){
 25127       pFile->lastErrno = tErrno;
 25129   } else {
 25130     /* got it, set the type and return ok */
 25131     pFile->eFileLock = eFileLock;
 25133   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
 25134            rc==SQLITE_OK ? "ok" : "failed"));
 25135 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 25136   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
 25137     rc = SQLITE_BUSY;
 25139 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 25140   return rc;
 25144 /*
 25145 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25146 ** must be either NO_LOCK or SHARED_LOCK.
 25147 **
 25148 ** If the locking level of the file descriptor is already at or below
 25149 ** the requested locking level, this routine is a no-op.
 25150 */
 25151 static int flockUnlock(sqlite3_file *id, int eFileLock) {
 25152   unixFile *pFile = (unixFile*)id;
 25154   assert( pFile );
 25155   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
 25156            pFile->eFileLock, getpid()));
 25157   assert( eFileLock<=SHARED_LOCK );
 25159   /* no-op if possible */
 25160   if( pFile->eFileLock==eFileLock ){
 25161     return SQLITE_OK;
 25164   /* shared can just be set because we always have an exclusive */
 25165   if (eFileLock==SHARED_LOCK) {
 25166     pFile->eFileLock = eFileLock;
 25167     return SQLITE_OK;
 25170   /* no, really, unlock. */
 25171   if( robust_flock(pFile->h, LOCK_UN) ){
 25172 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 25173     return SQLITE_OK;
 25174 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 25175     return SQLITE_IOERR_UNLOCK;
 25176   }else{
 25177     pFile->eFileLock = NO_LOCK;
 25178     return SQLITE_OK;
 25182 /*
 25183 ** Close a file.
 25184 */
 25185 static int flockClose(sqlite3_file *id) {
 25186   int rc = SQLITE_OK;
 25187   if( id ){
 25188     flockUnlock(id, NO_LOCK);
 25189     rc = closeUnixFile(id);
 25191   return rc;
 25194 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
 25196 /******************* End of the flock lock implementation *********************
 25197 ******************************************************************************/
 25199 /******************************************************************************
 25200 ************************ Begin Named Semaphore Locking ************************
 25201 **
 25202 ** Named semaphore locking is only supported on VxWorks.
 25203 **
 25204 ** Semaphore locking is like dot-lock and flock in that it really only
 25205 ** supports EXCLUSIVE locking.  Only a single process can read or write
 25206 ** the database file at a time.  This reduces potential concurrency, but
 25207 ** makes the lock implementation much easier.
 25208 */
 25209 #if OS_VXWORKS
 25211 /*
 25212 ** This routine checks if there is a RESERVED lock held on the specified
 25213 ** file by this or any other process. If such a lock is held, set *pResOut
 25214 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 25215 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 25216 */
 25217 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
 25218   int rc = SQLITE_OK;
 25219   int reserved = 0;
 25220   unixFile *pFile = (unixFile*)id;
 25222   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 25224   assert( pFile );
 25226   /* Check if a thread in this process holds such a lock */
 25227   if( pFile->eFileLock>SHARED_LOCK ){
 25228     reserved = 1;
 25231   /* Otherwise see if some other process holds it. */
 25232   if( !reserved ){
 25233     sem_t *pSem = pFile->pInode->pSem;
 25234     struct stat statBuf;
 25236     if( sem_trywait(pSem)==-1 ){
 25237       int tErrno = errno;
 25238       if( EAGAIN != tErrno ){
 25239         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
 25240         pFile->lastErrno = tErrno;
 25241       } else {
 25242         /* someone else has the lock when we are in NO_LOCK */
 25243         reserved = (pFile->eFileLock < SHARED_LOCK);
 25245     }else{
 25246       /* we could have it if we want it */
 25247       sem_post(pSem);
 25250   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
 25252   *pResOut = reserved;
 25253   return rc;
 25256 /*
 25257 ** Lock the file with the lock specified by parameter eFileLock - one
 25258 ** of the following:
 25259 **
 25260 **     (1) SHARED_LOCK
 25261 **     (2) RESERVED_LOCK
 25262 **     (3) PENDING_LOCK
 25263 **     (4) EXCLUSIVE_LOCK
 25264 **
 25265 ** Sometimes when requesting one lock state, additional lock states
 25266 ** are inserted in between.  The locking might fail on one of the later
 25267 ** transitions leaving the lock state different from what it started but
 25268 ** still short of its goal.  The following chart shows the allowed
 25269 ** transitions and the inserted intermediate states:
 25270 **
 25271 **    UNLOCKED -> SHARED
 25272 **    SHARED -> RESERVED
 25273 **    SHARED -> (PENDING) -> EXCLUSIVE
 25274 **    RESERVED -> (PENDING) -> EXCLUSIVE
 25275 **    PENDING -> EXCLUSIVE
 25276 **
 25277 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
 25278 ** lock states in the sqlite3_file structure, but all locks SHARED or
 25279 ** above are really EXCLUSIVE locks and exclude all other processes from
 25280 ** access the file.
 25281 **
 25282 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 25283 ** routine to lower a locking level.
 25284 */
 25285 static int semLock(sqlite3_file *id, int eFileLock) {
 25286   unixFile *pFile = (unixFile*)id;
 25287   int fd;
 25288   sem_t *pSem = pFile->pInode->pSem;
 25289   int rc = SQLITE_OK;
 25291   /* if we already have a lock, it is exclusive.  
 25292   ** Just adjust level and punt on outta here. */
 25293   if (pFile->eFileLock > NO_LOCK) {
 25294     pFile->eFileLock = eFileLock;
 25295     rc = SQLITE_OK;
 25296     goto sem_end_lock;
 25299   /* lock semaphore now but bail out when already locked. */
 25300   if( sem_trywait(pSem)==-1 ){
 25301     rc = SQLITE_BUSY;
 25302     goto sem_end_lock;
 25305   /* got it, set the type and return ok */
 25306   pFile->eFileLock = eFileLock;
 25308  sem_end_lock:
 25309   return rc;
 25312 /*
 25313 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25314 ** must be either NO_LOCK or SHARED_LOCK.
 25315 **
 25316 ** If the locking level of the file descriptor is already at or below
 25317 ** the requested locking level, this routine is a no-op.
 25318 */
 25319 static int semUnlock(sqlite3_file *id, int eFileLock) {
 25320   unixFile *pFile = (unixFile*)id;
 25321   sem_t *pSem = pFile->pInode->pSem;
 25323   assert( pFile );
 25324   assert( pSem );
 25325   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
 25326            pFile->eFileLock, getpid()));
 25327   assert( eFileLock<=SHARED_LOCK );
 25329   /* no-op if possible */
 25330   if( pFile->eFileLock==eFileLock ){
 25331     return SQLITE_OK;
 25334   /* shared can just be set because we always have an exclusive */
 25335   if (eFileLock==SHARED_LOCK) {
 25336     pFile->eFileLock = eFileLock;
 25337     return SQLITE_OK;
 25340   /* no, really unlock. */
 25341   if ( sem_post(pSem)==-1 ) {
 25342     int rc, tErrno = errno;
 25343     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
 25344     if( IS_LOCK_ERROR(rc) ){
 25345       pFile->lastErrno = tErrno;
 25347     return rc; 
 25349   pFile->eFileLock = NO_LOCK;
 25350   return SQLITE_OK;
 25353 /*
 25354  ** Close a file.
 25355  */
 25356 static int semClose(sqlite3_file *id) {
 25357   if( id ){
 25358     unixFile *pFile = (unixFile*)id;
 25359     semUnlock(id, NO_LOCK);
 25360     assert( pFile );
 25361     unixEnterMutex();
 25362     releaseInodeInfo(pFile);
 25363     unixLeaveMutex();
 25364     closeUnixFile(id);
 25366   return SQLITE_OK;
 25369 #endif /* OS_VXWORKS */
 25370 /*
 25371 ** Named semaphore locking is only available on VxWorks.
 25372 **
 25373 *************** End of the named semaphore lock implementation ****************
 25374 ******************************************************************************/
 25377 /******************************************************************************
 25378 *************************** Begin AFP Locking *********************************
 25379 **
 25380 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
 25381 ** on Apple Macintosh computers - both OS9 and OSX.
 25382 **
 25383 ** Third-party implementations of AFP are available.  But this code here
 25384 ** only works on OSX.
 25385 */
 25387 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 25388 /*
 25389 ** The afpLockingContext structure contains all afp lock specific state
 25390 */
 25391 typedef struct afpLockingContext afpLockingContext;
 25392 struct afpLockingContext {
 25393   int reserved;
 25394   const char *dbPath;             /* Name of the open file */
 25395 };
 25397 struct ByteRangeLockPB2
 25399   unsigned long long offset;        /* offset to first byte to lock */
 25400   unsigned long long length;        /* nbr of bytes to lock */
 25401   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
 25402   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
 25403   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
 25404   int fd;                           /* file desc to assoc this lock with */
 25405 };
 25407 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
 25409 /*
 25410 ** This is a utility for setting or clearing a bit-range lock on an
 25411 ** AFP filesystem.
 25412 ** 
 25413 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
 25414 */
 25415 static int afpSetLock(
 25416   const char *path,              /* Name of the file to be locked or unlocked */
 25417   unixFile *pFile,               /* Open file descriptor on path */
 25418   unsigned long long offset,     /* First byte to be locked */
 25419   unsigned long long length,     /* Number of bytes to lock */
 25420   int setLockFlag                /* True to set lock.  False to clear lock */
 25421 ){
 25422   struct ByteRangeLockPB2 pb;
 25423   int err;
 25425   pb.unLockFlag = setLockFlag ? 0 : 1;
 25426   pb.startEndFlag = 0;
 25427   pb.offset = offset;
 25428   pb.length = length; 
 25429   pb.fd = pFile->h;
 25431   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", 
 25432     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
 25433     offset, length));
 25434   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
 25435   if ( err==-1 ) {
 25436     int rc;
 25437     int tErrno = errno;
 25438     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
 25439              path, tErrno, strerror(tErrno)));
 25440 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
 25441     rc = SQLITE_BUSY;
 25442 #else
 25443     rc = sqliteErrorFromPosixError(tErrno,
 25444                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
 25445 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
 25446     if( IS_LOCK_ERROR(rc) ){
 25447       pFile->lastErrno = tErrno;
 25449     return rc;
 25450   } else {
 25451     return SQLITE_OK;
 25455 /*
 25456 ** This routine checks if there is a RESERVED lock held on the specified
 25457 ** file by this or any other process. If such a lock is held, set *pResOut
 25458 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 25459 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 25460 */
 25461 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
 25462   int rc = SQLITE_OK;
 25463   int reserved = 0;
 25464   unixFile *pFile = (unixFile*)id;
 25465   afpLockingContext *context;
 25467   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 25469   assert( pFile );
 25470   context = (afpLockingContext *) pFile->lockingContext;
 25471   if( context->reserved ){
 25472     *pResOut = 1;
 25473     return SQLITE_OK;
 25475   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
 25477   /* Check if a thread in this process holds such a lock */
 25478   if( pFile->pInode->eFileLock>SHARED_LOCK ){
 25479     reserved = 1;
 25482   /* Otherwise see if some other process holds it.
 25483    */
 25484   if( !reserved ){
 25485     /* lock the RESERVED byte */
 25486     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
 25487     if( SQLITE_OK==lrc ){
 25488       /* if we succeeded in taking the reserved lock, unlock it to restore
 25489       ** the original state */
 25490       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
 25491     } else {
 25492       /* if we failed to get the lock then someone else must have it */
 25493       reserved = 1;
 25495     if( IS_LOCK_ERROR(lrc) ){
 25496       rc=lrc;
 25500   unixLeaveMutex();
 25501   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
 25503   *pResOut = reserved;
 25504   return rc;
 25507 /*
 25508 ** Lock the file with the lock specified by parameter eFileLock - one
 25509 ** of the following:
 25510 **
 25511 **     (1) SHARED_LOCK
 25512 **     (2) RESERVED_LOCK
 25513 **     (3) PENDING_LOCK
 25514 **     (4) EXCLUSIVE_LOCK
 25515 **
 25516 ** Sometimes when requesting one lock state, additional lock states
 25517 ** are inserted in between.  The locking might fail on one of the later
 25518 ** transitions leaving the lock state different from what it started but
 25519 ** still short of its goal.  The following chart shows the allowed
 25520 ** transitions and the inserted intermediate states:
 25521 **
 25522 **    UNLOCKED -> SHARED
 25523 **    SHARED -> RESERVED
 25524 **    SHARED -> (PENDING) -> EXCLUSIVE
 25525 **    RESERVED -> (PENDING) -> EXCLUSIVE
 25526 **    PENDING -> EXCLUSIVE
 25527 **
 25528 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 25529 ** routine to lower a locking level.
 25530 */
 25531 static int afpLock(sqlite3_file *id, int eFileLock){
 25532   int rc = SQLITE_OK;
 25533   unixFile *pFile = (unixFile*)id;
 25534   unixInodeInfo *pInode = pFile->pInode;
 25535   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
 25537   assert( pFile );
 25538   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
 25539            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 25540            azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
 25542   /* If there is already a lock of this type or more restrictive on the
 25543   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
 25544   ** unixEnterMutex() hasn't been called yet.
 25545   */
 25546   if( pFile->eFileLock>=eFileLock ){
 25547     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
 25548            azFileLock(eFileLock)));
 25549     return SQLITE_OK;
 25552   /* Make sure the locking sequence is correct
 25553   **  (1) We never move from unlocked to anything higher than shared lock.
 25554   **  (2) SQLite never explicitly requests a pendig lock.
 25555   **  (3) A shared lock is always held when a reserve lock is requested.
 25556   */
 25557   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
 25558   assert( eFileLock!=PENDING_LOCK );
 25559   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
 25561   /* This mutex is needed because pFile->pInode is shared across threads
 25562   */
 25563   unixEnterMutex();
 25564   pInode = pFile->pInode;
 25566   /* If some thread using this PID has a lock via a different unixFile*
 25567   ** handle that precludes the requested lock, return BUSY.
 25568   */
 25569   if( (pFile->eFileLock!=pInode->eFileLock && 
 25570        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
 25571      ){
 25572     rc = SQLITE_BUSY;
 25573     goto afp_end_lock;
 25576   /* If a SHARED lock is requested, and some thread using this PID already
 25577   ** has a SHARED or RESERVED lock, then increment reference counts and
 25578   ** return SQLITE_OK.
 25579   */
 25580   if( eFileLock==SHARED_LOCK && 
 25581      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
 25582     assert( eFileLock==SHARED_LOCK );
 25583     assert( pFile->eFileLock==0 );
 25584     assert( pInode->nShared>0 );
 25585     pFile->eFileLock = SHARED_LOCK;
 25586     pInode->nShared++;
 25587     pInode->nLock++;
 25588     goto afp_end_lock;
 25591   /* A PENDING lock is needed before acquiring a SHARED lock and before
 25592   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
 25593   ** be released.
 25594   */
 25595   if( eFileLock==SHARED_LOCK 
 25596       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
 25597   ){
 25598     int failed;
 25599     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
 25600     if (failed) {
 25601       rc = failed;
 25602       goto afp_end_lock;
 25606   /* If control gets to this point, then actually go ahead and make
 25607   ** operating system calls for the specified lock.
 25608   */
 25609   if( eFileLock==SHARED_LOCK ){
 25610     int lrc1, lrc2, lrc1Errno = 0;
 25611     long lk, mask;
 25613     assert( pInode->nShared==0 );
 25614     assert( pInode->eFileLock==0 );
 25616     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
 25617     /* Now get the read-lock SHARED_LOCK */
 25618     /* note that the quality of the randomness doesn't matter that much */
 25619     lk = random(); 
 25620     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
 25621     lrc1 = afpSetLock(context->dbPath, pFile, 
 25622           SHARED_FIRST+pInode->sharedByte, 1, 1);
 25623     if( IS_LOCK_ERROR(lrc1) ){
 25624       lrc1Errno = pFile->lastErrno;
 25626     /* Drop the temporary PENDING lock */
 25627     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
 25629     if( IS_LOCK_ERROR(lrc1) ) {
 25630       pFile->lastErrno = lrc1Errno;
 25631       rc = lrc1;
 25632       goto afp_end_lock;
 25633     } else if( IS_LOCK_ERROR(lrc2) ){
 25634       rc = lrc2;
 25635       goto afp_end_lock;
 25636     } else if( lrc1 != SQLITE_OK ) {
 25637       rc = lrc1;
 25638     } else {
 25639       pFile->eFileLock = SHARED_LOCK;
 25640       pInode->nLock++;
 25641       pInode->nShared = 1;
 25643   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
 25644     /* We are trying for an exclusive lock but another thread in this
 25645      ** same process is still holding a shared lock. */
 25646     rc = SQLITE_BUSY;
 25647   }else{
 25648     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
 25649     ** assumed that there is a SHARED or greater lock on the file
 25650     ** already.
 25651     */
 25652     int failed = 0;
 25653     assert( 0!=pFile->eFileLock );
 25654     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
 25655         /* Acquire a RESERVED lock */
 25656         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
 25657       if( !failed ){
 25658         context->reserved = 1;
 25661     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
 25662       /* Acquire an EXCLUSIVE lock */
 25664       /* Remove the shared lock before trying the range.  we'll need to 
 25665       ** reestablish the shared lock if we can't get the  afpUnlock
 25666       */
 25667       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
 25668                          pInode->sharedByte, 1, 0)) ){
 25669         int failed2 = SQLITE_OK;
 25670         /* now attemmpt to get the exclusive lock range */
 25671         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
 25672                                SHARED_SIZE, 1);
 25673         if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
 25674                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
 25675           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
 25676           ** a critical I/O error
 25677           */
 25678           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
 25679                SQLITE_IOERR_LOCK;
 25680           goto afp_end_lock;
 25682       }else{
 25683         rc = failed; 
 25686     if( failed ){
 25687       rc = failed;
 25691   if( rc==SQLITE_OK ){
 25692     pFile->eFileLock = eFileLock;
 25693     pInode->eFileLock = eFileLock;
 25694   }else if( eFileLock==EXCLUSIVE_LOCK ){
 25695     pFile->eFileLock = PENDING_LOCK;
 25696     pInode->eFileLock = PENDING_LOCK;
 25699 afp_end_lock:
 25700   unixLeaveMutex();
 25701   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), 
 25702          rc==SQLITE_OK ? "ok" : "failed"));
 25703   return rc;
 25706 /*
 25707 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25708 ** must be either NO_LOCK or SHARED_LOCK.
 25709 **
 25710 ** If the locking level of the file descriptor is already at or below
 25711 ** the requested locking level, this routine is a no-op.
 25712 */
 25713 static int afpUnlock(sqlite3_file *id, int eFileLock) {
 25714   int rc = SQLITE_OK;
 25715   unixFile *pFile = (unixFile*)id;
 25716   unixInodeInfo *pInode;
 25717   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
 25718   int skipShared = 0;
 25719 #ifdef SQLITE_TEST
 25720   int h = pFile->h;
 25721 #endif
 25723   assert( pFile );
 25724   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
 25725            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 25726            getpid()));
 25728   assert( eFileLock<=SHARED_LOCK );
 25729   if( pFile->eFileLock<=eFileLock ){
 25730     return SQLITE_OK;
 25732   unixEnterMutex();
 25733   pInode = pFile->pInode;
 25734   assert( pInode->nShared!=0 );
 25735   if( pFile->eFileLock>SHARED_LOCK ){
 25736     assert( pInode->eFileLock==pFile->eFileLock );
 25737     SimulateIOErrorBenign(1);
 25738     SimulateIOError( h=(-1) )
 25739     SimulateIOErrorBenign(0);
 25741 #ifdef SQLITE_DEBUG
 25742     /* When reducing a lock such that other processes can start
 25743     ** reading the database file again, make sure that the
 25744     ** transaction counter was updated if any part of the database
 25745     ** file changed.  If the transaction counter is not updated,
 25746     ** other connections to the same file might not realize that
 25747     ** the file has changed and hence might not know to flush their
 25748     ** cache.  The use of a stale cache can lead to database corruption.
 25749     */
 25750     assert( pFile->inNormalWrite==0
 25751            || pFile->dbUpdate==0
 25752            || pFile->transCntrChng==1 );
 25753     pFile->inNormalWrite = 0;
 25754 #endif
 25756     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
 25757       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
 25758       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
 25759         /* only re-establish the shared lock if necessary */
 25760         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
 25761         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
 25762       } else {
 25763         skipShared = 1;
 25766     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
 25767       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
 25769     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
 25770       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
 25771       if( !rc ){ 
 25772         context->reserved = 0; 
 25775     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
 25776       pInode->eFileLock = SHARED_LOCK;
 25779   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
 25781     /* Decrement the shared lock counter.  Release the lock using an
 25782     ** OS call only when all threads in this same process have released
 25783     ** the lock.
 25784     */
 25785     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
 25786     pInode->nShared--;
 25787     if( pInode->nShared==0 ){
 25788       SimulateIOErrorBenign(1);
 25789       SimulateIOError( h=(-1) )
 25790       SimulateIOErrorBenign(0);
 25791       if( !skipShared ){
 25792         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
 25794       if( !rc ){
 25795         pInode->eFileLock = NO_LOCK;
 25796         pFile->eFileLock = NO_LOCK;
 25799     if( rc==SQLITE_OK ){
 25800       pInode->nLock--;
 25801       assert( pInode->nLock>=0 );
 25802       if( pInode->nLock==0 ){
 25803         closePendingFds(pFile);
 25808   unixLeaveMutex();
 25809   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 25810   return rc;
 25813 /*
 25814 ** Close a file & cleanup AFP specific locking context 
 25815 */
 25816 static int afpClose(sqlite3_file *id) {
 25817   int rc = SQLITE_OK;
 25818   if( id ){
 25819     unixFile *pFile = (unixFile*)id;
 25820     afpUnlock(id, NO_LOCK);
 25821     unixEnterMutex();
 25822     if( pFile->pInode && pFile->pInode->nLock ){
 25823       /* If there are outstanding locks, do not actually close the file just
 25824       ** yet because that would clear those locks.  Instead, add the file
 25825       ** descriptor to pInode->aPending.  It will be automatically closed when
 25826       ** the last lock is cleared.
 25827       */
 25828       setPendingFd(pFile);
 25830     releaseInodeInfo(pFile);
 25831     sqlite3_free(pFile->lockingContext);
 25832     rc = closeUnixFile(id);
 25833     unixLeaveMutex();
 25835   return rc;
 25838 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 25839 /*
 25840 ** The code above is the AFP lock implementation.  The code is specific
 25841 ** to MacOSX and does not work on other unix platforms.  No alternative
 25842 ** is available.  If you don't compile for a mac, then the "unix-afp"
 25843 ** VFS is not available.
 25844 **
 25845 ********************* End of the AFP lock implementation **********************
 25846 ******************************************************************************/
 25848 /******************************************************************************
 25849 *************************** Begin NFS Locking ********************************/
 25851 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 25852 /*
 25853  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25854  ** must be either NO_LOCK or SHARED_LOCK.
 25855  **
 25856  ** If the locking level of the file descriptor is already at or below
 25857  ** the requested locking level, this routine is a no-op.
 25858  */
 25859 static int nfsUnlock(sqlite3_file *id, int eFileLock){
 25860   return posixUnlock(id, eFileLock, 1);
 25863 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 25864 /*
 25865 ** The code above is the NFS lock implementation.  The code is specific
 25866 ** to MacOSX and does not work on other unix platforms.  No alternative
 25867 ** is available.  
 25868 **
 25869 ********************* End of the NFS lock implementation **********************
 25870 ******************************************************************************/
 25872 /******************************************************************************
 25873 **************** Non-locking sqlite3_file methods *****************************
 25874 **
 25875 ** The next division contains implementations for all methods of the 
 25876 ** sqlite3_file object other than the locking methods.  The locking
 25877 ** methods were defined in divisions above (one locking method per
 25878 ** division).  Those methods that are common to all locking modes
 25879 ** are gather together into this division.
 25880 */
 25882 /*
 25883 ** Seek to the offset passed as the second argument, then read cnt 
 25884 ** bytes into pBuf. Return the number of bytes actually read.
 25885 **
 25886 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
 25887 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
 25888 ** one system to another.  Since SQLite does not define USE_PREAD
 25889 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
 25890 ** See tickets #2741 and #2681.
 25891 **
 25892 ** To avoid stomping the errno value on a failed read the lastErrno value
 25893 ** is set before returning.
 25894 */
 25895 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
 25896   int got;
 25897   int prior = 0;
 25898 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
 25899   i64 newOffset;
 25900 #endif
 25901   TIMER_START;
 25902   assert( cnt==(cnt&0x1ffff) );
 25903   cnt &= 0x1ffff;
 25904   do{
 25905 #if defined(USE_PREAD)
 25906     got = osPread(id->h, pBuf, cnt, offset);
 25907     SimulateIOError( got = -1 );
 25908 #elif defined(USE_PREAD64)
 25909     got = osPread64(id->h, pBuf, cnt, offset);
 25910     SimulateIOError( got = -1 );
 25911 #else
 25912     newOffset = lseek(id->h, offset, SEEK_SET);
 25913     SimulateIOError( newOffset-- );
 25914     if( newOffset!=offset ){
 25915       if( newOffset == -1 ){
 25916         ((unixFile*)id)->lastErrno = errno;
 25917       }else{
 25918         ((unixFile*)id)->lastErrno = 0;
 25920       return -1;
 25922     got = osRead(id->h, pBuf, cnt);
 25923 #endif
 25924     if( got==cnt ) break;
 25925     if( got<0 ){
 25926       if( errno==EINTR ){ got = 1; continue; }
 25927       prior = 0;
 25928       ((unixFile*)id)->lastErrno = errno;
 25929       break;
 25930     }else if( got>0 ){
 25931       cnt -= got;
 25932       offset += got;
 25933       prior += got;
 25934       pBuf = (void*)(got + (char*)pBuf);
 25936   }while( got>0 );
 25937   TIMER_END;
 25938   OSTRACE(("READ    %-3d %5d %7lld %llu\n",
 25939             id->h, got+prior, offset-prior, TIMER_ELAPSED));
 25940   return got+prior;
 25943 /*
 25944 ** Read data from a file into a buffer.  Return SQLITE_OK if all
 25945 ** bytes were read successfully and SQLITE_IOERR if anything goes
 25946 ** wrong.
 25947 */
 25948 static int unixRead(
 25949   sqlite3_file *id, 
 25950   void *pBuf, 
 25951   int amt,
 25952   sqlite3_int64 offset
 25953 ){
 25954   unixFile *pFile = (unixFile *)id;
 25955   int got;
 25956   assert( id );
 25958   /* If this is a database file (not a journal, master-journal or temp
 25959   ** file), the bytes in the locking range should never be read or written. */
 25960 #if 0
 25961   assert( pFile->pUnused==0
 25962        || offset>=PENDING_BYTE+512
 25963        || offset+amt<=PENDING_BYTE 
 25964   );
 25965 #endif
 25967   got = seekAndRead(pFile, offset, pBuf, amt);
 25968   if( got==amt ){
 25969     return SQLITE_OK;
 25970   }else if( got<0 ){
 25971     /* lastErrno set by seekAndRead */
 25972     return SQLITE_IOERR_READ;
 25973   }else{
 25974     pFile->lastErrno = 0; /* not a system error */
 25975     /* Unread parts of the buffer must be zero-filled */
 25976     memset(&((char*)pBuf)[got], 0, amt-got);
 25977     return SQLITE_IOERR_SHORT_READ;
 25981 /*
 25982 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
 25983 ** Return the number of bytes actually read.  Update the offset.
 25984 **
 25985 ** To avoid stomping the errno value on a failed write the lastErrno value
 25986 ** is set before returning.
 25987 */
 25988 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
 25989   int got;
 25990 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
 25991   i64 newOffset;
 25992 #endif
 25993   assert( cnt==(cnt&0x1ffff) );
 25994   cnt &= 0x1ffff;
 25995   TIMER_START;
 25996 #if defined(USE_PREAD)
 25997   do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
 25998 #elif defined(USE_PREAD64)
 25999   do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
 26000 #else
 26001   do{
 26002     newOffset = lseek(id->h, offset, SEEK_SET);
 26003     SimulateIOError( newOffset-- );
 26004     if( newOffset!=offset ){
 26005       if( newOffset == -1 ){
 26006         ((unixFile*)id)->lastErrno = errno;
 26007       }else{
 26008         ((unixFile*)id)->lastErrno = 0;
 26010       return -1;
 26012     got = osWrite(id->h, pBuf, cnt);
 26013   }while( got<0 && errno==EINTR );
 26014 #endif
 26015   TIMER_END;
 26016   if( got<0 ){
 26017     ((unixFile*)id)->lastErrno = errno;
 26020   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
 26021   return got;
 26025 /*
 26026 ** Write data from a buffer into a file.  Return SQLITE_OK on success
 26027 ** or some other error code on failure.
 26028 */
 26029 static int unixWrite(
 26030   sqlite3_file *id, 
 26031   const void *pBuf, 
 26032   int amt,
 26033   sqlite3_int64 offset 
 26034 ){
 26035   unixFile *pFile = (unixFile*)id;
 26036   int wrote = 0;
 26037   assert( id );
 26038   assert( amt>0 );
 26040   /* If this is a database file (not a journal, master-journal or temp
 26041   ** file), the bytes in the locking range should never be read or written. */
 26042 #if 0
 26043   assert( pFile->pUnused==0
 26044        || offset>=PENDING_BYTE+512
 26045        || offset+amt<=PENDING_BYTE 
 26046   );
 26047 #endif
 26049 #ifdef SQLITE_DEBUG
 26050   /* If we are doing a normal write to a database file (as opposed to
 26051   ** doing a hot-journal rollback or a write to some file other than a
 26052   ** normal database file) then record the fact that the database
 26053   ** has changed.  If the transaction counter is modified, record that
 26054   ** fact too.
 26055   */
 26056   if( pFile->inNormalWrite ){
 26057     pFile->dbUpdate = 1;  /* The database has been modified */
 26058     if( offset<=24 && offset+amt>=27 ){
 26059       int rc;
 26060       char oldCntr[4];
 26061       SimulateIOErrorBenign(1);
 26062       rc = seekAndRead(pFile, 24, oldCntr, 4);
 26063       SimulateIOErrorBenign(0);
 26064       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
 26065         pFile->transCntrChng = 1;  /* The transaction counter has changed */
 26069 #endif
 26071   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
 26072     amt -= wrote;
 26073     offset += wrote;
 26074     pBuf = &((char*)pBuf)[wrote];
 26076   SimulateIOError(( wrote=(-1), amt=1 ));
 26077   SimulateDiskfullError(( wrote=0, amt=1 ));
 26079   if( amt>0 ){
 26080     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
 26081       /* lastErrno set by seekAndWrite */
 26082       return SQLITE_IOERR_WRITE;
 26083     }else{
 26084       pFile->lastErrno = 0; /* not a system error */
 26085       return SQLITE_FULL;
 26089   return SQLITE_OK;
 26092 #ifdef SQLITE_TEST
 26093 /*
 26094 ** Count the number of fullsyncs and normal syncs.  This is used to test
 26095 ** that syncs and fullsyncs are occurring at the right times.
 26096 */
 26097 SQLITE_API int sqlite3_sync_count = 0;
 26098 SQLITE_API int sqlite3_fullsync_count = 0;
 26099 #endif
 26101 /*
 26102 ** We do not trust systems to provide a working fdatasync().  Some do.
 26103 ** Others do no.  To be safe, we will stick with the (slightly slower)
 26104 ** fsync(). If you know that your system does support fdatasync() correctly,
 26105 ** then simply compile with -Dfdatasync=fdatasync
 26106 */
 26107 #if !defined(fdatasync)
 26108 # define fdatasync fsync
 26109 #endif
 26111 /*
 26112 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
 26113 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
 26114 ** only available on Mac OS X.  But that could change.
 26115 */
 26116 #ifdef F_FULLFSYNC
 26117 # define HAVE_FULLFSYNC 1
 26118 #else
 26119 # define HAVE_FULLFSYNC 0
 26120 #endif
 26123 /*
 26124 ** The fsync() system call does not work as advertised on many
 26125 ** unix systems.  The following procedure is an attempt to make
 26126 ** it work better.
 26127 **
 26128 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
 26129 ** for testing when we want to run through the test suite quickly.
 26130 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
 26131 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
 26132 ** or power failure will likely corrupt the database file.
 26133 **
 26134 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
 26135 ** The idea behind dataOnly is that it should only write the file content
 26136 ** to disk, not the inode.  We only set dataOnly if the file size is 
 26137 ** unchanged since the file size is part of the inode.  However, 
 26138 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
 26139 ** file size has changed.  The only real difference between fdatasync()
 26140 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
 26141 ** inode if the mtime or owner or other inode attributes have changed.
 26142 ** We only care about the file size, not the other file attributes, so
 26143 ** as far as SQLite is concerned, an fdatasync() is always adequate.
 26144 ** So, we always use fdatasync() if it is available, regardless of
 26145 ** the value of the dataOnly flag.
 26146 */
 26147 static int full_fsync(int fd, int fullSync, int dataOnly){
 26148   int rc;
 26150   /* The following "ifdef/elif/else/" block has the same structure as
 26151   ** the one below. It is replicated here solely to avoid cluttering 
 26152   ** up the real code with the UNUSED_PARAMETER() macros.
 26153   */
 26154 #ifdef SQLITE_NO_SYNC
 26155   UNUSED_PARAMETER(fd);
 26156   UNUSED_PARAMETER(fullSync);
 26157   UNUSED_PARAMETER(dataOnly);
 26158 #elif HAVE_FULLFSYNC
 26159   UNUSED_PARAMETER(dataOnly);
 26160 #else
 26161   UNUSED_PARAMETER(fullSync);
 26162   UNUSED_PARAMETER(dataOnly);
 26163 #endif
 26165   /* Record the number of times that we do a normal fsync() and 
 26166   ** FULLSYNC.  This is used during testing to verify that this procedure
 26167   ** gets called with the correct arguments.
 26168   */
 26169 #ifdef SQLITE_TEST
 26170   if( fullSync ) sqlite3_fullsync_count++;
 26171   sqlite3_sync_count++;
 26172 #endif
 26174   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 26175   ** no-op
 26176   */
 26177 #ifdef SQLITE_NO_SYNC
 26178   rc = SQLITE_OK;
 26179 #elif HAVE_FULLFSYNC
 26180   if( fullSync ){
 26181     rc = osFcntl(fd, F_FULLFSYNC, 0);
 26182   }else{
 26183     rc = 1;
 26185   /* If the FULLFSYNC failed, fall back to attempting an fsync().
 26186   ** It shouldn't be possible for fullfsync to fail on the local 
 26187   ** file system (on OSX), so failure indicates that FULLFSYNC
 26188   ** isn't supported for this file system. So, attempt an fsync 
 26189   ** and (for now) ignore the overhead of a superfluous fcntl call.  
 26190   ** It'd be better to detect fullfsync support once and avoid 
 26191   ** the fcntl call every time sync is called.
 26192   */
 26193   if( rc ) rc = fsync(fd);
 26195 #elif defined(__APPLE__)
 26196   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
 26197   ** so currently we default to the macro that redefines fdatasync to fsync
 26198   */
 26199   rc = fsync(fd);
 26200 #else 
 26201   rc = fdatasync(fd);
 26202 #if OS_VXWORKS
 26203   if( rc==-1 && errno==ENOTSUP ){
 26204     rc = fsync(fd);
 26206 #endif /* OS_VXWORKS */
 26207 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
 26209   if( OS_VXWORKS && rc!= -1 ){
 26210     rc = 0;
 26212   return rc;
 26215 /*
 26216 ** Open a file descriptor to the directory containing file zFilename.
 26217 ** If successful, *pFd is set to the opened file descriptor and
 26218 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
 26219 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
 26220 ** value.
 26221 **
 26222 ** The directory file descriptor is used for only one thing - to
 26223 ** fsync() a directory to make sure file creation and deletion events
 26224 ** are flushed to disk.  Such fsyncs are not needed on newer
 26225 ** journaling filesystems, but are required on older filesystems.
 26226 **
 26227 ** This routine can be overridden using the xSetSysCall interface.
 26228 ** The ability to override this routine was added in support of the
 26229 ** chromium sandbox.  Opening a directory is a security risk (we are
 26230 ** told) so making it overrideable allows the chromium sandbox to
 26231 ** replace this routine with a harmless no-op.  To make this routine
 26232 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
 26233 ** *pFd set to a negative number.
 26234 **
 26235 ** If SQLITE_OK is returned, the caller is responsible for closing
 26236 ** the file descriptor *pFd using close().
 26237 */
 26238 static int openDirectory(const char *zFilename, int *pFd){
 26239   int ii;
 26240   int fd = -1;
 26241   char zDirname[MAX_PATHNAME+1];
 26243   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
 26244   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
 26245   if( ii>0 ){
 26246     zDirname[ii] = '\0';
 26247     fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
 26248     if( fd>=0 ){
 26249       OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
 26252   *pFd = fd;
 26253   return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
 26256 /*
 26257 ** Make sure all writes to a particular file are committed to disk.
 26258 **
 26259 ** If dataOnly==0 then both the file itself and its metadata (file
 26260 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
 26261 ** file data is synced.
 26262 **
 26263 ** Under Unix, also make sure that the directory entry for the file
 26264 ** has been created by fsync-ing the directory that contains the file.
 26265 ** If we do not do this and we encounter a power failure, the directory
 26266 ** entry for the journal might not exist after we reboot.  The next
 26267 ** SQLite to access the file will not know that the journal exists (because
 26268 ** the directory entry for the journal was never created) and the transaction
 26269 ** will not roll back - possibly leading to database corruption.
 26270 */
 26271 static int unixSync(sqlite3_file *id, int flags){
 26272   int rc;
 26273   unixFile *pFile = (unixFile*)id;
 26275   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
 26276   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
 26278   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
 26279   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
 26280       || (flags&0x0F)==SQLITE_SYNC_FULL
 26281   );
 26283   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
 26284   ** line is to test that doing so does not cause any problems.
 26285   */
 26286   SimulateDiskfullError( return SQLITE_FULL );
 26288   assert( pFile );
 26289   OSTRACE(("SYNC    %-3d\n", pFile->h));
 26290   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
 26291   SimulateIOError( rc=1 );
 26292   if( rc ){
 26293     pFile->lastErrno = errno;
 26294     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
 26297   /* Also fsync the directory containing the file if the DIRSYNC flag
 26298   ** is set.  This is a one-time occurrance.  Many systems (examples: AIX)
 26299   ** are unable to fsync a directory, so ignore errors on the fsync.
 26300   */
 26301   if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
 26302     int dirfd;
 26303     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
 26304             HAVE_FULLFSYNC, isFullsync));
 26305     rc = osOpenDirectory(pFile->zPath, &dirfd);
 26306     if( rc==SQLITE_OK && dirfd>=0 ){
 26307       full_fsync(dirfd, 0, 0);
 26308       robust_close(pFile, dirfd, __LINE__);
 26309     }else if( rc==SQLITE_CANTOPEN ){
 26310       rc = SQLITE_OK;
 26312     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
 26314   return rc;
 26317 /*
 26318 ** Truncate an open file to a specified size
 26319 */
 26320 static int unixTruncate(sqlite3_file *id, i64 nByte){
 26321   unixFile *pFile = (unixFile *)id;
 26322   int rc;
 26323   assert( pFile );
 26324   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
 26326   /* If the user has configured a chunk-size for this file, truncate the
 26327   ** file so that it consists of an integer number of chunks (i.e. the
 26328   ** actual file size after the operation may be larger than the requested
 26329   ** size).
 26330   */
 26331   if( pFile->szChunk>0 ){
 26332     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 26335   rc = robust_ftruncate(pFile->h, (off_t)nByte);
 26336   if( rc ){
 26337     pFile->lastErrno = errno;
 26338     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 26339   }else{
 26340 #ifdef SQLITE_DEBUG
 26341     /* If we are doing a normal write to a database file (as opposed to
 26342     ** doing a hot-journal rollback or a write to some file other than a
 26343     ** normal database file) and we truncate the file to zero length,
 26344     ** that effectively updates the change counter.  This might happen
 26345     ** when restoring a database using the backup API from a zero-length
 26346     ** source.
 26347     */
 26348     if( pFile->inNormalWrite && nByte==0 ){
 26349       pFile->transCntrChng = 1;
 26351 #endif
 26353     return SQLITE_OK;
 26357 /*
 26358 ** Determine the current size of a file in bytes
 26359 */
 26360 static int unixFileSize(sqlite3_file *id, i64 *pSize){
 26361   int rc;
 26362   struct stat buf;
 26363   assert( id );
 26364   rc = osFstat(((unixFile*)id)->h, &buf);
 26365   SimulateIOError( rc=1 );
 26366   if( rc!=0 ){
 26367     ((unixFile*)id)->lastErrno = errno;
 26368     return SQLITE_IOERR_FSTAT;
 26370   *pSize = buf.st_size;
 26372   /* When opening a zero-size database, the findInodeInfo() procedure
 26373   ** writes a single byte into that file in order to work around a bug
 26374   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
 26375   ** layers, we need to report this file size as zero even though it is
 26376   ** really 1.   Ticket #3260.
 26377   */
 26378   if( *pSize==1 ) *pSize = 0;
 26381   return SQLITE_OK;
 26384 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 26385 /*
 26386 ** Handler for proxy-locking file-control verbs.  Defined below in the
 26387 ** proxying locking division.
 26388 */
 26389 static int proxyFileControl(sqlite3_file*,int,void*);
 26390 #endif
 26392 /* 
 26393 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 
 26394 ** file-control operation.  Enlarge the database to nBytes in size
 26395 ** (rounded up to the next chunk-size).  If the database is already
 26396 ** nBytes or larger, this routine is a no-op.
 26397 */
 26398 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
 26399   if( pFile->szChunk>0 ){
 26400     i64 nSize;                    /* Required file size */
 26401     struct stat buf;              /* Used to hold return values of fstat() */
 26403     if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
 26405     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
 26406     if( nSize>(i64)buf.st_size ){
 26408 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 26409       /* The code below is handling the return value of osFallocate() 
 26410       ** correctly. posix_fallocate() is defined to "returns zero on success, 
 26411       ** or an error number on  failure". See the manpage for details. */
 26412       int err;
 26413       do{
 26414         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
 26415       }while( err==EINTR );
 26416       if( err ) return SQLITE_IOERR_WRITE;
 26417 #else
 26418       /* If the OS does not have posix_fallocate(), fake it. First use
 26419       ** ftruncate() to set the file size, then write a single byte to
 26420       ** the last byte in each block within the extended region. This
 26421       ** is the same technique used by glibc to implement posix_fallocate()
 26422       ** on systems that do not have a real fallocate() system call.
 26423       */
 26424       int nBlk = buf.st_blksize;  /* File-system block size */
 26425       i64 iWrite;                 /* Next offset to write to */
 26427       if( robust_ftruncate(pFile->h, nSize) ){
 26428         pFile->lastErrno = errno;
 26429         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 26431       iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
 26432       while( iWrite<nSize ){
 26433         int nWrite = seekAndWrite(pFile, iWrite, "", 1);
 26434         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
 26435         iWrite += nBlk;
 26437 #endif
 26441   return SQLITE_OK;
 26444 /*
 26445 ** If *pArg is inititially negative then this is a query.  Set *pArg to
 26446 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
 26447 **
 26448 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
 26449 */
 26450 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
 26451   if( *pArg<0 ){
 26452     *pArg = (pFile->ctrlFlags & mask)!=0;
 26453   }else if( (*pArg)==0 ){
 26454     pFile->ctrlFlags &= ~mask;
 26455   }else{
 26456     pFile->ctrlFlags |= mask;
 26460 /* Forward declaration */
 26461 static int unixGetTempname(int nBuf, char *zBuf);
 26463 /*
 26464 ** Information and control of an open file handle.
 26465 */
 26466 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
 26467   unixFile *pFile = (unixFile*)id;
 26468   switch( op ){
 26469     case SQLITE_FCNTL_LOCKSTATE: {
 26470       *(int*)pArg = pFile->eFileLock;
 26471       return SQLITE_OK;
 26473     case SQLITE_LAST_ERRNO: {
 26474       *(int*)pArg = pFile->lastErrno;
 26475       return SQLITE_OK;
 26477     case SQLITE_FCNTL_CHUNK_SIZE: {
 26478       pFile->szChunk = *(int *)pArg;
 26479       return SQLITE_OK;
 26481     case SQLITE_FCNTL_SIZE_HINT: {
 26482       int rc;
 26483       SimulateIOErrorBenign(1);
 26484       rc = fcntlSizeHint(pFile, *(i64 *)pArg);
 26485       SimulateIOErrorBenign(0);
 26486       return rc;
 26488     case SQLITE_FCNTL_PERSIST_WAL: {
 26489       unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
 26490       return SQLITE_OK;
 26492     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
 26493       unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
 26494       return SQLITE_OK;
 26496     case SQLITE_FCNTL_VFSNAME: {
 26497       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
 26498       return SQLITE_OK;
 26500     case SQLITE_FCNTL_TEMPFILENAME: {
 26501       char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
 26502       if( zTFile ){
 26503         unixGetTempname(pFile->pVfs->mxPathname, zTFile);
 26504         *(char**)pArg = zTFile;
 26506       return SQLITE_OK;
 26508 #ifdef SQLITE_DEBUG
 26509     /* The pager calls this method to signal that it has done
 26510     ** a rollback and that the database is therefore unchanged and
 26511     ** it hence it is OK for the transaction change counter to be
 26512     ** unchanged.
 26513     */
 26514     case SQLITE_FCNTL_DB_UNCHANGED: {
 26515       ((unixFile*)id)->dbUpdate = 0;
 26516       return SQLITE_OK;
 26518 #endif
 26519 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 26520     case SQLITE_SET_LOCKPROXYFILE:
 26521     case SQLITE_GET_LOCKPROXYFILE: {
 26522       return proxyFileControl(id,op,pArg);
 26524 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
 26526   return SQLITE_NOTFOUND;
 26529 /*
 26530 ** Return the sector size in bytes of the underlying block device for
 26531 ** the specified file. This is almost always 512 bytes, but may be
 26532 ** larger for some devices.
 26533 **
 26534 ** SQLite code assumes this function cannot fail. It also assumes that
 26535 ** if two files are created in the same file-system directory (i.e.
 26536 ** a database and its journal file) that the sector size will be the
 26537 ** same for both.
 26538 */
 26539 #ifndef __QNXNTO__ 
 26540 static int unixSectorSize(sqlite3_file *NotUsed){
 26541   UNUSED_PARAMETER(NotUsed);
 26542   return SQLITE_DEFAULT_SECTOR_SIZE;
 26544 #endif
 26546 /*
 26547 ** The following version of unixSectorSize() is optimized for QNX.
 26548 */
 26549 #ifdef __QNXNTO__
 26550 #include <sys/dcmd_blk.h>
 26551 #include <sys/statvfs.h>
 26552 static int unixSectorSize(sqlite3_file *id){
 26553   unixFile *pFile = (unixFile*)id;
 26554   if( pFile->sectorSize == 0 ){
 26555     struct statvfs fsInfo;
 26557     /* Set defaults for non-supported filesystems */
 26558     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 26559     pFile->deviceCharacteristics = 0;
 26560     if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
 26561       return pFile->sectorSize;
 26564     if( !strcmp(fsInfo.f_basetype, "tmp") ) {
 26565       pFile->sectorSize = fsInfo.f_bsize;
 26566       pFile->deviceCharacteristics =
 26567         SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
 26568         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 26569                                       ** the write succeeds */
 26570         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 26571                                       ** so it is ordered */
 26572         0;
 26573     }else if( strstr(fsInfo.f_basetype, "etfs") ){
 26574       pFile->sectorSize = fsInfo.f_bsize;
 26575       pFile->deviceCharacteristics =
 26576         /* etfs cluster size writes are atomic */
 26577         (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
 26578         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 26579                                       ** the write succeeds */
 26580         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 26581                                       ** so it is ordered */
 26582         0;
 26583     }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
 26584       pFile->sectorSize = fsInfo.f_bsize;
 26585       pFile->deviceCharacteristics =
 26586         SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
 26587         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 26588                                       ** the write succeeds */
 26589         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 26590                                       ** so it is ordered */
 26591         0;
 26592     }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
 26593       pFile->sectorSize = fsInfo.f_bsize;
 26594       pFile->deviceCharacteristics =
 26595         /* full bitset of atomics from max sector size and smaller */
 26596         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
 26597         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 26598                                       ** so it is ordered */
 26599         0;
 26600     }else if( strstr(fsInfo.f_basetype, "dos") ){
 26601       pFile->sectorSize = fsInfo.f_bsize;
 26602       pFile->deviceCharacteristics =
 26603         /* full bitset of atomics from max sector size and smaller */
 26604         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
 26605         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 26606                                       ** so it is ordered */
 26607         0;
 26608     }else{
 26609       pFile->deviceCharacteristics =
 26610         SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
 26611         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 26612                                       ** the write succeeds */
 26613         0;
 26616   /* Last chance verification.  If the sector size isn't a multiple of 512
 26617   ** then it isn't valid.*/
 26618   if( pFile->sectorSize % 512 != 0 ){
 26619     pFile->deviceCharacteristics = 0;
 26620     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 26622   return pFile->sectorSize;
 26624 #endif /* __QNXNTO__ */
 26626 /*
 26627 ** Return the device characteristics for the file.
 26628 **
 26629 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
 26630 ** However, that choice is contraversial since technically the underlying
 26631 ** file system does not always provide powersafe overwrites.  (In other
 26632 ** words, after a power-loss event, parts of the file that were never
 26633 ** written might end up being altered.)  However, non-PSOW behavior is very,
 26634 ** very rare.  And asserting PSOW makes a large reduction in the amount
 26635 ** of required I/O for journaling, since a lot of padding is eliminated.
 26636 **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
 26637 ** available to turn it off and URI query parameter available to turn it off.
 26638 */
 26639 static int unixDeviceCharacteristics(sqlite3_file *id){
 26640   unixFile *p = (unixFile*)id;
 26641   int rc = 0;
 26642 #ifdef __QNXNTO__
 26643   if( p->sectorSize==0 ) unixSectorSize(id);
 26644   rc = p->deviceCharacteristics;
 26645 #endif
 26646   if( p->ctrlFlags & UNIXFILE_PSOW ){
 26647     rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
 26649   return rc;
 26652 #ifndef SQLITE_OMIT_WAL
 26655 /*
 26656 ** Object used to represent an shared memory buffer.  
 26657 **
 26658 ** When multiple threads all reference the same wal-index, each thread
 26659 ** has its own unixShm object, but they all point to a single instance
 26660 ** of this unixShmNode object.  In other words, each wal-index is opened
 26661 ** only once per process.
 26662 **
 26663 ** Each unixShmNode object is connected to a single unixInodeInfo object.
 26664 ** We could coalesce this object into unixInodeInfo, but that would mean
 26665 ** every open file that does not use shared memory (in other words, most
 26666 ** open files) would have to carry around this extra information.  So
 26667 ** the unixInodeInfo object contains a pointer to this unixShmNode object
 26668 ** and the unixShmNode object is created only when needed.
 26669 **
 26670 ** unixMutexHeld() must be true when creating or destroying
 26671 ** this object or while reading or writing the following fields:
 26672 **
 26673 **      nRef
 26674 **
 26675 ** The following fields are read-only after the object is created:
 26676 ** 
 26677 **      fid
 26678 **      zFilename
 26679 **
 26680 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
 26681 ** unixMutexHeld() is true when reading or writing any other field
 26682 ** in this structure.
 26683 */
 26684 struct unixShmNode {
 26685   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
 26686   sqlite3_mutex *mutex;      /* Mutex to access this object */
 26687   char *zFilename;           /* Name of the mmapped file */
 26688   int h;                     /* Open file descriptor */
 26689   int szRegion;              /* Size of shared-memory regions */
 26690   u16 nRegion;               /* Size of array apRegion */
 26691   u8 isReadonly;             /* True if read-only */
 26692   char **apRegion;           /* Array of mapped shared-memory regions */
 26693   int nRef;                  /* Number of unixShm objects pointing to this */
 26694   unixShm *pFirst;           /* All unixShm objects pointing to this */
 26695 #ifdef SQLITE_DEBUG
 26696   u8 exclMask;               /* Mask of exclusive locks held */
 26697   u8 sharedMask;             /* Mask of shared locks held */
 26698   u8 nextShmId;              /* Next available unixShm.id value */
 26699 #endif
 26700 };
 26702 /*
 26703 ** Structure used internally by this VFS to record the state of an
 26704 ** open shared memory connection.
 26705 **
 26706 ** The following fields are initialized when this object is created and
 26707 ** are read-only thereafter:
 26708 **
 26709 **    unixShm.pFile
 26710 **    unixShm.id
 26711 **
 26712 ** All other fields are read/write.  The unixShm.pFile->mutex must be held
 26713 ** while accessing any read/write fields.
 26714 */
 26715 struct unixShm {
 26716   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
 26717   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
 26718   u8 hasMutex;               /* True if holding the unixShmNode mutex */
 26719   u8 id;                     /* Id of this connection within its unixShmNode */
 26720   u16 sharedMask;            /* Mask of shared locks held */
 26721   u16 exclMask;              /* Mask of exclusive locks held */
 26722 };
 26724 /*
 26725 ** Constants used for locking
 26726 */
 26727 #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
 26728 #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 26730 /*
 26731 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
 26732 **
 26733 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
 26734 ** otherwise.
 26735 */
 26736 static int unixShmSystemLock(
 26737   unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
 26738   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
 26739   int ofst,              /* First byte of the locking range */
 26740   int n                  /* Number of bytes to lock */
 26741 ){
 26742   struct flock f;       /* The posix advisory locking structure */
 26743   int rc = SQLITE_OK;   /* Result code form fcntl() */
 26745   /* Access to the unixShmNode object is serialized by the caller */
 26746   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
 26748   /* Shared locks never span more than one byte */
 26749   assert( n==1 || lockType!=F_RDLCK );
 26751   /* Locks are within range */
 26752   assert( n>=1 && n<SQLITE_SHM_NLOCK );
 26754   if( pShmNode->h>=0 ){
 26755     /* Initialize the locking parameters */
 26756     memset(&f, 0, sizeof(f));
 26757     f.l_type = lockType;
 26758     f.l_whence = SEEK_SET;
 26759     f.l_start = ofst;
 26760     f.l_len = n;
 26762     rc = osFcntl(pShmNode->h, F_SETLK, &f);
 26763     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 26766   /* Update the global lock state and do debug tracing */
 26767 #ifdef SQLITE_DEBUG
 26768   { u16 mask;
 26769   OSTRACE(("SHM-LOCK "));
 26770   mask = (1<<(ofst+n)) - (1<<ofst);
 26771   if( rc==SQLITE_OK ){
 26772     if( lockType==F_UNLCK ){
 26773       OSTRACE(("unlock %d ok", ofst));
 26774       pShmNode->exclMask &= ~mask;
 26775       pShmNode->sharedMask &= ~mask;
 26776     }else if( lockType==F_RDLCK ){
 26777       OSTRACE(("read-lock %d ok", ofst));
 26778       pShmNode->exclMask &= ~mask;
 26779       pShmNode->sharedMask |= mask;
 26780     }else{
 26781       assert( lockType==F_WRLCK );
 26782       OSTRACE(("write-lock %d ok", ofst));
 26783       pShmNode->exclMask |= mask;
 26784       pShmNode->sharedMask &= ~mask;
 26786   }else{
 26787     if( lockType==F_UNLCK ){
 26788       OSTRACE(("unlock %d failed", ofst));
 26789     }else if( lockType==F_RDLCK ){
 26790       OSTRACE(("read-lock failed"));
 26791     }else{
 26792       assert( lockType==F_WRLCK );
 26793       OSTRACE(("write-lock %d failed", ofst));
 26796   OSTRACE((" - afterwards %03x,%03x\n",
 26797            pShmNode->sharedMask, pShmNode->exclMask));
 26799 #endif
 26801   return rc;        
 26805 /*
 26806 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
 26807 **
 26808 ** This is not a VFS shared-memory method; it is a utility function called
 26809 ** by VFS shared-memory methods.
 26810 */
 26811 static void unixShmPurge(unixFile *pFd){
 26812   unixShmNode *p = pFd->pInode->pShmNode;
 26813   assert( unixMutexHeld() );
 26814   if( p && p->nRef==0 ){
 26815     int i;
 26816     assert( p->pInode==pFd->pInode );
 26817     sqlite3_mutex_free(p->mutex);
 26818     for(i=0; i<p->nRegion; i++){
 26819       if( p->h>=0 ){
 26820         munmap(p->apRegion[i], p->szRegion);
 26821       }else{
 26822         sqlite3_free(p->apRegion[i]);
 26825     sqlite3_free(p->apRegion);
 26826     if( p->h>=0 ){
 26827       robust_close(pFd, p->h, __LINE__);
 26828       p->h = -1;
 26830     p->pInode->pShmNode = 0;
 26831     sqlite3_free(p);
 26835 /*
 26836 ** Open a shared-memory area associated with open database file pDbFd.  
 26837 ** This particular implementation uses mmapped files.
 26838 **
 26839 ** The file used to implement shared-memory is in the same directory
 26840 ** as the open database file and has the same name as the open database
 26841 ** file with the "-shm" suffix added.  For example, if the database file
 26842 ** is "/home/user1/config.db" then the file that is created and mmapped
 26843 ** for shared memory will be called "/home/user1/config.db-shm".  
 26844 **
 26845 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
 26846 ** some other tmpfs mount. But if a file in a different directory
 26847 ** from the database file is used, then differing access permissions
 26848 ** or a chroot() might cause two different processes on the same
 26849 ** database to end up using different files for shared memory - 
 26850 ** meaning that their memory would not really be shared - resulting
 26851 ** in database corruption.  Nevertheless, this tmpfs file usage
 26852 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
 26853 ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
 26854 ** option results in an incompatible build of SQLite;  builds of SQLite
 26855 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
 26856 ** same database file at the same time, database corruption will likely
 26857 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
 26858 ** "unsupported" and may go away in a future SQLite release.
 26859 **
 26860 ** When opening a new shared-memory file, if no other instances of that
 26861 ** file are currently open, in this process or in other processes, then
 26862 ** the file must be truncated to zero length or have its header cleared.
 26863 **
 26864 ** If the original database file (pDbFd) is using the "unix-excl" VFS
 26865 ** that means that an exclusive lock is held on the database file and
 26866 ** that no other processes are able to read or write the database.  In
 26867 ** that case, we do not really need shared memory.  No shared memory
 26868 ** file is created.  The shared memory will be simulated with heap memory.
 26869 */
 26870 static int unixOpenSharedMemory(unixFile *pDbFd){
 26871   struct unixShm *p = 0;          /* The connection to be opened */
 26872   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
 26873   int rc;                         /* Result code */
 26874   unixInodeInfo *pInode;          /* The inode of fd */
 26875   char *zShmFilename;             /* Name of the file used for SHM */
 26876   int nShmFilename;               /* Size of the SHM filename in bytes */
 26878   /* Allocate space for the new unixShm object. */
 26879   p = sqlite3_malloc( sizeof(*p) );
 26880   if( p==0 ) return SQLITE_NOMEM;
 26881   memset(p, 0, sizeof(*p));
 26882   assert( pDbFd->pShm==0 );
 26884   /* Check to see if a unixShmNode object already exists. Reuse an existing
 26885   ** one if present. Create a new one if necessary.
 26886   */
 26887   unixEnterMutex();
 26888   pInode = pDbFd->pInode;
 26889   pShmNode = pInode->pShmNode;
 26890   if( pShmNode==0 ){
 26891     struct stat sStat;                 /* fstat() info for database file */
 26893     /* Call fstat() to figure out the permissions on the database file. If
 26894     ** a new *-shm file is created, an attempt will be made to create it
 26895     ** with the same permissions.
 26896     */
 26897     if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
 26898       rc = SQLITE_IOERR_FSTAT;
 26899       goto shm_open_err;
 26902 #ifdef SQLITE_SHM_DIRECTORY
 26903     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
 26904 #else
 26905     nShmFilename = 6 + (int)strlen(pDbFd->zPath);
 26906 #endif
 26907     pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
 26908     if( pShmNode==0 ){
 26909       rc = SQLITE_NOMEM;
 26910       goto shm_open_err;
 26912     memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
 26913     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
 26914 #ifdef SQLITE_SHM_DIRECTORY
 26915     sqlite3_snprintf(nShmFilename, zShmFilename, 
 26916                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
 26917                      (u32)sStat.st_ino, (u32)sStat.st_dev);
 26918 #else
 26919     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
 26920     sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
 26921 #endif
 26922     pShmNode->h = -1;
 26923     pDbFd->pInode->pShmNode = pShmNode;
 26924     pShmNode->pInode = pDbFd->pInode;
 26925     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 26926     if( pShmNode->mutex==0 ){
 26927       rc = SQLITE_NOMEM;
 26928       goto shm_open_err;
 26931     if( pInode->bProcessLock==0 ){
 26932       int openFlags = O_RDWR | O_CREAT;
 26933       if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 26934         openFlags = O_RDONLY;
 26935         pShmNode->isReadonly = 1;
 26937       pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
 26938       if( pShmNode->h<0 ){
 26939         rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
 26940         goto shm_open_err;
 26943       /* If this process is running as root, make sure that the SHM file
 26944       ** is owned by the same user that owns the original database.  Otherwise,
 26945       ** the original owner will not be able to connect.
 26946       */
 26947       osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
 26949       /* Check to see if another process is holding the dead-man switch.
 26950       ** If not, truncate the file to zero length. 
 26951       */
 26952       rc = SQLITE_OK;
 26953       if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
 26954         if( robust_ftruncate(pShmNode->h, 0) ){
 26955           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
 26958       if( rc==SQLITE_OK ){
 26959         rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
 26961       if( rc ) goto shm_open_err;
 26965   /* Make the new connection a child of the unixShmNode */
 26966   p->pShmNode = pShmNode;
 26967 #ifdef SQLITE_DEBUG
 26968   p->id = pShmNode->nextShmId++;
 26969 #endif
 26970   pShmNode->nRef++;
 26971   pDbFd->pShm = p;
 26972   unixLeaveMutex();
 26974   /* The reference count on pShmNode has already been incremented under
 26975   ** the cover of the unixEnterMutex() mutex and the pointer from the
 26976   ** new (struct unixShm) object to the pShmNode has been set. All that is
 26977   ** left to do is to link the new object into the linked list starting
 26978   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 26979   ** mutex.
 26980   */
 26981   sqlite3_mutex_enter(pShmNode->mutex);
 26982   p->pNext = pShmNode->pFirst;
 26983   pShmNode->pFirst = p;
 26984   sqlite3_mutex_leave(pShmNode->mutex);
 26985   return SQLITE_OK;
 26987   /* Jump here on any error */
 26988 shm_open_err:
 26989   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
 26990   sqlite3_free(p);
 26991   unixLeaveMutex();
 26992   return rc;
 26995 /*
 26996 ** This function is called to obtain a pointer to region iRegion of the 
 26997 ** shared-memory associated with the database file fd. Shared-memory regions 
 26998 ** are numbered starting from zero. Each shared-memory region is szRegion 
 26999 ** bytes in size.
 27000 **
 27001 ** If an error occurs, an error code is returned and *pp is set to NULL.
 27002 **
 27003 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
 27004 ** region has not been allocated (by any client, including one running in a
 27005 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
 27006 ** bExtend is non-zero and the requested shared-memory region has not yet 
 27007 ** been allocated, it is allocated by this function.
 27008 **
 27009 ** If the shared-memory region has already been allocated or is allocated by
 27010 ** this call as described above, then it is mapped into this processes 
 27011 ** address space (if it is not already), *pp is set to point to the mapped 
 27012 ** memory and SQLITE_OK returned.
 27013 */
 27014 static int unixShmMap(
 27015   sqlite3_file *fd,               /* Handle open on database file */
 27016   int iRegion,                    /* Region to retrieve */
 27017   int szRegion,                   /* Size of regions */
 27018   int bExtend,                    /* True to extend file if necessary */
 27019   void volatile **pp              /* OUT: Mapped memory */
 27020 ){
 27021   unixFile *pDbFd = (unixFile*)fd;
 27022   unixShm *p;
 27023   unixShmNode *pShmNode;
 27024   int rc = SQLITE_OK;
 27026   /* If the shared-memory file has not yet been opened, open it now. */
 27027   if( pDbFd->pShm==0 ){
 27028     rc = unixOpenSharedMemory(pDbFd);
 27029     if( rc!=SQLITE_OK ) return rc;
 27032   p = pDbFd->pShm;
 27033   pShmNode = p->pShmNode;
 27034   sqlite3_mutex_enter(pShmNode->mutex);
 27035   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 27036   assert( pShmNode->pInode==pDbFd->pInode );
 27037   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 27038   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 27040   if( pShmNode->nRegion<=iRegion ){
 27041     char **apNew;                      /* New apRegion[] array */
 27042     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 27043     struct stat sStat;                 /* Used by fstat() */
 27045     pShmNode->szRegion = szRegion;
 27047     if( pShmNode->h>=0 ){
 27048       /* The requested region is not mapped into this processes address space.
 27049       ** Check to see if it has been allocated (i.e. if the wal-index file is
 27050       ** large enough to contain the requested region).
 27051       */
 27052       if( osFstat(pShmNode->h, &sStat) ){
 27053         rc = SQLITE_IOERR_SHMSIZE;
 27054         goto shmpage_out;
 27057       if( sStat.st_size<nByte ){
 27058         /* The requested memory region does not exist. If bExtend is set to
 27059         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
 27060         **
 27061         ** Alternatively, if bExtend is true, use ftruncate() to allocate
 27062         ** the requested memory region.
 27063         */
 27064         if( !bExtend ) goto shmpage_out;
 27065 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 27066         if( osFallocate(pShmNode->h, sStat.st_size, nByte)!=0 ){
 27067           rc = unixLogError(SQLITE_IOERR_SHMSIZE, "fallocate",
 27068                             pShmNode->zFilename);
 27069           goto shmpage_out;
 27071 #else
 27072         if( robust_ftruncate(pShmNode->h, nByte) ){
 27073           rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
 27074                             pShmNode->zFilename);
 27075           goto shmpage_out;
 27077 #endif
 27081     /* Map the requested memory region into this processes address space. */
 27082     apNew = (char **)sqlite3_realloc(
 27083         pShmNode->apRegion, (iRegion+1)*sizeof(char *)
 27084     );
 27085     if( !apNew ){
 27086       rc = SQLITE_IOERR_NOMEM;
 27087       goto shmpage_out;
 27089     pShmNode->apRegion = apNew;
 27090     while(pShmNode->nRegion<=iRegion){
 27091       void *pMem;
 27092       if( pShmNode->h>=0 ){
 27093         pMem = mmap(0, szRegion,
 27094             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
 27095             MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
 27096         );
 27097         if( pMem==MAP_FAILED ){
 27098           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 27099           goto shmpage_out;
 27101       }else{
 27102         pMem = sqlite3_malloc(szRegion);
 27103         if( pMem==0 ){
 27104           rc = SQLITE_NOMEM;
 27105           goto shmpage_out;
 27107         memset(pMem, 0, szRegion);
 27109       pShmNode->apRegion[pShmNode->nRegion] = pMem;
 27110       pShmNode->nRegion++;
 27114 shmpage_out:
 27115   if( pShmNode->nRegion>iRegion ){
 27116     *pp = pShmNode->apRegion[iRegion];
 27117   }else{
 27118     *pp = 0;
 27120   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 27121   sqlite3_mutex_leave(pShmNode->mutex);
 27122   return rc;
 27125 /*
 27126 ** Change the lock state for a shared-memory segment.
 27127 **
 27128 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
 27129 ** different here than in posix.  In xShmLock(), one can go from unlocked
 27130 ** to shared and back or from unlocked to exclusive and back.  But one may
 27131 ** not go from shared to exclusive or from exclusive to shared.
 27132 */
 27133 static int unixShmLock(
 27134   sqlite3_file *fd,          /* Database file holding the shared memory */
 27135   int ofst,                  /* First lock to acquire or release */
 27136   int n,                     /* Number of locks to acquire or release */
 27137   int flags                  /* What to do with the lock */
 27138 ){
 27139   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
 27140   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
 27141   unixShm *pX;                          /* For looping over all siblings */
 27142   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
 27143   int rc = SQLITE_OK;                   /* Result code */
 27144   u16 mask;                             /* Mask of locks to take or release */
 27146   assert( pShmNode==pDbFd->pInode->pShmNode );
 27147   assert( pShmNode->pInode==pDbFd->pInode );
 27148   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 27149   assert( n>=1 );
 27150   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 27151        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 27152        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 27153        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 27154   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 27155   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 27156   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 27158   mask = (1<<(ofst+n)) - (1<<ofst);
 27159   assert( n>1 || mask==(1<<ofst) );
 27160   sqlite3_mutex_enter(pShmNode->mutex);
 27161   if( flags & SQLITE_SHM_UNLOCK ){
 27162     u16 allMask = 0; /* Mask of locks held by siblings */
 27164     /* See if any siblings hold this same lock */
 27165     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 27166       if( pX==p ) continue;
 27167       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 27168       allMask |= pX->sharedMask;
 27171     /* Unlock the system-level locks */
 27172     if( (mask & allMask)==0 ){
 27173       rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
 27174     }else{
 27175       rc = SQLITE_OK;
 27178     /* Undo the local locks */
 27179     if( rc==SQLITE_OK ){
 27180       p->exclMask &= ~mask;
 27181       p->sharedMask &= ~mask;
 27183   }else if( flags & SQLITE_SHM_SHARED ){
 27184     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
 27186     /* Find out which shared locks are already held by sibling connections.
 27187     ** If any sibling already holds an exclusive lock, go ahead and return
 27188     ** SQLITE_BUSY.
 27189     */
 27190     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 27191       if( (pX->exclMask & mask)!=0 ){
 27192         rc = SQLITE_BUSY;
 27193         break;
 27195       allShared |= pX->sharedMask;
 27198     /* Get shared locks at the system level, if necessary */
 27199     if( rc==SQLITE_OK ){
 27200       if( (allShared & mask)==0 ){
 27201         rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
 27202       }else{
 27203         rc = SQLITE_OK;
 27207     /* Get the local shared locks */
 27208     if( rc==SQLITE_OK ){
 27209       p->sharedMask |= mask;
 27211   }else{
 27212     /* Make sure no sibling connections hold locks that will block this
 27213     ** lock.  If any do, return SQLITE_BUSY right away.
 27214     */
 27215     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 27216       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
 27217         rc = SQLITE_BUSY;
 27218         break;
 27222     /* Get the exclusive locks at the system level.  Then if successful
 27223     ** also mark the local connection as being locked.
 27224     */
 27225     if( rc==SQLITE_OK ){
 27226       rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
 27227       if( rc==SQLITE_OK ){
 27228         assert( (p->sharedMask & mask)==0 );
 27229         p->exclMask |= mask;
 27233   sqlite3_mutex_leave(pShmNode->mutex);
 27234   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
 27235            p->id, getpid(), p->sharedMask, p->exclMask));
 27236   return rc;
 27239 /*
 27240 ** Implement a memory barrier or memory fence on shared memory.  
 27241 **
 27242 ** All loads and stores begun before the barrier must complete before
 27243 ** any load or store begun after the barrier.
 27244 */
 27245 static void unixShmBarrier(
 27246   sqlite3_file *fd                /* Database file holding the shared memory */
 27247 ){
 27248   UNUSED_PARAMETER(fd);
 27249   unixEnterMutex();
 27250   unixLeaveMutex();
 27253 /*
 27254 ** Close a connection to shared-memory.  Delete the underlying 
 27255 ** storage if deleteFlag is true.
 27256 **
 27257 ** If there is no shared memory associated with the connection then this
 27258 ** routine is a harmless no-op.
 27259 */
 27260 static int unixShmUnmap(
 27261   sqlite3_file *fd,               /* The underlying database file */
 27262   int deleteFlag                  /* Delete shared-memory if true */
 27263 ){
 27264   unixShm *p;                     /* The connection to be closed */
 27265   unixShmNode *pShmNode;          /* The underlying shared-memory file */
 27266   unixShm **pp;                   /* For looping over sibling connections */
 27267   unixFile *pDbFd;                /* The underlying database file */
 27269   pDbFd = (unixFile*)fd;
 27270   p = pDbFd->pShm;
 27271   if( p==0 ) return SQLITE_OK;
 27272   pShmNode = p->pShmNode;
 27274   assert( pShmNode==pDbFd->pInode->pShmNode );
 27275   assert( pShmNode->pInode==pDbFd->pInode );
 27277   /* Remove connection p from the set of connections associated
 27278   ** with pShmNode */
 27279   sqlite3_mutex_enter(pShmNode->mutex);
 27280   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 27281   *pp = p->pNext;
 27283   /* Free the connection p */
 27284   sqlite3_free(p);
 27285   pDbFd->pShm = 0;
 27286   sqlite3_mutex_leave(pShmNode->mutex);
 27288   /* If pShmNode->nRef has reached 0, then close the underlying
 27289   ** shared-memory file, too */
 27290   unixEnterMutex();
 27291   assert( pShmNode->nRef>0 );
 27292   pShmNode->nRef--;
 27293   if( pShmNode->nRef==0 ){
 27294     if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
 27295     unixShmPurge(pDbFd);
 27297   unixLeaveMutex();
 27299   return SQLITE_OK;
 27303 #else
 27304 # define unixShmMap     0
 27305 # define unixShmLock    0
 27306 # define unixShmBarrier 0
 27307 # define unixShmUnmap   0
 27308 #endif /* #ifndef SQLITE_OMIT_WAL */
 27310 /*
 27311 ** Here ends the implementation of all sqlite3_file methods.
 27312 **
 27313 ********************** End sqlite3_file Methods *******************************
 27314 ******************************************************************************/
 27316 /*
 27317 ** This division contains definitions of sqlite3_io_methods objects that
 27318 ** implement various file locking strategies.  It also contains definitions
 27319 ** of "finder" functions.  A finder-function is used to locate the appropriate
 27320 ** sqlite3_io_methods object for a particular database file.  The pAppData
 27321 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
 27322 ** the correct finder-function for that VFS.
 27323 **
 27324 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
 27325 ** object.  The only interesting finder-function is autolockIoFinder, which
 27326 ** looks at the filesystem type and tries to guess the best locking
 27327 ** strategy from that.
 27328 **
 27329 ** For finder-funtion F, two objects are created:
 27330 **
 27331 **    (1) The real finder-function named "FImpt()".
 27332 **
 27333 **    (2) A constant pointer to this function named just "F".
 27334 **
 27335 **
 27336 ** A pointer to the F pointer is used as the pAppData value for VFS
 27337 ** objects.  We have to do this instead of letting pAppData point
 27338 ** directly at the finder-function since C90 rules prevent a void*
 27339 ** from be cast into a function pointer.
 27340 **
 27341 **
 27342 ** Each instance of this macro generates two objects:
 27343 **
 27344 **   *  A constant sqlite3_io_methods object call METHOD that has locking
 27345 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
 27346 **
 27347 **   *  An I/O method finder function called FINDER that returns a pointer
 27348 **      to the METHOD object in the previous bullet.
 27349 */
 27350 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
 27351 static const sqlite3_io_methods METHOD = {                                   \
 27352    VERSION,                    /* iVersion */                                \
 27353    CLOSE,                      /* xClose */                                  \
 27354    unixRead,                   /* xRead */                                   \
 27355    unixWrite,                  /* xWrite */                                  \
 27356    unixTruncate,               /* xTruncate */                               \
 27357    unixSync,                   /* xSync */                                   \
 27358    unixFileSize,               /* xFileSize */                               \
 27359    LOCK,                       /* xLock */                                   \
 27360    UNLOCK,                     /* xUnlock */                                 \
 27361    CKLOCK,                     /* xCheckReservedLock */                      \
 27362    unixFileControl,            /* xFileControl */                            \
 27363    unixSectorSize,             /* xSectorSize */                             \
 27364    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
 27365    unixShmMap,                 /* xShmMap */                                 \
 27366    unixShmLock,                /* xShmLock */                                \
 27367    unixShmBarrier,             /* xShmBarrier */                             \
 27368    unixShmUnmap                /* xShmUnmap */                               \
 27369 };                                                                           \
 27370 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
 27371   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
 27372   return &METHOD;                                                            \
 27373 }                                                                            \
 27374 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
 27375     = FINDER##Impl;
 27377 /*
 27378 ** Here are all of the sqlite3_io_methods objects for each of the
 27379 ** locking strategies.  Functions that return pointers to these methods
 27380 ** are also created.
 27381 */
 27382 IOMETHODS(
 27383   posixIoFinder,            /* Finder function name */
 27384   posixIoMethods,           /* sqlite3_io_methods object name */
 27385   2,                        /* shared memory is enabled */
 27386   unixClose,                /* xClose method */
 27387   unixLock,                 /* xLock method */
 27388   unixUnlock,               /* xUnlock method */
 27389   unixCheckReservedLock     /* xCheckReservedLock method */
 27391 IOMETHODS(
 27392   nolockIoFinder,           /* Finder function name */
 27393   nolockIoMethods,          /* sqlite3_io_methods object name */
 27394   1,                        /* shared memory is disabled */
 27395   nolockClose,              /* xClose method */
 27396   nolockLock,               /* xLock method */
 27397   nolockUnlock,             /* xUnlock method */
 27398   nolockCheckReservedLock   /* xCheckReservedLock method */
 27400 IOMETHODS(
 27401   dotlockIoFinder,          /* Finder function name */
 27402   dotlockIoMethods,         /* sqlite3_io_methods object name */
 27403   1,                        /* shared memory is disabled */
 27404   dotlockClose,             /* xClose method */
 27405   dotlockLock,              /* xLock method */
 27406   dotlockUnlock,            /* xUnlock method */
 27407   dotlockCheckReservedLock  /* xCheckReservedLock method */
 27410 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
 27411 IOMETHODS(
 27412   flockIoFinder,            /* Finder function name */
 27413   flockIoMethods,           /* sqlite3_io_methods object name */
 27414   1,                        /* shared memory is disabled */
 27415   flockClose,               /* xClose method */
 27416   flockLock,                /* xLock method */
 27417   flockUnlock,              /* xUnlock method */
 27418   flockCheckReservedLock    /* xCheckReservedLock method */
 27420 #endif
 27422 #if OS_VXWORKS
 27423 IOMETHODS(
 27424   semIoFinder,              /* Finder function name */
 27425   semIoMethods,             /* sqlite3_io_methods object name */
 27426   1,                        /* shared memory is disabled */
 27427   semClose,                 /* xClose method */
 27428   semLock,                  /* xLock method */
 27429   semUnlock,                /* xUnlock method */
 27430   semCheckReservedLock      /* xCheckReservedLock method */
 27432 #endif
 27434 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 27435 IOMETHODS(
 27436   afpIoFinder,              /* Finder function name */
 27437   afpIoMethods,             /* sqlite3_io_methods object name */
 27438   1,                        /* shared memory is disabled */
 27439   afpClose,                 /* xClose method */
 27440   afpLock,                  /* xLock method */
 27441   afpUnlock,                /* xUnlock method */
 27442   afpCheckReservedLock      /* xCheckReservedLock method */
 27444 #endif
 27446 /*
 27447 ** The proxy locking method is a "super-method" in the sense that it
 27448 ** opens secondary file descriptors for the conch and lock files and
 27449 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
 27450 ** secondary files.  For this reason, the division that implements
 27451 ** proxy locking is located much further down in the file.  But we need
 27452 ** to go ahead and define the sqlite3_io_methods and finder function
 27453 ** for proxy locking here.  So we forward declare the I/O methods.
 27454 */
 27455 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 27456 static int proxyClose(sqlite3_file*);
 27457 static int proxyLock(sqlite3_file*, int);
 27458 static int proxyUnlock(sqlite3_file*, int);
 27459 static int proxyCheckReservedLock(sqlite3_file*, int*);
 27460 IOMETHODS(
 27461   proxyIoFinder,            /* Finder function name */
 27462   proxyIoMethods,           /* sqlite3_io_methods object name */
 27463   1,                        /* shared memory is disabled */
 27464   proxyClose,               /* xClose method */
 27465   proxyLock,                /* xLock method */
 27466   proxyUnlock,              /* xUnlock method */
 27467   proxyCheckReservedLock    /* xCheckReservedLock method */
 27469 #endif
 27471 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
 27472 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 27473 IOMETHODS(
 27474   nfsIoFinder,               /* Finder function name */
 27475   nfsIoMethods,              /* sqlite3_io_methods object name */
 27476   1,                         /* shared memory is disabled */
 27477   unixClose,                 /* xClose method */
 27478   unixLock,                  /* xLock method */
 27479   nfsUnlock,                 /* xUnlock method */
 27480   unixCheckReservedLock      /* xCheckReservedLock method */
 27482 #endif
 27484 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 27485 /* 
 27486 ** This "finder" function attempts to determine the best locking strategy 
 27487 ** for the database file "filePath".  It then returns the sqlite3_io_methods
 27488 ** object that implements that strategy.
 27489 **
 27490 ** This is for MacOSX only.
 27491 */
 27492 static const sqlite3_io_methods *autolockIoFinderImpl(
 27493   const char *filePath,    /* name of the database file */
 27494   unixFile *pNew           /* open file object for the database file */
 27495 ){
 27496   static const struct Mapping {
 27497     const char *zFilesystem;              /* Filesystem type name */
 27498     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
 27499   } aMap[] = {
 27500     { "hfs",    &posixIoMethods },
 27501     { "ufs",    &posixIoMethods },
 27502     { "afpfs",  &afpIoMethods },
 27503     { "smbfs",  &afpIoMethods },
 27504     { "webdav", &nolockIoMethods },
 27505     { 0, 0 }
 27506   };
 27507   int i;
 27508   struct statfs fsInfo;
 27509   struct flock lockInfo;
 27511   if( !filePath ){
 27512     /* If filePath==NULL that means we are dealing with a transient file
 27513     ** that does not need to be locked. */
 27514     return &nolockIoMethods;
 27516   if( statfs(filePath, &fsInfo) != -1 ){
 27517     if( fsInfo.f_flags & MNT_RDONLY ){
 27518       return &nolockIoMethods;
 27520     for(i=0; aMap[i].zFilesystem; i++){
 27521       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
 27522         return aMap[i].pMethods;
 27527   /* Default case. Handles, amongst others, "nfs".
 27528   ** Test byte-range lock using fcntl(). If the call succeeds, 
 27529   ** assume that the file-system supports POSIX style locks. 
 27530   */
 27531   lockInfo.l_len = 1;
 27532   lockInfo.l_start = 0;
 27533   lockInfo.l_whence = SEEK_SET;
 27534   lockInfo.l_type = F_RDLCK;
 27535   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 27536     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
 27537       return &nfsIoMethods;
 27538     } else {
 27539       return &posixIoMethods;
 27541   }else{
 27542     return &dotlockIoMethods;
 27545 static const sqlite3_io_methods 
 27546   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
 27548 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 27550 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
 27551 /* 
 27552 ** This "finder" function attempts to determine the best locking strategy 
 27553 ** for the database file "filePath".  It then returns the sqlite3_io_methods
 27554 ** object that implements that strategy.
 27555 **
 27556 ** This is for VXWorks only.
 27557 */
 27558 static const sqlite3_io_methods *autolockIoFinderImpl(
 27559   const char *filePath,    /* name of the database file */
 27560   unixFile *pNew           /* the open file object */
 27561 ){
 27562   struct flock lockInfo;
 27564   if( !filePath ){
 27565     /* If filePath==NULL that means we are dealing with a transient file
 27566     ** that does not need to be locked. */
 27567     return &nolockIoMethods;
 27570   /* Test if fcntl() is supported and use POSIX style locks.
 27571   ** Otherwise fall back to the named semaphore method.
 27572   */
 27573   lockInfo.l_len = 1;
 27574   lockInfo.l_start = 0;
 27575   lockInfo.l_whence = SEEK_SET;
 27576   lockInfo.l_type = F_RDLCK;
 27577   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 27578     return &posixIoMethods;
 27579   }else{
 27580     return &semIoMethods;
 27583 static const sqlite3_io_methods 
 27584   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
 27586 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
 27588 /*
 27589 ** An abstract type for a pointer to a IO method finder function:
 27590 */
 27591 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
 27594 /****************************************************************************
 27595 **************************** sqlite3_vfs methods ****************************
 27596 **
 27597 ** This division contains the implementation of methods on the
 27598 ** sqlite3_vfs object.
 27599 */
 27601 /*
 27602 ** Initialize the contents of the unixFile structure pointed to by pId.
 27603 */
 27604 static int fillInUnixFile(
 27605   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
 27606   int h,                  /* Open file descriptor of file being opened */
 27607   sqlite3_file *pId,      /* Write to the unixFile structure here */
 27608   const char *zFilename,  /* Name of the file being opened */
 27609   int ctrlFlags           /* Zero or more UNIXFILE_* values */
 27610 ){
 27611   const sqlite3_io_methods *pLockingStyle;
 27612   unixFile *pNew = (unixFile *)pId;
 27613   int rc = SQLITE_OK;
 27615   assert( pNew->pInode==NULL );
 27617   /* Usually the path zFilename should not be a relative pathname. The
 27618   ** exception is when opening the proxy "conch" file in builds that
 27619   ** include the special Apple locking styles.
 27620   */
 27621 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 27622   assert( zFilename==0 || zFilename[0]=='/' 
 27623     || pVfs->pAppData==(void*)&autolockIoFinder );
 27624 #else
 27625   assert( zFilename==0 || zFilename[0]=='/' );
 27626 #endif
 27628   /* No locking occurs in temporary files */
 27629   assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
 27631   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
 27632   pNew->h = h;
 27633   pNew->pVfs = pVfs;
 27634   pNew->zPath = zFilename;
 27635   pNew->ctrlFlags = (u8)ctrlFlags;
 27636   if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
 27637                            "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 27638     pNew->ctrlFlags |= UNIXFILE_PSOW;
 27640   if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
 27641     pNew->ctrlFlags |= UNIXFILE_EXCL;
 27644 #if OS_VXWORKS
 27645   pNew->pId = vxworksFindFileId(zFilename);
 27646   if( pNew->pId==0 ){
 27647     ctrlFlags |= UNIXFILE_NOLOCK;
 27648     rc = SQLITE_NOMEM;
 27650 #endif
 27652   if( ctrlFlags & UNIXFILE_NOLOCK ){
 27653     pLockingStyle = &nolockIoMethods;
 27654   }else{
 27655     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
 27656 #if SQLITE_ENABLE_LOCKING_STYLE
 27657     /* Cache zFilename in the locking context (AFP and dotlock override) for
 27658     ** proxyLock activation is possible (remote proxy is based on db name)
 27659     ** zFilename remains valid until file is closed, to support */
 27660     pNew->lockingContext = (void*)zFilename;
 27661 #endif
 27664   if( pLockingStyle == &posixIoMethods
 27665 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 27666     || pLockingStyle == &nfsIoMethods
 27667 #endif
 27668   ){
 27669     unixEnterMutex();
 27670     rc = findInodeInfo(pNew, &pNew->pInode);
 27671     if( rc!=SQLITE_OK ){
 27672       /* If an error occured in findInodeInfo(), close the file descriptor
 27673       ** immediately, before releasing the mutex. findInodeInfo() may fail
 27674       ** in two scenarios:
 27675       **
 27676       **   (a) A call to fstat() failed.
 27677       **   (b) A malloc failed.
 27678       **
 27679       ** Scenario (b) may only occur if the process is holding no other
 27680       ** file descriptors open on the same file. If there were other file
 27681       ** descriptors on this file, then no malloc would be required by
 27682       ** findInodeInfo(). If this is the case, it is quite safe to close
 27683       ** handle h - as it is guaranteed that no posix locks will be released
 27684       ** by doing so.
 27685       **
 27686       ** If scenario (a) caused the error then things are not so safe. The
 27687       ** implicit assumption here is that if fstat() fails, things are in
 27688       ** such bad shape that dropping a lock or two doesn't matter much.
 27689       */
 27690       robust_close(pNew, h, __LINE__);
 27691       h = -1;
 27693     unixLeaveMutex();
 27696 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 27697   else if( pLockingStyle == &afpIoMethods ){
 27698     /* AFP locking uses the file path so it needs to be included in
 27699     ** the afpLockingContext.
 27700     */
 27701     afpLockingContext *pCtx;
 27702     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
 27703     if( pCtx==0 ){
 27704       rc = SQLITE_NOMEM;
 27705     }else{
 27706       /* NB: zFilename exists and remains valid until the file is closed
 27707       ** according to requirement F11141.  So we do not need to make a
 27708       ** copy of the filename. */
 27709       pCtx->dbPath = zFilename;
 27710       pCtx->reserved = 0;
 27711       srandomdev();
 27712       unixEnterMutex();
 27713       rc = findInodeInfo(pNew, &pNew->pInode);
 27714       if( rc!=SQLITE_OK ){
 27715         sqlite3_free(pNew->lockingContext);
 27716         robust_close(pNew, h, __LINE__);
 27717         h = -1;
 27719       unixLeaveMutex();        
 27722 #endif
 27724   else if( pLockingStyle == &dotlockIoMethods ){
 27725     /* Dotfile locking uses the file path so it needs to be included in
 27726     ** the dotlockLockingContext 
 27727     */
 27728     char *zLockFile;
 27729     int nFilename;
 27730     assert( zFilename!=0 );
 27731     nFilename = (int)strlen(zFilename) + 6;
 27732     zLockFile = (char *)sqlite3_malloc(nFilename);
 27733     if( zLockFile==0 ){
 27734       rc = SQLITE_NOMEM;
 27735     }else{
 27736       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
 27738     pNew->lockingContext = zLockFile;
 27741 #if OS_VXWORKS
 27742   else if( pLockingStyle == &semIoMethods ){
 27743     /* Named semaphore locking uses the file path so it needs to be
 27744     ** included in the semLockingContext
 27745     */
 27746     unixEnterMutex();
 27747     rc = findInodeInfo(pNew, &pNew->pInode);
 27748     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
 27749       char *zSemName = pNew->pInode->aSemName;
 27750       int n;
 27751       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
 27752                        pNew->pId->zCanonicalName);
 27753       for( n=1; zSemName[n]; n++ )
 27754         if( zSemName[n]=='/' ) zSemName[n] = '_';
 27755       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
 27756       if( pNew->pInode->pSem == SEM_FAILED ){
 27757         rc = SQLITE_NOMEM;
 27758         pNew->pInode->aSemName[0] = '\0';
 27761     unixLeaveMutex();
 27763 #endif
 27765   pNew->lastErrno = 0;
 27766 #if OS_VXWORKS
 27767   if( rc!=SQLITE_OK ){
 27768     if( h>=0 ) robust_close(pNew, h, __LINE__);
 27769     h = -1;
 27770     osUnlink(zFilename);
 27771     isDelete = 0;
 27773   if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
 27774 #endif
 27775   if( rc!=SQLITE_OK ){
 27776     if( h>=0 ) robust_close(pNew, h, __LINE__);
 27777   }else{
 27778     pNew->pMethod = pLockingStyle;
 27779     OpenCounter(+1);
 27781   return rc;
 27784 /*
 27785 ** Return the name of a directory in which to put temporary files.
 27786 ** If no suitable temporary file directory can be found, return NULL.
 27787 */
 27788 static const char *unixTempFileDir(void){
 27789   static const char *azDirs[] = {
 27790      0,
 27791      0,
 27792      "/var/tmp",
 27793      "/usr/tmp",
 27794      "/tmp",
 27795      0        /* List terminator */
 27796   };
 27797   unsigned int i;
 27798   struct stat buf;
 27799   const char *zDir = 0;
 27801   azDirs[0] = sqlite3_temp_directory;
 27802   if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
 27803   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
 27804     if( zDir==0 ) continue;
 27805     if( osStat(zDir, &buf) ) continue;
 27806     if( !S_ISDIR(buf.st_mode) ) continue;
 27807     if( osAccess(zDir, 07) ) continue;
 27808     break;
 27810   return zDir;
 27813 /*
 27814 ** Create a temporary file name in zBuf.  zBuf must be allocated
 27815 ** by the calling process and must be big enough to hold at least
 27816 ** pVfs->mxPathname bytes.
 27817 */
 27818 static int unixGetTempname(int nBuf, char *zBuf){
 27819   static const unsigned char zChars[] =
 27820     "abcdefghijklmnopqrstuvwxyz"
 27821     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 27822     "0123456789";
 27823   unsigned int i, j;
 27824   const char *zDir;
 27826   /* It's odd to simulate an io-error here, but really this is just
 27827   ** using the io-error infrastructure to test that SQLite handles this
 27828   ** function failing. 
 27829   */
 27830   SimulateIOError( return SQLITE_IOERR );
 27832   zDir = unixTempFileDir();
 27833   if( zDir==0 ) zDir = ".";
 27835   /* Check that the output buffer is large enough for the temporary file 
 27836   ** name. If it is not, return SQLITE_ERROR.
 27837   */
 27838   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
 27839     return SQLITE_ERROR;
 27842   do{
 27843     sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
 27844     j = (int)strlen(zBuf);
 27845     sqlite3_randomness(15, &zBuf[j]);
 27846     for(i=0; i<15; i++, j++){
 27847       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 27849     zBuf[j] = 0;
 27850     zBuf[j+1] = 0;
 27851   }while( osAccess(zBuf,0)==0 );
 27852   return SQLITE_OK;
 27855 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 27856 /*
 27857 ** Routine to transform a unixFile into a proxy-locking unixFile.
 27858 ** Implementation in the proxy-lock division, but used by unixOpen()
 27859 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
 27860 */
 27861 static int proxyTransformUnixFile(unixFile*, const char*);
 27862 #endif
 27864 /*
 27865 ** Search for an unused file descriptor that was opened on the database 
 27866 ** file (not a journal or master-journal file) identified by pathname
 27867 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
 27868 ** argument to this function.
 27869 **
 27870 ** Such a file descriptor may exist if a database connection was closed
 27871 ** but the associated file descriptor could not be closed because some
 27872 ** other file descriptor open on the same file is holding a file-lock.
 27873 ** Refer to comments in the unixClose() function and the lengthy comment
 27874 ** describing "Posix Advisory Locking" at the start of this file for 
 27875 ** further details. Also, ticket #4018.
 27876 **
 27877 ** If a suitable file descriptor is found, then it is returned. If no
 27878 ** such file descriptor is located, -1 is returned.
 27879 */
 27880 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
 27881   UnixUnusedFd *pUnused = 0;
 27883   /* Do not search for an unused file descriptor on vxworks. Not because
 27884   ** vxworks would not benefit from the change (it might, we're not sure),
 27885   ** but because no way to test it is currently available. It is better 
 27886   ** not to risk breaking vxworks support for the sake of such an obscure 
 27887   ** feature.  */
 27888 #if !OS_VXWORKS
 27889   struct stat sStat;                   /* Results of stat() call */
 27891   /* A stat() call may fail for various reasons. If this happens, it is
 27892   ** almost certain that an open() call on the same path will also fail.
 27893   ** For this reason, if an error occurs in the stat() call here, it is
 27894   ** ignored and -1 is returned. The caller will try to open a new file
 27895   ** descriptor on the same path, fail, and return an error to SQLite.
 27896   **
 27897   ** Even if a subsequent open() call does succeed, the consequences of
 27898   ** not searching for a resusable file descriptor are not dire.  */
 27899   if( 0==osStat(zPath, &sStat) ){
 27900     unixInodeInfo *pInode;
 27902     unixEnterMutex();
 27903     pInode = inodeList;
 27904     while( pInode && (pInode->fileId.dev!=sStat.st_dev
 27905                      || pInode->fileId.ino!=sStat.st_ino) ){
 27906        pInode = pInode->pNext;
 27908     if( pInode ){
 27909       UnixUnusedFd **pp;
 27910       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
 27911       pUnused = *pp;
 27912       if( pUnused ){
 27913         *pp = pUnused->pNext;
 27916     unixLeaveMutex();
 27918 #endif    /* if !OS_VXWORKS */
 27919   return pUnused;
 27922 /*
 27923 ** This function is called by unixOpen() to determine the unix permissions
 27924 ** to create new files with. If no error occurs, then SQLITE_OK is returned
 27925 ** and a value suitable for passing as the third argument to open(2) is
 27926 ** written to *pMode. If an IO error occurs, an SQLite error code is 
 27927 ** returned and the value of *pMode is not modified.
 27928 **
 27929 ** In most cases cases, this routine sets *pMode to 0, which will become
 27930 ** an indication to robust_open() to create the file using
 27931 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
 27932 ** But if the file being opened is a WAL or regular journal file, then 
 27933 ** this function queries the file-system for the permissions on the 
 27934 ** corresponding database file and sets *pMode to this value. Whenever 
 27935 ** possible, WAL and journal files are created using the same permissions 
 27936 ** as the associated database file.
 27937 **
 27938 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
 27939 ** original filename is unavailable.  But 8_3_NAMES is only used for
 27940 ** FAT filesystems and permissions do not matter there, so just use
 27941 ** the default permissions.
 27942 */
 27943 static int findCreateFileMode(
 27944   const char *zPath,              /* Path of file (possibly) being created */
 27945   int flags,                      /* Flags passed as 4th argument to xOpen() */
 27946   mode_t *pMode,                  /* OUT: Permissions to open file with */
 27947   uid_t *pUid,                    /* OUT: uid to set on the file */
 27948   gid_t *pGid                     /* OUT: gid to set on the file */
 27949 ){
 27950   int rc = SQLITE_OK;             /* Return Code */
 27951   *pMode = 0;
 27952   *pUid = 0;
 27953   *pGid = 0;
 27954   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 27955     char zDb[MAX_PATHNAME+1];     /* Database file path */
 27956     int nDb;                      /* Number of valid bytes in zDb */
 27957     struct stat sStat;            /* Output of stat() on database file */
 27959     /* zPath is a path to a WAL or journal file. The following block derives
 27960     ** the path to the associated database file from zPath. This block handles
 27961     ** the following naming conventions:
 27962     **
 27963     **   "<path to db>-journal"
 27964     **   "<path to db>-wal"
 27965     **   "<path to db>-journalNN"
 27966     **   "<path to db>-walNN"
 27967     **
 27968     ** where NN is a decimal number. The NN naming schemes are 
 27969     ** used by the test_multiplex.c module.
 27970     */
 27971     nDb = sqlite3Strlen30(zPath) - 1; 
 27972 #ifdef SQLITE_ENABLE_8_3_NAMES
 27973     while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
 27974     if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
 27975 #else
 27976     while( zPath[nDb]!='-' ){
 27977       assert( nDb>0 );
 27978       assert( zPath[nDb]!='\n' );
 27979       nDb--;
 27981 #endif
 27982     memcpy(zDb, zPath, nDb);
 27983     zDb[nDb] = '\0';
 27985     if( 0==osStat(zDb, &sStat) ){
 27986       *pMode = sStat.st_mode & 0777;
 27987       *pUid = sStat.st_uid;
 27988       *pGid = sStat.st_gid;
 27989     }else{
 27990       rc = SQLITE_IOERR_FSTAT;
 27992   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
 27993     *pMode = 0600;
 27995   return rc;
 27998 /*
 27999 ** Open the file zPath.
 28000 ** 
 28001 ** Previously, the SQLite OS layer used three functions in place of this
 28002 ** one:
 28003 **
 28004 **     sqlite3OsOpenReadWrite();
 28005 **     sqlite3OsOpenReadOnly();
 28006 **     sqlite3OsOpenExclusive();
 28007 **
 28008 ** These calls correspond to the following combinations of flags:
 28009 **
 28010 **     ReadWrite() ->     (READWRITE | CREATE)
 28011 **     ReadOnly()  ->     (READONLY) 
 28012 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
 28013 **
 28014 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
 28015 ** true, the file was configured to be automatically deleted when the
 28016 ** file handle closed. To achieve the same effect using this new 
 28017 ** interface, add the DELETEONCLOSE flag to those specified above for 
 28018 ** OpenExclusive().
 28019 */
 28020 static int unixOpen(
 28021   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
 28022   const char *zPath,           /* Pathname of file to be opened */
 28023   sqlite3_file *pFile,         /* The file descriptor to be filled in */
 28024   int flags,                   /* Input flags to control the opening */
 28025   int *pOutFlags               /* Output flags returned to SQLite core */
 28026 ){
 28027   unixFile *p = (unixFile *)pFile;
 28028   int fd = -1;                   /* File descriptor returned by open() */
 28029   int openFlags = 0;             /* Flags to pass to open() */
 28030   int eType = flags&0xFFFFFF00;  /* Type of file to open */
 28031   int noLock;                    /* True to omit locking primitives */
 28032   int rc = SQLITE_OK;            /* Function Return Code */
 28033   int ctrlFlags = 0;             /* UNIXFILE_* flags */
 28035   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
 28036   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
 28037   int isCreate     = (flags & SQLITE_OPEN_CREATE);
 28038   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
 28039   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
 28040 #if SQLITE_ENABLE_LOCKING_STYLE
 28041   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
 28042 #endif
 28043 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 28044   struct statfs fsInfo;
 28045 #endif
 28047   /* If creating a master or main-file journal, this function will open
 28048   ** a file-descriptor on the directory too. The first time unixSync()
 28049   ** is called the directory file descriptor will be fsync()ed and close()d.
 28050   */
 28051   int syncDir = (isCreate && (
 28052         eType==SQLITE_OPEN_MASTER_JOURNAL 
 28053      || eType==SQLITE_OPEN_MAIN_JOURNAL 
 28054      || eType==SQLITE_OPEN_WAL
 28055   ));
 28057   /* If argument zPath is a NULL pointer, this function is required to open
 28058   ** a temporary file. Use this buffer to store the file name in.
 28059   */
 28060   char zTmpname[MAX_PATHNAME+2];
 28061   const char *zName = zPath;
 28063   /* Check the following statements are true: 
 28064   **
 28065   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
 28066   **   (b) if CREATE is set, then READWRITE must also be set, and
 28067   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
 28068   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
 28069   */
 28070   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
 28071   assert(isCreate==0 || isReadWrite);
 28072   assert(isExclusive==0 || isCreate);
 28073   assert(isDelete==0 || isCreate);
 28075   /* The main DB, main journal, WAL file and master journal are never 
 28076   ** automatically deleted. Nor are they ever temporary files.  */
 28077   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
 28078   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
 28079   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
 28080   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
 28082   /* Assert that the upper layer has set one of the "file-type" flags. */
 28083   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
 28084        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
 28085        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
 28086        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
 28087   );
 28089   memset(p, 0, sizeof(unixFile));
 28091   if( eType==SQLITE_OPEN_MAIN_DB ){
 28092     UnixUnusedFd *pUnused;
 28093     pUnused = findReusableFd(zName, flags);
 28094     if( pUnused ){
 28095       fd = pUnused->fd;
 28096     }else{
 28097       pUnused = sqlite3_malloc(sizeof(*pUnused));
 28098       if( !pUnused ){
 28099         return SQLITE_NOMEM;
 28102     p->pUnused = pUnused;
 28104     /* Database filenames are double-zero terminated if they are not
 28105     ** URIs with parameters.  Hence, they can always be passed into
 28106     ** sqlite3_uri_parameter(). */
 28107     assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
 28109   }else if( !zName ){
 28110     /* If zName is NULL, the upper layer is requesting a temp file. */
 28111     assert(isDelete && !syncDir);
 28112     rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
 28113     if( rc!=SQLITE_OK ){
 28114       return rc;
 28116     zName = zTmpname;
 28118     /* Generated temporary filenames are always double-zero terminated
 28119     ** for use by sqlite3_uri_parameter(). */
 28120     assert( zName[strlen(zName)+1]==0 );
 28123   /* Determine the value of the flags parameter passed to POSIX function
 28124   ** open(). These must be calculated even if open() is not called, as
 28125   ** they may be stored as part of the file handle and used by the 
 28126   ** 'conch file' locking functions later on.  */
 28127   if( isReadonly )  openFlags |= O_RDONLY;
 28128   if( isReadWrite ) openFlags |= O_RDWR;
 28129   if( isCreate )    openFlags |= O_CREAT;
 28130   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
 28131   openFlags |= (O_LARGEFILE|O_BINARY);
 28133   if( fd<0 ){
 28134     mode_t openMode;              /* Permissions to create file with */
 28135     uid_t uid;                    /* Userid for the file */
 28136     gid_t gid;                    /* Groupid for the file */
 28137     rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
 28138     if( rc!=SQLITE_OK ){
 28139       assert( !p->pUnused );
 28140       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
 28141       return rc;
 28143     fd = robust_open(zName, openFlags, openMode);
 28144     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
 28145     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
 28146       /* Failed to open the file for read/write access. Try read-only. */
 28147       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
 28148       openFlags &= ~(O_RDWR|O_CREAT);
 28149       flags |= SQLITE_OPEN_READONLY;
 28150       openFlags |= O_RDONLY;
 28151       isReadonly = 1;
 28152       fd = robust_open(zName, openFlags, openMode);
 28154     if( fd<0 ){
 28155       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
 28156       goto open_finished;
 28159     /* If this process is running as root and if creating a new rollback
 28160     ** journal or WAL file, set the ownership of the journal or WAL to be
 28161     ** the same as the original database.
 28162     */
 28163     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 28164       osFchown(fd, uid, gid);
 28167   assert( fd>=0 );
 28168   if( pOutFlags ){
 28169     *pOutFlags = flags;
 28172   if( p->pUnused ){
 28173     p->pUnused->fd = fd;
 28174     p->pUnused->flags = flags;
 28177   if( isDelete ){
 28178 #if OS_VXWORKS
 28179     zPath = zName;
 28180 #else
 28181     osUnlink(zName);
 28182 #endif
 28184 #if SQLITE_ENABLE_LOCKING_STYLE
 28185   else{
 28186     p->openFlags = openFlags;
 28188 #endif
 28190   noLock = eType!=SQLITE_OPEN_MAIN_DB;
 28193 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 28194   if( fstatfs(fd, &fsInfo) == -1 ){
 28195     ((unixFile*)pFile)->lastErrno = errno;
 28196     robust_close(p, fd, __LINE__);
 28197     return SQLITE_IOERR_ACCESS;
 28199   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
 28200     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
 28202 #endif
 28204   /* Set up appropriate ctrlFlags */
 28205   if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
 28206   if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
 28207   if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
 28208   if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
 28209   if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
 28211 #if SQLITE_ENABLE_LOCKING_STYLE
 28212 #if SQLITE_PREFER_PROXY_LOCKING
 28213   isAutoProxy = 1;
 28214 #endif
 28215   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
 28216     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
 28217     int useProxy = 0;
 28219     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
 28220     ** never use proxy, NULL means use proxy for non-local files only.  */
 28221     if( envforce!=NULL ){
 28222       useProxy = atoi(envforce)>0;
 28223     }else{
 28224       if( statfs(zPath, &fsInfo) == -1 ){
 28225         /* In theory, the close(fd) call is sub-optimal. If the file opened
 28226         ** with fd is a database file, and there are other connections open
 28227         ** on that file that are currently holding advisory locks on it,
 28228         ** then the call to close() will cancel those locks. In practice,
 28229         ** we're assuming that statfs() doesn't fail very often. At least
 28230         ** not while other file descriptors opened by the same process on
 28231         ** the same file are working.  */
 28232         p->lastErrno = errno;
 28233         robust_close(p, fd, __LINE__);
 28234         rc = SQLITE_IOERR_ACCESS;
 28235         goto open_finished;
 28237       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
 28239     if( useProxy ){
 28240       rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 28241       if( rc==SQLITE_OK ){
 28242         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
 28243         if( rc!=SQLITE_OK ){
 28244           /* Use unixClose to clean up the resources added in fillInUnixFile 
 28245           ** and clear all the structure's references.  Specifically, 
 28246           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op 
 28247           */
 28248           unixClose(pFile);
 28249           return rc;
 28252       goto open_finished;
 28255 #endif
 28257   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 28259 open_finished:
 28260   if( rc!=SQLITE_OK ){
 28261     sqlite3_free(p->pUnused);
 28263   return rc;
 28267 /*
 28268 ** Delete the file at zPath. If the dirSync argument is true, fsync()
 28269 ** the directory after deleting the file.
 28270 */
 28271 static int unixDelete(
 28272   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
 28273   const char *zPath,        /* Name of file to be deleted */
 28274   int dirSync               /* If true, fsync() directory after deleting file */
 28275 ){
 28276   int rc = SQLITE_OK;
 28277   UNUSED_PARAMETER(NotUsed);
 28278   SimulateIOError(return SQLITE_IOERR_DELETE);
 28279   if( osUnlink(zPath)==(-1) ){
 28280     if( errno==ENOENT ){
 28281       rc = SQLITE_IOERR_DELETE_NOENT;
 28282     }else{
 28283       rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
 28285     return rc;
 28287 #ifndef SQLITE_DISABLE_DIRSYNC
 28288   if( (dirSync & 1)!=0 ){
 28289     int fd;
 28290     rc = osOpenDirectory(zPath, &fd);
 28291     if( rc==SQLITE_OK ){
 28292 #if OS_VXWORKS
 28293       if( fsync(fd)==-1 )
 28294 #else
 28295       if( fsync(fd) )
 28296 #endif
 28298         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
 28300       robust_close(0, fd, __LINE__);
 28301     }else if( rc==SQLITE_CANTOPEN ){
 28302       rc = SQLITE_OK;
 28305 #endif
 28306   return rc;
 28309 /*
 28310 ** Test the existance of or access permissions of file zPath. The
 28311 ** test performed depends on the value of flags:
 28312 **
 28313 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
 28314 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
 28315 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
 28316 **
 28317 ** Otherwise return 0.
 28318 */
 28319 static int unixAccess(
 28320   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
 28321   const char *zPath,      /* Path of the file to examine */
 28322   int flags,              /* What do we want to learn about the zPath file? */
 28323   int *pResOut            /* Write result boolean here */
 28324 ){
 28325   int amode = 0;
 28326   UNUSED_PARAMETER(NotUsed);
 28327   SimulateIOError( return SQLITE_IOERR_ACCESS; );
 28328   switch( flags ){
 28329     case SQLITE_ACCESS_EXISTS:
 28330       amode = F_OK;
 28331       break;
 28332     case SQLITE_ACCESS_READWRITE:
 28333       amode = W_OK|R_OK;
 28334       break;
 28335     case SQLITE_ACCESS_READ:
 28336       amode = R_OK;
 28337       break;
 28339     default:
 28340       assert(!"Invalid flags argument");
 28342   *pResOut = (osAccess(zPath, amode)==0);
 28343   if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
 28344     struct stat buf;
 28345     if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
 28346       *pResOut = 0;
 28349   return SQLITE_OK;
 28353 /*
 28354 ** Turn a relative pathname into a full pathname. The relative path
 28355 ** is stored as a nul-terminated string in the buffer pointed to by
 28356 ** zPath. 
 28357 **
 28358 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 
 28359 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
 28360 ** this buffer before returning.
 28361 */
 28362 static int unixFullPathname(
 28363   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
 28364   const char *zPath,            /* Possibly relative input path */
 28365   int nOut,                     /* Size of output buffer in bytes */
 28366   char *zOut                    /* Output buffer */
 28367 ){
 28369   /* It's odd to simulate an io-error here, but really this is just
 28370   ** using the io-error infrastructure to test that SQLite handles this
 28371   ** function failing. This function could fail if, for example, the
 28372   ** current working directory has been unlinked.
 28373   */
 28374   SimulateIOError( return SQLITE_ERROR );
 28376   assert( pVfs->mxPathname==MAX_PATHNAME );
 28377   UNUSED_PARAMETER(pVfs);
 28379   zOut[nOut-1] = '\0';
 28380   if( zPath[0]=='/' ){
 28381     sqlite3_snprintf(nOut, zOut, "%s", zPath);
 28382   }else{
 28383     int nCwd;
 28384     if( osGetcwd(zOut, nOut-1)==0 ){
 28385       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
 28387     nCwd = (int)strlen(zOut);
 28388     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
 28390   return SQLITE_OK;
 28394 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 28395 /*
 28396 ** Interfaces for opening a shared library, finding entry points
 28397 ** within the shared library, and closing the shared library.
 28398 */
 28399 #include <dlfcn.h>
 28400 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
 28401   UNUSED_PARAMETER(NotUsed);
 28402   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
 28405 /*
 28406 ** SQLite calls this function immediately after a call to unixDlSym() or
 28407 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
 28408 ** message is available, it is written to zBufOut. If no error message
 28409 ** is available, zBufOut is left unmodified and SQLite uses a default
 28410 ** error message.
 28411 */
 28412 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
 28413   const char *zErr;
 28414   UNUSED_PARAMETER(NotUsed);
 28415   unixEnterMutex();
 28416   zErr = dlerror();
 28417   if( zErr ){
 28418     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
 28420   unixLeaveMutex();
 28422 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
 28423   /* 
 28424   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
 28425   ** cast into a pointer to a function.  And yet the library dlsym() routine
 28426   ** returns a void* which is really a pointer to a function.  So how do we
 28427   ** use dlsym() with -pedantic-errors?
 28428   **
 28429   ** Variable x below is defined to be a pointer to a function taking
 28430   ** parameters void* and const char* and returning a pointer to a function.
 28431   ** We initialize x by assigning it a pointer to the dlsym() function.
 28432   ** (That assignment requires a cast.)  Then we call the function that
 28433   ** x points to.  
 28434   **
 28435   ** This work-around is unlikely to work correctly on any system where
 28436   ** you really cannot cast a function pointer into void*.  But then, on the
 28437   ** other hand, dlsym() will not work on such a system either, so we have
 28438   ** not really lost anything.
 28439   */
 28440   void (*(*x)(void*,const char*))(void);
 28441   UNUSED_PARAMETER(NotUsed);
 28442   x = (void(*(*)(void*,const char*))(void))dlsym;
 28443   return (*x)(p, zSym);
 28445 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
 28446   UNUSED_PARAMETER(NotUsed);
 28447   dlclose(pHandle);
 28449 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 28450   #define unixDlOpen  0
 28451   #define unixDlError 0
 28452   #define unixDlSym   0
 28453   #define unixDlClose 0
 28454 #endif
 28456 /*
 28457 ** Write nBuf bytes of random data to the supplied buffer zBuf.
 28458 */
 28459 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
 28460   UNUSED_PARAMETER(NotUsed);
 28461   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
 28463   /* We have to initialize zBuf to prevent valgrind from reporting
 28464   ** errors.  The reports issued by valgrind are incorrect - we would
 28465   ** prefer that the randomness be increased by making use of the
 28466   ** uninitialized space in zBuf - but valgrind errors tend to worry
 28467   ** some users.  Rather than argue, it seems easier just to initialize
 28468   ** the whole array and silence valgrind, even if that means less randomness
 28469   ** in the random seed.
 28470   **
 28471   ** When testing, initializing zBuf[] to zero is all we do.  That means
 28472   ** that we always use the same random number sequence.  This makes the
 28473   ** tests repeatable.
 28474   */
 28475   memset(zBuf, 0, nBuf);
 28476 #if !defined(SQLITE_TEST)
 28478     int pid, fd, got;
 28479     fd = robust_open("/dev/urandom", O_RDONLY, 0);
 28480     if( fd<0 ){
 28481       time_t t;
 28482       time(&t);
 28483       memcpy(zBuf, &t, sizeof(t));
 28484       pid = getpid();
 28485       memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
 28486       assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
 28487       nBuf = sizeof(t) + sizeof(pid);
 28488     }else{
 28489       do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
 28490       robust_close(0, fd, __LINE__);
 28493 #endif
 28494   return nBuf;
 28498 /*
 28499 ** Sleep for a little while.  Return the amount of time slept.
 28500 ** The argument is the number of microseconds we want to sleep.
 28501 ** The return value is the number of microseconds of sleep actually
 28502 ** requested from the underlying operating system, a number which
 28503 ** might be greater than or equal to the argument, but not less
 28504 ** than the argument.
 28505 */
 28506 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
 28507 #if OS_VXWORKS
 28508   struct timespec sp;
 28510   sp.tv_sec = microseconds / 1000000;
 28511   sp.tv_nsec = (microseconds % 1000000) * 1000;
 28512   nanosleep(&sp, NULL);
 28513   UNUSED_PARAMETER(NotUsed);
 28514   return microseconds;
 28515 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
 28516   usleep(microseconds);
 28517   UNUSED_PARAMETER(NotUsed);
 28518   return microseconds;
 28519 #else
 28520   int seconds = (microseconds+999999)/1000000;
 28521   sleep(seconds);
 28522   UNUSED_PARAMETER(NotUsed);
 28523   return seconds*1000000;
 28524 #endif
 28527 /*
 28528 ** The following variable, if set to a non-zero value, is interpreted as
 28529 ** the number of seconds since 1970 and is used to set the result of
 28530 ** sqlite3OsCurrentTime() during testing.
 28531 */
 28532 #ifdef SQLITE_TEST
 28533 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
 28534 #endif
 28536 /*
 28537 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
 28538 ** the current time and date as a Julian Day number times 86_400_000.  In
 28539 ** other words, write into *piNow the number of milliseconds since the Julian
 28540 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 28541 ** proleptic Gregorian calendar.
 28542 **
 28543 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
 28544 ** cannot be found.
 28545 */
 28546 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
 28547   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 28548   int rc = SQLITE_OK;
 28549 #if defined(NO_GETTOD)
 28550   time_t t;
 28551   time(&t);
 28552   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
 28553 #elif OS_VXWORKS
 28554   struct timespec sNow;
 28555   clock_gettime(CLOCK_REALTIME, &sNow);
 28556   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
 28557 #else
 28558   struct timeval sNow;
 28559   if( gettimeofday(&sNow, 0)==0 ){
 28560     *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
 28561   }else{
 28562     rc = SQLITE_ERROR;
 28564 #endif
 28566 #ifdef SQLITE_TEST
 28567   if( sqlite3_current_time ){
 28568     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 28570 #endif
 28571   UNUSED_PARAMETER(NotUsed);
 28572   return rc;
 28575 /*
 28576 ** Find the current time (in Universal Coordinated Time).  Write the
 28577 ** current time and date as a Julian Day number into *prNow and
 28578 ** return 0.  Return 1 if the time and date cannot be found.
 28579 */
 28580 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
 28581   sqlite3_int64 i = 0;
 28582   int rc;
 28583   UNUSED_PARAMETER(NotUsed);
 28584   rc = unixCurrentTimeInt64(0, &i);
 28585   *prNow = i/86400000.0;
 28586   return rc;
 28589 /*
 28590 ** We added the xGetLastError() method with the intention of providing
 28591 ** better low-level error messages when operating-system problems come up
 28592 ** during SQLite operation.  But so far, none of that has been implemented
 28593 ** in the core.  So this routine is never called.  For now, it is merely
 28594 ** a place-holder.
 28595 */
 28596 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
 28597   UNUSED_PARAMETER(NotUsed);
 28598   UNUSED_PARAMETER(NotUsed2);
 28599   UNUSED_PARAMETER(NotUsed3);
 28600   return 0;
 28604 /*
 28605 ************************ End of sqlite3_vfs methods ***************************
 28606 ******************************************************************************/
 28608 /******************************************************************************
 28609 ************************** Begin Proxy Locking ********************************
 28610 **
 28611 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
 28612 ** other locking methods on secondary lock files.  Proxy locking is a
 28613 ** meta-layer over top of the primitive locking implemented above.  For
 28614 ** this reason, the division that implements of proxy locking is deferred
 28615 ** until late in the file (here) after all of the other I/O methods have
 28616 ** been defined - so that the primitive locking methods are available
 28617 ** as services to help with the implementation of proxy locking.
 28618 **
 28619 ****
 28620 **
 28621 ** The default locking schemes in SQLite use byte-range locks on the
 28622 ** database file to coordinate safe, concurrent access by multiple readers
 28623 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
 28624 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
 28625 ** as POSIX read & write locks over fixed set of locations (via fsctl),
 28626 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
 28627 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
 28628 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
 28629 ** address in the shared range is taken for a SHARED lock, the entire
 28630 ** shared range is taken for an EXCLUSIVE lock):
 28631 **
 28632 **      PENDING_BYTE        0x40000000
 28633 **      RESERVED_BYTE       0x40000001
 28634 **      SHARED_RANGE        0x40000002 -> 0x40000200
 28635 **
 28636 ** This works well on the local file system, but shows a nearly 100x
 28637 ** slowdown in read performance on AFP because the AFP client disables
 28638 ** the read cache when byte-range locks are present.  Enabling the read
 28639 ** cache exposes a cache coherency problem that is present on all OS X
 28640 ** supported network file systems.  NFS and AFP both observe the
 28641 ** close-to-open semantics for ensuring cache coherency
 28642 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
 28643 ** address the requirements for concurrent database access by multiple
 28644 ** readers and writers
 28645 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
 28646 **
 28647 ** To address the performance and cache coherency issues, proxy file locking
 28648 ** changes the way database access is controlled by limiting access to a
 28649 ** single host at a time and moving file locks off of the database file
 28650 ** and onto a proxy file on the local file system.  
 28651 **
 28652 **
 28653 ** Using proxy locks
 28654 ** -----------------
 28655 **
 28656 ** C APIs
 28657 **
 28658 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
 28659 **                       <proxy_path> | ":auto:");
 28660 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
 28661 **
 28662 **
 28663 ** SQL pragmas
 28664 **
 28665 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
 28666 **  PRAGMA [database.]lock_proxy_file
 28667 **
 28668 ** Specifying ":auto:" means that if there is a conch file with a matching
 28669 ** host ID in it, the proxy path in the conch file will be used, otherwise
 28670 ** a proxy path based on the user's temp dir
 28671 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
 28672 ** actual proxy file name is generated from the name and path of the
 28673 ** database file.  For example:
 28674 **
 28675 **       For database path "/Users/me/foo.db" 
 28676 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
 28677 **
 28678 ** Once a lock proxy is configured for a database connection, it can not
 28679 ** be removed, however it may be switched to a different proxy path via
 28680 ** the above APIs (assuming the conch file is not being held by another
 28681 ** connection or process). 
 28682 **
 28683 **
 28684 ** How proxy locking works
 28685 ** -----------------------
 28686 **
 28687 ** Proxy file locking relies primarily on two new supporting files: 
 28688 **
 28689 **   *  conch file to limit access to the database file to a single host
 28690 **      at a time
 28691 **
 28692 **   *  proxy file to act as a proxy for the advisory locks normally
 28693 **      taken on the database
 28694 **
 28695 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
 28696 ** by taking an sqlite-style shared lock on the conch file, reading the
 28697 ** contents and comparing the host's unique host ID (see below) and lock
 28698 ** proxy path against the values stored in the conch.  The conch file is
 28699 ** stored in the same directory as the database file and the file name
 28700 ** is patterned after the database file name as ".<databasename>-conch".
 28701 ** If the conch file does not exist, or it's contents do not match the
 28702 ** host ID and/or proxy path, then the lock is escalated to an exclusive
 28703 ** lock and the conch file contents is updated with the host ID and proxy
 28704 ** path and the lock is downgraded to a shared lock again.  If the conch
 28705 ** is held by another process (with a shared lock), the exclusive lock
 28706 ** will fail and SQLITE_BUSY is returned.
 28707 **
 28708 ** The proxy file - a single-byte file used for all advisory file locks
 28709 ** normally taken on the database file.   This allows for safe sharing
 28710 ** of the database file for multiple readers and writers on the same
 28711 ** host (the conch ensures that they all use the same local lock file).
 28712 **
 28713 ** Requesting the lock proxy does not immediately take the conch, it is
 28714 ** only taken when the first request to lock database file is made.  
 28715 ** This matches the semantics of the traditional locking behavior, where
 28716 ** opening a connection to a database file does not take a lock on it.
 28717 ** The shared lock and an open file descriptor are maintained until 
 28718 ** the connection to the database is closed. 
 28719 **
 28720 ** The proxy file and the lock file are never deleted so they only need
 28721 ** to be created the first time they are used.
 28722 **
 28723 ** Configuration options
 28724 ** ---------------------
 28725 **
 28726 **  SQLITE_PREFER_PROXY_LOCKING
 28727 **
 28728 **       Database files accessed on non-local file systems are
 28729 **       automatically configured for proxy locking, lock files are
 28730 **       named automatically using the same logic as
 28731 **       PRAGMA lock_proxy_file=":auto:"
 28732 **    
 28733 **  SQLITE_PROXY_DEBUG
 28734 **
 28735 **       Enables the logging of error messages during host id file
 28736 **       retrieval and creation
 28737 **
 28738 **  LOCKPROXYDIR
 28739 **
 28740 **       Overrides the default directory used for lock proxy files that
 28741 **       are named automatically via the ":auto:" setting
 28742 **
 28743 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 28744 **
 28745 **       Permissions to use when creating a directory for storing the
 28746 **       lock proxy files, only used when LOCKPROXYDIR is not set.
 28747 **    
 28748 **    
 28749 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
 28750 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
 28751 ** force proxy locking to be used for every database file opened, and 0
 28752 ** will force automatic proxy locking to be disabled for all database
 28753 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
 28754 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
 28755 */
 28757 /*
 28758 ** Proxy locking is only available on MacOSX 
 28759 */
 28760 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 28762 /*
 28763 ** The proxyLockingContext has the path and file structures for the remote 
 28764 ** and local proxy files in it
 28765 */
 28766 typedef struct proxyLockingContext proxyLockingContext;
 28767 struct proxyLockingContext {
 28768   unixFile *conchFile;         /* Open conch file */
 28769   char *conchFilePath;         /* Name of the conch file */
 28770   unixFile *lockProxy;         /* Open proxy lock file */
 28771   char *lockProxyPath;         /* Name of the proxy lock file */
 28772   char *dbPath;                /* Name of the open file */
 28773   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
 28774   void *oldLockingContext;     /* Original lockingcontext to restore on close */
 28775   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
 28776 };
 28778 /* 
 28779 ** The proxy lock file path for the database at dbPath is written into lPath, 
 28780 ** which must point to valid, writable memory large enough for a maxLen length
 28781 ** file path. 
 28782 */
 28783 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
 28784   int len;
 28785   int dbLen;
 28786   int i;
 28788 #ifdef LOCKPROXYDIR
 28789   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
 28790 #else
 28791 # ifdef _CS_DARWIN_USER_TEMP_DIR
 28793     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
 28794       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
 28795                lPath, errno, getpid()));
 28796       return SQLITE_IOERR_LOCK;
 28798     len = strlcat(lPath, "sqliteplocks", maxLen);    
 28800 # else
 28801   len = strlcpy(lPath, "/tmp/", maxLen);
 28802 # endif
 28803 #endif
 28805   if( lPath[len-1]!='/' ){
 28806     len = strlcat(lPath, "/", maxLen);
 28809   /* transform the db path to a unique cache name */
 28810   dbLen = (int)strlen(dbPath);
 28811   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
 28812     char c = dbPath[i];
 28813     lPath[i+len] = (c=='/')?'_':c;
 28815   lPath[i+len]='\0';
 28816   strlcat(lPath, ":auto:", maxLen);
 28817   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
 28818   return SQLITE_OK;
 28821 /* 
 28822  ** Creates the lock file and any missing directories in lockPath
 28823  */
 28824 static int proxyCreateLockPath(const char *lockPath){
 28825   int i, len;
 28826   char buf[MAXPATHLEN];
 28827   int start = 0;
 28829   assert(lockPath!=NULL);
 28830   /* try to create all the intermediate directories */
 28831   len = (int)strlen(lockPath);
 28832   buf[0] = lockPath[0];
 28833   for( i=1; i<len; i++ ){
 28834     if( lockPath[i] == '/' && (i - start > 0) ){
 28835       /* only mkdir if leaf dir != "." or "/" or ".." */
 28836       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') 
 28837          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
 28838         buf[i]='\0';
 28839         if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
 28840           int err=errno;
 28841           if( err!=EEXIST ) {
 28842             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
 28843                      "'%s' proxy lock path=%s pid=%d\n",
 28844                      buf, strerror(err), lockPath, getpid()));
 28845             return err;
 28849       start=i+1;
 28851     buf[i] = lockPath[i];
 28853   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
 28854   return 0;
 28857 /*
 28858 ** Create a new VFS file descriptor (stored in memory obtained from
 28859 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
 28860 **
 28861 ** The caller is responsible not only for closing the file descriptor
 28862 ** but also for freeing the memory associated with the file descriptor.
 28863 */
 28864 static int proxyCreateUnixFile(
 28865     const char *path,        /* path for the new unixFile */
 28866     unixFile **ppFile,       /* unixFile created and returned by ref */
 28867     int islockfile           /* if non zero missing dirs will be created */
 28868 ) {
 28869   int fd = -1;
 28870   unixFile *pNew;
 28871   int rc = SQLITE_OK;
 28872   int openFlags = O_RDWR | O_CREAT;
 28873   sqlite3_vfs dummyVfs;
 28874   int terrno = 0;
 28875   UnixUnusedFd *pUnused = NULL;
 28877   /* 1. first try to open/create the file
 28878   ** 2. if that fails, and this is a lock file (not-conch), try creating
 28879   ** the parent directories and then try again.
 28880   ** 3. if that fails, try to open the file read-only
 28881   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
 28882   */
 28883   pUnused = findReusableFd(path, openFlags);
 28884   if( pUnused ){
 28885     fd = pUnused->fd;
 28886   }else{
 28887     pUnused = sqlite3_malloc(sizeof(*pUnused));
 28888     if( !pUnused ){
 28889       return SQLITE_NOMEM;
 28892   if( fd<0 ){
 28893     fd = robust_open(path, openFlags, 0);
 28894     terrno = errno;
 28895     if( fd<0 && errno==ENOENT && islockfile ){
 28896       if( proxyCreateLockPath(path) == SQLITE_OK ){
 28897         fd = robust_open(path, openFlags, 0);
 28901   if( fd<0 ){
 28902     openFlags = O_RDONLY;
 28903     fd = robust_open(path, openFlags, 0);
 28904     terrno = errno;
 28906   if( fd<0 ){
 28907     if( islockfile ){
 28908       return SQLITE_BUSY;
 28910     switch (terrno) {
 28911       case EACCES:
 28912         return SQLITE_PERM;
 28913       case EIO: 
 28914         return SQLITE_IOERR_LOCK; /* even though it is the conch */
 28915       default:
 28916         return SQLITE_CANTOPEN_BKPT;
 28920   pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
 28921   if( pNew==NULL ){
 28922     rc = SQLITE_NOMEM;
 28923     goto end_create_proxy;
 28925   memset(pNew, 0, sizeof(unixFile));
 28926   pNew->openFlags = openFlags;
 28927   memset(&dummyVfs, 0, sizeof(dummyVfs));
 28928   dummyVfs.pAppData = (void*)&autolockIoFinder;
 28929   dummyVfs.zName = "dummy";
 28930   pUnused->fd = fd;
 28931   pUnused->flags = openFlags;
 28932   pNew->pUnused = pUnused;
 28934   rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
 28935   if( rc==SQLITE_OK ){
 28936     *ppFile = pNew;
 28937     return SQLITE_OK;
 28939 end_create_proxy:    
 28940   robust_close(pNew, fd, __LINE__);
 28941   sqlite3_free(pNew);
 28942   sqlite3_free(pUnused);
 28943   return rc;
 28946 #ifdef SQLITE_TEST
 28947 /* simulate multiple hosts by creating unique hostid file paths */
 28948 SQLITE_API int sqlite3_hostid_num = 0;
 28949 #endif
 28951 #define PROXY_HOSTIDLEN    16  /* conch file host id length */
 28953 /* Not always defined in the headers as it ought to be */
 28954 extern int gethostuuid(uuid_t id, const struct timespec *wait);
 28956 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
 28957 ** bytes of writable memory.
 28958 */
 28959 static int proxyGetHostID(unsigned char *pHostID, int *pError){
 28960   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
 28961   memset(pHostID, 0, PROXY_HOSTIDLEN);
 28962 #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
 28963                && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
 28965     static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
 28966     if( gethostuuid(pHostID, &timeout) ){
 28967       int err = errno;
 28968       if( pError ){
 28969         *pError = err;
 28971       return SQLITE_IOERR;
 28974 #else
 28975   UNUSED_PARAMETER(pError);
 28976 #endif
 28977 #ifdef SQLITE_TEST
 28978   /* simulate multiple hosts by creating unique hostid file paths */
 28979   if( sqlite3_hostid_num != 0){
 28980     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
 28982 #endif
 28984   return SQLITE_OK;
 28987 /* The conch file contains the header, host id and lock file path
 28988  */
 28989 #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
 28990 #define PROXY_HEADERLEN    1   /* conch file header length */
 28991 #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
 28992 #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
 28994 /* 
 28995 ** Takes an open conch file, copies the contents to a new path and then moves 
 28996 ** it back.  The newly created file's file descriptor is assigned to the
 28997 ** conch file structure and finally the original conch file descriptor is 
 28998 ** closed.  Returns zero if successful.
 28999 */
 29000 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
 29001   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 29002   unixFile *conchFile = pCtx->conchFile;
 29003   char tPath[MAXPATHLEN];
 29004   char buf[PROXY_MAXCONCHLEN];
 29005   char *cPath = pCtx->conchFilePath;
 29006   size_t readLen = 0;
 29007   size_t pathLen = 0;
 29008   char errmsg[64] = "";
 29009   int fd = -1;
 29010   int rc = -1;
 29011   UNUSED_PARAMETER(myHostID);
 29013   /* create a new path by replace the trailing '-conch' with '-break' */
 29014   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
 29015   if( pathLen>MAXPATHLEN || pathLen<6 || 
 29016      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
 29017     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
 29018     goto end_breaklock;
 29020   /* read the conch content */
 29021   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
 29022   if( readLen<PROXY_PATHINDEX ){
 29023     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
 29024     goto end_breaklock;
 29026   /* write it out to the temporary break file */
 29027   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
 29028   if( fd<0 ){
 29029     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
 29030     goto end_breaklock;
 29032   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
 29033     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
 29034     goto end_breaklock;
 29036   if( rename(tPath, cPath) ){
 29037     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
 29038     goto end_breaklock;
 29040   rc = 0;
 29041   fprintf(stderr, "broke stale lock on %s\n", cPath);
 29042   robust_close(pFile, conchFile->h, __LINE__);
 29043   conchFile->h = fd;
 29044   conchFile->openFlags = O_RDWR | O_CREAT;
 29046 end_breaklock:
 29047   if( rc ){
 29048     if( fd>=0 ){
 29049       osUnlink(tPath);
 29050       robust_close(pFile, fd, __LINE__);
 29052     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
 29054   return rc;
 29057 /* Take the requested lock on the conch file and break a stale lock if the 
 29058 ** host id matches.
 29059 */
 29060 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
 29061   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 29062   unixFile *conchFile = pCtx->conchFile;
 29063   int rc = SQLITE_OK;
 29064   int nTries = 0;
 29065   struct timespec conchModTime;
 29067   memset(&conchModTime, 0, sizeof(conchModTime));
 29068   do {
 29069     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
 29070     nTries ++;
 29071     if( rc==SQLITE_BUSY ){
 29072       /* If the lock failed (busy):
 29073        * 1st try: get the mod time of the conch, wait 0.5s and try again. 
 29074        * 2nd try: fail if the mod time changed or host id is different, wait 
 29075        *           10 sec and try again
 29076        * 3rd try: break the lock unless the mod time has changed.
 29077        */
 29078       struct stat buf;
 29079       if( osFstat(conchFile->h, &buf) ){
 29080         pFile->lastErrno = errno;
 29081         return SQLITE_IOERR_LOCK;
 29084       if( nTries==1 ){
 29085         conchModTime = buf.st_mtimespec;
 29086         usleep(500000); /* wait 0.5 sec and try the lock again*/
 29087         continue;  
 29090       assert( nTries>1 );
 29091       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
 29092          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
 29093         return SQLITE_BUSY;
 29096       if( nTries==2 ){  
 29097         char tBuf[PROXY_MAXCONCHLEN];
 29098         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
 29099         if( len<0 ){
 29100           pFile->lastErrno = errno;
 29101           return SQLITE_IOERR_LOCK;
 29103         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
 29104           /* don't break the lock if the host id doesn't match */
 29105           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
 29106             return SQLITE_BUSY;
 29108         }else{
 29109           /* don't break the lock on short read or a version mismatch */
 29110           return SQLITE_BUSY;
 29112         usleep(10000000); /* wait 10 sec and try the lock again */
 29113         continue; 
 29116       assert( nTries==3 );
 29117       if( 0==proxyBreakConchLock(pFile, myHostID) ){
 29118         rc = SQLITE_OK;
 29119         if( lockType==EXCLUSIVE_LOCK ){
 29120           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);          
 29122         if( !rc ){
 29123           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
 29127   } while( rc==SQLITE_BUSY && nTries<3 );
 29129   return rc;
 29132 /* Takes the conch by taking a shared lock and read the contents conch, if 
 29133 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL 
 29134 ** lockPath means that the lockPath in the conch file will be used if the 
 29135 ** host IDs match, or a new lock path will be generated automatically 
 29136 ** and written to the conch file.
 29137 */
 29138 static int proxyTakeConch(unixFile *pFile){
 29139   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 29141   if( pCtx->conchHeld!=0 ){
 29142     return SQLITE_OK;
 29143   }else{
 29144     unixFile *conchFile = pCtx->conchFile;
 29145     uuid_t myHostID;
 29146     int pError = 0;
 29147     char readBuf[PROXY_MAXCONCHLEN];
 29148     char lockPath[MAXPATHLEN];
 29149     char *tempLockPath = NULL;
 29150     int rc = SQLITE_OK;
 29151     int createConch = 0;
 29152     int hostIdMatch = 0;
 29153     int readLen = 0;
 29154     int tryOldLockPath = 0;
 29155     int forceNewLockPath = 0;
 29157     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
 29158              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
 29160     rc = proxyGetHostID(myHostID, &pError);
 29161     if( (rc&0xff)==SQLITE_IOERR ){
 29162       pFile->lastErrno = pError;
 29163       goto end_takeconch;
 29165     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
 29166     if( rc!=SQLITE_OK ){
 29167       goto end_takeconch;
 29169     /* read the existing conch file */
 29170     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
 29171     if( readLen<0 ){
 29172       /* I/O error: lastErrno set by seekAndRead */
 29173       pFile->lastErrno = conchFile->lastErrno;
 29174       rc = SQLITE_IOERR_READ;
 29175       goto end_takeconch;
 29176     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || 
 29177              readBuf[0]!=(char)PROXY_CONCHVERSION ){
 29178       /* a short read or version format mismatch means we need to create a new 
 29179       ** conch file. 
 29180       */
 29181       createConch = 1;
 29183     /* if the host id matches and the lock path already exists in the conch
 29184     ** we'll try to use the path there, if we can't open that path, we'll 
 29185     ** retry with a new auto-generated path 
 29186     */
 29187     do { /* in case we need to try again for an :auto: named lock file */
 29189       if( !createConch && !forceNewLockPath ){
 29190         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, 
 29191                                   PROXY_HOSTIDLEN);
 29192         /* if the conch has data compare the contents */
 29193         if( !pCtx->lockProxyPath ){
 29194           /* for auto-named local lock file, just check the host ID and we'll
 29195            ** use the local lock file path that's already in there
 29196            */
 29197           if( hostIdMatch ){
 29198             size_t pathLen = (readLen - PROXY_PATHINDEX);
 29200             if( pathLen>=MAXPATHLEN ){
 29201               pathLen=MAXPATHLEN-1;
 29203             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
 29204             lockPath[pathLen] = 0;
 29205             tempLockPath = lockPath;
 29206             tryOldLockPath = 1;
 29207             /* create a copy of the lock path if the conch is taken */
 29208             goto end_takeconch;
 29210         }else if( hostIdMatch
 29211                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
 29212                            readLen-PROXY_PATHINDEX)
 29213         ){
 29214           /* conch host and lock path match */
 29215           goto end_takeconch; 
 29219       /* if the conch isn't writable and doesn't match, we can't take it */
 29220       if( (conchFile->openFlags&O_RDWR) == 0 ){
 29221         rc = SQLITE_BUSY;
 29222         goto end_takeconch;
 29225       /* either the conch didn't match or we need to create a new one */
 29226       if( !pCtx->lockProxyPath ){
 29227         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
 29228         tempLockPath = lockPath;
 29229         /* create a copy of the lock path _only_ if the conch is taken */
 29232       /* update conch with host and path (this will fail if other process
 29233       ** has a shared lock already), if the host id matches, use the big
 29234       ** stick.
 29235       */
 29236       futimes(conchFile->h, NULL);
 29237       if( hostIdMatch && !createConch ){
 29238         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
 29239           /* We are trying for an exclusive lock but another thread in this
 29240            ** same process is still holding a shared lock. */
 29241           rc = SQLITE_BUSY;
 29242         } else {          
 29243           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
 29245       }else{
 29246         rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
 29248       if( rc==SQLITE_OK ){
 29249         char writeBuffer[PROXY_MAXCONCHLEN];
 29250         int writeSize = 0;
 29252         writeBuffer[0] = (char)PROXY_CONCHVERSION;
 29253         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
 29254         if( pCtx->lockProxyPath!=NULL ){
 29255           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
 29256         }else{
 29257           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
 29259         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
 29260         robust_ftruncate(conchFile->h, writeSize);
 29261         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
 29262         fsync(conchFile->h);
 29263         /* If we created a new conch file (not just updated the contents of a 
 29264          ** valid conch file), try to match the permissions of the database 
 29265          */
 29266         if( rc==SQLITE_OK && createConch ){
 29267           struct stat buf;
 29268           int err = osFstat(pFile->h, &buf);
 29269           if( err==0 ){
 29270             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
 29271                                         S_IROTH|S_IWOTH);
 29272             /* try to match the database file R/W permissions, ignore failure */
 29273 #ifndef SQLITE_PROXY_DEBUG
 29274             osFchmod(conchFile->h, cmode);
 29275 #else
 29276             do{
 29277               rc = osFchmod(conchFile->h, cmode);
 29278             }while( rc==(-1) && errno==EINTR );
 29279             if( rc!=0 ){
 29280               int code = errno;
 29281               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
 29282                       cmode, code, strerror(code));
 29283             } else {
 29284               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
 29286           }else{
 29287             int code = errno;
 29288             fprintf(stderr, "STAT FAILED[%d] with %d %s\n", 
 29289                     err, code, strerror(code));
 29290 #endif
 29294       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
 29296     end_takeconch:
 29297       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
 29298       if( rc==SQLITE_OK && pFile->openFlags ){
 29299         int fd;
 29300         if( pFile->h>=0 ){
 29301           robust_close(pFile, pFile->h, __LINE__);
 29303         pFile->h = -1;
 29304         fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
 29305         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
 29306         if( fd>=0 ){
 29307           pFile->h = fd;
 29308         }else{
 29309           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
 29310            during locking */
 29313       if( rc==SQLITE_OK && !pCtx->lockProxy ){
 29314         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
 29315         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
 29316         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
 29317           /* we couldn't create the proxy lock file with the old lock file path
 29318            ** so try again via auto-naming 
 29319            */
 29320           forceNewLockPath = 1;
 29321           tryOldLockPath = 0;
 29322           continue; /* go back to the do {} while start point, try again */
 29325       if( rc==SQLITE_OK ){
 29326         /* Need to make a copy of path if we extracted the value
 29327          ** from the conch file or the path was allocated on the stack
 29328          */
 29329         if( tempLockPath ){
 29330           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
 29331           if( !pCtx->lockProxyPath ){
 29332             rc = SQLITE_NOMEM;
 29336       if( rc==SQLITE_OK ){
 29337         pCtx->conchHeld = 1;
 29339         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
 29340           afpLockingContext *afpCtx;
 29341           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
 29342           afpCtx->dbPath = pCtx->lockProxyPath;
 29344       } else {
 29345         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
 29347       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
 29348                rc==SQLITE_OK?"ok":"failed"));
 29349       return rc;
 29350     } while (1); /* in case we need to retry the :auto: lock file - 
 29351                  ** we should never get here except via the 'continue' call. */
 29355 /*
 29356 ** If pFile holds a lock on a conch file, then release that lock.
 29357 */
 29358 static int proxyReleaseConch(unixFile *pFile){
 29359   int rc = SQLITE_OK;         /* Subroutine return code */
 29360   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
 29361   unixFile *conchFile;        /* Name of the conch file */
 29363   pCtx = (proxyLockingContext *)pFile->lockingContext;
 29364   conchFile = pCtx->conchFile;
 29365   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
 29366            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
 29367            getpid()));
 29368   if( pCtx->conchHeld>0 ){
 29369     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
 29371   pCtx->conchHeld = 0;
 29372   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
 29373            (rc==SQLITE_OK ? "ok" : "failed")));
 29374   return rc;
 29377 /*
 29378 ** Given the name of a database file, compute the name of its conch file.
 29379 ** Store the conch filename in memory obtained from sqlite3_malloc().
 29380 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
 29381 ** or SQLITE_NOMEM if unable to obtain memory.
 29382 **
 29383 ** The caller is responsible for ensuring that the allocated memory
 29384 ** space is eventually freed.
 29385 **
 29386 ** *pConchPath is set to NULL if a memory allocation error occurs.
 29387 */
 29388 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
 29389   int i;                        /* Loop counter */
 29390   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
 29391   char *conchPath;              /* buffer in which to construct conch name */
 29393   /* Allocate space for the conch filename and initialize the name to
 29394   ** the name of the original database file. */  
 29395   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
 29396   if( conchPath==0 ){
 29397     return SQLITE_NOMEM;
 29399   memcpy(conchPath, dbPath, len+1);
 29401   /* now insert a "." before the last / character */
 29402   for( i=(len-1); i>=0; i-- ){
 29403     if( conchPath[i]=='/' ){
 29404       i++;
 29405       break;
 29408   conchPath[i]='.';
 29409   while ( i<len ){
 29410     conchPath[i+1]=dbPath[i];
 29411     i++;
 29414   /* append the "-conch" suffix to the file */
 29415   memcpy(&conchPath[i+1], "-conch", 7);
 29416   assert( (int)strlen(conchPath) == len+7 );
 29418   return SQLITE_OK;
 29422 /* Takes a fully configured proxy locking-style unix file and switches
 29423 ** the local lock file path 
 29424 */
 29425 static int switchLockProxyPath(unixFile *pFile, const char *path) {
 29426   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
 29427   char *oldPath = pCtx->lockProxyPath;
 29428   int rc = SQLITE_OK;
 29430   if( pFile->eFileLock!=NO_LOCK ){
 29431     return SQLITE_BUSY;
 29434   /* nothing to do if the path is NULL, :auto: or matches the existing path */
 29435   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
 29436     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
 29437     return SQLITE_OK;
 29438   }else{
 29439     unixFile *lockProxy = pCtx->lockProxy;
 29440     pCtx->lockProxy=NULL;
 29441     pCtx->conchHeld = 0;
 29442     if( lockProxy!=NULL ){
 29443       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
 29444       if( rc ) return rc;
 29445       sqlite3_free(lockProxy);
 29447     sqlite3_free(oldPath);
 29448     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
 29451   return rc;
 29454 /*
 29455 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
 29456 ** is a string buffer at least MAXPATHLEN+1 characters in size.
 29457 **
 29458 ** This routine find the filename associated with pFile and writes it
 29459 ** int dbPath.
 29460 */
 29461 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
 29462 #if defined(__APPLE__)
 29463   if( pFile->pMethod == &afpIoMethods ){
 29464     /* afp style keeps a reference to the db path in the filePath field 
 29465     ** of the struct */
 29466     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
 29467     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
 29468   } else
 29469 #endif
 29470   if( pFile->pMethod == &dotlockIoMethods ){
 29471     /* dot lock style uses the locking context to store the dot lock
 29472     ** file path */
 29473     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
 29474     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
 29475   }else{
 29476     /* all other styles use the locking context to store the db file path */
 29477     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
 29478     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
 29480   return SQLITE_OK;
 29483 /*
 29484 ** Takes an already filled in unix file and alters it so all file locking 
 29485 ** will be performed on the local proxy lock file.  The following fields
 29486 ** are preserved in the locking context so that they can be restored and 
 29487 ** the unix structure properly cleaned up at close time:
 29488 **  ->lockingContext
 29489 **  ->pMethod
 29490 */
 29491 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
 29492   proxyLockingContext *pCtx;
 29493   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
 29494   char *lockPath=NULL;
 29495   int rc = SQLITE_OK;
 29497   if( pFile->eFileLock!=NO_LOCK ){
 29498     return SQLITE_BUSY;
 29500   proxyGetDbPathForUnixFile(pFile, dbPath);
 29501   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
 29502     lockPath=NULL;
 29503   }else{
 29504     lockPath=(char *)path;
 29507   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
 29508            (lockPath ? lockPath : ":auto:"), getpid()));
 29510   pCtx = sqlite3_malloc( sizeof(*pCtx) );
 29511   if( pCtx==0 ){
 29512     return SQLITE_NOMEM;
 29514   memset(pCtx, 0, sizeof(*pCtx));
 29516   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
 29517   if( rc==SQLITE_OK ){
 29518     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
 29519     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
 29520       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
 29521       ** (c) the file system is read-only, then enable no-locking access.
 29522       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
 29523       ** that openFlags will have only one of O_RDONLY or O_RDWR.
 29524       */
 29525       struct statfs fsInfo;
 29526       struct stat conchInfo;
 29527       int goLockless = 0;
 29529       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
 29530         int err = errno;
 29531         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
 29532           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
 29535       if( goLockless ){
 29536         pCtx->conchHeld = -1; /* read only FS/ lockless */
 29537         rc = SQLITE_OK;
 29541   if( rc==SQLITE_OK && lockPath ){
 29542     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
 29545   if( rc==SQLITE_OK ){
 29546     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
 29547     if( pCtx->dbPath==NULL ){
 29548       rc = SQLITE_NOMEM;
 29551   if( rc==SQLITE_OK ){
 29552     /* all memory is allocated, proxys are created and assigned, 
 29553     ** switch the locking context and pMethod then return.
 29554     */
 29555     pCtx->oldLockingContext = pFile->lockingContext;
 29556     pFile->lockingContext = pCtx;
 29557     pCtx->pOldMethod = pFile->pMethod;
 29558     pFile->pMethod = &proxyIoMethods;
 29559   }else{
 29560     if( pCtx->conchFile ){ 
 29561       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
 29562       sqlite3_free(pCtx->conchFile);
 29564     sqlite3DbFree(0, pCtx->lockProxyPath);
 29565     sqlite3_free(pCtx->conchFilePath); 
 29566     sqlite3_free(pCtx);
 29568   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
 29569            (rc==SQLITE_OK ? "ok" : "failed")));
 29570   return rc;
 29574 /*
 29575 ** This routine handles sqlite3_file_control() calls that are specific
 29576 ** to proxy locking.
 29577 */
 29578 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
 29579   switch( op ){
 29580     case SQLITE_GET_LOCKPROXYFILE: {
 29581       unixFile *pFile = (unixFile*)id;
 29582       if( pFile->pMethod == &proxyIoMethods ){
 29583         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
 29584         proxyTakeConch(pFile);
 29585         if( pCtx->lockProxyPath ){
 29586           *(const char **)pArg = pCtx->lockProxyPath;
 29587         }else{
 29588           *(const char **)pArg = ":auto: (not held)";
 29590       } else {
 29591         *(const char **)pArg = NULL;
 29593       return SQLITE_OK;
 29595     case SQLITE_SET_LOCKPROXYFILE: {
 29596       unixFile *pFile = (unixFile*)id;
 29597       int rc = SQLITE_OK;
 29598       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
 29599       if( pArg==NULL || (const char *)pArg==0 ){
 29600         if( isProxyStyle ){
 29601           /* turn off proxy locking - not supported */
 29602           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
 29603         }else{
 29604           /* turn off proxy locking - already off - NOOP */
 29605           rc = SQLITE_OK;
 29607       }else{
 29608         const char *proxyPath = (const char *)pArg;
 29609         if( isProxyStyle ){
 29610           proxyLockingContext *pCtx = 
 29611             (proxyLockingContext*)pFile->lockingContext;
 29612           if( !strcmp(pArg, ":auto:") 
 29613            || (pCtx->lockProxyPath &&
 29614                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
 29615           ){
 29616             rc = SQLITE_OK;
 29617           }else{
 29618             rc = switchLockProxyPath(pFile, proxyPath);
 29620         }else{
 29621           /* turn on proxy file locking */
 29622           rc = proxyTransformUnixFile(pFile, proxyPath);
 29625       return rc;
 29627     default: {
 29628       assert( 0 );  /* The call assures that only valid opcodes are sent */
 29631   /*NOTREACHED*/
 29632   return SQLITE_ERROR;
 29635 /*
 29636 ** Within this division (the proxying locking implementation) the procedures
 29637 ** above this point are all utilities.  The lock-related methods of the
 29638 ** proxy-locking sqlite3_io_method object follow.
 29639 */
 29642 /*
 29643 ** This routine checks if there is a RESERVED lock held on the specified
 29644 ** file by this or any other process. If such a lock is held, set *pResOut
 29645 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 29646 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 29647 */
 29648 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
 29649   unixFile *pFile = (unixFile*)id;
 29650   int rc = proxyTakeConch(pFile);
 29651   if( rc==SQLITE_OK ){
 29652     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 29653     if( pCtx->conchHeld>0 ){
 29654       unixFile *proxy = pCtx->lockProxy;
 29655       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
 29656     }else{ /* conchHeld < 0 is lockless */
 29657       pResOut=0;
 29660   return rc;
 29663 /*
 29664 ** Lock the file with the lock specified by parameter eFileLock - one
 29665 ** of the following:
 29666 **
 29667 **     (1) SHARED_LOCK
 29668 **     (2) RESERVED_LOCK
 29669 **     (3) PENDING_LOCK
 29670 **     (4) EXCLUSIVE_LOCK
 29671 **
 29672 ** Sometimes when requesting one lock state, additional lock states
 29673 ** are inserted in between.  The locking might fail on one of the later
 29674 ** transitions leaving the lock state different from what it started but
 29675 ** still short of its goal.  The following chart shows the allowed
 29676 ** transitions and the inserted intermediate states:
 29677 **
 29678 **    UNLOCKED -> SHARED
 29679 **    SHARED -> RESERVED
 29680 **    SHARED -> (PENDING) -> EXCLUSIVE
 29681 **    RESERVED -> (PENDING) -> EXCLUSIVE
 29682 **    PENDING -> EXCLUSIVE
 29683 **
 29684 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 29685 ** routine to lower a locking level.
 29686 */
 29687 static int proxyLock(sqlite3_file *id, int eFileLock) {
 29688   unixFile *pFile = (unixFile*)id;
 29689   int rc = proxyTakeConch(pFile);
 29690   if( rc==SQLITE_OK ){
 29691     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 29692     if( pCtx->conchHeld>0 ){
 29693       unixFile *proxy = pCtx->lockProxy;
 29694       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
 29695       pFile->eFileLock = proxy->eFileLock;
 29696     }else{
 29697       /* conchHeld < 0 is lockless */
 29700   return rc;
 29704 /*
 29705 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 29706 ** must be either NO_LOCK or SHARED_LOCK.
 29707 **
 29708 ** If the locking level of the file descriptor is already at or below
 29709 ** the requested locking level, this routine is a no-op.
 29710 */
 29711 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
 29712   unixFile *pFile = (unixFile*)id;
 29713   int rc = proxyTakeConch(pFile);
 29714   if( rc==SQLITE_OK ){
 29715     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 29716     if( pCtx->conchHeld>0 ){
 29717       unixFile *proxy = pCtx->lockProxy;
 29718       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
 29719       pFile->eFileLock = proxy->eFileLock;
 29720     }else{
 29721       /* conchHeld < 0 is lockless */
 29724   return rc;
 29727 /*
 29728 ** Close a file that uses proxy locks.
 29729 */
 29730 static int proxyClose(sqlite3_file *id) {
 29731   if( id ){
 29732     unixFile *pFile = (unixFile*)id;
 29733     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 29734     unixFile *lockProxy = pCtx->lockProxy;
 29735     unixFile *conchFile = pCtx->conchFile;
 29736     int rc = SQLITE_OK;
 29738     if( lockProxy ){
 29739       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
 29740       if( rc ) return rc;
 29741       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
 29742       if( rc ) return rc;
 29743       sqlite3_free(lockProxy);
 29744       pCtx->lockProxy = 0;
 29746     if( conchFile ){
 29747       if( pCtx->conchHeld ){
 29748         rc = proxyReleaseConch(pFile);
 29749         if( rc ) return rc;
 29751       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
 29752       if( rc ) return rc;
 29753       sqlite3_free(conchFile);
 29755     sqlite3DbFree(0, pCtx->lockProxyPath);
 29756     sqlite3_free(pCtx->conchFilePath);
 29757     sqlite3DbFree(0, pCtx->dbPath);
 29758     /* restore the original locking context and pMethod then close it */
 29759     pFile->lockingContext = pCtx->oldLockingContext;
 29760     pFile->pMethod = pCtx->pOldMethod;
 29761     sqlite3_free(pCtx);
 29762     return pFile->pMethod->xClose(id);
 29764   return SQLITE_OK;
 29769 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 29770 /*
 29771 ** The proxy locking style is intended for use with AFP filesystems.
 29772 ** And since AFP is only supported on MacOSX, the proxy locking is also
 29773 ** restricted to MacOSX.
 29774 ** 
 29775 **
 29776 ******************* End of the proxy lock implementation **********************
 29777 ******************************************************************************/
 29779 /*
 29780 ** Initialize the operating system interface.
 29781 **
 29782 ** This routine registers all VFS implementations for unix-like operating
 29783 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
 29784 ** should be the only routines in this file that are visible from other
 29785 ** files.
 29786 **
 29787 ** This routine is called once during SQLite initialization and by a
 29788 ** single thread.  The memory allocation and mutex subsystems have not
 29789 ** necessarily been initialized when this routine is called, and so they
 29790 ** should not be used.
 29791 */
 29792 SQLITE_API int sqlite3_os_init(void){ 
 29793   /* 
 29794   ** The following macro defines an initializer for an sqlite3_vfs object.
 29795   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
 29796   ** to the "finder" function.  (pAppData is a pointer to a pointer because
 29797   ** silly C90 rules prohibit a void* from being cast to a function pointer
 29798   ** and so we have to go through the intermediate pointer to avoid problems
 29799   ** when compiling with -pedantic-errors on GCC.)
 29800   **
 29801   ** The FINDER parameter to this macro is the name of the pointer to the
 29802   ** finder-function.  The finder-function returns a pointer to the
 29803   ** sqlite_io_methods object that implements the desired locking
 29804   ** behaviors.  See the division above that contains the IOMETHODS
 29805   ** macro for addition information on finder-functions.
 29806   **
 29807   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
 29808   ** object.  But the "autolockIoFinder" available on MacOSX does a little
 29809   ** more than that; it looks at the filesystem type that hosts the 
 29810   ** database file and tries to choose an locking method appropriate for
 29811   ** that filesystem time.
 29812   */
 29813   #define UNIXVFS(VFSNAME, FINDER) {                        \
 29814     3,                    /* iVersion */                    \
 29815     sizeof(unixFile),     /* szOsFile */                    \
 29816     MAX_PATHNAME,         /* mxPathname */                  \
 29817     0,                    /* pNext */                       \
 29818     VFSNAME,              /* zName */                       \
 29819     (void*)&FINDER,       /* pAppData */                    \
 29820     unixOpen,             /* xOpen */                       \
 29821     unixDelete,           /* xDelete */                     \
 29822     unixAccess,           /* xAccess */                     \
 29823     unixFullPathname,     /* xFullPathname */               \
 29824     unixDlOpen,           /* xDlOpen */                     \
 29825     unixDlError,          /* xDlError */                    \
 29826     unixDlSym,            /* xDlSym */                      \
 29827     unixDlClose,          /* xDlClose */                    \
 29828     unixRandomness,       /* xRandomness */                 \
 29829     unixSleep,            /* xSleep */                      \
 29830     unixCurrentTime,      /* xCurrentTime */                \
 29831     unixGetLastError,     /* xGetLastError */               \
 29832     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
 29833     unixSetSystemCall,    /* xSetSystemCall */              \
 29834     unixGetSystemCall,    /* xGetSystemCall */              \
 29835     unixNextSystemCall,   /* xNextSystemCall */             \
 29838   /*
 29839   ** All default VFSes for unix are contained in the following array.
 29840   **
 29841   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
 29842   ** by the SQLite core when the VFS is registered.  So the following
 29843   ** array cannot be const.
 29844   */
 29845   static sqlite3_vfs aVfs[] = {
 29846 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
 29847     UNIXVFS("unix",          autolockIoFinder ),
 29848 #else
 29849     UNIXVFS("unix",          posixIoFinder ),
 29850 #endif
 29851     UNIXVFS("unix-none",     nolockIoFinder ),
 29852     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
 29853     UNIXVFS("unix-excl",     posixIoFinder ),
 29854 #if OS_VXWORKS
 29855     UNIXVFS("unix-namedsem", semIoFinder ),
 29856 #endif
 29857 #if SQLITE_ENABLE_LOCKING_STYLE
 29858     UNIXVFS("unix-posix",    posixIoFinder ),
 29859 #if !OS_VXWORKS
 29860     UNIXVFS("unix-flock",    flockIoFinder ),
 29861 #endif
 29862 #endif
 29863 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 29864     UNIXVFS("unix-afp",      afpIoFinder ),
 29865     UNIXVFS("unix-nfs",      nfsIoFinder ),
 29866     UNIXVFS("unix-proxy",    proxyIoFinder ),
 29867 #endif
 29868   };
 29869   unsigned int i;          /* Loop counter */
 29871   /* Double-check that the aSyscall[] array has been constructed
 29872   ** correctly.  See ticket [bb3a86e890c8e96ab] */
 29873   assert( ArraySize(aSyscall)==22 );
 29875   /* Register all VFSes defined in the aVfs[] array */
 29876   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 29877     sqlite3_vfs_register(&aVfs[i], i==0);
 29879   return SQLITE_OK; 
 29882 /*
 29883 ** Shutdown the operating system interface.
 29884 **
 29885 ** Some operating systems might need to do some cleanup in this routine,
 29886 ** to release dynamically allocated objects.  But not on unix.
 29887 ** This routine is a no-op for unix.
 29888 */
 29889 SQLITE_API int sqlite3_os_end(void){ 
 29890   return SQLITE_OK; 
 29893 #endif /* SQLITE_OS_UNIX */
 29895 /************** End of os_unix.c *********************************************/
 29896 /************** Begin file os_win.c ******************************************/
 29897 /*
 29898 ** 2004 May 22
 29899 **
 29900 ** The author disclaims copyright to this source code.  In place of
 29901 ** a legal notice, here is a blessing:
 29902 **
 29903 **    May you do good and not evil.
 29904 **    May you find forgiveness for yourself and forgive others.
 29905 **    May you share freely, never taking more than you give.
 29906 **
 29907 ******************************************************************************
 29908 **
 29909 ** This file contains code that is specific to Windows.
 29910 */
 29911 #if SQLITE_OS_WIN               /* This file is used for Windows only */
 29913 #ifdef __CYGWIN__
 29914 # include <sys/cygwin.h>
 29915 #endif
 29917 /*
 29918 ** Include code that is common to all os_*.c files
 29919 */
 29920 /************** Include os_common.h in the middle of os_win.c ****************/
 29921 /************** Begin file os_common.h ***************************************/
 29922 /*
 29923 ** 2004 May 22
 29924 **
 29925 ** The author disclaims copyright to this source code.  In place of
 29926 ** a legal notice, here is a blessing:
 29927 **
 29928 **    May you do good and not evil.
 29929 **    May you find forgiveness for yourself and forgive others.
 29930 **    May you share freely, never taking more than you give.
 29931 **
 29932 ******************************************************************************
 29933 **
 29934 ** This file contains macros and a little bit of code that is common to
 29935 ** all of the platform-specific files (os_*.c) and is #included into those
 29936 ** files.
 29937 **
 29938 ** This file should be #included by the os_*.c files only.  It is not a
 29939 ** general purpose header file.
 29940 */
 29941 #ifndef _OS_COMMON_H_
 29942 #define _OS_COMMON_H_
 29944 /*
 29945 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
 29946 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 29947 ** switch.  The following code should catch this problem at compile-time.
 29948 */
 29949 #ifdef MEMORY_DEBUG
 29950 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 29951 #endif
 29953 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 29954 # ifndef SQLITE_DEBUG_OS_TRACE
 29955 #   define SQLITE_DEBUG_OS_TRACE 0
 29956 # endif
 29957   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 29958 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 29959 #else
 29960 # define OSTRACE(X)
 29961 #endif
 29963 /*
 29964 ** Macros for performance tracing.  Normally turned off.  Only works
 29965 ** on i486 hardware.
 29966 */
 29967 #ifdef SQLITE_PERFORMANCE_TRACE
 29969 /* 
 29970 ** hwtime.h contains inline assembler code for implementing 
 29971 ** high-performance timing routines.
 29972 */
 29973 /************** Include hwtime.h in the middle of os_common.h ****************/
 29974 /************** Begin file hwtime.h ******************************************/
 29975 /*
 29976 ** 2008 May 27
 29977 **
 29978 ** The author disclaims copyright to this source code.  In place of
 29979 ** a legal notice, here is a blessing:
 29980 **
 29981 **    May you do good and not evil.
 29982 **    May you find forgiveness for yourself and forgive others.
 29983 **    May you share freely, never taking more than you give.
 29984 **
 29985 ******************************************************************************
 29986 **
 29987 ** This file contains inline asm code for retrieving "high-performance"
 29988 ** counters for x86 class CPUs.
 29989 */
 29990 #ifndef _HWTIME_H_
 29991 #define _HWTIME_H_
 29993 /*
 29994 ** The following routine only works on pentium-class (or newer) processors.
 29995 ** It uses the RDTSC opcode to read the cycle count value out of the
 29996 ** processor and returns that value.  This can be used for high-res
 29997 ** profiling.
 29998 */
 29999 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
 30000       (defined(i386) || defined(__i386__) || defined(_M_IX86))
 30002   #if defined(__GNUC__)
 30004   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 30005      unsigned int lo, hi;
 30006      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 30007      return (sqlite_uint64)hi << 32 | lo;
 30010   #elif defined(_MSC_VER)
 30012   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 30013      __asm {
 30014         rdtsc
 30015         ret       ; return value at EDX:EAX
 30019   #endif
 30021 #elif (defined(__GNUC__) && defined(__x86_64__))
 30023   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 30024       unsigned long val;
 30025       __asm__ __volatile__ ("rdtsc" : "=A" (val));
 30026       return val;
 30029 #elif (defined(__GNUC__) && defined(__ppc__))
 30031   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 30032       unsigned long long retval;
 30033       unsigned long junk;
 30034       __asm__ __volatile__ ("\n\
 30035           1:      mftbu   %1\n\
 30036                   mftb    %L0\n\
 30037                   mftbu   %0\n\
 30038                   cmpw    %0,%1\n\
 30039                   bne     1b"
 30040                   : "=r" (retval), "=r" (junk));
 30041       return retval;
 30044 #else
 30046   #error Need implementation of sqlite3Hwtime() for your platform.
 30048   /*
 30049   ** To compile without implementing sqlite3Hwtime() for your platform,
 30050   ** you can remove the above #error and use the following
 30051   ** stub function.  You will lose timing support for many
 30052   ** of the debugging and testing utilities, but it should at
 30053   ** least compile and run.
 30054   */
 30055 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 30057 #endif
 30059 #endif /* !defined(_HWTIME_H_) */
 30061 /************** End of hwtime.h **********************************************/
 30062 /************** Continuing where we left off in os_common.h ******************/
 30064 static sqlite_uint64 g_start;
 30065 static sqlite_uint64 g_elapsed;
 30066 #define TIMER_START       g_start=sqlite3Hwtime()
 30067 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
 30068 #define TIMER_ELAPSED     g_elapsed
 30069 #else
 30070 #define TIMER_START
 30071 #define TIMER_END
 30072 #define TIMER_ELAPSED     ((sqlite_uint64)0)
 30073 #endif
 30075 /*
 30076 ** If we compile with the SQLITE_TEST macro set, then the following block
 30077 ** of code will give us the ability to simulate a disk I/O error.  This
 30078 ** is used for testing the I/O recovery logic.
 30079 */
 30080 #ifdef SQLITE_TEST
 30081 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
 30082 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
 30083 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
 30084 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
 30085 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
 30086 SQLITE_API int sqlite3_diskfull_pending = 0;
 30087 SQLITE_API int sqlite3_diskfull = 0;
 30088 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
 30089 #define SimulateIOError(CODE)  \
 30090   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
 30091        || sqlite3_io_error_pending-- == 1 )  \
 30092               { local_ioerr(); CODE; }
 30093 static void local_ioerr(){
 30094   IOTRACE(("IOERR\n"));
 30095   sqlite3_io_error_hit++;
 30096   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
 30098 #define SimulateDiskfullError(CODE) \
 30099    if( sqlite3_diskfull_pending ){ \
 30100      if( sqlite3_diskfull_pending == 1 ){ \
 30101        local_ioerr(); \
 30102        sqlite3_diskfull = 1; \
 30103        sqlite3_io_error_hit = 1; \
 30104        CODE; \
 30105      }else{ \
 30106        sqlite3_diskfull_pending--; \
 30107      } \
 30109 #else
 30110 #define SimulateIOErrorBenign(X)
 30111 #define SimulateIOError(A)
 30112 #define SimulateDiskfullError(A)
 30113 #endif
 30115 /*
 30116 ** When testing, keep a count of the number of open files.
 30117 */
 30118 #ifdef SQLITE_TEST
 30119 SQLITE_API int sqlite3_open_file_count = 0;
 30120 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
 30121 #else
 30122 #define OpenCounter(X)
 30123 #endif
 30125 #endif /* !defined(_OS_COMMON_H_) */
 30127 /************** End of os_common.h *******************************************/
 30128 /************** Continuing where we left off in os_win.c *********************/
 30130 /*
 30131 ** Compiling and using WAL mode requires several APIs that are only
 30132 ** available in Windows platforms based on the NT kernel.
 30133 */
 30134 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
 30135 # error "WAL mode requires support from the Windows NT kernel, compile\
 30136  with SQLITE_OMIT_WAL."
 30137 #endif
 30139 /*
 30140 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
 30141 ** based on the sub-platform)?
 30142 */
 30143 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 30144 #  define SQLITE_WIN32_HAS_ANSI
 30145 #endif
 30147 /*
 30148 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
 30149 ** based on the sub-platform)?
 30150 */
 30151 #if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
 30152 #  define SQLITE_WIN32_HAS_WIDE
 30153 #endif
 30155 /*
 30156 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
 30157 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
 30158 ** are not present in the header file)?
 30159 */
 30160 #if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
 30161 /*
 30162 ** Two of the file mapping APIs are different under WinRT.  Figure out which
 30163 ** set we need.
 30164 */
 30165 #if SQLITE_OS_WINRT
 30166 WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
 30167         LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
 30169 WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
 30170 #else
 30171 #if defined(SQLITE_WIN32_HAS_ANSI)
 30172 WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
 30173         DWORD, DWORD, DWORD, LPCSTR);
 30174 #endif /* defined(SQLITE_WIN32_HAS_ANSI) */
 30176 #if defined(SQLITE_WIN32_HAS_WIDE)
 30177 WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
 30178         DWORD, DWORD, DWORD, LPCWSTR);
 30179 #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
 30181 WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
 30182 #endif /* SQLITE_OS_WINRT */
 30184 /*
 30185 ** This file mapping API is common to both Win32 and WinRT.
 30186 */
 30187 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
 30188 #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
 30190 /*
 30191 ** Macro to find the minimum of two numeric values.
 30192 */
 30193 #ifndef MIN
 30194 # define MIN(x,y) ((x)<(y)?(x):(y))
 30195 #endif
 30197 /*
 30198 ** Some Microsoft compilers lack this definition.
 30199 */
 30200 #ifndef INVALID_FILE_ATTRIBUTES
 30201 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
 30202 #endif
 30204 #ifndef FILE_FLAG_MASK
 30205 # define FILE_FLAG_MASK          (0xFF3C0000)
 30206 #endif
 30208 #ifndef FILE_ATTRIBUTE_MASK
 30209 # define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
 30210 #endif
 30212 #ifndef SQLITE_OMIT_WAL
 30213 /* Forward references */
 30214 typedef struct winShm winShm;           /* A connection to shared-memory */
 30215 typedef struct winShmNode winShmNode;   /* A region of shared-memory */
 30216 #endif
 30218 /*
 30219 ** WinCE lacks native support for file locking so we have to fake it
 30220 ** with some code of our own.
 30221 */
 30222 #if SQLITE_OS_WINCE
 30223 typedef struct winceLock {
 30224   int nReaders;       /* Number of reader locks obtained */
 30225   BOOL bPending;      /* Indicates a pending lock has been obtained */
 30226   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
 30227   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
 30228 } winceLock;
 30229 #endif
 30231 /*
 30232 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
 30233 ** portability layer.
 30234 */
 30235 typedef struct winFile winFile;
 30236 struct winFile {
 30237   const sqlite3_io_methods *pMethod; /*** Must be first ***/
 30238   sqlite3_vfs *pVfs;      /* The VFS used to open this file */
 30239   HANDLE h;               /* Handle for accessing the file */
 30240   u8 locktype;            /* Type of lock currently held on this file */
 30241   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
 30242   u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
 30243   DWORD lastErrno;        /* The Windows errno from the last I/O error */
 30244 #ifndef SQLITE_OMIT_WAL
 30245   winShm *pShm;           /* Instance of shared memory on this file */
 30246 #endif
 30247   const char *zPath;      /* Full pathname of this file */
 30248   int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
 30249 #if SQLITE_OS_WINCE
 30250   LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
 30251   HANDLE hMutex;          /* Mutex used to control access to shared lock */  
 30252   HANDLE hShared;         /* Shared memory segment used for locking */
 30253   winceLock local;        /* Locks obtained by this instance of winFile */
 30254   winceLock *shared;      /* Global shared lock memory for the file  */
 30255 #endif
 30256 };
 30258 /*
 30259 ** Allowed values for winFile.ctrlFlags
 30260 */
 30261 #define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
 30262 #define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
 30264 /*
 30265  * The size of the buffer used by sqlite3_win32_write_debug().
 30266  */
 30267 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
 30268 #  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
 30269 #endif
 30271 /*
 30272  * The value used with sqlite3_win32_set_directory() to specify that
 30273  * the data directory should be changed.
 30274  */
 30275 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
 30276 #  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
 30277 #endif
 30279 /*
 30280  * The value used with sqlite3_win32_set_directory() to specify that
 30281  * the temporary directory should be changed.
 30282  */
 30283 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 30284 #  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
 30285 #endif
 30287 /*
 30288  * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
 30289  * various Win32 API heap functions instead of our own.
 30290  */
 30291 #ifdef SQLITE_WIN32_MALLOC
 30293 /*
 30294  * If this is non-zero, an isolated heap will be created by the native Win32
 30295  * allocator subsystem; otherwise, the default process heap will be used.  This
 30296  * setting has no effect when compiling for WinRT.  By default, this is enabled
 30297  * and an isolated heap will be created to store all allocated data.
 30299  ******************************************************************************
 30300  * WARNING: It is important to note that when this setting is non-zero and the
 30301  *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
 30302  *          function), all data that was allocated using the isolated heap will
 30303  *          be freed immediately and any attempt to access any of that freed
 30304  *          data will almost certainly result in an immediate access violation.
 30305  ******************************************************************************
 30306  */
 30307 #ifndef SQLITE_WIN32_HEAP_CREATE
 30308 #  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
 30309 #endif
 30311 /*
 30312  * The initial size of the Win32-specific heap.  This value may be zero.
 30313  */
 30314 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
 30315 #  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
 30316                                        (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
 30317 #endif
 30319 /*
 30320  * The maximum size of the Win32-specific heap.  This value may be zero.
 30321  */
 30322 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
 30323 #  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
 30324 #endif
 30326 /*
 30327  * The extra flags to use in calls to the Win32 heap APIs.  This value may be
 30328  * zero for the default behavior.
 30329  */
 30330 #ifndef SQLITE_WIN32_HEAP_FLAGS
 30331 #  define SQLITE_WIN32_HEAP_FLAGS     (0)
 30332 #endif
 30334 /*
 30335 ** The winMemData structure stores information required by the Win32-specific
 30336 ** sqlite3_mem_methods implementation.
 30337 */
 30338 typedef struct winMemData winMemData;
 30339 struct winMemData {
 30340 #ifndef NDEBUG
 30341   u32 magic;    /* Magic number to detect structure corruption. */
 30342 #endif
 30343   HANDLE hHeap; /* The handle to our heap. */
 30344   BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
 30345 };
 30347 #ifndef NDEBUG
 30348 #define WINMEM_MAGIC     0x42b2830b
 30349 #endif
 30351 static struct winMemData win_mem_data = {
 30352 #ifndef NDEBUG
 30353   WINMEM_MAGIC,
 30354 #endif
 30355   NULL, FALSE
 30356 };
 30358 #ifndef NDEBUG
 30359 #define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
 30360 #else
 30361 #define winMemAssertMagic()
 30362 #endif
 30364 #define winMemGetHeap() win_mem_data.hHeap
 30366 static void *winMemMalloc(int nBytes);
 30367 static void winMemFree(void *pPrior);
 30368 static void *winMemRealloc(void *pPrior, int nBytes);
 30369 static int winMemSize(void *p);
 30370 static int winMemRoundup(int n);
 30371 static int winMemInit(void *pAppData);
 30372 static void winMemShutdown(void *pAppData);
 30374 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
 30375 #endif /* SQLITE_WIN32_MALLOC */
 30377 /*
 30378 ** The following variable is (normally) set once and never changes
 30379 ** thereafter.  It records whether the operating system is Win9x
 30380 ** or WinNT.
 30381 **
 30382 ** 0:   Operating system unknown.
 30383 ** 1:   Operating system is Win9x.
 30384 ** 2:   Operating system is WinNT.
 30385 **
 30386 ** In order to facilitate testing on a WinNT system, the test fixture
 30387 ** can manually set this value to 1 to emulate Win98 behavior.
 30388 */
 30389 #ifdef SQLITE_TEST
 30390 SQLITE_API int sqlite3_os_type = 0;
 30391 #else
 30392 static int sqlite3_os_type = 0;
 30393 #endif
 30395 #ifndef SYSCALL
 30396 #  define SYSCALL sqlite3_syscall_ptr
 30397 #endif
 30399 /*
 30400 ** This function is not available on Windows CE or WinRT.
 30401  */
 30403 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 30404 #  define osAreFileApisANSI()       1
 30405 #endif
 30407 /*
 30408 ** Many system calls are accessed through pointer-to-functions so that
 30409 ** they may be overridden at runtime to facilitate fault injection during
 30410 ** testing and sandboxing.  The following array holds the names and pointers
 30411 ** to all overrideable system calls.
 30412 */
 30413 static struct win_syscall {
 30414   const char *zName;            /* Name of the sytem call */
 30415   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
 30416   sqlite3_syscall_ptr pDefault; /* Default value */
 30417 } aSyscall[] = {
 30418 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 30419   { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
 30420 #else
 30421   { "AreFileApisANSI",         (SYSCALL)0,                       0 },
 30422 #endif
 30424 #ifndef osAreFileApisANSI
 30425 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
 30426 #endif
 30428 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
 30429   { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
 30430 #else
 30431   { "CharLowerW",              (SYSCALL)0,                       0 },
 30432 #endif
 30434 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
 30436 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
 30437   { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
 30438 #else
 30439   { "CharUpperW",              (SYSCALL)0,                       0 },
 30440 #endif
 30442 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
 30444   { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
 30446 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
 30448 #if defined(SQLITE_WIN32_HAS_ANSI)
 30449   { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
 30450 #else
 30451   { "CreateFileA",             (SYSCALL)0,                       0 },
 30452 #endif
 30454 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
 30455         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
 30457 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 30458   { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
 30459 #else
 30460   { "CreateFileW",             (SYSCALL)0,                       0 },
 30461 #endif
 30463 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
 30464         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
 30466 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
 30467         !defined(SQLITE_OMIT_WAL))
 30468   { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
 30469 #else
 30470   { "CreateFileMappingA",      (SYSCALL)0,                       0 },
 30471 #endif
 30473 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
 30474         DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
 30476 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
 30477         !defined(SQLITE_OMIT_WAL))
 30478   { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
 30479 #else
 30480   { "CreateFileMappingW",      (SYSCALL)0,                       0 },
 30481 #endif
 30483 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
 30484         DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
 30486 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 30487   { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
 30488 #else
 30489   { "CreateMutexW",            (SYSCALL)0,                       0 },
 30490 #endif
 30492 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
 30493         LPCWSTR))aSyscall[8].pCurrent)
 30495 #if defined(SQLITE_WIN32_HAS_ANSI)
 30496   { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
 30497 #else
 30498   { "DeleteFileA",             (SYSCALL)0,                       0 },
 30499 #endif
 30501 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
 30503 #if defined(SQLITE_WIN32_HAS_WIDE)
 30504   { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
 30505 #else
 30506   { "DeleteFileW",             (SYSCALL)0,                       0 },
 30507 #endif
 30509 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
 30511 #if SQLITE_OS_WINCE
 30512   { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
 30513 #else
 30514   { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
 30515 #endif
 30517 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
 30518         LPFILETIME))aSyscall[11].pCurrent)
 30520 #if SQLITE_OS_WINCE
 30521   { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
 30522 #else
 30523   { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
 30524 #endif
 30526 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
 30527         LPSYSTEMTIME))aSyscall[12].pCurrent)
 30529   { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
 30531 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
 30533 #if defined(SQLITE_WIN32_HAS_ANSI)
 30534   { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
 30535 #else
 30536   { "FormatMessageA",          (SYSCALL)0,                       0 },
 30537 #endif
 30539 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
 30540         DWORD,va_list*))aSyscall[14].pCurrent)
 30542 #if defined(SQLITE_WIN32_HAS_WIDE)
 30543   { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
 30544 #else
 30545   { "FormatMessageW",          (SYSCALL)0,                       0 },
 30546 #endif
 30548 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
 30549         DWORD,va_list*))aSyscall[15].pCurrent)
 30551 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
 30552   { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
 30553 #else
 30554   { "FreeLibrary",             (SYSCALL)0,                       0 },
 30555 #endif
 30557 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
 30559   { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
 30561 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
 30563 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
 30564   { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
 30565 #else
 30566   { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
 30567 #endif
 30569 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
 30570         LPDWORD))aSyscall[18].pCurrent)
 30572 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 30573   { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
 30574 #else
 30575   { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
 30576 #endif
 30578 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
 30579         LPDWORD))aSyscall[19].pCurrent)
 30581 #if defined(SQLITE_WIN32_HAS_ANSI)
 30582   { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
 30583 #else
 30584   { "GetFileAttributesA",      (SYSCALL)0,                       0 },
 30585 #endif
 30587 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
 30589 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 30590   { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
 30591 #else
 30592   { "GetFileAttributesW",      (SYSCALL)0,                       0 },
 30593 #endif
 30595 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
 30597 #if defined(SQLITE_WIN32_HAS_WIDE)
 30598   { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
 30599 #else
 30600   { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
 30601 #endif
 30603 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
 30604         LPVOID))aSyscall[22].pCurrent)
 30606 #if !SQLITE_OS_WINRT
 30607   { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
 30608 #else
 30609   { "GetFileSize",             (SYSCALL)0,                       0 },
 30610 #endif
 30612 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
 30614 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
 30615   { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
 30616 #else
 30617   { "GetFullPathNameA",        (SYSCALL)0,                       0 },
 30618 #endif
 30620 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
 30621         LPSTR*))aSyscall[24].pCurrent)
 30623 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 30624   { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
 30625 #else
 30626   { "GetFullPathNameW",        (SYSCALL)0,                       0 },
 30627 #endif
 30629 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
 30630         LPWSTR*))aSyscall[25].pCurrent)
 30632   { "GetLastError",            (SYSCALL)GetLastError,            0 },
 30634 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
 30636 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
 30637 #if SQLITE_OS_WINCE
 30638   /* The GetProcAddressA() routine is only available on Windows CE. */
 30639   { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
 30640 #else
 30641   /* All other Windows platforms expect GetProcAddress() to take
 30642   ** an ANSI string regardless of the _UNICODE setting */
 30643   { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
 30644 #endif
 30645 #else
 30646   { "GetProcAddressA",         (SYSCALL)0,                       0 },
 30647 #endif
 30649 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
 30650         LPCSTR))aSyscall[27].pCurrent)
 30652 #if !SQLITE_OS_WINRT
 30653   { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
 30654 #else
 30655   { "GetSystemInfo",           (SYSCALL)0,                       0 },
 30656 #endif
 30658 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
 30660   { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
 30662 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
 30664 #if !SQLITE_OS_WINCE
 30665   { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
 30666 #else
 30667   { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
 30668 #endif
 30670 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
 30671         LPFILETIME))aSyscall[30].pCurrent)
 30673 #if defined(SQLITE_WIN32_HAS_ANSI)
 30674   { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
 30675 #else
 30676   { "GetTempPathA",            (SYSCALL)0,                       0 },
 30677 #endif
 30679 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
 30681 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 30682   { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
 30683 #else
 30684   { "GetTempPathW",            (SYSCALL)0,                       0 },
 30685 #endif
 30687 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
 30689 #if !SQLITE_OS_WINRT
 30690   { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
 30691 #else
 30692   { "GetTickCount",            (SYSCALL)0,                       0 },
 30693 #endif
 30695 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
 30697 #if defined(SQLITE_WIN32_HAS_ANSI)
 30698   { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
 30699 #else
 30700   { "GetVersionExA",           (SYSCALL)0,                       0 },
 30701 #endif
 30703 #define osGetVersionExA ((BOOL(WINAPI*)( \
 30704         LPOSVERSIONINFOA))aSyscall[34].pCurrent)
 30706   { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
 30708 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
 30709         SIZE_T))aSyscall[35].pCurrent)
 30711 #if !SQLITE_OS_WINRT
 30712   { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
 30713 #else
 30714   { "HeapCreate",              (SYSCALL)0,                       0 },
 30715 #endif
 30717 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
 30718         SIZE_T))aSyscall[36].pCurrent)
 30720 #if !SQLITE_OS_WINRT
 30721   { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
 30722 #else
 30723   { "HeapDestroy",             (SYSCALL)0,                       0 },
 30724 #endif
 30726 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
 30728   { "HeapFree",                (SYSCALL)HeapFree,                0 },
 30730 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
 30732   { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
 30734 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
 30735         SIZE_T))aSyscall[39].pCurrent)
 30737   { "HeapSize",                (SYSCALL)HeapSize,                0 },
 30739 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
 30740         LPCVOID))aSyscall[40].pCurrent)
 30742 #if !SQLITE_OS_WINRT
 30743   { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
 30744 #else
 30745   { "HeapValidate",            (SYSCALL)0,                       0 },
 30746 #endif
 30748 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
 30749         LPCVOID))aSyscall[41].pCurrent)
 30751 #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
 30752   { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
 30753 #else
 30754   { "LoadLibraryA",            (SYSCALL)0,                       0 },
 30755 #endif
 30757 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
 30759 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
 30760         !defined(SQLITE_OMIT_LOAD_EXTENSION)
 30761   { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
 30762 #else
 30763   { "LoadLibraryW",            (SYSCALL)0,                       0 },
 30764 #endif
 30766 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
 30768 #if !SQLITE_OS_WINRT
 30769   { "LocalFree",               (SYSCALL)LocalFree,               0 },
 30770 #else
 30771   { "LocalFree",               (SYSCALL)0,                       0 },
 30772 #endif
 30774 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
 30776 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 30777   { "LockFile",                (SYSCALL)LockFile,                0 },
 30778 #else
 30779   { "LockFile",                (SYSCALL)0,                       0 },
 30780 #endif
 30782 #ifndef osLockFile
 30783 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 30784         DWORD))aSyscall[45].pCurrent)
 30785 #endif
 30787 #if !SQLITE_OS_WINCE
 30788   { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
 30789 #else
 30790   { "LockFileEx",              (SYSCALL)0,                       0 },
 30791 #endif
 30793 #ifndef osLockFileEx
 30794 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
 30795         LPOVERLAPPED))aSyscall[46].pCurrent)
 30796 #endif
 30798 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
 30799   { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
 30800 #else
 30801   { "MapViewOfFile",           (SYSCALL)0,                       0 },
 30802 #endif
 30804 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 30805         SIZE_T))aSyscall[47].pCurrent)
 30807   { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
 30809 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
 30810         int))aSyscall[48].pCurrent)
 30812   { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
 30814 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
 30815         LARGE_INTEGER*))aSyscall[49].pCurrent)
 30817   { "ReadFile",                (SYSCALL)ReadFile,                0 },
 30819 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
 30820         LPOVERLAPPED))aSyscall[50].pCurrent)
 30822   { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
 30824 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
 30826 #if !SQLITE_OS_WINRT
 30827   { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
 30828 #else
 30829   { "SetFilePointer",          (SYSCALL)0,                       0 },
 30830 #endif
 30832 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
 30833         DWORD))aSyscall[52].pCurrent)
 30835 #if !SQLITE_OS_WINRT
 30836   { "Sleep",                   (SYSCALL)Sleep,                   0 },
 30837 #else
 30838   { "Sleep",                   (SYSCALL)0,                       0 },
 30839 #endif
 30841 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
 30843   { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
 30845 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
 30846         LPFILETIME))aSyscall[54].pCurrent)
 30848 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 30849   { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
 30850 #else
 30851   { "UnlockFile",              (SYSCALL)0,                       0 },
 30852 #endif
 30854 #ifndef osUnlockFile
 30855 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 30856         DWORD))aSyscall[55].pCurrent)
 30857 #endif
 30859 #if !SQLITE_OS_WINCE
 30860   { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
 30861 #else
 30862   { "UnlockFileEx",            (SYSCALL)0,                       0 },
 30863 #endif
 30865 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 30866         LPOVERLAPPED))aSyscall[56].pCurrent)
 30868 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
 30869   { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
 30870 #else
 30871   { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
 30872 #endif
 30874 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
 30876   { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
 30878 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
 30879         LPCSTR,LPBOOL))aSyscall[58].pCurrent)
 30881   { "WriteFile",               (SYSCALL)WriteFile,               0 },
 30883 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
 30884         LPOVERLAPPED))aSyscall[59].pCurrent)
 30886 #if SQLITE_OS_WINRT
 30887   { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
 30888 #else
 30889   { "CreateEventExW",          (SYSCALL)0,                       0 },
 30890 #endif
 30892 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
 30893         DWORD,DWORD))aSyscall[60].pCurrent)
 30895 #if !SQLITE_OS_WINRT
 30896   { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
 30897 #else
 30898   { "WaitForSingleObject",     (SYSCALL)0,                       0 },
 30899 #endif
 30901 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
 30902         DWORD))aSyscall[61].pCurrent)
 30904 #if SQLITE_OS_WINRT
 30905   { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
 30906 #else
 30907   { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
 30908 #endif
 30910 #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
 30911         BOOL))aSyscall[62].pCurrent)
 30913 #if SQLITE_OS_WINRT
 30914   { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
 30915 #else
 30916   { "SetFilePointerEx",        (SYSCALL)0,                       0 },
 30917 #endif
 30919 #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
 30920         PLARGE_INTEGER,DWORD))aSyscall[63].pCurrent)
 30922 #if SQLITE_OS_WINRT
 30923   { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
 30924 #else
 30925   { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
 30926 #endif
 30928 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
 30929         FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[64].pCurrent)
 30931 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 30932   { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
 30933 #else
 30934   { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
 30935 #endif
 30937 #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
 30938         SIZE_T))aSyscall[65].pCurrent)
 30940 #if SQLITE_OS_WINRT
 30941   { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
 30942 #else
 30943   { "CreateFile2",             (SYSCALL)0,                       0 },
 30944 #endif
 30946 #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
 30947         LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[66].pCurrent)
 30949 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
 30950   { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
 30951 #else
 30952   { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
 30953 #endif
 30955 #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
 30956         DWORD))aSyscall[67].pCurrent)
 30958 #if SQLITE_OS_WINRT
 30959   { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
 30960 #else
 30961   { "GetTickCount64",          (SYSCALL)0,                       0 },
 30962 #endif
 30964 #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[68].pCurrent)
 30966 #if SQLITE_OS_WINRT
 30967   { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
 30968 #else
 30969   { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
 30970 #endif
 30972 #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
 30973         LPSYSTEM_INFO))aSyscall[69].pCurrent)
 30975 #if defined(SQLITE_WIN32_HAS_ANSI)
 30976   { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
 30977 #else
 30978   { "OutputDebugStringA",      (SYSCALL)0,                       0 },
 30979 #endif
 30981 #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[70].pCurrent)
 30983 #if defined(SQLITE_WIN32_HAS_WIDE)
 30984   { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
 30985 #else
 30986   { "OutputDebugStringW",      (SYSCALL)0,                       0 },
 30987 #endif
 30989 #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[71].pCurrent)
 30991   { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
 30993 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[72].pCurrent)
 30995 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 30996   { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
 30997 #else
 30998   { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
 30999 #endif
 31001 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
 31002         LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[73].pCurrent)
 31004 }; /* End of the overrideable system calls */
 31006 /*
 31007 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 31008 ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
 31009 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
 31010 ** system call named zName.
 31011 */
 31012 static int winSetSystemCall(
 31013   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
 31014   const char *zName,            /* Name of system call to override */
 31015   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
 31016 ){
 31017   unsigned int i;
 31018   int rc = SQLITE_NOTFOUND;
 31020   UNUSED_PARAMETER(pNotUsed);
 31021   if( zName==0 ){
 31022     /* If no zName is given, restore all system calls to their default
 31023     ** settings and return NULL
 31024     */
 31025     rc = SQLITE_OK;
 31026     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 31027       if( aSyscall[i].pDefault ){
 31028         aSyscall[i].pCurrent = aSyscall[i].pDefault;
 31031   }else{
 31032     /* If zName is specified, operate on only the one system call
 31033     ** specified.
 31034     */
 31035     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 31036       if( strcmp(zName, aSyscall[i].zName)==0 ){
 31037         if( aSyscall[i].pDefault==0 ){
 31038           aSyscall[i].pDefault = aSyscall[i].pCurrent;
 31040         rc = SQLITE_OK;
 31041         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
 31042         aSyscall[i].pCurrent = pNewFunc;
 31043         break;
 31047   return rc;
 31050 /*
 31051 ** Return the value of a system call.  Return NULL if zName is not a
 31052 ** recognized system call name.  NULL is also returned if the system call
 31053 ** is currently undefined.
 31054 */
 31055 static sqlite3_syscall_ptr winGetSystemCall(
 31056   sqlite3_vfs *pNotUsed,
 31057   const char *zName
 31058 ){
 31059   unsigned int i;
 31061   UNUSED_PARAMETER(pNotUsed);
 31062   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 31063     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
 31065   return 0;
 31068 /*
 31069 ** Return the name of the first system call after zName.  If zName==NULL
 31070 ** then return the name of the first system call.  Return NULL if zName
 31071 ** is the last system call or if zName is not the name of a valid
 31072 ** system call.
 31073 */
 31074 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
 31075   int i = -1;
 31077   UNUSED_PARAMETER(p);
 31078   if( zName ){
 31079     for(i=0; i<ArraySize(aSyscall)-1; i++){
 31080       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
 31083   for(i++; i<ArraySize(aSyscall); i++){
 31084     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
 31086   return 0;
 31089 /*
 31090 ** This function outputs the specified (ANSI) string to the Win32 debugger
 31091 ** (if available).
 31092 */
 31094 SQLITE_API void sqlite3_win32_write_debug(char *zBuf, int nBuf){
 31095   char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
 31096   int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
 31097   if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
 31098   assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
 31099 #if defined(SQLITE_WIN32_HAS_ANSI)
 31100   if( nMin>0 ){
 31101     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 31102     memcpy(zDbgBuf, zBuf, nMin);
 31103     osOutputDebugStringA(zDbgBuf);
 31104   }else{
 31105     osOutputDebugStringA(zBuf);
 31107 #elif defined(SQLITE_WIN32_HAS_WIDE)
 31108   memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 31109   if ( osMultiByteToWideChar(
 31110           osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
 31111           nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
 31112     return;
 31114   osOutputDebugStringW((LPCWSTR)zDbgBuf);
 31115 #else
 31116   if( nMin>0 ){
 31117     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 31118     memcpy(zDbgBuf, zBuf, nMin);
 31119     fprintf(stderr, "%s", zDbgBuf);
 31120   }else{
 31121     fprintf(stderr, "%s", zBuf);
 31123 #endif
 31126 /*
 31127 ** The following routine suspends the current thread for at least ms
 31128 ** milliseconds.  This is equivalent to the Win32 Sleep() interface.
 31129 */
 31130 #if SQLITE_OS_WINRT
 31131 static HANDLE sleepObj = NULL;
 31132 #endif
 31134 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
 31135 #if SQLITE_OS_WINRT
 31136   if ( sleepObj==NULL ){
 31137     sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
 31138                                 SYNCHRONIZE);
 31140   assert( sleepObj!=NULL );
 31141   osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
 31142 #else
 31143   osSleep(milliseconds);
 31144 #endif
 31147 /*
 31148 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
 31149 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
 31150 **
 31151 ** Here is an interesting observation:  Win95, Win98, and WinME lack
 31152 ** the LockFileEx() API.  But we can still statically link against that
 31153 ** API as long as we don't call it when running Win95/98/ME.  A call to
 31154 ** this routine is used to determine if the host is Win95/98/ME or
 31155 ** WinNT/2K/XP so that we will know whether or not we can safely call
 31156 ** the LockFileEx() API.
 31157 */
 31158 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 31159 # define isNT()  (1)
 31160 #elif !defined(SQLITE_WIN32_HAS_WIDE)
 31161 # define isNT()  (0)
 31162 #else
 31163   static int isNT(void){
 31164     if( sqlite3_os_type==0 ){
 31165       OSVERSIONINFOA sInfo;
 31166       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
 31167       osGetVersionExA(&sInfo);
 31168       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
 31170     return sqlite3_os_type==2;
 31172 #endif
 31174 #ifdef SQLITE_WIN32_MALLOC
 31175 /*
 31176 ** Allocate nBytes of memory.
 31177 */
 31178 static void *winMemMalloc(int nBytes){
 31179   HANDLE hHeap;
 31180   void *p;
 31182   winMemAssertMagic();
 31183   hHeap = winMemGetHeap();
 31184   assert( hHeap!=0 );
 31185   assert( hHeap!=INVALID_HANDLE_VALUE );
 31186 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 31187   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 31188 #endif
 31189   assert( nBytes>=0 );
 31190   p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
 31191   if( !p ){
 31192     sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
 31193                 nBytes, osGetLastError(), (void*)hHeap);
 31195   return p;
 31198 /*
 31199 ** Free memory.
 31200 */
 31201 static void winMemFree(void *pPrior){
 31202   HANDLE hHeap;
 31204   winMemAssertMagic();
 31205   hHeap = winMemGetHeap();
 31206   assert( hHeap!=0 );
 31207   assert( hHeap!=INVALID_HANDLE_VALUE );
 31208 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 31209   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
 31210 #endif
 31211   if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
 31212   if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
 31213     sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
 31214                 pPrior, osGetLastError(), (void*)hHeap);
 31218 /*
 31219 ** Change the size of an existing memory allocation
 31220 */
 31221 static void *winMemRealloc(void *pPrior, int nBytes){
 31222   HANDLE hHeap;
 31223   void *p;
 31225   winMemAssertMagic();
 31226   hHeap = winMemGetHeap();
 31227   assert( hHeap!=0 );
 31228   assert( hHeap!=INVALID_HANDLE_VALUE );
 31229 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 31230   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
 31231 #endif
 31232   assert( nBytes>=0 );
 31233   if( !pPrior ){
 31234     p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
 31235   }else{
 31236     p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
 31238   if( !p ){
 31239     sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
 31240                 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
 31241                 (void*)hHeap);
 31243   return p;
 31246 /*
 31247 ** Return the size of an outstanding allocation, in bytes.
 31248 */
 31249 static int winMemSize(void *p){
 31250   HANDLE hHeap;
 31251   SIZE_T n;
 31253   winMemAssertMagic();
 31254   hHeap = winMemGetHeap();
 31255   assert( hHeap!=0 );
 31256   assert( hHeap!=INVALID_HANDLE_VALUE );
 31257 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 31258   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 31259 #endif
 31260   if( !p ) return 0;
 31261   n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
 31262   if( n==(SIZE_T)-1 ){
 31263     sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
 31264                 p, osGetLastError(), (void*)hHeap);
 31265     return 0;
 31267   return (int)n;
 31270 /*
 31271 ** Round up a request size to the next valid allocation size.
 31272 */
 31273 static int winMemRoundup(int n){
 31274   return n;
 31277 /*
 31278 ** Initialize this module.
 31279 */
 31280 static int winMemInit(void *pAppData){
 31281   winMemData *pWinMemData = (winMemData *)pAppData;
 31283   if( !pWinMemData ) return SQLITE_ERROR;
 31284   assert( pWinMemData->magic==WINMEM_MAGIC );
 31286 #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
 31287   if( !pWinMemData->hHeap ){
 31288     pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
 31289                                       SQLITE_WIN32_HEAP_INIT_SIZE,
 31290                                       SQLITE_WIN32_HEAP_MAX_SIZE);
 31291     if( !pWinMemData->hHeap ){
 31292       sqlite3_log(SQLITE_NOMEM,
 31293           "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
 31294           osGetLastError(), SQLITE_WIN32_HEAP_FLAGS,
 31295           SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE);
 31296       return SQLITE_NOMEM;
 31298     pWinMemData->bOwned = TRUE;
 31299     assert( pWinMemData->bOwned );
 31301 #else
 31302   pWinMemData->hHeap = osGetProcessHeap();
 31303   if( !pWinMemData->hHeap ){
 31304     sqlite3_log(SQLITE_NOMEM,
 31305         "failed to GetProcessHeap (%d)", osGetLastError());
 31306     return SQLITE_NOMEM;
 31308   pWinMemData->bOwned = FALSE;
 31309   assert( !pWinMemData->bOwned );
 31310 #endif
 31311   assert( pWinMemData->hHeap!=0 );
 31312   assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
 31313 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 31314   assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 31315 #endif
 31316   return SQLITE_OK;
 31319 /*
 31320 ** Deinitialize this module.
 31321 */
 31322 static void winMemShutdown(void *pAppData){
 31323   winMemData *pWinMemData = (winMemData *)pAppData;
 31325   if( !pWinMemData ) return;
 31326   if( pWinMemData->hHeap ){
 31327     assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
 31328 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 31329     assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 31330 #endif
 31331     if( pWinMemData->bOwned ){
 31332       if( !osHeapDestroy(pWinMemData->hHeap) ){
 31333         sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
 31334                     osGetLastError(), (void*)pWinMemData->hHeap);
 31336       pWinMemData->bOwned = FALSE;
 31338     pWinMemData->hHeap = NULL;
 31342 /*
 31343 ** Populate the low-level memory allocation function pointers in
 31344 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
 31345 ** arguments specify the block of memory to manage.
 31346 **
 31347 ** This routine is only called by sqlite3_config(), and therefore
 31348 ** is not required to be threadsafe (it is not).
 31349 */
 31350 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
 31351   static const sqlite3_mem_methods winMemMethods = {
 31352     winMemMalloc,
 31353     winMemFree,
 31354     winMemRealloc,
 31355     winMemSize,
 31356     winMemRoundup,
 31357     winMemInit,
 31358     winMemShutdown,
 31359     &win_mem_data
 31360   };
 31361   return &winMemMethods;
 31364 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 31365   sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
 31367 #endif /* SQLITE_WIN32_MALLOC */
 31369 /*
 31370 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
 31371 **
 31372 ** Space to hold the returned string is obtained from malloc.
 31373 */
 31374 static LPWSTR utf8ToUnicode(const char *zFilename){
 31375   int nChar;
 31376   LPWSTR zWideFilename;
 31378   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
 31379   if( nChar==0 ){
 31380     return 0;
 31382   zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
 31383   if( zWideFilename==0 ){
 31384     return 0;
 31386   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
 31387                                 nChar);
 31388   if( nChar==0 ){
 31389     sqlite3_free(zWideFilename);
 31390     zWideFilename = 0;
 31392   return zWideFilename;
 31395 /*
 31396 ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
 31397 ** obtained from sqlite3_malloc().
 31398 */
 31399 static char *unicodeToUtf8(LPCWSTR zWideFilename){
 31400   int nByte;
 31401   char *zFilename;
 31403   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
 31404   if( nByte == 0 ){
 31405     return 0;
 31407   zFilename = sqlite3MallocZero( nByte );
 31408   if( zFilename==0 ){
 31409     return 0;
 31411   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
 31412                                 0, 0);
 31413   if( nByte == 0 ){
 31414     sqlite3_free(zFilename);
 31415     zFilename = 0;
 31417   return zFilename;
 31420 /*
 31421 ** Convert an ANSI string to Microsoft Unicode, based on the
 31422 ** current codepage settings for file apis.
 31423 ** 
 31424 ** Space to hold the returned string is obtained
 31425 ** from sqlite3_malloc.
 31426 */
 31427 static LPWSTR mbcsToUnicode(const char *zFilename){
 31428   int nByte;
 31429   LPWSTR zMbcsFilename;
 31430   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
 31432   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
 31433                                 0)*sizeof(WCHAR);
 31434   if( nByte==0 ){
 31435     return 0;
 31437   zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
 31438   if( zMbcsFilename==0 ){
 31439     return 0;
 31441   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
 31442                                 nByte);
 31443   if( nByte==0 ){
 31444     sqlite3_free(zMbcsFilename);
 31445     zMbcsFilename = 0;
 31447   return zMbcsFilename;
 31450 /*
 31451 ** Convert Microsoft Unicode to multi-byte character string, based on the
 31452 ** user's ANSI codepage.
 31453 **
 31454 ** Space to hold the returned string is obtained from
 31455 ** sqlite3_malloc().
 31456 */
 31457 static char *unicodeToMbcs(LPCWSTR zWideFilename){
 31458   int nByte;
 31459   char *zFilename;
 31460   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
 31462   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
 31463   if( nByte == 0 ){
 31464     return 0;
 31466   zFilename = sqlite3MallocZero( nByte );
 31467   if( zFilename==0 ){
 31468     return 0;
 31470   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
 31471                                 nByte, 0, 0);
 31472   if( nByte == 0 ){
 31473     sqlite3_free(zFilename);
 31474     zFilename = 0;
 31476   return zFilename;
 31479 /*
 31480 ** Convert multibyte character string to UTF-8.  Space to hold the
 31481 ** returned string is obtained from sqlite3_malloc().
 31482 */
 31483 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
 31484   char *zFilenameUtf8;
 31485   LPWSTR zTmpWide;
 31487   zTmpWide = mbcsToUnicode(zFilename);
 31488   if( zTmpWide==0 ){
 31489     return 0;
 31491   zFilenameUtf8 = unicodeToUtf8(zTmpWide);
 31492   sqlite3_free(zTmpWide);
 31493   return zFilenameUtf8;
 31496 /*
 31497 ** Convert UTF-8 to multibyte character string.  Space to hold the 
 31498 ** returned string is obtained from sqlite3_malloc().
 31499 */
 31500 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
 31501   char *zFilenameMbcs;
 31502   LPWSTR zTmpWide;
 31504   zTmpWide = utf8ToUnicode(zFilename);
 31505   if( zTmpWide==0 ){
 31506     return 0;
 31508   zFilenameMbcs = unicodeToMbcs(zTmpWide);
 31509   sqlite3_free(zTmpWide);
 31510   return zFilenameMbcs;
 31513 /*
 31514 ** This function sets the data directory or the temporary directory based on
 31515 ** the provided arguments.  The type argument must be 1 in order to set the
 31516 ** data directory or 2 in order to set the temporary directory.  The zValue
 31517 ** argument is the name of the directory to use.  The return value will be
 31518 ** SQLITE_OK if successful.
 31519 */
 31520 SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
 31521   char **ppDirectory = 0;
 31522 #ifndef SQLITE_OMIT_AUTOINIT
 31523   int rc = sqlite3_initialize();
 31524   if( rc ) return rc;
 31525 #endif
 31526   if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
 31527     ppDirectory = &sqlite3_data_directory;
 31528   }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
 31529     ppDirectory = &sqlite3_temp_directory;
 31531   assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
 31532           || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 31533   );
 31534   assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
 31535   if( ppDirectory ){
 31536     char *zValueUtf8 = 0;
 31537     if( zValue && zValue[0] ){
 31538       zValueUtf8 = unicodeToUtf8(zValue);
 31539       if ( zValueUtf8==0 ){
 31540         return SQLITE_NOMEM;
 31543     sqlite3_free(*ppDirectory);
 31544     *ppDirectory = zValueUtf8;
 31545     return SQLITE_OK;
 31547   return SQLITE_ERROR;
 31550 /*
 31551 ** The return value of getLastErrorMsg
 31552 ** is zero if the error message fits in the buffer, or non-zero
 31553 ** otherwise (if the message was truncated).
 31554 */
 31555 static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
 31556   /* FormatMessage returns 0 on failure.  Otherwise it
 31557   ** returns the number of TCHARs written to the output
 31558   ** buffer, excluding the terminating null char.
 31559   */
 31560   DWORD dwLen = 0;
 31561   char *zOut = 0;
 31563   if( isNT() ){
 31564 #if SQLITE_OS_WINRT
 31565     WCHAR zTempWide[MAX_PATH+1]; /* NOTE: Somewhat arbitrary. */
 31566     dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
 31567                              FORMAT_MESSAGE_IGNORE_INSERTS,
 31568                              NULL,
 31569                              lastErrno,
 31570                              0,
 31571                              zTempWide,
 31572                              MAX_PATH,
 31573                              0);
 31574 #else
 31575     LPWSTR zTempWide = NULL;
 31576     dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
 31577                              FORMAT_MESSAGE_FROM_SYSTEM |
 31578                              FORMAT_MESSAGE_IGNORE_INSERTS,
 31579                              NULL,
 31580                              lastErrno,
 31581                              0,
 31582                              (LPWSTR) &zTempWide,
 31583                              0,
 31584                              0);
 31585 #endif
 31586     if( dwLen > 0 ){
 31587       /* allocate a buffer and convert to UTF8 */
 31588       sqlite3BeginBenignMalloc();
 31589       zOut = unicodeToUtf8(zTempWide);
 31590       sqlite3EndBenignMalloc();
 31591 #if !SQLITE_OS_WINRT
 31592       /* free the system buffer allocated by FormatMessage */
 31593       osLocalFree(zTempWide);
 31594 #endif
 31597 #ifdef SQLITE_WIN32_HAS_ANSI
 31598   else{
 31599     char *zTemp = NULL;
 31600     dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
 31601                              FORMAT_MESSAGE_FROM_SYSTEM |
 31602                              FORMAT_MESSAGE_IGNORE_INSERTS,
 31603                              NULL,
 31604                              lastErrno,
 31605                              0,
 31606                              (LPSTR) &zTemp,
 31607                              0,
 31608                              0);
 31609     if( dwLen > 0 ){
 31610       /* allocate a buffer and convert to UTF8 */
 31611       sqlite3BeginBenignMalloc();
 31612       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
 31613       sqlite3EndBenignMalloc();
 31614       /* free the system buffer allocated by FormatMessage */
 31615       osLocalFree(zTemp);
 31618 #endif
 31619   if( 0 == dwLen ){
 31620     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
 31621   }else{
 31622     /* copy a maximum of nBuf chars to output buffer */
 31623     sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
 31624     /* free the UTF8 buffer */
 31625     sqlite3_free(zOut);
 31627   return 0;
 31630 /*
 31631 **
 31632 ** This function - winLogErrorAtLine() - is only ever called via the macro
 31633 ** winLogError().
 31634 **
 31635 ** This routine is invoked after an error occurs in an OS function.
 31636 ** It logs a message using sqlite3_log() containing the current value of
 31637 ** error code and, if possible, the human-readable equivalent from 
 31638 ** FormatMessage.
 31639 **
 31640 ** The first argument passed to the macro should be the error code that
 31641 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
 31642 ** The two subsequent arguments should be the name of the OS function that
 31643 ** failed and the associated file-system path, if any.
 31644 */
 31645 #define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
 31646 static int winLogErrorAtLine(
 31647   int errcode,                    /* SQLite error code */
 31648   DWORD lastErrno,                /* Win32 last error */
 31649   const char *zFunc,              /* Name of OS function that failed */
 31650   const char *zPath,              /* File path associated with error */
 31651   int iLine                       /* Source line number where error occurred */
 31652 ){
 31653   char zMsg[500];                 /* Human readable error text */
 31654   int i;                          /* Loop counter */
 31656   zMsg[0] = 0;
 31657   getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
 31658   assert( errcode!=SQLITE_OK );
 31659   if( zPath==0 ) zPath = "";
 31660   for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
 31661   zMsg[i] = 0;
 31662   sqlite3_log(errcode,
 31663       "os_win.c:%d: (%d) %s(%s) - %s",
 31664       iLine, lastErrno, zFunc, zPath, zMsg
 31665   );
 31667   return errcode;
 31670 /*
 31671 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
 31672 ** will be retried following a locking error - probably caused by 
 31673 ** antivirus software.  Also the initial delay before the first retry.
 31674 ** The delay increases linearly with each retry.
 31675 */
 31676 #ifndef SQLITE_WIN32_IOERR_RETRY
 31677 # define SQLITE_WIN32_IOERR_RETRY 10
 31678 #endif
 31679 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
 31680 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
 31681 #endif
 31682 static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
 31683 static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
 31685 /*
 31686 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
 31687 ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
 31688 ** to give up with an error.
 31689 */
 31690 static int retryIoerr(int *pnRetry, DWORD *pError){
 31691   DWORD e = osGetLastError();
 31692   if( *pnRetry>=win32IoerrRetry ){
 31693     if( pError ){
 31694       *pError = e;
 31696     return 0;
 31698   if( e==ERROR_ACCESS_DENIED ||
 31699       e==ERROR_LOCK_VIOLATION ||
 31700       e==ERROR_SHARING_VIOLATION ){
 31701     sqlite3_win32_sleep(win32IoerrRetryDelay*(1+*pnRetry));
 31702     ++*pnRetry;
 31703     return 1;
 31705   if( pError ){
 31706     *pError = e;
 31708   return 0;
 31711 /*
 31712 ** Log a I/O error retry episode.
 31713 */
 31714 static void logIoerr(int nRetry){
 31715   if( nRetry ){
 31716     sqlite3_log(SQLITE_IOERR, 
 31717       "delayed %dms for lock/sharing conflict",
 31718       win32IoerrRetryDelay*nRetry*(nRetry+1)/2
 31719     );
 31723 #if SQLITE_OS_WINCE
 31724 /*************************************************************************
 31725 ** This section contains code for WinCE only.
 31726 */
 31727 /*
 31728 ** Windows CE does not have a localtime() function.  So create a
 31729 ** substitute.
 31730 */
 31731 /* #include <time.h> */
 31732 struct tm *__cdecl localtime(const time_t *t)
 31734   static struct tm y;
 31735   FILETIME uTm, lTm;
 31736   SYSTEMTIME pTm;
 31737   sqlite3_int64 t64;
 31738   t64 = *t;
 31739   t64 = (t64 + 11644473600)*10000000;
 31740   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
 31741   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
 31742   osFileTimeToLocalFileTime(&uTm,&lTm);
 31743   osFileTimeToSystemTime(&lTm,&pTm);
 31744   y.tm_year = pTm.wYear - 1900;
 31745   y.tm_mon = pTm.wMonth - 1;
 31746   y.tm_wday = pTm.wDayOfWeek;
 31747   y.tm_mday = pTm.wDay;
 31748   y.tm_hour = pTm.wHour;
 31749   y.tm_min = pTm.wMinute;
 31750   y.tm_sec = pTm.wSecond;
 31751   return &y;
 31754 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
 31756 /*
 31757 ** Acquire a lock on the handle h
 31758 */
 31759 static void winceMutexAcquire(HANDLE h){
 31760    DWORD dwErr;
 31761    do {
 31762      dwErr = osWaitForSingleObject(h, INFINITE);
 31763    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
 31765 /*
 31766 ** Release a lock acquired by winceMutexAcquire()
 31767 */
 31768 #define winceMutexRelease(h) ReleaseMutex(h)
 31770 /*
 31771 ** Create the mutex and shared memory used for locking in the file
 31772 ** descriptor pFile
 31773 */
 31774 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
 31775   LPWSTR zTok;
 31776   LPWSTR zName;
 31777   BOOL bInit = TRUE;
 31779   zName = utf8ToUnicode(zFilename);
 31780   if( zName==0 ){
 31781     /* out of memory */
 31782     return FALSE;
 31785   /* Initialize the local lockdata */
 31786   memset(&pFile->local, 0, sizeof(pFile->local));
 31788   /* Replace the backslashes from the filename and lowercase it
 31789   ** to derive a mutex name. */
 31790   zTok = osCharLowerW(zName);
 31791   for (;*zTok;zTok++){
 31792     if (*zTok == '\\') *zTok = '_';
 31795   /* Create/open the named mutex */
 31796   pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
 31797   if (!pFile->hMutex){
 31798     pFile->lastErrno = osGetLastError();
 31799     winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
 31800     sqlite3_free(zName);
 31801     return FALSE;
 31804   /* Acquire the mutex before continuing */
 31805   winceMutexAcquire(pFile->hMutex);
 31807   /* Since the names of named mutexes, semaphores, file mappings etc are 
 31808   ** case-sensitive, take advantage of that by uppercasing the mutex name
 31809   ** and using that as the shared filemapping name.
 31810   */
 31811   osCharUpperW(zName);
 31812   pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
 31813                                         PAGE_READWRITE, 0, sizeof(winceLock),
 31814                                         zName);  
 31816   /* Set a flag that indicates we're the first to create the memory so it 
 31817   ** must be zero-initialized */
 31818   if (osGetLastError() == ERROR_ALREADY_EXISTS){
 31819     bInit = FALSE;
 31822   sqlite3_free(zName);
 31824   /* If we succeeded in making the shared memory handle, map it. */
 31825   if (pFile->hShared){
 31826     pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
 31827              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
 31828     /* If mapping failed, close the shared memory handle and erase it */
 31829     if (!pFile->shared){
 31830       pFile->lastErrno = osGetLastError();
 31831       winLogError(SQLITE_ERROR, pFile->lastErrno,
 31832                "winceCreateLock2", zFilename);
 31833       osCloseHandle(pFile->hShared);
 31834       pFile->hShared = NULL;
 31838   /* If shared memory could not be created, then close the mutex and fail */
 31839   if (pFile->hShared == NULL){
 31840     winceMutexRelease(pFile->hMutex);
 31841     osCloseHandle(pFile->hMutex);
 31842     pFile->hMutex = NULL;
 31843     return FALSE;
 31846   /* Initialize the shared memory if we're supposed to */
 31847   if (bInit) {
 31848     memset(pFile->shared, 0, sizeof(winceLock));
 31851   winceMutexRelease(pFile->hMutex);
 31852   return TRUE;
 31855 /*
 31856 ** Destroy the part of winFile that deals with wince locks
 31857 */
 31858 static void winceDestroyLock(winFile *pFile){
 31859   if (pFile->hMutex){
 31860     /* Acquire the mutex */
 31861     winceMutexAcquire(pFile->hMutex);
 31863     /* The following blocks should probably assert in debug mode, but they
 31864        are to cleanup in case any locks remained open */
 31865     if (pFile->local.nReaders){
 31866       pFile->shared->nReaders --;
 31868     if (pFile->local.bReserved){
 31869       pFile->shared->bReserved = FALSE;
 31871     if (pFile->local.bPending){
 31872       pFile->shared->bPending = FALSE;
 31874     if (pFile->local.bExclusive){
 31875       pFile->shared->bExclusive = FALSE;
 31878     /* De-reference and close our copy of the shared memory handle */
 31879     osUnmapViewOfFile(pFile->shared);
 31880     osCloseHandle(pFile->hShared);
 31882     /* Done with the mutex */
 31883     winceMutexRelease(pFile->hMutex);    
 31884     osCloseHandle(pFile->hMutex);
 31885     pFile->hMutex = NULL;
 31889 /* 
 31890 ** An implementation of the LockFile() API of Windows for CE
 31891 */
 31892 static BOOL winceLockFile(
 31893   LPHANDLE phFile,
 31894   DWORD dwFileOffsetLow,
 31895   DWORD dwFileOffsetHigh,
 31896   DWORD nNumberOfBytesToLockLow,
 31897   DWORD nNumberOfBytesToLockHigh
 31898 ){
 31899   winFile *pFile = HANDLE_TO_WINFILE(phFile);
 31900   BOOL bReturn = FALSE;
 31902   UNUSED_PARAMETER(dwFileOffsetHigh);
 31903   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
 31905   if (!pFile->hMutex) return TRUE;
 31906   winceMutexAcquire(pFile->hMutex);
 31908   /* Wanting an exclusive lock? */
 31909   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
 31910        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
 31911     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
 31912        pFile->shared->bExclusive = TRUE;
 31913        pFile->local.bExclusive = TRUE;
 31914        bReturn = TRUE;
 31918   /* Want a read-only lock? */
 31919   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
 31920            nNumberOfBytesToLockLow == 1){
 31921     if (pFile->shared->bExclusive == 0){
 31922       pFile->local.nReaders ++;
 31923       if (pFile->local.nReaders == 1){
 31924         pFile->shared->nReaders ++;
 31926       bReturn = TRUE;
 31930   /* Want a pending lock? */
 31931   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
 31932     /* If no pending lock has been acquired, then acquire it */
 31933     if (pFile->shared->bPending == 0) {
 31934       pFile->shared->bPending = TRUE;
 31935       pFile->local.bPending = TRUE;
 31936       bReturn = TRUE;
 31940   /* Want a reserved lock? */
 31941   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
 31942     if (pFile->shared->bReserved == 0) {
 31943       pFile->shared->bReserved = TRUE;
 31944       pFile->local.bReserved = TRUE;
 31945       bReturn = TRUE;
 31949   winceMutexRelease(pFile->hMutex);
 31950   return bReturn;
 31953 /*
 31954 ** An implementation of the UnlockFile API of Windows for CE
 31955 */
 31956 static BOOL winceUnlockFile(
 31957   LPHANDLE phFile,
 31958   DWORD dwFileOffsetLow,
 31959   DWORD dwFileOffsetHigh,
 31960   DWORD nNumberOfBytesToUnlockLow,
 31961   DWORD nNumberOfBytesToUnlockHigh
 31962 ){
 31963   winFile *pFile = HANDLE_TO_WINFILE(phFile);
 31964   BOOL bReturn = FALSE;
 31966   UNUSED_PARAMETER(dwFileOffsetHigh);
 31967   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
 31969   if (!pFile->hMutex) return TRUE;
 31970   winceMutexAcquire(pFile->hMutex);
 31972   /* Releasing a reader lock or an exclusive lock */
 31973   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
 31974     /* Did we have an exclusive lock? */
 31975     if (pFile->local.bExclusive){
 31976       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
 31977       pFile->local.bExclusive = FALSE;
 31978       pFile->shared->bExclusive = FALSE;
 31979       bReturn = TRUE;
 31982     /* Did we just have a reader lock? */
 31983     else if (pFile->local.nReaders){
 31984       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
 31985       pFile->local.nReaders --;
 31986       if (pFile->local.nReaders == 0)
 31988         pFile->shared->nReaders --;
 31990       bReturn = TRUE;
 31994   /* Releasing a pending lock */
 31995   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
 31996     if (pFile->local.bPending){
 31997       pFile->local.bPending = FALSE;
 31998       pFile->shared->bPending = FALSE;
 31999       bReturn = TRUE;
 32002   /* Releasing a reserved lock */
 32003   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
 32004     if (pFile->local.bReserved) {
 32005       pFile->local.bReserved = FALSE;
 32006       pFile->shared->bReserved = FALSE;
 32007       bReturn = TRUE;
 32011   winceMutexRelease(pFile->hMutex);
 32012   return bReturn;
 32014 /*
 32015 ** End of the special code for wince
 32016 *****************************************************************************/
 32017 #endif /* SQLITE_OS_WINCE */
 32019 /*
 32020 ** Lock a file region.
 32021 */
 32022 static BOOL winLockFile(
 32023   LPHANDLE phFile,
 32024   DWORD flags,
 32025   DWORD offsetLow,
 32026   DWORD offsetHigh,
 32027   DWORD numBytesLow,
 32028   DWORD numBytesHigh
 32029 ){
 32030 #if SQLITE_OS_WINCE
 32031   /*
 32032   ** NOTE: Windows CE is handled differently here due its lack of the Win32
 32033   **       API LockFile.
 32034   */
 32035   return winceLockFile(phFile, offsetLow, offsetHigh,
 32036                        numBytesLow, numBytesHigh);
 32037 #else
 32038   if( isNT() ){
 32039     OVERLAPPED ovlp;
 32040     memset(&ovlp, 0, sizeof(OVERLAPPED));
 32041     ovlp.Offset = offsetLow;
 32042     ovlp.OffsetHigh = offsetHigh;
 32043     return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
 32044   }else{
 32045     return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
 32046                       numBytesHigh);
 32048 #endif
 32051 /*
 32052 ** Unlock a file region.
 32053  */
 32054 static BOOL winUnlockFile(
 32055   LPHANDLE phFile,
 32056   DWORD offsetLow,
 32057   DWORD offsetHigh,
 32058   DWORD numBytesLow,
 32059   DWORD numBytesHigh
 32060 ){
 32061 #if SQLITE_OS_WINCE
 32062   /*
 32063   ** NOTE: Windows CE is handled differently here due its lack of the Win32
 32064   **       API UnlockFile.
 32065   */
 32066   return winceUnlockFile(phFile, offsetLow, offsetHigh,
 32067                          numBytesLow, numBytesHigh);
 32068 #else
 32069   if( isNT() ){
 32070     OVERLAPPED ovlp;
 32071     memset(&ovlp, 0, sizeof(OVERLAPPED));
 32072     ovlp.Offset = offsetLow;
 32073     ovlp.OffsetHigh = offsetHigh;
 32074     return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
 32075   }else{
 32076     return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
 32077                         numBytesHigh);
 32079 #endif
 32082 /*****************************************************************************
 32083 ** The next group of routines implement the I/O methods specified
 32084 ** by the sqlite3_io_methods object.
 32085 ******************************************************************************/
 32087 /*
 32088 ** Some Microsoft compilers lack this definition.
 32089 */
 32090 #ifndef INVALID_SET_FILE_POINTER
 32091 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
 32092 #endif
 32094 /*
 32095 ** Move the current position of the file handle passed as the first 
 32096 ** argument to offset iOffset within the file. If successful, return 0. 
 32097 ** Otherwise, set pFile->lastErrno and return non-zero.
 32098 */
 32099 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
 32100 #if !SQLITE_OS_WINRT
 32101   LONG upperBits;                 /* Most sig. 32 bits of new offset */
 32102   LONG lowerBits;                 /* Least sig. 32 bits of new offset */
 32103   DWORD dwRet;                    /* Value returned by SetFilePointer() */
 32104   DWORD lastErrno;                /* Value returned by GetLastError() */
 32106   upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
 32107   lowerBits = (LONG)(iOffset & 0xffffffff);
 32109   /* API oddity: If successful, SetFilePointer() returns a dword 
 32110   ** containing the lower 32-bits of the new file-offset. Or, if it fails,
 32111   ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
 32112   ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
 32113   ** whether an error has actually occured, it is also necessary to call 
 32114   ** GetLastError().
 32115   */
 32116   dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
 32118   if( (dwRet==INVALID_SET_FILE_POINTER
 32119       && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
 32120     pFile->lastErrno = lastErrno;
 32121     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
 32122              "seekWinFile", pFile->zPath);
 32123     return 1;
 32126   return 0;
 32127 #else
 32128   /*
 32129   ** Same as above, except that this implementation works for WinRT.
 32130   */
 32132   LARGE_INTEGER x;                /* The new offset */
 32133   BOOL bRet;                      /* Value returned by SetFilePointerEx() */
 32135   x.QuadPart = iOffset;
 32136   bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
 32138   if(!bRet){
 32139     pFile->lastErrno = osGetLastError();
 32140     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
 32141              "seekWinFile", pFile->zPath);
 32142     return 1;
 32145   return 0;
 32146 #endif
 32149 /*
 32150 ** Close a file.
 32151 **
 32152 ** It is reported that an attempt to close a handle might sometimes
 32153 ** fail.  This is a very unreasonable result, but Windows is notorious
 32154 ** for being unreasonable so I do not doubt that it might happen.  If
 32155 ** the close fails, we pause for 100 milliseconds and try again.  As
 32156 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
 32157 ** giving up and returning an error.
 32158 */
 32159 #define MX_CLOSE_ATTEMPT 3
 32160 static int winClose(sqlite3_file *id){
 32161   int rc, cnt = 0;
 32162   winFile *pFile = (winFile*)id;
 32164   assert( id!=0 );
 32165 #ifndef SQLITE_OMIT_WAL
 32166   assert( pFile->pShm==0 );
 32167 #endif
 32168   OSTRACE(("CLOSE %d\n", pFile->h));
 32169   do{
 32170     rc = osCloseHandle(pFile->h);
 32171     /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
 32172   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
 32173 #if SQLITE_OS_WINCE
 32174 #define WINCE_DELETION_ATTEMPTS 3
 32175   winceDestroyLock(pFile);
 32176   if( pFile->zDeleteOnClose ){
 32177     int cnt = 0;
 32178     while(
 32179            osDeleteFileW(pFile->zDeleteOnClose)==0
 32180         && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
 32181         && cnt++ < WINCE_DELETION_ATTEMPTS
 32182     ){
 32183        sqlite3_win32_sleep(100);  /* Wait a little before trying again */
 32185     sqlite3_free(pFile->zDeleteOnClose);
 32187 #endif
 32188   OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
 32189   if( rc ){
 32190     pFile->h = NULL;
 32192   OpenCounter(-1);
 32193   return rc ? SQLITE_OK
 32194             : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
 32195                           "winClose", pFile->zPath);
 32198 /*
 32199 ** Read data from a file into a buffer.  Return SQLITE_OK if all
 32200 ** bytes were read successfully and SQLITE_IOERR if anything goes
 32201 ** wrong.
 32202 */
 32203 static int winRead(
 32204   sqlite3_file *id,          /* File to read from */
 32205   void *pBuf,                /* Write content into this buffer */
 32206   int amt,                   /* Number of bytes to read */
 32207   sqlite3_int64 offset       /* Begin reading at this offset */
 32208 ){
 32209 #if !SQLITE_OS_WINCE
 32210   OVERLAPPED overlapped;          /* The offset for ReadFile. */
 32211 #endif
 32212   winFile *pFile = (winFile*)id;  /* file handle */
 32213   DWORD nRead;                    /* Number of bytes actually read from file */
 32214   int nRetry = 0;                 /* Number of retrys */
 32216   assert( id!=0 );
 32217   SimulateIOError(return SQLITE_IOERR_READ);
 32218   OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
 32220 #if SQLITE_OS_WINCE
 32221   if( seekWinFile(pFile, offset) ){
 32222     return SQLITE_FULL;
 32224   while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
 32225 #else
 32226   memset(&overlapped, 0, sizeof(OVERLAPPED));
 32227   overlapped.Offset = (LONG)(offset & 0xffffffff);
 32228   overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 32229   while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
 32230          osGetLastError()!=ERROR_HANDLE_EOF ){
 32231 #endif
 32232     DWORD lastErrno;
 32233     if( retryIoerr(&nRetry, &lastErrno) ) continue;
 32234     pFile->lastErrno = lastErrno;
 32235     return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
 32236              "winRead", pFile->zPath);
 32238   logIoerr(nRetry);
 32239   if( nRead<(DWORD)amt ){
 32240     /* Unread parts of the buffer must be zero-filled */
 32241     memset(&((char*)pBuf)[nRead], 0, amt-nRead);
 32242     return SQLITE_IOERR_SHORT_READ;
 32245   return SQLITE_OK;
 32248 /*
 32249 ** Write data from a buffer into a file.  Return SQLITE_OK on success
 32250 ** or some other error code on failure.
 32251 */
 32252 static int winWrite(
 32253   sqlite3_file *id,               /* File to write into */
 32254   const void *pBuf,               /* The bytes to be written */
 32255   int amt,                        /* Number of bytes to write */
 32256   sqlite3_int64 offset            /* Offset into the file to begin writing at */
 32257 ){
 32258   int rc = 0;                     /* True if error has occured, else false */
 32259   winFile *pFile = (winFile*)id;  /* File handle */
 32260   int nRetry = 0;                 /* Number of retries */
 32262   assert( amt>0 );
 32263   assert( pFile );
 32264   SimulateIOError(return SQLITE_IOERR_WRITE);
 32265   SimulateDiskfullError(return SQLITE_FULL);
 32267   OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
 32269 #if SQLITE_OS_WINCE
 32270   rc = seekWinFile(pFile, offset);
 32271   if( rc==0 ){
 32272 #else
 32274 #endif
 32275 #if !SQLITE_OS_WINCE
 32276     OVERLAPPED overlapped;        /* The offset for WriteFile. */
 32277 #endif
 32278     u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
 32279     int nRem = amt;               /* Number of bytes yet to be written */
 32280     DWORD nWrite;                 /* Bytes written by each WriteFile() call */
 32281     DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
 32283 #if !SQLITE_OS_WINCE
 32284     memset(&overlapped, 0, sizeof(OVERLAPPED));
 32285     overlapped.Offset = (LONG)(offset & 0xffffffff);
 32286     overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 32287 #endif
 32289     while( nRem>0 ){
 32290 #if SQLITE_OS_WINCE
 32291       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
 32292 #else
 32293       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
 32294 #endif
 32295         if( retryIoerr(&nRetry, &lastErrno) ) continue;
 32296         break;
 32298       assert( nWrite==0 || nWrite<=(DWORD)nRem );
 32299       if( nWrite==0 || nWrite>(DWORD)nRem ){
 32300         lastErrno = osGetLastError();
 32301         break;
 32303 #if !SQLITE_OS_WINCE
 32304       offset += nWrite;
 32305       overlapped.Offset = (LONG)(offset & 0xffffffff);
 32306       overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 32307 #endif
 32308       aRem += nWrite;
 32309       nRem -= nWrite;
 32311     if( nRem>0 ){
 32312       pFile->lastErrno = lastErrno;
 32313       rc = 1;
 32317   if( rc ){
 32318     if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
 32319        || ( pFile->lastErrno==ERROR_DISK_FULL )){
 32320       return SQLITE_FULL;
 32322     return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
 32323              "winWrite", pFile->zPath);
 32324   }else{
 32325     logIoerr(nRetry);
 32327   return SQLITE_OK;
 32330 /*
 32331 ** Truncate an open file to a specified size
 32332 */
 32333 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
 32334   winFile *pFile = (winFile*)id;  /* File handle object */
 32335   int rc = SQLITE_OK;             /* Return code for this function */
 32337   assert( pFile );
 32339   OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
 32340   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
 32342   /* If the user has configured a chunk-size for this file, truncate the
 32343   ** file so that it consists of an integer number of chunks (i.e. the
 32344   ** actual file size after the operation may be larger than the requested
 32345   ** size).
 32346   */
 32347   if( pFile->szChunk>0 ){
 32348     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 32351   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
 32352   if( seekWinFile(pFile, nByte) ){
 32353     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
 32354              "winTruncate1", pFile->zPath);
 32355   }else if( 0==osSetEndOfFile(pFile->h) ){
 32356     pFile->lastErrno = osGetLastError();
 32357     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
 32358              "winTruncate2", pFile->zPath);
 32361   OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
 32362   return rc;
 32365 #ifdef SQLITE_TEST
 32366 /*
 32367 ** Count the number of fullsyncs and normal syncs.  This is used to test
 32368 ** that syncs and fullsyncs are occuring at the right times.
 32369 */
 32370 SQLITE_API int sqlite3_sync_count = 0;
 32371 SQLITE_API int sqlite3_fullsync_count = 0;
 32372 #endif
 32374 /*
 32375 ** Make sure all writes to a particular file are committed to disk.
 32376 */
 32377 static int winSync(sqlite3_file *id, int flags){
 32378 #ifndef SQLITE_NO_SYNC
 32379   /*
 32380   ** Used only when SQLITE_NO_SYNC is not defined.
 32381    */
 32382   BOOL rc;
 32383 #endif
 32384 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
 32385     (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
 32386   /*
 32387   ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
 32388   ** OSTRACE() macros.
 32389    */
 32390   winFile *pFile = (winFile*)id;
 32391 #else
 32392   UNUSED_PARAMETER(id);
 32393 #endif
 32395   assert( pFile );
 32396   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
 32397   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
 32398       || (flags&0x0F)==SQLITE_SYNC_FULL
 32399   );
 32401   OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
 32403   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
 32404   ** line is to test that doing so does not cause any problems.
 32405   */
 32406   SimulateDiskfullError( return SQLITE_FULL );
 32408 #ifndef SQLITE_TEST
 32409   UNUSED_PARAMETER(flags);
 32410 #else
 32411   if( (flags&0x0F)==SQLITE_SYNC_FULL ){
 32412     sqlite3_fullsync_count++;
 32414   sqlite3_sync_count++;
 32415 #endif
 32417   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 32418   ** no-op
 32419   */
 32420 #ifdef SQLITE_NO_SYNC
 32421   return SQLITE_OK;
 32422 #else
 32423   rc = osFlushFileBuffers(pFile->h);
 32424   SimulateIOError( rc=FALSE );
 32425   if( rc ){
 32426     return SQLITE_OK;
 32427   }else{
 32428     pFile->lastErrno = osGetLastError();
 32429     return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
 32430              "winSync", pFile->zPath);
 32432 #endif
 32435 /*
 32436 ** Determine the current size of a file in bytes
 32437 */
 32438 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
 32439   winFile *pFile = (winFile*)id;
 32440   int rc = SQLITE_OK;
 32442   assert( id!=0 );
 32443   SimulateIOError(return SQLITE_IOERR_FSTAT);
 32444 #if SQLITE_OS_WINRT
 32446     FILE_STANDARD_INFO info;
 32447     if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
 32448                                      &info, sizeof(info)) ){
 32449       *pSize = info.EndOfFile.QuadPart;
 32450     }else{
 32451       pFile->lastErrno = osGetLastError();
 32452       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
 32453                        "winFileSize", pFile->zPath);
 32456 #else
 32458     DWORD upperBits;
 32459     DWORD lowerBits;
 32460     DWORD lastErrno;
 32462     lowerBits = osGetFileSize(pFile->h, &upperBits);
 32463     *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
 32464     if(   (lowerBits == INVALID_FILE_SIZE)
 32465        && ((lastErrno = osGetLastError())!=NO_ERROR) ){
 32466       pFile->lastErrno = lastErrno;
 32467       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
 32468              "winFileSize", pFile->zPath);
 32471 #endif
 32472   return rc;
 32475 /*
 32476 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
 32477 */
 32478 #ifndef LOCKFILE_FAIL_IMMEDIATELY
 32479 # define LOCKFILE_FAIL_IMMEDIATELY 1
 32480 #endif
 32482 #ifndef LOCKFILE_EXCLUSIVE_LOCK
 32483 # define LOCKFILE_EXCLUSIVE_LOCK 2
 32484 #endif
 32486 /*
 32487 ** Historically, SQLite has used both the LockFile and LockFileEx functions.
 32488 ** When the LockFile function was used, it was always expected to fail
 32489 ** immediately if the lock could not be obtained.  Also, it always expected to
 32490 ** obtain an exclusive lock.  These flags are used with the LockFileEx function
 32491 ** and reflect those expectations; therefore, they should not be changed.
 32492 */
 32493 #ifndef SQLITE_LOCKFILE_FLAGS
 32494 # define SQLITE_LOCKFILE_FLAGS   (LOCKFILE_FAIL_IMMEDIATELY | \
 32495                                   LOCKFILE_EXCLUSIVE_LOCK)
 32496 #endif
 32498 /*
 32499 ** Currently, SQLite never calls the LockFileEx function without wanting the
 32500 ** call to fail immediately if the lock cannot be obtained.
 32501 */
 32502 #ifndef SQLITE_LOCKFILEEX_FLAGS
 32503 # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
 32504 #endif
 32506 /*
 32507 ** Acquire a reader lock.
 32508 ** Different API routines are called depending on whether or not this
 32509 ** is Win9x or WinNT.
 32510 */
 32511 static int getReadLock(winFile *pFile){
 32512   int res;
 32513   if( isNT() ){
 32514 #if SQLITE_OS_WINCE
 32515     /*
 32516     ** NOTE: Windows CE is handled differently here due its lack of the Win32
 32517     **       API LockFileEx.
 32518     */
 32519     res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
 32520 #else
 32521     res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
 32522                       SHARED_SIZE, 0);
 32523 #endif
 32525 #ifdef SQLITE_WIN32_HAS_ANSI
 32526   else{
 32527     int lk;
 32528     sqlite3_randomness(sizeof(lk), &lk);
 32529     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
 32530     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
 32531                       SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
 32533 #endif
 32534   if( res == 0 ){
 32535     pFile->lastErrno = osGetLastError();
 32536     /* No need to log a failure to lock */
 32538   return res;
 32541 /*
 32542 ** Undo a readlock
 32543 */
 32544 static int unlockReadLock(winFile *pFile){
 32545   int res;
 32546   DWORD lastErrno;
 32547   if( isNT() ){
 32548     res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
 32550 #ifdef SQLITE_WIN32_HAS_ANSI
 32551   else{
 32552     res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
 32554 #endif
 32555   if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
 32556     pFile->lastErrno = lastErrno;
 32557     winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
 32558              "unlockReadLock", pFile->zPath);
 32560   return res;
 32563 /*
 32564 ** Lock the file with the lock specified by parameter locktype - one
 32565 ** of the following:
 32566 **
 32567 **     (1) SHARED_LOCK
 32568 **     (2) RESERVED_LOCK
 32569 **     (3) PENDING_LOCK
 32570 **     (4) EXCLUSIVE_LOCK
 32571 **
 32572 ** Sometimes when requesting one lock state, additional lock states
 32573 ** are inserted in between.  The locking might fail on one of the later
 32574 ** transitions leaving the lock state different from what it started but
 32575 ** still short of its goal.  The following chart shows the allowed
 32576 ** transitions and the inserted intermediate states:
 32577 **
 32578 **    UNLOCKED -> SHARED
 32579 **    SHARED -> RESERVED
 32580 **    SHARED -> (PENDING) -> EXCLUSIVE
 32581 **    RESERVED -> (PENDING) -> EXCLUSIVE
 32582 **    PENDING -> EXCLUSIVE
 32583 **
 32584 ** This routine will only increase a lock.  The winUnlock() routine
 32585 ** erases all locks at once and returns us immediately to locking level 0.
 32586 ** It is not possible to lower the locking level one step at a time.  You
 32587 ** must go straight to locking level 0.
 32588 */
 32589 static int winLock(sqlite3_file *id, int locktype){
 32590   int rc = SQLITE_OK;    /* Return code from subroutines */
 32591   int res = 1;           /* Result of a Windows lock call */
 32592   int newLocktype;       /* Set pFile->locktype to this value before exiting */
 32593   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
 32594   winFile *pFile = (winFile*)id;
 32595   DWORD lastErrno = NO_ERROR;
 32597   assert( id!=0 );
 32598   OSTRACE(("LOCK %d %d was %d(%d)\n",
 32599            pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
 32601   /* If there is already a lock of this type or more restrictive on the
 32602   ** OsFile, do nothing. Don't use the end_lock: exit path, as
 32603   ** sqlite3OsEnterMutex() hasn't been called yet.
 32604   */
 32605   if( pFile->locktype>=locktype ){
 32606     return SQLITE_OK;
 32609   /* Make sure the locking sequence is correct
 32610   */
 32611   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
 32612   assert( locktype!=PENDING_LOCK );
 32613   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
 32615   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
 32616   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
 32617   ** the PENDING_LOCK byte is temporary.
 32618   */
 32619   newLocktype = pFile->locktype;
 32620   if(   (pFile->locktype==NO_LOCK)
 32621      || (   (locktype==EXCLUSIVE_LOCK)
 32622          && (pFile->locktype==RESERVED_LOCK))
 32623   ){
 32624     int cnt = 3;
 32625     while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
 32626                                          PENDING_BYTE, 0, 1, 0))==0 ){
 32627       /* Try 3 times to get the pending lock.  This is needed to work
 32628       ** around problems caused by indexing and/or anti-virus software on
 32629       ** Windows systems.
 32630       ** If you are using this code as a model for alternative VFSes, do not
 32631       ** copy this retry logic.  It is a hack intended for Windows only.
 32632       */
 32633       OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
 32634       if( cnt ) sqlite3_win32_sleep(1);
 32636     gotPendingLock = res;
 32637     if( !res ){
 32638       lastErrno = osGetLastError();
 32642   /* Acquire a shared lock
 32643   */
 32644   if( locktype==SHARED_LOCK && res ){
 32645     assert( pFile->locktype==NO_LOCK );
 32646     res = getReadLock(pFile);
 32647     if( res ){
 32648       newLocktype = SHARED_LOCK;
 32649     }else{
 32650       lastErrno = osGetLastError();
 32654   /* Acquire a RESERVED lock
 32655   */
 32656   if( locktype==RESERVED_LOCK && res ){
 32657     assert( pFile->locktype==SHARED_LOCK );
 32658     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
 32659     if( res ){
 32660       newLocktype = RESERVED_LOCK;
 32661     }else{
 32662       lastErrno = osGetLastError();
 32666   /* Acquire a PENDING lock
 32667   */
 32668   if( locktype==EXCLUSIVE_LOCK && res ){
 32669     newLocktype = PENDING_LOCK;
 32670     gotPendingLock = 0;
 32673   /* Acquire an EXCLUSIVE lock
 32674   */
 32675   if( locktype==EXCLUSIVE_LOCK && res ){
 32676     assert( pFile->locktype>=SHARED_LOCK );
 32677     res = unlockReadLock(pFile);
 32678     OSTRACE(("unreadlock = %d\n", res));
 32679     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
 32680                       SHARED_SIZE, 0);
 32681     if( res ){
 32682       newLocktype = EXCLUSIVE_LOCK;
 32683     }else{
 32684       lastErrno = osGetLastError();
 32685       OSTRACE(("error-code = %d\n", lastErrno));
 32686       getReadLock(pFile);
 32690   /* If we are holding a PENDING lock that ought to be released, then
 32691   ** release it now.
 32692   */
 32693   if( gotPendingLock && locktype==SHARED_LOCK ){
 32694     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
 32697   /* Update the state of the lock has held in the file descriptor then
 32698   ** return the appropriate result code.
 32699   */
 32700   if( res ){
 32701     rc = SQLITE_OK;
 32702   }else{
 32703     OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
 32704            locktype, newLocktype));
 32705     pFile->lastErrno = lastErrno;
 32706     rc = SQLITE_BUSY;
 32708   pFile->locktype = (u8)newLocktype;
 32709   return rc;
 32712 /*
 32713 ** This routine checks if there is a RESERVED lock held on the specified
 32714 ** file by this or any other process. If such a lock is held, return
 32715 ** non-zero, otherwise zero.
 32716 */
 32717 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
 32718   int rc;
 32719   winFile *pFile = (winFile*)id;
 32721   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 32723   assert( id!=0 );
 32724   if( pFile->locktype>=RESERVED_LOCK ){
 32725     rc = 1;
 32726     OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
 32727   }else{
 32728     rc = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
 32729     if( rc ){
 32730       winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
 32732     rc = !rc;
 32733     OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
 32735   *pResOut = rc;
 32736   return SQLITE_OK;
 32739 /*
 32740 ** Lower the locking level on file descriptor id to locktype.  locktype
 32741 ** must be either NO_LOCK or SHARED_LOCK.
 32742 **
 32743 ** If the locking level of the file descriptor is already at or below
 32744 ** the requested locking level, this routine is a no-op.
 32745 **
 32746 ** It is not possible for this routine to fail if the second argument
 32747 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
 32748 ** might return SQLITE_IOERR;
 32749 */
 32750 static int winUnlock(sqlite3_file *id, int locktype){
 32751   int type;
 32752   winFile *pFile = (winFile*)id;
 32753   int rc = SQLITE_OK;
 32754   assert( pFile!=0 );
 32755   assert( locktype<=SHARED_LOCK );
 32756   OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
 32757           pFile->locktype, pFile->sharedLockByte));
 32758   type = pFile->locktype;
 32759   if( type>=EXCLUSIVE_LOCK ){
 32760     winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
 32761     if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
 32762       /* This should never happen.  We should always be able to
 32763       ** reacquire the read lock */
 32764       rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
 32765                "winUnlock", pFile->zPath);
 32768   if( type>=RESERVED_LOCK ){
 32769     winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
 32771   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
 32772     unlockReadLock(pFile);
 32774   if( type>=PENDING_LOCK ){
 32775     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
 32777   pFile->locktype = (u8)locktype;
 32778   return rc;
 32781 /*
 32782 ** If *pArg is inititially negative then this is a query.  Set *pArg to
 32783 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
 32784 **
 32785 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
 32786 */
 32787 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
 32788   if( *pArg<0 ){
 32789     *pArg = (pFile->ctrlFlags & mask)!=0;
 32790   }else if( (*pArg)==0 ){
 32791     pFile->ctrlFlags &= ~mask;
 32792   }else{
 32793     pFile->ctrlFlags |= mask;
 32797 /* Forward declaration */
 32798 static int getTempname(int nBuf, char *zBuf);
 32800 /*
 32801 ** Control and query of the open file handle.
 32802 */
 32803 static int winFileControl(sqlite3_file *id, int op, void *pArg){
 32804   winFile *pFile = (winFile*)id;
 32805   switch( op ){
 32806     case SQLITE_FCNTL_LOCKSTATE: {
 32807       *(int*)pArg = pFile->locktype;
 32808       return SQLITE_OK;
 32810     case SQLITE_LAST_ERRNO: {
 32811       *(int*)pArg = (int)pFile->lastErrno;
 32812       return SQLITE_OK;
 32814     case SQLITE_FCNTL_CHUNK_SIZE: {
 32815       pFile->szChunk = *(int *)pArg;
 32816       return SQLITE_OK;
 32818     case SQLITE_FCNTL_SIZE_HINT: {
 32819       if( pFile->szChunk>0 ){
 32820         sqlite3_int64 oldSz;
 32821         int rc = winFileSize(id, &oldSz);
 32822         if( rc==SQLITE_OK ){
 32823           sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
 32824           if( newSz>oldSz ){
 32825             SimulateIOErrorBenign(1);
 32826             rc = winTruncate(id, newSz);
 32827             SimulateIOErrorBenign(0);
 32830         return rc;
 32832       return SQLITE_OK;
 32834     case SQLITE_FCNTL_PERSIST_WAL: {
 32835       winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
 32836       return SQLITE_OK;
 32838     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
 32839       winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
 32840       return SQLITE_OK;
 32842     case SQLITE_FCNTL_VFSNAME: {
 32843       *(char**)pArg = sqlite3_mprintf("win32");
 32844       return SQLITE_OK;
 32846     case SQLITE_FCNTL_WIN32_AV_RETRY: {
 32847       int *a = (int*)pArg;
 32848       if( a[0]>0 ){
 32849         win32IoerrRetry = a[0];
 32850       }else{
 32851         a[0] = win32IoerrRetry;
 32853       if( a[1]>0 ){
 32854         win32IoerrRetryDelay = a[1];
 32855       }else{
 32856         a[1] = win32IoerrRetryDelay;
 32858       return SQLITE_OK;
 32860     case SQLITE_FCNTL_TEMPFILENAME: {
 32861       char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
 32862       if( zTFile ){
 32863         getTempname(pFile->pVfs->mxPathname, zTFile);
 32864         *(char**)pArg = zTFile;
 32866       return SQLITE_OK;
 32869   return SQLITE_NOTFOUND;
 32872 /*
 32873 ** Return the sector size in bytes of the underlying block device for
 32874 ** the specified file. This is almost always 512 bytes, but may be
 32875 ** larger for some devices.
 32876 **
 32877 ** SQLite code assumes this function cannot fail. It also assumes that
 32878 ** if two files are created in the same file-system directory (i.e.
 32879 ** a database and its journal file) that the sector size will be the
 32880 ** same for both.
 32881 */
 32882 static int winSectorSize(sqlite3_file *id){
 32883   (void)id;
 32884   return SQLITE_DEFAULT_SECTOR_SIZE;
 32887 /*
 32888 ** Return a vector of device characteristics.
 32889 */
 32890 static int winDeviceCharacteristics(sqlite3_file *id){
 32891   winFile *p = (winFile*)id;
 32892   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
 32893          ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
 32896 #ifndef SQLITE_OMIT_WAL
 32898 /* 
 32899 ** Windows will only let you create file view mappings
 32900 ** on allocation size granularity boundaries.
 32901 ** During sqlite3_os_init() we do a GetSystemInfo()
 32902 ** to get the granularity size.
 32903 */
 32904 SYSTEM_INFO winSysInfo;
 32906 /*
 32907 ** Helper functions to obtain and relinquish the global mutex. The
 32908 ** global mutex is used to protect the winLockInfo objects used by 
 32909 ** this file, all of which may be shared by multiple threads.
 32910 **
 32911 ** Function winShmMutexHeld() is used to assert() that the global mutex 
 32912 ** is held when required. This function is only used as part of assert() 
 32913 ** statements. e.g.
 32914 **
 32915 **   winShmEnterMutex()
 32916 **     assert( winShmMutexHeld() );
 32917 **   winShmLeaveMutex()
 32918 */
 32919 static void winShmEnterMutex(void){
 32920   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 32922 static void winShmLeaveMutex(void){
 32923   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 32925 #ifdef SQLITE_DEBUG
 32926 static int winShmMutexHeld(void) {
 32927   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 32929 #endif
 32931 /*
 32932 ** Object used to represent a single file opened and mmapped to provide
 32933 ** shared memory.  When multiple threads all reference the same
 32934 ** log-summary, each thread has its own winFile object, but they all
 32935 ** point to a single instance of this object.  In other words, each
 32936 ** log-summary is opened only once per process.
 32937 **
 32938 ** winShmMutexHeld() must be true when creating or destroying
 32939 ** this object or while reading or writing the following fields:
 32940 **
 32941 **      nRef
 32942 **      pNext 
 32943 **
 32944 ** The following fields are read-only after the object is created:
 32945 ** 
 32946 **      fid
 32947 **      zFilename
 32948 **
 32949 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
 32950 ** winShmMutexHeld() is true when reading or writing any other field
 32951 ** in this structure.
 32952 **
 32953 */
 32954 struct winShmNode {
 32955   sqlite3_mutex *mutex;      /* Mutex to access this object */
 32956   char *zFilename;           /* Name of the file */
 32957   winFile hFile;             /* File handle from winOpen */
 32959   int szRegion;              /* Size of shared-memory regions */
 32960   int nRegion;               /* Size of array apRegion */
 32961   struct ShmRegion {
 32962     HANDLE hMap;             /* File handle from CreateFileMapping */
 32963     void *pMap;
 32964   } *aRegion;
 32965   DWORD lastErrno;           /* The Windows errno from the last I/O error */
 32967   int nRef;                  /* Number of winShm objects pointing to this */
 32968   winShm *pFirst;            /* All winShm objects pointing to this */
 32969   winShmNode *pNext;         /* Next in list of all winShmNode objects */
 32970 #ifdef SQLITE_DEBUG
 32971   u8 nextShmId;              /* Next available winShm.id value */
 32972 #endif
 32973 };
 32975 /*
 32976 ** A global array of all winShmNode objects.
 32977 **
 32978 ** The winShmMutexHeld() must be true while reading or writing this list.
 32979 */
 32980 static winShmNode *winShmNodeList = 0;
 32982 /*
 32983 ** Structure used internally by this VFS to record the state of an
 32984 ** open shared memory connection.
 32985 **
 32986 ** The following fields are initialized when this object is created and
 32987 ** are read-only thereafter:
 32988 **
 32989 **    winShm.pShmNode
 32990 **    winShm.id
 32991 **
 32992 ** All other fields are read/write.  The winShm.pShmNode->mutex must be held
 32993 ** while accessing any read/write fields.
 32994 */
 32995 struct winShm {
 32996   winShmNode *pShmNode;      /* The underlying winShmNode object */
 32997   winShm *pNext;             /* Next winShm with the same winShmNode */
 32998   u8 hasMutex;               /* True if holding the winShmNode mutex */
 32999   u16 sharedMask;            /* Mask of shared locks held */
 33000   u16 exclMask;              /* Mask of exclusive locks held */
 33001 #ifdef SQLITE_DEBUG
 33002   u8 id;                     /* Id of this connection with its winShmNode */
 33003 #endif
 33004 };
 33006 /*
 33007 ** Constants used for locking
 33008 */
 33009 #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
 33010 #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 33012 /*
 33013 ** Apply advisory locks for all n bytes beginning at ofst.
 33014 */
 33015 #define _SHM_UNLCK  1
 33016 #define _SHM_RDLCK  2
 33017 #define _SHM_WRLCK  3
 33018 static int winShmSystemLock(
 33019   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
 33020   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
 33021   int ofst,             /* Offset to first byte to be locked/unlocked */
 33022   int nByte             /* Number of bytes to lock or unlock */
 33023 ){
 33024   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
 33026   /* Access to the winShmNode object is serialized by the caller */
 33027   assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
 33029   /* Release/Acquire the system-level lock */
 33030   if( lockType==_SHM_UNLCK ){
 33031     rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
 33032   }else{
 33033     /* Initialize the locking parameters */
 33034     DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
 33035     if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
 33036     rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
 33039   if( rc!= 0 ){
 33040     rc = SQLITE_OK;
 33041   }else{
 33042     pFile->lastErrno =  osGetLastError();
 33043     rc = SQLITE_BUSY;
 33046   OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
 33047            pFile->hFile.h,
 33048            rc==SQLITE_OK ? "ok" : "failed",
 33049            lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
 33050            pFile->lastErrno));
 33052   return rc;
 33055 /* Forward references to VFS methods */
 33056 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
 33057 static int winDelete(sqlite3_vfs *,const char*,int);
 33059 /*
 33060 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
 33061 **
 33062 ** This is not a VFS shared-memory method; it is a utility function called
 33063 ** by VFS shared-memory methods.
 33064 */
 33065 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
 33066   winShmNode **pp;
 33067   winShmNode *p;
 33068   BOOL bRc;
 33069   assert( winShmMutexHeld() );
 33070   pp = &winShmNodeList;
 33071   while( (p = *pp)!=0 ){
 33072     if( p->nRef==0 ){
 33073       int i;
 33074       if( p->mutex ) sqlite3_mutex_free(p->mutex);
 33075       for(i=0; i<p->nRegion; i++){
 33076         bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
 33077         OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
 33078                  (int)osGetCurrentProcessId(), i,
 33079                  bRc ? "ok" : "failed"));
 33080         bRc = osCloseHandle(p->aRegion[i].hMap);
 33081         OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
 33082                  (int)osGetCurrentProcessId(), i,
 33083                  bRc ? "ok" : "failed"));
 33085       if( p->hFile.h != INVALID_HANDLE_VALUE ){
 33086         SimulateIOErrorBenign(1);
 33087         winClose((sqlite3_file *)&p->hFile);
 33088         SimulateIOErrorBenign(0);
 33090       if( deleteFlag ){
 33091         SimulateIOErrorBenign(1);
 33092         sqlite3BeginBenignMalloc();
 33093         winDelete(pVfs, p->zFilename, 0);
 33094         sqlite3EndBenignMalloc();
 33095         SimulateIOErrorBenign(0);
 33097       *pp = p->pNext;
 33098       sqlite3_free(p->aRegion);
 33099       sqlite3_free(p);
 33100     }else{
 33101       pp = &p->pNext;
 33106 /*
 33107 ** Open the shared-memory area associated with database file pDbFd.
 33108 **
 33109 ** When opening a new shared-memory file, if no other instances of that
 33110 ** file are currently open, in this process or in other processes, then
 33111 ** the file must be truncated to zero length or have its header cleared.
 33112 */
 33113 static int winOpenSharedMemory(winFile *pDbFd){
 33114   struct winShm *p;                  /* The connection to be opened */
 33115   struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
 33116   int rc;                            /* Result code */
 33117   struct winShmNode *pNew;           /* Newly allocated winShmNode */
 33118   int nName;                         /* Size of zName in bytes */
 33120   assert( pDbFd->pShm==0 );    /* Not previously opened */
 33122   /* Allocate space for the new sqlite3_shm object.  Also speculatively
 33123   ** allocate space for a new winShmNode and filename.
 33124   */
 33125   p = sqlite3MallocZero( sizeof(*p) );
 33126   if( p==0 ) return SQLITE_IOERR_NOMEM;
 33127   nName = sqlite3Strlen30(pDbFd->zPath);
 33128   pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
 33129   if( pNew==0 ){
 33130     sqlite3_free(p);
 33131     return SQLITE_IOERR_NOMEM;
 33133   pNew->zFilename = (char*)&pNew[1];
 33134   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
 33135   sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
 33137   /* Look to see if there is an existing winShmNode that can be used.
 33138   ** If no matching winShmNode currently exists, create a new one.
 33139   */
 33140   winShmEnterMutex();
 33141   for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
 33142     /* TBD need to come up with better match here.  Perhaps
 33143     ** use FILE_ID_BOTH_DIR_INFO Structure.
 33144     */
 33145     if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
 33147   if( pShmNode ){
 33148     sqlite3_free(pNew);
 33149   }else{
 33150     pShmNode = pNew;
 33151     pNew = 0;
 33152     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
 33153     pShmNode->pNext = winShmNodeList;
 33154     winShmNodeList = pShmNode;
 33156     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 33157     if( pShmNode->mutex==0 ){
 33158       rc = SQLITE_IOERR_NOMEM;
 33159       goto shm_open_err;
 33162     rc = winOpen(pDbFd->pVfs,
 33163                  pShmNode->zFilename,             /* Name of the file (UTF-8) */
 33164                  (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
 33165                  SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
 33166                  0);
 33167     if( SQLITE_OK!=rc ){
 33168       goto shm_open_err;
 33171     /* Check to see if another process is holding the dead-man switch.
 33172     ** If not, truncate the file to zero length. 
 33173     */
 33174     if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
 33175       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
 33176       if( rc!=SQLITE_OK ){
 33177         rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
 33178                  "winOpenShm", pDbFd->zPath);
 33181     if( rc==SQLITE_OK ){
 33182       winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
 33183       rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
 33185     if( rc ) goto shm_open_err;
 33188   /* Make the new connection a child of the winShmNode */
 33189   p->pShmNode = pShmNode;
 33190 #ifdef SQLITE_DEBUG
 33191   p->id = pShmNode->nextShmId++;
 33192 #endif
 33193   pShmNode->nRef++;
 33194   pDbFd->pShm = p;
 33195   winShmLeaveMutex();
 33197   /* The reference count on pShmNode has already been incremented under
 33198   ** the cover of the winShmEnterMutex() mutex and the pointer from the
 33199   ** new (struct winShm) object to the pShmNode has been set. All that is
 33200   ** left to do is to link the new object into the linked list starting
 33201   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 33202   ** mutex.
 33203   */
 33204   sqlite3_mutex_enter(pShmNode->mutex);
 33205   p->pNext = pShmNode->pFirst;
 33206   pShmNode->pFirst = p;
 33207   sqlite3_mutex_leave(pShmNode->mutex);
 33208   return SQLITE_OK;
 33210   /* Jump here on any error */
 33211 shm_open_err:
 33212   winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
 33213   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
 33214   sqlite3_free(p);
 33215   sqlite3_free(pNew);
 33216   winShmLeaveMutex();
 33217   return rc;
 33220 /*
 33221 ** Close a connection to shared-memory.  Delete the underlying 
 33222 ** storage if deleteFlag is true.
 33223 */
 33224 static int winShmUnmap(
 33225   sqlite3_file *fd,          /* Database holding shared memory */
 33226   int deleteFlag             /* Delete after closing if true */
 33227 ){
 33228   winFile *pDbFd;       /* Database holding shared-memory */
 33229   winShm *p;            /* The connection to be closed */
 33230   winShmNode *pShmNode; /* The underlying shared-memory file */
 33231   winShm **pp;          /* For looping over sibling connections */
 33233   pDbFd = (winFile*)fd;
 33234   p = pDbFd->pShm;
 33235   if( p==0 ) return SQLITE_OK;
 33236   pShmNode = p->pShmNode;
 33238   /* Remove connection p from the set of connections associated
 33239   ** with pShmNode */
 33240   sqlite3_mutex_enter(pShmNode->mutex);
 33241   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 33242   *pp = p->pNext;
 33244   /* Free the connection p */
 33245   sqlite3_free(p);
 33246   pDbFd->pShm = 0;
 33247   sqlite3_mutex_leave(pShmNode->mutex);
 33249   /* If pShmNode->nRef has reached 0, then close the underlying
 33250   ** shared-memory file, too */
 33251   winShmEnterMutex();
 33252   assert( pShmNode->nRef>0 );
 33253   pShmNode->nRef--;
 33254   if( pShmNode->nRef==0 ){
 33255     winShmPurge(pDbFd->pVfs, deleteFlag);
 33257   winShmLeaveMutex();
 33259   return SQLITE_OK;
 33262 /*
 33263 ** Change the lock state for a shared-memory segment.
 33264 */
 33265 static int winShmLock(
 33266   sqlite3_file *fd,          /* Database file holding the shared memory */
 33267   int ofst,                  /* First lock to acquire or release */
 33268   int n,                     /* Number of locks to acquire or release */
 33269   int flags                  /* What to do with the lock */
 33270 ){
 33271   winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
 33272   winShm *p = pDbFd->pShm;              /* The shared memory being locked */
 33273   winShm *pX;                           /* For looping over all siblings */
 33274   winShmNode *pShmNode = p->pShmNode;
 33275   int rc = SQLITE_OK;                   /* Result code */
 33276   u16 mask;                             /* Mask of locks to take or release */
 33278   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 33279   assert( n>=1 );
 33280   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 33281        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 33282        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 33283        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 33284   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 33286   mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
 33287   assert( n>1 || mask==(1<<ofst) );
 33288   sqlite3_mutex_enter(pShmNode->mutex);
 33289   if( flags & SQLITE_SHM_UNLOCK ){
 33290     u16 allMask = 0; /* Mask of locks held by siblings */
 33292     /* See if any siblings hold this same lock */
 33293     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 33294       if( pX==p ) continue;
 33295       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 33296       allMask |= pX->sharedMask;
 33299     /* Unlock the system-level locks */
 33300     if( (mask & allMask)==0 ){
 33301       rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
 33302     }else{
 33303       rc = SQLITE_OK;
 33306     /* Undo the local locks */
 33307     if( rc==SQLITE_OK ){
 33308       p->exclMask &= ~mask;
 33309       p->sharedMask &= ~mask;
 33311   }else if( flags & SQLITE_SHM_SHARED ){
 33312     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
 33314     /* Find out which shared locks are already held by sibling connections.
 33315     ** If any sibling already holds an exclusive lock, go ahead and return
 33316     ** SQLITE_BUSY.
 33317     */
 33318     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 33319       if( (pX->exclMask & mask)!=0 ){
 33320         rc = SQLITE_BUSY;
 33321         break;
 33323       allShared |= pX->sharedMask;
 33326     /* Get shared locks at the system level, if necessary */
 33327     if( rc==SQLITE_OK ){
 33328       if( (allShared & mask)==0 ){
 33329         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
 33330       }else{
 33331         rc = SQLITE_OK;
 33335     /* Get the local shared locks */
 33336     if( rc==SQLITE_OK ){
 33337       p->sharedMask |= mask;
 33339   }else{
 33340     /* Make sure no sibling connections hold locks that will block this
 33341     ** lock.  If any do, return SQLITE_BUSY right away.
 33342     */
 33343     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 33344       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
 33345         rc = SQLITE_BUSY;
 33346         break;
 33350     /* Get the exclusive locks at the system level.  Then if successful
 33351     ** also mark the local connection as being locked.
 33352     */
 33353     if( rc==SQLITE_OK ){
 33354       rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
 33355       if( rc==SQLITE_OK ){
 33356         assert( (p->sharedMask & mask)==0 );
 33357         p->exclMask |= mask;
 33361   sqlite3_mutex_leave(pShmNode->mutex);
 33362   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
 33363            p->id, (int)osGetCurrentProcessId(), p->sharedMask, p->exclMask,
 33364            rc ? "failed" : "ok"));
 33365   return rc;
 33368 /*
 33369 ** Implement a memory barrier or memory fence on shared memory.  
 33370 **
 33371 ** All loads and stores begun before the barrier must complete before
 33372 ** any load or store begun after the barrier.
 33373 */
 33374 static void winShmBarrier(
 33375   sqlite3_file *fd          /* Database holding the shared memory */
 33376 ){
 33377   UNUSED_PARAMETER(fd);
 33378   /* MemoryBarrier(); // does not work -- do not know why not */
 33379   winShmEnterMutex();
 33380   winShmLeaveMutex();
 33383 /*
 33384 ** This function is called to obtain a pointer to region iRegion of the 
 33385 ** shared-memory associated with the database file fd. Shared-memory regions 
 33386 ** are numbered starting from zero. Each shared-memory region is szRegion 
 33387 ** bytes in size.
 33388 **
 33389 ** If an error occurs, an error code is returned and *pp is set to NULL.
 33390 **
 33391 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
 33392 ** region has not been allocated (by any client, including one running in a
 33393 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
 33394 ** isWrite is non-zero and the requested shared-memory region has not yet 
 33395 ** been allocated, it is allocated by this function.
 33396 **
 33397 ** If the shared-memory region has already been allocated or is allocated by
 33398 ** this call as described above, then it is mapped into this processes 
 33399 ** address space (if it is not already), *pp is set to point to the mapped 
 33400 ** memory and SQLITE_OK returned.
 33401 */
 33402 static int winShmMap(
 33403   sqlite3_file *fd,               /* Handle open on database file */
 33404   int iRegion,                    /* Region to retrieve */
 33405   int szRegion,                   /* Size of regions */
 33406   int isWrite,                    /* True to extend file if necessary */
 33407   void volatile **pp              /* OUT: Mapped memory */
 33408 ){
 33409   winFile *pDbFd = (winFile*)fd;
 33410   winShm *p = pDbFd->pShm;
 33411   winShmNode *pShmNode;
 33412   int rc = SQLITE_OK;
 33414   if( !p ){
 33415     rc = winOpenSharedMemory(pDbFd);
 33416     if( rc!=SQLITE_OK ) return rc;
 33417     p = pDbFd->pShm;
 33419   pShmNode = p->pShmNode;
 33421   sqlite3_mutex_enter(pShmNode->mutex);
 33422   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 33424   if( pShmNode->nRegion<=iRegion ){
 33425     struct ShmRegion *apNew;           /* New aRegion[] array */
 33426     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 33427     sqlite3_int64 sz;                  /* Current size of wal-index file */
 33429     pShmNode->szRegion = szRegion;
 33431     /* The requested region is not mapped into this processes address space.
 33432     ** Check to see if it has been allocated (i.e. if the wal-index file is
 33433     ** large enough to contain the requested region).
 33434     */
 33435     rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
 33436     if( rc!=SQLITE_OK ){
 33437       rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
 33438                "winShmMap1", pDbFd->zPath);
 33439       goto shmpage_out;
 33442     if( sz<nByte ){
 33443       /* The requested memory region does not exist. If isWrite is set to
 33444       ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
 33445       **
 33446       ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
 33447       ** the requested memory region.
 33448       */
 33449       if( !isWrite ) goto shmpage_out;
 33450       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
 33451       if( rc!=SQLITE_OK ){
 33452         rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
 33453                  "winShmMap2", pDbFd->zPath);
 33454         goto shmpage_out;
 33458     /* Map the requested memory region into this processes address space. */
 33459     apNew = (struct ShmRegion *)sqlite3_realloc(
 33460         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
 33461     );
 33462     if( !apNew ){
 33463       rc = SQLITE_IOERR_NOMEM;
 33464       goto shmpage_out;
 33466     pShmNode->aRegion = apNew;
 33468     while( pShmNode->nRegion<=iRegion ){
 33469       HANDLE hMap = NULL;         /* file-mapping handle */
 33470       void *pMap = 0;             /* Mapped memory region */
 33472 #if SQLITE_OS_WINRT
 33473       hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
 33474           NULL, PAGE_READWRITE, nByte, NULL
 33475       );
 33476 #elif defined(SQLITE_WIN32_HAS_WIDE)
 33477       hMap = osCreateFileMappingW(pShmNode->hFile.h, 
 33478           NULL, PAGE_READWRITE, 0, nByte, NULL
 33479       );
 33480 #elif defined(SQLITE_WIN32_HAS_ANSI)
 33481       hMap = osCreateFileMappingA(pShmNode->hFile.h, 
 33482           NULL, PAGE_READWRITE, 0, nByte, NULL
 33483       );
 33484 #endif
 33485       OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
 33486                (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
 33487                hMap ? "ok" : "failed"));
 33488       if( hMap ){
 33489         int iOffset = pShmNode->nRegion*szRegion;
 33490         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 33491 #if SQLITE_OS_WINRT
 33492         pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
 33493             iOffset - iOffsetShift, szRegion + iOffsetShift
 33494         );
 33495 #else
 33496         pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
 33497             0, iOffset - iOffsetShift, szRegion + iOffsetShift
 33498         );
 33499 #endif
 33500         OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
 33501                  (int)osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
 33502                  szRegion, pMap ? "ok" : "failed"));
 33504       if( !pMap ){
 33505         pShmNode->lastErrno = osGetLastError();
 33506         rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
 33507                  "winShmMap3", pDbFd->zPath);
 33508         if( hMap ) osCloseHandle(hMap);
 33509         goto shmpage_out;
 33512       pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
 33513       pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
 33514       pShmNode->nRegion++;
 33518 shmpage_out:
 33519   if( pShmNode->nRegion>iRegion ){
 33520     int iOffset = iRegion*szRegion;
 33521     int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 33522     char *p = (char *)pShmNode->aRegion[iRegion].pMap;
 33523     *pp = (void *)&p[iOffsetShift];
 33524   }else{
 33525     *pp = 0;
 33527   sqlite3_mutex_leave(pShmNode->mutex);
 33528   return rc;
 33531 #else
 33532 # define winShmMap     0
 33533 # define winShmLock    0
 33534 # define winShmBarrier 0
 33535 # define winShmUnmap   0
 33536 #endif /* #ifndef SQLITE_OMIT_WAL */
 33538 /*
 33539 ** Here ends the implementation of all sqlite3_file methods.
 33540 **
 33541 ********************** End sqlite3_file Methods *******************************
 33542 ******************************************************************************/
 33544 /*
 33545 ** This vector defines all the methods that can operate on an
 33546 ** sqlite3_file for win32.
 33547 */
 33548 static const sqlite3_io_methods winIoMethod = {
 33549   2,                              /* iVersion */
 33550   winClose,                       /* xClose */
 33551   winRead,                        /* xRead */
 33552   winWrite,                       /* xWrite */
 33553   winTruncate,                    /* xTruncate */
 33554   winSync,                        /* xSync */
 33555   winFileSize,                    /* xFileSize */
 33556   winLock,                        /* xLock */
 33557   winUnlock,                      /* xUnlock */
 33558   winCheckReservedLock,           /* xCheckReservedLock */
 33559   winFileControl,                 /* xFileControl */
 33560   winSectorSize,                  /* xSectorSize */
 33561   winDeviceCharacteristics,       /* xDeviceCharacteristics */
 33562   winShmMap,                      /* xShmMap */
 33563   winShmLock,                     /* xShmLock */
 33564   winShmBarrier,                  /* xShmBarrier */
 33565   winShmUnmap                     /* xShmUnmap */
 33566 };
 33568 /****************************************************************************
 33569 **************************** sqlite3_vfs methods ****************************
 33570 **
 33571 ** This division contains the implementation of methods on the
 33572 ** sqlite3_vfs object.
 33573 */
 33575 /*
 33576 ** Convert a UTF-8 filename into whatever form the underlying
 33577 ** operating system wants filenames in.  Space to hold the result
 33578 ** is obtained from malloc and must be freed by the calling
 33579 ** function.
 33580 */
 33581 static void *convertUtf8Filename(const char *zFilename){
 33582   void *zConverted = 0;
 33583   if( isNT() ){
 33584     zConverted = utf8ToUnicode(zFilename);
 33586 #ifdef SQLITE_WIN32_HAS_ANSI
 33587   else{
 33588     zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
 33590 #endif
 33591   /* caller will handle out of memory */
 33592   return zConverted;
 33595 /*
 33596 ** Create a temporary file name in zBuf.  zBuf must be big enough to
 33597 ** hold at pVfs->mxPathname characters.
 33598 */
 33599 static int getTempname(int nBuf, char *zBuf){
 33600   static char zChars[] =
 33601     "abcdefghijklmnopqrstuvwxyz"
 33602     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 33603     "0123456789";
 33604   size_t i, j;
 33605   int nTempPath;
 33606   char zTempPath[MAX_PATH+2];
 33608   /* It's odd to simulate an io-error here, but really this is just
 33609   ** using the io-error infrastructure to test that SQLite handles this
 33610   ** function failing. 
 33611   */
 33612   SimulateIOError( return SQLITE_IOERR );
 33614   memset(zTempPath, 0, MAX_PATH+2);
 33616   if( sqlite3_temp_directory ){
 33617     sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
 33619 #if !SQLITE_OS_WINRT
 33620   else if( isNT() ){
 33621     char *zMulti;
 33622     WCHAR zWidePath[MAX_PATH];
 33623     osGetTempPathW(MAX_PATH-30, zWidePath);
 33624     zMulti = unicodeToUtf8(zWidePath);
 33625     if( zMulti ){
 33626       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
 33627       sqlite3_free(zMulti);
 33628     }else{
 33629       return SQLITE_IOERR_NOMEM;
 33632 #ifdef SQLITE_WIN32_HAS_ANSI
 33633   else{
 33634     char *zUtf8;
 33635     char zMbcsPath[MAX_PATH];
 33636     osGetTempPathA(MAX_PATH-30, zMbcsPath);
 33637     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
 33638     if( zUtf8 ){
 33639       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
 33640       sqlite3_free(zUtf8);
 33641     }else{
 33642       return SQLITE_IOERR_NOMEM;
 33645 #endif
 33646 #endif
 33648   /* Check that the output buffer is large enough for the temporary file 
 33649   ** name. If it is not, return SQLITE_ERROR.
 33650   */
 33651   nTempPath = sqlite3Strlen30(zTempPath);
 33653   if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
 33654     return SQLITE_ERROR;
 33657   for(i=nTempPath; i>0 && zTempPath[i-1]=='\\'; i--){}
 33658   zTempPath[i] = 0;
 33660   sqlite3_snprintf(nBuf-18, zBuf, (nTempPath > 0) ?
 33661                        "%s\\"SQLITE_TEMP_FILE_PREFIX : SQLITE_TEMP_FILE_PREFIX,
 33662                    zTempPath);
 33663   j = sqlite3Strlen30(zBuf);
 33664   sqlite3_randomness(15, &zBuf[j]);
 33665   for(i=0; i<15; i++, j++){
 33666     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 33668   zBuf[j] = 0;
 33669   zBuf[j+1] = 0;
 33671   OSTRACE(("TEMP FILENAME: %s\n", zBuf));
 33672   return SQLITE_OK; 
 33675 /*
 33676 ** Return TRUE if the named file is really a directory.  Return false if
 33677 ** it is something other than a directory, or if there is any kind of memory
 33678 ** allocation failure.
 33679 */
 33680 static int winIsDir(const void *zConverted){
 33681   DWORD attr;
 33682   int rc = 0;
 33683   DWORD lastErrno;
 33685   if( isNT() ){
 33686     int cnt = 0;
 33687     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 33688     memset(&sAttrData, 0, sizeof(sAttrData));
 33689     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
 33690                              GetFileExInfoStandard,
 33691                              &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
 33692     if( !rc ){
 33693       return 0; /* Invalid name? */
 33695     attr = sAttrData.dwFileAttributes;
 33696 #if SQLITE_OS_WINCE==0
 33697   }else{
 33698     attr = osGetFileAttributesA((char*)zConverted);
 33699 #endif
 33701   return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
 33704 /*
 33705 ** Open a file.
 33706 */
 33707 static int winOpen(
 33708   sqlite3_vfs *pVfs,        /* Not used */
 33709   const char *zName,        /* Name of the file (UTF-8) */
 33710   sqlite3_file *id,         /* Write the SQLite file handle here */
 33711   int flags,                /* Open mode flags */
 33712   int *pOutFlags            /* Status return flags */
 33713 ){
 33714   HANDLE h;
 33715   DWORD lastErrno;
 33716   DWORD dwDesiredAccess;
 33717   DWORD dwShareMode;
 33718   DWORD dwCreationDisposition;
 33719   DWORD dwFlagsAndAttributes = 0;
 33720 #if SQLITE_OS_WINCE
 33721   int isTemp = 0;
 33722 #endif
 33723   winFile *pFile = (winFile*)id;
 33724   void *zConverted;              /* Filename in OS encoding */
 33725   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
 33726   int cnt = 0;
 33728   /* If argument zPath is a NULL pointer, this function is required to open
 33729   ** a temporary file. Use this buffer to store the file name in.
 33730   */
 33731   char zTmpname[MAX_PATH+2];     /* Buffer used to create temp filename */
 33733   int rc = SQLITE_OK;            /* Function Return Code */
 33734 #if !defined(NDEBUG) || SQLITE_OS_WINCE
 33735   int eType = flags&0xFFFFFF00;  /* Type of file to open */
 33736 #endif
 33738   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
 33739   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
 33740   int isCreate     = (flags & SQLITE_OPEN_CREATE);
 33741 #ifndef NDEBUG
 33742   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
 33743 #endif
 33744   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
 33746 #ifndef NDEBUG
 33747   int isOpenJournal = (isCreate && (
 33748         eType==SQLITE_OPEN_MASTER_JOURNAL 
 33749      || eType==SQLITE_OPEN_MAIN_JOURNAL 
 33750      || eType==SQLITE_OPEN_WAL
 33751   ));
 33752 #endif
 33754   /* Check the following statements are true: 
 33755   **
 33756   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
 33757   **   (b) if CREATE is set, then READWRITE must also be set, and
 33758   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
 33759   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
 33760   */
 33761   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
 33762   assert(isCreate==0 || isReadWrite);
 33763   assert(isExclusive==0 || isCreate);
 33764   assert(isDelete==0 || isCreate);
 33766   /* The main DB, main journal, WAL file and master journal are never 
 33767   ** automatically deleted. Nor are they ever temporary files.  */
 33768   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
 33769   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
 33770   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
 33771   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
 33773   /* Assert that the upper layer has set one of the "file-type" flags. */
 33774   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
 33775        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
 33776        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
 33777        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
 33778   );
 33780   assert( id!=0 );
 33781   UNUSED_PARAMETER(pVfs);
 33783 #if SQLITE_OS_WINRT
 33784   if( !sqlite3_temp_directory ){
 33785     sqlite3_log(SQLITE_ERROR,
 33786         "sqlite3_temp_directory variable should be set for WinRT");
 33788 #endif
 33790   pFile->h = INVALID_HANDLE_VALUE;
 33792   /* If the second argument to this function is NULL, generate a 
 33793   ** temporary file name to use 
 33794   */
 33795   if( !zUtf8Name ){
 33796     assert(isDelete && !isOpenJournal);
 33797     rc = getTempname(MAX_PATH+2, zTmpname);
 33798     if( rc!=SQLITE_OK ){
 33799       return rc;
 33801     zUtf8Name = zTmpname;
 33804   /* Database filenames are double-zero terminated if they are not
 33805   ** URIs with parameters.  Hence, they can always be passed into
 33806   ** sqlite3_uri_parameter().
 33807   */
 33808   assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
 33809         zUtf8Name[strlen(zUtf8Name)+1]==0 );
 33811   /* Convert the filename to the system encoding. */
 33812   zConverted = convertUtf8Filename(zUtf8Name);
 33813   if( zConverted==0 ){
 33814     return SQLITE_IOERR_NOMEM;
 33817   if( winIsDir(zConverted) ){
 33818     sqlite3_free(zConverted);
 33819     return SQLITE_CANTOPEN_ISDIR;
 33822   if( isReadWrite ){
 33823     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
 33824   }else{
 33825     dwDesiredAccess = GENERIC_READ;
 33828   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
 33829   ** created. SQLite doesn't use it to indicate "exclusive access" 
 33830   ** as it is usually understood.
 33831   */
 33832   if( isExclusive ){
 33833     /* Creates a new file, only if it does not already exist. */
 33834     /* If the file exists, it fails. */
 33835     dwCreationDisposition = CREATE_NEW;
 33836   }else if( isCreate ){
 33837     /* Open existing file, or create if it doesn't exist */
 33838     dwCreationDisposition = OPEN_ALWAYS;
 33839   }else{
 33840     /* Opens a file, only if it exists. */
 33841     dwCreationDisposition = OPEN_EXISTING;
 33844   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
 33846   if( isDelete ){
 33847 #if SQLITE_OS_WINCE
 33848     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
 33849     isTemp = 1;
 33850 #else
 33851     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
 33852                                | FILE_ATTRIBUTE_HIDDEN
 33853                                | FILE_FLAG_DELETE_ON_CLOSE;
 33854 #endif
 33855   }else{
 33856     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
 33858   /* Reports from the internet are that performance is always
 33859   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
 33860 #if SQLITE_OS_WINCE
 33861   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
 33862 #endif
 33864   if( isNT() ){
 33865 #if SQLITE_OS_WINRT
 33866     CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
 33867     extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
 33868     extendedParameters.dwFileAttributes =
 33869             dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
 33870     extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
 33871     extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
 33872     extendedParameters.lpSecurityAttributes = NULL;
 33873     extendedParameters.hTemplateFile = NULL;
 33874     while( (h = osCreateFile2((LPCWSTR)zConverted,
 33875                               dwDesiredAccess,
 33876                               dwShareMode,
 33877                               dwCreationDisposition,
 33878                               &extendedParameters))==INVALID_HANDLE_VALUE &&
 33879                               retryIoerr(&cnt, &lastErrno) ){
 33880                /* Noop */
 33882 #else
 33883     while( (h = osCreateFileW((LPCWSTR)zConverted,
 33884                               dwDesiredAccess,
 33885                               dwShareMode, NULL,
 33886                               dwCreationDisposition,
 33887                               dwFlagsAndAttributes,
 33888                               NULL))==INVALID_HANDLE_VALUE &&
 33889                               retryIoerr(&cnt, &lastErrno) ){
 33890                /* Noop */
 33892 #endif
 33894 #ifdef SQLITE_WIN32_HAS_ANSI
 33895   else{
 33896     while( (h = osCreateFileA((LPCSTR)zConverted,
 33897                               dwDesiredAccess,
 33898                               dwShareMode, NULL,
 33899                               dwCreationDisposition,
 33900                               dwFlagsAndAttributes,
 33901                               NULL))==INVALID_HANDLE_VALUE &&
 33902                               retryIoerr(&cnt, &lastErrno) ){
 33903                /* Noop */
 33906 #endif
 33907   logIoerr(cnt);
 33909   OSTRACE(("OPEN %d %s 0x%lx %s\n", 
 33910            h, zName, dwDesiredAccess, 
 33911            h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
 33913   if( h==INVALID_HANDLE_VALUE ){
 33914     pFile->lastErrno = lastErrno;
 33915     winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
 33916     sqlite3_free(zConverted);
 33917     if( isReadWrite && !isExclusive ){
 33918       return winOpen(pVfs, zName, id, 
 33919              ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
 33920     }else{
 33921       return SQLITE_CANTOPEN_BKPT;
 33925   if( pOutFlags ){
 33926     if( isReadWrite ){
 33927       *pOutFlags = SQLITE_OPEN_READWRITE;
 33928     }else{
 33929       *pOutFlags = SQLITE_OPEN_READONLY;
 33933   memset(pFile, 0, sizeof(*pFile));
 33934   pFile->pMethod = &winIoMethod;
 33935   pFile->h = h;
 33936   pFile->lastErrno = NO_ERROR;
 33937   pFile->pVfs = pVfs;
 33938 #ifndef SQLITE_OMIT_WAL
 33939   pFile->pShm = 0;
 33940 #endif
 33941   pFile->zPath = zName;
 33942   if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 33943     pFile->ctrlFlags |= WINFILE_PSOW;
 33946 #if SQLITE_OS_WINCE
 33947   if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
 33948        && !winceCreateLock(zName, pFile)
 33949   ){
 33950     osCloseHandle(h);
 33951     sqlite3_free(zConverted);
 33952     return SQLITE_CANTOPEN_BKPT;
 33954   if( isTemp ){
 33955     pFile->zDeleteOnClose = zConverted;
 33956   }else
 33957 #endif
 33959     sqlite3_free(zConverted);
 33962   OpenCounter(+1);
 33963   return rc;
 33966 /*
 33967 ** Delete the named file.
 33968 **
 33969 ** Note that Windows does not allow a file to be deleted if some other
 33970 ** process has it open.  Sometimes a virus scanner or indexing program
 33971 ** will open a journal file shortly after it is created in order to do
 33972 ** whatever it does.  While this other process is holding the
 33973 ** file open, we will be unable to delete it.  To work around this
 33974 ** problem, we delay 100 milliseconds and try to delete again.  Up
 33975 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
 33976 ** up and returning an error.
 33977 */
 33978 static int winDelete(
 33979   sqlite3_vfs *pVfs,          /* Not used on win32 */
 33980   const char *zFilename,      /* Name of file to delete */
 33981   int syncDir                 /* Not used on win32 */
 33982 ){
 33983   int cnt = 0;
 33984   int rc;
 33985   DWORD attr;
 33986   DWORD lastErrno;
 33987   void *zConverted;
 33988   UNUSED_PARAMETER(pVfs);
 33989   UNUSED_PARAMETER(syncDir);
 33991   SimulateIOError(return SQLITE_IOERR_DELETE);
 33992   zConverted = convertUtf8Filename(zFilename);
 33993   if( zConverted==0 ){
 33994     return SQLITE_IOERR_NOMEM;
 33996   if( isNT() ){
 33997     do {
 33998 #if SQLITE_OS_WINRT
 33999       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 34000       memset(&sAttrData, 0, sizeof(sAttrData));
 34001       if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
 34002                                   &sAttrData) ){
 34003         attr = sAttrData.dwFileAttributes;
 34004       }else{
 34005         lastErrno = osGetLastError();
 34006         if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
 34007           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 34008         }else{
 34009           rc = SQLITE_ERROR;
 34011         break;
 34013 #else
 34014       attr = osGetFileAttributesW(zConverted);
 34015 #endif
 34016       if ( attr==INVALID_FILE_ATTRIBUTES ){
 34017         lastErrno = osGetLastError();
 34018         if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
 34019           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 34020         }else{
 34021           rc = SQLITE_ERROR;
 34023         break;
 34025       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
 34026         rc = SQLITE_ERROR; /* Files only. */
 34027         break;
 34029       if ( osDeleteFileW(zConverted) ){
 34030         rc = SQLITE_OK; /* Deleted OK. */
 34031         break;
 34033       if ( !retryIoerr(&cnt, &lastErrno) ){
 34034         rc = SQLITE_ERROR; /* No more retries. */
 34035         break;
 34037     } while(1);
 34039 #ifdef SQLITE_WIN32_HAS_ANSI
 34040   else{
 34041     do {
 34042       attr = osGetFileAttributesA(zConverted);
 34043       if ( attr==INVALID_FILE_ATTRIBUTES ){
 34044         lastErrno = osGetLastError();
 34045         if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
 34046           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 34047         }else{
 34048           rc = SQLITE_ERROR;
 34050         break;
 34052       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
 34053         rc = SQLITE_ERROR; /* Files only. */
 34054         break;
 34056       if ( osDeleteFileA(zConverted) ){
 34057         rc = SQLITE_OK; /* Deleted OK. */
 34058         break;
 34060       if ( !retryIoerr(&cnt, &lastErrno) ){
 34061         rc = SQLITE_ERROR; /* No more retries. */
 34062         break;
 34064     } while(1);
 34066 #endif
 34067   if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
 34068     rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
 34069              "winDelete", zFilename);
 34070   }else{
 34071     logIoerr(cnt);
 34073   sqlite3_free(zConverted);
 34074   OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
 34075   return rc;
 34078 /*
 34079 ** Check the existance and status of a file.
 34080 */
 34081 static int winAccess(
 34082   sqlite3_vfs *pVfs,         /* Not used on win32 */
 34083   const char *zFilename,     /* Name of file to check */
 34084   int flags,                 /* Type of test to make on this file */
 34085   int *pResOut               /* OUT: Result */
 34086 ){
 34087   DWORD attr;
 34088   int rc = 0;
 34089   DWORD lastErrno;
 34090   void *zConverted;
 34091   UNUSED_PARAMETER(pVfs);
 34093   SimulateIOError( return SQLITE_IOERR_ACCESS; );
 34094   zConverted = convertUtf8Filename(zFilename);
 34095   if( zConverted==0 ){
 34096     return SQLITE_IOERR_NOMEM;
 34098   if( isNT() ){
 34099     int cnt = 0;
 34100     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 34101     memset(&sAttrData, 0, sizeof(sAttrData));
 34102     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
 34103                              GetFileExInfoStandard, 
 34104                              &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
 34105     if( rc ){
 34106       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
 34107       ** as if it does not exist.
 34108       */
 34109       if(    flags==SQLITE_ACCESS_EXISTS
 34110           && sAttrData.nFileSizeHigh==0 
 34111           && sAttrData.nFileSizeLow==0 ){
 34112         attr = INVALID_FILE_ATTRIBUTES;
 34113       }else{
 34114         attr = sAttrData.dwFileAttributes;
 34116     }else{
 34117       logIoerr(cnt);
 34118       if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
 34119         winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
 34120         sqlite3_free(zConverted);
 34121         return SQLITE_IOERR_ACCESS;
 34122       }else{
 34123         attr = INVALID_FILE_ATTRIBUTES;
 34127 #ifdef SQLITE_WIN32_HAS_ANSI
 34128   else{
 34129     attr = osGetFileAttributesA((char*)zConverted);
 34131 #endif
 34132   sqlite3_free(zConverted);
 34133   switch( flags ){
 34134     case SQLITE_ACCESS_READ:
 34135     case SQLITE_ACCESS_EXISTS:
 34136       rc = attr!=INVALID_FILE_ATTRIBUTES;
 34137       break;
 34138     case SQLITE_ACCESS_READWRITE:
 34139       rc = attr!=INVALID_FILE_ATTRIBUTES &&
 34140              (attr & FILE_ATTRIBUTE_READONLY)==0;
 34141       break;
 34142     default:
 34143       assert(!"Invalid flags argument");
 34145   *pResOut = rc;
 34146   return SQLITE_OK;
 34150 /*
 34151 ** Returns non-zero if the specified path name should be used verbatim.  If
 34152 ** non-zero is returned from this function, the calling function must simply
 34153 ** use the provided path name verbatim -OR- resolve it into a full path name
 34154 ** using the GetFullPathName Win32 API function (if available).
 34155 */
 34156 static BOOL winIsVerbatimPathname(
 34157   const char *zPathname
 34158 ){
 34159   /*
 34160   ** If the path name starts with a forward slash or a backslash, it is either
 34161   ** a legal UNC name, a volume relative path, or an absolute path name in the
 34162   ** "Unix" format on Windows.  There is no easy way to differentiate between
 34163   ** the final two cases; therefore, we return the safer return value of TRUE
 34164   ** so that callers of this function will simply use it verbatim.
 34165   */
 34166   if ( zPathname[0]=='/' || zPathname[0]=='\\' ){
 34167     return TRUE;
 34170   /*
 34171   ** If the path name starts with a letter and a colon it is either a volume
 34172   ** relative path or an absolute path.  Callers of this function must not
 34173   ** attempt to treat it as a relative path name (i.e. they should simply use
 34174   ** it verbatim).
 34175   */
 34176   if ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ){
 34177     return TRUE;
 34180   /*
 34181   ** If we get to this point, the path name should almost certainly be a purely
 34182   ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
 34183   */
 34184   return FALSE;
 34187 /*
 34188 ** Turn a relative pathname into a full pathname.  Write the full
 34189 ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
 34190 ** bytes in size.
 34191 */
 34192 static int winFullPathname(
 34193   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
 34194   const char *zRelative,        /* Possibly relative input path */
 34195   int nFull,                    /* Size of output buffer in bytes */
 34196   char *zFull                   /* Output buffer */
 34197 ){
 34199 #if defined(__CYGWIN__)
 34200   SimulateIOError( return SQLITE_ERROR );
 34201   UNUSED_PARAMETER(nFull);
 34202   assert( pVfs->mxPathname>=MAX_PATH );
 34203   assert( nFull>=pVfs->mxPathname );
 34204   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 34205     /*
 34206     ** NOTE: We are dealing with a relative path name and the data
 34207     **       directory has been set.  Therefore, use it as the basis
 34208     **       for converting the relative path name to an absolute
 34209     **       one by prepending the data directory and a slash.
 34210     */
 34211     char zOut[MAX_PATH+1];
 34212     memset(zOut, 0, MAX_PATH+1);
 34213     cygwin_conv_to_win32_path(zRelative, zOut); /* POSIX to Win32 */
 34214     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
 34215                      sqlite3_data_directory, zOut);
 34216   }else{
 34217     /*
 34218     ** NOTE: The Cygwin docs state that the maximum length needed
 34219     **       for the buffer passed to cygwin_conv_to_full_win32_path
 34220     **       is MAX_PATH.
 34221     */
 34222     cygwin_conv_to_full_win32_path(zRelative, zFull);
 34224   return SQLITE_OK;
 34225 #endif
 34227 #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
 34228   SimulateIOError( return SQLITE_ERROR );
 34229   /* WinCE has no concept of a relative pathname, or so I am told. */
 34230   /* WinRT has no way to convert a relative path to an absolute one. */
 34231   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 34232     /*
 34233     ** NOTE: We are dealing with a relative path name and the data
 34234     **       directory has been set.  Therefore, use it as the basis
 34235     **       for converting the relative path name to an absolute
 34236     **       one by prepending the data directory and a backslash.
 34237     */
 34238     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
 34239                      sqlite3_data_directory, zRelative);
 34240   }else{
 34241     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
 34243   return SQLITE_OK;
 34244 #endif
 34246 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
 34247   DWORD nByte;
 34248   void *zConverted;
 34249   char *zOut;
 34251   /* If this path name begins with "/X:", where "X" is any alphabetic
 34252   ** character, discard the initial "/" from the pathname.
 34253   */
 34254   if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
 34255     zRelative++;
 34258   /* It's odd to simulate an io-error here, but really this is just
 34259   ** using the io-error infrastructure to test that SQLite handles this
 34260   ** function failing. This function could fail if, for example, the
 34261   ** current working directory has been unlinked.
 34262   */
 34263   SimulateIOError( return SQLITE_ERROR );
 34264   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 34265     /*
 34266     ** NOTE: We are dealing with a relative path name and the data
 34267     **       directory has been set.  Therefore, use it as the basis
 34268     **       for converting the relative path name to an absolute
 34269     **       one by prepending the data directory and a backslash.
 34270     */
 34271     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
 34272                      sqlite3_data_directory, zRelative);
 34273     return SQLITE_OK;
 34275   zConverted = convertUtf8Filename(zRelative);
 34276   if( zConverted==0 ){
 34277     return SQLITE_IOERR_NOMEM;
 34279   if( isNT() ){
 34280     LPWSTR zTemp;
 34281     nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
 34282     if( nByte==0 ){
 34283       winLogError(SQLITE_ERROR, osGetLastError(),
 34284                   "GetFullPathNameW1", zConverted);
 34285       sqlite3_free(zConverted);
 34286       return SQLITE_CANTOPEN_FULLPATH;
 34288     nByte += 3;
 34289     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
 34290     if( zTemp==0 ){
 34291       sqlite3_free(zConverted);
 34292       return SQLITE_IOERR_NOMEM;
 34294     nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
 34295     if( nByte==0 ){
 34296       winLogError(SQLITE_ERROR, osGetLastError(),
 34297                   "GetFullPathNameW2", zConverted);
 34298       sqlite3_free(zConverted);
 34299       sqlite3_free(zTemp);
 34300       return SQLITE_CANTOPEN_FULLPATH;
 34302     sqlite3_free(zConverted);
 34303     zOut = unicodeToUtf8(zTemp);
 34304     sqlite3_free(zTemp);
 34306 #ifdef SQLITE_WIN32_HAS_ANSI
 34307   else{
 34308     char *zTemp;
 34309     nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
 34310     if( nByte==0 ){
 34311       winLogError(SQLITE_ERROR, osGetLastError(),
 34312                   "GetFullPathNameA1", zConverted);
 34313       sqlite3_free(zConverted);
 34314       return SQLITE_CANTOPEN_FULLPATH;
 34316     nByte += 3;
 34317     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
 34318     if( zTemp==0 ){
 34319       sqlite3_free(zConverted);
 34320       return SQLITE_IOERR_NOMEM;
 34322     nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
 34323     if( nByte==0 ){
 34324       winLogError(SQLITE_ERROR, osGetLastError(),
 34325                   "GetFullPathNameA2", zConverted);
 34326       sqlite3_free(zConverted);
 34327       sqlite3_free(zTemp);
 34328       return SQLITE_CANTOPEN_FULLPATH;
 34330     sqlite3_free(zConverted);
 34331     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
 34332     sqlite3_free(zTemp);
 34334 #endif
 34335   if( zOut ){
 34336     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
 34337     sqlite3_free(zOut);
 34338     return SQLITE_OK;
 34339   }else{
 34340     return SQLITE_IOERR_NOMEM;
 34342 #endif
 34345 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 34346 /*
 34347 ** Interfaces for opening a shared library, finding entry points
 34348 ** within the shared library, and closing the shared library.
 34349 */
 34350 /*
 34351 ** Interfaces for opening a shared library, finding entry points
 34352 ** within the shared library, and closing the shared library.
 34353 */
 34354 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
 34355   HANDLE h;
 34356   void *zConverted = convertUtf8Filename(zFilename);
 34357   UNUSED_PARAMETER(pVfs);
 34358   if( zConverted==0 ){
 34359     return 0;
 34361   if( isNT() ){
 34362 #if SQLITE_OS_WINRT
 34363     h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
 34364 #else
 34365     h = osLoadLibraryW((LPCWSTR)zConverted);
 34366 #endif
 34368 #ifdef SQLITE_WIN32_HAS_ANSI
 34369   else{
 34370     h = osLoadLibraryA((char*)zConverted);
 34372 #endif
 34373   sqlite3_free(zConverted);
 34374   return (void*)h;
 34376 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
 34377   UNUSED_PARAMETER(pVfs);
 34378   getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
 34380 static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
 34381   UNUSED_PARAMETER(pVfs);
 34382   return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol);
 34384 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
 34385   UNUSED_PARAMETER(pVfs);
 34386   osFreeLibrary((HANDLE)pHandle);
 34388 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 34389   #define winDlOpen  0
 34390   #define winDlError 0
 34391   #define winDlSym   0
 34392   #define winDlClose 0
 34393 #endif
 34396 /*
 34397 ** Write up to nBuf bytes of randomness into zBuf.
 34398 */
 34399 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 34400   int n = 0;
 34401   UNUSED_PARAMETER(pVfs);
 34402 #if defined(SQLITE_TEST)
 34403   n = nBuf;
 34404   memset(zBuf, 0, nBuf);
 34405 #else
 34406   if( sizeof(SYSTEMTIME)<=nBuf-n ){
 34407     SYSTEMTIME x;
 34408     osGetSystemTime(&x);
 34409     memcpy(&zBuf[n], &x, sizeof(x));
 34410     n += sizeof(x);
 34412   if( sizeof(DWORD)<=nBuf-n ){
 34413     DWORD pid = osGetCurrentProcessId();
 34414     memcpy(&zBuf[n], &pid, sizeof(pid));
 34415     n += sizeof(pid);
 34417 #if SQLITE_OS_WINRT
 34418   if( sizeof(ULONGLONG)<=nBuf-n ){
 34419     ULONGLONG cnt = osGetTickCount64();
 34420     memcpy(&zBuf[n], &cnt, sizeof(cnt));
 34421     n += sizeof(cnt);
 34423 #else
 34424   if( sizeof(DWORD)<=nBuf-n ){
 34425     DWORD cnt = osGetTickCount();
 34426     memcpy(&zBuf[n], &cnt, sizeof(cnt));
 34427     n += sizeof(cnt);
 34429 #endif
 34430   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
 34431     LARGE_INTEGER i;
 34432     osQueryPerformanceCounter(&i);
 34433     memcpy(&zBuf[n], &i, sizeof(i));
 34434     n += sizeof(i);
 34436 #endif
 34437   return n;
 34441 /*
 34442 ** Sleep for a little while.  Return the amount of time slept.
 34443 */
 34444 static int winSleep(sqlite3_vfs *pVfs, int microsec){
 34445   sqlite3_win32_sleep((microsec+999)/1000);
 34446   UNUSED_PARAMETER(pVfs);
 34447   return ((microsec+999)/1000)*1000;
 34450 /*
 34451 ** The following variable, if set to a non-zero value, is interpreted as
 34452 ** the number of seconds since 1970 and is used to set the result of
 34453 ** sqlite3OsCurrentTime() during testing.
 34454 */
 34455 #ifdef SQLITE_TEST
 34456 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
 34457 #endif
 34459 /*
 34460 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
 34461 ** the current time and date as a Julian Day number times 86_400_000.  In
 34462 ** other words, write into *piNow the number of milliseconds since the Julian
 34463 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 34464 ** proleptic Gregorian calendar.
 34465 **
 34466 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
 34467 ** cannot be found.
 34468 */
 34469 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
 34470   /* FILETIME structure is a 64-bit value representing the number of 
 34471      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
 34472   */
 34473   FILETIME ft;
 34474   static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
 34475 #ifdef SQLITE_TEST
 34476   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 34477 #endif
 34478   /* 2^32 - to avoid use of LL and warnings in gcc */
 34479   static const sqlite3_int64 max32BitValue = 
 34480       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
 34482 #if SQLITE_OS_WINCE
 34483   SYSTEMTIME time;
 34484   osGetSystemTime(&time);
 34485   /* if SystemTimeToFileTime() fails, it returns zero. */
 34486   if (!osSystemTimeToFileTime(&time,&ft)){
 34487     return SQLITE_ERROR;
 34489 #else
 34490   osGetSystemTimeAsFileTime( &ft );
 34491 #endif
 34493   *piNow = winFiletimeEpoch +
 34494             ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
 34495                (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
 34497 #ifdef SQLITE_TEST
 34498   if( sqlite3_current_time ){
 34499     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 34501 #endif
 34502   UNUSED_PARAMETER(pVfs);
 34503   return SQLITE_OK;
 34506 /*
 34507 ** Find the current time (in Universal Coordinated Time).  Write the
 34508 ** current time and date as a Julian Day number into *prNow and
 34509 ** return 0.  Return 1 if the time and date cannot be found.
 34510 */
 34511 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
 34512   int rc;
 34513   sqlite3_int64 i;
 34514   rc = winCurrentTimeInt64(pVfs, &i);
 34515   if( !rc ){
 34516     *prNow = i/86400000.0;
 34518   return rc;
 34521 /*
 34522 ** The idea is that this function works like a combination of
 34523 ** GetLastError() and FormatMessage() on Windows (or errno and
 34524 ** strerror_r() on Unix). After an error is returned by an OS
 34525 ** function, SQLite calls this function with zBuf pointing to
 34526 ** a buffer of nBuf bytes. The OS layer should populate the
 34527 ** buffer with a nul-terminated UTF-8 encoded error message
 34528 ** describing the last IO error to have occurred within the calling
 34529 ** thread.
 34530 **
 34531 ** If the error message is too large for the supplied buffer,
 34532 ** it should be truncated. The return value of xGetLastError
 34533 ** is zero if the error message fits in the buffer, or non-zero
 34534 ** otherwise (if the message was truncated). If non-zero is returned,
 34535 ** then it is not necessary to include the nul-terminator character
 34536 ** in the output buffer.
 34537 **
 34538 ** Not supplying an error message will have no adverse effect
 34539 ** on SQLite. It is fine to have an implementation that never
 34540 ** returns an error message:
 34541 **
 34542 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 34543 **     assert(zBuf[0]=='\0');
 34544 **     return 0;
 34545 **   }
 34546 **
 34547 ** However if an error message is supplied, it will be incorporated
 34548 ** by sqlite into the error message available to the user using
 34549 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
 34550 */
 34551 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 34552   UNUSED_PARAMETER(pVfs);
 34553   return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
 34556 /*
 34557 ** Initialize and deinitialize the operating system interface.
 34558 */
 34559 SQLITE_API int sqlite3_os_init(void){
 34560   static sqlite3_vfs winVfs = {
 34561     3,                   /* iVersion */
 34562     sizeof(winFile),     /* szOsFile */
 34563     MAX_PATH,            /* mxPathname */
 34564     0,                   /* pNext */
 34565     "win32",             /* zName */
 34566     0,                   /* pAppData */
 34567     winOpen,             /* xOpen */
 34568     winDelete,           /* xDelete */
 34569     winAccess,           /* xAccess */
 34570     winFullPathname,     /* xFullPathname */
 34571     winDlOpen,           /* xDlOpen */
 34572     winDlError,          /* xDlError */
 34573     winDlSym,            /* xDlSym */
 34574     winDlClose,          /* xDlClose */
 34575     winRandomness,       /* xRandomness */
 34576     winSleep,            /* xSleep */
 34577     winCurrentTime,      /* xCurrentTime */
 34578     winGetLastError,     /* xGetLastError */
 34579     winCurrentTimeInt64, /* xCurrentTimeInt64 */
 34580     winSetSystemCall,    /* xSetSystemCall */
 34581     winGetSystemCall,    /* xGetSystemCall */
 34582     winNextSystemCall,   /* xNextSystemCall */
 34583   };
 34585   /* Double-check that the aSyscall[] array has been constructed
 34586   ** correctly.  See ticket [bb3a86e890c8e96ab] */
 34587   assert( ArraySize(aSyscall)==74 );
 34589 #ifndef SQLITE_OMIT_WAL
 34590   /* get memory map allocation granularity */
 34591   memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
 34592 #if SQLITE_OS_WINRT
 34593   osGetNativeSystemInfo(&winSysInfo);
 34594 #else
 34595   osGetSystemInfo(&winSysInfo);
 34596 #endif
 34597   assert(winSysInfo.dwAllocationGranularity > 0);
 34598 #endif
 34600   sqlite3_vfs_register(&winVfs, 1);
 34601   return SQLITE_OK; 
 34604 SQLITE_API int sqlite3_os_end(void){ 
 34605 #if SQLITE_OS_WINRT
 34606   if( sleepObj!=NULL ){
 34607     osCloseHandle(sleepObj);
 34608     sleepObj = NULL;
 34610 #endif
 34611   return SQLITE_OK;
 34614 #endif /* SQLITE_OS_WIN */
 34616 /************** End of os_win.c **********************************************/
 34617 /************** Begin file bitvec.c ******************************************/
 34618 /*
 34619 ** 2008 February 16
 34620 **
 34621 ** The author disclaims copyright to this source code.  In place of
 34622 ** a legal notice, here is a blessing:
 34623 **
 34624 **    May you do good and not evil.
 34625 **    May you find forgiveness for yourself and forgive others.
 34626 **    May you share freely, never taking more than you give.
 34627 **
 34628 *************************************************************************
 34629 ** This file implements an object that represents a fixed-length
 34630 ** bitmap.  Bits are numbered starting with 1.
 34631 **
 34632 ** A bitmap is used to record which pages of a database file have been
 34633 ** journalled during a transaction, or which pages have the "dont-write"
 34634 ** property.  Usually only a few pages are meet either condition.
 34635 ** So the bitmap is usually sparse and has low cardinality.
 34636 ** But sometimes (for example when during a DROP of a large table) most
 34637 ** or all of the pages in a database can get journalled.  In those cases, 
 34638 ** the bitmap becomes dense with high cardinality.  The algorithm needs 
 34639 ** to handle both cases well.
 34640 **
 34641 ** The size of the bitmap is fixed when the object is created.
 34642 **
 34643 ** All bits are clear when the bitmap is created.  Individual bits
 34644 ** may be set or cleared one at a time.
 34645 **
 34646 ** Test operations are about 100 times more common that set operations.
 34647 ** Clear operations are exceedingly rare.  There are usually between
 34648 ** 5 and 500 set operations per Bitvec object, though the number of sets can
 34649 ** sometimes grow into tens of thousands or larger.  The size of the
 34650 ** Bitvec object is the number of pages in the database file at the
 34651 ** start of a transaction, and is thus usually less than a few thousand,
 34652 ** but can be as large as 2 billion for a really big database.
 34653 */
 34655 /* Size of the Bitvec structure in bytes. */
 34656 #define BITVEC_SZ        512
 34658 /* Round the union size down to the nearest pointer boundary, since that's how 
 34659 ** it will be aligned within the Bitvec struct. */
 34660 #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
 34662 /* Type of the array "element" for the bitmap representation. 
 34663 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
 34664 ** Setting this to the "natural word" size of your CPU may improve
 34665 ** performance. */
 34666 #define BITVEC_TELEM     u8
 34667 /* Size, in bits, of the bitmap element. */
 34668 #define BITVEC_SZELEM    8
 34669 /* Number of elements in a bitmap array. */
 34670 #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
 34671 /* Number of bits in the bitmap array. */
 34672 #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
 34674 /* Number of u32 values in hash table. */
 34675 #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
 34676 /* Maximum number of entries in hash table before 
 34677 ** sub-dividing and re-hashing. */
 34678 #define BITVEC_MXHASH    (BITVEC_NINT/2)
 34679 /* Hashing function for the aHash representation.
 34680 ** Empirical testing showed that the *37 multiplier 
 34681 ** (an arbitrary prime)in the hash function provided 
 34682 ** no fewer collisions than the no-op *1. */
 34683 #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
 34685 #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
 34688 /*
 34689 ** A bitmap is an instance of the following structure.
 34690 **
 34691 ** This bitmap records the existance of zero or more bits
 34692 ** with values between 1 and iSize, inclusive.
 34693 **
 34694 ** There are three possible representations of the bitmap.
 34695 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
 34696 ** bitmap.  The least significant bit is bit 1.
 34697 **
 34698 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
 34699 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
 34700 **
 34701 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
 34702 ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
 34703 ** handles up to iDivisor separate values of i.  apSub[0] holds
 34704 ** values between 1 and iDivisor.  apSub[1] holds values between
 34705 ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
 34706 ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
 34707 ** to hold deal with values between 1 and iDivisor.
 34708 */
 34709 struct Bitvec {
 34710   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
 34711   u32 nSet;       /* Number of bits that are set - only valid for aHash
 34712                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
 34713                   ** this would be 125. */
 34714   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
 34715                   /* Should >=0 for apSub element. */
 34716                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
 34717                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
 34718   union {
 34719     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
 34720     u32 aHash[BITVEC_NINT];      /* Hash table representation */
 34721     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
 34722   } u;
 34723 };
 34725 /*
 34726 ** Create a new bitmap object able to handle bits between 0 and iSize,
 34727 ** inclusive.  Return a pointer to the new object.  Return NULL if 
 34728 ** malloc fails.
 34729 */
 34730 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
 34731   Bitvec *p;
 34732   assert( sizeof(*p)==BITVEC_SZ );
 34733   p = sqlite3MallocZero( sizeof(*p) );
 34734   if( p ){
 34735     p->iSize = iSize;
 34737   return p;
 34740 /*
 34741 ** Check to see if the i-th bit is set.  Return true or false.
 34742 ** If p is NULL (if the bitmap has not been created) or if
 34743 ** i is out of range, then return false.
 34744 */
 34745 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
 34746   if( p==0 ) return 0;
 34747   if( i>p->iSize || i==0 ) return 0;
 34748   i--;
 34749   while( p->iDivisor ){
 34750     u32 bin = i/p->iDivisor;
 34751     i = i%p->iDivisor;
 34752     p = p->u.apSub[bin];
 34753     if (!p) {
 34754       return 0;
 34757   if( p->iSize<=BITVEC_NBIT ){
 34758     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
 34759   } else{
 34760     u32 h = BITVEC_HASH(i++);
 34761     while( p->u.aHash[h] ){
 34762       if( p->u.aHash[h]==i ) return 1;
 34763       h = (h+1) % BITVEC_NINT;
 34765     return 0;
 34769 /*
 34770 ** Set the i-th bit.  Return 0 on success and an error code if
 34771 ** anything goes wrong.
 34772 **
 34773 ** This routine might cause sub-bitmaps to be allocated.  Failing
 34774 ** to get the memory needed to hold the sub-bitmap is the only
 34775 ** that can go wrong with an insert, assuming p and i are valid.
 34776 **
 34777 ** The calling function must ensure that p is a valid Bitvec object
 34778 ** and that the value for "i" is within range of the Bitvec object.
 34779 ** Otherwise the behavior is undefined.
 34780 */
 34781 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
 34782   u32 h;
 34783   if( p==0 ) return SQLITE_OK;
 34784   assert( i>0 );
 34785   assert( i<=p->iSize );
 34786   i--;
 34787   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
 34788     u32 bin = i/p->iDivisor;
 34789     i = i%p->iDivisor;
 34790     if( p->u.apSub[bin]==0 ){
 34791       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
 34792       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
 34794     p = p->u.apSub[bin];
 34796   if( p->iSize<=BITVEC_NBIT ){
 34797     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
 34798     return SQLITE_OK;
 34800   h = BITVEC_HASH(i++);
 34801   /* if there wasn't a hash collision, and this doesn't */
 34802   /* completely fill the hash, then just add it without */
 34803   /* worring about sub-dividing and re-hashing. */
 34804   if( !p->u.aHash[h] ){
 34805     if (p->nSet<(BITVEC_NINT-1)) {
 34806       goto bitvec_set_end;
 34807     } else {
 34808       goto bitvec_set_rehash;
 34811   /* there was a collision, check to see if it's already */
 34812   /* in hash, if not, try to find a spot for it */
 34813   do {
 34814     if( p->u.aHash[h]==i ) return SQLITE_OK;
 34815     h++;
 34816     if( h>=BITVEC_NINT ) h = 0;
 34817   } while( p->u.aHash[h] );
 34818   /* we didn't find it in the hash.  h points to the first */
 34819   /* available free spot. check to see if this is going to */
 34820   /* make our hash too "full".  */
 34821 bitvec_set_rehash:
 34822   if( p->nSet>=BITVEC_MXHASH ){
 34823     unsigned int j;
 34824     int rc;
 34825     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
 34826     if( aiValues==0 ){
 34827       return SQLITE_NOMEM;
 34828     }else{
 34829       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
 34830       memset(p->u.apSub, 0, sizeof(p->u.apSub));
 34831       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
 34832       rc = sqlite3BitvecSet(p, i);
 34833       for(j=0; j<BITVEC_NINT; j++){
 34834         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
 34836       sqlite3StackFree(0, aiValues);
 34837       return rc;
 34840 bitvec_set_end:
 34841   p->nSet++;
 34842   p->u.aHash[h] = i;
 34843   return SQLITE_OK;
 34846 /*
 34847 ** Clear the i-th bit.
 34848 **
 34849 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
 34850 ** that BitvecClear can use to rebuilt its hash table.
 34851 */
 34852 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
 34853   if( p==0 ) return;
 34854   assert( i>0 );
 34855   i--;
 34856   while( p->iDivisor ){
 34857     u32 bin = i/p->iDivisor;
 34858     i = i%p->iDivisor;
 34859     p = p->u.apSub[bin];
 34860     if (!p) {
 34861       return;
 34864   if( p->iSize<=BITVEC_NBIT ){
 34865     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
 34866   }else{
 34867     unsigned int j;
 34868     u32 *aiValues = pBuf;
 34869     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
 34870     memset(p->u.aHash, 0, sizeof(p->u.aHash));
 34871     p->nSet = 0;
 34872     for(j=0; j<BITVEC_NINT; j++){
 34873       if( aiValues[j] && aiValues[j]!=(i+1) ){
 34874         u32 h = BITVEC_HASH(aiValues[j]-1);
 34875         p->nSet++;
 34876         while( p->u.aHash[h] ){
 34877           h++;
 34878           if( h>=BITVEC_NINT ) h = 0;
 34880         p->u.aHash[h] = aiValues[j];
 34886 /*
 34887 ** Destroy a bitmap object.  Reclaim all memory used.
 34888 */
 34889 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
 34890   if( p==0 ) return;
 34891   if( p->iDivisor ){
 34892     unsigned int i;
 34893     for(i=0; i<BITVEC_NPTR; i++){
 34894       sqlite3BitvecDestroy(p->u.apSub[i]);
 34897   sqlite3_free(p);
 34900 /*
 34901 ** Return the value of the iSize parameter specified when Bitvec *p
 34902 ** was created.
 34903 */
 34904 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
 34905   return p->iSize;
 34908 #ifndef SQLITE_OMIT_BUILTIN_TEST
 34909 /*
 34910 ** Let V[] be an array of unsigned characters sufficient to hold
 34911 ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
 34912 ** Then the following macros can be used to set, clear, or test
 34913 ** individual bits within V.
 34914 */
 34915 #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
 34916 #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
 34917 #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
 34919 /*
 34920 ** This routine runs an extensive test of the Bitvec code.
 34921 **
 34922 ** The input is an array of integers that acts as a program
 34923 ** to test the Bitvec.  The integers are opcodes followed
 34924 ** by 0, 1, or 3 operands, depending on the opcode.  Another
 34925 ** opcode follows immediately after the last operand.
 34926 **
 34927 ** There are 6 opcodes numbered from 0 through 5.  0 is the
 34928 ** "halt" opcode and causes the test to end.
 34929 **
 34930 **    0          Halt and return the number of errors
 34931 **    1 N S X    Set N bits beginning with S and incrementing by X
 34932 **    2 N S X    Clear N bits beginning with S and incrementing by X
 34933 **    3 N        Set N randomly chosen bits
 34934 **    4 N        Clear N randomly chosen bits
 34935 **    5 N S X    Set N bits from S increment X in array only, not in bitvec
 34936 **
 34937 ** The opcodes 1 through 4 perform set and clear operations are performed
 34938 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
 34939 ** Opcode 5 works on the linear array only, not on the Bitvec.
 34940 ** Opcode 5 is used to deliberately induce a fault in order to
 34941 ** confirm that error detection works.
 34942 **
 34943 ** At the conclusion of the test the linear array is compared
 34944 ** against the Bitvec object.  If there are any differences,
 34945 ** an error is returned.  If they are the same, zero is returned.
 34946 **
 34947 ** If a memory allocation error occurs, return -1.
 34948 */
 34949 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
 34950   Bitvec *pBitvec = 0;
 34951   unsigned char *pV = 0;
 34952   int rc = -1;
 34953   int i, nx, pc, op;
 34954   void *pTmpSpace;
 34956   /* Allocate the Bitvec to be tested and a linear array of
 34957   ** bits to act as the reference */
 34958   pBitvec = sqlite3BitvecCreate( sz );
 34959   pV = sqlite3MallocZero( (sz+7)/8 + 1 );
 34960   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
 34961   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
 34963   /* NULL pBitvec tests */
 34964   sqlite3BitvecSet(0, 1);
 34965   sqlite3BitvecClear(0, 1, pTmpSpace);
 34967   /* Run the program */
 34968   pc = 0;
 34969   while( (op = aOp[pc])!=0 ){
 34970     switch( op ){
 34971       case 1:
 34972       case 2:
 34973       case 5: {
 34974         nx = 4;
 34975         i = aOp[pc+2] - 1;
 34976         aOp[pc+2] += aOp[pc+3];
 34977         break;
 34979       case 3:
 34980       case 4: 
 34981       default: {
 34982         nx = 2;
 34983         sqlite3_randomness(sizeof(i), &i);
 34984         break;
 34987     if( (--aOp[pc+1]) > 0 ) nx = 0;
 34988     pc += nx;
 34989     i = (i & 0x7fffffff)%sz;
 34990     if( (op & 1)!=0 ){
 34991       SETBIT(pV, (i+1));
 34992       if( op!=5 ){
 34993         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
 34995     }else{
 34996       CLEARBIT(pV, (i+1));
 34997       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
 35001   /* Test to make sure the linear array exactly matches the
 35002   ** Bitvec object.  Start with the assumption that they do
 35003   ** match (rc==0).  Change rc to non-zero if a discrepancy
 35004   ** is found.
 35005   */
 35006   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
 35007           + sqlite3BitvecTest(pBitvec, 0)
 35008           + (sqlite3BitvecSize(pBitvec) - sz);
 35009   for(i=1; i<=sz; i++){
 35010     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
 35011       rc = i;
 35012       break;
 35016   /* Free allocated structure */
 35017 bitvec_end:
 35018   sqlite3_free(pTmpSpace);
 35019   sqlite3_free(pV);
 35020   sqlite3BitvecDestroy(pBitvec);
 35021   return rc;
 35023 #endif /* SQLITE_OMIT_BUILTIN_TEST */
 35025 /************** End of bitvec.c **********************************************/
 35026 /************** Begin file pcache.c ******************************************/
 35027 /*
 35028 ** 2008 August 05
 35029 **
 35030 ** The author disclaims copyright to this source code.  In place of
 35031 ** a legal notice, here is a blessing:
 35032 **
 35033 **    May you do good and not evil.
 35034 **    May you find forgiveness for yourself and forgive others.
 35035 **    May you share freely, never taking more than you give.
 35036 **
 35037 *************************************************************************
 35038 ** This file implements that page cache.
 35039 */
 35041 /*
 35042 ** A complete page cache is an instance of this structure.
 35043 */
 35044 struct PCache {
 35045   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
 35046   PgHdr *pSynced;                     /* Last synced page in dirty page list */
 35047   int nRef;                           /* Number of referenced pages */
 35048   int szCache;                        /* Configured cache size */
 35049   int szPage;                         /* Size of every page in this cache */
 35050   int szExtra;                        /* Size of extra space for each page */
 35051   int bPurgeable;                     /* True if pages are on backing store */
 35052   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
 35053   void *pStress;                      /* Argument to xStress */
 35054   sqlite3_pcache *pCache;             /* Pluggable cache module */
 35055   PgHdr *pPage1;                      /* Reference to page 1 */
 35056 };
 35058 /*
 35059 ** Some of the assert() macros in this code are too expensive to run
 35060 ** even during normal debugging.  Use them only rarely on long-running
 35061 ** tests.  Enable the expensive asserts using the
 35062 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
 35063 */
 35064 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 35065 # define expensive_assert(X)  assert(X)
 35066 #else
 35067 # define expensive_assert(X)
 35068 #endif
 35070 /********************************** Linked List Management ********************/
 35072 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
 35073 /*
 35074 ** Check that the pCache->pSynced variable is set correctly. If it
 35075 ** is not, either fail an assert or return zero. Otherwise, return
 35076 ** non-zero. This is only used in debugging builds, as follows:
 35077 **
 35078 **   expensive_assert( pcacheCheckSynced(pCache) );
 35079 */
 35080 static int pcacheCheckSynced(PCache *pCache){
 35081   PgHdr *p;
 35082   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
 35083     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
 35085   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
 35087 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
 35089 /*
 35090 ** Remove page pPage from the list of dirty pages.
 35091 */
 35092 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
 35093   PCache *p = pPage->pCache;
 35095   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
 35096   assert( pPage->pDirtyPrev || pPage==p->pDirty );
 35098   /* Update the PCache1.pSynced variable if necessary. */
 35099   if( p->pSynced==pPage ){
 35100     PgHdr *pSynced = pPage->pDirtyPrev;
 35101     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
 35102       pSynced = pSynced->pDirtyPrev;
 35104     p->pSynced = pSynced;
 35107   if( pPage->pDirtyNext ){
 35108     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
 35109   }else{
 35110     assert( pPage==p->pDirtyTail );
 35111     p->pDirtyTail = pPage->pDirtyPrev;
 35113   if( pPage->pDirtyPrev ){
 35114     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
 35115   }else{
 35116     assert( pPage==p->pDirty );
 35117     p->pDirty = pPage->pDirtyNext;
 35119   pPage->pDirtyNext = 0;
 35120   pPage->pDirtyPrev = 0;
 35122   expensive_assert( pcacheCheckSynced(p) );
 35125 /*
 35126 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
 35127 ** pPage).
 35128 */
 35129 static void pcacheAddToDirtyList(PgHdr *pPage){
 35130   PCache *p = pPage->pCache;
 35132   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
 35134   pPage->pDirtyNext = p->pDirty;
 35135   if( pPage->pDirtyNext ){
 35136     assert( pPage->pDirtyNext->pDirtyPrev==0 );
 35137     pPage->pDirtyNext->pDirtyPrev = pPage;
 35139   p->pDirty = pPage;
 35140   if( !p->pDirtyTail ){
 35141     p->pDirtyTail = pPage;
 35143   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
 35144     p->pSynced = pPage;
 35146   expensive_assert( pcacheCheckSynced(p) );
 35149 /*
 35150 ** Wrapper around the pluggable caches xUnpin method. If the cache is
 35151 ** being used for an in-memory database, this function is a no-op.
 35152 */
 35153 static void pcacheUnpin(PgHdr *p){
 35154   PCache *pCache = p->pCache;
 35155   if( pCache->bPurgeable ){
 35156     if( p->pgno==1 ){
 35157       pCache->pPage1 = 0;
 35159     sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
 35163 /*************************************************** General Interfaces ******
 35164 **
 35165 ** Initialize and shutdown the page cache subsystem. Neither of these 
 35166 ** functions are threadsafe.
 35167 */
 35168 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
 35169   if( sqlite3GlobalConfig.pcache2.xInit==0 ){
 35170     /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
 35171     ** built-in default page cache is used instead of the application defined
 35172     ** page cache. */
 35173     sqlite3PCacheSetDefault();
 35175   return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
 35177 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
 35178   if( sqlite3GlobalConfig.pcache2.xShutdown ){
 35179     /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
 35180     sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
 35184 /*
 35185 ** Return the size in bytes of a PCache object.
 35186 */
 35187 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
 35189 /*
 35190 ** Create a new PCache object. Storage space to hold the object
 35191 ** has already been allocated and is passed in as the p pointer. 
 35192 ** The caller discovers how much space needs to be allocated by 
 35193 ** calling sqlite3PcacheSize().
 35194 */
 35195 SQLITE_PRIVATE void sqlite3PcacheOpen(
 35196   int szPage,                  /* Size of every page */
 35197   int szExtra,                 /* Extra space associated with each page */
 35198   int bPurgeable,              /* True if pages are on backing store */
 35199   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
 35200   void *pStress,               /* Argument to xStress */
 35201   PCache *p                    /* Preallocated space for the PCache */
 35202 ){
 35203   memset(p, 0, sizeof(PCache));
 35204   p->szPage = szPage;
 35205   p->szExtra = szExtra;
 35206   p->bPurgeable = bPurgeable;
 35207   p->xStress = xStress;
 35208   p->pStress = pStress;
 35209   p->szCache = 100;
 35212 /*
 35213 ** Change the page size for PCache object. The caller must ensure that there
 35214 ** are no outstanding page references when this function is called.
 35215 */
 35216 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
 35217   assert( pCache->nRef==0 && pCache->pDirty==0 );
 35218   if( pCache->pCache ){
 35219     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
 35220     pCache->pCache = 0;
 35221     pCache->pPage1 = 0;
 35223   pCache->szPage = szPage;
 35226 /*
 35227 ** Compute the number of pages of cache requested.
 35228 */
 35229 static int numberOfCachePages(PCache *p){
 35230   if( p->szCache>=0 ){
 35231     return p->szCache;
 35232   }else{
 35233     return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
 35237 /*
 35238 ** Try to obtain a page from the cache.
 35239 */
 35240 SQLITE_PRIVATE int sqlite3PcacheFetch(
 35241   PCache *pCache,       /* Obtain the page from this cache */
 35242   Pgno pgno,            /* Page number to obtain */
 35243   int createFlag,       /* If true, create page if it does not exist already */
 35244   PgHdr **ppPage        /* Write the page here */
 35245 ){
 35246   sqlite3_pcache_page *pPage = 0;
 35247   PgHdr *pPgHdr = 0;
 35248   int eCreate;
 35250   assert( pCache!=0 );
 35251   assert( createFlag==1 || createFlag==0 );
 35252   assert( pgno>0 );
 35254   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
 35255   ** allocate it now.
 35256   */
 35257   if( !pCache->pCache && createFlag ){
 35258     sqlite3_pcache *p;
 35259     p = sqlite3GlobalConfig.pcache2.xCreate(
 35260         pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
 35261     );
 35262     if( !p ){
 35263       return SQLITE_NOMEM;
 35265     sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
 35266     pCache->pCache = p;
 35269   eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
 35270   if( pCache->pCache ){
 35271     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
 35274   if( !pPage && eCreate==1 ){
 35275     PgHdr *pPg;
 35277     /* Find a dirty page to write-out and recycle. First try to find a 
 35278     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
 35279     ** cleared), but if that is not possible settle for any other 
 35280     ** unreferenced dirty page.
 35281     */
 35282     expensive_assert( pcacheCheckSynced(pCache) );
 35283     for(pPg=pCache->pSynced; 
 35284         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
 35285         pPg=pPg->pDirtyPrev
 35286     );
 35287     pCache->pSynced = pPg;
 35288     if( !pPg ){
 35289       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
 35291     if( pPg ){
 35292       int rc;
 35293 #ifdef SQLITE_LOG_CACHE_SPILL
 35294       sqlite3_log(SQLITE_FULL, 
 35295                   "spill page %d making room for %d - cache used: %d/%d",
 35296                   pPg->pgno, pgno,
 35297                   sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
 35298                   numberOfCachePages(pCache));
 35299 #endif
 35300       rc = pCache->xStress(pCache->pStress, pPg);
 35301       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 35302         return rc;
 35306     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
 35309   if( pPage ){
 35310     pPgHdr = (PgHdr *)pPage->pExtra;
 35312     if( !pPgHdr->pPage ){
 35313       memset(pPgHdr, 0, sizeof(PgHdr));
 35314       pPgHdr->pPage = pPage;
 35315       pPgHdr->pData = pPage->pBuf;
 35316       pPgHdr->pExtra = (void *)&pPgHdr[1];
 35317       memset(pPgHdr->pExtra, 0, pCache->szExtra);
 35318       pPgHdr->pCache = pCache;
 35319       pPgHdr->pgno = pgno;
 35321     assert( pPgHdr->pCache==pCache );
 35322     assert( pPgHdr->pgno==pgno );
 35323     assert( pPgHdr->pData==pPage->pBuf );
 35324     assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
 35326     if( 0==pPgHdr->nRef ){
 35327       pCache->nRef++;
 35329     pPgHdr->nRef++;
 35330     if( pgno==1 ){
 35331       pCache->pPage1 = pPgHdr;
 35334   *ppPage = pPgHdr;
 35335   return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
 35338 /*
 35339 ** Decrement the reference count on a page. If the page is clean and the
 35340 ** reference count drops to 0, then it is made elible for recycling.
 35341 */
 35342 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
 35343   assert( p->nRef>0 );
 35344   p->nRef--;
 35345   if( p->nRef==0 ){
 35346     PCache *pCache = p->pCache;
 35347     pCache->nRef--;
 35348     if( (p->flags&PGHDR_DIRTY)==0 ){
 35349       pcacheUnpin(p);
 35350     }else{
 35351       /* Move the page to the head of the dirty list. */
 35352       pcacheRemoveFromDirtyList(p);
 35353       pcacheAddToDirtyList(p);
 35358 /*
 35359 ** Increase the reference count of a supplied page by 1.
 35360 */
 35361 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
 35362   assert(p->nRef>0);
 35363   p->nRef++;
 35366 /*
 35367 ** Drop a page from the cache. There must be exactly one reference to the
 35368 ** page. This function deletes that reference, so after it returns the
 35369 ** page pointed to by p is invalid.
 35370 */
 35371 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
 35372   PCache *pCache;
 35373   assert( p->nRef==1 );
 35374   if( p->flags&PGHDR_DIRTY ){
 35375     pcacheRemoveFromDirtyList(p);
 35377   pCache = p->pCache;
 35378   pCache->nRef--;
 35379   if( p->pgno==1 ){
 35380     pCache->pPage1 = 0;
 35382   sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
 35385 /*
 35386 ** Make sure the page is marked as dirty. If it isn't dirty already,
 35387 ** make it so.
 35388 */
 35389 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
 35390   p->flags &= ~PGHDR_DONT_WRITE;
 35391   assert( p->nRef>0 );
 35392   if( 0==(p->flags & PGHDR_DIRTY) ){
 35393     p->flags |= PGHDR_DIRTY;
 35394     pcacheAddToDirtyList( p);
 35398 /*
 35399 ** Make sure the page is marked as clean. If it isn't clean already,
 35400 ** make it so.
 35401 */
 35402 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
 35403   if( (p->flags & PGHDR_DIRTY) ){
 35404     pcacheRemoveFromDirtyList(p);
 35405     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
 35406     if( p->nRef==0 ){
 35407       pcacheUnpin(p);
 35412 /*
 35413 ** Make every page in the cache clean.
 35414 */
 35415 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
 35416   PgHdr *p;
 35417   while( (p = pCache->pDirty)!=0 ){
 35418     sqlite3PcacheMakeClean(p);
 35422 /*
 35423 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
 35424 */
 35425 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
 35426   PgHdr *p;
 35427   for(p=pCache->pDirty; p; p=p->pDirtyNext){
 35428     p->flags &= ~PGHDR_NEED_SYNC;
 35430   pCache->pSynced = pCache->pDirtyTail;
 35433 /*
 35434 ** Change the page number of page p to newPgno. 
 35435 */
 35436 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
 35437   PCache *pCache = p->pCache;
 35438   assert( p->nRef>0 );
 35439   assert( newPgno>0 );
 35440   sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
 35441   p->pgno = newPgno;
 35442   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
 35443     pcacheRemoveFromDirtyList(p);
 35444     pcacheAddToDirtyList(p);
 35448 /*
 35449 ** Drop every cache entry whose page number is greater than "pgno". The
 35450 ** caller must ensure that there are no outstanding references to any pages
 35451 ** other than page 1 with a page number greater than pgno.
 35452 **
 35453 ** If there is a reference to page 1 and the pgno parameter passed to this
 35454 ** function is 0, then the data area associated with page 1 is zeroed, but
 35455 ** the page object is not dropped.
 35456 */
 35457 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
 35458   if( pCache->pCache ){
 35459     PgHdr *p;
 35460     PgHdr *pNext;
 35461     for(p=pCache->pDirty; p; p=pNext){
 35462       pNext = p->pDirtyNext;
 35463       /* This routine never gets call with a positive pgno except right
 35464       ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
 35465       ** it must be that pgno==0.
 35466       */
 35467       assert( p->pgno>0 );
 35468       if( ALWAYS(p->pgno>pgno) ){
 35469         assert( p->flags&PGHDR_DIRTY );
 35470         sqlite3PcacheMakeClean(p);
 35473     if( pgno==0 && pCache->pPage1 ){
 35474       memset(pCache->pPage1->pData, 0, pCache->szPage);
 35475       pgno = 1;
 35477     sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
 35481 /*
 35482 ** Close a cache.
 35483 */
 35484 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
 35485   if( pCache->pCache ){
 35486     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
 35490 /* 
 35491 ** Discard the contents of the cache.
 35492 */
 35493 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
 35494   sqlite3PcacheTruncate(pCache, 0);
 35497 /*
 35498 ** Merge two lists of pages connected by pDirty and in pgno order.
 35499 ** Do not both fixing the pDirtyPrev pointers.
 35500 */
 35501 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
 35502   PgHdr result, *pTail;
 35503   pTail = &result;
 35504   while( pA && pB ){
 35505     if( pA->pgno<pB->pgno ){
 35506       pTail->pDirty = pA;
 35507       pTail = pA;
 35508       pA = pA->pDirty;
 35509     }else{
 35510       pTail->pDirty = pB;
 35511       pTail = pB;
 35512       pB = pB->pDirty;
 35515   if( pA ){
 35516     pTail->pDirty = pA;
 35517   }else if( pB ){
 35518     pTail->pDirty = pB;
 35519   }else{
 35520     pTail->pDirty = 0;
 35522   return result.pDirty;
 35525 /*
 35526 ** Sort the list of pages in accending order by pgno.  Pages are
 35527 ** connected by pDirty pointers.  The pDirtyPrev pointers are
 35528 ** corrupted by this sort.
 35529 **
 35530 ** Since there cannot be more than 2^31 distinct pages in a database,
 35531 ** there cannot be more than 31 buckets required by the merge sorter.
 35532 ** One extra bucket is added to catch overflow in case something
 35533 ** ever changes to make the previous sentence incorrect.
 35534 */
 35535 #define N_SORT_BUCKET  32
 35536 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
 35537   PgHdr *a[N_SORT_BUCKET], *p;
 35538   int i;
 35539   memset(a, 0, sizeof(a));
 35540   while( pIn ){
 35541     p = pIn;
 35542     pIn = p->pDirty;
 35543     p->pDirty = 0;
 35544     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
 35545       if( a[i]==0 ){
 35546         a[i] = p;
 35547         break;
 35548       }else{
 35549         p = pcacheMergeDirtyList(a[i], p);
 35550         a[i] = 0;
 35553     if( NEVER(i==N_SORT_BUCKET-1) ){
 35554       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
 35555       ** the input list.  But that is impossible.
 35556       */
 35557       a[i] = pcacheMergeDirtyList(a[i], p);
 35560   p = a[0];
 35561   for(i=1; i<N_SORT_BUCKET; i++){
 35562     p = pcacheMergeDirtyList(p, a[i]);
 35564   return p;
 35567 /*
 35568 ** Return a list of all dirty pages in the cache, sorted by page number.
 35569 */
 35570 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
 35571   PgHdr *p;
 35572   for(p=pCache->pDirty; p; p=p->pDirtyNext){
 35573     p->pDirty = p->pDirtyNext;
 35575   return pcacheSortDirtyList(pCache->pDirty);
 35578 /* 
 35579 ** Return the total number of referenced pages held by the cache.
 35580 */
 35581 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
 35582   return pCache->nRef;
 35585 /*
 35586 ** Return the number of references to the page supplied as an argument.
 35587 */
 35588 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
 35589   return p->nRef;
 35592 /* 
 35593 ** Return the total number of pages in the cache.
 35594 */
 35595 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
 35596   int nPage = 0;
 35597   if( pCache->pCache ){
 35598     nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
 35600   return nPage;
 35603 #ifdef SQLITE_TEST
 35604 /*
 35605 ** Get the suggested cache-size value.
 35606 */
 35607 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
 35608   return numberOfCachePages(pCache);
 35610 #endif
 35612 /*
 35613 ** Set the suggested cache-size value.
 35614 */
 35615 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
 35616   pCache->szCache = mxPage;
 35617   if( pCache->pCache ){
 35618     sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
 35619                                            numberOfCachePages(pCache));
 35623 /*
 35624 ** Free up as much memory as possible from the page cache.
 35625 */
 35626 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
 35627   if( pCache->pCache ){
 35628     sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
 35632 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
 35633 /*
 35634 ** For all dirty pages currently in the cache, invoke the specified
 35635 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
 35636 ** defined.
 35637 */
 35638 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
 35639   PgHdr *pDirty;
 35640   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
 35641     xIter(pDirty);
 35644 #endif
 35646 /************** End of pcache.c **********************************************/
 35647 /************** Begin file pcache1.c *****************************************/
 35648 /*
 35649 ** 2008 November 05
 35650 **
 35651 ** The author disclaims copyright to this source code.  In place of
 35652 ** a legal notice, here is a blessing:
 35653 **
 35654 **    May you do good and not evil.
 35655 **    May you find forgiveness for yourself and forgive others.
 35656 **    May you share freely, never taking more than you give.
 35657 **
 35658 *************************************************************************
 35659 **
 35660 ** This file implements the default page cache implementation (the
 35661 ** sqlite3_pcache interface). It also contains part of the implementation
 35662 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
 35663 ** If the default page cache implementation is overriden, then neither of
 35664 ** these two features are available.
 35665 */
 35668 typedef struct PCache1 PCache1;
 35669 typedef struct PgHdr1 PgHdr1;
 35670 typedef struct PgFreeslot PgFreeslot;
 35671 typedef struct PGroup PGroup;
 35673 /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
 35674 ** of one or more PCaches that are able to recycle each others unpinned
 35675 ** pages when they are under memory pressure.  A PGroup is an instance of
 35676 ** the following object.
 35677 **
 35678 ** This page cache implementation works in one of two modes:
 35679 **
 35680 **   (1)  Every PCache is the sole member of its own PGroup.  There is
 35681 **        one PGroup per PCache.
 35682 **
 35683 **   (2)  There is a single global PGroup that all PCaches are a member
 35684 **        of.
 35685 **
 35686 ** Mode 1 uses more memory (since PCache instances are not able to rob
 35687 ** unused pages from other PCaches) but it also operates without a mutex,
 35688 ** and is therefore often faster.  Mode 2 requires a mutex in order to be
 35689 ** threadsafe, but recycles pages more efficiently.
 35690 **
 35691 ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
 35692 ** PGroup which is the pcache1.grp global variable and its mutex is
 35693 ** SQLITE_MUTEX_STATIC_LRU.
 35694 */
 35695 struct PGroup {
 35696   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
 35697   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
 35698   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
 35699   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
 35700   unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
 35701   PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
 35702 };
 35704 /* Each page cache is an instance of the following object.  Every
 35705 ** open database file (including each in-memory database and each
 35706 ** temporary or transient database) has a single page cache which
 35707 ** is an instance of this object.
 35708 **
 35709 ** Pointers to structures of this type are cast and returned as 
 35710 ** opaque sqlite3_pcache* handles.
 35711 */
 35712 struct PCache1 {
 35713   /* Cache configuration parameters. Page size (szPage) and the purgeable
 35714   ** flag (bPurgeable) are set when the cache is created. nMax may be 
 35715   ** modified at any time by a call to the pcache1Cachesize() method.
 35716   ** The PGroup mutex must be held when accessing nMax.
 35717   */
 35718   PGroup *pGroup;                     /* PGroup this cache belongs to */
 35719   int szPage;                         /* Size of allocated pages in bytes */
 35720   int szExtra;                        /* Size of extra space in bytes */
 35721   int bPurgeable;                     /* True if cache is purgeable */
 35722   unsigned int nMin;                  /* Minimum number of pages reserved */
 35723   unsigned int nMax;                  /* Configured "cache_size" value */
 35724   unsigned int n90pct;                /* nMax*9/10 */
 35725   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
 35727   /* Hash table of all pages. The following variables may only be accessed
 35728   ** when the accessor is holding the PGroup mutex.
 35729   */
 35730   unsigned int nRecyclable;           /* Number of pages in the LRU list */
 35731   unsigned int nPage;                 /* Total number of pages in apHash */
 35732   unsigned int nHash;                 /* Number of slots in apHash[] */
 35733   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
 35734 };
 35736 /*
 35737 ** Each cache entry is represented by an instance of the following 
 35738 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
 35739 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
 35740 ** in memory.
 35741 */
 35742 struct PgHdr1 {
 35743   sqlite3_pcache_page page;
 35744   unsigned int iKey;             /* Key value (page number) */
 35745   PgHdr1 *pNext;                 /* Next in hash table chain */
 35746   PCache1 *pCache;               /* Cache that currently owns this page */
 35747   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 35748   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 35749 };
 35751 /*
 35752 ** Free slots in the allocator used to divide up the buffer provided using
 35753 ** the SQLITE_CONFIG_PAGECACHE mechanism.
 35754 */
 35755 struct PgFreeslot {
 35756   PgFreeslot *pNext;  /* Next free slot */
 35757 };
 35759 /*
 35760 ** Global data used by this cache.
 35761 */
 35762 static SQLITE_WSD struct PCacheGlobal {
 35763   PGroup grp;                    /* The global PGroup for mode (2) */
 35765   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
 35766   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
 35767   ** fixed at sqlite3_initialize() time and do not require mutex protection.
 35768   ** The nFreeSlot and pFree values do require mutex protection.
 35769   */
 35770   int isInit;                    /* True if initialized */
 35771   int szSlot;                    /* Size of each free slot */
 35772   int nSlot;                     /* The number of pcache slots */
 35773   int nReserve;                  /* Try to keep nFreeSlot above this */
 35774   void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
 35775   /* Above requires no mutex.  Use mutex below for variable that follow. */
 35776   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
 35777   PgFreeslot *pFree;             /* Free page blocks */
 35778   int nFreeSlot;                 /* Number of unused pcache slots */
 35779   /* The following value requires a mutex to change.  We skip the mutex on
 35780   ** reading because (1) most platforms read a 32-bit integer atomically and
 35781   ** (2) even if an incorrect value is read, no great harm is done since this
 35782   ** is really just an optimization. */
 35783   int bUnderPressure;            /* True if low on PAGECACHE memory */
 35784 } pcache1_g;
 35786 /*
 35787 ** All code in this file should access the global structure above via the
 35788 ** alias "pcache1". This ensures that the WSD emulation is used when
 35789 ** compiling for systems that do not support real WSD.
 35790 */
 35791 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
 35793 /*
 35794 ** Macros to enter and leave the PCache LRU mutex.
 35795 */
 35796 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
 35797 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
 35799 /******************************************************************************/
 35800 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
 35802 /*
 35803 ** This function is called during initialization if a static buffer is 
 35804 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
 35805 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
 35806 ** enough to contain 'n' buffers of 'sz' bytes each.
 35807 **
 35808 ** This routine is called from sqlite3_initialize() and so it is guaranteed
 35809 ** to be serialized already.  There is no need for further mutexing.
 35810 */
 35811 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
 35812   if( pcache1.isInit ){
 35813     PgFreeslot *p;
 35814     sz = ROUNDDOWN8(sz);
 35815     pcache1.szSlot = sz;
 35816     pcache1.nSlot = pcache1.nFreeSlot = n;
 35817     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
 35818     pcache1.pStart = pBuf;
 35819     pcache1.pFree = 0;
 35820     pcache1.bUnderPressure = 0;
 35821     while( n-- ){
 35822       p = (PgFreeslot*)pBuf;
 35823       p->pNext = pcache1.pFree;
 35824       pcache1.pFree = p;
 35825       pBuf = (void*)&((char*)pBuf)[sz];
 35827     pcache1.pEnd = pBuf;
 35831 /*
 35832 ** Malloc function used within this file to allocate space from the buffer
 35833 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
 35834 ** such buffer exists or there is no space left in it, this function falls 
 35835 ** back to sqlite3Malloc().
 35836 **
 35837 ** Multiple threads can run this routine at the same time.  Global variables
 35838 ** in pcache1 need to be protected via mutex.
 35839 */
 35840 static void *pcache1Alloc(int nByte){
 35841   void *p = 0;
 35842   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 35843   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
 35844   if( nByte<=pcache1.szSlot ){
 35845     sqlite3_mutex_enter(pcache1.mutex);
 35846     p = (PgHdr1 *)pcache1.pFree;
 35847     if( p ){
 35848       pcache1.pFree = pcache1.pFree->pNext;
 35849       pcache1.nFreeSlot--;
 35850       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
 35851       assert( pcache1.nFreeSlot>=0 );
 35852       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
 35854     sqlite3_mutex_leave(pcache1.mutex);
 35856   if( p==0 ){
 35857     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
 35858     ** it from sqlite3Malloc instead.
 35859     */
 35860     p = sqlite3Malloc(nByte);
 35861 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
 35862     if( p ){
 35863       int sz = sqlite3MallocSize(p);
 35864       sqlite3_mutex_enter(pcache1.mutex);
 35865       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
 35866       sqlite3_mutex_leave(pcache1.mutex);
 35868 #endif
 35869     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 35871   return p;
 35874 /*
 35875 ** Free an allocated buffer obtained from pcache1Alloc().
 35876 */
 35877 static int pcache1Free(void *p){
 35878   int nFreed = 0;
 35879   if( p==0 ) return 0;
 35880   if( p>=pcache1.pStart && p<pcache1.pEnd ){
 35881     PgFreeslot *pSlot;
 35882     sqlite3_mutex_enter(pcache1.mutex);
 35883     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
 35884     pSlot = (PgFreeslot*)p;
 35885     pSlot->pNext = pcache1.pFree;
 35886     pcache1.pFree = pSlot;
 35887     pcache1.nFreeSlot++;
 35888     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
 35889     assert( pcache1.nFreeSlot<=pcache1.nSlot );
 35890     sqlite3_mutex_leave(pcache1.mutex);
 35891   }else{
 35892     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 35893     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 35894     nFreed = sqlite3MallocSize(p);
 35895 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
 35896     sqlite3_mutex_enter(pcache1.mutex);
 35897     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
 35898     sqlite3_mutex_leave(pcache1.mutex);
 35899 #endif
 35900     sqlite3_free(p);
 35902   return nFreed;
 35905 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 35906 /*
 35907 ** Return the size of a pcache allocation
 35908 */
 35909 static int pcache1MemSize(void *p){
 35910   if( p>=pcache1.pStart && p<pcache1.pEnd ){
 35911     return pcache1.szSlot;
 35912   }else{
 35913     int iSize;
 35914     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 35915     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 35916     iSize = sqlite3MallocSize(p);
 35917     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 35918     return iSize;
 35921 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 35923 /*
 35924 ** Allocate a new page object initially associated with cache pCache.
 35925 */
 35926 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
 35927   PgHdr1 *p = 0;
 35928   void *pPg;
 35930   /* The group mutex must be released before pcache1Alloc() is called. This
 35931   ** is because it may call sqlite3_release_memory(), which assumes that 
 35932   ** this mutex is not held. */
 35933   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 35934   pcache1LeaveMutex(pCache->pGroup);
 35935 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 35936   pPg = pcache1Alloc(pCache->szPage);
 35937   p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
 35938   if( !pPg || !p ){
 35939     pcache1Free(pPg);
 35940     sqlite3_free(p);
 35941     pPg = 0;
 35943 #else
 35944   pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
 35945   p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
 35946 #endif
 35947   pcache1EnterMutex(pCache->pGroup);
 35949   if( pPg ){
 35950     p->page.pBuf = pPg;
 35951     p->page.pExtra = &p[1];
 35952     if( pCache->bPurgeable ){
 35953       pCache->pGroup->nCurrentPage++;
 35955     return p;
 35957   return 0;
 35960 /*
 35961 ** Free a page object allocated by pcache1AllocPage().
 35962 **
 35963 ** The pointer is allowed to be NULL, which is prudent.  But it turns out
 35964 ** that the current implementation happens to never call this routine
 35965 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
 35966 */
 35967 static void pcache1FreePage(PgHdr1 *p){
 35968   if( ALWAYS(p) ){
 35969     PCache1 *pCache = p->pCache;
 35970     assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
 35971     pcache1Free(p->page.pBuf);
 35972 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 35973     sqlite3_free(p);
 35974 #endif
 35975     if( pCache->bPurgeable ){
 35976       pCache->pGroup->nCurrentPage--;
 35981 /*
 35982 ** Malloc function used by SQLite to obtain space from the buffer configured
 35983 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
 35984 ** exists, this function falls back to sqlite3Malloc().
 35985 */
 35986 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
 35987   return pcache1Alloc(sz);
 35990 /*
 35991 ** Free an allocated buffer obtained from sqlite3PageMalloc().
 35992 */
 35993 SQLITE_PRIVATE void sqlite3PageFree(void *p){
 35994   pcache1Free(p);
 35998 /*
 35999 ** Return true if it desirable to avoid allocating a new page cache
 36000 ** entry.
 36001 **
 36002 ** If memory was allocated specifically to the page cache using
 36003 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
 36004 ** it is desirable to avoid allocating a new page cache entry because
 36005 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
 36006 ** for all page cache needs and we should not need to spill the
 36007 ** allocation onto the heap.
 36008 **
 36009 ** Or, the heap is used for all page cache memory but the heap is
 36010 ** under memory pressure, then again it is desirable to avoid
 36011 ** allocating a new page cache entry in order to avoid stressing
 36012 ** the heap even further.
 36013 */
 36014 static int pcache1UnderMemoryPressure(PCache1 *pCache){
 36015   if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
 36016     return pcache1.bUnderPressure;
 36017   }else{
 36018     return sqlite3HeapNearlyFull();
 36022 /******************************************************************************/
 36023 /******** General Implementation Functions ************************************/
 36025 /*
 36026 ** This function is used to resize the hash table used by the cache passed
 36027 ** as the first argument.
 36028 **
 36029 ** The PCache mutex must be held when this function is called.
 36030 */
 36031 static int pcache1ResizeHash(PCache1 *p){
 36032   PgHdr1 **apNew;
 36033   unsigned int nNew;
 36034   unsigned int i;
 36036   assert( sqlite3_mutex_held(p->pGroup->mutex) );
 36038   nNew = p->nHash*2;
 36039   if( nNew<256 ){
 36040     nNew = 256;
 36043   pcache1LeaveMutex(p->pGroup);
 36044   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
 36045   apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
 36046   if( p->nHash ){ sqlite3EndBenignMalloc(); }
 36047   pcache1EnterMutex(p->pGroup);
 36048   if( apNew ){
 36049     for(i=0; i<p->nHash; i++){
 36050       PgHdr1 *pPage;
 36051       PgHdr1 *pNext = p->apHash[i];
 36052       while( (pPage = pNext)!=0 ){
 36053         unsigned int h = pPage->iKey % nNew;
 36054         pNext = pPage->pNext;
 36055         pPage->pNext = apNew[h];
 36056         apNew[h] = pPage;
 36059     sqlite3_free(p->apHash);
 36060     p->apHash = apNew;
 36061     p->nHash = nNew;
 36064   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
 36067 /*
 36068 ** This function is used internally to remove the page pPage from the 
 36069 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
 36070 ** LRU list, then this function is a no-op.
 36071 **
 36072 ** The PGroup mutex must be held when this function is called.
 36073 **
 36074 ** If pPage is NULL then this routine is a no-op.
 36075 */
 36076 static void pcache1PinPage(PgHdr1 *pPage){
 36077   PCache1 *pCache;
 36078   PGroup *pGroup;
 36080   if( pPage==0 ) return;
 36081   pCache = pPage->pCache;
 36082   pGroup = pCache->pGroup;
 36083   assert( sqlite3_mutex_held(pGroup->mutex) );
 36084   if( pPage->pLruNext || pPage==pGroup->pLruTail ){
 36085     if( pPage->pLruPrev ){
 36086       pPage->pLruPrev->pLruNext = pPage->pLruNext;
 36088     if( pPage->pLruNext ){
 36089       pPage->pLruNext->pLruPrev = pPage->pLruPrev;
 36091     if( pGroup->pLruHead==pPage ){
 36092       pGroup->pLruHead = pPage->pLruNext;
 36094     if( pGroup->pLruTail==pPage ){
 36095       pGroup->pLruTail = pPage->pLruPrev;
 36097     pPage->pLruNext = 0;
 36098     pPage->pLruPrev = 0;
 36099     pPage->pCache->nRecyclable--;
 36104 /*
 36105 ** Remove the page supplied as an argument from the hash table 
 36106 ** (PCache1.apHash structure) that it is currently stored in.
 36107 **
 36108 ** The PGroup mutex must be held when this function is called.
 36109 */
 36110 static void pcache1RemoveFromHash(PgHdr1 *pPage){
 36111   unsigned int h;
 36112   PCache1 *pCache = pPage->pCache;
 36113   PgHdr1 **pp;
 36115   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 36116   h = pPage->iKey % pCache->nHash;
 36117   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
 36118   *pp = (*pp)->pNext;
 36120   pCache->nPage--;
 36123 /*
 36124 ** If there are currently more than nMaxPage pages allocated, try
 36125 ** to recycle pages to reduce the number allocated to nMaxPage.
 36126 */
 36127 static void pcache1EnforceMaxPage(PGroup *pGroup){
 36128   assert( sqlite3_mutex_held(pGroup->mutex) );
 36129   while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
 36130     PgHdr1 *p = pGroup->pLruTail;
 36131     assert( p->pCache->pGroup==pGroup );
 36132     pcache1PinPage(p);
 36133     pcache1RemoveFromHash(p);
 36134     pcache1FreePage(p);
 36138 /*
 36139 ** Discard all pages from cache pCache with a page number (key value) 
 36140 ** greater than or equal to iLimit. Any pinned pages that meet this 
 36141 ** criteria are unpinned before they are discarded.
 36142 **
 36143 ** The PCache mutex must be held when this function is called.
 36144 */
 36145 static void pcache1TruncateUnsafe(
 36146   PCache1 *pCache,             /* The cache to truncate */
 36147   unsigned int iLimit          /* Drop pages with this pgno or larger */
 36148 ){
 36149   TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
 36150   unsigned int h;
 36151   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 36152   for(h=0; h<pCache->nHash; h++){
 36153     PgHdr1 **pp = &pCache->apHash[h]; 
 36154     PgHdr1 *pPage;
 36155     while( (pPage = *pp)!=0 ){
 36156       if( pPage->iKey>=iLimit ){
 36157         pCache->nPage--;
 36158         *pp = pPage->pNext;
 36159         pcache1PinPage(pPage);
 36160         pcache1FreePage(pPage);
 36161       }else{
 36162         pp = &pPage->pNext;
 36163         TESTONLY( nPage++; )
 36167   assert( pCache->nPage==nPage );
 36170 /******************************************************************************/
 36171 /******** sqlite3_pcache Methods **********************************************/
 36173 /*
 36174 ** Implementation of the sqlite3_pcache.xInit method.
 36175 */
 36176 static int pcache1Init(void *NotUsed){
 36177   UNUSED_PARAMETER(NotUsed);
 36178   assert( pcache1.isInit==0 );
 36179   memset(&pcache1, 0, sizeof(pcache1));
 36180   if( sqlite3GlobalConfig.bCoreMutex ){
 36181     pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
 36182     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
 36184   pcache1.grp.mxPinned = 10;
 36185   pcache1.isInit = 1;
 36186   return SQLITE_OK;
 36189 /*
 36190 ** Implementation of the sqlite3_pcache.xShutdown method.
 36191 ** Note that the static mutex allocated in xInit does 
 36192 ** not need to be freed.
 36193 */
 36194 static void pcache1Shutdown(void *NotUsed){
 36195   UNUSED_PARAMETER(NotUsed);
 36196   assert( pcache1.isInit!=0 );
 36197   memset(&pcache1, 0, sizeof(pcache1));
 36200 /*
 36201 ** Implementation of the sqlite3_pcache.xCreate method.
 36202 **
 36203 ** Allocate a new cache.
 36204 */
 36205 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
 36206   PCache1 *pCache;      /* The newly created page cache */
 36207   PGroup *pGroup;       /* The group the new page cache will belong to */
 36208   int sz;               /* Bytes of memory required to allocate the new cache */
 36210   /*
 36211   ** The seperateCache variable is true if each PCache has its own private
 36212   ** PGroup.  In other words, separateCache is true for mode (1) where no
 36213   ** mutexing is required.
 36214   **
 36215   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
 36216   **
 36217   **   *  Always use a unified cache in single-threaded applications
 36218   **
 36219   **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
 36220   **      use separate caches (mode-1)
 36221   */
 36222 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
 36223   const int separateCache = 0;
 36224 #else
 36225   int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
 36226 #endif
 36228   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
 36229   assert( szExtra < 300 );
 36231   sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
 36232   pCache = (PCache1 *)sqlite3MallocZero(sz);
 36233   if( pCache ){
 36234     if( separateCache ){
 36235       pGroup = (PGroup*)&pCache[1];
 36236       pGroup->mxPinned = 10;
 36237     }else{
 36238       pGroup = &pcache1.grp;
 36240     pCache->pGroup = pGroup;
 36241     pCache->szPage = szPage;
 36242     pCache->szExtra = szExtra;
 36243     pCache->bPurgeable = (bPurgeable ? 1 : 0);
 36244     if( bPurgeable ){
 36245       pCache->nMin = 10;
 36246       pcache1EnterMutex(pGroup);
 36247       pGroup->nMinPage += pCache->nMin;
 36248       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 36249       pcache1LeaveMutex(pGroup);
 36252   return (sqlite3_pcache *)pCache;
 36255 /*
 36256 ** Implementation of the sqlite3_pcache.xCachesize method. 
 36257 **
 36258 ** Configure the cache_size limit for a cache.
 36259 */
 36260 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
 36261   PCache1 *pCache = (PCache1 *)p;
 36262   if( pCache->bPurgeable ){
 36263     PGroup *pGroup = pCache->pGroup;
 36264     pcache1EnterMutex(pGroup);
 36265     pGroup->nMaxPage += (nMax - pCache->nMax);
 36266     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 36267     pCache->nMax = nMax;
 36268     pCache->n90pct = pCache->nMax*9/10;
 36269     pcache1EnforceMaxPage(pGroup);
 36270     pcache1LeaveMutex(pGroup);
 36274 /*
 36275 ** Implementation of the sqlite3_pcache.xShrink method. 
 36276 **
 36277 ** Free up as much memory as possible.
 36278 */
 36279 static void pcache1Shrink(sqlite3_pcache *p){
 36280   PCache1 *pCache = (PCache1*)p;
 36281   if( pCache->bPurgeable ){
 36282     PGroup *pGroup = pCache->pGroup;
 36283     int savedMaxPage;
 36284     pcache1EnterMutex(pGroup);
 36285     savedMaxPage = pGroup->nMaxPage;
 36286     pGroup->nMaxPage = 0;
 36287     pcache1EnforceMaxPage(pGroup);
 36288     pGroup->nMaxPage = savedMaxPage;
 36289     pcache1LeaveMutex(pGroup);
 36293 /*
 36294 ** Implementation of the sqlite3_pcache.xPagecount method. 
 36295 */
 36296 static int pcache1Pagecount(sqlite3_pcache *p){
 36297   int n;
 36298   PCache1 *pCache = (PCache1*)p;
 36299   pcache1EnterMutex(pCache->pGroup);
 36300   n = pCache->nPage;
 36301   pcache1LeaveMutex(pCache->pGroup);
 36302   return n;
 36305 /*
 36306 ** Implementation of the sqlite3_pcache.xFetch method. 
 36307 **
 36308 ** Fetch a page by key value.
 36309 **
 36310 ** Whether or not a new page may be allocated by this function depends on
 36311 ** the value of the createFlag argument.  0 means do not allocate a new
 36312 ** page.  1 means allocate a new page if space is easily available.  2 
 36313 ** means to try really hard to allocate a new page.
 36314 **
 36315 ** For a non-purgeable cache (a cache used as the storage for an in-memory
 36316 ** database) there is really no difference between createFlag 1 and 2.  So
 36317 ** the calling function (pcache.c) will never have a createFlag of 1 on
 36318 ** a non-purgeable cache.
 36319 **
 36320 ** There are three different approaches to obtaining space for a page,
 36321 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
 36322 **
 36323 **   1. Regardless of the value of createFlag, the cache is searched for a 
 36324 **      copy of the requested page. If one is found, it is returned.
 36325 **
 36326 **   2. If createFlag==0 and the page is not already in the cache, NULL is
 36327 **      returned.
 36328 **
 36329 **   3. If createFlag is 1, and the page is not already in the cache, then
 36330 **      return NULL (do not allocate a new page) if any of the following
 36331 **      conditions are true:
 36332 **
 36333 **       (a) the number of pages pinned by the cache is greater than
 36334 **           PCache1.nMax, or
 36335 **
 36336 **       (b) the number of pages pinned by the cache is greater than
 36337 **           the sum of nMax for all purgeable caches, less the sum of 
 36338 **           nMin for all other purgeable caches, or
 36339 **
 36340 **   4. If none of the first three conditions apply and the cache is marked
 36341 **      as purgeable, and if one of the following is true:
 36342 **
 36343 **       (a) The number of pages allocated for the cache is already 
 36344 **           PCache1.nMax, or
 36345 **
 36346 **       (b) The number of pages allocated for all purgeable caches is
 36347 **           already equal to or greater than the sum of nMax for all
 36348 **           purgeable caches,
 36349 **
 36350 **       (c) The system is under memory pressure and wants to avoid
 36351 **           unnecessary pages cache entry allocations
 36352 **
 36353 **      then attempt to recycle a page from the LRU list. If it is the right
 36354 **      size, return the recycled buffer. Otherwise, free the buffer and
 36355 **      proceed to step 5. 
 36356 **
 36357 **   5. Otherwise, allocate and return a new page buffer.
 36358 */
 36359 static sqlite3_pcache_page *pcache1Fetch(
 36360   sqlite3_pcache *p, 
 36361   unsigned int iKey, 
 36362   int createFlag
 36363 ){
 36364   unsigned int nPinned;
 36365   PCache1 *pCache = (PCache1 *)p;
 36366   PGroup *pGroup;
 36367   PgHdr1 *pPage = 0;
 36369   assert( pCache->bPurgeable || createFlag!=1 );
 36370   assert( pCache->bPurgeable || pCache->nMin==0 );
 36371   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
 36372   assert( pCache->nMin==0 || pCache->bPurgeable );
 36373   pcache1EnterMutex(pGroup = pCache->pGroup);
 36375   /* Step 1: Search the hash table for an existing entry. */
 36376   if( pCache->nHash>0 ){
 36377     unsigned int h = iKey % pCache->nHash;
 36378     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
 36381   /* Step 2: Abort if no existing page is found and createFlag is 0 */
 36382   if( pPage || createFlag==0 ){
 36383     pcache1PinPage(pPage);
 36384     goto fetch_out;
 36387   /* The pGroup local variable will normally be initialized by the
 36388   ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
 36389   ** then pcache1EnterMutex() is a no-op, so we have to initialize the
 36390   ** local variable here.  Delaying the initialization of pGroup is an
 36391   ** optimization:  The common case is to exit the module before reaching
 36392   ** this point.
 36393   */
 36394 #ifdef SQLITE_MUTEX_OMIT
 36395   pGroup = pCache->pGroup;
 36396 #endif
 36398   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
 36399   assert( pCache->nPage >= pCache->nRecyclable );
 36400   nPinned = pCache->nPage - pCache->nRecyclable;
 36401   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
 36402   assert( pCache->n90pct == pCache->nMax*9/10 );
 36403   if( createFlag==1 && (
 36404         nPinned>=pGroup->mxPinned
 36405      || nPinned>=pCache->n90pct
 36406      || pcache1UnderMemoryPressure(pCache)
 36407   )){
 36408     goto fetch_out;
 36411   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
 36412     goto fetch_out;
 36415   /* Step 4. Try to recycle a page. */
 36416   if( pCache->bPurgeable && pGroup->pLruTail && (
 36417          (pCache->nPage+1>=pCache->nMax)
 36418       || pGroup->nCurrentPage>=pGroup->nMaxPage
 36419       || pcache1UnderMemoryPressure(pCache)
 36420   )){
 36421     PCache1 *pOther;
 36422     pPage = pGroup->pLruTail;
 36423     pcache1RemoveFromHash(pPage);
 36424     pcache1PinPage(pPage);
 36425     pOther = pPage->pCache;
 36427     /* We want to verify that szPage and szExtra are the same for pOther
 36428     ** and pCache.  Assert that we can verify this by comparing sums. */
 36429     assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
 36430     assert( pCache->szExtra<512 );
 36431     assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
 36432     assert( pOther->szExtra<512 );
 36434     if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
 36435       pcache1FreePage(pPage);
 36436       pPage = 0;
 36437     }else{
 36438       pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
 36442   /* Step 5. If a usable page buffer has still not been found, 
 36443   ** attempt to allocate a new one. 
 36444   */
 36445   if( !pPage ){
 36446     if( createFlag==1 ) sqlite3BeginBenignMalloc();
 36447     pPage = pcache1AllocPage(pCache);
 36448     if( createFlag==1 ) sqlite3EndBenignMalloc();
 36451   if( pPage ){
 36452     unsigned int h = iKey % pCache->nHash;
 36453     pCache->nPage++;
 36454     pPage->iKey = iKey;
 36455     pPage->pNext = pCache->apHash[h];
 36456     pPage->pCache = pCache;
 36457     pPage->pLruPrev = 0;
 36458     pPage->pLruNext = 0;
 36459     *(void **)pPage->page.pExtra = 0;
 36460     pCache->apHash[h] = pPage;
 36463 fetch_out:
 36464   if( pPage && iKey>pCache->iMaxKey ){
 36465     pCache->iMaxKey = iKey;
 36467   pcache1LeaveMutex(pGroup);
 36468   return &pPage->page;
 36472 /*
 36473 ** Implementation of the sqlite3_pcache.xUnpin method.
 36474 **
 36475 ** Mark a page as unpinned (eligible for asynchronous recycling).
 36476 */
 36477 static void pcache1Unpin(
 36478   sqlite3_pcache *p, 
 36479   sqlite3_pcache_page *pPg, 
 36480   int reuseUnlikely
 36481 ){
 36482   PCache1 *pCache = (PCache1 *)p;
 36483   PgHdr1 *pPage = (PgHdr1 *)pPg;
 36484   PGroup *pGroup = pCache->pGroup;
 36486   assert( pPage->pCache==pCache );
 36487   pcache1EnterMutex(pGroup);
 36489   /* It is an error to call this function if the page is already 
 36490   ** part of the PGroup LRU list.
 36491   */
 36492   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
 36493   assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
 36495   if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
 36496     pcache1RemoveFromHash(pPage);
 36497     pcache1FreePage(pPage);
 36498   }else{
 36499     /* Add the page to the PGroup LRU list. */
 36500     if( pGroup->pLruHead ){
 36501       pGroup->pLruHead->pLruPrev = pPage;
 36502       pPage->pLruNext = pGroup->pLruHead;
 36503       pGroup->pLruHead = pPage;
 36504     }else{
 36505       pGroup->pLruTail = pPage;
 36506       pGroup->pLruHead = pPage;
 36508     pCache->nRecyclable++;
 36511   pcache1LeaveMutex(pCache->pGroup);
 36514 /*
 36515 ** Implementation of the sqlite3_pcache.xRekey method. 
 36516 */
 36517 static void pcache1Rekey(
 36518   sqlite3_pcache *p,
 36519   sqlite3_pcache_page *pPg,
 36520   unsigned int iOld,
 36521   unsigned int iNew
 36522 ){
 36523   PCache1 *pCache = (PCache1 *)p;
 36524   PgHdr1 *pPage = (PgHdr1 *)pPg;
 36525   PgHdr1 **pp;
 36526   unsigned int h; 
 36527   assert( pPage->iKey==iOld );
 36528   assert( pPage->pCache==pCache );
 36530   pcache1EnterMutex(pCache->pGroup);
 36532   h = iOld%pCache->nHash;
 36533   pp = &pCache->apHash[h];
 36534   while( (*pp)!=pPage ){
 36535     pp = &(*pp)->pNext;
 36537   *pp = pPage->pNext;
 36539   h = iNew%pCache->nHash;
 36540   pPage->iKey = iNew;
 36541   pPage->pNext = pCache->apHash[h];
 36542   pCache->apHash[h] = pPage;
 36543   if( iNew>pCache->iMaxKey ){
 36544     pCache->iMaxKey = iNew;
 36547   pcache1LeaveMutex(pCache->pGroup);
 36550 /*
 36551 ** Implementation of the sqlite3_pcache.xTruncate method. 
 36552 **
 36553 ** Discard all unpinned pages in the cache with a page number equal to
 36554 ** or greater than parameter iLimit. Any pinned pages with a page number
 36555 ** equal to or greater than iLimit are implicitly unpinned.
 36556 */
 36557 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
 36558   PCache1 *pCache = (PCache1 *)p;
 36559   pcache1EnterMutex(pCache->pGroup);
 36560   if( iLimit<=pCache->iMaxKey ){
 36561     pcache1TruncateUnsafe(pCache, iLimit);
 36562     pCache->iMaxKey = iLimit-1;
 36564   pcache1LeaveMutex(pCache->pGroup);
 36567 /*
 36568 ** Implementation of the sqlite3_pcache.xDestroy method. 
 36569 **
 36570 ** Destroy a cache allocated using pcache1Create().
 36571 */
 36572 static void pcache1Destroy(sqlite3_pcache *p){
 36573   PCache1 *pCache = (PCache1 *)p;
 36574   PGroup *pGroup = pCache->pGroup;
 36575   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
 36576   pcache1EnterMutex(pGroup);
 36577   pcache1TruncateUnsafe(pCache, 0);
 36578   assert( pGroup->nMaxPage >= pCache->nMax );
 36579   pGroup->nMaxPage -= pCache->nMax;
 36580   assert( pGroup->nMinPage >= pCache->nMin );
 36581   pGroup->nMinPage -= pCache->nMin;
 36582   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 36583   pcache1EnforceMaxPage(pGroup);
 36584   pcache1LeaveMutex(pGroup);
 36585   sqlite3_free(pCache->apHash);
 36586   sqlite3_free(pCache);
 36589 /*
 36590 ** This function is called during initialization (sqlite3_initialize()) to
 36591 ** install the default pluggable cache module, assuming the user has not
 36592 ** already provided an alternative.
 36593 */
 36594 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
 36595   static const sqlite3_pcache_methods2 defaultMethods = {
 36596     1,                       /* iVersion */
 36597     0,                       /* pArg */
 36598     pcache1Init,             /* xInit */
 36599     pcache1Shutdown,         /* xShutdown */
 36600     pcache1Create,           /* xCreate */
 36601     pcache1Cachesize,        /* xCachesize */
 36602     pcache1Pagecount,        /* xPagecount */
 36603     pcache1Fetch,            /* xFetch */
 36604     pcache1Unpin,            /* xUnpin */
 36605     pcache1Rekey,            /* xRekey */
 36606     pcache1Truncate,         /* xTruncate */
 36607     pcache1Destroy,          /* xDestroy */
 36608     pcache1Shrink            /* xShrink */
 36609   };
 36610   sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
 36613 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 36614 /*
 36615 ** This function is called to free superfluous dynamically allocated memory
 36616 ** held by the pager system. Memory in use by any SQLite pager allocated
 36617 ** by the current thread may be sqlite3_free()ed.
 36618 **
 36619 ** nReq is the number of bytes of memory required. Once this much has
 36620 ** been released, the function returns. The return value is the total number 
 36621 ** of bytes of memory released.
 36622 */
 36623 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
 36624   int nFree = 0;
 36625   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 36626   assert( sqlite3_mutex_notheld(pcache1.mutex) );
 36627   if( pcache1.pStart==0 ){
 36628     PgHdr1 *p;
 36629     pcache1EnterMutex(&pcache1.grp);
 36630     while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
 36631       nFree += pcache1MemSize(p->page.pBuf);
 36632 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 36633       nFree += sqlite3MemSize(p);
 36634 #endif
 36635       pcache1PinPage(p);
 36636       pcache1RemoveFromHash(p);
 36637       pcache1FreePage(p);
 36639     pcache1LeaveMutex(&pcache1.grp);
 36641   return nFree;
 36643 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 36645 #ifdef SQLITE_TEST
 36646 /*
 36647 ** This function is used by test procedures to inspect the internal state
 36648 ** of the global cache.
 36649 */
 36650 SQLITE_PRIVATE void sqlite3PcacheStats(
 36651   int *pnCurrent,      /* OUT: Total number of pages cached */
 36652   int *pnMax,          /* OUT: Global maximum cache size */
 36653   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
 36654   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
 36655 ){
 36656   PgHdr1 *p;
 36657   int nRecyclable = 0;
 36658   for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
 36659     nRecyclable++;
 36661   *pnCurrent = pcache1.grp.nCurrentPage;
 36662   *pnMax = (int)pcache1.grp.nMaxPage;
 36663   *pnMin = (int)pcache1.grp.nMinPage;
 36664   *pnRecyclable = nRecyclable;
 36666 #endif
 36668 /************** End of pcache1.c *********************************************/
 36669 /************** Begin file rowset.c ******************************************/
 36670 /*
 36671 ** 2008 December 3
 36672 **
 36673 ** The author disclaims copyright to this source code.  In place of
 36674 ** a legal notice, here is a blessing:
 36675 **
 36676 **    May you do good and not evil.
 36677 **    May you find forgiveness for yourself and forgive others.
 36678 **    May you share freely, never taking more than you give.
 36679 **
 36680 *************************************************************************
 36681 **
 36682 ** This module implements an object we call a "RowSet".
 36683 **
 36684 ** The RowSet object is a collection of rowids.  Rowids
 36685 ** are inserted into the RowSet in an arbitrary order.  Inserts
 36686 ** can be intermixed with tests to see if a given rowid has been
 36687 ** previously inserted into the RowSet.
 36688 **
 36689 ** After all inserts are finished, it is possible to extract the
 36690 ** elements of the RowSet in sorted order.  Once this extraction
 36691 ** process has started, no new elements may be inserted.
 36692 **
 36693 ** Hence, the primitive operations for a RowSet are:
 36694 **
 36695 **    CREATE
 36696 **    INSERT
 36697 **    TEST
 36698 **    SMALLEST
 36699 **    DESTROY
 36700 **
 36701 ** The CREATE and DESTROY primitives are the constructor and destructor,
 36702 ** obviously.  The INSERT primitive adds a new element to the RowSet.
 36703 ** TEST checks to see if an element is already in the RowSet.  SMALLEST
 36704 ** extracts the least value from the RowSet.
 36705 **
 36706 ** The INSERT primitive might allocate additional memory.  Memory is
 36707 ** allocated in chunks so most INSERTs do no allocation.  There is an 
 36708 ** upper bound on the size of allocated memory.  No memory is freed
 36709 ** until DESTROY.
 36710 **
 36711 ** The TEST primitive includes a "batch" number.  The TEST primitive
 36712 ** will only see elements that were inserted before the last change
 36713 ** in the batch number.  In other words, if an INSERT occurs between
 36714 ** two TESTs where the TESTs have the same batch nubmer, then the
 36715 ** value added by the INSERT will not be visible to the second TEST.
 36716 ** The initial batch number is zero, so if the very first TEST contains
 36717 ** a non-zero batch number, it will see all prior INSERTs.
 36718 **
 36719 ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
 36720 ** that is attempted.
 36721 **
 36722 ** The cost of an INSERT is roughly constant.  (Sometime new memory
 36723 ** has to be allocated on an INSERT.)  The cost of a TEST with a new
 36724 ** batch number is O(NlogN) where N is the number of elements in the RowSet.
 36725 ** The cost of a TEST using the same batch number is O(logN).  The cost
 36726 ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
 36727 ** primitives are constant time.  The cost of DESTROY is O(N).
 36728 **
 36729 ** There is an added cost of O(N) when switching between TEST and
 36730 ** SMALLEST primitives.
 36731 */
 36734 /*
 36735 ** Target size for allocation chunks.
 36736 */
 36737 #define ROWSET_ALLOCATION_SIZE 1024
 36739 /*
 36740 ** The number of rowset entries per allocation chunk.
 36741 */
 36742 #define ROWSET_ENTRY_PER_CHUNK  \
 36743                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
 36745 /*
 36746 ** Each entry in a RowSet is an instance of the following object.
 36747 **
 36748 ** This same object is reused to store a linked list of trees of RowSetEntry
 36749 ** objects.  In that alternative use, pRight points to the next entry
 36750 ** in the list, pLeft points to the tree, and v is unused.  The
 36751 ** RowSet.pForest value points to the head of this forest list.
 36752 */
 36753 struct RowSetEntry {            
 36754   i64 v;                        /* ROWID value for this entry */
 36755   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
 36756   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
 36757 };
 36759 /*
 36760 ** RowSetEntry objects are allocated in large chunks (instances of the
 36761 ** following structure) to reduce memory allocation overhead.  The
 36762 ** chunks are kept on a linked list so that they can be deallocated
 36763 ** when the RowSet is destroyed.
 36764 */
 36765 struct RowSetChunk {
 36766   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
 36767   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
 36768 };
 36770 /*
 36771 ** A RowSet in an instance of the following structure.
 36772 **
 36773 ** A typedef of this structure if found in sqliteInt.h.
 36774 */
 36775 struct RowSet {
 36776   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
 36777   sqlite3 *db;                   /* The database connection */
 36778   struct RowSetEntry *pEntry;    /* List of entries using pRight */
 36779   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
 36780   struct RowSetEntry *pFresh;    /* Source of new entry objects */
 36781   struct RowSetEntry *pForest;   /* List of binary trees of entries */
 36782   u16 nFresh;                    /* Number of objects on pFresh */
 36783   u8 rsFlags;                    /* Various flags */
 36784   u8 iBatch;                     /* Current insert batch */
 36785 };
 36787 /*
 36788 ** Allowed values for RowSet.rsFlags
 36789 */
 36790 #define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
 36791 #define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
 36793 /*
 36794 ** Turn bulk memory into a RowSet object.  N bytes of memory
 36795 ** are available at pSpace.  The db pointer is used as a memory context
 36796 ** for any subsequent allocations that need to occur.
 36797 ** Return a pointer to the new RowSet object.
 36798 **
 36799 ** It must be the case that N is sufficient to make a Rowset.  If not
 36800 ** an assertion fault occurs.
 36801 ** 
 36802 ** If N is larger than the minimum, use the surplus as an initial
 36803 ** allocation of entries available to be filled.
 36804 */
 36805 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
 36806   RowSet *p;
 36807   assert( N >= ROUND8(sizeof(*p)) );
 36808   p = pSpace;
 36809   p->pChunk = 0;
 36810   p->db = db;
 36811   p->pEntry = 0;
 36812   p->pLast = 0;
 36813   p->pForest = 0;
 36814   p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
 36815   p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
 36816   p->rsFlags = ROWSET_SORTED;
 36817   p->iBatch = 0;
 36818   return p;
 36821 /*
 36822 ** Deallocate all chunks from a RowSet.  This frees all memory that
 36823 ** the RowSet has allocated over its lifetime.  This routine is
 36824 ** the destructor for the RowSet.
 36825 */
 36826 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
 36827   struct RowSetChunk *pChunk, *pNextChunk;
 36828   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
 36829     pNextChunk = pChunk->pNextChunk;
 36830     sqlite3DbFree(p->db, pChunk);
 36832   p->pChunk = 0;
 36833   p->nFresh = 0;
 36834   p->pEntry = 0;
 36835   p->pLast = 0;
 36836   p->pForest = 0;
 36837   p->rsFlags = ROWSET_SORTED;
 36840 /*
 36841 ** Allocate a new RowSetEntry object that is associated with the
 36842 ** given RowSet.  Return a pointer to the new and completely uninitialized
 36843 ** objected.
 36844 **
 36845 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
 36846 ** routine returns NULL.
 36847 */
 36848 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
 36849   assert( p!=0 );
 36850   if( p->nFresh==0 ){
 36851     struct RowSetChunk *pNew;
 36852     pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
 36853     if( pNew==0 ){
 36854       return 0;
 36856     pNew->pNextChunk = p->pChunk;
 36857     p->pChunk = pNew;
 36858     p->pFresh = pNew->aEntry;
 36859     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
 36861   p->nFresh--;
 36862   return p->pFresh++;
 36865 /*
 36866 ** Insert a new value into a RowSet.
 36867 **
 36868 ** The mallocFailed flag of the database connection is set if a
 36869 ** memory allocation fails.
 36870 */
 36871 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
 36872   struct RowSetEntry *pEntry;  /* The new entry */
 36873   struct RowSetEntry *pLast;   /* The last prior entry */
 36875   /* This routine is never called after sqlite3RowSetNext() */
 36876   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
 36878   pEntry = rowSetEntryAlloc(p);
 36879   if( pEntry==0 ) return;
 36880   pEntry->v = rowid;
 36881   pEntry->pRight = 0;
 36882   pLast = p->pLast;
 36883   if( pLast ){
 36884     if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
 36885       p->rsFlags &= ~ROWSET_SORTED;
 36887     pLast->pRight = pEntry;
 36888   }else{
 36889     p->pEntry = pEntry;
 36891   p->pLast = pEntry;
 36894 /*
 36895 ** Merge two lists of RowSetEntry objects.  Remove duplicates.
 36896 **
 36897 ** The input lists are connected via pRight pointers and are 
 36898 ** assumed to each already be in sorted order.
 36899 */
 36900 static struct RowSetEntry *rowSetEntryMerge(
 36901   struct RowSetEntry *pA,    /* First sorted list to be merged */
 36902   struct RowSetEntry *pB     /* Second sorted list to be merged */
 36903 ){
 36904   struct RowSetEntry head;
 36905   struct RowSetEntry *pTail;
 36907   pTail = &head;
 36908   while( pA && pB ){
 36909     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
 36910     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
 36911     if( pA->v<pB->v ){
 36912       pTail->pRight = pA;
 36913       pA = pA->pRight;
 36914       pTail = pTail->pRight;
 36915     }else if( pB->v<pA->v ){
 36916       pTail->pRight = pB;
 36917       pB = pB->pRight;
 36918       pTail = pTail->pRight;
 36919     }else{
 36920       pA = pA->pRight;
 36923   if( pA ){
 36924     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
 36925     pTail->pRight = pA;
 36926   }else{
 36927     assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
 36928     pTail->pRight = pB;
 36930   return head.pRight;
 36933 /*
 36934 ** Sort all elements on the list of RowSetEntry objects into order of
 36935 ** increasing v.
 36936 */ 
 36937 static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
 36938   unsigned int i;
 36939   struct RowSetEntry *pNext, *aBucket[40];
 36941   memset(aBucket, 0, sizeof(aBucket));
 36942   while( pIn ){
 36943     pNext = pIn->pRight;
 36944     pIn->pRight = 0;
 36945     for(i=0; aBucket[i]; i++){
 36946       pIn = rowSetEntryMerge(aBucket[i], pIn);
 36947       aBucket[i] = 0;
 36949     aBucket[i] = pIn;
 36950     pIn = pNext;
 36952   pIn = 0;
 36953   for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
 36954     pIn = rowSetEntryMerge(pIn, aBucket[i]);
 36956   return pIn;
 36960 /*
 36961 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
 36962 ** Convert this tree into a linked list connected by the pRight pointers
 36963 ** and return pointers to the first and last elements of the new list.
 36964 */
 36965 static void rowSetTreeToList(
 36966   struct RowSetEntry *pIn,         /* Root of the input tree */
 36967   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
 36968   struct RowSetEntry **ppLast      /* Write tail of the output list here */
 36969 ){
 36970   assert( pIn!=0 );
 36971   if( pIn->pLeft ){
 36972     struct RowSetEntry *p;
 36973     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
 36974     p->pRight = pIn;
 36975   }else{
 36976     *ppFirst = pIn;
 36978   if( pIn->pRight ){
 36979     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
 36980   }else{
 36981     *ppLast = pIn;
 36983   assert( (*ppLast)->pRight==0 );
 36987 /*
 36988 ** Convert a sorted list of elements (connected by pRight) into a binary
 36989 ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
 36990 ** node taken from the head of *ppList.  A depth of 2 means a tree with
 36991 ** three nodes.  And so forth.
 36992 **
 36993 ** Use as many entries from the input list as required and update the
 36994 ** *ppList to point to the unused elements of the list.  If the input
 36995 ** list contains too few elements, then construct an incomplete tree
 36996 ** and leave *ppList set to NULL.
 36997 **
 36998 ** Return a pointer to the root of the constructed binary tree.
 36999 */
 37000 static struct RowSetEntry *rowSetNDeepTree(
 37001   struct RowSetEntry **ppList,
 37002   int iDepth
 37003 ){
 37004   struct RowSetEntry *p;         /* Root of the new tree */
 37005   struct RowSetEntry *pLeft;     /* Left subtree */
 37006   if( *ppList==0 ){
 37007     return 0;
 37009   if( iDepth==1 ){
 37010     p = *ppList;
 37011     *ppList = p->pRight;
 37012     p->pLeft = p->pRight = 0;
 37013     return p;
 37015   pLeft = rowSetNDeepTree(ppList, iDepth-1);
 37016   p = *ppList;
 37017   if( p==0 ){
 37018     return pLeft;
 37020   p->pLeft = pLeft;
 37021   *ppList = p->pRight;
 37022   p->pRight = rowSetNDeepTree(ppList, iDepth-1);
 37023   return p;
 37026 /*
 37027 ** Convert a sorted list of elements into a binary tree. Make the tree
 37028 ** as deep as it needs to be in order to contain the entire list.
 37029 */
 37030 static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
 37031   int iDepth;           /* Depth of the tree so far */
 37032   struct RowSetEntry *p;       /* Current tree root */
 37033   struct RowSetEntry *pLeft;   /* Left subtree */
 37035   assert( pList!=0 );
 37036   p = pList;
 37037   pList = p->pRight;
 37038   p->pLeft = p->pRight = 0;
 37039   for(iDepth=1; pList; iDepth++){
 37040     pLeft = p;
 37041     p = pList;
 37042     pList = p->pRight;
 37043     p->pLeft = pLeft;
 37044     p->pRight = rowSetNDeepTree(&pList, iDepth);
 37046   return p;
 37049 /*
 37050 ** Take all the entries on p->pEntry and on the trees in p->pForest and
 37051 ** sort them all together into one big ordered list on p->pEntry.
 37052 **
 37053 ** This routine should only be called once in the life of a RowSet.
 37054 */
 37055 static void rowSetToList(RowSet *p){
 37057   /* This routine is called only once */
 37058   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
 37060   if( (p->rsFlags & ROWSET_SORTED)==0 ){
 37061     p->pEntry = rowSetEntrySort(p->pEntry);
 37064   /* While this module could theoretically support it, sqlite3RowSetNext()
 37065   ** is never called after sqlite3RowSetText() for the same RowSet.  So
 37066   ** there is never a forest to deal with.  Should this change, simply
 37067   ** remove the assert() and the #if 0. */
 37068   assert( p->pForest==0 );
 37069 #if 0
 37070   while( p->pForest ){
 37071     struct RowSetEntry *pTree = p->pForest->pLeft;
 37072     if( pTree ){
 37073       struct RowSetEntry *pHead, *pTail;
 37074       rowSetTreeToList(pTree, &pHead, &pTail);
 37075       p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
 37077     p->pForest = p->pForest->pRight;
 37079 #endif
 37080   p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
 37083 /*
 37084 ** Extract the smallest element from the RowSet.
 37085 ** Write the element into *pRowid.  Return 1 on success.  Return
 37086 ** 0 if the RowSet is already empty.
 37087 **
 37088 ** After this routine has been called, the sqlite3RowSetInsert()
 37089 ** routine may not be called again.  
 37090 */
 37091 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
 37092   assert( p!=0 );
 37094   /* Merge the forest into a single sorted list on first call */
 37095   if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
 37097   /* Return the next entry on the list */
 37098   if( p->pEntry ){
 37099     *pRowid = p->pEntry->v;
 37100     p->pEntry = p->pEntry->pRight;
 37101     if( p->pEntry==0 ){
 37102       sqlite3RowSetClear(p);
 37104     return 1;
 37105   }else{
 37106     return 0;
 37110 /*
 37111 ** Check to see if element iRowid was inserted into the rowset as
 37112 ** part of any insert batch prior to iBatch.  Return 1 or 0.
 37113 **
 37114 ** If this is the first test of a new batch and if there exist entires
 37115 ** on pRowSet->pEntry, then sort those entires into the forest at
 37116 ** pRowSet->pForest so that they can be tested.
 37117 */
 37118 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
 37119   struct RowSetEntry *p, *pTree;
 37121   /* This routine is never called after sqlite3RowSetNext() */
 37122   assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
 37124   /* Sort entries into the forest on the first test of a new batch 
 37125   */
 37126   if( iBatch!=pRowSet->iBatch ){
 37127     p = pRowSet->pEntry;
 37128     if( p ){
 37129       struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
 37130       if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
 37131         p = rowSetEntrySort(p);
 37133       for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
 37134         ppPrevTree = &pTree->pRight;
 37135         if( pTree->pLeft==0 ){
 37136           pTree->pLeft = rowSetListToTree(p);
 37137           break;
 37138         }else{
 37139           struct RowSetEntry *pAux, *pTail;
 37140           rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
 37141           pTree->pLeft = 0;
 37142           p = rowSetEntryMerge(pAux, p);
 37145       if( pTree==0 ){
 37146         *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
 37147         if( pTree ){
 37148           pTree->v = 0;
 37149           pTree->pRight = 0;
 37150           pTree->pLeft = rowSetListToTree(p);
 37153       pRowSet->pEntry = 0;
 37154       pRowSet->pLast = 0;
 37155       pRowSet->rsFlags |= ROWSET_SORTED;
 37157     pRowSet->iBatch = iBatch;
 37160   /* Test to see if the iRowid value appears anywhere in the forest.
 37161   ** Return 1 if it does and 0 if not.
 37162   */
 37163   for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
 37164     p = pTree->pLeft;
 37165     while( p ){
 37166       if( p->v<iRowid ){
 37167         p = p->pRight;
 37168       }else if( p->v>iRowid ){
 37169         p = p->pLeft;
 37170       }else{
 37171         return 1;
 37175   return 0;
 37178 /************** End of rowset.c **********************************************/
 37179 /************** Begin file pager.c *******************************************/
 37180 /*
 37181 ** 2001 September 15
 37182 **
 37183 ** The author disclaims copyright to this source code.  In place of
 37184 ** a legal notice, here is a blessing:
 37185 **
 37186 **    May you do good and not evil.
 37187 **    May you find forgiveness for yourself and forgive others.
 37188 **    May you share freely, never taking more than you give.
 37189 **
 37190 *************************************************************************
 37191 ** This is the implementation of the page cache subsystem or "pager".
 37192 ** 
 37193 ** The pager is used to access a database disk file.  It implements
 37194 ** atomic commit and rollback through the use of a journal file that
 37195 ** is separate from the database file.  The pager also implements file
 37196 ** locking to prevent two processes from writing the same database
 37197 ** file simultaneously, or one process from reading the database while
 37198 ** another is writing.
 37199 */
 37200 #ifndef SQLITE_OMIT_DISKIO
 37201 /************** Include wal.h in the middle of pager.c ***********************/
 37202 /************** Begin file wal.h *********************************************/
 37203 /*
 37204 ** 2010 February 1
 37205 **
 37206 ** The author disclaims copyright to this source code.  In place of
 37207 ** a legal notice, here is a blessing:
 37208 **
 37209 **    May you do good and not evil.
 37210 **    May you find forgiveness for yourself and forgive others.
 37211 **    May you share freely, never taking more than you give.
 37212 **
 37213 *************************************************************************
 37214 ** This header file defines the interface to the write-ahead logging 
 37215 ** system. Refer to the comments below and the header comment attached to 
 37216 ** the implementation of each function in log.c for further details.
 37217 */
 37219 #ifndef _WAL_H_
 37220 #define _WAL_H_
 37223 /* Additional values that can be added to the sync_flags argument of
 37224 ** sqlite3WalFrames():
 37225 */
 37226 #define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
 37227 #define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
 37229 #ifdef SQLITE_OMIT_WAL
 37230 # define sqlite3WalOpen(x,y,z)                   0
 37231 # define sqlite3WalLimit(x,y)
 37232 # define sqlite3WalClose(w,x,y,z)                0
 37233 # define sqlite3WalBeginReadTransaction(y,z)     0
 37234 # define sqlite3WalEndReadTransaction(z)
 37235 # define sqlite3WalRead(v,w,x,y,z)               0
 37236 # define sqlite3WalDbsize(y)                     0
 37237 # define sqlite3WalBeginWriteTransaction(y)      0
 37238 # define sqlite3WalEndWriteTransaction(x)        0
 37239 # define sqlite3WalUndo(x,y,z)                   0
 37240 # define sqlite3WalSavepoint(y,z)
 37241 # define sqlite3WalSavepointUndo(y,z)            0
 37242 # define sqlite3WalFrames(u,v,w,x,y,z)           0
 37243 # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
 37244 # define sqlite3WalCallback(z)                   0
 37245 # define sqlite3WalExclusiveMode(y,z)            0
 37246 # define sqlite3WalHeapMemory(z)                 0
 37247 # define sqlite3WalFramesize(z)                  0
 37248 #else
 37250 #define WAL_SAVEPOINT_NDATA 4
 37252 /* Connection to a write-ahead log (WAL) file. 
 37253 ** There is one object of this type for each pager. 
 37254 */
 37255 typedef struct Wal Wal;
 37257 /* Open and close a connection to a write-ahead log. */
 37258 SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
 37259 SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
 37261 /* Set the limiting size of a WAL file. */
 37262 SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
 37264 /* Used by readers to open (lock) and close (unlock) a snapshot.  A 
 37265 ** snapshot is like a read-transaction.  It is the state of the database
 37266 ** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
 37267 ** preserves the current state even if the other threads or processes
 37268 ** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
 37269 ** transaction and releases the lock.
 37270 */
 37271 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
 37272 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
 37274 /* Read a page from the write-ahead log, if it is present. */
 37275 SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
 37277 /* If the WAL is not empty, return the size of the database. */
 37278 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
 37280 /* Obtain or release the WRITER lock. */
 37281 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
 37282 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
 37284 /* Undo any frames written (but not committed) to the log */
 37285 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
 37287 /* Return an integer that records the current (uncommitted) write
 37288 ** position in the WAL */
 37289 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
 37291 /* Move the write position of the WAL back to iFrame.  Called in
 37292 ** response to a ROLLBACK TO command. */
 37293 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
 37295 /* Write a frame or frames to the log. */
 37296 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
 37298 /* Copy pages from the log to the database file */ 
 37299 SQLITE_PRIVATE int sqlite3WalCheckpoint(
 37300   Wal *pWal,                      /* Write-ahead log connection */
 37301   int eMode,                      /* One of PASSIVE, FULL and RESTART */
 37302   int (*xBusy)(void*),            /* Function to call when busy */
 37303   void *pBusyArg,                 /* Context argument for xBusyHandler */
 37304   int sync_flags,                 /* Flags to sync db file with (or 0) */
 37305   int nBuf,                       /* Size of buffer nBuf */
 37306   u8 *zBuf,                       /* Temporary buffer to use */
 37307   int *pnLog,                     /* OUT: Number of frames in WAL */
 37308   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 37309 );
 37311 /* Return the value to pass to a sqlite3_wal_hook callback, the
 37312 ** number of frames in the WAL at the point of the last commit since
 37313 ** sqlite3WalCallback() was called.  If no commits have occurred since
 37314 ** the last call, then return 0.
 37315 */
 37316 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
 37318 /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
 37319 ** by the pager layer on the database file.
 37320 */
 37321 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
 37323 /* Return true if the argument is non-NULL and the WAL module is using
 37324 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
 37325 ** WAL module is using shared-memory, return false. 
 37326 */
 37327 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
 37329 #ifdef SQLITE_ENABLE_ZIPVFS
 37330 /* If the WAL file is not empty, return the number of bytes of content
 37331 ** stored in each frame (i.e. the db page-size when the WAL was created).
 37332 */
 37333 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
 37334 #endif
 37336 #endif /* ifndef SQLITE_OMIT_WAL */
 37337 #endif /* _WAL_H_ */
 37339 /************** End of wal.h *************************************************/
 37340 /************** Continuing where we left off in pager.c **********************/
 37343 /******************* NOTES ON THE DESIGN OF THE PAGER ************************
 37344 **
 37345 ** This comment block describes invariants that hold when using a rollback
 37346 ** journal.  These invariants do not apply for journal_mode=WAL,
 37347 ** journal_mode=MEMORY, or journal_mode=OFF.
 37348 **
 37349 ** Within this comment block, a page is deemed to have been synced
 37350 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
 37351 ** Otherwise, the page is not synced until the xSync method of the VFS
 37352 ** is called successfully on the file containing the page.
 37353 **
 37354 ** Definition:  A page of the database file is said to be "overwriteable" if
 37355 ** one or more of the following are true about the page:
 37356 ** 
 37357 **     (a)  The original content of the page as it was at the beginning of
 37358 **          the transaction has been written into the rollback journal and
 37359 **          synced.
 37360 ** 
 37361 **     (b)  The page was a freelist leaf page at the start of the transaction.
 37362 ** 
 37363 **     (c)  The page number is greater than the largest page that existed in
 37364 **          the database file at the start of the transaction.
 37365 ** 
 37366 ** (1) A page of the database file is never overwritten unless one of the
 37367 **     following are true:
 37368 ** 
 37369 **     (a) The page and all other pages on the same sector are overwriteable.
 37370 ** 
 37371 **     (b) The atomic page write optimization is enabled, and the entire
 37372 **         transaction other than the update of the transaction sequence
 37373 **         number consists of a single page change.
 37374 ** 
 37375 ** (2) The content of a page written into the rollback journal exactly matches
 37376 **     both the content in the database when the rollback journal was written
 37377 **     and the content in the database at the beginning of the current
 37378 **     transaction.
 37379 ** 
 37380 ** (3) Writes to the database file are an integer multiple of the page size
 37381 **     in length and are aligned on a page boundary.
 37382 ** 
 37383 ** (4) Reads from the database file are either aligned on a page boundary and
 37384 **     an integer multiple of the page size in length or are taken from the
 37385 **     first 100 bytes of the database file.
 37386 ** 
 37387 ** (5) All writes to the database file are synced prior to the rollback journal
 37388 **     being deleted, truncated, or zeroed.
 37389 ** 
 37390 ** (6) If a master journal file is used, then all writes to the database file
 37391 **     are synced prior to the master journal being deleted.
 37392 ** 
 37393 ** Definition: Two databases (or the same database at two points it time)
 37394 ** are said to be "logically equivalent" if they give the same answer to
 37395 ** all queries.  Note in particular the content of freelist leaf
 37396 ** pages can be changed arbitarily without effecting the logical equivalence
 37397 ** of the database.
 37398 ** 
 37399 ** (7) At any time, if any subset, including the empty set and the total set,
 37400 **     of the unsynced changes to a rollback journal are removed and the 
 37401 **     journal is rolled back, the resulting database file will be logical
 37402 **     equivalent to the database file at the beginning of the transaction.
 37403 ** 
 37404 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
 37405 **     is called to restore the database file to the same size it was at
 37406 **     the beginning of the transaction.  (In some VFSes, the xTruncate
 37407 **     method is a no-op, but that does not change the fact the SQLite will
 37408 **     invoke it.)
 37409 ** 
 37410 ** (9) Whenever the database file is modified, at least one bit in the range
 37411 **     of bytes from 24 through 39 inclusive will be changed prior to releasing
 37412 **     the EXCLUSIVE lock, thus signaling other connections on the same
 37413 **     database to flush their caches.
 37414 **
 37415 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
 37416 **      than one billion transactions.
 37417 **
 37418 ** (11) A database file is well-formed at the beginning and at the conclusion
 37419 **      of every transaction.
 37420 **
 37421 ** (12) An EXCLUSIVE lock is held on the database file when writing to
 37422 **      the database file.
 37423 **
 37424 ** (13) A SHARED lock is held on the database file while reading any
 37425 **      content out of the database file.
 37426 **
 37427 ******************************************************************************/
 37429 /*
 37430 ** Macros for troubleshooting.  Normally turned off
 37431 */
 37432 #if 0
 37433 int sqlite3PagerTrace=1;  /* True to enable tracing */
 37434 #define sqlite3DebugPrintf printf
 37435 #define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
 37436 #else
 37437 #define PAGERTRACE(X)
 37438 #endif
 37440 /*
 37441 ** The following two macros are used within the PAGERTRACE() macros above
 37442 ** to print out file-descriptors. 
 37443 **
 37444 ** PAGERID() takes a pointer to a Pager struct as its argument. The
 37445 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
 37446 ** struct as its argument.
 37447 */
 37448 #define PAGERID(p) ((int)(p->fd))
 37449 #define FILEHANDLEID(fd) ((int)fd)
 37451 /*
 37452 ** The Pager.eState variable stores the current 'state' of a pager. A
 37453 ** pager may be in any one of the seven states shown in the following
 37454 ** state diagram.
 37455 **
 37456 **                            OPEN <------+------+
 37457 **                              |         |      |
 37458 **                              V         |      |
 37459 **               +---------> READER-------+      |
 37460 **               |              |                |
 37461 **               |              V                |
 37462 **               |<-------WRITER_LOCKED------> ERROR
 37463 **               |              |                ^  
 37464 **               |              V                |
 37465 **               |<------WRITER_CACHEMOD-------->|
 37466 **               |              |                |
 37467 **               |              V                |
 37468 **               |<-------WRITER_DBMOD---------->|
 37469 **               |              |                |
 37470 **               |              V                |
 37471 **               +<------WRITER_FINISHED-------->+
 37472 **
 37473 **
 37474 ** List of state transitions and the C [function] that performs each:
 37475 ** 
 37476 **   OPEN              -> READER              [sqlite3PagerSharedLock]
 37477 **   READER            -> OPEN                [pager_unlock]
 37478 **
 37479 **   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
 37480 **   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
 37481 **   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
 37482 **   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
 37483 **   WRITER_***        -> READER              [pager_end_transaction]
 37484 **
 37485 **   WRITER_***        -> ERROR               [pager_error]
 37486 **   ERROR             -> OPEN                [pager_unlock]
 37487 ** 
 37488 **
 37489 **  OPEN:
 37490 **
 37491 **    The pager starts up in this state. Nothing is guaranteed in this
 37492 **    state - the file may or may not be locked and the database size is
 37493 **    unknown. The database may not be read or written.
 37494 **
 37495 **    * No read or write transaction is active.
 37496 **    * Any lock, or no lock at all, may be held on the database file.
 37497 **    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
 37498 **
 37499 **  READER:
 37500 **
 37501 **    In this state all the requirements for reading the database in 
 37502 **    rollback (non-WAL) mode are met. Unless the pager is (or recently
 37503 **    was) in exclusive-locking mode, a user-level read transaction is 
 37504 **    open. The database size is known in this state.
 37505 **
 37506 **    A connection running with locking_mode=normal enters this state when
 37507 **    it opens a read-transaction on the database and returns to state
 37508 **    OPEN after the read-transaction is completed. However a connection
 37509 **    running in locking_mode=exclusive (including temp databases) remains in
 37510 **    this state even after the read-transaction is closed. The only way
 37511 **    a locking_mode=exclusive connection can transition from READER to OPEN
 37512 **    is via the ERROR state (see below).
 37513 ** 
 37514 **    * A read transaction may be active (but a write-transaction cannot).
 37515 **    * A SHARED or greater lock is held on the database file.
 37516 **    * The dbSize variable may be trusted (even if a user-level read 
 37517 **      transaction is not active). The dbOrigSize and dbFileSize variables
 37518 **      may not be trusted at this point.
 37519 **    * If the database is a WAL database, then the WAL connection is open.
 37520 **    * Even if a read-transaction is not open, it is guaranteed that 
 37521 **      there is no hot-journal in the file-system.
 37522 **
 37523 **  WRITER_LOCKED:
 37524 **
 37525 **    The pager moves to this state from READER when a write-transaction
 37526 **    is first opened on the database. In WRITER_LOCKED state, all locks 
 37527 **    required to start a write-transaction are held, but no actual 
 37528 **    modifications to the cache or database have taken place.
 37529 **
 37530 **    In rollback mode, a RESERVED or (if the transaction was opened with 
 37531 **    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
 37532 **    moving to this state, but the journal file is not written to or opened 
 37533 **    to in this state. If the transaction is committed or rolled back while 
 37534 **    in WRITER_LOCKED state, all that is required is to unlock the database 
 37535 **    file.
 37536 **
 37537 **    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
 37538 **    If the connection is running with locking_mode=exclusive, an attempt
 37539 **    is made to obtain an EXCLUSIVE lock on the database file.
 37540 **
 37541 **    * A write transaction is active.
 37542 **    * If the connection is open in rollback-mode, a RESERVED or greater 
 37543 **      lock is held on the database file.
 37544 **    * If the connection is open in WAL-mode, a WAL write transaction
 37545 **      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
 37546 **      called).
 37547 **    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
 37548 **    * The contents of the pager cache have not been modified.
 37549 **    * The journal file may or may not be open.
 37550 **    * Nothing (not even the first header) has been written to the journal.
 37551 **
 37552 **  WRITER_CACHEMOD:
 37553 **
 37554 **    A pager moves from WRITER_LOCKED state to this state when a page is
 37555 **    first modified by the upper layer. In rollback mode the journal file
 37556 **    is opened (if it is not already open) and a header written to the
 37557 **    start of it. The database file on disk has not been modified.
 37558 **
 37559 **    * A write transaction is active.
 37560 **    * A RESERVED or greater lock is held on the database file.
 37561 **    * The journal file is open and the first header has been written 
 37562 **      to it, but the header has not been synced to disk.
 37563 **    * The contents of the page cache have been modified.
 37564 **
 37565 **  WRITER_DBMOD:
 37566 **
 37567 **    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
 37568 **    when it modifies the contents of the database file. WAL connections
 37569 **    never enter this state (since they do not modify the database file,
 37570 **    just the log file).
 37571 **
 37572 **    * A write transaction is active.
 37573 **    * An EXCLUSIVE or greater lock is held on the database file.
 37574 **    * The journal file is open and the first header has been written 
 37575 **      and synced to disk.
 37576 **    * The contents of the page cache have been modified (and possibly
 37577 **      written to disk).
 37578 **
 37579 **  WRITER_FINISHED:
 37580 **
 37581 **    It is not possible for a WAL connection to enter this state.
 37582 **
 37583 **    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
 37584 **    state after the entire transaction has been successfully written into the
 37585 **    database file. In this state the transaction may be committed simply
 37586 **    by finalizing the journal file. Once in WRITER_FINISHED state, it is 
 37587 **    not possible to modify the database further. At this point, the upper 
 37588 **    layer must either commit or rollback the transaction.
 37589 **
 37590 **    * A write transaction is active.
 37591 **    * An EXCLUSIVE or greater lock is held on the database file.
 37592 **    * All writing and syncing of journal and database data has finished.
 37593 **      If no error occured, all that remains is to finalize the journal to
 37594 **      commit the transaction. If an error did occur, the caller will need
 37595 **      to rollback the transaction. 
 37596 **
 37597 **  ERROR:
 37598 **
 37599 **    The ERROR state is entered when an IO or disk-full error (including
 37600 **    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it 
 37601 **    difficult to be sure that the in-memory pager state (cache contents, 
 37602 **    db size etc.) are consistent with the contents of the file-system.
 37603 **
 37604 **    Temporary pager files may enter the ERROR state, but in-memory pagers
 37605 **    cannot.
 37606 **
 37607 **    For example, if an IO error occurs while performing a rollback, 
 37608 **    the contents of the page-cache may be left in an inconsistent state.
 37609 **    At this point it would be dangerous to change back to READER state
 37610 **    (as usually happens after a rollback). Any subsequent readers might
 37611 **    report database corruption (due to the inconsistent cache), and if
 37612 **    they upgrade to writers, they may inadvertently corrupt the database
 37613 **    file. To avoid this hazard, the pager switches into the ERROR state
 37614 **    instead of READER following such an error.
 37615 **
 37616 **    Once it has entered the ERROR state, any attempt to use the pager
 37617 **    to read or write data returns an error. Eventually, once all 
 37618 **    outstanding transactions have been abandoned, the pager is able to
 37619 **    transition back to OPEN state, discarding the contents of the 
 37620 **    page-cache and any other in-memory state at the same time. Everything
 37621 **    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
 37622 **    when a read-transaction is next opened on the pager (transitioning
 37623 **    the pager into READER state). At that point the system has recovered 
 37624 **    from the error.
 37625 **
 37626 **    Specifically, the pager jumps into the ERROR state if:
 37627 **
 37628 **      1. An error occurs while attempting a rollback. This happens in
 37629 **         function sqlite3PagerRollback().
 37630 **
 37631 **      2. An error occurs while attempting to finalize a journal file
 37632 **         following a commit in function sqlite3PagerCommitPhaseTwo().
 37633 **
 37634 **      3. An error occurs while attempting to write to the journal or
 37635 **         database file in function pagerStress() in order to free up
 37636 **         memory.
 37637 **
 37638 **    In other cases, the error is returned to the b-tree layer. The b-tree
 37639 **    layer then attempts a rollback operation. If the error condition 
 37640 **    persists, the pager enters the ERROR state via condition (1) above.
 37641 **
 37642 **    Condition (3) is necessary because it can be triggered by a read-only
 37643 **    statement executed within a transaction. In this case, if the error
 37644 **    code were simply returned to the user, the b-tree layer would not
 37645 **    automatically attempt a rollback, as it assumes that an error in a
 37646 **    read-only statement cannot leave the pager in an internally inconsistent 
 37647 **    state.
 37648 **
 37649 **    * The Pager.errCode variable is set to something other than SQLITE_OK.
 37650 **    * There are one or more outstanding references to pages (after the
 37651 **      last reference is dropped the pager should move back to OPEN state).
 37652 **    * The pager is not an in-memory pager.
 37653 **    
 37654 **
 37655 ** Notes:
 37656 **
 37657 **   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
 37658 **     connection is open in WAL mode. A WAL connection is always in one
 37659 **     of the first four states.
 37660 **
 37661 **   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
 37662 **     state. There are two exceptions: immediately after exclusive-mode has
 37663 **     been turned on (and before any read or write transactions are 
 37664 **     executed), and when the pager is leaving the "error state".
 37665 **
 37666 **   * See also: assert_pager_state().
 37667 */
 37668 #define PAGER_OPEN                  0
 37669 #define PAGER_READER                1
 37670 #define PAGER_WRITER_LOCKED         2
 37671 #define PAGER_WRITER_CACHEMOD       3
 37672 #define PAGER_WRITER_DBMOD          4
 37673 #define PAGER_WRITER_FINISHED       5
 37674 #define PAGER_ERROR                 6
 37676 /*
 37677 ** The Pager.eLock variable is almost always set to one of the 
 37678 ** following locking-states, according to the lock currently held on
 37679 ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
 37680 ** This variable is kept up to date as locks are taken and released by
 37681 ** the pagerLockDb() and pagerUnlockDb() wrappers.
 37682 **
 37683 ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
 37684 ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
 37685 ** the operation was successful. In these circumstances pagerLockDb() and
 37686 ** pagerUnlockDb() take a conservative approach - eLock is always updated
 37687 ** when unlocking the file, and only updated when locking the file if the
 37688 ** VFS call is successful. This way, the Pager.eLock variable may be set
 37689 ** to a less exclusive (lower) value than the lock that is actually held
 37690 ** at the system level, but it is never set to a more exclusive value.
 37691 **
 37692 ** This is usually safe. If an xUnlock fails or appears to fail, there may 
 37693 ** be a few redundant xLock() calls or a lock may be held for longer than
 37694 ** required, but nothing really goes wrong.
 37695 **
 37696 ** The exception is when the database file is unlocked as the pager moves
 37697 ** from ERROR to OPEN state. At this point there may be a hot-journal file 
 37698 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
 37699 ** transition, by the same pager or any other). If the call to xUnlock()
 37700 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
 37701 ** can confuse the call to xCheckReservedLock() call made later as part
 37702 ** of hot-journal detection.
 37703 **
 37704 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED 
 37705 ** lock held by this process or any others". So xCheckReservedLock may 
 37706 ** return true because the caller itself is holding an EXCLUSIVE lock (but
 37707 ** doesn't know it because of a previous error in xUnlock). If this happens
 37708 ** a hot-journal may be mistaken for a journal being created by an active
 37709 ** transaction in another process, causing SQLite to read from the database
 37710 ** without rolling it back.
 37711 **
 37712 ** To work around this, if a call to xUnlock() fails when unlocking the
 37713 ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
 37714 ** is only changed back to a real locking state after a successful call
 37715 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
 37716 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK 
 37717 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
 37718 ** lock on the database file before attempting to roll it back. See function
 37719 ** PagerSharedLock() for more detail.
 37720 **
 37721 ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in 
 37722 ** PAGER_OPEN state.
 37723 */
 37724 #define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
 37726 /*
 37727 ** A macro used for invoking the codec if there is one
 37728 */
 37729 #ifdef SQLITE_HAS_CODEC
 37730 # define CODEC1(P,D,N,X,E) \
 37731     if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
 37732 # define CODEC2(P,D,N,X,E,O) \
 37733     if( P->xCodec==0 ){ O=(char*)D; }else \
 37734     if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
 37735 #else
 37736 # define CODEC1(P,D,N,X,E)   /* NO-OP */
 37737 # define CODEC2(P,D,N,X,E,O) O=(char*)D
 37738 #endif
 37740 /*
 37741 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method 
 37742 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
 37743 ** This could conceivably cause corruption following a power failure on
 37744 ** such a system. This is currently an undocumented limit.
 37745 */
 37746 #define MAX_SECTOR_SIZE 0x10000
 37748 /*
 37749 ** An instance of the following structure is allocated for each active
 37750 ** savepoint and statement transaction in the system. All such structures
 37751 ** are stored in the Pager.aSavepoint[] array, which is allocated and
 37752 ** resized using sqlite3Realloc().
 37753 **
 37754 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
 37755 ** set to 0. If a journal-header is written into the main journal while
 37756 ** the savepoint is active, then iHdrOffset is set to the byte offset 
 37757 ** immediately following the last journal record written into the main
 37758 ** journal before the journal-header. This is required during savepoint
 37759 ** rollback (see pagerPlaybackSavepoint()).
 37760 */
 37761 typedef struct PagerSavepoint PagerSavepoint;
 37762 struct PagerSavepoint {
 37763   i64 iOffset;                 /* Starting offset in main journal */
 37764   i64 iHdrOffset;              /* See above */
 37765   Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
 37766   Pgno nOrig;                  /* Original number of pages in file */
 37767   Pgno iSubRec;                /* Index of first record in sub-journal */
 37768 #ifndef SQLITE_OMIT_WAL
 37769   u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
 37770 #endif
 37771 };
 37773 /*
 37774 ** A open page cache is an instance of struct Pager. A description of
 37775 ** some of the more important member variables follows:
 37776 **
 37777 ** eState
 37778 **
 37779 **   The current 'state' of the pager object. See the comment and state
 37780 **   diagram above for a description of the pager state.
 37781 **
 37782 ** eLock
 37783 **
 37784 **   For a real on-disk database, the current lock held on the database file -
 37785 **   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
 37786 **
 37787 **   For a temporary or in-memory database (neither of which require any
 37788 **   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
 37789 **   databases always have Pager.exclusiveMode==1, this tricks the pager
 37790 **   logic into thinking that it already has all the locks it will ever
 37791 **   need (and no reason to release them).
 37792 **
 37793 **   In some (obscure) circumstances, this variable may also be set to
 37794 **   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
 37795 **   details.
 37796 **
 37797 ** changeCountDone
 37798 **
 37799 **   This boolean variable is used to make sure that the change-counter 
 37800 **   (the 4-byte header field at byte offset 24 of the database file) is 
 37801 **   not updated more often than necessary. 
 37802 **
 37803 **   It is set to true when the change-counter field is updated, which 
 37804 **   can only happen if an exclusive lock is held on the database file.
 37805 **   It is cleared (set to false) whenever an exclusive lock is 
 37806 **   relinquished on the database file. Each time a transaction is committed,
 37807 **   The changeCountDone flag is inspected. If it is true, the work of
 37808 **   updating the change-counter is omitted for the current transaction.
 37809 **
 37810 **   This mechanism means that when running in exclusive mode, a connection 
 37811 **   need only update the change-counter once, for the first transaction
 37812 **   committed.
 37813 **
 37814 ** setMaster
 37815 **
 37816 **   When PagerCommitPhaseOne() is called to commit a transaction, it may
 37817 **   (or may not) specify a master-journal name to be written into the 
 37818 **   journal file before it is synced to disk.
 37819 **
 37820 **   Whether or not a journal file contains a master-journal pointer affects 
 37821 **   the way in which the journal file is finalized after the transaction is 
 37822 **   committed or rolled back when running in "journal_mode=PERSIST" mode.
 37823 **   If a journal file does not contain a master-journal pointer, it is
 37824 **   finalized by overwriting the first journal header with zeroes. If
 37825 **   it does contain a master-journal pointer the journal file is finalized 
 37826 **   by truncating it to zero bytes, just as if the connection were 
 37827 **   running in "journal_mode=truncate" mode.
 37828 **
 37829 **   Journal files that contain master journal pointers cannot be finalized
 37830 **   simply by overwriting the first journal-header with zeroes, as the
 37831 **   master journal pointer could interfere with hot-journal rollback of any
 37832 **   subsequently interrupted transaction that reuses the journal file.
 37833 **
 37834 **   The flag is cleared as soon as the journal file is finalized (either
 37835 **   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
 37836 **   journal file from being successfully finalized, the setMaster flag
 37837 **   is cleared anyway (and the pager will move to ERROR state).
 37838 **
 37839 ** doNotSpill, doNotSyncSpill
 37840 **
 37841 **   These two boolean variables control the behaviour of cache-spills
 37842 **   (calls made by the pcache module to the pagerStress() routine to
 37843 **   write cached data to the file-system in order to free up memory).
 37844 **
 37845 **   When doNotSpill is non-zero, writing to the database from pagerStress()
 37846 **   is disabled altogether. This is done in a very obscure case that
 37847 **   comes up during savepoint rollback that requires the pcache module
 37848 **   to allocate a new page to prevent the journal file from being written
 37849 **   while it is being traversed by code in pager_playback().
 37850 ** 
 37851 **   If doNotSyncSpill is non-zero, writing to the database from pagerStress()
 37852 **   is permitted, but syncing the journal file is not. This flag is set
 37853 **   by sqlite3PagerWrite() when the file-system sector-size is larger than
 37854 **   the database page-size in order to prevent a journal sync from happening 
 37855 **   in between the journalling of two pages on the same sector. 
 37856 **
 37857 ** subjInMemory
 37858 **
 37859 **   This is a boolean variable. If true, then any required sub-journal
 37860 **   is opened as an in-memory journal file. If false, then in-memory
 37861 **   sub-journals are only used for in-memory pager files.
 37862 **
 37863 **   This variable is updated by the upper layer each time a new 
 37864 **   write-transaction is opened.
 37865 **
 37866 ** dbSize, dbOrigSize, dbFileSize
 37867 **
 37868 **   Variable dbSize is set to the number of pages in the database file.
 37869 **   It is valid in PAGER_READER and higher states (all states except for
 37870 **   OPEN and ERROR). 
 37871 **
 37872 **   dbSize is set based on the size of the database file, which may be 
 37873 **   larger than the size of the database (the value stored at offset
 37874 **   28 of the database header by the btree). If the size of the file
 37875 **   is not an integer multiple of the page-size, the value stored in
 37876 **   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
 37877 **   Except, any file that is greater than 0 bytes in size is considered
 37878 **   to have at least one page. (i.e. a 1KB file with 2K page-size leads
 37879 **   to dbSize==1).
 37880 **
 37881 **   During a write-transaction, if pages with page-numbers greater than
 37882 **   dbSize are modified in the cache, dbSize is updated accordingly.
 37883 **   Similarly, if the database is truncated using PagerTruncateImage(), 
 37884 **   dbSize is updated.
 37885 **
 37886 **   Variables dbOrigSize and dbFileSize are valid in states 
 37887 **   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
 37888 **   variable at the start of the transaction. It is used during rollback,
 37889 **   and to determine whether or not pages need to be journalled before
 37890 **   being modified.
 37891 **
 37892 **   Throughout a write-transaction, dbFileSize contains the size of
 37893 **   the file on disk in pages. It is set to a copy of dbSize when the
 37894 **   write-transaction is first opened, and updated when VFS calls are made
 37895 **   to write or truncate the database file on disk. 
 37896 **
 37897 **   The only reason the dbFileSize variable is required is to suppress 
 37898 **   unnecessary calls to xTruncate() after committing a transaction. If, 
 37899 **   when a transaction is committed, the dbFileSize variable indicates 
 37900 **   that the database file is larger than the database image (Pager.dbSize), 
 37901 **   pager_truncate() is called. The pager_truncate() call uses xFilesize()
 37902 **   to measure the database file on disk, and then truncates it if required.
 37903 **   dbFileSize is not used when rolling back a transaction. In this case
 37904 **   pager_truncate() is called unconditionally (which means there may be
 37905 **   a call to xFilesize() that is not strictly required). In either case,
 37906 **   pager_truncate() may cause the file to become smaller or larger.
 37907 **
 37908 ** dbHintSize
 37909 **
 37910 **   The dbHintSize variable is used to limit the number of calls made to
 37911 **   the VFS xFileControl(FCNTL_SIZE_HINT) method. 
 37912 **
 37913 **   dbHintSize is set to a copy of the dbSize variable when a
 37914 **   write-transaction is opened (at the same time as dbFileSize and
 37915 **   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
 37916 **   dbHintSize is increased to the number of pages that correspond to the
 37917 **   size-hint passed to the method call. See pager_write_pagelist() for 
 37918 **   details.
 37919 **
 37920 ** errCode
 37921 **
 37922 **   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
 37923 **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 
 37924 **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 
 37925 **   sub-codes.
 37926 */
 37927 struct Pager {
 37928   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
 37929   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
 37930   u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
 37931   u8 useJournal;              /* Use a rollback journal on this file */
 37932   u8 noSync;                  /* Do not sync the journal if true */
 37933   u8 fullSync;                /* Do extra syncs of the journal for robustness */
 37934   u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
 37935   u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
 37936   u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
 37937   u8 tempFile;                /* zFilename is a temporary file */
 37938   u8 readOnly;                /* True for a read-only database */
 37939   u8 memDb;                   /* True to inhibit all file I/O */
 37941   /**************************************************************************
 37942   ** The following block contains those class members that change during
 37943   ** routine opertion.  Class members not in this block are either fixed
 37944   ** when the pager is first created or else only change when there is a
 37945   ** significant mode change (such as changing the page_size, locking_mode,
 37946   ** or the journal_mode).  From another view, these class members describe
 37947   ** the "state" of the pager, while other class members describe the
 37948   ** "configuration" of the pager.
 37949   */
 37950   u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
 37951   u8 eLock;                   /* Current lock held on database file */
 37952   u8 changeCountDone;         /* Set after incrementing the change-counter */
 37953   u8 setMaster;               /* True if a m-j name has been written to jrnl */
 37954   u8 doNotSpill;              /* Do not spill the cache when non-zero */
 37955   u8 doNotSyncSpill;          /* Do not do a spill that requires jrnl sync */
 37956   u8 subjInMemory;            /* True to use in-memory sub-journals */
 37957   Pgno dbSize;                /* Number of pages in the database */
 37958   Pgno dbOrigSize;            /* dbSize before the current transaction */
 37959   Pgno dbFileSize;            /* Number of pages in the database file */
 37960   Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
 37961   int errCode;                /* One of several kinds of errors */
 37962   int nRec;                   /* Pages journalled since last j-header written */
 37963   u32 cksumInit;              /* Quasi-random value added to every checksum */
 37964   u32 nSubRec;                /* Number of records written to sub-journal */
 37965   Bitvec *pInJournal;         /* One bit for each page in the database file */
 37966   sqlite3_file *fd;           /* File descriptor for database */
 37967   sqlite3_file *jfd;          /* File descriptor for main journal */
 37968   sqlite3_file *sjfd;         /* File descriptor for sub-journal */
 37969   i64 journalOff;             /* Current write offset in the journal file */
 37970   i64 journalHdr;             /* Byte offset to previous journal header */
 37971   sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
 37972   PagerSavepoint *aSavepoint; /* Array of active savepoints */
 37973   int nSavepoint;             /* Number of elements in aSavepoint[] */
 37974   char dbFileVers[16];        /* Changes whenever database file changes */
 37975   /*
 37976   ** End of the routinely-changing class members
 37977   ***************************************************************************/
 37979   u16 nExtra;                 /* Add this many bytes to each in-memory page */
 37980   i16 nReserve;               /* Number of unused bytes at end of each page */
 37981   u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
 37982   u32 sectorSize;             /* Assumed sector size during rollback */
 37983   int pageSize;               /* Number of bytes in a page */
 37984   Pgno mxPgno;                /* Maximum allowed size of the database */
 37985   i64 journalSizeLimit;       /* Size limit for persistent journal files */
 37986   char *zFilename;            /* Name of the database file */
 37987   char *zJournal;             /* Name of the journal file */
 37988   int (*xBusyHandler)(void*); /* Function to call when busy */
 37989   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 37990   int aStat[3];               /* Total cache hits, misses and writes */
 37991 #ifdef SQLITE_TEST
 37992   int nRead;                  /* Database pages read */
 37993 #endif
 37994   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 37995 #ifdef SQLITE_HAS_CODEC
 37996   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
 37997   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
 37998   void (*xCodecFree)(void*);             /* Destructor for the codec */
 37999   void *pCodec;               /* First argument to xCodec... methods */
 38000 #endif
 38001   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
 38002   PCache *pPCache;            /* Pointer to page cache object */
 38003 #ifndef SQLITE_OMIT_WAL
 38004   Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
 38005   char *zWal;                 /* File name for write-ahead log */
 38006 #endif
 38007 };
 38009 /*
 38010 ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 38011 ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 38012 ** or CACHE_WRITE to sqlite3_db_status().
 38013 */
 38014 #define PAGER_STAT_HIT   0
 38015 #define PAGER_STAT_MISS  1
 38016 #define PAGER_STAT_WRITE 2
 38018 /*
 38019 ** The following global variables hold counters used for
 38020 ** testing purposes only.  These variables do not exist in
 38021 ** a non-testing build.  These variables are not thread-safe.
 38022 */
 38023 #ifdef SQLITE_TEST
 38024 SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
 38025 SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
 38026 SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
 38027 # define PAGER_INCR(v)  v++
 38028 #else
 38029 # define PAGER_INCR(v)
 38030 #endif
 38034 /*
 38035 ** Journal files begin with the following magic string.  The data
 38036 ** was obtained from /dev/random.  It is used only as a sanity check.
 38037 **
 38038 ** Since version 2.8.0, the journal format contains additional sanity
 38039 ** checking information.  If the power fails while the journal is being
 38040 ** written, semi-random garbage data might appear in the journal
 38041 ** file after power is restored.  If an attempt is then made
 38042 ** to roll the journal back, the database could be corrupted.  The additional
 38043 ** sanity checking data is an attempt to discover the garbage in the
 38044 ** journal and ignore it.
 38045 **
 38046 ** The sanity checking information for the new journal format consists
 38047 ** of a 32-bit checksum on each page of data.  The checksum covers both
 38048 ** the page number and the pPager->pageSize bytes of data for the page.
 38049 ** This cksum is initialized to a 32-bit random value that appears in the
 38050 ** journal file right after the header.  The random initializer is important,
 38051 ** because garbage data that appears at the end of a journal is likely
 38052 ** data that was once in other files that have now been deleted.  If the
 38053 ** garbage data came from an obsolete journal file, the checksums might
 38054 ** be correct.  But by initializing the checksum to random value which
 38055 ** is different for every journal, we minimize that risk.
 38056 */
 38057 static const unsigned char aJournalMagic[] = {
 38058   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
 38059 };
 38061 /*
 38062 ** The size of the of each page record in the journal is given by
 38063 ** the following macro.
 38064 */
 38065 #define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
 38067 /*
 38068 ** The journal header size for this pager. This is usually the same 
 38069 ** size as a single disk sector. See also setSectorSize().
 38070 */
 38071 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
 38073 /*
 38074 ** The macro MEMDB is true if we are dealing with an in-memory database.
 38075 ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
 38076 ** the value of MEMDB will be a constant and the compiler will optimize
 38077 ** out code that would never execute.
 38078 */
 38079 #ifdef SQLITE_OMIT_MEMORYDB
 38080 # define MEMDB 0
 38081 #else
 38082 # define MEMDB pPager->memDb
 38083 #endif
 38085 /*
 38086 ** The maximum legal page number is (2^31 - 1).
 38087 */
 38088 #define PAGER_MAX_PGNO 2147483647
 38090 /*
 38091 ** The argument to this macro is a file descriptor (type sqlite3_file*).
 38092 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
 38093 **
 38094 ** This is so that expressions can be written as:
 38095 **
 38096 **   if( isOpen(pPager->jfd) ){ ...
 38097 **
 38098 ** instead of
 38099 **
 38100 **   if( pPager->jfd->pMethods ){ ...
 38101 */
 38102 #define isOpen(pFd) ((pFd)->pMethods)
 38104 /*
 38105 ** Return true if this pager uses a write-ahead log instead of the usual
 38106 ** rollback journal. Otherwise false.
 38107 */
 38108 #ifndef SQLITE_OMIT_WAL
 38109 static int pagerUseWal(Pager *pPager){
 38110   return (pPager->pWal!=0);
 38112 #else
 38113 # define pagerUseWal(x) 0
 38114 # define pagerRollbackWal(x) 0
 38115 # define pagerWalFrames(v,w,x,y) 0
 38116 # define pagerOpenWalIfPresent(z) SQLITE_OK
 38117 # define pagerBeginReadTransaction(z) SQLITE_OK
 38118 #endif
 38120 #ifndef NDEBUG 
 38121 /*
 38122 ** Usage:
 38123 **
 38124 **   assert( assert_pager_state(pPager) );
 38125 **
 38126 ** This function runs many asserts to try to find inconsistencies in
 38127 ** the internal state of the Pager object.
 38128 */
 38129 static int assert_pager_state(Pager *p){
 38130   Pager *pPager = p;
 38132   /* State must be valid. */
 38133   assert( p->eState==PAGER_OPEN
 38134        || p->eState==PAGER_READER
 38135        || p->eState==PAGER_WRITER_LOCKED
 38136        || p->eState==PAGER_WRITER_CACHEMOD
 38137        || p->eState==PAGER_WRITER_DBMOD
 38138        || p->eState==PAGER_WRITER_FINISHED
 38139        || p->eState==PAGER_ERROR
 38140   );
 38142   /* Regardless of the current state, a temp-file connection always behaves
 38143   ** as if it has an exclusive lock on the database file. It never updates
 38144   ** the change-counter field, so the changeCountDone flag is always set.
 38145   */
 38146   assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
 38147   assert( p->tempFile==0 || pPager->changeCountDone );
 38149   /* If the useJournal flag is clear, the journal-mode must be "OFF". 
 38150   ** And if the journal-mode is "OFF", the journal file must not be open.
 38151   */
 38152   assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
 38153   assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
 38155   /* Check that MEMDB implies noSync. And an in-memory journal. Since 
 38156   ** this means an in-memory pager performs no IO at all, it cannot encounter 
 38157   ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing 
 38158   ** a journal file. (although the in-memory journal implementation may 
 38159   ** return SQLITE_IOERR_NOMEM while the journal file is being written). It 
 38160   ** is therefore not possible for an in-memory pager to enter the ERROR 
 38161   ** state.
 38162   */
 38163   if( MEMDB ){
 38164     assert( p->noSync );
 38165     assert( p->journalMode==PAGER_JOURNALMODE_OFF 
 38166          || p->journalMode==PAGER_JOURNALMODE_MEMORY 
 38167     );
 38168     assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
 38169     assert( pagerUseWal(p)==0 );
 38172   /* If changeCountDone is set, a RESERVED lock or greater must be held
 38173   ** on the file.
 38174   */
 38175   assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
 38176   assert( p->eLock!=PENDING_LOCK );
 38178   switch( p->eState ){
 38179     case PAGER_OPEN:
 38180       assert( !MEMDB );
 38181       assert( pPager->errCode==SQLITE_OK );
 38182       assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
 38183       break;
 38185     case PAGER_READER:
 38186       assert( pPager->errCode==SQLITE_OK );
 38187       assert( p->eLock!=UNKNOWN_LOCK );
 38188       assert( p->eLock>=SHARED_LOCK );
 38189       break;
 38191     case PAGER_WRITER_LOCKED:
 38192       assert( p->eLock!=UNKNOWN_LOCK );
 38193       assert( pPager->errCode==SQLITE_OK );
 38194       if( !pagerUseWal(pPager) ){
 38195         assert( p->eLock>=RESERVED_LOCK );
 38197       assert( pPager->dbSize==pPager->dbOrigSize );
 38198       assert( pPager->dbOrigSize==pPager->dbFileSize );
 38199       assert( pPager->dbOrigSize==pPager->dbHintSize );
 38200       assert( pPager->setMaster==0 );
 38201       break;
 38203     case PAGER_WRITER_CACHEMOD:
 38204       assert( p->eLock!=UNKNOWN_LOCK );
 38205       assert( pPager->errCode==SQLITE_OK );
 38206       if( !pagerUseWal(pPager) ){
 38207         /* It is possible that if journal_mode=wal here that neither the
 38208         ** journal file nor the WAL file are open. This happens during
 38209         ** a rollback transaction that switches from journal_mode=off
 38210         ** to journal_mode=wal.
 38211         */
 38212         assert( p->eLock>=RESERVED_LOCK );
 38213         assert( isOpen(p->jfd) 
 38214              || p->journalMode==PAGER_JOURNALMODE_OFF 
 38215              || p->journalMode==PAGER_JOURNALMODE_WAL 
 38216         );
 38218       assert( pPager->dbOrigSize==pPager->dbFileSize );
 38219       assert( pPager->dbOrigSize==pPager->dbHintSize );
 38220       break;
 38222     case PAGER_WRITER_DBMOD:
 38223       assert( p->eLock==EXCLUSIVE_LOCK );
 38224       assert( pPager->errCode==SQLITE_OK );
 38225       assert( !pagerUseWal(pPager) );
 38226       assert( p->eLock>=EXCLUSIVE_LOCK );
 38227       assert( isOpen(p->jfd) 
 38228            || p->journalMode==PAGER_JOURNALMODE_OFF 
 38229            || p->journalMode==PAGER_JOURNALMODE_WAL 
 38230       );
 38231       assert( pPager->dbOrigSize<=pPager->dbHintSize );
 38232       break;
 38234     case PAGER_WRITER_FINISHED:
 38235       assert( p->eLock==EXCLUSIVE_LOCK );
 38236       assert( pPager->errCode==SQLITE_OK );
 38237       assert( !pagerUseWal(pPager) );
 38238       assert( isOpen(p->jfd) 
 38239            || p->journalMode==PAGER_JOURNALMODE_OFF 
 38240            || p->journalMode==PAGER_JOURNALMODE_WAL 
 38241       );
 38242       break;
 38244     case PAGER_ERROR:
 38245       /* There must be at least one outstanding reference to the pager if
 38246       ** in ERROR state. Otherwise the pager should have already dropped
 38247       ** back to OPEN state.
 38248       */
 38249       assert( pPager->errCode!=SQLITE_OK );
 38250       assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
 38251       break;
 38254   return 1;
 38256 #endif /* ifndef NDEBUG */
 38258 #ifdef SQLITE_DEBUG 
 38259 /*
 38260 ** Return a pointer to a human readable string in a static buffer
 38261 ** containing the state of the Pager object passed as an argument. This
 38262 ** is intended to be used within debuggers. For example, as an alternative
 38263 ** to "print *pPager" in gdb:
 38264 **
 38265 ** (gdb) printf "%s", print_pager_state(pPager)
 38266 */
 38267 static char *print_pager_state(Pager *p){
 38268   static char zRet[1024];
 38270   sqlite3_snprintf(1024, zRet,
 38271       "Filename:      %s\n"
 38272       "State:         %s errCode=%d\n"
 38273       "Lock:          %s\n"
 38274       "Locking mode:  locking_mode=%s\n"
 38275       "Journal mode:  journal_mode=%s\n"
 38276       "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
 38277       "Journal:       journalOff=%lld journalHdr=%lld\n"
 38278       "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
 38279       , p->zFilename
 38280       , p->eState==PAGER_OPEN            ? "OPEN" :
 38281         p->eState==PAGER_READER          ? "READER" :
 38282         p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
 38283         p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
 38284         p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
 38285         p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
 38286         p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
 38287       , (int)p->errCode
 38288       , p->eLock==NO_LOCK         ? "NO_LOCK" :
 38289         p->eLock==RESERVED_LOCK   ? "RESERVED" :
 38290         p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
 38291         p->eLock==SHARED_LOCK     ? "SHARED" :
 38292         p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
 38293       , p->exclusiveMode ? "exclusive" : "normal"
 38294       , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
 38295         p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
 38296         p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
 38297         p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
 38298         p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
 38299         p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
 38300       , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
 38301       , p->journalOff, p->journalHdr
 38302       , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
 38303   );
 38305   return zRet;
 38307 #endif
 38309 /*
 38310 ** Return true if it is necessary to write page *pPg into the sub-journal.
 38311 ** A page needs to be written into the sub-journal if there exists one
 38312 ** or more open savepoints for which:
 38313 **
 38314 **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
 38315 **   * The bit corresponding to the page-number is not set in
 38316 **     PagerSavepoint.pInSavepoint.
 38317 */
 38318 static int subjRequiresPage(PgHdr *pPg){
 38319   Pgno pgno = pPg->pgno;
 38320   Pager *pPager = pPg->pPager;
 38321   int i;
 38322   for(i=0; i<pPager->nSavepoint; i++){
 38323     PagerSavepoint *p = &pPager->aSavepoint[i];
 38324     if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
 38325       return 1;
 38328   return 0;
 38331 /*
 38332 ** Return true if the page is already in the journal file.
 38333 */
 38334 static int pageInJournal(PgHdr *pPg){
 38335   return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
 38338 /*
 38339 ** Read a 32-bit integer from the given file descriptor.  Store the integer
 38340 ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
 38341 ** error code is something goes wrong.
 38342 **
 38343 ** All values are stored on disk as big-endian.
 38344 */
 38345 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
 38346   unsigned char ac[4];
 38347   int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
 38348   if( rc==SQLITE_OK ){
 38349     *pRes = sqlite3Get4byte(ac);
 38351   return rc;
 38354 /*
 38355 ** Write a 32-bit integer into a string buffer in big-endian byte order.
 38356 */
 38357 #define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
 38360 /*
 38361 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
 38362 ** on success or an error code is something goes wrong.
 38363 */
 38364 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
 38365   char ac[4];
 38366   put32bits(ac, val);
 38367   return sqlite3OsWrite(fd, ac, 4, offset);
 38370 /*
 38371 ** Unlock the database file to level eLock, which must be either NO_LOCK
 38372 ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
 38373 ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
 38374 **
 38375 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
 38376 ** called, do not modify it. See the comment above the #define of 
 38377 ** UNKNOWN_LOCK for an explanation of this.
 38378 */
 38379 static int pagerUnlockDb(Pager *pPager, int eLock){
 38380   int rc = SQLITE_OK;
 38382   assert( !pPager->exclusiveMode || pPager->eLock==eLock );
 38383   assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
 38384   assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
 38385   if( isOpen(pPager->fd) ){
 38386     assert( pPager->eLock>=eLock );
 38387     rc = sqlite3OsUnlock(pPager->fd, eLock);
 38388     if( pPager->eLock!=UNKNOWN_LOCK ){
 38389       pPager->eLock = (u8)eLock;
 38391     IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
 38393   return rc;
 38396 /*
 38397 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
 38398 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
 38399 ** Pager.eLock variable to the new locking state. 
 38400 **
 38401 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is 
 38402 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. 
 38403 ** See the comment above the #define of UNKNOWN_LOCK for an explanation 
 38404 ** of this.
 38405 */
 38406 static int pagerLockDb(Pager *pPager, int eLock){
 38407   int rc = SQLITE_OK;
 38409   assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
 38410   if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
 38411     rc = sqlite3OsLock(pPager->fd, eLock);
 38412     if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
 38413       pPager->eLock = (u8)eLock;
 38414       IOTRACE(("LOCK %p %d\n", pPager, eLock))
 38417   return rc;
 38420 /*
 38421 ** This function determines whether or not the atomic-write optimization
 38422 ** can be used with this pager. The optimization can be used if:
 38423 **
 38424 **  (a) the value returned by OsDeviceCharacteristics() indicates that
 38425 **      a database page may be written atomically, and
 38426 **  (b) the value returned by OsSectorSize() is less than or equal
 38427 **      to the page size.
 38428 **
 38429 ** The optimization is also always enabled for temporary files. It is
 38430 ** an error to call this function if pPager is opened on an in-memory
 38431 ** database.
 38432 **
 38433 ** If the optimization cannot be used, 0 is returned. If it can be used,
 38434 ** then the value returned is the size of the journal file when it
 38435 ** contains rollback data for exactly one page.
 38436 */
 38437 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 38438 static int jrnlBufferSize(Pager *pPager){
 38439   assert( !MEMDB );
 38440   if( !pPager->tempFile ){
 38441     int dc;                           /* Device characteristics */
 38442     int nSector;                      /* Sector size */
 38443     int szPage;                       /* Page size */
 38445     assert( isOpen(pPager->fd) );
 38446     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 38447     nSector = pPager->sectorSize;
 38448     szPage = pPager->pageSize;
 38450     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 38451     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 38452     if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
 38453       return 0;
 38457   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
 38459 #endif
 38461 /*
 38462 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
 38463 ** on the cache using a hash function.  This is used for testing
 38464 ** and debugging only.
 38465 */
 38466 #ifdef SQLITE_CHECK_PAGES
 38467 /*
 38468 ** Return a 32-bit hash of the page data for pPage.
 38469 */
 38470 static u32 pager_datahash(int nByte, unsigned char *pData){
 38471   u32 hash = 0;
 38472   int i;
 38473   for(i=0; i<nByte; i++){
 38474     hash = (hash*1039) + pData[i];
 38476   return hash;
 38478 static u32 pager_pagehash(PgHdr *pPage){
 38479   return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
 38481 static void pager_set_pagehash(PgHdr *pPage){
 38482   pPage->pageHash = pager_pagehash(pPage);
 38485 /*
 38486 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
 38487 ** is defined, and NDEBUG is not defined, an assert() statement checks
 38488 ** that the page is either dirty or still matches the calculated page-hash.
 38489 */
 38490 #define CHECK_PAGE(x) checkPage(x)
 38491 static void checkPage(PgHdr *pPg){
 38492   Pager *pPager = pPg->pPager;
 38493   assert( pPager->eState!=PAGER_ERROR );
 38494   assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
 38497 #else
 38498 #define pager_datahash(X,Y)  0
 38499 #define pager_pagehash(X)  0
 38500 #define pager_set_pagehash(X)
 38501 #define CHECK_PAGE(x)
 38502 #endif  /* SQLITE_CHECK_PAGES */
 38504 /*
 38505 ** When this is called the journal file for pager pPager must be open.
 38506 ** This function attempts to read a master journal file name from the 
 38507 ** end of the file and, if successful, copies it into memory supplied 
 38508 ** by the caller. See comments above writeMasterJournal() for the format
 38509 ** used to store a master journal file name at the end of a journal file.
 38510 **
 38511 ** zMaster must point to a buffer of at least nMaster bytes allocated by
 38512 ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
 38513 ** enough space to write the master journal name). If the master journal
 38514 ** name in the journal is longer than nMaster bytes (including a
 38515 ** nul-terminator), then this is handled as if no master journal name
 38516 ** were present in the journal.
 38517 **
 38518 ** If a master journal file name is present at the end of the journal
 38519 ** file, then it is copied into the buffer pointed to by zMaster. A
 38520 ** nul-terminator byte is appended to the buffer following the master
 38521 ** journal file name.
 38522 **
 38523 ** If it is determined that no master journal file name is present 
 38524 ** zMaster[0] is set to 0 and SQLITE_OK returned.
 38525 **
 38526 ** If an error occurs while reading from the journal file, an SQLite
 38527 ** error code is returned.
 38528 */
 38529 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
 38530   int rc;                    /* Return code */
 38531   u32 len;                   /* Length in bytes of master journal name */
 38532   i64 szJ;                   /* Total size in bytes of journal file pJrnl */
 38533   u32 cksum;                 /* MJ checksum value read from journal */
 38534   u32 u;                     /* Unsigned loop counter */
 38535   unsigned char aMagic[8];   /* A buffer to hold the magic header */
 38536   zMaster[0] = '\0';
 38538   if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
 38539    || szJ<16
 38540    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
 38541    || len>=nMaster 
 38542    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
 38543    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
 38544    || memcmp(aMagic, aJournalMagic, 8)
 38545    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
 38546   ){
 38547     return rc;
 38550   /* See if the checksum matches the master journal name */
 38551   for(u=0; u<len; u++){
 38552     cksum -= zMaster[u];
 38554   if( cksum ){
 38555     /* If the checksum doesn't add up, then one or more of the disk sectors
 38556     ** containing the master journal filename is corrupted. This means
 38557     ** definitely roll back, so just return SQLITE_OK and report a (nul)
 38558     ** master-journal filename.
 38559     */
 38560     len = 0;
 38562   zMaster[len] = '\0';
 38564   return SQLITE_OK;
 38567 /*
 38568 ** Return the offset of the sector boundary at or immediately 
 38569 ** following the value in pPager->journalOff, assuming a sector 
 38570 ** size of pPager->sectorSize bytes.
 38571 **
 38572 ** i.e for a sector size of 512:
 38573 **
 38574 **   Pager.journalOff          Return value
 38575 **   ---------------------------------------
 38576 **   0                         0
 38577 **   512                       512
 38578 **   100                       512
 38579 **   2000                      2048
 38580 ** 
 38581 */
 38582 static i64 journalHdrOffset(Pager *pPager){
 38583   i64 offset = 0;
 38584   i64 c = pPager->journalOff;
 38585   if( c ){
 38586     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
 38588   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
 38589   assert( offset>=c );
 38590   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
 38591   return offset;
 38594 /*
 38595 ** The journal file must be open when this function is called.
 38596 **
 38597 ** This function is a no-op if the journal file has not been written to
 38598 ** within the current transaction (i.e. if Pager.journalOff==0).
 38599 **
 38600 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
 38601 ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
 38602 ** zero the 28-byte header at the start of the journal file. In either case, 
 38603 ** if the pager is not in no-sync mode, sync the journal file immediately 
 38604 ** after writing or truncating it.
 38605 **
 38606 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
 38607 ** following the truncation or zeroing described above the size of the 
 38608 ** journal file in bytes is larger than this value, then truncate the
 38609 ** journal file to Pager.journalSizeLimit bytes. The journal file does
 38610 ** not need to be synced following this operation.
 38611 **
 38612 ** If an IO error occurs, abandon processing and return the IO error code.
 38613 ** Otherwise, return SQLITE_OK.
 38614 */
 38615 static int zeroJournalHdr(Pager *pPager, int doTruncate){
 38616   int rc = SQLITE_OK;                               /* Return code */
 38617   assert( isOpen(pPager->jfd) );
 38618   if( pPager->journalOff ){
 38619     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
 38621     IOTRACE(("JZEROHDR %p\n", pPager))
 38622     if( doTruncate || iLimit==0 ){
 38623       rc = sqlite3OsTruncate(pPager->jfd, 0);
 38624     }else{
 38625       static const char zeroHdr[28] = {0};
 38626       rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
 38628     if( rc==SQLITE_OK && !pPager->noSync ){
 38629       rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
 38632     /* At this point the transaction is committed but the write lock 
 38633     ** is still held on the file. If there is a size limit configured for 
 38634     ** the persistent journal and the journal file currently consumes more
 38635     ** space than that limit allows for, truncate it now. There is no need
 38636     ** to sync the file following this operation.
 38637     */
 38638     if( rc==SQLITE_OK && iLimit>0 ){
 38639       i64 sz;
 38640       rc = sqlite3OsFileSize(pPager->jfd, &sz);
 38641       if( rc==SQLITE_OK && sz>iLimit ){
 38642         rc = sqlite3OsTruncate(pPager->jfd, iLimit);
 38646   return rc;
 38649 /*
 38650 ** The journal file must be open when this routine is called. A journal
 38651 ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
 38652 ** current location.
 38653 **
 38654 ** The format for the journal header is as follows:
 38655 ** - 8 bytes: Magic identifying journal format.
 38656 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
 38657 ** - 4 bytes: Random number used for page hash.
 38658 ** - 4 bytes: Initial database page count.
 38659 ** - 4 bytes: Sector size used by the process that wrote this journal.
 38660 ** - 4 bytes: Database page size.
 38661 ** 
 38662 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
 38663 */
 38664 static int writeJournalHdr(Pager *pPager){
 38665   int rc = SQLITE_OK;                 /* Return code */
 38666   char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
 38667   u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
 38668   u32 nWrite;                         /* Bytes of header sector written */
 38669   int ii;                             /* Loop counter */
 38671   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
 38673   if( nHeader>JOURNAL_HDR_SZ(pPager) ){
 38674     nHeader = JOURNAL_HDR_SZ(pPager);
 38677   /* If there are active savepoints and any of them were created 
 38678   ** since the most recent journal header was written, update the 
 38679   ** PagerSavepoint.iHdrOffset fields now.
 38680   */
 38681   for(ii=0; ii<pPager->nSavepoint; ii++){
 38682     if( pPager->aSavepoint[ii].iHdrOffset==0 ){
 38683       pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
 38687   pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
 38689   /* 
 38690   ** Write the nRec Field - the number of page records that follow this
 38691   ** journal header. Normally, zero is written to this value at this time.
 38692   ** After the records are added to the journal (and the journal synced, 
 38693   ** if in full-sync mode), the zero is overwritten with the true number
 38694   ** of records (see syncJournal()).
 38695   **
 38696   ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
 38697   ** reading the journal this value tells SQLite to assume that the
 38698   ** rest of the journal file contains valid page records. This assumption
 38699   ** is dangerous, as if a failure occurred whilst writing to the journal
 38700   ** file it may contain some garbage data. There are two scenarios
 38701   ** where this risk can be ignored:
 38702   **
 38703   **   * When the pager is in no-sync mode. Corruption can follow a
 38704   **     power failure in this case anyway.
 38705   **
 38706   **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
 38707   **     that garbage data is never appended to the journal file.
 38708   */
 38709   assert( isOpen(pPager->fd) || pPager->noSync );
 38710   if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
 38711    || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
 38712   ){
 38713     memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
 38714     put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
 38715   }else{
 38716     memset(zHeader, 0, sizeof(aJournalMagic)+4);
 38719   /* The random check-hash initialiser */ 
 38720   sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
 38721   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
 38722   /* The initial database size */
 38723   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
 38724   /* The assumed sector size for this process */
 38725   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
 38727   /* The page size */
 38728   put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
 38730   /* Initializing the tail of the buffer is not necessary.  Everything
 38731   ** works find if the following memset() is omitted.  But initializing
 38732   ** the memory prevents valgrind from complaining, so we are willing to
 38733   ** take the performance hit.
 38734   */
 38735   memset(&zHeader[sizeof(aJournalMagic)+20], 0,
 38736          nHeader-(sizeof(aJournalMagic)+20));
 38738   /* In theory, it is only necessary to write the 28 bytes that the 
 38739   ** journal header consumes to the journal file here. Then increment the 
 38740   ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next 
 38741   ** record is written to the following sector (leaving a gap in the file
 38742   ** that will be implicitly filled in by the OS).
 38743   **
 38744   ** However it has been discovered that on some systems this pattern can 
 38745   ** be significantly slower than contiguously writing data to the file,
 38746   ** even if that means explicitly writing data to the block of 
 38747   ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
 38748   ** is done. 
 38749   **
 38750   ** The loop is required here in case the sector-size is larger than the 
 38751   ** database page size. Since the zHeader buffer is only Pager.pageSize
 38752   ** bytes in size, more than one call to sqlite3OsWrite() may be required
 38753   ** to populate the entire journal header sector.
 38754   */ 
 38755   for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
 38756     IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
 38757     rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
 38758     assert( pPager->journalHdr <= pPager->journalOff );
 38759     pPager->journalOff += nHeader;
 38762   return rc;
 38765 /*
 38766 ** The journal file must be open when this is called. A journal header file
 38767 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
 38768 ** file. The current location in the journal file is given by
 38769 ** pPager->journalOff. See comments above function writeJournalHdr() for
 38770 ** a description of the journal header format.
 38771 **
 38772 ** If the header is read successfully, *pNRec is set to the number of
 38773 ** page records following this header and *pDbSize is set to the size of the
 38774 ** database before the transaction began, in pages. Also, pPager->cksumInit
 38775 ** is set to the value read from the journal header. SQLITE_OK is returned
 38776 ** in this case.
 38777 **
 38778 ** If the journal header file appears to be corrupted, SQLITE_DONE is
 38779 ** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
 38780 ** cannot be read from the journal file an error code is returned.
 38781 */
 38782 static int readJournalHdr(
 38783   Pager *pPager,               /* Pager object */
 38784   int isHot,
 38785   i64 journalSize,             /* Size of the open journal file in bytes */
 38786   u32 *pNRec,                  /* OUT: Value read from the nRec field */
 38787   u32 *pDbSize                 /* OUT: Value of original database size field */
 38788 ){
 38789   int rc;                      /* Return code */
 38790   unsigned char aMagic[8];     /* A buffer to hold the magic header */
 38791   i64 iHdrOff;                 /* Offset of journal header being read */
 38793   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
 38795   /* Advance Pager.journalOff to the start of the next sector. If the
 38796   ** journal file is too small for there to be a header stored at this
 38797   ** point, return SQLITE_DONE.
 38798   */
 38799   pPager->journalOff = journalHdrOffset(pPager);
 38800   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
 38801     return SQLITE_DONE;
 38803   iHdrOff = pPager->journalOff;
 38805   /* Read in the first 8 bytes of the journal header. If they do not match
 38806   ** the  magic string found at the start of each journal header, return
 38807   ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
 38808   ** proceed.
 38809   */
 38810   if( isHot || iHdrOff!=pPager->journalHdr ){
 38811     rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
 38812     if( rc ){
 38813       return rc;
 38815     if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
 38816       return SQLITE_DONE;
 38820   /* Read the first three 32-bit fields of the journal header: The nRec
 38821   ** field, the checksum-initializer and the database size at the start
 38822   ** of the transaction. Return an error code if anything goes wrong.
 38823   */
 38824   if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
 38825    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
 38826    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
 38827   ){
 38828     return rc;
 38831   if( pPager->journalOff==0 ){
 38832     u32 iPageSize;               /* Page-size field of journal header */
 38833     u32 iSectorSize;             /* Sector-size field of journal header */
 38835     /* Read the page-size and sector-size journal header fields. */
 38836     if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
 38837      || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
 38838     ){
 38839       return rc;
 38842     /* Versions of SQLite prior to 3.5.8 set the page-size field of the
 38843     ** journal header to zero. In this case, assume that the Pager.pageSize
 38844     ** variable is already set to the correct page size.
 38845     */
 38846     if( iPageSize==0 ){
 38847       iPageSize = pPager->pageSize;
 38850     /* Check that the values read from the page-size and sector-size fields
 38851     ** are within range. To be 'in range', both values need to be a power
 38852     ** of two greater than or equal to 512 or 32, and not greater than their 
 38853     ** respective compile time maximum limits.
 38854     */
 38855     if( iPageSize<512                  || iSectorSize<32
 38856      || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
 38857      || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0 
 38858     ){
 38859       /* If the either the page-size or sector-size in the journal-header is 
 38860       ** invalid, then the process that wrote the journal-header must have 
 38861       ** crashed before the header was synced. In this case stop reading 
 38862       ** the journal file here.
 38863       */
 38864       return SQLITE_DONE;
 38867     /* Update the page-size to match the value read from the journal. 
 38868     ** Use a testcase() macro to make sure that malloc failure within 
 38869     ** PagerSetPagesize() is tested.
 38870     */
 38871     rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
 38872     testcase( rc!=SQLITE_OK );
 38874     /* Update the assumed sector-size to match the value used by 
 38875     ** the process that created this journal. If this journal was
 38876     ** created by a process other than this one, then this routine
 38877     ** is being called from within pager_playback(). The local value
 38878     ** of Pager.sectorSize is restored at the end of that routine.
 38879     */
 38880     pPager->sectorSize = iSectorSize;
 38883   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
 38884   return rc;
 38888 /*
 38889 ** Write the supplied master journal name into the journal file for pager
 38890 ** pPager at the current location. The master journal name must be the last
 38891 ** thing written to a journal file. If the pager is in full-sync mode, the
 38892 ** journal file descriptor is advanced to the next sector boundary before
 38893 ** anything is written. The format is:
 38894 **
 38895 **   + 4 bytes: PAGER_MJ_PGNO.
 38896 **   + N bytes: Master journal filename in utf-8.
 38897 **   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
 38898 **   + 4 bytes: Master journal name checksum.
 38899 **   + 8 bytes: aJournalMagic[].
 38900 **
 38901 ** The master journal page checksum is the sum of the bytes in the master
 38902 ** journal name, where each byte is interpreted as a signed 8-bit integer.
 38903 **
 38904 ** If zMaster is a NULL pointer (occurs for a single database transaction), 
 38905 ** this call is a no-op.
 38906 */
 38907 static int writeMasterJournal(Pager *pPager, const char *zMaster){
 38908   int rc;                          /* Return code */
 38909   int nMaster;                     /* Length of string zMaster */
 38910   i64 iHdrOff;                     /* Offset of header in journal file */
 38911   i64 jrnlSize;                    /* Size of journal file on disk */
 38912   u32 cksum = 0;                   /* Checksum of string zMaster */
 38914   assert( pPager->setMaster==0 );
 38915   assert( !pagerUseWal(pPager) );
 38917   if( !zMaster 
 38918    || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
 38919    || pPager->journalMode==PAGER_JOURNALMODE_OFF 
 38920   ){
 38921     return SQLITE_OK;
 38923   pPager->setMaster = 1;
 38924   assert( isOpen(pPager->jfd) );
 38925   assert( pPager->journalHdr <= pPager->journalOff );
 38927   /* Calculate the length in bytes and the checksum of zMaster */
 38928   for(nMaster=0; zMaster[nMaster]; nMaster++){
 38929     cksum += zMaster[nMaster];
 38932   /* If in full-sync mode, advance to the next disk sector before writing
 38933   ** the master journal name. This is in case the previous page written to
 38934   ** the journal has already been synced.
 38935   */
 38936   if( pPager->fullSync ){
 38937     pPager->journalOff = journalHdrOffset(pPager);
 38939   iHdrOff = pPager->journalOff;
 38941   /* Write the master journal data to the end of the journal file. If
 38942   ** an error occurs, return the error code to the caller.
 38943   */
 38944   if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
 38945    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
 38946    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
 38947    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
 38948    || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
 38949   ){
 38950     return rc;
 38952   pPager->journalOff += (nMaster+20);
 38954   /* If the pager is in peristent-journal mode, then the physical 
 38955   ** journal-file may extend past the end of the master-journal name
 38956   ** and 8 bytes of magic data just written to the file. This is 
 38957   ** dangerous because the code to rollback a hot-journal file
 38958   ** will not be able to find the master-journal name to determine 
 38959   ** whether or not the journal is hot. 
 38960   **
 38961   ** Easiest thing to do in this scenario is to truncate the journal 
 38962   ** file to the required size.
 38963   */ 
 38964   if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
 38965    && jrnlSize>pPager->journalOff
 38966   ){
 38967     rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
 38969   return rc;
 38972 /*
 38973 ** Find a page in the hash table given its page number. Return
 38974 ** a pointer to the page or NULL if the requested page is not 
 38975 ** already in memory.
 38976 */
 38977 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
 38978   PgHdr *p;                         /* Return value */
 38980   /* It is not possible for a call to PcacheFetch() with createFlag==0 to
 38981   ** fail, since no attempt to allocate dynamic memory will be made.
 38982   */
 38983   (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
 38984   return p;
 38987 /*
 38988 ** Discard the entire contents of the in-memory page-cache.
 38989 */
 38990 static void pager_reset(Pager *pPager){
 38991   sqlite3BackupRestart(pPager->pBackup);
 38992   sqlite3PcacheClear(pPager->pPCache);
 38995 /*
 38996 ** Free all structures in the Pager.aSavepoint[] array and set both
 38997 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
 38998 ** if it is open and the pager is not in exclusive mode.
 38999 */
 39000 static void releaseAllSavepoints(Pager *pPager){
 39001   int ii;               /* Iterator for looping through Pager.aSavepoint */
 39002   for(ii=0; ii<pPager->nSavepoint; ii++){
 39003     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
 39005   if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
 39006     sqlite3OsClose(pPager->sjfd);
 39008   sqlite3_free(pPager->aSavepoint);
 39009   pPager->aSavepoint = 0;
 39010   pPager->nSavepoint = 0;
 39011   pPager->nSubRec = 0;
 39014 /*
 39015 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint 
 39016 ** bitvecs of all open savepoints. Return SQLITE_OK if successful
 39017 ** or SQLITE_NOMEM if a malloc failure occurs.
 39018 */
 39019 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
 39020   int ii;                   /* Loop counter */
 39021   int rc = SQLITE_OK;       /* Result code */
 39023   for(ii=0; ii<pPager->nSavepoint; ii++){
 39024     PagerSavepoint *p = &pPager->aSavepoint[ii];
 39025     if( pgno<=p->nOrig ){
 39026       rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
 39027       testcase( rc==SQLITE_NOMEM );
 39028       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
 39031   return rc;
 39034 /*
 39035 ** This function is a no-op if the pager is in exclusive mode and not
 39036 ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
 39037 ** state.
 39038 **
 39039 ** If the pager is not in exclusive-access mode, the database file is
 39040 ** completely unlocked. If the file is unlocked and the file-system does
 39041 ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
 39042 ** closed (if it is open).
 39043 **
 39044 ** If the pager is in ERROR state when this function is called, the 
 39045 ** contents of the pager cache are discarded before switching back to 
 39046 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
 39047 ** or not, any journal file left in the file-system will be treated
 39048 ** as a hot-journal and rolled back the next time a read-transaction
 39049 ** is opened (by this or by any other connection).
 39050 */
 39051 static void pager_unlock(Pager *pPager){
 39053   assert( pPager->eState==PAGER_READER 
 39054        || pPager->eState==PAGER_OPEN 
 39055        || pPager->eState==PAGER_ERROR 
 39056   );
 39058   sqlite3BitvecDestroy(pPager->pInJournal);
 39059   pPager->pInJournal = 0;
 39060   releaseAllSavepoints(pPager);
 39062   if( pagerUseWal(pPager) ){
 39063     assert( !isOpen(pPager->jfd) );
 39064     sqlite3WalEndReadTransaction(pPager->pWal);
 39065     pPager->eState = PAGER_OPEN;
 39066   }else if( !pPager->exclusiveMode ){
 39067     int rc;                       /* Error code returned by pagerUnlockDb() */
 39068     int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
 39070     /* If the operating system support deletion of open files, then
 39071     ** close the journal file when dropping the database lock.  Otherwise
 39072     ** another connection with journal_mode=delete might delete the file
 39073     ** out from under us.
 39074     */
 39075     assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
 39076     assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
 39077     assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
 39078     assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
 39079     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
 39080     assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
 39081     if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
 39082      || 1!=(pPager->journalMode & 5)
 39083     ){
 39084       sqlite3OsClose(pPager->jfd);
 39087     /* If the pager is in the ERROR state and the call to unlock the database
 39088     ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
 39089     ** above the #define for UNKNOWN_LOCK for an explanation of why this
 39090     ** is necessary.
 39091     */
 39092     rc = pagerUnlockDb(pPager, NO_LOCK);
 39093     if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
 39094       pPager->eLock = UNKNOWN_LOCK;
 39097     /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
 39098     ** without clearing the error code. This is intentional - the error
 39099     ** code is cleared and the cache reset in the block below.
 39100     */
 39101     assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
 39102     pPager->changeCountDone = 0;
 39103     pPager->eState = PAGER_OPEN;
 39106   /* If Pager.errCode is set, the contents of the pager cache cannot be
 39107   ** trusted. Now that there are no outstanding references to the pager,
 39108   ** it can safely move back to PAGER_OPEN state. This happens in both
 39109   ** normal and exclusive-locking mode.
 39110   */
 39111   if( pPager->errCode ){
 39112     assert( !MEMDB );
 39113     pager_reset(pPager);
 39114     pPager->changeCountDone = pPager->tempFile;
 39115     pPager->eState = PAGER_OPEN;
 39116     pPager->errCode = SQLITE_OK;
 39119   pPager->journalOff = 0;
 39120   pPager->journalHdr = 0;
 39121   pPager->setMaster = 0;
 39124 /*
 39125 ** This function is called whenever an IOERR or FULL error that requires
 39126 ** the pager to transition into the ERROR state may ahve occurred.
 39127 ** The first argument is a pointer to the pager structure, the second 
 39128 ** the error-code about to be returned by a pager API function. The 
 39129 ** value returned is a copy of the second argument to this function. 
 39130 **
 39131 ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
 39132 ** IOERR sub-codes, the pager enters the ERROR state and the error code
 39133 ** is stored in Pager.errCode. While the pager remains in the ERROR state,
 39134 ** all major API calls on the Pager will immediately return Pager.errCode.
 39135 **
 39136 ** The ERROR state indicates that the contents of the pager-cache 
 39137 ** cannot be trusted. This state can be cleared by completely discarding 
 39138 ** the contents of the pager-cache. If a transaction was active when
 39139 ** the persistent error occurred, then the rollback journal may need
 39140 ** to be replayed to restore the contents of the database file (as if
 39141 ** it were a hot-journal).
 39142 */
 39143 static int pager_error(Pager *pPager, int rc){
 39144   int rc2 = rc & 0xff;
 39145   assert( rc==SQLITE_OK || !MEMDB );
 39146   assert(
 39147        pPager->errCode==SQLITE_FULL ||
 39148        pPager->errCode==SQLITE_OK ||
 39149        (pPager->errCode & 0xff)==SQLITE_IOERR
 39150   );
 39151   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
 39152     pPager->errCode = rc;
 39153     pPager->eState = PAGER_ERROR;
 39155   return rc;
 39158 /*
 39159 ** This routine ends a transaction. A transaction is usually ended by 
 39160 ** either a COMMIT or a ROLLBACK operation. This routine may be called 
 39161 ** after rollback of a hot-journal, or if an error occurs while opening
 39162 ** the journal file or writing the very first journal-header of a
 39163 ** database transaction.
 39164 ** 
 39165 ** This routine is never called in PAGER_ERROR state. If it is called
 39166 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
 39167 ** exclusive than a RESERVED lock, it is a no-op.
 39168 **
 39169 ** Otherwise, any active savepoints are released.
 39170 **
 39171 ** If the journal file is open, then it is "finalized". Once a journal 
 39172 ** file has been finalized it is not possible to use it to roll back a 
 39173 ** transaction. Nor will it be considered to be a hot-journal by this
 39174 ** or any other database connection. Exactly how a journal is finalized
 39175 ** depends on whether or not the pager is running in exclusive mode and
 39176 ** the current journal-mode (Pager.journalMode value), as follows:
 39177 **
 39178 **   journalMode==MEMORY
 39179 **     Journal file descriptor is simply closed. This destroys an 
 39180 **     in-memory journal.
 39181 **
 39182 **   journalMode==TRUNCATE
 39183 **     Journal file is truncated to zero bytes in size.
 39184 **
 39185 **   journalMode==PERSIST
 39186 **     The first 28 bytes of the journal file are zeroed. This invalidates
 39187 **     the first journal header in the file, and hence the entire journal
 39188 **     file. An invalid journal file cannot be rolled back.
 39189 **
 39190 **   journalMode==DELETE
 39191 **     The journal file is closed and deleted using sqlite3OsDelete().
 39192 **
 39193 **     If the pager is running in exclusive mode, this method of finalizing
 39194 **     the journal file is never used. Instead, if the journalMode is
 39195 **     DELETE and the pager is in exclusive mode, the method described under
 39196 **     journalMode==PERSIST is used instead.
 39197 **
 39198 ** After the journal is finalized, the pager moves to PAGER_READER state.
 39199 ** If running in non-exclusive rollback mode, the lock on the file is 
 39200 ** downgraded to a SHARED_LOCK.
 39201 **
 39202 ** SQLITE_OK is returned if no error occurs. If an error occurs during
 39203 ** any of the IO operations to finalize the journal file or unlock the
 39204 ** database then the IO error code is returned to the user. If the 
 39205 ** operation to finalize the journal file fails, then the code still
 39206 ** tries to unlock the database file if not in exclusive mode. If the
 39207 ** unlock operation fails as well, then the first error code related
 39208 ** to the first error encountered (the journal finalization one) is
 39209 ** returned.
 39210 */
 39211 static int pager_end_transaction(Pager *pPager, int hasMaster){
 39212   int rc = SQLITE_OK;      /* Error code from journal finalization operation */
 39213   int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
 39215   /* Do nothing if the pager does not have an open write transaction
 39216   ** or at least a RESERVED lock. This function may be called when there
 39217   ** is no write-transaction active but a RESERVED or greater lock is
 39218   ** held under two circumstances:
 39219   **
 39220   **   1. After a successful hot-journal rollback, it is called with
 39221   **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
 39222   **
 39223   **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE 
 39224   **      lock switches back to locking_mode=normal and then executes a
 39225   **      read-transaction, this function is called with eState==PAGER_READER 
 39226   **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
 39227   */
 39228   assert( assert_pager_state(pPager) );
 39229   assert( pPager->eState!=PAGER_ERROR );
 39230   if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
 39231     return SQLITE_OK;
 39234   releaseAllSavepoints(pPager);
 39235   assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
 39236   if( isOpen(pPager->jfd) ){
 39237     assert( !pagerUseWal(pPager) );
 39239     /* Finalize the journal file. */
 39240     if( sqlite3IsMemJournal(pPager->jfd) ){
 39241       assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
 39242       sqlite3OsClose(pPager->jfd);
 39243     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
 39244       if( pPager->journalOff==0 ){
 39245         rc = SQLITE_OK;
 39246       }else{
 39247         rc = sqlite3OsTruncate(pPager->jfd, 0);
 39249       pPager->journalOff = 0;
 39250     }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
 39251       || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
 39252     ){
 39253       rc = zeroJournalHdr(pPager, hasMaster);
 39254       pPager->journalOff = 0;
 39255     }else{
 39256       /* This branch may be executed with Pager.journalMode==MEMORY if
 39257       ** a hot-journal was just rolled back. In this case the journal
 39258       ** file should be closed and deleted. If this connection writes to
 39259       ** the database file, it will do so using an in-memory journal. 
 39260       */
 39261       int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
 39262       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
 39263            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
 39264            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
 39265       );
 39266       sqlite3OsClose(pPager->jfd);
 39267       if( bDelete ){
 39268         rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 39273 #ifdef SQLITE_CHECK_PAGES
 39274   sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
 39275   if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
 39276     PgHdr *p = pager_lookup(pPager, 1);
 39277     if( p ){
 39278       p->pageHash = 0;
 39279       sqlite3PagerUnref(p);
 39282 #endif
 39284   sqlite3BitvecDestroy(pPager->pInJournal);
 39285   pPager->pInJournal = 0;
 39286   pPager->nRec = 0;
 39287   sqlite3PcacheCleanAll(pPager->pPCache);
 39288   sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
 39290   if( pagerUseWal(pPager) ){
 39291     /* Drop the WAL write-lock, if any. Also, if the connection was in 
 39292     ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 
 39293     ** lock held on the database file.
 39294     */
 39295     rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
 39296     assert( rc2==SQLITE_OK );
 39298   if( !pPager->exclusiveMode 
 39299    && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
 39300   ){
 39301     rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
 39302     pPager->changeCountDone = 0;
 39304   pPager->eState = PAGER_READER;
 39305   pPager->setMaster = 0;
 39307   return (rc==SQLITE_OK?rc2:rc);
 39310 /*
 39311 ** Execute a rollback if a transaction is active and unlock the 
 39312 ** database file. 
 39313 **
 39314 ** If the pager has already entered the ERROR state, do not attempt 
 39315 ** the rollback at this time. Instead, pager_unlock() is called. The
 39316 ** call to pager_unlock() will discard all in-memory pages, unlock
 39317 ** the database file and move the pager back to OPEN state. If this 
 39318 ** means that there is a hot-journal left in the file-system, the next 
 39319 ** connection to obtain a shared lock on the pager (which may be this one) 
 39320 ** will roll it back.
 39321 **
 39322 ** If the pager has not already entered the ERROR state, but an IO or
 39323 ** malloc error occurs during a rollback, then this will itself cause 
 39324 ** the pager to enter the ERROR state. Which will be cleared by the
 39325 ** call to pager_unlock(), as described above.
 39326 */
 39327 static void pagerUnlockAndRollback(Pager *pPager){
 39328   if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
 39329     assert( assert_pager_state(pPager) );
 39330     if( pPager->eState>=PAGER_WRITER_LOCKED ){
 39331       sqlite3BeginBenignMalloc();
 39332       sqlite3PagerRollback(pPager);
 39333       sqlite3EndBenignMalloc();
 39334     }else if( !pPager->exclusiveMode ){
 39335       assert( pPager->eState==PAGER_READER );
 39336       pager_end_transaction(pPager, 0);
 39339   pager_unlock(pPager);
 39342 /*
 39343 ** Parameter aData must point to a buffer of pPager->pageSize bytes
 39344 ** of data. Compute and return a checksum based ont the contents of the 
 39345 ** page of data and the current value of pPager->cksumInit.
 39346 **
 39347 ** This is not a real checksum. It is really just the sum of the 
 39348 ** random initial value (pPager->cksumInit) and every 200th byte
 39349 ** of the page data, starting with byte offset (pPager->pageSize%200).
 39350 ** Each byte is interpreted as an 8-bit unsigned integer.
 39351 **
 39352 ** Changing the formula used to compute this checksum results in an
 39353 ** incompatible journal file format.
 39354 **
 39355 ** If journal corruption occurs due to a power failure, the most likely 
 39356 ** scenario is that one end or the other of the record will be changed. 
 39357 ** It is much less likely that the two ends of the journal record will be
 39358 ** correct and the middle be corrupt.  Thus, this "checksum" scheme,
 39359 ** though fast and simple, catches the mostly likely kind of corruption.
 39360 */
 39361 static u32 pager_cksum(Pager *pPager, const u8 *aData){
 39362   u32 cksum = pPager->cksumInit;         /* Checksum value to return */
 39363   int i = pPager->pageSize-200;          /* Loop counter */
 39364   while( i>0 ){
 39365     cksum += aData[i];
 39366     i -= 200;
 39368   return cksum;
 39371 /*
 39372 ** Report the current page size and number of reserved bytes back
 39373 ** to the codec.
 39374 */
 39375 #ifdef SQLITE_HAS_CODEC
 39376 static void pagerReportSize(Pager *pPager){
 39377   if( pPager->xCodecSizeChng ){
 39378     pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
 39379                            (int)pPager->nReserve);
 39382 #else
 39383 # define pagerReportSize(X)     /* No-op if we do not support a codec */
 39384 #endif
 39386 /*
 39387 ** Read a single page from either the journal file (if isMainJrnl==1) or
 39388 ** from the sub-journal (if isMainJrnl==0) and playback that page.
 39389 ** The page begins at offset *pOffset into the file. The *pOffset
 39390 ** value is increased to the start of the next page in the journal.
 39391 **
 39392 ** The main rollback journal uses checksums - the statement journal does 
 39393 ** not.
 39394 **
 39395 ** If the page number of the page record read from the (sub-)journal file
 39396 ** is greater than the current value of Pager.dbSize, then playback is
 39397 ** skipped and SQLITE_OK is returned.
 39398 **
 39399 ** If pDone is not NULL, then it is a record of pages that have already
 39400 ** been played back.  If the page at *pOffset has already been played back
 39401 ** (if the corresponding pDone bit is set) then skip the playback.
 39402 ** Make sure the pDone bit corresponding to the *pOffset page is set
 39403 ** prior to returning.
 39404 **
 39405 ** If the page record is successfully read from the (sub-)journal file
 39406 ** and played back, then SQLITE_OK is returned. If an IO error occurs
 39407 ** while reading the record from the (sub-)journal file or while writing
 39408 ** to the database file, then the IO error code is returned. If data
 39409 ** is successfully read from the (sub-)journal file but appears to be
 39410 ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
 39411 ** two circumstances:
 39412 ** 
 39413 **   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
 39414 **   * If the record is being rolled back from the main journal file
 39415 **     and the checksum field does not match the record content.
 39416 **
 39417 ** Neither of these two scenarios are possible during a savepoint rollback.
 39418 **
 39419 ** If this is a savepoint rollback, then memory may have to be dynamically
 39420 ** allocated by this function. If this is the case and an allocation fails,
 39421 ** SQLITE_NOMEM is returned.
 39422 */
 39423 static int pager_playback_one_page(
 39424   Pager *pPager,                /* The pager being played back */
 39425   i64 *pOffset,                 /* Offset of record to playback */
 39426   Bitvec *pDone,                /* Bitvec of pages already played back */
 39427   int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
 39428   int isSavepnt                 /* True for a savepoint rollback */
 39429 ){
 39430   int rc;
 39431   PgHdr *pPg;                   /* An existing page in the cache */
 39432   Pgno pgno;                    /* The page number of a page in journal */
 39433   u32 cksum;                    /* Checksum used for sanity checking */
 39434   char *aData;                  /* Temporary storage for the page */
 39435   sqlite3_file *jfd;            /* The file descriptor for the journal file */
 39436   int isSynced;                 /* True if journal page is synced */
 39438   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
 39439   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
 39440   assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
 39441   assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
 39443   aData = pPager->pTmpSpace;
 39444   assert( aData );         /* Temp storage must have already been allocated */
 39445   assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
 39447   /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction 
 39448   ** or savepoint rollback done at the request of the caller) or this is
 39449   ** a hot-journal rollback. If it is a hot-journal rollback, the pager
 39450   ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
 39451   ** only reads from the main journal, not the sub-journal.
 39452   */
 39453   assert( pPager->eState>=PAGER_WRITER_CACHEMOD
 39454        || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
 39455   );
 39456   assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
 39458   /* Read the page number and page data from the journal or sub-journal
 39459   ** file. Return an error code to the caller if an IO error occurs.
 39460   */
 39461   jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
 39462   rc = read32bits(jfd, *pOffset, &pgno);
 39463   if( rc!=SQLITE_OK ) return rc;
 39464   rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
 39465   if( rc!=SQLITE_OK ) return rc;
 39466   *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
 39468   /* Sanity checking on the page.  This is more important that I originally
 39469   ** thought.  If a power failure occurs while the journal is being written,
 39470   ** it could cause invalid data to be written into the journal.  We need to
 39471   ** detect this invalid data (with high probability) and ignore it.
 39472   */
 39473   if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
 39474     assert( !isSavepnt );
 39475     return SQLITE_DONE;
 39477   if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
 39478     return SQLITE_OK;
 39480   if( isMainJrnl ){
 39481     rc = read32bits(jfd, (*pOffset)-4, &cksum);
 39482     if( rc ) return rc;
 39483     if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
 39484       return SQLITE_DONE;
 39488   /* If this page has already been played by before during the current
 39489   ** rollback, then don't bother to play it back again.
 39490   */
 39491   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
 39492     return rc;
 39495   /* When playing back page 1, restore the nReserve setting
 39496   */
 39497   if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
 39498     pPager->nReserve = ((u8*)aData)[20];
 39499     pagerReportSize(pPager);
 39502   /* If the pager is in CACHEMOD state, then there must be a copy of this
 39503   ** page in the pager cache. In this case just update the pager cache,
 39504   ** not the database file. The page is left marked dirty in this case.
 39505   **
 39506   ** An exception to the above rule: If the database is in no-sync mode
 39507   ** and a page is moved during an incremental vacuum then the page may
 39508   ** not be in the pager cache. Later: if a malloc() or IO error occurs
 39509   ** during a Movepage() call, then the page may not be in the cache
 39510   ** either. So the condition described in the above paragraph is not
 39511   ** assert()able.
 39512   **
 39513   ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
 39514   ** pager cache if it exists and the main file. The page is then marked 
 39515   ** not dirty. Since this code is only executed in PAGER_OPEN state for
 39516   ** a hot-journal rollback, it is guaranteed that the page-cache is empty
 39517   ** if the pager is in OPEN state.
 39518   **
 39519   ** Ticket #1171:  The statement journal might contain page content that is
 39520   ** different from the page content at the start of the transaction.
 39521   ** This occurs when a page is changed prior to the start of a statement
 39522   ** then changed again within the statement.  When rolling back such a
 39523   ** statement we must not write to the original database unless we know
 39524   ** for certain that original page contents are synced into the main rollback
 39525   ** journal.  Otherwise, a power loss might leave modified data in the
 39526   ** database file without an entry in the rollback journal that can
 39527   ** restore the database to its original form.  Two conditions must be
 39528   ** met before writing to the database files. (1) the database must be
 39529   ** locked.  (2) we know that the original page content is fully synced
 39530   ** in the main journal either because the page is not in cache or else
 39531   ** the page is marked as needSync==0.
 39532   **
 39533   ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
 39534   ** is possible to fail a statement on a database that does not yet exist.
 39535   ** Do not attempt to write if database file has never been opened.
 39536   */
 39537   if( pagerUseWal(pPager) ){
 39538     pPg = 0;
 39539   }else{
 39540     pPg = pager_lookup(pPager, pgno);
 39542   assert( pPg || !MEMDB );
 39543   assert( pPager->eState!=PAGER_OPEN || pPg==0 );
 39544   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
 39545            PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
 39546            (isMainJrnl?"main-journal":"sub-journal")
 39547   ));
 39548   if( isMainJrnl ){
 39549     isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
 39550   }else{
 39551     isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
 39553   if( isOpen(pPager->fd)
 39554    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
 39555    && isSynced
 39556   ){
 39557     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
 39558     testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
 39559     assert( !pagerUseWal(pPager) );
 39560     rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
 39561     if( pgno>pPager->dbFileSize ){
 39562       pPager->dbFileSize = pgno;
 39564     if( pPager->pBackup ){
 39565       CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
 39566       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
 39567       CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
 39569   }else if( !isMainJrnl && pPg==0 ){
 39570     /* If this is a rollback of a savepoint and data was not written to
 39571     ** the database and the page is not in-memory, there is a potential
 39572     ** problem. When the page is next fetched by the b-tree layer, it 
 39573     ** will be read from the database file, which may or may not be 
 39574     ** current. 
 39575     **
 39576     ** There are a couple of different ways this can happen. All are quite
 39577     ** obscure. When running in synchronous mode, this can only happen 
 39578     ** if the page is on the free-list at the start of the transaction, then
 39579     ** populated, then moved using sqlite3PagerMovepage().
 39580     **
 39581     ** The solution is to add an in-memory page to the cache containing
 39582     ** the data just read from the sub-journal. Mark the page as dirty 
 39583     ** and if the pager requires a journal-sync, then mark the page as 
 39584     ** requiring a journal-sync before it is written.
 39585     */
 39586     assert( isSavepnt );
 39587     assert( pPager->doNotSpill==0 );
 39588     pPager->doNotSpill++;
 39589     rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
 39590     assert( pPager->doNotSpill==1 );
 39591     pPager->doNotSpill--;
 39592     if( rc!=SQLITE_OK ) return rc;
 39593     pPg->flags &= ~PGHDR_NEED_READ;
 39594     sqlite3PcacheMakeDirty(pPg);
 39596   if( pPg ){
 39597     /* No page should ever be explicitly rolled back that is in use, except
 39598     ** for page 1 which is held in use in order to keep the lock on the
 39599     ** database active. However such a page may be rolled back as a result
 39600     ** of an internal error resulting in an automatic call to
 39601     ** sqlite3PagerRollback().
 39602     */
 39603     void *pData;
 39604     pData = pPg->pData;
 39605     memcpy(pData, (u8*)aData, pPager->pageSize);
 39606     pPager->xReiniter(pPg);
 39607     if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
 39608       /* If the contents of this page were just restored from the main 
 39609       ** journal file, then its content must be as they were when the 
 39610       ** transaction was first opened. In this case we can mark the page
 39611       ** as clean, since there will be no need to write it out to the
 39612       ** database.
 39613       **
 39614       ** There is one exception to this rule. If the page is being rolled
 39615       ** back as part of a savepoint (or statement) rollback from an 
 39616       ** unsynced portion of the main journal file, then it is not safe
 39617       ** to mark the page as clean. This is because marking the page as
 39618       ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
 39619       ** already in the journal file (recorded in Pager.pInJournal) and
 39620       ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
 39621       ** again within this transaction, it will be marked as dirty but
 39622       ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
 39623       ** be written out into the database file before its journal file
 39624       ** segment is synced. If a crash occurs during or following this,
 39625       ** database corruption may ensue.
 39626       */
 39627       assert( !pagerUseWal(pPager) );
 39628       sqlite3PcacheMakeClean(pPg);
 39630     pager_set_pagehash(pPg);
 39632     /* If this was page 1, then restore the value of Pager.dbFileVers.
 39633     ** Do this before any decoding. */
 39634     if( pgno==1 ){
 39635       memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
 39638     /* Decode the page just read from disk */
 39639     CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
 39640     sqlite3PcacheRelease(pPg);
 39642   return rc;
 39645 /*
 39646 ** Parameter zMaster is the name of a master journal file. A single journal
 39647 ** file that referred to the master journal file has just been rolled back.
 39648 ** This routine checks if it is possible to delete the master journal file,
 39649 ** and does so if it is.
 39650 **
 39651 ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
 39652 ** available for use within this function.
 39653 **
 39654 ** When a master journal file is created, it is populated with the names 
 39655 ** of all of its child journals, one after another, formatted as utf-8 
 39656 ** encoded text. The end of each child journal file is marked with a 
 39657 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
 39658 ** file for a transaction involving two databases might be:
 39659 **
 39660 **   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
 39661 **
 39662 ** A master journal file may only be deleted once all of its child 
 39663 ** journals have been rolled back.
 39664 **
 39665 ** This function reads the contents of the master-journal file into 
 39666 ** memory and loops through each of the child journal names. For
 39667 ** each child journal, it checks if:
 39668 **
 39669 **   * if the child journal exists, and if so
 39670 **   * if the child journal contains a reference to master journal 
 39671 **     file zMaster
 39672 **
 39673 ** If a child journal can be found that matches both of the criteria
 39674 ** above, this function returns without doing anything. Otherwise, if
 39675 ** no such child journal can be found, file zMaster is deleted from
 39676 ** the file-system using sqlite3OsDelete().
 39677 **
 39678 ** If an IO error within this function, an error code is returned. This
 39679 ** function allocates memory by calling sqlite3Malloc(). If an allocation
 39680 ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors 
 39681 ** occur, SQLITE_OK is returned.
 39682 **
 39683 ** TODO: This function allocates a single block of memory to load
 39684 ** the entire contents of the master journal file. This could be
 39685 ** a couple of kilobytes or so - potentially larger than the page 
 39686 ** size.
 39687 */
 39688 static int pager_delmaster(Pager *pPager, const char *zMaster){
 39689   sqlite3_vfs *pVfs = pPager->pVfs;
 39690   int rc;                   /* Return code */
 39691   sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
 39692   sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
 39693   char *zMasterJournal = 0; /* Contents of master journal file */
 39694   i64 nMasterJournal;       /* Size of master journal file */
 39695   char *zJournal;           /* Pointer to one journal within MJ file */
 39696   char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
 39697   int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
 39699   /* Allocate space for both the pJournal and pMaster file descriptors.
 39700   ** If successful, open the master journal file for reading.
 39701   */
 39702   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
 39703   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
 39704   if( !pMaster ){
 39705     rc = SQLITE_NOMEM;
 39706   }else{
 39707     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
 39708     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
 39710   if( rc!=SQLITE_OK ) goto delmaster_out;
 39712   /* Load the entire master journal file into space obtained from
 39713   ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
 39714   ** sufficient space (in zMasterPtr) to hold the names of master
 39715   ** journal files extracted from regular rollback-journals.
 39716   */
 39717   rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
 39718   if( rc!=SQLITE_OK ) goto delmaster_out;
 39719   nMasterPtr = pVfs->mxPathname+1;
 39720   zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
 39721   if( !zMasterJournal ){
 39722     rc = SQLITE_NOMEM;
 39723     goto delmaster_out;
 39725   zMasterPtr = &zMasterJournal[nMasterJournal+1];
 39726   rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
 39727   if( rc!=SQLITE_OK ) goto delmaster_out;
 39728   zMasterJournal[nMasterJournal] = 0;
 39730   zJournal = zMasterJournal;
 39731   while( (zJournal-zMasterJournal)<nMasterJournal ){
 39732     int exists;
 39733     rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
 39734     if( rc!=SQLITE_OK ){
 39735       goto delmaster_out;
 39737     if( exists ){
 39738       /* One of the journals pointed to by the master journal exists.
 39739       ** Open it and check if it points at the master journal. If
 39740       ** so, return without deleting the master journal file.
 39741       */
 39742       int c;
 39743       int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
 39744       rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
 39745       if( rc!=SQLITE_OK ){
 39746         goto delmaster_out;
 39749       rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
 39750       sqlite3OsClose(pJournal);
 39751       if( rc!=SQLITE_OK ){
 39752         goto delmaster_out;
 39755       c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
 39756       if( c ){
 39757         /* We have a match. Do not delete the master journal file. */
 39758         goto delmaster_out;
 39761     zJournal += (sqlite3Strlen30(zJournal)+1);
 39764   sqlite3OsClose(pMaster);
 39765   rc = sqlite3OsDelete(pVfs, zMaster, 0);
 39767 delmaster_out:
 39768   sqlite3_free(zMasterJournal);
 39769   if( pMaster ){
 39770     sqlite3OsClose(pMaster);
 39771     assert( !isOpen(pJournal) );
 39772     sqlite3_free(pMaster);
 39774   return rc;
 39778 /*
 39779 ** This function is used to change the actual size of the database 
 39780 ** file in the file-system. This only happens when committing a transaction,
 39781 ** or rolling back a transaction (including rolling back a hot-journal).
 39782 **
 39783 ** If the main database file is not open, or the pager is not in either
 39784 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size 
 39785 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). 
 39786 ** If the file on disk is currently larger than nPage pages, then use the VFS
 39787 ** xTruncate() method to truncate it.
 39788 **
 39789 ** Or, it might might be the case that the file on disk is smaller than 
 39790 ** nPage pages. Some operating system implementations can get confused if 
 39791 ** you try to truncate a file to some size that is larger than it 
 39792 ** currently is, so detect this case and write a single zero byte to 
 39793 ** the end of the new file instead.
 39794 **
 39795 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
 39796 ** the database file, return the error code to the caller.
 39797 */
 39798 static int pager_truncate(Pager *pPager, Pgno nPage){
 39799   int rc = SQLITE_OK;
 39800   assert( pPager->eState!=PAGER_ERROR );
 39801   assert( pPager->eState!=PAGER_READER );
 39803   if( isOpen(pPager->fd) 
 39804    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 
 39805   ){
 39806     i64 currentSize, newSize;
 39807     int szPage = pPager->pageSize;
 39808     assert( pPager->eLock==EXCLUSIVE_LOCK );
 39809     /* TODO: Is it safe to use Pager.dbFileSize here? */
 39810     rc = sqlite3OsFileSize(pPager->fd, &currentSize);
 39811     newSize = szPage*(i64)nPage;
 39812     if( rc==SQLITE_OK && currentSize!=newSize ){
 39813       if( currentSize>newSize ){
 39814         rc = sqlite3OsTruncate(pPager->fd, newSize);
 39815       }else if( (currentSize+szPage)<=newSize ){
 39816         char *pTmp = pPager->pTmpSpace;
 39817         memset(pTmp, 0, szPage);
 39818         testcase( (newSize-szPage) == currentSize );
 39819         testcase( (newSize-szPage) >  currentSize );
 39820         rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
 39822       if( rc==SQLITE_OK ){
 39823         pPager->dbFileSize = nPage;
 39827   return rc;
 39830 /*
 39831 ** Return a sanitized version of the sector-size of OS file pFile. The
 39832 ** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
 39833 */
 39834 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
 39835   int iRet = sqlite3OsSectorSize(pFile);
 39836   if( iRet<32 ){
 39837     iRet = 512;
 39838   }else if( iRet>MAX_SECTOR_SIZE ){
 39839     assert( MAX_SECTOR_SIZE>=512 );
 39840     iRet = MAX_SECTOR_SIZE;
 39842   return iRet;
 39845 /*
 39846 ** Set the value of the Pager.sectorSize variable for the given
 39847 ** pager based on the value returned by the xSectorSize method
 39848 ** of the open database file. The sector size will be used used 
 39849 ** to determine the size and alignment of journal header and 
 39850 ** master journal pointers within created journal files.
 39851 **
 39852 ** For temporary files the effective sector size is always 512 bytes.
 39853 **
 39854 ** Otherwise, for non-temporary files, the effective sector size is
 39855 ** the value returned by the xSectorSize() method rounded up to 32 if
 39856 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
 39857 ** is greater than MAX_SECTOR_SIZE.
 39858 **
 39859 ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
 39860 ** the effective sector size to its minimum value (512).  The purpose of
 39861 ** pPager->sectorSize is to define the "blast radius" of bytes that
 39862 ** might change if a crash occurs while writing to a single byte in
 39863 ** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
 39864 ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
 39865 ** size.  For backwards compatibility of the rollback journal file format,
 39866 ** we cannot reduce the effective sector size below 512.
 39867 */
 39868 static void setSectorSize(Pager *pPager){
 39869   assert( isOpen(pPager->fd) || pPager->tempFile );
 39871   if( pPager->tempFile
 39872    || (sqlite3OsDeviceCharacteristics(pPager->fd) & 
 39873               SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
 39874   ){
 39875     /* Sector size doesn't matter for temporary files. Also, the file
 39876     ** may not have been opened yet, in which case the OsSectorSize()
 39877     ** call will segfault. */
 39878     pPager->sectorSize = 512;
 39879   }else{
 39880     pPager->sectorSize = sqlite3SectorSize(pPager->fd);
 39884 /*
 39885 ** Playback the journal and thus restore the database file to
 39886 ** the state it was in before we started making changes.  
 39887 **
 39888 ** The journal file format is as follows: 
 39889 **
 39890 **  (1)  8 byte prefix.  A copy of aJournalMagic[].
 39891 **  (2)  4 byte big-endian integer which is the number of valid page records
 39892 **       in the journal.  If this value is 0xffffffff, then compute the
 39893 **       number of page records from the journal size.
 39894 **  (3)  4 byte big-endian integer which is the initial value for the 
 39895 **       sanity checksum.
 39896 **  (4)  4 byte integer which is the number of pages to truncate the
 39897 **       database to during a rollback.
 39898 **  (5)  4 byte big-endian integer which is the sector size.  The header
 39899 **       is this many bytes in size.
 39900 **  (6)  4 byte big-endian integer which is the page size.
 39901 **  (7)  zero padding out to the next sector size.
 39902 **  (8)  Zero or more pages instances, each as follows:
 39903 **        +  4 byte page number.
 39904 **        +  pPager->pageSize bytes of data.
 39905 **        +  4 byte checksum
 39906 **
 39907 ** When we speak of the journal header, we mean the first 7 items above.
 39908 ** Each entry in the journal is an instance of the 8th item.
 39909 **
 39910 ** Call the value from the second bullet "nRec".  nRec is the number of
 39911 ** valid page entries in the journal.  In most cases, you can compute the
 39912 ** value of nRec from the size of the journal file.  But if a power
 39913 ** failure occurred while the journal was being written, it could be the
 39914 ** case that the size of the journal file had already been increased but
 39915 ** the extra entries had not yet made it safely to disk.  In such a case,
 39916 ** the value of nRec computed from the file size would be too large.  For
 39917 ** that reason, we always use the nRec value in the header.
 39918 **
 39919 ** If the nRec value is 0xffffffff it means that nRec should be computed
 39920 ** from the file size.  This value is used when the user selects the
 39921 ** no-sync option for the journal.  A power failure could lead to corruption
 39922 ** in this case.  But for things like temporary table (which will be
 39923 ** deleted when the power is restored) we don't care.  
 39924 **
 39925 ** If the file opened as the journal file is not a well-formed
 39926 ** journal file then all pages up to the first corrupted page are rolled
 39927 ** back (or no pages if the journal header is corrupted). The journal file
 39928 ** is then deleted and SQLITE_OK returned, just as if no corruption had
 39929 ** been encountered.
 39930 **
 39931 ** If an I/O or malloc() error occurs, the journal-file is not deleted
 39932 ** and an error code is returned.
 39933 **
 39934 ** The isHot parameter indicates that we are trying to rollback a journal
 39935 ** that might be a hot journal.  Or, it could be that the journal is 
 39936 ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
 39937 ** If the journal really is hot, reset the pager cache prior rolling
 39938 ** back any content.  If the journal is merely persistent, no reset is
 39939 ** needed.
 39940 */
 39941 static int pager_playback(Pager *pPager, int isHot){
 39942   sqlite3_vfs *pVfs = pPager->pVfs;
 39943   i64 szJ;                 /* Size of the journal file in bytes */
 39944   u32 nRec;                /* Number of Records in the journal */
 39945   u32 u;                   /* Unsigned loop counter */
 39946   Pgno mxPg = 0;           /* Size of the original file in pages */
 39947   int rc;                  /* Result code of a subroutine */
 39948   int res = 1;             /* Value returned by sqlite3OsAccess() */
 39949   char *zMaster = 0;       /* Name of master journal file if any */
 39950   int needPagerReset;      /* True to reset page prior to first page rollback */
 39952   /* Figure out how many records are in the journal.  Abort early if
 39953   ** the journal is empty.
 39954   */
 39955   assert( isOpen(pPager->jfd) );
 39956   rc = sqlite3OsFileSize(pPager->jfd, &szJ);
 39957   if( rc!=SQLITE_OK ){
 39958     goto end_playback;
 39961   /* Read the master journal name from the journal, if it is present.
 39962   ** If a master journal file name is specified, but the file is not
 39963   ** present on disk, then the journal is not hot and does not need to be
 39964   ** played back.
 39965   **
 39966   ** TODO: Technically the following is an error because it assumes that
 39967   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
 39968   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
 39969   **  mxPathname is 512, which is the same as the minimum allowable value
 39970   ** for pageSize.
 39971   */
 39972   zMaster = pPager->pTmpSpace;
 39973   rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
 39974   if( rc==SQLITE_OK && zMaster[0] ){
 39975     rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
 39977   zMaster = 0;
 39978   if( rc!=SQLITE_OK || !res ){
 39979     goto end_playback;
 39981   pPager->journalOff = 0;
 39982   needPagerReset = isHot;
 39984   /* This loop terminates either when a readJournalHdr() or 
 39985   ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 
 39986   ** occurs. 
 39987   */
 39988   while( 1 ){
 39989     /* Read the next journal header from the journal file.  If there are
 39990     ** not enough bytes left in the journal file for a complete header, or
 39991     ** it is corrupted, then a process must have failed while writing it.
 39992     ** This indicates nothing more needs to be rolled back.
 39993     */
 39994     rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
 39995     if( rc!=SQLITE_OK ){ 
 39996       if( rc==SQLITE_DONE ){
 39997         rc = SQLITE_OK;
 39999       goto end_playback;
 40002     /* If nRec is 0xffffffff, then this journal was created by a process
 40003     ** working in no-sync mode. This means that the rest of the journal
 40004     ** file consists of pages, there are no more journal headers. Compute
 40005     ** the value of nRec based on this assumption.
 40006     */
 40007     if( nRec==0xffffffff ){
 40008       assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
 40009       nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
 40012     /* If nRec is 0 and this rollback is of a transaction created by this
 40013     ** process and if this is the final header in the journal, then it means
 40014     ** that this part of the journal was being filled but has not yet been
 40015     ** synced to disk.  Compute the number of pages based on the remaining
 40016     ** size of the file.
 40017     **
 40018     ** The third term of the test was added to fix ticket #2565.
 40019     ** When rolling back a hot journal, nRec==0 always means that the next
 40020     ** chunk of the journal contains zero pages to be rolled back.  But
 40021     ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
 40022     ** the journal, it means that the journal might contain additional
 40023     ** pages that need to be rolled back and that the number of pages 
 40024     ** should be computed based on the journal file size.
 40025     */
 40026     if( nRec==0 && !isHot &&
 40027         pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
 40028       nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
 40031     /* If this is the first header read from the journal, truncate the
 40032     ** database file back to its original size.
 40033     */
 40034     if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
 40035       rc = pager_truncate(pPager, mxPg);
 40036       if( rc!=SQLITE_OK ){
 40037         goto end_playback;
 40039       pPager->dbSize = mxPg;
 40042     /* Copy original pages out of the journal and back into the 
 40043     ** database file and/or page cache.
 40044     */
 40045     for(u=0; u<nRec; u++){
 40046       if( needPagerReset ){
 40047         pager_reset(pPager);
 40048         needPagerReset = 0;
 40050       rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
 40051       if( rc!=SQLITE_OK ){
 40052         if( rc==SQLITE_DONE ){
 40053           pPager->journalOff = szJ;
 40054           break;
 40055         }else if( rc==SQLITE_IOERR_SHORT_READ ){
 40056           /* If the journal has been truncated, simply stop reading and
 40057           ** processing the journal. This might happen if the journal was
 40058           ** not completely written and synced prior to a crash.  In that
 40059           ** case, the database should have never been written in the
 40060           ** first place so it is OK to simply abandon the rollback. */
 40061           rc = SQLITE_OK;
 40062           goto end_playback;
 40063         }else{
 40064           /* If we are unable to rollback, quit and return the error
 40065           ** code.  This will cause the pager to enter the error state
 40066           ** so that no further harm will be done.  Perhaps the next
 40067           ** process to come along will be able to rollback the database.
 40068           */
 40069           goto end_playback;
 40074   /*NOTREACHED*/
 40075   assert( 0 );
 40077 end_playback:
 40078   /* Following a rollback, the database file should be back in its original
 40079   ** state prior to the start of the transaction, so invoke the
 40080   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
 40081   ** assertion that the transaction counter was modified.
 40082   */
 40083 #ifdef SQLITE_DEBUG
 40084   if( pPager->fd->pMethods ){
 40085     sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 40087 #endif
 40089   /* If this playback is happening automatically as a result of an IO or 
 40090   ** malloc error that occurred after the change-counter was updated but 
 40091   ** before the transaction was committed, then the change-counter 
 40092   ** modification may just have been reverted. If this happens in exclusive 
 40093   ** mode, then subsequent transactions performed by the connection will not
 40094   ** update the change-counter at all. This may lead to cache inconsistency
 40095   ** problems for other processes at some point in the future. So, just
 40096   ** in case this has happened, clear the changeCountDone flag now.
 40097   */
 40098   pPager->changeCountDone = pPager->tempFile;
 40100   if( rc==SQLITE_OK ){
 40101     zMaster = pPager->pTmpSpace;
 40102     rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
 40103     testcase( rc!=SQLITE_OK );
 40105   if( rc==SQLITE_OK
 40106    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
 40107   ){
 40108     rc = sqlite3PagerSync(pPager);
 40110   if( rc==SQLITE_OK ){
 40111     rc = pager_end_transaction(pPager, zMaster[0]!='\0');
 40112     testcase( rc!=SQLITE_OK );
 40114   if( rc==SQLITE_OK && zMaster[0] && res ){
 40115     /* If there was a master journal and this routine will return success,
 40116     ** see if it is possible to delete the master journal.
 40117     */
 40118     rc = pager_delmaster(pPager, zMaster);
 40119     testcase( rc!=SQLITE_OK );
 40122   /* The Pager.sectorSize variable may have been updated while rolling
 40123   ** back a journal created by a process with a different sector size
 40124   ** value. Reset it to the correct value for this process.
 40125   */
 40126   setSectorSize(pPager);
 40127   return rc;
 40131 /*
 40132 ** Read the content for page pPg out of the database file and into 
 40133 ** pPg->pData. A shared lock or greater must be held on the database
 40134 ** file before this function is called.
 40135 **
 40136 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
 40137 ** the value read from the database file.
 40138 **
 40139 ** If an IO error occurs, then the IO error is returned to the caller.
 40140 ** Otherwise, SQLITE_OK is returned.
 40141 */
 40142 static int readDbPage(PgHdr *pPg){
 40143   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
 40144   Pgno pgno = pPg->pgno;       /* Page number to read */
 40145   int rc = SQLITE_OK;          /* Return code */
 40146   int isInWal = 0;             /* True if page is in log file */
 40147   int pgsz = pPager->pageSize; /* Number of bytes to read */
 40149   assert( pPager->eState>=PAGER_READER && !MEMDB );
 40150   assert( isOpen(pPager->fd) );
 40152   if( NEVER(!isOpen(pPager->fd)) ){
 40153     assert( pPager->tempFile );
 40154     memset(pPg->pData, 0, pPager->pageSize);
 40155     return SQLITE_OK;
 40158   if( pagerUseWal(pPager) ){
 40159     /* Try to pull the page from the write-ahead log. */
 40160     rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
 40162   if( rc==SQLITE_OK && !isInWal ){
 40163     i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
 40164     rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
 40165     if( rc==SQLITE_IOERR_SHORT_READ ){
 40166       rc = SQLITE_OK;
 40170   if( pgno==1 ){
 40171     if( rc ){
 40172       /* If the read is unsuccessful, set the dbFileVers[] to something
 40173       ** that will never be a valid file version.  dbFileVers[] is a copy
 40174       ** of bytes 24..39 of the database.  Bytes 28..31 should always be
 40175       ** zero or the size of the database in page. Bytes 32..35 and 35..39
 40176       ** should be page numbers which are never 0xffffffff.  So filling
 40177       ** pPager->dbFileVers[] with all 0xff bytes should suffice.
 40178       **
 40179       ** For an encrypted database, the situation is more complex:  bytes
 40180       ** 24..39 of the database are white noise.  But the probability of
 40181       ** white noising equaling 16 bytes of 0xff is vanishingly small so
 40182       ** we should still be ok.
 40183       */
 40184       memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
 40185     }else{
 40186       u8 *dbFileVers = &((u8*)pPg->pData)[24];
 40187       memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
 40190   CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
 40192   PAGER_INCR(sqlite3_pager_readdb_count);
 40193   PAGER_INCR(pPager->nRead);
 40194   IOTRACE(("PGIN %p %d\n", pPager, pgno));
 40195   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
 40196                PAGERID(pPager), pgno, pager_pagehash(pPg)));
 40198   return rc;
 40201 /*
 40202 ** Update the value of the change-counter at offsets 24 and 92 in
 40203 ** the header and the sqlite version number at offset 96.
 40204 **
 40205 ** This is an unconditional update.  See also the pager_incr_changecounter()
 40206 ** routine which only updates the change-counter if the update is actually
 40207 ** needed, as determined by the pPager->changeCountDone state variable.
 40208 */
 40209 static void pager_write_changecounter(PgHdr *pPg){
 40210   u32 change_counter;
 40212   /* Increment the value just read and write it back to byte 24. */
 40213   change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
 40214   put32bits(((char*)pPg->pData)+24, change_counter);
 40216   /* Also store the SQLite version number in bytes 96..99 and in
 40217   ** bytes 92..95 store the change counter for which the version number
 40218   ** is valid. */
 40219   put32bits(((char*)pPg->pData)+92, change_counter);
 40220   put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
 40223 #ifndef SQLITE_OMIT_WAL
 40224 /*
 40225 ** This function is invoked once for each page that has already been 
 40226 ** written into the log file when a WAL transaction is rolled back.
 40227 ** Parameter iPg is the page number of said page. The pCtx argument 
 40228 ** is actually a pointer to the Pager structure.
 40229 **
 40230 ** If page iPg is present in the cache, and has no outstanding references,
 40231 ** it is discarded. Otherwise, if there are one or more outstanding
 40232 ** references, the page content is reloaded from the database. If the
 40233 ** attempt to reload content from the database is required and fails, 
 40234 ** return an SQLite error code. Otherwise, SQLITE_OK.
 40235 */
 40236 static int pagerUndoCallback(void *pCtx, Pgno iPg){
 40237   int rc = SQLITE_OK;
 40238   Pager *pPager = (Pager *)pCtx;
 40239   PgHdr *pPg;
 40241   pPg = sqlite3PagerLookup(pPager, iPg);
 40242   if( pPg ){
 40243     if( sqlite3PcachePageRefcount(pPg)==1 ){
 40244       sqlite3PcacheDrop(pPg);
 40245     }else{
 40246       rc = readDbPage(pPg);
 40247       if( rc==SQLITE_OK ){
 40248         pPager->xReiniter(pPg);
 40250       sqlite3PagerUnref(pPg);
 40254   /* Normally, if a transaction is rolled back, any backup processes are
 40255   ** updated as data is copied out of the rollback journal and into the
 40256   ** database. This is not generally possible with a WAL database, as
 40257   ** rollback involves simply truncating the log file. Therefore, if one
 40258   ** or more frames have already been written to the log (and therefore 
 40259   ** also copied into the backup databases) as part of this transaction,
 40260   ** the backups must be restarted.
 40261   */
 40262   sqlite3BackupRestart(pPager->pBackup);
 40264   return rc;
 40267 /*
 40268 ** This function is called to rollback a transaction on a WAL database.
 40269 */
 40270 static int pagerRollbackWal(Pager *pPager){
 40271   int rc;                         /* Return Code */
 40272   PgHdr *pList;                   /* List of dirty pages to revert */
 40274   /* For all pages in the cache that are currently dirty or have already
 40275   ** been written (but not committed) to the log file, do one of the 
 40276   ** following:
 40277   **
 40278   **   + Discard the cached page (if refcount==0), or
 40279   **   + Reload page content from the database (if refcount>0).
 40280   */
 40281   pPager->dbSize = pPager->dbOrigSize;
 40282   rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
 40283   pList = sqlite3PcacheDirtyList(pPager->pPCache);
 40284   while( pList && rc==SQLITE_OK ){
 40285     PgHdr *pNext = pList->pDirty;
 40286     rc = pagerUndoCallback((void *)pPager, pList->pgno);
 40287     pList = pNext;
 40290   return rc;
 40293 /*
 40294 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
 40295 ** the contents of the list of pages headed by pList (connected by pDirty),
 40296 ** this function notifies any active backup processes that the pages have
 40297 ** changed. 
 40298 **
 40299 ** The list of pages passed into this routine is always sorted by page number.
 40300 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
 40301 */ 
 40302 static int pagerWalFrames(
 40303   Pager *pPager,                  /* Pager object */
 40304   PgHdr *pList,                   /* List of frames to log */
 40305   Pgno nTruncate,                 /* Database size after this commit */
 40306   int isCommit                    /* True if this is a commit */
 40307 ){
 40308   int rc;                         /* Return code */
 40309   int nList;                      /* Number of pages in pList */
 40310 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
 40311   PgHdr *p;                       /* For looping over pages */
 40312 #endif
 40314   assert( pPager->pWal );
 40315   assert( pList );
 40316 #ifdef SQLITE_DEBUG
 40317   /* Verify that the page list is in accending order */
 40318   for(p=pList; p && p->pDirty; p=p->pDirty){
 40319     assert( p->pgno < p->pDirty->pgno );
 40321 #endif
 40323   assert( pList->pDirty==0 || isCommit );
 40324   if( isCommit ){
 40325     /* If a WAL transaction is being committed, there is no point in writing
 40326     ** any pages with page numbers greater than nTruncate into the WAL file.
 40327     ** They will never be read by any client. So remove them from the pDirty
 40328     ** list here. */
 40329     PgHdr *p;
 40330     PgHdr **ppNext = &pList;
 40331     nList = 0;
 40332     for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
 40333       if( p->pgno<=nTruncate ){
 40334         ppNext = &p->pDirty;
 40335         nList++;
 40338     assert( pList );
 40339   }else{
 40340     nList = 1;
 40342   pPager->aStat[PAGER_STAT_WRITE] += nList;
 40344   if( pList->pgno==1 ) pager_write_changecounter(pList);
 40345   rc = sqlite3WalFrames(pPager->pWal, 
 40346       pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
 40347   );
 40348   if( rc==SQLITE_OK && pPager->pBackup ){
 40349     PgHdr *p;
 40350     for(p=pList; p; p=p->pDirty){
 40351       sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
 40355 #ifdef SQLITE_CHECK_PAGES
 40356   pList = sqlite3PcacheDirtyList(pPager->pPCache);
 40357   for(p=pList; p; p=p->pDirty){
 40358     pager_set_pagehash(p);
 40360 #endif
 40362   return rc;
 40365 /*
 40366 ** Begin a read transaction on the WAL.
 40367 **
 40368 ** This routine used to be called "pagerOpenSnapshot()" because it essentially
 40369 ** makes a snapshot of the database at the current point in time and preserves
 40370 ** that snapshot for use by the reader in spite of concurrently changes by
 40371 ** other writers or checkpointers.
 40372 */
 40373 static int pagerBeginReadTransaction(Pager *pPager){
 40374   int rc;                         /* Return code */
 40375   int changed = 0;                /* True if cache must be reset */
 40377   assert( pagerUseWal(pPager) );
 40378   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
 40380   /* sqlite3WalEndReadTransaction() was not called for the previous
 40381   ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
 40382   ** are in locking_mode=NORMAL and EndRead() was previously called,
 40383   ** the duplicate call is harmless.
 40384   */
 40385   sqlite3WalEndReadTransaction(pPager->pWal);
 40387   rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
 40388   if( rc!=SQLITE_OK || changed ){
 40389     pager_reset(pPager);
 40392   return rc;
 40394 #endif
 40396 /*
 40397 ** This function is called as part of the transition from PAGER_OPEN
 40398 ** to PAGER_READER state to determine the size of the database file
 40399 ** in pages (assuming the page size currently stored in Pager.pageSize).
 40400 **
 40401 ** If no error occurs, SQLITE_OK is returned and the size of the database
 40402 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
 40403 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
 40404 */
 40405 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
 40406   Pgno nPage;                     /* Value to return via *pnPage */
 40408   /* Query the WAL sub-system for the database size. The WalDbsize()
 40409   ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
 40410   ** if the database size is not available. The database size is not
 40411   ** available from the WAL sub-system if the log file is empty or
 40412   ** contains no valid committed transactions.
 40413   */
 40414   assert( pPager->eState==PAGER_OPEN );
 40415   assert( pPager->eLock>=SHARED_LOCK );
 40416   nPage = sqlite3WalDbsize(pPager->pWal);
 40418   /* If the database size was not available from the WAL sub-system,
 40419   ** determine it based on the size of the database file. If the size
 40420   ** of the database file is not an integer multiple of the page-size,
 40421   ** round down to the nearest page. Except, any file larger than 0
 40422   ** bytes in size is considered to contain at least one page.
 40423   */
 40424   if( nPage==0 ){
 40425     i64 n = 0;                    /* Size of db file in bytes */
 40426     assert( isOpen(pPager->fd) || pPager->tempFile );
 40427     if( isOpen(pPager->fd) ){
 40428       int rc = sqlite3OsFileSize(pPager->fd, &n);
 40429       if( rc!=SQLITE_OK ){
 40430         return rc;
 40433     nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
 40436   /* If the current number of pages in the file is greater than the
 40437   ** configured maximum pager number, increase the allowed limit so
 40438   ** that the file can be read.
 40439   */
 40440   if( nPage>pPager->mxPgno ){
 40441     pPager->mxPgno = (Pgno)nPage;
 40444   *pnPage = nPage;
 40445   return SQLITE_OK;
 40448 #ifndef SQLITE_OMIT_WAL
 40449 /*
 40450 ** Check if the *-wal file that corresponds to the database opened by pPager
 40451 ** exists if the database is not empy, or verify that the *-wal file does
 40452 ** not exist (by deleting it) if the database file is empty.
 40453 **
 40454 ** If the database is not empty and the *-wal file exists, open the pager
 40455 ** in WAL mode.  If the database is empty or if no *-wal file exists and
 40456 ** if no error occurs, make sure Pager.journalMode is not set to
 40457 ** PAGER_JOURNALMODE_WAL.
 40458 **
 40459 ** Return SQLITE_OK or an error code.
 40460 **
 40461 ** The caller must hold a SHARED lock on the database file to call this
 40462 ** function. Because an EXCLUSIVE lock on the db file is required to delete 
 40463 ** a WAL on a none-empty database, this ensures there is no race condition 
 40464 ** between the xAccess() below and an xDelete() being executed by some 
 40465 ** other connection.
 40466 */
 40467 static int pagerOpenWalIfPresent(Pager *pPager){
 40468   int rc = SQLITE_OK;
 40469   assert( pPager->eState==PAGER_OPEN );
 40470   assert( pPager->eLock>=SHARED_LOCK );
 40472   if( !pPager->tempFile ){
 40473     int isWal;                    /* True if WAL file exists */
 40474     Pgno nPage;                   /* Size of the database file */
 40476     rc = pagerPagecount(pPager, &nPage);
 40477     if( rc ) return rc;
 40478     if( nPage==0 ){
 40479       rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
 40480       if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
 40481       isWal = 0;
 40482     }else{
 40483       rc = sqlite3OsAccess(
 40484           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
 40485       );
 40487     if( rc==SQLITE_OK ){
 40488       if( isWal ){
 40489         testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
 40490         rc = sqlite3PagerOpenWal(pPager, 0);
 40491       }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
 40492         pPager->journalMode = PAGER_JOURNALMODE_DELETE;
 40496   return rc;
 40498 #endif
 40500 /*
 40501 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
 40502 ** the entire master journal file. The case pSavepoint==NULL occurs when 
 40503 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction 
 40504 ** savepoint.
 40505 **
 40506 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is 
 40507 ** being rolled back), then the rollback consists of up to three stages,
 40508 ** performed in the order specified:
 40509 **
 40510 **   * Pages are played back from the main journal starting at byte
 40511 **     offset PagerSavepoint.iOffset and continuing to 
 40512 **     PagerSavepoint.iHdrOffset, or to the end of the main journal
 40513 **     file if PagerSavepoint.iHdrOffset is zero.
 40514 **
 40515 **   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
 40516 **     back starting from the journal header immediately following 
 40517 **     PagerSavepoint.iHdrOffset to the end of the main journal file.
 40518 **
 40519 **   * Pages are then played back from the sub-journal file, starting
 40520 **     with the PagerSavepoint.iSubRec and continuing to the end of
 40521 **     the journal file.
 40522 **
 40523 ** Throughout the rollback process, each time a page is rolled back, the
 40524 ** corresponding bit is set in a bitvec structure (variable pDone in the
 40525 ** implementation below). This is used to ensure that a page is only
 40526 ** rolled back the first time it is encountered in either journal.
 40527 **
 40528 ** If pSavepoint is NULL, then pages are only played back from the main
 40529 ** journal file. There is no need for a bitvec in this case.
 40530 **
 40531 ** In either case, before playback commences the Pager.dbSize variable
 40532 ** is reset to the value that it held at the start of the savepoint 
 40533 ** (or transaction). No page with a page-number greater than this value
 40534 ** is played back. If one is encountered it is simply skipped.
 40535 */
 40536 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
 40537   i64 szJ;                 /* Effective size of the main journal */
 40538   i64 iHdrOff;             /* End of first segment of main-journal records */
 40539   int rc = SQLITE_OK;      /* Return code */
 40540   Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
 40542   assert( pPager->eState!=PAGER_ERROR );
 40543   assert( pPager->eState>=PAGER_WRITER_LOCKED );
 40545   /* Allocate a bitvec to use to store the set of pages rolled back */
 40546   if( pSavepoint ){
 40547     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
 40548     if( !pDone ){
 40549       return SQLITE_NOMEM;
 40553   /* Set the database size back to the value it was before the savepoint 
 40554   ** being reverted was opened.
 40555   */
 40556   pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
 40557   pPager->changeCountDone = pPager->tempFile;
 40559   if( !pSavepoint && pagerUseWal(pPager) ){
 40560     return pagerRollbackWal(pPager);
 40563   /* Use pPager->journalOff as the effective size of the main rollback
 40564   ** journal.  The actual file might be larger than this in
 40565   ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
 40566   ** past pPager->journalOff is off-limits to us.
 40567   */
 40568   szJ = pPager->journalOff;
 40569   assert( pagerUseWal(pPager)==0 || szJ==0 );
 40571   /* Begin by rolling back records from the main journal starting at
 40572   ** PagerSavepoint.iOffset and continuing to the next journal header.
 40573   ** There might be records in the main journal that have a page number
 40574   ** greater than the current database size (pPager->dbSize) but those
 40575   ** will be skipped automatically.  Pages are added to pDone as they
 40576   ** are played back.
 40577   */
 40578   if( pSavepoint && !pagerUseWal(pPager) ){
 40579     iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
 40580     pPager->journalOff = pSavepoint->iOffset;
 40581     while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
 40582       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
 40584     assert( rc!=SQLITE_DONE );
 40585   }else{
 40586     pPager->journalOff = 0;
 40589   /* Continue rolling back records out of the main journal starting at
 40590   ** the first journal header seen and continuing until the effective end
 40591   ** of the main journal file.  Continue to skip out-of-range pages and
 40592   ** continue adding pages rolled back to pDone.
 40593   */
 40594   while( rc==SQLITE_OK && pPager->journalOff<szJ ){
 40595     u32 ii;            /* Loop counter */
 40596     u32 nJRec = 0;     /* Number of Journal Records */
 40597     u32 dummy;
 40598     rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
 40599     assert( rc!=SQLITE_DONE );
 40601     /*
 40602     ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
 40603     ** test is related to ticket #2565.  See the discussion in the
 40604     ** pager_playback() function for additional information.
 40605     */
 40606     if( nJRec==0 
 40607      && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
 40608     ){
 40609       nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
 40611     for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
 40612       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
 40614     assert( rc!=SQLITE_DONE );
 40616   assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
 40618   /* Finally,  rollback pages from the sub-journal.  Page that were
 40619   ** previously rolled back out of the main journal (and are hence in pDone)
 40620   ** will be skipped.  Out-of-range pages are also skipped.
 40621   */
 40622   if( pSavepoint ){
 40623     u32 ii;            /* Loop counter */
 40624     i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
 40626     if( pagerUseWal(pPager) ){
 40627       rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
 40629     for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
 40630       assert( offset==(i64)ii*(4+pPager->pageSize) );
 40631       rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
 40633     assert( rc!=SQLITE_DONE );
 40636   sqlite3BitvecDestroy(pDone);
 40637   if( rc==SQLITE_OK ){
 40638     pPager->journalOff = szJ;
 40641   return rc;
 40644 /*
 40645 ** Change the maximum number of in-memory pages that are allowed.
 40646 */
 40647 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
 40648   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
 40651 /*
 40652 ** Free as much memory as possible from the pager.
 40653 */
 40654 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
 40655   sqlite3PcacheShrink(pPager->pPCache);
 40658 /*
 40659 ** Adjust the robustness of the database to damage due to OS crashes
 40660 ** or power failures by changing the number of syncs()s when writing
 40661 ** the rollback journal.  There are three levels:
 40662 **
 40663 **    OFF       sqlite3OsSync() is never called.  This is the default
 40664 **              for temporary and transient files.
 40665 **
 40666 **    NORMAL    The journal is synced once before writes begin on the
 40667 **              database.  This is normally adequate protection, but
 40668 **              it is theoretically possible, though very unlikely,
 40669 **              that an inopertune power failure could leave the journal
 40670 **              in a state which would cause damage to the database
 40671 **              when it is rolled back.
 40672 **
 40673 **    FULL      The journal is synced twice before writes begin on the
 40674 **              database (with some additional information - the nRec field
 40675 **              of the journal header - being written in between the two
 40676 **              syncs).  If we assume that writing a
 40677 **              single disk sector is atomic, then this mode provides
 40678 **              assurance that the journal will not be corrupted to the
 40679 **              point of causing damage to the database during rollback.
 40680 **
 40681 ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
 40682 ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
 40683 ** prior to the start of checkpoint and that the database file is synced
 40684 ** at the conclusion of the checkpoint if the entire content of the WAL
 40685 ** was written back into the database.  But no sync operations occur for
 40686 ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
 40687 ** file is synced following each commit operation, in addition to the
 40688 ** syncs associated with NORMAL.
 40689 **
 40690 ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
 40691 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
 40692 ** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
 40693 ** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
 40694 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
 40695 ** synchronous=FULL versus synchronous=NORMAL setting determines when
 40696 ** the xSync primitive is called and is relevant to all platforms.
 40697 **
 40698 ** Numeric values associated with these states are OFF==1, NORMAL=2,
 40699 ** and FULL=3.
 40700 */
 40701 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 40702 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
 40703   Pager *pPager,        /* The pager to set safety level for */
 40704   int level,            /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */  
 40705   int bFullFsync,       /* PRAGMA fullfsync */
 40706   int bCkptFullFsync    /* PRAGMA checkpoint_fullfsync */
 40707 ){
 40708   assert( level>=1 && level<=3 );
 40709   pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
 40710   pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
 40711   if( pPager->noSync ){
 40712     pPager->syncFlags = 0;
 40713     pPager->ckptSyncFlags = 0;
 40714   }else if( bFullFsync ){
 40715     pPager->syncFlags = SQLITE_SYNC_FULL;
 40716     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 40717   }else if( bCkptFullFsync ){
 40718     pPager->syncFlags = SQLITE_SYNC_NORMAL;
 40719     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 40720   }else{
 40721     pPager->syncFlags = SQLITE_SYNC_NORMAL;
 40722     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
 40724   pPager->walSyncFlags = pPager->syncFlags;
 40725   if( pPager->fullSync ){
 40726     pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
 40729 #endif
 40731 /*
 40732 ** The following global variable is incremented whenever the library
 40733 ** attempts to open a temporary file.  This information is used for
 40734 ** testing and analysis only.  
 40735 */
 40736 #ifdef SQLITE_TEST
 40737 SQLITE_API int sqlite3_opentemp_count = 0;
 40738 #endif
 40740 /*
 40741 ** Open a temporary file.
 40742 **
 40743 ** Write the file descriptor into *pFile. Return SQLITE_OK on success 
 40744 ** or some other error code if we fail. The OS will automatically 
 40745 ** delete the temporary file when it is closed.
 40746 **
 40747 ** The flags passed to the VFS layer xOpen() call are those specified
 40748 ** by parameter vfsFlags ORed with the following:
 40749 **
 40750 **     SQLITE_OPEN_READWRITE
 40751 **     SQLITE_OPEN_CREATE
 40752 **     SQLITE_OPEN_EXCLUSIVE
 40753 **     SQLITE_OPEN_DELETEONCLOSE
 40754 */
 40755 static int pagerOpentemp(
 40756   Pager *pPager,        /* The pager object */
 40757   sqlite3_file *pFile,  /* Write the file descriptor here */
 40758   int vfsFlags          /* Flags passed through to the VFS */
 40759 ){
 40760   int rc;               /* Return code */
 40762 #ifdef SQLITE_TEST
 40763   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
 40764 #endif
 40766   vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
 40767             SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
 40768   rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
 40769   assert( rc!=SQLITE_OK || isOpen(pFile) );
 40770   return rc;
 40773 /*
 40774 ** Set the busy handler function.
 40775 **
 40776 ** The pager invokes the busy-handler if sqlite3OsLock() returns 
 40777 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
 40778 ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE 
 40779 ** lock. It does *not* invoke the busy handler when upgrading from
 40780 ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
 40781 ** (which occurs during hot-journal rollback). Summary:
 40782 **
 40783 **   Transition                        | Invokes xBusyHandler
 40784 **   --------------------------------------------------------
 40785 **   NO_LOCK       -> SHARED_LOCK      | Yes
 40786 **   SHARED_LOCK   -> RESERVED_LOCK    | No
 40787 **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
 40788 **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
 40789 **
 40790 ** If the busy-handler callback returns non-zero, the lock is 
 40791 ** retried. If it returns zero, then the SQLITE_BUSY error is
 40792 ** returned to the caller of the pager API function.
 40793 */
 40794 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
 40795   Pager *pPager,                       /* Pager object */
 40796   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
 40797   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
 40798 ){
 40799   pPager->xBusyHandler = xBusyHandler;
 40800   pPager->pBusyHandlerArg = pBusyHandlerArg;
 40802   if( isOpen(pPager->fd) ){
 40803     void **ap = (void **)&pPager->xBusyHandler;
 40804     assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
 40805     assert( ap[1]==pBusyHandlerArg );
 40806     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 40810 /*
 40811 ** Change the page size used by the Pager object. The new page size 
 40812 ** is passed in *pPageSize.
 40813 **
 40814 ** If the pager is in the error state when this function is called, it
 40815 ** is a no-op. The value returned is the error state error code (i.e. 
 40816 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
 40817 **
 40818 ** Otherwise, if all of the following are true:
 40819 **
 40820 **   * the new page size (value of *pPageSize) is valid (a power 
 40821 **     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
 40822 **
 40823 **   * there are no outstanding page references, and
 40824 **
 40825 **   * the database is either not an in-memory database or it is
 40826 **     an in-memory database that currently consists of zero pages.
 40827 **
 40828 ** then the pager object page size is set to *pPageSize.
 40829 **
 40830 ** If the page size is changed, then this function uses sqlite3PagerMalloc() 
 40831 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt 
 40832 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged. 
 40833 ** In all other cases, SQLITE_OK is returned.
 40834 **
 40835 ** If the page size is not changed, either because one of the enumerated
 40836 ** conditions above is not true, the pager was in error state when this
 40837 ** function was called, or because the memory allocation attempt failed, 
 40838 ** then *pPageSize is set to the old, retained page size before returning.
 40839 */
 40840 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
 40841   int rc = SQLITE_OK;
 40843   /* It is not possible to do a full assert_pager_state() here, as this
 40844   ** function may be called from within PagerOpen(), before the state
 40845   ** of the Pager object is internally consistent.
 40846   **
 40847   ** At one point this function returned an error if the pager was in 
 40848   ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
 40849   ** there is at least one outstanding page reference, this function
 40850   ** is a no-op for that case anyhow.
 40851   */
 40853   u32 pageSize = *pPageSize;
 40854   assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
 40855   if( (pPager->memDb==0 || pPager->dbSize==0)
 40856    && sqlite3PcacheRefCount(pPager->pPCache)==0 
 40857    && pageSize && pageSize!=(u32)pPager->pageSize 
 40858   ){
 40859     char *pNew = NULL;             /* New temp space */
 40860     i64 nByte = 0;
 40862     if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
 40863       rc = sqlite3OsFileSize(pPager->fd, &nByte);
 40865     if( rc==SQLITE_OK ){
 40866       pNew = (char *)sqlite3PageMalloc(pageSize);
 40867       if( !pNew ) rc = SQLITE_NOMEM;
 40870     if( rc==SQLITE_OK ){
 40871       pager_reset(pPager);
 40872       pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
 40873       pPager->pageSize = pageSize;
 40874       sqlite3PageFree(pPager->pTmpSpace);
 40875       pPager->pTmpSpace = pNew;
 40876       sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
 40880   *pPageSize = pPager->pageSize;
 40881   if( rc==SQLITE_OK ){
 40882     if( nReserve<0 ) nReserve = pPager->nReserve;
 40883     assert( nReserve>=0 && nReserve<1000 );
 40884     pPager->nReserve = (i16)nReserve;
 40885     pagerReportSize(pPager);
 40887   return rc;
 40890 /*
 40891 ** Return a pointer to the "temporary page" buffer held internally
 40892 ** by the pager.  This is a buffer that is big enough to hold the
 40893 ** entire content of a database page.  This buffer is used internally
 40894 ** during rollback and will be overwritten whenever a rollback
 40895 ** occurs.  But other modules are free to use it too, as long as
 40896 ** no rollbacks are happening.
 40897 */
 40898 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
 40899   return pPager->pTmpSpace;
 40902 /*
 40903 ** Attempt to set the maximum database page count if mxPage is positive. 
 40904 ** Make no changes if mxPage is zero or negative.  And never reduce the
 40905 ** maximum page count below the current size of the database.
 40906 **
 40907 ** Regardless of mxPage, return the current maximum page count.
 40908 */
 40909 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
 40910   if( mxPage>0 ){
 40911     pPager->mxPgno = mxPage;
 40913   assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
 40914   assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
 40915   return pPager->mxPgno;
 40918 /*
 40919 ** The following set of routines are used to disable the simulated
 40920 ** I/O error mechanism.  These routines are used to avoid simulated
 40921 ** errors in places where we do not care about errors.
 40922 **
 40923 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
 40924 ** and generate no code.
 40925 */
 40926 #ifdef SQLITE_TEST
 40927 SQLITE_API extern int sqlite3_io_error_pending;
 40928 SQLITE_API extern int sqlite3_io_error_hit;
 40929 static int saved_cnt;
 40930 void disable_simulated_io_errors(void){
 40931   saved_cnt = sqlite3_io_error_pending;
 40932   sqlite3_io_error_pending = -1;
 40934 void enable_simulated_io_errors(void){
 40935   sqlite3_io_error_pending = saved_cnt;
 40937 #else
 40938 # define disable_simulated_io_errors()
 40939 # define enable_simulated_io_errors()
 40940 #endif
 40942 /*
 40943 ** Read the first N bytes from the beginning of the file into memory
 40944 ** that pDest points to. 
 40945 **
 40946 ** If the pager was opened on a transient file (zFilename==""), or
 40947 ** opened on a file less than N bytes in size, the output buffer is
 40948 ** zeroed and SQLITE_OK returned. The rationale for this is that this 
 40949 ** function is used to read database headers, and a new transient or
 40950 ** zero sized database has a header than consists entirely of zeroes.
 40951 **
 40952 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
 40953 ** the error code is returned to the caller and the contents of the
 40954 ** output buffer undefined.
 40955 */
 40956 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
 40957   int rc = SQLITE_OK;
 40958   memset(pDest, 0, N);
 40959   assert( isOpen(pPager->fd) || pPager->tempFile );
 40961   /* This routine is only called by btree immediately after creating
 40962   ** the Pager object.  There has not been an opportunity to transition
 40963   ** to WAL mode yet.
 40964   */
 40965   assert( !pagerUseWal(pPager) );
 40967   if( isOpen(pPager->fd) ){
 40968     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
 40969     rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
 40970     if( rc==SQLITE_IOERR_SHORT_READ ){
 40971       rc = SQLITE_OK;
 40974   return rc;
 40977 /*
 40978 ** This function may only be called when a read-transaction is open on
 40979 ** the pager. It returns the total number of pages in the database.
 40980 **
 40981 ** However, if the file is between 1 and <page-size> bytes in size, then 
 40982 ** this is considered a 1 page file.
 40983 */
 40984 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
 40985   assert( pPager->eState>=PAGER_READER );
 40986   assert( pPager->eState!=PAGER_WRITER_FINISHED );
 40987   *pnPage = (int)pPager->dbSize;
 40991 /*
 40992 ** Try to obtain a lock of type locktype on the database file. If
 40993 ** a similar or greater lock is already held, this function is a no-op
 40994 ** (returning SQLITE_OK immediately).
 40995 **
 40996 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke 
 40997 ** the busy callback if the lock is currently not available. Repeat 
 40998 ** until the busy callback returns false or until the attempt to 
 40999 ** obtain the lock succeeds.
 41000 **
 41001 ** Return SQLITE_OK on success and an error code if we cannot obtain
 41002 ** the lock. If the lock is obtained successfully, set the Pager.state 
 41003 ** variable to locktype before returning.
 41004 */
 41005 static int pager_wait_on_lock(Pager *pPager, int locktype){
 41006   int rc;                              /* Return code */
 41008   /* Check that this is either a no-op (because the requested lock is 
 41009   ** already held, or one of the transistions that the busy-handler
 41010   ** may be invoked during, according to the comment above
 41011   ** sqlite3PagerSetBusyhandler().
 41012   */
 41013   assert( (pPager->eLock>=locktype)
 41014        || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
 41015        || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
 41016   );
 41018   do {
 41019     rc = pagerLockDb(pPager, locktype);
 41020   }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
 41021   return rc;
 41024 /*
 41025 ** Function assertTruncateConstraint(pPager) checks that one of the 
 41026 ** following is true for all dirty pages currently in the page-cache:
 41027 **
 41028 **   a) The page number is less than or equal to the size of the 
 41029 **      current database image, in pages, OR
 41030 **
 41031 **   b) if the page content were written at this time, it would not
 41032 **      be necessary to write the current content out to the sub-journal
 41033 **      (as determined by function subjRequiresPage()).
 41034 **
 41035 ** If the condition asserted by this function were not true, and the
 41036 ** dirty page were to be discarded from the cache via the pagerStress()
 41037 ** routine, pagerStress() would not write the current page content to
 41038 ** the database file. If a savepoint transaction were rolled back after
 41039 ** this happened, the correct behaviour would be to restore the current
 41040 ** content of the page. However, since this content is not present in either
 41041 ** the database file or the portion of the rollback journal and 
 41042 ** sub-journal rolled back the content could not be restored and the
 41043 ** database image would become corrupt. It is therefore fortunate that 
 41044 ** this circumstance cannot arise.
 41045 */
 41046 #if defined(SQLITE_DEBUG)
 41047 static void assertTruncateConstraintCb(PgHdr *pPg){
 41048   assert( pPg->flags&PGHDR_DIRTY );
 41049   assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
 41051 static void assertTruncateConstraint(Pager *pPager){
 41052   sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
 41054 #else
 41055 # define assertTruncateConstraint(pPager)
 41056 #endif
 41058 /*
 41059 ** Truncate the in-memory database file image to nPage pages. This 
 41060 ** function does not actually modify the database file on disk. It 
 41061 ** just sets the internal state of the pager object so that the 
 41062 ** truncation will be done when the current transaction is committed.
 41063 */
 41064 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
 41065   assert( pPager->dbSize>=nPage );
 41066   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
 41067   pPager->dbSize = nPage;
 41068   assertTruncateConstraint(pPager);
 41072 /*
 41073 ** This function is called before attempting a hot-journal rollback. It
 41074 ** syncs the journal file to disk, then sets pPager->journalHdr to the
 41075 ** size of the journal file so that the pager_playback() routine knows
 41076 ** that the entire journal file has been synced.
 41077 **
 41078 ** Syncing a hot-journal to disk before attempting to roll it back ensures 
 41079 ** that if a power-failure occurs during the rollback, the process that
 41080 ** attempts rollback following system recovery sees the same journal
 41081 ** content as this process.
 41082 **
 41083 ** If everything goes as planned, SQLITE_OK is returned. Otherwise, 
 41084 ** an SQLite error code.
 41085 */
 41086 static int pagerSyncHotJournal(Pager *pPager){
 41087   int rc = SQLITE_OK;
 41088   if( !pPager->noSync ){
 41089     rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
 41091   if( rc==SQLITE_OK ){
 41092     rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
 41094   return rc;
 41097 /*
 41098 ** Shutdown the page cache.  Free all memory and close all files.
 41099 **
 41100 ** If a transaction was in progress when this routine is called, that
 41101 ** transaction is rolled back.  All outstanding pages are invalidated
 41102 ** and their memory is freed.  Any attempt to use a page associated
 41103 ** with this page cache after this function returns will likely
 41104 ** result in a coredump.
 41105 **
 41106 ** This function always succeeds. If a transaction is active an attempt
 41107 ** is made to roll it back. If an error occurs during the rollback 
 41108 ** a hot journal may be left in the filesystem but no error is returned
 41109 ** to the caller.
 41110 */
 41111 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
 41112   u8 *pTmp = (u8 *)pPager->pTmpSpace;
 41114   assert( assert_pager_state(pPager) );
 41115   disable_simulated_io_errors();
 41116   sqlite3BeginBenignMalloc();
 41117   /* pPager->errCode = 0; */
 41118   pPager->exclusiveMode = 0;
 41119 #ifndef SQLITE_OMIT_WAL
 41120   sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
 41121   pPager->pWal = 0;
 41122 #endif
 41123   pager_reset(pPager);
 41124   if( MEMDB ){
 41125     pager_unlock(pPager);
 41126   }else{
 41127     /* If it is open, sync the journal file before calling UnlockAndRollback.
 41128     ** If this is not done, then an unsynced portion of the open journal 
 41129     ** file may be played back into the database. If a power failure occurs 
 41130     ** while this is happening, the database could become corrupt.
 41131     **
 41132     ** If an error occurs while trying to sync the journal, shift the pager
 41133     ** into the ERROR state. This causes UnlockAndRollback to unlock the
 41134     ** database and close the journal file without attempting to roll it
 41135     ** back or finalize it. The next database user will have to do hot-journal
 41136     ** rollback before accessing the database file.
 41137     */
 41138     if( isOpen(pPager->jfd) ){
 41139       pager_error(pPager, pagerSyncHotJournal(pPager));
 41141     pagerUnlockAndRollback(pPager);
 41143   sqlite3EndBenignMalloc();
 41144   enable_simulated_io_errors();
 41145   PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
 41146   IOTRACE(("CLOSE %p\n", pPager))
 41147   sqlite3OsClose(pPager->jfd);
 41148   sqlite3OsClose(pPager->fd);
 41149   sqlite3PageFree(pTmp);
 41150   sqlite3PcacheClose(pPager->pPCache);
 41152 #ifdef SQLITE_HAS_CODEC
 41153   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
 41154 #endif
 41156   assert( !pPager->aSavepoint && !pPager->pInJournal );
 41157   assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
 41159   sqlite3_free(pPager);
 41160   return SQLITE_OK;
 41163 #if !defined(NDEBUG) || defined(SQLITE_TEST)
 41164 /*
 41165 ** Return the page number for page pPg.
 41166 */
 41167 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
 41168   return pPg->pgno;
 41170 #endif
 41172 /*
 41173 ** Increment the reference count for page pPg.
 41174 */
 41175 SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
 41176   sqlite3PcacheRef(pPg);
 41179 /*
 41180 ** Sync the journal. In other words, make sure all the pages that have
 41181 ** been written to the journal have actually reached the surface of the
 41182 ** disk and can be restored in the event of a hot-journal rollback.
 41183 **
 41184 ** If the Pager.noSync flag is set, then this function is a no-op.
 41185 ** Otherwise, the actions required depend on the journal-mode and the 
 41186 ** device characteristics of the file-system, as follows:
 41187 **
 41188 **   * If the journal file is an in-memory journal file, no action need
 41189 **     be taken.
 41190 **
 41191 **   * Otherwise, if the device does not support the SAFE_APPEND property,
 41192 **     then the nRec field of the most recently written journal header
 41193 **     is updated to contain the number of journal records that have
 41194 **     been written following it. If the pager is operating in full-sync
 41195 **     mode, then the journal file is synced before this field is updated.
 41196 **
 41197 **   * If the device does not support the SEQUENTIAL property, then 
 41198 **     journal file is synced.
 41199 **
 41200 ** Or, in pseudo-code:
 41201 **
 41202 **   if( NOT <in-memory journal> ){
 41203 **     if( NOT SAFE_APPEND ){
 41204 **       if( <full-sync mode> ) xSync(<journal file>);
 41205 **       <update nRec field>
 41206 **     } 
 41207 **     if( NOT SEQUENTIAL ) xSync(<journal file>);
 41208 **   }
 41209 **
 41210 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every 
 41211 ** page currently held in memory before returning SQLITE_OK. If an IO
 41212 ** error is encountered, then the IO error code is returned to the caller.
 41213 */
 41214 static int syncJournal(Pager *pPager, int newHdr){
 41215   int rc;                         /* Return code */
 41217   assert( pPager->eState==PAGER_WRITER_CACHEMOD
 41218        || pPager->eState==PAGER_WRITER_DBMOD
 41219   );
 41220   assert( assert_pager_state(pPager) );
 41221   assert( !pagerUseWal(pPager) );
 41223   rc = sqlite3PagerExclusiveLock(pPager);
 41224   if( rc!=SQLITE_OK ) return rc;
 41226   if( !pPager->noSync ){
 41227     assert( !pPager->tempFile );
 41228     if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
 41229       const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
 41230       assert( isOpen(pPager->jfd) );
 41232       if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
 41233         /* This block deals with an obscure problem. If the last connection
 41234         ** that wrote to this database was operating in persistent-journal
 41235         ** mode, then the journal file may at this point actually be larger
 41236         ** than Pager.journalOff bytes. If the next thing in the journal
 41237         ** file happens to be a journal-header (written as part of the
 41238         ** previous connection's transaction), and a crash or power-failure 
 41239         ** occurs after nRec is updated but before this connection writes 
 41240         ** anything else to the journal file (or commits/rolls back its 
 41241         ** transaction), then SQLite may become confused when doing the 
 41242         ** hot-journal rollback following recovery. It may roll back all
 41243         ** of this connections data, then proceed to rolling back the old,
 41244         ** out-of-date data that follows it. Database corruption.
 41245         **
 41246         ** To work around this, if the journal file does appear to contain
 41247         ** a valid header following Pager.journalOff, then write a 0x00
 41248         ** byte to the start of it to prevent it from being recognized.
 41249         **
 41250         ** Variable iNextHdrOffset is set to the offset at which this
 41251         ** problematic header will occur, if it exists. aMagic is used 
 41252         ** as a temporary buffer to inspect the first couple of bytes of
 41253         ** the potential journal header.
 41254         */
 41255         i64 iNextHdrOffset;
 41256         u8 aMagic[8];
 41257         u8 zHeader[sizeof(aJournalMagic)+4];
 41259         memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
 41260         put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
 41262         iNextHdrOffset = journalHdrOffset(pPager);
 41263         rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
 41264         if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
 41265           static const u8 zerobyte = 0;
 41266           rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
 41268         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
 41269           return rc;
 41272         /* Write the nRec value into the journal file header. If in
 41273         ** full-synchronous mode, sync the journal first. This ensures that
 41274         ** all data has really hit the disk before nRec is updated to mark
 41275         ** it as a candidate for rollback.
 41276         **
 41277         ** This is not required if the persistent media supports the
 41278         ** SAFE_APPEND property. Because in this case it is not possible 
 41279         ** for garbage data to be appended to the file, the nRec field
 41280         ** is populated with 0xFFFFFFFF when the journal header is written
 41281         ** and never needs to be updated.
 41282         */
 41283         if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
 41284           PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
 41285           IOTRACE(("JSYNC %p\n", pPager))
 41286           rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
 41287           if( rc!=SQLITE_OK ) return rc;
 41289         IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
 41290         rc = sqlite3OsWrite(
 41291             pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
 41292         );
 41293         if( rc!=SQLITE_OK ) return rc;
 41295       if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
 41296         PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
 41297         IOTRACE(("JSYNC %p\n", pPager))
 41298         rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| 
 41299           (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
 41300         );
 41301         if( rc!=SQLITE_OK ) return rc;
 41304       pPager->journalHdr = pPager->journalOff;
 41305       if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
 41306         pPager->nRec = 0;
 41307         rc = writeJournalHdr(pPager);
 41308         if( rc!=SQLITE_OK ) return rc;
 41310     }else{
 41311       pPager->journalHdr = pPager->journalOff;
 41315   /* Unless the pager is in noSync mode, the journal file was just 
 41316   ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on 
 41317   ** all pages.
 41318   */
 41319   sqlite3PcacheClearSyncFlags(pPager->pPCache);
 41320   pPager->eState = PAGER_WRITER_DBMOD;
 41321   assert( assert_pager_state(pPager) );
 41322   return SQLITE_OK;
 41325 /*
 41326 ** The argument is the first in a linked list of dirty pages connected
 41327 ** by the PgHdr.pDirty pointer. This function writes each one of the
 41328 ** in-memory pages in the list to the database file. The argument may
 41329 ** be NULL, representing an empty list. In this case this function is
 41330 ** a no-op.
 41331 **
 41332 ** The pager must hold at least a RESERVED lock when this function
 41333 ** is called. Before writing anything to the database file, this lock
 41334 ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
 41335 ** SQLITE_BUSY is returned and no data is written to the database file.
 41336 ** 
 41337 ** If the pager is a temp-file pager and the actual file-system file
 41338 ** is not yet open, it is created and opened before any data is 
 41339 ** written out.
 41340 **
 41341 ** Once the lock has been upgraded and, if necessary, the file opened,
 41342 ** the pages are written out to the database file in list order. Writing
 41343 ** a page is skipped if it meets either of the following criteria:
 41344 **
 41345 **   * The page number is greater than Pager.dbSize, or
 41346 **   * The PGHDR_DONT_WRITE flag is set on the page.
 41347 **
 41348 ** If writing out a page causes the database file to grow, Pager.dbFileSize
 41349 ** is updated accordingly. If page 1 is written out, then the value cached
 41350 ** in Pager.dbFileVers[] is updated to match the new value stored in
 41351 ** the database file.
 41352 **
 41353 ** If everything is successful, SQLITE_OK is returned. If an IO error 
 41354 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
 41355 ** be obtained, SQLITE_BUSY is returned.
 41356 */
 41357 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
 41358   int rc = SQLITE_OK;                  /* Return code */
 41360   /* This function is only called for rollback pagers in WRITER_DBMOD state. */
 41361   assert( !pagerUseWal(pPager) );
 41362   assert( pPager->eState==PAGER_WRITER_DBMOD );
 41363   assert( pPager->eLock==EXCLUSIVE_LOCK );
 41365   /* If the file is a temp-file has not yet been opened, open it now. It
 41366   ** is not possible for rc to be other than SQLITE_OK if this branch
 41367   ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
 41368   */
 41369   if( !isOpen(pPager->fd) ){
 41370     assert( pPager->tempFile && rc==SQLITE_OK );
 41371     rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
 41374   /* Before the first write, give the VFS a hint of what the final
 41375   ** file size will be.
 41376   */
 41377   assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
 41378   if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
 41379     sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
 41380     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
 41381     pPager->dbHintSize = pPager->dbSize;
 41384   while( rc==SQLITE_OK && pList ){
 41385     Pgno pgno = pList->pgno;
 41387     /* If there are dirty pages in the page cache with page numbers greater
 41388     ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
 41389     ** make the file smaller (presumably by auto-vacuum code). Do not write
 41390     ** any such pages to the file.
 41391     **
 41392     ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
 41393     ** set (set by sqlite3PagerDontWrite()).
 41394     */
 41395     if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
 41396       i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
 41397       char *pData;                                   /* Data to write */    
 41399       assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
 41400       if( pList->pgno==1 ) pager_write_changecounter(pList);
 41402       /* Encode the database */
 41403       CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
 41405       /* Write out the page data. */
 41406       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
 41408       /* If page 1 was just written, update Pager.dbFileVers to match
 41409       ** the value now stored in the database file. If writing this 
 41410       ** page caused the database file to grow, update dbFileSize. 
 41411       */
 41412       if( pgno==1 ){
 41413         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
 41415       if( pgno>pPager->dbFileSize ){
 41416         pPager->dbFileSize = pgno;
 41418       pPager->aStat[PAGER_STAT_WRITE]++;
 41420       /* Update any backup objects copying the contents of this pager. */
 41421       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
 41423       PAGERTRACE(("STORE %d page %d hash(%08x)\n",
 41424                    PAGERID(pPager), pgno, pager_pagehash(pList)));
 41425       IOTRACE(("PGOUT %p %d\n", pPager, pgno));
 41426       PAGER_INCR(sqlite3_pager_writedb_count);
 41427     }else{
 41428       PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
 41430     pager_set_pagehash(pList);
 41431     pList = pList->pDirty;
 41434   return rc;
 41437 /*
 41438 ** Ensure that the sub-journal file is open. If it is already open, this 
 41439 ** function is a no-op.
 41440 **
 41441 ** SQLITE_OK is returned if everything goes according to plan. An 
 41442 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() 
 41443 ** fails.
 41444 */
 41445 static int openSubJournal(Pager *pPager){
 41446   int rc = SQLITE_OK;
 41447   if( !isOpen(pPager->sjfd) ){
 41448     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
 41449       sqlite3MemJournalOpen(pPager->sjfd);
 41450     }else{
 41451       rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
 41454   return rc;
 41457 /*
 41458 ** Append a record of the current state of page pPg to the sub-journal. 
 41459 ** It is the callers responsibility to use subjRequiresPage() to check 
 41460 ** that it is really required before calling this function.
 41461 **
 41462 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
 41463 ** for all open savepoints before returning.
 41464 **
 41465 ** This function returns SQLITE_OK if everything is successful, an IO
 41466 ** error code if the attempt to write to the sub-journal fails, or 
 41467 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
 41468 ** bitvec.
 41469 */
 41470 static int subjournalPage(PgHdr *pPg){
 41471   int rc = SQLITE_OK;
 41472   Pager *pPager = pPg->pPager;
 41473   if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
 41475     /* Open the sub-journal, if it has not already been opened */
 41476     assert( pPager->useJournal );
 41477     assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
 41478     assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
 41479     assert( pagerUseWal(pPager) 
 41480          || pageInJournal(pPg) 
 41481          || pPg->pgno>pPager->dbOrigSize 
 41482     );
 41483     rc = openSubJournal(pPager);
 41485     /* If the sub-journal was opened successfully (or was already open),
 41486     ** write the journal record into the file.  */
 41487     if( rc==SQLITE_OK ){
 41488       void *pData = pPg->pData;
 41489       i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
 41490       char *pData2;
 41492       CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
 41493       PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
 41494       rc = write32bits(pPager->sjfd, offset, pPg->pgno);
 41495       if( rc==SQLITE_OK ){
 41496         rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
 41500   if( rc==SQLITE_OK ){
 41501     pPager->nSubRec++;
 41502     assert( pPager->nSavepoint>0 );
 41503     rc = addToSavepointBitvecs(pPager, pPg->pgno);
 41505   return rc;
 41508 /*
 41509 ** This function is called by the pcache layer when it has reached some
 41510 ** soft memory limit. The first argument is a pointer to a Pager object
 41511 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
 41512 ** database). The second argument is a reference to a page that is 
 41513 ** currently dirty but has no outstanding references. The page
 41514 ** is always associated with the Pager object passed as the first 
 41515 ** argument.
 41516 **
 41517 ** The job of this function is to make pPg clean by writing its contents
 41518 ** out to the database file, if possible. This may involve syncing the
 41519 ** journal file. 
 41520 **
 41521 ** If successful, sqlite3PcacheMakeClean() is called on the page and
 41522 ** SQLITE_OK returned. If an IO error occurs while trying to make the
 41523 ** page clean, the IO error code is returned. If the page cannot be
 41524 ** made clean for some other reason, but no error occurs, then SQLITE_OK
 41525 ** is returned by sqlite3PcacheMakeClean() is not called.
 41526 */
 41527 static int pagerStress(void *p, PgHdr *pPg){
 41528   Pager *pPager = (Pager *)p;
 41529   int rc = SQLITE_OK;
 41531   assert( pPg->pPager==pPager );
 41532   assert( pPg->flags&PGHDR_DIRTY );
 41534   /* The doNotSyncSpill flag is set during times when doing a sync of
 41535   ** journal (and adding a new header) is not allowed.  This occurs
 41536   ** during calls to sqlite3PagerWrite() while trying to journal multiple
 41537   ** pages belonging to the same sector.
 41538   **
 41539   ** The doNotSpill flag inhibits all cache spilling regardless of whether
 41540   ** or not a sync is required.  This is set during a rollback.
 41541   **
 41542   ** Spilling is also prohibited when in an error state since that could
 41543   ** lead to database corruption.   In the current implementaton it 
 41544   ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
 41545   ** while in the error state, hence it is impossible for this routine to
 41546   ** be called in the error state.  Nevertheless, we include a NEVER()
 41547   ** test for the error state as a safeguard against future changes.
 41548   */
 41549   if( NEVER(pPager->errCode) ) return SQLITE_OK;
 41550   if( pPager->doNotSpill ) return SQLITE_OK;
 41551   if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
 41552     return SQLITE_OK;
 41555   pPg->pDirty = 0;
 41556   if( pagerUseWal(pPager) ){
 41557     /* Write a single frame for this page to the log. */
 41558     if( subjRequiresPage(pPg) ){ 
 41559       rc = subjournalPage(pPg); 
 41561     if( rc==SQLITE_OK ){
 41562       rc = pagerWalFrames(pPager, pPg, 0, 0);
 41564   }else{
 41566     /* Sync the journal file if required. */
 41567     if( pPg->flags&PGHDR_NEED_SYNC 
 41568      || pPager->eState==PAGER_WRITER_CACHEMOD
 41569     ){
 41570       rc = syncJournal(pPager, 1);
 41573     /* If the page number of this page is larger than the current size of
 41574     ** the database image, it may need to be written to the sub-journal.
 41575     ** This is because the call to pager_write_pagelist() below will not
 41576     ** actually write data to the file in this case.
 41577     **
 41578     ** Consider the following sequence of events:
 41579     **
 41580     **   BEGIN;
 41581     **     <journal page X>
 41582     **     <modify page X>
 41583     **     SAVEPOINT sp;
 41584     **       <shrink database file to Y pages>
 41585     **       pagerStress(page X)
 41586     **     ROLLBACK TO sp;
 41587     **
 41588     ** If (X>Y), then when pagerStress is called page X will not be written
 41589     ** out to the database file, but will be dropped from the cache. Then,
 41590     ** following the "ROLLBACK TO sp" statement, reading page X will read
 41591     ** data from the database file. This will be the copy of page X as it
 41592     ** was when the transaction started, not as it was when "SAVEPOINT sp"
 41593     ** was executed.
 41594     **
 41595     ** The solution is to write the current data for page X into the 
 41596     ** sub-journal file now (if it is not already there), so that it will
 41597     ** be restored to its current value when the "ROLLBACK TO sp" is 
 41598     ** executed.
 41599     */
 41600     if( NEVER(
 41601         rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
 41602     ) ){
 41603       rc = subjournalPage(pPg);
 41606     /* Write the contents of the page out to the database file. */
 41607     if( rc==SQLITE_OK ){
 41608       assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
 41609       rc = pager_write_pagelist(pPager, pPg);
 41613   /* Mark the page as clean. */
 41614   if( rc==SQLITE_OK ){
 41615     PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
 41616     sqlite3PcacheMakeClean(pPg);
 41619   return pager_error(pPager, rc); 
 41623 /*
 41624 ** Allocate and initialize a new Pager object and put a pointer to it
 41625 ** in *ppPager. The pager should eventually be freed by passing it
 41626 ** to sqlite3PagerClose().
 41627 **
 41628 ** The zFilename argument is the path to the database file to open.
 41629 ** If zFilename is NULL then a randomly-named temporary file is created
 41630 ** and used as the file to be cached. Temporary files are be deleted
 41631 ** automatically when they are closed. If zFilename is ":memory:" then 
 41632 ** all information is held in cache. It is never written to disk. 
 41633 ** This can be used to implement an in-memory database.
 41634 **
 41635 ** The nExtra parameter specifies the number of bytes of space allocated
 41636 ** along with each page reference. This space is available to the user
 41637 ** via the sqlite3PagerGetExtra() API.
 41638 **
 41639 ** The flags argument is used to specify properties that affect the
 41640 ** operation of the pager. It should be passed some bitwise combination
 41641 ** of the PAGER_* flags.
 41642 **
 41643 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
 41644 ** of the xOpen() method of the supplied VFS when opening files. 
 41645 **
 41646 ** If the pager object is allocated and the specified file opened 
 41647 ** successfully, SQLITE_OK is returned and *ppPager set to point to
 41648 ** the new pager object. If an error occurs, *ppPager is set to NULL
 41649 ** and error code returned. This function may return SQLITE_NOMEM
 41650 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or 
 41651 ** various SQLITE_IO_XXX errors.
 41652 */
 41653 SQLITE_PRIVATE int sqlite3PagerOpen(
 41654   sqlite3_vfs *pVfs,       /* The virtual file system to use */
 41655   Pager **ppPager,         /* OUT: Return the Pager structure here */
 41656   const char *zFilename,   /* Name of the database file to open */
 41657   int nExtra,              /* Extra bytes append to each in-memory page */
 41658   int flags,               /* flags controlling this file */
 41659   int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
 41660   void (*xReinit)(DbPage*) /* Function to reinitialize pages */
 41661 ){
 41662   u8 *pPtr;
 41663   Pager *pPager = 0;       /* Pager object to allocate and return */
 41664   int rc = SQLITE_OK;      /* Return code */
 41665   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
 41666   int memDb = 0;           /* True if this is an in-memory file */
 41667   int readOnly = 0;        /* True if this is a read-only file */
 41668   int journalFileSize;     /* Bytes to allocate for each journal fd */
 41669   char *zPathname = 0;     /* Full path to database file */
 41670   int nPathname = 0;       /* Number of bytes in zPathname */
 41671   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
 41672   int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
 41673   u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
 41674   const char *zUri = 0;    /* URI args to copy */
 41675   int nUri = 0;            /* Number of bytes of URI args at *zUri */
 41677   /* Figure out how much space is required for each journal file-handle
 41678   ** (there are two of them, the main journal and the sub-journal). This
 41679   ** is the maximum space required for an in-memory journal file handle 
 41680   ** and a regular journal file-handle. Note that a "regular journal-handle"
 41681   ** may be a wrapper capable of caching the first portion of the journal
 41682   ** file in memory to implement the atomic-write optimization (see 
 41683   ** source file journal.c).
 41684   */
 41685   if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
 41686     journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
 41687   }else{
 41688     journalFileSize = ROUND8(sqlite3MemJournalSize());
 41691   /* Set the output variable to NULL in case an error occurs. */
 41692   *ppPager = 0;
 41694 #ifndef SQLITE_OMIT_MEMORYDB
 41695   if( flags & PAGER_MEMORY ){
 41696     memDb = 1;
 41697     if( zFilename && zFilename[0] ){
 41698       zPathname = sqlite3DbStrDup(0, zFilename);
 41699       if( zPathname==0  ) return SQLITE_NOMEM;
 41700       nPathname = sqlite3Strlen30(zPathname);
 41701       zFilename = 0;
 41704 #endif
 41706   /* Compute and store the full pathname in an allocated buffer pointed
 41707   ** to by zPathname, length nPathname. Or, if this is a temporary file,
 41708   ** leave both nPathname and zPathname set to 0.
 41709   */
 41710   if( zFilename && zFilename[0] ){
 41711     const char *z;
 41712     nPathname = pVfs->mxPathname+1;
 41713     zPathname = sqlite3DbMallocRaw(0, nPathname*2);
 41714     if( zPathname==0 ){
 41715       return SQLITE_NOMEM;
 41717     zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
 41718     rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
 41719     nPathname = sqlite3Strlen30(zPathname);
 41720     z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
 41721     while( *z ){
 41722       z += sqlite3Strlen30(z)+1;
 41723       z += sqlite3Strlen30(z)+1;
 41725     nUri = (int)(&z[1] - zUri);
 41726     assert( nUri>=0 );
 41727     if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
 41728       /* This branch is taken when the journal path required by
 41729       ** the database being opened will be more than pVfs->mxPathname
 41730       ** bytes in length. This means the database cannot be opened,
 41731       ** as it will not be possible to open the journal file or even
 41732       ** check for a hot-journal before reading.
 41733       */
 41734       rc = SQLITE_CANTOPEN_BKPT;
 41736     if( rc!=SQLITE_OK ){
 41737       sqlite3DbFree(0, zPathname);
 41738       return rc;
 41742   /* Allocate memory for the Pager structure, PCache object, the
 41743   ** three file descriptors, the database file name and the journal 
 41744   ** file name. The layout in memory is as follows:
 41745   **
 41746   **     Pager object                    (sizeof(Pager) bytes)
 41747   **     PCache object                   (sqlite3PcacheSize() bytes)
 41748   **     Database file handle            (pVfs->szOsFile bytes)
 41749   **     Sub-journal file handle         (journalFileSize bytes)
 41750   **     Main journal file handle        (journalFileSize bytes)
 41751   **     Database file name              (nPathname+1 bytes)
 41752   **     Journal file name               (nPathname+8+1 bytes)
 41753   */
 41754   pPtr = (u8 *)sqlite3MallocZero(
 41755     ROUND8(sizeof(*pPager)) +      /* Pager structure */
 41756     ROUND8(pcacheSize) +           /* PCache object */
 41757     ROUND8(pVfs->szOsFile) +       /* The main db file */
 41758     journalFileSize * 2 +          /* The two journal files */ 
 41759     nPathname + 1 + nUri +         /* zFilename */
 41760     nPathname + 8 + 2              /* zJournal */
 41761 #ifndef SQLITE_OMIT_WAL
 41762     + nPathname + 4 + 2            /* zWal */
 41763 #endif
 41764   );
 41765   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
 41766   if( !pPtr ){
 41767     sqlite3DbFree(0, zPathname);
 41768     return SQLITE_NOMEM;
 41770   pPager =              (Pager*)(pPtr);
 41771   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
 41772   pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
 41773   pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
 41774   pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
 41775   pPager->zFilename =    (char*)(pPtr += journalFileSize);
 41776   assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
 41778   /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
 41779   if( zPathname ){
 41780     assert( nPathname>0 );
 41781     pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
 41782     memcpy(pPager->zFilename, zPathname, nPathname);
 41783     if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
 41784     memcpy(pPager->zJournal, zPathname, nPathname);
 41785     memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
 41786     sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
 41787 #ifndef SQLITE_OMIT_WAL
 41788     pPager->zWal = &pPager->zJournal[nPathname+8+1];
 41789     memcpy(pPager->zWal, zPathname, nPathname);
 41790     memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
 41791     sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
 41792 #endif
 41793     sqlite3DbFree(0, zPathname);
 41795   pPager->pVfs = pVfs;
 41796   pPager->vfsFlags = vfsFlags;
 41798   /* Open the pager file.
 41799   */
 41800   if( zFilename && zFilename[0] ){
 41801     int fout = 0;                    /* VFS flags returned by xOpen() */
 41802     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
 41803     assert( !memDb );
 41804     readOnly = (fout&SQLITE_OPEN_READONLY);
 41806     /* If the file was successfully opened for read/write access,
 41807     ** choose a default page size in case we have to create the
 41808     ** database file. The default page size is the maximum of:
 41809     **
 41810     **    + SQLITE_DEFAULT_PAGE_SIZE,
 41811     **    + The value returned by sqlite3OsSectorSize()
 41812     **    + The largest page size that can be written atomically.
 41813     */
 41814     if( rc==SQLITE_OK && !readOnly ){
 41815       setSectorSize(pPager);
 41816       assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
 41817       if( szPageDflt<pPager->sectorSize ){
 41818         if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
 41819           szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
 41820         }else{
 41821           szPageDflt = (u32)pPager->sectorSize;
 41824 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 41826         int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
 41827         int ii;
 41828         assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 41829         assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 41830         assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
 41831         for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
 41832           if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
 41833             szPageDflt = ii;
 41837 #endif
 41839   }else{
 41840     /* If a temporary file is requested, it is not opened immediately.
 41841     ** In this case we accept the default page size and delay actually
 41842     ** opening the file until the first call to OsWrite().
 41843     **
 41844     ** This branch is also run for an in-memory database. An in-memory
 41845     ** database is the same as a temp-file that is never written out to
 41846     ** disk and uses an in-memory rollback journal.
 41847     */ 
 41848     tempFile = 1;
 41849     pPager->eState = PAGER_READER;
 41850     pPager->eLock = EXCLUSIVE_LOCK;
 41851     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
 41854   /* The following call to PagerSetPagesize() serves to set the value of 
 41855   ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
 41856   */
 41857   if( rc==SQLITE_OK ){
 41858     assert( pPager->memDb==0 );
 41859     rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
 41860     testcase( rc!=SQLITE_OK );
 41863   /* If an error occurred in either of the blocks above, free the 
 41864   ** Pager structure and close the file.
 41865   */
 41866   if( rc!=SQLITE_OK ){
 41867     assert( !pPager->pTmpSpace );
 41868     sqlite3OsClose(pPager->fd);
 41869     sqlite3_free(pPager);
 41870     return rc;
 41873   /* Initialize the PCache object. */
 41874   assert( nExtra<1000 );
 41875   nExtra = ROUND8(nExtra);
 41876   sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
 41877                     !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
 41879   PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
 41880   IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
 41882   pPager->useJournal = (u8)useJournal;
 41883   /* pPager->stmtOpen = 0; */
 41884   /* pPager->stmtInUse = 0; */
 41885   /* pPager->nRef = 0; */
 41886   /* pPager->stmtSize = 0; */
 41887   /* pPager->stmtJSize = 0; */
 41888   /* pPager->nPage = 0; */
 41889   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
 41890   /* pPager->state = PAGER_UNLOCK; */
 41891 #if 0
 41892   assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
 41893 #endif
 41894   /* pPager->errMask = 0; */
 41895   pPager->tempFile = (u8)tempFile;
 41896   assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
 41897           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
 41898   assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
 41899   pPager->exclusiveMode = (u8)tempFile; 
 41900   pPager->changeCountDone = pPager->tempFile;
 41901   pPager->memDb = (u8)memDb;
 41902   pPager->readOnly = (u8)readOnly;
 41903   assert( useJournal || pPager->tempFile );
 41904   pPager->noSync = pPager->tempFile;
 41905   if( pPager->noSync ){
 41906     assert( pPager->fullSync==0 );
 41907     assert( pPager->syncFlags==0 );
 41908     assert( pPager->walSyncFlags==0 );
 41909     assert( pPager->ckptSyncFlags==0 );
 41910   }else{
 41911     pPager->fullSync = 1;
 41912     pPager->syncFlags = SQLITE_SYNC_NORMAL;
 41913     pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
 41914     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
 41916   /* pPager->pFirst = 0; */
 41917   /* pPager->pFirstSynced = 0; */
 41918   /* pPager->pLast = 0; */
 41919   pPager->nExtra = (u16)nExtra;
 41920   pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 41921   assert( isOpen(pPager->fd) || tempFile );
 41922   setSectorSize(pPager);
 41923   if( !useJournal ){
 41924     pPager->journalMode = PAGER_JOURNALMODE_OFF;
 41925   }else if( memDb ){
 41926     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 41928   /* pPager->xBusyHandler = 0; */
 41929   /* pPager->pBusyHandlerArg = 0; */
 41930   pPager->xReiniter = xReinit;
 41931   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
 41933   *ppPager = pPager;
 41934   return SQLITE_OK;
 41939 /*
 41940 ** This function is called after transitioning from PAGER_UNLOCK to
 41941 ** PAGER_SHARED state. It tests if there is a hot journal present in
 41942 ** the file-system for the given pager. A hot journal is one that 
 41943 ** needs to be played back. According to this function, a hot-journal
 41944 ** file exists if the following criteria are met:
 41945 **
 41946 **   * The journal file exists in the file system, and
 41947 **   * No process holds a RESERVED or greater lock on the database file, and
 41948 **   * The database file itself is greater than 0 bytes in size, and
 41949 **   * The first byte of the journal file exists and is not 0x00.
 41950 **
 41951 ** If the current size of the database file is 0 but a journal file
 41952 ** exists, that is probably an old journal left over from a prior
 41953 ** database with the same name. In this case the journal file is
 41954 ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
 41955 ** is returned.
 41956 **
 41957 ** This routine does not check if there is a master journal filename
 41958 ** at the end of the file. If there is, and that master journal file
 41959 ** does not exist, then the journal file is not really hot. In this
 41960 ** case this routine will return a false-positive. The pager_playback()
 41961 ** routine will discover that the journal file is not really hot and 
 41962 ** will not roll it back. 
 41963 **
 41964 ** If a hot-journal file is found to exist, *pExists is set to 1 and 
 41965 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
 41966 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
 41967 ** to determine whether or not a hot-journal file exists, the IO error
 41968 ** code is returned and the value of *pExists is undefined.
 41969 */
 41970 static int hasHotJournal(Pager *pPager, int *pExists){
 41971   sqlite3_vfs * const pVfs = pPager->pVfs;
 41972   int rc = SQLITE_OK;           /* Return code */
 41973   int exists = 1;               /* True if a journal file is present */
 41974   int jrnlOpen = !!isOpen(pPager->jfd);
 41976   assert( pPager->useJournal );
 41977   assert( isOpen(pPager->fd) );
 41978   assert( pPager->eState==PAGER_OPEN );
 41980   assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
 41981     SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
 41982   ));
 41984   *pExists = 0;
 41985   if( !jrnlOpen ){
 41986     rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
 41988   if( rc==SQLITE_OK && exists ){
 41989     int locked = 0;             /* True if some process holds a RESERVED lock */
 41991     /* Race condition here:  Another process might have been holding the
 41992     ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 
 41993     ** call above, but then delete the journal and drop the lock before
 41994     ** we get to the following sqlite3OsCheckReservedLock() call.  If that
 41995     ** is the case, this routine might think there is a hot journal when
 41996     ** in fact there is none.  This results in a false-positive which will
 41997     ** be dealt with by the playback routine.  Ticket #3883.
 41998     */
 41999     rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
 42000     if( rc==SQLITE_OK && !locked ){
 42001       Pgno nPage;                 /* Number of pages in database file */
 42003       /* Check the size of the database file. If it consists of 0 pages,
 42004       ** then delete the journal file. See the header comment above for 
 42005       ** the reasoning here.  Delete the obsolete journal file under
 42006       ** a RESERVED lock to avoid race conditions and to avoid violating
 42007       ** [H33020].
 42008       */
 42009       rc = pagerPagecount(pPager, &nPage);
 42010       if( rc==SQLITE_OK ){
 42011         if( nPage==0 ){
 42012           sqlite3BeginBenignMalloc();
 42013           if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
 42014             sqlite3OsDelete(pVfs, pPager->zJournal, 0);
 42015             if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
 42017           sqlite3EndBenignMalloc();
 42018         }else{
 42019           /* The journal file exists and no other connection has a reserved
 42020           ** or greater lock on the database file. Now check that there is
 42021           ** at least one non-zero bytes at the start of the journal file.
 42022           ** If there is, then we consider this journal to be hot. If not, 
 42023           ** it can be ignored.
 42024           */
 42025           if( !jrnlOpen ){
 42026             int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
 42027             rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
 42029           if( rc==SQLITE_OK ){
 42030             u8 first = 0;
 42031             rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
 42032             if( rc==SQLITE_IOERR_SHORT_READ ){
 42033               rc = SQLITE_OK;
 42035             if( !jrnlOpen ){
 42036               sqlite3OsClose(pPager->jfd);
 42038             *pExists = (first!=0);
 42039           }else if( rc==SQLITE_CANTOPEN ){
 42040             /* If we cannot open the rollback journal file in order to see if
 42041             ** its has a zero header, that might be due to an I/O error, or
 42042             ** it might be due to the race condition described above and in
 42043             ** ticket #3883.  Either way, assume that the journal is hot.
 42044             ** This might be a false positive.  But if it is, then the
 42045             ** automatic journal playback and recovery mechanism will deal
 42046             ** with it under an EXCLUSIVE lock where we do not need to
 42047             ** worry so much with race conditions.
 42048             */
 42049             *pExists = 1;
 42050             rc = SQLITE_OK;
 42057   return rc;
 42060 /*
 42061 ** This function is called to obtain a shared lock on the database file.
 42062 ** It is illegal to call sqlite3PagerAcquire() until after this function
 42063 ** has been successfully called. If a shared-lock is already held when
 42064 ** this function is called, it is a no-op.
 42065 **
 42066 ** The following operations are also performed by this function.
 42067 **
 42068 **   1) If the pager is currently in PAGER_OPEN state (no lock held
 42069 **      on the database file), then an attempt is made to obtain a
 42070 **      SHARED lock on the database file. Immediately after obtaining
 42071 **      the SHARED lock, the file-system is checked for a hot-journal,
 42072 **      which is played back if present. Following any hot-journal 
 42073 **      rollback, the contents of the cache are validated by checking
 42074 **      the 'change-counter' field of the database file header and
 42075 **      discarded if they are found to be invalid.
 42076 **
 42077 **   2) If the pager is running in exclusive-mode, and there are currently
 42078 **      no outstanding references to any pages, and is in the error state,
 42079 **      then an attempt is made to clear the error state by discarding
 42080 **      the contents of the page cache and rolling back any open journal
 42081 **      file.
 42082 **
 42083 ** If everything is successful, SQLITE_OK is returned. If an IO error 
 42084 ** occurs while locking the database, checking for a hot-journal file or 
 42085 ** rolling back a journal file, the IO error code is returned.
 42086 */
 42087 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
 42088   int rc = SQLITE_OK;                /* Return code */
 42090   /* This routine is only called from b-tree and only when there are no
 42091   ** outstanding pages. This implies that the pager state should either
 42092   ** be OPEN or READER. READER is only possible if the pager is or was in 
 42093   ** exclusive access mode.
 42094   */
 42095   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
 42096   assert( assert_pager_state(pPager) );
 42097   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
 42098   if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
 42100   if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
 42101     int bHotJournal = 1;          /* True if there exists a hot journal-file */
 42103     assert( !MEMDB );
 42105     rc = pager_wait_on_lock(pPager, SHARED_LOCK);
 42106     if( rc!=SQLITE_OK ){
 42107       assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
 42108       goto failed;
 42111     /* If a journal file exists, and there is no RESERVED lock on the
 42112     ** database file, then it either needs to be played back or deleted.
 42113     */
 42114     if( pPager->eLock<=SHARED_LOCK ){
 42115       rc = hasHotJournal(pPager, &bHotJournal);
 42117     if( rc!=SQLITE_OK ){
 42118       goto failed;
 42120     if( bHotJournal ){
 42121       /* Get an EXCLUSIVE lock on the database file. At this point it is
 42122       ** important that a RESERVED lock is not obtained on the way to the
 42123       ** EXCLUSIVE lock. If it were, another process might open the
 42124       ** database file, detect the RESERVED lock, and conclude that the
 42125       ** database is safe to read while this process is still rolling the 
 42126       ** hot-journal back.
 42127       ** 
 42128       ** Because the intermediate RESERVED lock is not requested, any
 42129       ** other process attempting to access the database file will get to 
 42130       ** this point in the code and fail to obtain its own EXCLUSIVE lock 
 42131       ** on the database file.
 42132       **
 42133       ** Unless the pager is in locking_mode=exclusive mode, the lock is
 42134       ** downgraded to SHARED_LOCK before this function returns.
 42135       */
 42136       rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 42137       if( rc!=SQLITE_OK ){
 42138         goto failed;
 42141       /* If it is not already open and the file exists on disk, open the 
 42142       ** journal for read/write access. Write access is required because 
 42143       ** in exclusive-access mode the file descriptor will be kept open 
 42144       ** and possibly used for a transaction later on. Also, write-access 
 42145       ** is usually required to finalize the journal in journal_mode=persist 
 42146       ** mode (and also for journal_mode=truncate on some systems).
 42147       **
 42148       ** If the journal does not exist, it usually means that some 
 42149       ** other connection managed to get in and roll it back before 
 42150       ** this connection obtained the exclusive lock above. Or, it 
 42151       ** may mean that the pager was in the error-state when this
 42152       ** function was called and the journal file does not exist.
 42153       */
 42154       if( !isOpen(pPager->jfd) ){
 42155         sqlite3_vfs * const pVfs = pPager->pVfs;
 42156         int bExists;              /* True if journal file exists */
 42157         rc = sqlite3OsAccess(
 42158             pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
 42159         if( rc==SQLITE_OK && bExists ){
 42160           int fout = 0;
 42161           int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
 42162           assert( !pPager->tempFile );
 42163           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
 42164           assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
 42165           if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
 42166             rc = SQLITE_CANTOPEN_BKPT;
 42167             sqlite3OsClose(pPager->jfd);
 42172       /* Playback and delete the journal.  Drop the database write
 42173       ** lock and reacquire the read lock. Purge the cache before
 42174       ** playing back the hot-journal so that we don't end up with
 42175       ** an inconsistent cache.  Sync the hot journal before playing
 42176       ** it back since the process that crashed and left the hot journal
 42177       ** probably did not sync it and we are required to always sync
 42178       ** the journal before playing it back.
 42179       */
 42180       if( isOpen(pPager->jfd) ){
 42181         assert( rc==SQLITE_OK );
 42182         rc = pagerSyncHotJournal(pPager);
 42183         if( rc==SQLITE_OK ){
 42184           rc = pager_playback(pPager, 1);
 42185           pPager->eState = PAGER_OPEN;
 42187       }else if( !pPager->exclusiveMode ){
 42188         pagerUnlockDb(pPager, SHARED_LOCK);
 42191       if( rc!=SQLITE_OK ){
 42192         /* This branch is taken if an error occurs while trying to open
 42193         ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
 42194         ** pager_unlock() routine will be called before returning to unlock
 42195         ** the file. If the unlock attempt fails, then Pager.eLock must be
 42196         ** set to UNKNOWN_LOCK (see the comment above the #define for 
 42197         ** UNKNOWN_LOCK above for an explanation). 
 42198         **
 42199         ** In order to get pager_unlock() to do this, set Pager.eState to
 42200         ** PAGER_ERROR now. This is not actually counted as a transition
 42201         ** to ERROR state in the state diagram at the top of this file,
 42202         ** since we know that the same call to pager_unlock() will very
 42203         ** shortly transition the pager object to the OPEN state. Calling
 42204         ** assert_pager_state() would fail now, as it should not be possible
 42205         ** to be in ERROR state when there are zero outstanding page 
 42206         ** references.
 42207         */
 42208         pager_error(pPager, rc);
 42209         goto failed;
 42212       assert( pPager->eState==PAGER_OPEN );
 42213       assert( (pPager->eLock==SHARED_LOCK)
 42214            || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
 42215       );
 42218     if( !pPager->tempFile 
 42219      && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0) 
 42220     ){
 42221       /* The shared-lock has just been acquired on the database file
 42222       ** and there are already pages in the cache (from a previous
 42223       ** read or write transaction).  Check to see if the database
 42224       ** has been modified.  If the database has changed, flush the
 42225       ** cache.
 42226       **
 42227       ** Database changes is detected by looking at 15 bytes beginning
 42228       ** at offset 24 into the file.  The first 4 of these 16 bytes are
 42229       ** a 32-bit counter that is incremented with each change.  The
 42230       ** other bytes change randomly with each file change when
 42231       ** a codec is in use.
 42232       ** 
 42233       ** There is a vanishingly small chance that a change will not be 
 42234       ** detected.  The chance of an undetected change is so small that
 42235       ** it can be neglected.
 42236       */
 42237       Pgno nPage = 0;
 42238       char dbFileVers[sizeof(pPager->dbFileVers)];
 42240       rc = pagerPagecount(pPager, &nPage);
 42241       if( rc ) goto failed;
 42243       if( nPage>0 ){
 42244         IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
 42245         rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
 42246         if( rc!=SQLITE_OK ){
 42247           goto failed;
 42249       }else{
 42250         memset(dbFileVers, 0, sizeof(dbFileVers));
 42253       if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
 42254         pager_reset(pPager);
 42258     /* If there is a WAL file in the file-system, open this database in WAL
 42259     ** mode. Otherwise, the following function call is a no-op.
 42260     */
 42261     rc = pagerOpenWalIfPresent(pPager);
 42262 #ifndef SQLITE_OMIT_WAL
 42263     assert( pPager->pWal==0 || rc==SQLITE_OK );
 42264 #endif
 42267   if( pagerUseWal(pPager) ){
 42268     assert( rc==SQLITE_OK );
 42269     rc = pagerBeginReadTransaction(pPager);
 42272   if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
 42273     rc = pagerPagecount(pPager, &pPager->dbSize);
 42276  failed:
 42277   if( rc!=SQLITE_OK ){
 42278     assert( !MEMDB );
 42279     pager_unlock(pPager);
 42280     assert( pPager->eState==PAGER_OPEN );
 42281   }else{
 42282     pPager->eState = PAGER_READER;
 42284   return rc;
 42287 /*
 42288 ** If the reference count has reached zero, rollback any active
 42289 ** transaction and unlock the pager.
 42290 **
 42291 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
 42292 ** the rollback journal, the unlock is not performed and there is
 42293 ** nothing to rollback, so this routine is a no-op.
 42294 */ 
 42295 static void pagerUnlockIfUnused(Pager *pPager){
 42296   if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
 42297     pagerUnlockAndRollback(pPager);
 42301 /*
 42302 ** Acquire a reference to page number pgno in pager pPager (a page
 42303 ** reference has type DbPage*). If the requested reference is 
 42304 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
 42305 **
 42306 ** If the requested page is already in the cache, it is returned. 
 42307 ** Otherwise, a new page object is allocated and populated with data
 42308 ** read from the database file. In some cases, the pcache module may
 42309 ** choose not to allocate a new page object and may reuse an existing
 42310 ** object with no outstanding references.
 42311 **
 42312 ** The extra data appended to a page is always initialized to zeros the 
 42313 ** first time a page is loaded into memory. If the page requested is 
 42314 ** already in the cache when this function is called, then the extra
 42315 ** data is left as it was when the page object was last used.
 42316 **
 42317 ** If the database image is smaller than the requested page or if a 
 42318 ** non-zero value is passed as the noContent parameter and the 
 42319 ** requested page is not already stored in the cache, then no 
 42320 ** actual disk read occurs. In this case the memory image of the 
 42321 ** page is initialized to all zeros. 
 42322 **
 42323 ** If noContent is true, it means that we do not care about the contents
 42324 ** of the page. This occurs in two seperate scenarios:
 42325 **
 42326 **   a) When reading a free-list leaf page from the database, and
 42327 **
 42328 **   b) When a savepoint is being rolled back and we need to load
 42329 **      a new page into the cache to be filled with the data read
 42330 **      from the savepoint journal.
 42331 **
 42332 ** If noContent is true, then the data returned is zeroed instead of
 42333 ** being read from the database. Additionally, the bits corresponding
 42334 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
 42335 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
 42336 ** savepoints are set. This means if the page is made writable at any
 42337 ** point in the future, using a call to sqlite3PagerWrite(), its contents
 42338 ** will not be journaled. This saves IO.
 42339 **
 42340 ** The acquisition might fail for several reasons.  In all cases,
 42341 ** an appropriate error code is returned and *ppPage is set to NULL.
 42342 **
 42343 ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
 42344 ** to find a page in the in-memory cache first.  If the page is not already
 42345 ** in memory, this routine goes to disk to read it in whereas Lookup()
 42346 ** just returns 0.  This routine acquires a read-lock the first time it
 42347 ** has to go to disk, and could also playback an old journal if necessary.
 42348 ** Since Lookup() never goes to disk, it never has to deal with locks
 42349 ** or journal files.
 42350 */
 42351 SQLITE_PRIVATE int sqlite3PagerAcquire(
 42352   Pager *pPager,      /* The pager open on the database file */
 42353   Pgno pgno,          /* Page number to fetch */
 42354   DbPage **ppPage,    /* Write a pointer to the page here */
 42355   int noContent       /* Do not bother reading content from disk if true */
 42356 ){
 42357   int rc;
 42358   PgHdr *pPg;
 42360   assert( pPager->eState>=PAGER_READER );
 42361   assert( assert_pager_state(pPager) );
 42363   if( pgno==0 ){
 42364     return SQLITE_CORRUPT_BKPT;
 42367   /* If the pager is in the error state, return an error immediately. 
 42368   ** Otherwise, request the page from the PCache layer. */
 42369   if( pPager->errCode!=SQLITE_OK ){
 42370     rc = pPager->errCode;
 42371   }else{
 42372     rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
 42375   if( rc!=SQLITE_OK ){
 42376     /* Either the call to sqlite3PcacheFetch() returned an error or the
 42377     ** pager was already in the error-state when this function was called.
 42378     ** Set pPg to 0 and jump to the exception handler.  */
 42379     pPg = 0;
 42380     goto pager_acquire_err;
 42382   assert( (*ppPage)->pgno==pgno );
 42383   assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
 42385   if( (*ppPage)->pPager && !noContent ){
 42386     /* In this case the pcache already contains an initialized copy of
 42387     ** the page. Return without further ado.  */
 42388     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
 42389     pPager->aStat[PAGER_STAT_HIT]++;
 42390     return SQLITE_OK;
 42392   }else{
 42393     /* The pager cache has created a new page. Its content needs to 
 42394     ** be initialized.  */
 42396     pPg = *ppPage;
 42397     pPg->pPager = pPager;
 42399     /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
 42400     ** number greater than this, or the unused locking-page, is requested. */
 42401     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
 42402       rc = SQLITE_CORRUPT_BKPT;
 42403       goto pager_acquire_err;
 42406     if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
 42407       if( pgno>pPager->mxPgno ){
 42408         rc = SQLITE_FULL;
 42409         goto pager_acquire_err;
 42411       if( noContent ){
 42412         /* Failure to set the bits in the InJournal bit-vectors is benign.
 42413         ** It merely means that we might do some extra work to journal a 
 42414         ** page that does not need to be journaled.  Nevertheless, be sure 
 42415         ** to test the case where a malloc error occurs while trying to set 
 42416         ** a bit in a bit vector.
 42417         */
 42418         sqlite3BeginBenignMalloc();
 42419         if( pgno<=pPager->dbOrigSize ){
 42420           TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
 42421           testcase( rc==SQLITE_NOMEM );
 42423         TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
 42424         testcase( rc==SQLITE_NOMEM );
 42425         sqlite3EndBenignMalloc();
 42427       memset(pPg->pData, 0, pPager->pageSize);
 42428       IOTRACE(("ZERO %p %d\n", pPager, pgno));
 42429     }else{
 42430       assert( pPg->pPager==pPager );
 42431       pPager->aStat[PAGER_STAT_MISS]++;
 42432       rc = readDbPage(pPg);
 42433       if( rc!=SQLITE_OK ){
 42434         goto pager_acquire_err;
 42437     pager_set_pagehash(pPg);
 42440   return SQLITE_OK;
 42442 pager_acquire_err:
 42443   assert( rc!=SQLITE_OK );
 42444   if( pPg ){
 42445     sqlite3PcacheDrop(pPg);
 42447   pagerUnlockIfUnused(pPager);
 42449   *ppPage = 0;
 42450   return rc;
 42453 /*
 42454 ** Acquire a page if it is already in the in-memory cache.  Do
 42455 ** not read the page from disk.  Return a pointer to the page,
 42456 ** or 0 if the page is not in cache. 
 42457 **
 42458 ** See also sqlite3PagerGet().  The difference between this routine
 42459 ** and sqlite3PagerGet() is that _get() will go to the disk and read
 42460 ** in the page if the page is not already in cache.  This routine
 42461 ** returns NULL if the page is not in cache or if a disk I/O error 
 42462 ** has ever happened.
 42463 */
 42464 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
 42465   PgHdr *pPg = 0;
 42466   assert( pPager!=0 );
 42467   assert( pgno!=0 );
 42468   assert( pPager->pPCache!=0 );
 42469   assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
 42470   sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
 42471   return pPg;
 42474 /*
 42475 ** Release a page reference.
 42476 **
 42477 ** If the number of references to the page drop to zero, then the
 42478 ** page is added to the LRU list.  When all references to all pages
 42479 ** are released, a rollback occurs and the lock on the database is
 42480 ** removed.
 42481 */
 42482 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
 42483   if( pPg ){
 42484     Pager *pPager = pPg->pPager;
 42485     sqlite3PcacheRelease(pPg);
 42486     pagerUnlockIfUnused(pPager);
 42490 /*
 42491 ** This function is called at the start of every write transaction.
 42492 ** There must already be a RESERVED or EXCLUSIVE lock on the database 
 42493 ** file when this routine is called.
 42494 **
 42495 ** Open the journal file for pager pPager and write a journal header
 42496 ** to the start of it. If there are active savepoints, open the sub-journal
 42497 ** as well. This function is only used when the journal file is being 
 42498 ** opened to write a rollback log for a transaction. It is not used 
 42499 ** when opening a hot journal file to roll it back.
 42500 **
 42501 ** If the journal file is already open (as it may be in exclusive mode),
 42502 ** then this function just writes a journal header to the start of the
 42503 ** already open file. 
 42504 **
 42505 ** Whether or not the journal file is opened by this function, the
 42506 ** Pager.pInJournal bitvec structure is allocated.
 42507 **
 42508 ** Return SQLITE_OK if everything is successful. Otherwise, return 
 42509 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or 
 42510 ** an IO error code if opening or writing the journal file fails.
 42511 */
 42512 static int pager_open_journal(Pager *pPager){
 42513   int rc = SQLITE_OK;                        /* Return code */
 42514   sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
 42516   assert( pPager->eState==PAGER_WRITER_LOCKED );
 42517   assert( assert_pager_state(pPager) );
 42518   assert( pPager->pInJournal==0 );
 42520   /* If already in the error state, this function is a no-op.  But on
 42521   ** the other hand, this routine is never called if we are already in
 42522   ** an error state. */
 42523   if( NEVER(pPager->errCode) ) return pPager->errCode;
 42525   if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
 42526     pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
 42527     if( pPager->pInJournal==0 ){
 42528       return SQLITE_NOMEM;
 42531     /* Open the journal file if it is not already open. */
 42532     if( !isOpen(pPager->jfd) ){
 42533       if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
 42534         sqlite3MemJournalOpen(pPager->jfd);
 42535       }else{
 42536         const int flags =                   /* VFS flags to open journal file */
 42537           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
 42538           (pPager->tempFile ? 
 42539             (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
 42540             (SQLITE_OPEN_MAIN_JOURNAL)
 42541           );
 42542   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 42543         rc = sqlite3JournalOpen(
 42544             pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
 42545         );
 42546   #else
 42547         rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
 42548   #endif
 42550       assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
 42554     /* Write the first journal header to the journal file and open 
 42555     ** the sub-journal if necessary.
 42556     */
 42557     if( rc==SQLITE_OK ){
 42558       /* TODO: Check if all of these are really required. */
 42559       pPager->nRec = 0;
 42560       pPager->journalOff = 0;
 42561       pPager->setMaster = 0;
 42562       pPager->journalHdr = 0;
 42563       rc = writeJournalHdr(pPager);
 42567   if( rc!=SQLITE_OK ){
 42568     sqlite3BitvecDestroy(pPager->pInJournal);
 42569     pPager->pInJournal = 0;
 42570   }else{
 42571     assert( pPager->eState==PAGER_WRITER_LOCKED );
 42572     pPager->eState = PAGER_WRITER_CACHEMOD;
 42575   return rc;
 42578 /*
 42579 ** Begin a write-transaction on the specified pager object. If a 
 42580 ** write-transaction has already been opened, this function is a no-op.
 42581 **
 42582 ** If the exFlag argument is false, then acquire at least a RESERVED
 42583 ** lock on the database file. If exFlag is true, then acquire at least
 42584 ** an EXCLUSIVE lock. If such a lock is already held, no locking 
 42585 ** functions need be called.
 42586 **
 42587 ** If the subjInMemory argument is non-zero, then any sub-journal opened
 42588 ** within this transaction will be opened as an in-memory file. This
 42589 ** has no effect if the sub-journal is already opened (as it may be when
 42590 ** running in exclusive mode) or if the transaction does not require a
 42591 ** sub-journal. If the subjInMemory argument is zero, then any required
 42592 ** sub-journal is implemented in-memory if pPager is an in-memory database, 
 42593 ** or using a temporary file otherwise.
 42594 */
 42595 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
 42596   int rc = SQLITE_OK;
 42598   if( pPager->errCode ) return pPager->errCode;
 42599   assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
 42600   pPager->subjInMemory = (u8)subjInMemory;
 42602   if( ALWAYS(pPager->eState==PAGER_READER) ){
 42603     assert( pPager->pInJournal==0 );
 42605     if( pagerUseWal(pPager) ){
 42606       /* If the pager is configured to use locking_mode=exclusive, and an
 42607       ** exclusive lock on the database is not already held, obtain it now.
 42608       */
 42609       if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
 42610         rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 42611         if( rc!=SQLITE_OK ){
 42612           return rc;
 42614         sqlite3WalExclusiveMode(pPager->pWal, 1);
 42617       /* Grab the write lock on the log file. If successful, upgrade to
 42618       ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
 42619       ** The busy-handler is not invoked if another connection already
 42620       ** holds the write-lock. If possible, the upper layer will call it.
 42621       */
 42622       rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
 42623     }else{
 42624       /* Obtain a RESERVED lock on the database file. If the exFlag parameter
 42625       ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
 42626       ** busy-handler callback can be used when upgrading to the EXCLUSIVE
 42627       ** lock, but not when obtaining the RESERVED lock.
 42628       */
 42629       rc = pagerLockDb(pPager, RESERVED_LOCK);
 42630       if( rc==SQLITE_OK && exFlag ){
 42631         rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
 42635     if( rc==SQLITE_OK ){
 42636       /* Change to WRITER_LOCKED state.
 42637       **
 42638       ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
 42639       ** when it has an open transaction, but never to DBMOD or FINISHED.
 42640       ** This is because in those states the code to roll back savepoint 
 42641       ** transactions may copy data from the sub-journal into the database 
 42642       ** file as well as into the page cache. Which would be incorrect in 
 42643       ** WAL mode.
 42644       */
 42645       pPager->eState = PAGER_WRITER_LOCKED;
 42646       pPager->dbHintSize = pPager->dbSize;
 42647       pPager->dbFileSize = pPager->dbSize;
 42648       pPager->dbOrigSize = pPager->dbSize;
 42649       pPager->journalOff = 0;
 42652     assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
 42653     assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
 42654     assert( assert_pager_state(pPager) );
 42657   PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
 42658   return rc;
 42661 /*
 42662 ** Mark a single data page as writeable. The page is written into the 
 42663 ** main journal or sub-journal as required. If the page is written into
 42664 ** one of the journals, the corresponding bit is set in the 
 42665 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
 42666 ** of any open savepoints as appropriate.
 42667 */
 42668 static int pager_write(PgHdr *pPg){
 42669   void *pData = pPg->pData;
 42670   Pager *pPager = pPg->pPager;
 42671   int rc = SQLITE_OK;
 42673   /* This routine is not called unless a write-transaction has already 
 42674   ** been started. The journal file may or may not be open at this point.
 42675   ** It is never called in the ERROR state.
 42676   */
 42677   assert( pPager->eState==PAGER_WRITER_LOCKED
 42678        || pPager->eState==PAGER_WRITER_CACHEMOD
 42679        || pPager->eState==PAGER_WRITER_DBMOD
 42680   );
 42681   assert( assert_pager_state(pPager) );
 42683   /* If an error has been previously detected, report the same error
 42684   ** again. This should not happen, but the check provides robustness. */
 42685   if( NEVER(pPager->errCode) )  return pPager->errCode;
 42687   /* Higher-level routines never call this function if database is not
 42688   ** writable.  But check anyway, just for robustness. */
 42689   if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
 42691   CHECK_PAGE(pPg);
 42693   /* The journal file needs to be opened. Higher level routines have already
 42694   ** obtained the necessary locks to begin the write-transaction, but the
 42695   ** rollback journal might not yet be open. Open it now if this is the case.
 42696   **
 42697   ** This is done before calling sqlite3PcacheMakeDirty() on the page. 
 42698   ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
 42699   ** an error might occur and the pager would end up in WRITER_LOCKED state
 42700   ** with pages marked as dirty in the cache.
 42701   */
 42702   if( pPager->eState==PAGER_WRITER_LOCKED ){
 42703     rc = pager_open_journal(pPager);
 42704     if( rc!=SQLITE_OK ) return rc;
 42706   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
 42707   assert( assert_pager_state(pPager) );
 42709   /* Mark the page as dirty.  If the page has already been written
 42710   ** to the journal then we can return right away.
 42711   */
 42712   sqlite3PcacheMakeDirty(pPg);
 42713   if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
 42714     assert( !pagerUseWal(pPager) );
 42715   }else{
 42717     /* The transaction journal now exists and we have a RESERVED or an
 42718     ** EXCLUSIVE lock on the main database file.  Write the current page to
 42719     ** the transaction journal if it is not there already.
 42720     */
 42721     if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
 42722       assert( pagerUseWal(pPager)==0 );
 42723       if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
 42724         u32 cksum;
 42725         char *pData2;
 42726         i64 iOff = pPager->journalOff;
 42728         /* We should never write to the journal file the page that
 42729         ** contains the database locks.  The following assert verifies
 42730         ** that we do not. */
 42731         assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
 42733         assert( pPager->journalHdr<=pPager->journalOff );
 42734         CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
 42735         cksum = pager_cksum(pPager, (u8*)pData2);
 42737         /* Even if an IO or diskfull error occurs while journalling the
 42738         ** page in the block above, set the need-sync flag for the page.
 42739         ** Otherwise, when the transaction is rolled back, the logic in
 42740         ** playback_one_page() will think that the page needs to be restored
 42741         ** in the database file. And if an IO error occurs while doing so,
 42742         ** then corruption may follow.
 42743         */
 42744         pPg->flags |= PGHDR_NEED_SYNC;
 42746         rc = write32bits(pPager->jfd, iOff, pPg->pgno);
 42747         if( rc!=SQLITE_OK ) return rc;
 42748         rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
 42749         if( rc!=SQLITE_OK ) return rc;
 42750         rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
 42751         if( rc!=SQLITE_OK ) return rc;
 42753         IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
 42754                  pPager->journalOff, pPager->pageSize));
 42755         PAGER_INCR(sqlite3_pager_writej_count);
 42756         PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
 42757              PAGERID(pPager), pPg->pgno, 
 42758              ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
 42760         pPager->journalOff += 8 + pPager->pageSize;
 42761         pPager->nRec++;
 42762         assert( pPager->pInJournal!=0 );
 42763         rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
 42764         testcase( rc==SQLITE_NOMEM );
 42765         assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
 42766         rc |= addToSavepointBitvecs(pPager, pPg->pgno);
 42767         if( rc!=SQLITE_OK ){
 42768           assert( rc==SQLITE_NOMEM );
 42769           return rc;
 42771       }else{
 42772         if( pPager->eState!=PAGER_WRITER_DBMOD ){
 42773           pPg->flags |= PGHDR_NEED_SYNC;
 42775         PAGERTRACE(("APPEND %d page %d needSync=%d\n",
 42776                 PAGERID(pPager), pPg->pgno,
 42777                ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
 42781     /* If the statement journal is open and the page is not in it,
 42782     ** then write the current page to the statement journal.  Note that
 42783     ** the statement journal format differs from the standard journal format
 42784     ** in that it omits the checksums and the header.
 42785     */
 42786     if( subjRequiresPage(pPg) ){
 42787       rc = subjournalPage(pPg);
 42791   /* Update the database size and return.
 42792   */
 42793   if( pPager->dbSize<pPg->pgno ){
 42794     pPager->dbSize = pPg->pgno;
 42796   return rc;
 42799 /*
 42800 ** Mark a data page as writeable. This routine must be called before 
 42801 ** making changes to a page. The caller must check the return value 
 42802 ** of this function and be careful not to change any page data unless 
 42803 ** this routine returns SQLITE_OK.
 42804 **
 42805 ** The difference between this function and pager_write() is that this
 42806 ** function also deals with the special case where 2 or more pages
 42807 ** fit on a single disk sector. In this case all co-resident pages
 42808 ** must have been written to the journal file before returning.
 42809 **
 42810 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
 42811 ** as appropriate. Otherwise, SQLITE_OK.
 42812 */
 42813 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
 42814   int rc = SQLITE_OK;
 42816   PgHdr *pPg = pDbPage;
 42817   Pager *pPager = pPg->pPager;
 42818   Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
 42820   assert( pPager->eState>=PAGER_WRITER_LOCKED );
 42821   assert( pPager->eState!=PAGER_ERROR );
 42822   assert( assert_pager_state(pPager) );
 42824   if( nPagePerSector>1 ){
 42825     Pgno nPageCount;          /* Total number of pages in database file */
 42826     Pgno pg1;                 /* First page of the sector pPg is located on. */
 42827     int nPage = 0;            /* Number of pages starting at pg1 to journal */
 42828     int ii;                   /* Loop counter */
 42829     int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
 42831     /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
 42832     ** a journal header to be written between the pages journaled by
 42833     ** this function.
 42834     */
 42835     assert( !MEMDB );
 42836     assert( pPager->doNotSyncSpill==0 );
 42837     pPager->doNotSyncSpill++;
 42839     /* This trick assumes that both the page-size and sector-size are
 42840     ** an integer power of 2. It sets variable pg1 to the identifier
 42841     ** of the first page of the sector pPg is located on.
 42842     */
 42843     pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
 42845     nPageCount = pPager->dbSize;
 42846     if( pPg->pgno>nPageCount ){
 42847       nPage = (pPg->pgno - pg1)+1;
 42848     }else if( (pg1+nPagePerSector-1)>nPageCount ){
 42849       nPage = nPageCount+1-pg1;
 42850     }else{
 42851       nPage = nPagePerSector;
 42853     assert(nPage>0);
 42854     assert(pg1<=pPg->pgno);
 42855     assert((pg1+nPage)>pPg->pgno);
 42857     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
 42858       Pgno pg = pg1+ii;
 42859       PgHdr *pPage;
 42860       if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
 42861         if( pg!=PAGER_MJ_PGNO(pPager) ){
 42862           rc = sqlite3PagerGet(pPager, pg, &pPage);
 42863           if( rc==SQLITE_OK ){
 42864             rc = pager_write(pPage);
 42865             if( pPage->flags&PGHDR_NEED_SYNC ){
 42866               needSync = 1;
 42868             sqlite3PagerUnref(pPage);
 42871       }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
 42872         if( pPage->flags&PGHDR_NEED_SYNC ){
 42873           needSync = 1;
 42875         sqlite3PagerUnref(pPage);
 42879     /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
 42880     ** starting at pg1, then it needs to be set for all of them. Because
 42881     ** writing to any of these nPage pages may damage the others, the
 42882     ** journal file must contain sync()ed copies of all of them
 42883     ** before any of them can be written out to the database file.
 42884     */
 42885     if( rc==SQLITE_OK && needSync ){
 42886       assert( !MEMDB );
 42887       for(ii=0; ii<nPage; ii++){
 42888         PgHdr *pPage = pager_lookup(pPager, pg1+ii);
 42889         if( pPage ){
 42890           pPage->flags |= PGHDR_NEED_SYNC;
 42891           sqlite3PagerUnref(pPage);
 42896     assert( pPager->doNotSyncSpill==1 );
 42897     pPager->doNotSyncSpill--;
 42898   }else{
 42899     rc = pager_write(pDbPage);
 42901   return rc;
 42904 /*
 42905 ** Return TRUE if the page given in the argument was previously passed
 42906 ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
 42907 ** to change the content of the page.
 42908 */
 42909 #ifndef NDEBUG
 42910 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
 42911   return pPg->flags&PGHDR_DIRTY;
 42913 #endif
 42915 /*
 42916 ** A call to this routine tells the pager that it is not necessary to
 42917 ** write the information on page pPg back to the disk, even though
 42918 ** that page might be marked as dirty.  This happens, for example, when
 42919 ** the page has been added as a leaf of the freelist and so its
 42920 ** content no longer matters.
 42921 **
 42922 ** The overlying software layer calls this routine when all of the data
 42923 ** on the given page is unused. The pager marks the page as clean so
 42924 ** that it does not get written to disk.
 42925 **
 42926 ** Tests show that this optimization can quadruple the speed of large 
 42927 ** DELETE operations.
 42928 */
 42929 SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
 42930   Pager *pPager = pPg->pPager;
 42931   if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
 42932     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
 42933     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
 42934     pPg->flags |= PGHDR_DONT_WRITE;
 42935     pager_set_pagehash(pPg);
 42939 /*
 42940 ** This routine is called to increment the value of the database file 
 42941 ** change-counter, stored as a 4-byte big-endian integer starting at 
 42942 ** byte offset 24 of the pager file.  The secondary change counter at
 42943 ** 92 is also updated, as is the SQLite version number at offset 96.
 42944 **
 42945 ** But this only happens if the pPager->changeCountDone flag is false.
 42946 ** To avoid excess churning of page 1, the update only happens once.
 42947 ** See also the pager_write_changecounter() routine that does an 
 42948 ** unconditional update of the change counters.
 42949 **
 42950 ** If the isDirectMode flag is zero, then this is done by calling 
 42951 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
 42952 ** page data. In this case the file will be updated when the current
 42953 ** transaction is committed.
 42954 **
 42955 ** The isDirectMode flag may only be non-zero if the library was compiled
 42956 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
 42957 ** if isDirect is non-zero, then the database file is updated directly
 42958 ** by writing an updated version of page 1 using a call to the 
 42959 ** sqlite3OsWrite() function.
 42960 */
 42961 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
 42962   int rc = SQLITE_OK;
 42964   assert( pPager->eState==PAGER_WRITER_CACHEMOD
 42965        || pPager->eState==PAGER_WRITER_DBMOD
 42966   );
 42967   assert( assert_pager_state(pPager) );
 42969   /* Declare and initialize constant integer 'isDirect'. If the
 42970   ** atomic-write optimization is enabled in this build, then isDirect
 42971   ** is initialized to the value passed as the isDirectMode parameter
 42972   ** to this function. Otherwise, it is always set to zero.
 42973   **
 42974   ** The idea is that if the atomic-write optimization is not
 42975   ** enabled at compile time, the compiler can omit the tests of
 42976   ** 'isDirect' below, as well as the block enclosed in the
 42977   ** "if( isDirect )" condition.
 42978   */
 42979 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
 42980 # define DIRECT_MODE 0
 42981   assert( isDirectMode==0 );
 42982   UNUSED_PARAMETER(isDirectMode);
 42983 #else
 42984 # define DIRECT_MODE isDirectMode
 42985 #endif
 42987   if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
 42988     PgHdr *pPgHdr;                /* Reference to page 1 */
 42990     assert( !pPager->tempFile && isOpen(pPager->fd) );
 42992     /* Open page 1 of the file for writing. */
 42993     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
 42994     assert( pPgHdr==0 || rc==SQLITE_OK );
 42996     /* If page one was fetched successfully, and this function is not
 42997     ** operating in direct-mode, make page 1 writable.  When not in 
 42998     ** direct mode, page 1 is always held in cache and hence the PagerGet()
 42999     ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
 43000     */
 43001     if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
 43002       rc = sqlite3PagerWrite(pPgHdr);
 43005     if( rc==SQLITE_OK ){
 43006       /* Actually do the update of the change counter */
 43007       pager_write_changecounter(pPgHdr);
 43009       /* If running in direct mode, write the contents of page 1 to the file. */
 43010       if( DIRECT_MODE ){
 43011         const void *zBuf;
 43012         assert( pPager->dbFileSize>0 );
 43013         CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
 43014         if( rc==SQLITE_OK ){
 43015           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
 43016           pPager->aStat[PAGER_STAT_WRITE]++;
 43018         if( rc==SQLITE_OK ){
 43019           pPager->changeCountDone = 1;
 43021       }else{
 43022         pPager->changeCountDone = 1;
 43026     /* Release the page reference. */
 43027     sqlite3PagerUnref(pPgHdr);
 43029   return rc;
 43032 /*
 43033 ** Sync the database file to disk. This is a no-op for in-memory databases
 43034 ** or pages with the Pager.noSync flag set.
 43035 **
 43036 ** If successful, or if called on a pager for which it is a no-op, this
 43037 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
 43038 */
 43039 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
 43040   int rc = SQLITE_OK;
 43041   if( !pPager->noSync ){
 43042     assert( !MEMDB );
 43043     rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
 43044   }else if( isOpen(pPager->fd) ){
 43045     assert( !MEMDB );
 43046     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
 43047     if( rc==SQLITE_NOTFOUND ){
 43048       rc = SQLITE_OK;
 43051   return rc;
 43054 /*
 43055 ** This function may only be called while a write-transaction is active in
 43056 ** rollback. If the connection is in WAL mode, this call is a no-op. 
 43057 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on 
 43058 ** the database file, an attempt is made to obtain one.
 43059 **
 43060 ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
 43061 ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
 43062 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is 
 43063 ** returned.
 43064 */
 43065 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
 43066   int rc = SQLITE_OK;
 43067   assert( pPager->eState==PAGER_WRITER_CACHEMOD 
 43068        || pPager->eState==PAGER_WRITER_DBMOD 
 43069        || pPager->eState==PAGER_WRITER_LOCKED 
 43070   );
 43071   assert( assert_pager_state(pPager) );
 43072   if( 0==pagerUseWal(pPager) ){
 43073     rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
 43075   return rc;
 43078 /*
 43079 ** Sync the database file for the pager pPager. zMaster points to the name
 43080 ** of a master journal file that should be written into the individual
 43081 ** journal file. zMaster may be NULL, which is interpreted as no master
 43082 ** journal (a single database transaction).
 43083 **
 43084 ** This routine ensures that:
 43085 **
 43086 **   * The database file change-counter is updated,
 43087 **   * the journal is synced (unless the atomic-write optimization is used),
 43088 **   * all dirty pages are written to the database file, 
 43089 **   * the database file is truncated (if required), and
 43090 **   * the database file synced. 
 43091 **
 43092 ** The only thing that remains to commit the transaction is to finalize 
 43093 ** (delete, truncate or zero the first part of) the journal file (or 
 43094 ** delete the master journal file if specified).
 43095 **
 43096 ** Note that if zMaster==NULL, this does not overwrite a previous value
 43097 ** passed to an sqlite3PagerCommitPhaseOne() call.
 43098 **
 43099 ** If the final parameter - noSync - is true, then the database file itself
 43100 ** is not synced. The caller must call sqlite3PagerSync() directly to
 43101 ** sync the database file before calling CommitPhaseTwo() to delete the
 43102 ** journal file in this case.
 43103 */
 43104 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
 43105   Pager *pPager,                  /* Pager object */
 43106   const char *zMaster,            /* If not NULL, the master journal name */
 43107   int noSync                      /* True to omit the xSync on the db file */
 43108 ){
 43109   int rc = SQLITE_OK;             /* Return code */
 43111   assert( pPager->eState==PAGER_WRITER_LOCKED
 43112        || pPager->eState==PAGER_WRITER_CACHEMOD
 43113        || pPager->eState==PAGER_WRITER_DBMOD
 43114        || pPager->eState==PAGER_ERROR
 43115   );
 43116   assert( assert_pager_state(pPager) );
 43118   /* If a prior error occurred, report that error again. */
 43119   if( NEVER(pPager->errCode) ) return pPager->errCode;
 43121   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
 43122       pPager->zFilename, zMaster, pPager->dbSize));
 43124   /* If no database changes have been made, return early. */
 43125   if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
 43127   if( MEMDB ){
 43128     /* If this is an in-memory db, or no pages have been written to, or this
 43129     ** function has already been called, it is mostly a no-op.  However, any
 43130     ** backup in progress needs to be restarted.
 43131     */
 43132     sqlite3BackupRestart(pPager->pBackup);
 43133   }else{
 43134     if( pagerUseWal(pPager) ){
 43135       PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
 43136       PgHdr *pPageOne = 0;
 43137       if( pList==0 ){
 43138         /* Must have at least one page for the WAL commit flag.
 43139         ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
 43140         rc = sqlite3PagerGet(pPager, 1, &pPageOne);
 43141         pList = pPageOne;
 43142         pList->pDirty = 0;
 43144       assert( rc==SQLITE_OK );
 43145       if( ALWAYS(pList) ){
 43146         rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
 43148       sqlite3PagerUnref(pPageOne);
 43149       if( rc==SQLITE_OK ){
 43150         sqlite3PcacheCleanAll(pPager->pPCache);
 43152     }else{
 43153       /* The following block updates the change-counter. Exactly how it
 43154       ** does this depends on whether or not the atomic-update optimization
 43155       ** was enabled at compile time, and if this transaction meets the 
 43156       ** runtime criteria to use the operation: 
 43157       **
 43158       **    * The file-system supports the atomic-write property for
 43159       **      blocks of size page-size, and 
 43160       **    * This commit is not part of a multi-file transaction, and
 43161       **    * Exactly one page has been modified and store in the journal file.
 43162       **
 43163       ** If the optimization was not enabled at compile time, then the
 43164       ** pager_incr_changecounter() function is called to update the change
 43165       ** counter in 'indirect-mode'. If the optimization is compiled in but
 43166       ** is not applicable to this transaction, call sqlite3JournalCreate()
 43167       ** to make sure the journal file has actually been created, then call
 43168       ** pager_incr_changecounter() to update the change-counter in indirect
 43169       ** mode. 
 43170       **
 43171       ** Otherwise, if the optimization is both enabled and applicable,
 43172       ** then call pager_incr_changecounter() to update the change-counter
 43173       ** in 'direct' mode. In this case the journal file will never be
 43174       ** created for this transaction.
 43175       */
 43176   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 43177       PgHdr *pPg;
 43178       assert( isOpen(pPager->jfd) 
 43179            || pPager->journalMode==PAGER_JOURNALMODE_OFF 
 43180            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
 43181       );
 43182       if( !zMaster && isOpen(pPager->jfd) 
 43183        && pPager->journalOff==jrnlBufferSize(pPager) 
 43184        && pPager->dbSize>=pPager->dbOrigSize
 43185        && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
 43186       ){
 43187         /* Update the db file change counter via the direct-write method. The 
 43188         ** following call will modify the in-memory representation of page 1 
 43189         ** to include the updated change counter and then write page 1 
 43190         ** directly to the database file. Because of the atomic-write 
 43191         ** property of the host file-system, this is safe.
 43192         */
 43193         rc = pager_incr_changecounter(pPager, 1);
 43194       }else{
 43195         rc = sqlite3JournalCreate(pPager->jfd);
 43196         if( rc==SQLITE_OK ){
 43197           rc = pager_incr_changecounter(pPager, 0);
 43200   #else
 43201       rc = pager_incr_changecounter(pPager, 0);
 43202   #endif
 43203       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 43205       /* If this transaction has made the database smaller, then all pages
 43206       ** being discarded by the truncation must be written to the journal
 43207       ** file.
 43208       **
 43209       ** Before reading the pages with page numbers larger than the 
 43210       ** current value of Pager.dbSize, set dbSize back to the value
 43211       ** that it took at the start of the transaction. Otherwise, the
 43212       ** calls to sqlite3PagerGet() return zeroed pages instead of 
 43213       ** reading data from the database file.
 43214       */
 43215       if( pPager->dbSize<pPager->dbOrigSize 
 43216        && pPager->journalMode!=PAGER_JOURNALMODE_OFF
 43217       ){
 43218         Pgno i;                                   /* Iterator variable */
 43219         const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
 43220         const Pgno dbSize = pPager->dbSize;       /* Database image size */ 
 43221         pPager->dbSize = pPager->dbOrigSize;
 43222         for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
 43223           if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
 43224             PgHdr *pPage;             /* Page to journal */
 43225             rc = sqlite3PagerGet(pPager, i, &pPage);
 43226             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 43227             rc = sqlite3PagerWrite(pPage);
 43228             sqlite3PagerUnref(pPage);
 43229             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 43232         pPager->dbSize = dbSize;
 43235       /* Write the master journal name into the journal file. If a master 
 43236       ** journal file name has already been written to the journal file, 
 43237       ** or if zMaster is NULL (no master journal), then this call is a no-op.
 43238       */
 43239       rc = writeMasterJournal(pPager, zMaster);
 43240       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 43242       /* Sync the journal file and write all dirty pages to the database.
 43243       ** If the atomic-update optimization is being used, this sync will not 
 43244       ** create the journal file or perform any real IO.
 43245       **
 43246       ** Because the change-counter page was just modified, unless the
 43247       ** atomic-update optimization is used it is almost certain that the
 43248       ** journal requires a sync here. However, in locking_mode=exclusive
 43249       ** on a system under memory pressure it is just possible that this is 
 43250       ** not the case. In this case it is likely enough that the redundant
 43251       ** xSync() call will be changed to a no-op by the OS anyhow. 
 43252       */
 43253       rc = syncJournal(pPager, 0);
 43254       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 43256       rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
 43257       if( rc!=SQLITE_OK ){
 43258         assert( rc!=SQLITE_IOERR_BLOCKED );
 43259         goto commit_phase_one_exit;
 43261       sqlite3PcacheCleanAll(pPager->pPCache);
 43263       /* If the file on disk is not the same size as the database image,
 43264       ** then use pager_truncate to grow or shrink the file here.
 43265       */
 43266       if( pPager->dbSize!=pPager->dbFileSize ){
 43267         Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
 43268         assert( pPager->eState==PAGER_WRITER_DBMOD );
 43269         rc = pager_truncate(pPager, nNew);
 43270         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 43273       /* Finally, sync the database file. */
 43274       if( !noSync ){
 43275         rc = sqlite3PagerSync(pPager);
 43277       IOTRACE(("DBSYNC %p\n", pPager))
 43281 commit_phase_one_exit:
 43282   if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
 43283     pPager->eState = PAGER_WRITER_FINISHED;
 43285   return rc;
 43289 /*
 43290 ** When this function is called, the database file has been completely
 43291 ** updated to reflect the changes made by the current transaction and
 43292 ** synced to disk. The journal file still exists in the file-system 
 43293 ** though, and if a failure occurs at this point it will eventually
 43294 ** be used as a hot-journal and the current transaction rolled back.
 43295 **
 43296 ** This function finalizes the journal file, either by deleting, 
 43297 ** truncating or partially zeroing it, so that it cannot be used 
 43298 ** for hot-journal rollback. Once this is done the transaction is
 43299 ** irrevocably committed.
 43300 **
 43301 ** If an error occurs, an IO error code is returned and the pager
 43302 ** moves into the error state. Otherwise, SQLITE_OK is returned.
 43303 */
 43304 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
 43305   int rc = SQLITE_OK;                  /* Return code */
 43307   /* This routine should not be called if a prior error has occurred.
 43308   ** But if (due to a coding error elsewhere in the system) it does get
 43309   ** called, just return the same error code without doing anything. */
 43310   if( NEVER(pPager->errCode) ) return pPager->errCode;
 43312   assert( pPager->eState==PAGER_WRITER_LOCKED
 43313        || pPager->eState==PAGER_WRITER_FINISHED
 43314        || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
 43315   );
 43316   assert( assert_pager_state(pPager) );
 43318   /* An optimization. If the database was not actually modified during
 43319   ** this transaction, the pager is running in exclusive-mode and is
 43320   ** using persistent journals, then this function is a no-op.
 43321   **
 43322   ** The start of the journal file currently contains a single journal 
 43323   ** header with the nRec field set to 0. If such a journal is used as
 43324   ** a hot-journal during hot-journal rollback, 0 changes will be made
 43325   ** to the database file. So there is no need to zero the journal 
 43326   ** header. Since the pager is in exclusive mode, there is no need
 43327   ** to drop any locks either.
 43328   */
 43329   if( pPager->eState==PAGER_WRITER_LOCKED 
 43330    && pPager->exclusiveMode 
 43331    && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
 43332   ){
 43333     assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
 43334     pPager->eState = PAGER_READER;
 43335     return SQLITE_OK;
 43338   PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
 43339   rc = pager_end_transaction(pPager, pPager->setMaster);
 43340   return pager_error(pPager, rc);
 43343 /*
 43344 ** If a write transaction is open, then all changes made within the 
 43345 ** transaction are reverted and the current write-transaction is closed.
 43346 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
 43347 ** state if an error occurs.
 43348 **
 43349 ** If the pager is already in PAGER_ERROR state when this function is called,
 43350 ** it returns Pager.errCode immediately. No work is performed in this case.
 43351 **
 43352 ** Otherwise, in rollback mode, this function performs two functions:
 43353 **
 43354 **   1) It rolls back the journal file, restoring all database file and 
 43355 **      in-memory cache pages to the state they were in when the transaction
 43356 **      was opened, and
 43357 **
 43358 **   2) It finalizes the journal file, so that it is not used for hot
 43359 **      rollback at any point in the future.
 43360 **
 43361 ** Finalization of the journal file (task 2) is only performed if the 
 43362 ** rollback is successful.
 43363 **
 43364 ** In WAL mode, all cache-entries containing data modified within the
 43365 ** current transaction are either expelled from the cache or reverted to
 43366 ** their pre-transaction state by re-reading data from the database or
 43367 ** WAL files. The WAL transaction is then closed.
 43368 */
 43369 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
 43370   int rc = SQLITE_OK;                  /* Return code */
 43371   PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
 43373   /* PagerRollback() is a no-op if called in READER or OPEN state. If
 43374   ** the pager is already in the ERROR state, the rollback is not 
 43375   ** attempted here. Instead, the error code is returned to the caller.
 43376   */
 43377   assert( assert_pager_state(pPager) );
 43378   if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
 43379   if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
 43381   if( pagerUseWal(pPager) ){
 43382     int rc2;
 43383     rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
 43384     rc2 = pager_end_transaction(pPager, pPager->setMaster);
 43385     if( rc==SQLITE_OK ) rc = rc2;
 43386   }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
 43387     int eState = pPager->eState;
 43388     rc = pager_end_transaction(pPager, 0);
 43389     if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
 43390       /* This can happen using journal_mode=off. Move the pager to the error 
 43391       ** state to indicate that the contents of the cache may not be trusted.
 43392       ** Any active readers will get SQLITE_ABORT.
 43393       */
 43394       pPager->errCode = SQLITE_ABORT;
 43395       pPager->eState = PAGER_ERROR;
 43396       return rc;
 43398   }else{
 43399     rc = pager_playback(pPager, 0);
 43402   assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
 43403   assert( rc==SQLITE_OK || rc==SQLITE_FULL
 43404           || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
 43406   /* If an error occurs during a ROLLBACK, we can no longer trust the pager
 43407   ** cache. So call pager_error() on the way out to make any error persistent.
 43408   */
 43409   return pager_error(pPager, rc);
 43412 /*
 43413 ** Return TRUE if the database file is opened read-only.  Return FALSE
 43414 ** if the database is (in theory) writable.
 43415 */
 43416 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
 43417   return pPager->readOnly;
 43420 /*
 43421 ** Return the number of references to the pager.
 43422 */
 43423 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
 43424   return sqlite3PcacheRefCount(pPager->pPCache);
 43427 /*
 43428 ** Return the approximate number of bytes of memory currently
 43429 ** used by the pager and its associated cache.
 43430 */
 43431 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
 43432   int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
 43433                                      + 5*sizeof(void*);
 43434   return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
 43435            + sqlite3MallocSize(pPager)
 43436            + pPager->pageSize;
 43439 /*
 43440 ** Return the number of references to the specified page.
 43441 */
 43442 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
 43443   return sqlite3PcachePageRefcount(pPage);
 43446 #ifdef SQLITE_TEST
 43447 /*
 43448 ** This routine is used for testing and analysis only.
 43449 */
 43450 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
 43451   static int a[11];
 43452   a[0] = sqlite3PcacheRefCount(pPager->pPCache);
 43453   a[1] = sqlite3PcachePagecount(pPager->pPCache);
 43454   a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
 43455   a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
 43456   a[4] = pPager->eState;
 43457   a[5] = pPager->errCode;
 43458   a[6] = pPager->aStat[PAGER_STAT_HIT];
 43459   a[7] = pPager->aStat[PAGER_STAT_MISS];
 43460   a[8] = 0;  /* Used to be pPager->nOvfl */
 43461   a[9] = pPager->nRead;
 43462   a[10] = pPager->aStat[PAGER_STAT_WRITE];
 43463   return a;
 43465 #endif
 43467 /*
 43468 ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 43469 ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
 43470 ** current cache hit or miss count, according to the value of eStat. If the 
 43471 ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 43472 ** returning.
 43473 */
 43474 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 43476   assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 43477        || eStat==SQLITE_DBSTATUS_CACHE_MISS
 43478        || eStat==SQLITE_DBSTATUS_CACHE_WRITE
 43479   );
 43481   assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 43482   assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 43483   assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
 43485   *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
 43486   if( reset ){
 43487     pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
 43491 /*
 43492 ** Return true if this is an in-memory pager.
 43493 */
 43494 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
 43495   return MEMDB;
 43498 /*
 43499 ** Check that there are at least nSavepoint savepoints open. If there are
 43500 ** currently less than nSavepoints open, then open one or more savepoints
 43501 ** to make up the difference. If the number of savepoints is already
 43502 ** equal to nSavepoint, then this function is a no-op.
 43503 **
 43504 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
 43505 ** occurs while opening the sub-journal file, then an IO error code is
 43506 ** returned. Otherwise, SQLITE_OK.
 43507 */
 43508 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
 43509   int rc = SQLITE_OK;                       /* Return code */
 43510   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
 43512   assert( pPager->eState>=PAGER_WRITER_LOCKED );
 43513   assert( assert_pager_state(pPager) );
 43515   if( nSavepoint>nCurrent && pPager->useJournal ){
 43516     int ii;                                 /* Iterator variable */
 43517     PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
 43519     /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
 43520     ** if the allocation fails. Otherwise, zero the new portion in case a 
 43521     ** malloc failure occurs while populating it in the for(...) loop below.
 43522     */
 43523     aNew = (PagerSavepoint *)sqlite3Realloc(
 43524         pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
 43525     );
 43526     if( !aNew ){
 43527       return SQLITE_NOMEM;
 43529     memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
 43530     pPager->aSavepoint = aNew;
 43532     /* Populate the PagerSavepoint structures just allocated. */
 43533     for(ii=nCurrent; ii<nSavepoint; ii++){
 43534       aNew[ii].nOrig = pPager->dbSize;
 43535       if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
 43536         aNew[ii].iOffset = pPager->journalOff;
 43537       }else{
 43538         aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
 43540       aNew[ii].iSubRec = pPager->nSubRec;
 43541       aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
 43542       if( !aNew[ii].pInSavepoint ){
 43543         return SQLITE_NOMEM;
 43545       if( pagerUseWal(pPager) ){
 43546         sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
 43548       pPager->nSavepoint = ii+1;
 43550     assert( pPager->nSavepoint==nSavepoint );
 43551     assertTruncateConstraint(pPager);
 43554   return rc;
 43557 /*
 43558 ** This function is called to rollback or release (commit) a savepoint.
 43559 ** The savepoint to release or rollback need not be the most recently 
 43560 ** created savepoint.
 43561 **
 43562 ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
 43563 ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
 43564 ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
 43565 ** that have occurred since the specified savepoint was created.
 43566 **
 43567 ** The savepoint to rollback or release is identified by parameter 
 43568 ** iSavepoint. A value of 0 means to operate on the outermost savepoint
 43569 ** (the first created). A value of (Pager.nSavepoint-1) means operate
 43570 ** on the most recently created savepoint. If iSavepoint is greater than
 43571 ** (Pager.nSavepoint-1), then this function is a no-op.
 43572 **
 43573 ** If a negative value is passed to this function, then the current
 43574 ** transaction is rolled back. This is different to calling 
 43575 ** sqlite3PagerRollback() because this function does not terminate
 43576 ** the transaction or unlock the database, it just restores the 
 43577 ** contents of the database to its original state. 
 43578 **
 43579 ** In any case, all savepoints with an index greater than iSavepoint 
 43580 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
 43581 ** then savepoint iSavepoint is also destroyed.
 43582 **
 43583 ** This function may return SQLITE_NOMEM if a memory allocation fails,
 43584 ** or an IO error code if an IO error occurs while rolling back a 
 43585 ** savepoint. If no errors occur, SQLITE_OK is returned.
 43586 */ 
 43587 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
 43588   int rc = pPager->errCode;       /* Return code */
 43590   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 43591   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
 43593   if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
 43594     int ii;            /* Iterator variable */
 43595     int nNew;          /* Number of remaining savepoints after this op. */
 43597     /* Figure out how many savepoints will still be active after this
 43598     ** operation. Store this value in nNew. Then free resources associated 
 43599     ** with any savepoints that are destroyed by this operation.
 43600     */
 43601     nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
 43602     for(ii=nNew; ii<pPager->nSavepoint; ii++){
 43603       sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
 43605     pPager->nSavepoint = nNew;
 43607     /* If this is a release of the outermost savepoint, truncate 
 43608     ** the sub-journal to zero bytes in size. */
 43609     if( op==SAVEPOINT_RELEASE ){
 43610       if( nNew==0 && isOpen(pPager->sjfd) ){
 43611         /* Only truncate if it is an in-memory sub-journal. */
 43612         if( sqlite3IsMemJournal(pPager->sjfd) ){
 43613           rc = sqlite3OsTruncate(pPager->sjfd, 0);
 43614           assert( rc==SQLITE_OK );
 43616         pPager->nSubRec = 0;
 43619     /* Else this is a rollback operation, playback the specified savepoint.
 43620     ** If this is a temp-file, it is possible that the journal file has
 43621     ** not yet been opened. In this case there have been no changes to
 43622     ** the database file, so the playback operation can be skipped.
 43623     */
 43624     else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
 43625       PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
 43626       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
 43627       assert(rc!=SQLITE_DONE);
 43631   return rc;
 43634 /*
 43635 ** Return the full pathname of the database file.
 43636 **
 43637 ** Except, if the pager is in-memory only, then return an empty string if
 43638 ** nullIfMemDb is true.  This routine is called with nullIfMemDb==1 when
 43639 ** used to report the filename to the user, for compatibility with legacy
 43640 ** behavior.  But when the Btree needs to know the filename for matching to
 43641 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
 43642 ** participate in shared-cache.
 43643 */
 43644 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
 43645   return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
 43648 /*
 43649 ** Return the VFS structure for the pager.
 43650 */
 43651 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
 43652   return pPager->pVfs;
 43655 /*
 43656 ** Return the file handle for the database file associated
 43657 ** with the pager.  This might return NULL if the file has
 43658 ** not yet been opened.
 43659 */
 43660 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
 43661   return pPager->fd;
 43664 /*
 43665 ** Return the full pathname of the journal file.
 43666 */
 43667 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
 43668   return pPager->zJournal;
 43671 /*
 43672 ** Return true if fsync() calls are disabled for this pager.  Return FALSE
 43673 ** if fsync()s are executed normally.
 43674 */
 43675 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
 43676   return pPager->noSync;
 43679 #ifdef SQLITE_HAS_CODEC
 43680 /*
 43681 ** Set or retrieve the codec for this pager
 43682 */
 43683 SQLITE_PRIVATE void sqlite3PagerSetCodec(
 43684   Pager *pPager,
 43685   void *(*xCodec)(void*,void*,Pgno,int),
 43686   void (*xCodecSizeChng)(void*,int,int),
 43687   void (*xCodecFree)(void*),
 43688   void *pCodec
 43689 ){
 43690   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
 43691   pPager->xCodec = pPager->memDb ? 0 : xCodec;
 43692   pPager->xCodecSizeChng = xCodecSizeChng;
 43693   pPager->xCodecFree = xCodecFree;
 43694   pPager->pCodec = pCodec;
 43695   pagerReportSize(pPager);
 43697 SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
 43698   return pPager->pCodec;
 43700 #endif
 43702 #ifndef SQLITE_OMIT_AUTOVACUUM
 43703 /*
 43704 ** Move the page pPg to location pgno in the file.
 43705 **
 43706 ** There must be no references to the page previously located at
 43707 ** pgno (which we call pPgOld) though that page is allowed to be
 43708 ** in cache.  If the page previously located at pgno is not already
 43709 ** in the rollback journal, it is not put there by by this routine.
 43710 **
 43711 ** References to the page pPg remain valid. Updating any
 43712 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
 43713 ** allocated along with the page) is the responsibility of the caller.
 43714 **
 43715 ** A transaction must be active when this routine is called. It used to be
 43716 ** required that a statement transaction was not active, but this restriction
 43717 ** has been removed (CREATE INDEX needs to move a page when a statement
 43718 ** transaction is active).
 43719 **
 43720 ** If the fourth argument, isCommit, is non-zero, then this page is being
 43721 ** moved as part of a database reorganization just before the transaction 
 43722 ** is being committed. In this case, it is guaranteed that the database page 
 43723 ** pPg refers to will not be written to again within this transaction.
 43724 **
 43725 ** This function may return SQLITE_NOMEM or an IO error code if an error
 43726 ** occurs. Otherwise, it returns SQLITE_OK.
 43727 */
 43728 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
 43729   PgHdr *pPgOld;               /* The page being overwritten. */
 43730   Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
 43731   int rc;                      /* Return code */
 43732   Pgno origPgno;               /* The original page number */
 43734   assert( pPg->nRef>0 );
 43735   assert( pPager->eState==PAGER_WRITER_CACHEMOD
 43736        || pPager->eState==PAGER_WRITER_DBMOD
 43737   );
 43738   assert( assert_pager_state(pPager) );
 43740   /* In order to be able to rollback, an in-memory database must journal
 43741   ** the page we are moving from.
 43742   */
 43743   if( MEMDB ){
 43744     rc = sqlite3PagerWrite(pPg);
 43745     if( rc ) return rc;
 43748   /* If the page being moved is dirty and has not been saved by the latest
 43749   ** savepoint, then save the current contents of the page into the 
 43750   ** sub-journal now. This is required to handle the following scenario:
 43751   **
 43752   **   BEGIN;
 43753   **     <journal page X, then modify it in memory>
 43754   **     SAVEPOINT one;
 43755   **       <Move page X to location Y>
 43756   **     ROLLBACK TO one;
 43757   **
 43758   ** If page X were not written to the sub-journal here, it would not
 43759   ** be possible to restore its contents when the "ROLLBACK TO one"
 43760   ** statement were is processed.
 43761   **
 43762   ** subjournalPage() may need to allocate space to store pPg->pgno into
 43763   ** one or more savepoint bitvecs. This is the reason this function
 43764   ** may return SQLITE_NOMEM.
 43765   */
 43766   if( pPg->flags&PGHDR_DIRTY
 43767    && subjRequiresPage(pPg)
 43768    && SQLITE_OK!=(rc = subjournalPage(pPg))
 43769   ){
 43770     return rc;
 43773   PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 
 43774       PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
 43775   IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
 43777   /* If the journal needs to be sync()ed before page pPg->pgno can
 43778   ** be written to, store pPg->pgno in local variable needSyncPgno.
 43779   **
 43780   ** If the isCommit flag is set, there is no need to remember that
 43781   ** the journal needs to be sync()ed before database page pPg->pgno 
 43782   ** can be written to. The caller has already promised not to write to it.
 43783   */
 43784   if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
 43785     needSyncPgno = pPg->pgno;
 43786     assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
 43787     assert( pPg->flags&PGHDR_DIRTY );
 43790   /* If the cache contains a page with page-number pgno, remove it
 43791   ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
 43792   ** page pgno before the 'move' operation, it needs to be retained 
 43793   ** for the page moved there.
 43794   */
 43795   pPg->flags &= ~PGHDR_NEED_SYNC;
 43796   pPgOld = pager_lookup(pPager, pgno);
 43797   assert( !pPgOld || pPgOld->nRef==1 );
 43798   if( pPgOld ){
 43799     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
 43800     if( MEMDB ){
 43801       /* Do not discard pages from an in-memory database since we might
 43802       ** need to rollback later.  Just move the page out of the way. */
 43803       sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
 43804     }else{
 43805       sqlite3PcacheDrop(pPgOld);
 43809   origPgno = pPg->pgno;
 43810   sqlite3PcacheMove(pPg, pgno);
 43811   sqlite3PcacheMakeDirty(pPg);
 43813   /* For an in-memory database, make sure the original page continues
 43814   ** to exist, in case the transaction needs to roll back.  Use pPgOld
 43815   ** as the original page since it has already been allocated.
 43816   */
 43817   if( MEMDB ){
 43818     assert( pPgOld );
 43819     sqlite3PcacheMove(pPgOld, origPgno);
 43820     sqlite3PagerUnref(pPgOld);
 43823   if( needSyncPgno ){
 43824     /* If needSyncPgno is non-zero, then the journal file needs to be 
 43825     ** sync()ed before any data is written to database file page needSyncPgno.
 43826     ** Currently, no such page exists in the page-cache and the 
 43827     ** "is journaled" bitvec flag has been set. This needs to be remedied by
 43828     ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
 43829     ** flag.
 43830     **
 43831     ** If the attempt to load the page into the page-cache fails, (due
 43832     ** to a malloc() or IO failure), clear the bit in the pInJournal[]
 43833     ** array. Otherwise, if the page is loaded and written again in
 43834     ** this transaction, it may be written to the database file before
 43835     ** it is synced into the journal file. This way, it may end up in
 43836     ** the journal file twice, but that is not a problem.
 43837     */
 43838     PgHdr *pPgHdr;
 43839     rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
 43840     if( rc!=SQLITE_OK ){
 43841       if( needSyncPgno<=pPager->dbOrigSize ){
 43842         assert( pPager->pTmpSpace!=0 );
 43843         sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
 43845       return rc;
 43847     pPgHdr->flags |= PGHDR_NEED_SYNC;
 43848     sqlite3PcacheMakeDirty(pPgHdr);
 43849     sqlite3PagerUnref(pPgHdr);
 43852   return SQLITE_OK;
 43854 #endif
 43856 /*
 43857 ** Return a pointer to the data for the specified page.
 43858 */
 43859 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
 43860   assert( pPg->nRef>0 || pPg->pPager->memDb );
 43861   return pPg->pData;
 43864 /*
 43865 ** Return a pointer to the Pager.nExtra bytes of "extra" space 
 43866 ** allocated along with the specified page.
 43867 */
 43868 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
 43869   return pPg->pExtra;
 43872 /*
 43873 ** Get/set the locking-mode for this pager. Parameter eMode must be one
 43874 ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
 43875 ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
 43876 ** the locking-mode is set to the value specified.
 43877 **
 43878 ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
 43879 ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
 43880 ** locking-mode.
 43881 */
 43882 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
 43883   assert( eMode==PAGER_LOCKINGMODE_QUERY
 43884             || eMode==PAGER_LOCKINGMODE_NORMAL
 43885             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
 43886   assert( PAGER_LOCKINGMODE_QUERY<0 );
 43887   assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
 43888   assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
 43889   if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
 43890     pPager->exclusiveMode = (u8)eMode;
 43892   return (int)pPager->exclusiveMode;
 43895 /*
 43896 ** Set the journal-mode for this pager. Parameter eMode must be one of:
 43897 **
 43898 **    PAGER_JOURNALMODE_DELETE
 43899 **    PAGER_JOURNALMODE_TRUNCATE
 43900 **    PAGER_JOURNALMODE_PERSIST
 43901 **    PAGER_JOURNALMODE_OFF
 43902 **    PAGER_JOURNALMODE_MEMORY
 43903 **    PAGER_JOURNALMODE_WAL
 43904 **
 43905 ** The journalmode is set to the value specified if the change is allowed.
 43906 ** The change may be disallowed for the following reasons:
 43907 **
 43908 **   *  An in-memory database can only have its journal_mode set to _OFF
 43909 **      or _MEMORY.
 43910 **
 43911 **   *  Temporary databases cannot have _WAL journalmode.
 43912 **
 43913 ** The returned indicate the current (possibly updated) journal-mode.
 43914 */
 43915 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
 43916   u8 eOld = pPager->journalMode;    /* Prior journalmode */
 43918 #ifdef SQLITE_DEBUG
 43919   /* The print_pager_state() routine is intended to be used by the debugger
 43920   ** only.  We invoke it once here to suppress a compiler warning. */
 43921   print_pager_state(pPager);
 43922 #endif
 43925   /* The eMode parameter is always valid */
 43926   assert(      eMode==PAGER_JOURNALMODE_DELETE
 43927             || eMode==PAGER_JOURNALMODE_TRUNCATE
 43928             || eMode==PAGER_JOURNALMODE_PERSIST
 43929             || eMode==PAGER_JOURNALMODE_OFF 
 43930             || eMode==PAGER_JOURNALMODE_WAL 
 43931             || eMode==PAGER_JOURNALMODE_MEMORY );
 43933   /* This routine is only called from the OP_JournalMode opcode, and
 43934   ** the logic there will never allow a temporary file to be changed
 43935   ** to WAL mode.
 43936   */
 43937   assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
 43939   /* Do allow the journalmode of an in-memory database to be set to
 43940   ** anything other than MEMORY or OFF
 43941   */
 43942   if( MEMDB ){
 43943     assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
 43944     if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
 43945       eMode = eOld;
 43949   if( eMode!=eOld ){
 43951     /* Change the journal mode. */
 43952     assert( pPager->eState!=PAGER_ERROR );
 43953     pPager->journalMode = (u8)eMode;
 43955     /* When transistioning from TRUNCATE or PERSIST to any other journal
 43956     ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
 43957     ** delete the journal file.
 43958     */
 43959     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
 43960     assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
 43961     assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
 43962     assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
 43963     assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
 43964     assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
 43966     assert( isOpen(pPager->fd) || pPager->exclusiveMode );
 43967     if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
 43969       /* In this case we would like to delete the journal file. If it is
 43970       ** not possible, then that is not a problem. Deleting the journal file
 43971       ** here is an optimization only.
 43972       **
 43973       ** Before deleting the journal file, obtain a RESERVED lock on the
 43974       ** database file. This ensures that the journal file is not deleted
 43975       ** while it is in use by some other client.
 43976       */
 43977       sqlite3OsClose(pPager->jfd);
 43978       if( pPager->eLock>=RESERVED_LOCK ){
 43979         sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 43980       }else{
 43981         int rc = SQLITE_OK;
 43982         int state = pPager->eState;
 43983         assert( state==PAGER_OPEN || state==PAGER_READER );
 43984         if( state==PAGER_OPEN ){
 43985           rc = sqlite3PagerSharedLock(pPager);
 43987         if( pPager->eState==PAGER_READER ){
 43988           assert( rc==SQLITE_OK );
 43989           rc = pagerLockDb(pPager, RESERVED_LOCK);
 43991         if( rc==SQLITE_OK ){
 43992           sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 43994         if( rc==SQLITE_OK && state==PAGER_READER ){
 43995           pagerUnlockDb(pPager, SHARED_LOCK);
 43996         }else if( state==PAGER_OPEN ){
 43997           pager_unlock(pPager);
 43999         assert( state==pPager->eState );
 44004   /* Return the new journal mode */
 44005   return (int)pPager->journalMode;
 44008 /*
 44009 ** Return the current journal mode.
 44010 */
 44011 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
 44012   return (int)pPager->journalMode;
 44015 /*
 44016 ** Return TRUE if the pager is in a state where it is OK to change the
 44017 ** journalmode.  Journalmode changes can only happen when the database
 44018 ** is unmodified.
 44019 */
 44020 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
 44021   assert( assert_pager_state(pPager) );
 44022   if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
 44023   if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
 44024   return 1;
 44027 /*
 44028 ** Get/set the size-limit used for persistent journal files.
 44029 **
 44030 ** Setting the size limit to -1 means no limit is enforced.
 44031 ** An attempt to set a limit smaller than -1 is a no-op.
 44032 */
 44033 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
 44034   if( iLimit>=-1 ){
 44035     pPager->journalSizeLimit = iLimit;
 44036     sqlite3WalLimit(pPager->pWal, iLimit);
 44038   return pPager->journalSizeLimit;
 44041 /*
 44042 ** Return a pointer to the pPager->pBackup variable. The backup module
 44043 ** in backup.c maintains the content of this variable. This module
 44044 ** uses it opaquely as an argument to sqlite3BackupRestart() and
 44045 ** sqlite3BackupUpdate() only.
 44046 */
 44047 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
 44048   return &pPager->pBackup;
 44051 #ifndef SQLITE_OMIT_VACUUM
 44052 /*
 44053 ** Unless this is an in-memory or temporary database, clear the pager cache.
 44054 */
 44055 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
 44056   if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
 44058 #endif
 44060 #ifndef SQLITE_OMIT_WAL
 44061 /*
 44062 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
 44063 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
 44064 ** or wal_blocking_checkpoint() API functions.
 44065 **
 44066 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 44067 */
 44068 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
 44069   int rc = SQLITE_OK;
 44070   if( pPager->pWal ){
 44071     rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
 44072         pPager->xBusyHandler, pPager->pBusyHandlerArg,
 44073         pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 44074         pnLog, pnCkpt
 44075     );
 44077   return rc;
 44080 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 44081   return sqlite3WalCallback(pPager->pWal);
 44084 /*
 44085 ** Return true if the underlying VFS for the given pager supports the
 44086 ** primitives necessary for write-ahead logging.
 44087 */
 44088 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
 44089   const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
 44090   return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
 44093 /*
 44094 ** Attempt to take an exclusive lock on the database file. If a PENDING lock
 44095 ** is obtained instead, immediately release it.
 44096 */
 44097 static int pagerExclusiveLock(Pager *pPager){
 44098   int rc;                         /* Return code */
 44100   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
 44101   rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 44102   if( rc!=SQLITE_OK ){
 44103     /* If the attempt to grab the exclusive lock failed, release the 
 44104     ** pending lock that may have been obtained instead.  */
 44105     pagerUnlockDb(pPager, SHARED_LOCK);
 44108   return rc;
 44111 /*
 44112 ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in 
 44113 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
 44114 ** lock on the database file and use heap-memory to store the wal-index
 44115 ** in. Otherwise, use the normal shared-memory.
 44116 */
 44117 static int pagerOpenWal(Pager *pPager){
 44118   int rc = SQLITE_OK;
 44120   assert( pPager->pWal==0 && pPager->tempFile==0 );
 44121   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
 44123   /* If the pager is already in exclusive-mode, the WAL module will use 
 44124   ** heap-memory for the wal-index instead of the VFS shared-memory 
 44125   ** implementation. Take the exclusive lock now, before opening the WAL
 44126   ** file, to make sure this is safe.
 44127   */
 44128   if( pPager->exclusiveMode ){
 44129     rc = pagerExclusiveLock(pPager);
 44132   /* Open the connection to the log file. If this operation fails, 
 44133   ** (e.g. due to malloc() failure), return an error code.
 44134   */
 44135   if( rc==SQLITE_OK ){
 44136     rc = sqlite3WalOpen(pPager->pVfs, 
 44137         pPager->fd, pPager->zWal, pPager->exclusiveMode,
 44138         pPager->journalSizeLimit, &pPager->pWal
 44139     );
 44142   return rc;
 44146 /*
 44147 ** The caller must be holding a SHARED lock on the database file to call
 44148 ** this function.
 44149 **
 44150 ** If the pager passed as the first argument is open on a real database
 44151 ** file (not a temp file or an in-memory database), and the WAL file
 44152 ** is not already open, make an attempt to open it now. If successful,
 44153 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does 
 44154 ** not support the xShmXXX() methods, return an error code. *pbOpen is
 44155 ** not modified in either case.
 44156 **
 44157 ** If the pager is open on a temp-file (or in-memory database), or if
 44158 ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
 44159 ** without doing anything.
 44160 */
 44161 SQLITE_PRIVATE int sqlite3PagerOpenWal(
 44162   Pager *pPager,                  /* Pager object */
 44163   int *pbOpen                     /* OUT: Set to true if call is a no-op */
 44164 ){
 44165   int rc = SQLITE_OK;             /* Return code */
 44167   assert( assert_pager_state(pPager) );
 44168   assert( pPager->eState==PAGER_OPEN   || pbOpen );
 44169   assert( pPager->eState==PAGER_READER || !pbOpen );
 44170   assert( pbOpen==0 || *pbOpen==0 );
 44171   assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
 44173   if( !pPager->tempFile && !pPager->pWal ){
 44174     if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
 44176     /* Close any rollback journal previously open */
 44177     sqlite3OsClose(pPager->jfd);
 44179     rc = pagerOpenWal(pPager);
 44180     if( rc==SQLITE_OK ){
 44181       pPager->journalMode = PAGER_JOURNALMODE_WAL;
 44182       pPager->eState = PAGER_OPEN;
 44184   }else{
 44185     *pbOpen = 1;
 44188   return rc;
 44191 /*
 44192 ** This function is called to close the connection to the log file prior
 44193 ** to switching from WAL to rollback mode.
 44194 **
 44195 ** Before closing the log file, this function attempts to take an 
 44196 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
 44197 ** error (SQLITE_BUSY) is returned and the log connection is not closed.
 44198 ** If successful, the EXCLUSIVE lock is not released before returning.
 44199 */
 44200 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
 44201   int rc = SQLITE_OK;
 44203   assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
 44205   /* If the log file is not already open, but does exist in the file-system,
 44206   ** it may need to be checkpointed before the connection can switch to
 44207   ** rollback mode. Open it now so this can happen.
 44208   */
 44209   if( !pPager->pWal ){
 44210     int logexists = 0;
 44211     rc = pagerLockDb(pPager, SHARED_LOCK);
 44212     if( rc==SQLITE_OK ){
 44213       rc = sqlite3OsAccess(
 44214           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
 44215       );
 44217     if( rc==SQLITE_OK && logexists ){
 44218       rc = pagerOpenWal(pPager);
 44222   /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
 44223   ** the database file, the log and log-summary files will be deleted.
 44224   */
 44225   if( rc==SQLITE_OK && pPager->pWal ){
 44226     rc = pagerExclusiveLock(pPager);
 44227     if( rc==SQLITE_OK ){
 44228       rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
 44229                            pPager->pageSize, (u8*)pPager->pTmpSpace);
 44230       pPager->pWal = 0;
 44233   return rc;
 44236 #endif /* !SQLITE_OMIT_WAL */
 44238 #ifdef SQLITE_ENABLE_ZIPVFS
 44239 /*
 44240 ** A read-lock must be held on the pager when this function is called. If
 44241 ** the pager is in WAL mode and the WAL file currently contains one or more
 44242 ** frames, return the size in bytes of the page images stored within the
 44243 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
 44244 ** is empty, return 0.
 44245 */
 44246 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
 44247   assert( pPager->eState==PAGER_READER );
 44248   return sqlite3WalFramesize(pPager->pWal);
 44250 #endif
 44252 #ifdef SQLITE_HAS_CODEC
 44253 /*
 44254 ** This function is called by the wal module when writing page content
 44255 ** into the log file.
 44256 **
 44257 ** This function returns a pointer to a buffer containing the encrypted
 44258 ** page content. If a malloc fails, this function may return NULL.
 44259 */
 44260 SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
 44261   void *aData = 0;
 44262   CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
 44263   return aData;
 44265 #endif /* SQLITE_HAS_CODEC */
 44267 #endif /* SQLITE_OMIT_DISKIO */
 44269 /************** End of pager.c ***********************************************/
 44270 /************** Begin file wal.c *********************************************/
 44271 /*
 44272 ** 2010 February 1
 44273 **
 44274 ** The author disclaims copyright to this source code.  In place of
 44275 ** a legal notice, here is a blessing:
 44276 **
 44277 **    May you do good and not evil.
 44278 **    May you find forgiveness for yourself and forgive others.
 44279 **    May you share freely, never taking more than you give.
 44280 **
 44281 *************************************************************************
 44282 **
 44283 ** This file contains the implementation of a write-ahead log (WAL) used in 
 44284 ** "journal_mode=WAL" mode.
 44285 **
 44286 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
 44287 **
 44288 ** A WAL file consists of a header followed by zero or more "frames".
 44289 ** Each frame records the revised content of a single page from the
 44290 ** database file.  All changes to the database are recorded by writing
 44291 ** frames into the WAL.  Transactions commit when a frame is written that
 44292 ** contains a commit marker.  A single WAL can and usually does record 
 44293 ** multiple transactions.  Periodically, the content of the WAL is
 44294 ** transferred back into the database file in an operation called a
 44295 ** "checkpoint".
 44296 **
 44297 ** A single WAL file can be used multiple times.  In other words, the
 44298 ** WAL can fill up with frames and then be checkpointed and then new
 44299 ** frames can overwrite the old ones.  A WAL always grows from beginning
 44300 ** toward the end.  Checksums and counters attached to each frame are
 44301 ** used to determine which frames within the WAL are valid and which
 44302 ** are leftovers from prior checkpoints.
 44303 **
 44304 ** The WAL header is 32 bytes in size and consists of the following eight
 44305 ** big-endian 32-bit unsigned integer values:
 44306 **
 44307 **     0: Magic number.  0x377f0682 or 0x377f0683
 44308 **     4: File format version.  Currently 3007000
 44309 **     8: Database page size.  Example: 1024
 44310 **    12: Checkpoint sequence number
 44311 **    16: Salt-1, random integer incremented with each checkpoint
 44312 **    20: Salt-2, a different random integer changing with each ckpt
 44313 **    24: Checksum-1 (first part of checksum for first 24 bytes of header).
 44314 **    28: Checksum-2 (second part of checksum for first 24 bytes of header).
 44315 **
 44316 ** Immediately following the wal-header are zero or more frames. Each
 44317 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
 44318 ** of page data. The frame-header is six big-endian 32-bit unsigned 
 44319 ** integer values, as follows:
 44320 **
 44321 **     0: Page number.
 44322 **     4: For commit records, the size of the database image in pages 
 44323 **        after the commit. For all other records, zero.
 44324 **     8: Salt-1 (copied from the header)
 44325 **    12: Salt-2 (copied from the header)
 44326 **    16: Checksum-1.
 44327 **    20: Checksum-2.
 44328 **
 44329 ** A frame is considered valid if and only if the following conditions are
 44330 ** true:
 44331 **
 44332 **    (1) The salt-1 and salt-2 values in the frame-header match
 44333 **        salt values in the wal-header
 44334 **
 44335 **    (2) The checksum values in the final 8 bytes of the frame-header
 44336 **        exactly match the checksum computed consecutively on the
 44337 **        WAL header and the first 8 bytes and the content of all frames
 44338 **        up to and including the current frame.
 44339 **
 44340 ** The checksum is computed using 32-bit big-endian integers if the
 44341 ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
 44342 ** is computed using little-endian if the magic number is 0x377f0682.
 44343 ** The checksum values are always stored in the frame header in a
 44344 ** big-endian format regardless of which byte order is used to compute
 44345 ** the checksum.  The checksum is computed by interpreting the input as
 44346 ** an even number of unsigned 32-bit integers: x[0] through x[N].  The
 44347 ** algorithm used for the checksum is as follows:
 44348 ** 
 44349 **   for i from 0 to n-1 step 2:
 44350 **     s0 += x[i] + s1;
 44351 **     s1 += x[i+1] + s0;
 44352 **   endfor
 44353 **
 44354 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
 44355 ** in reverse order (the largest fibonacci weight occurs on the first element
 44356 ** of the sequence being summed.)  The s1 value spans all 32-bit 
 44357 ** terms of the sequence whereas s0 omits the final term.
 44358 **
 44359 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
 44360 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
 44361 ** The VFS.xSync operations serve as write barriers - all writes launched
 44362 ** before the xSync must complete before any write that launches after the
 44363 ** xSync begins.
 44364 **
 44365 ** After each checkpoint, the salt-1 value is incremented and the salt-2
 44366 ** value is randomized.  This prevents old and new frames in the WAL from
 44367 ** being considered valid at the same time and being checkpointing together
 44368 ** following a crash.
 44369 **
 44370 ** READER ALGORITHM
 44371 **
 44372 ** To read a page from the database (call it page number P), a reader
 44373 ** first checks the WAL to see if it contains page P.  If so, then the
 44374 ** last valid instance of page P that is a followed by a commit frame
 44375 ** or is a commit frame itself becomes the value read.  If the WAL
 44376 ** contains no copies of page P that are valid and which are a commit
 44377 ** frame or are followed by a commit frame, then page P is read from
 44378 ** the database file.
 44379 **
 44380 ** To start a read transaction, the reader records the index of the last
 44381 ** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
 44382 ** for all subsequent read operations.  New transactions can be appended
 44383 ** to the WAL, but as long as the reader uses its original mxFrame value
 44384 ** and ignores the newly appended content, it will see a consistent snapshot
 44385 ** of the database from a single point in time.  This technique allows
 44386 ** multiple concurrent readers to view different versions of the database
 44387 ** content simultaneously.
 44388 **
 44389 ** The reader algorithm in the previous paragraphs works correctly, but 
 44390 ** because frames for page P can appear anywhere within the WAL, the
 44391 ** reader has to scan the entire WAL looking for page P frames.  If the
 44392 ** WAL is large (multiple megabytes is typical) that scan can be slow,
 44393 ** and read performance suffers.  To overcome this problem, a separate
 44394 ** data structure called the wal-index is maintained to expedite the
 44395 ** search for frames of a particular page.
 44396 ** 
 44397 ** WAL-INDEX FORMAT
 44398 **
 44399 ** Conceptually, the wal-index is shared memory, though VFS implementations
 44400 ** might choose to implement the wal-index using a mmapped file.  Because
 44401 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL 
 44402 ** on a network filesystem.  All users of the database must be able to
 44403 ** share memory.
 44404 **
 44405 ** The wal-index is transient.  After a crash, the wal-index can (and should
 44406 ** be) reconstructed from the original WAL file.  In fact, the VFS is required
 44407 ** to either truncate or zero the header of the wal-index when the last
 44408 ** connection to it closes.  Because the wal-index is transient, it can
 44409 ** use an architecture-specific format; it does not have to be cross-platform.
 44410 ** Hence, unlike the database and WAL file formats which store all values
 44411 ** as big endian, the wal-index can store multi-byte values in the native
 44412 ** byte order of the host computer.
 44413 **
 44414 ** The purpose of the wal-index is to answer this question quickly:  Given
 44415 ** a page number P and a maximum frame index M, return the index of the 
 44416 ** last frame in the wal before frame M for page P in the WAL, or return
 44417 ** NULL if there are no frames for page P in the WAL prior to M.
 44418 **
 44419 ** The wal-index consists of a header region, followed by an one or
 44420 ** more index blocks.  
 44421 **
 44422 ** The wal-index header contains the total number of frames within the WAL
 44423 ** in the mxFrame field.
 44424 **
 44425 ** Each index block except for the first contains information on 
 44426 ** HASHTABLE_NPAGE frames. The first index block contains information on
 44427 ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and 
 44428 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
 44429 ** first index block are the same size as all other index blocks in the
 44430 ** wal-index.
 44431 **
 44432 ** Each index block contains two sections, a page-mapping that contains the
 44433 ** database page number associated with each wal frame, and a hash-table 
 44434 ** that allows readers to query an index block for a specific page number.
 44435 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
 44436 ** for the first index block) 32-bit page numbers. The first entry in the 
 44437 ** first index-block contains the database page number corresponding to the
 44438 ** first frame in the WAL file. The first entry in the second index block
 44439 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
 44440 ** the log, and so on.
 44441 **
 44442 ** The last index block in a wal-index usually contains less than the full
 44443 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
 44444 ** depending on the contents of the WAL file. This does not change the
 44445 ** allocated size of the page-mapping array - the page-mapping array merely
 44446 ** contains unused entries.
 44447 **
 44448 ** Even without using the hash table, the last frame for page P
 44449 ** can be found by scanning the page-mapping sections of each index block
 44450 ** starting with the last index block and moving toward the first, and
 44451 ** within each index block, starting at the end and moving toward the
 44452 ** beginning.  The first entry that equals P corresponds to the frame
 44453 ** holding the content for that page.
 44454 **
 44455 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
 44456 ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
 44457 ** hash table for each page number in the mapping section, so the hash 
 44458 ** table is never more than half full.  The expected number of collisions 
 44459 ** prior to finding a match is 1.  Each entry of the hash table is an
 44460 ** 1-based index of an entry in the mapping section of the same
 44461 ** index block.   Let K be the 1-based index of the largest entry in
 44462 ** the mapping section.  (For index blocks other than the last, K will
 44463 ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
 44464 ** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
 44465 ** contain a value of 0.
 44466 **
 44467 ** To look for page P in the hash table, first compute a hash iKey on
 44468 ** P as follows:
 44469 **
 44470 **      iKey = (P * 383) % HASHTABLE_NSLOT
 44471 **
 44472 ** Then start scanning entries of the hash table, starting with iKey
 44473 ** (wrapping around to the beginning when the end of the hash table is
 44474 ** reached) until an unused hash slot is found. Let the first unused slot
 44475 ** be at index iUnused.  (iUnused might be less than iKey if there was
 44476 ** wrap-around.) Because the hash table is never more than half full,
 44477 ** the search is guaranteed to eventually hit an unused entry.  Let 
 44478 ** iMax be the value between iKey and iUnused, closest to iUnused,
 44479 ** where aHash[iMax]==P.  If there is no iMax entry (if there exists
 44480 ** no hash slot such that aHash[i]==p) then page P is not in the
 44481 ** current index block.  Otherwise the iMax-th mapping entry of the
 44482 ** current index block corresponds to the last entry that references 
 44483 ** page P.
 44484 **
 44485 ** A hash search begins with the last index block and moves toward the
 44486 ** first index block, looking for entries corresponding to page P.  On
 44487 ** average, only two or three slots in each index block need to be
 44488 ** examined in order to either find the last entry for page P, or to
 44489 ** establish that no such entry exists in the block.  Each index block
 44490 ** holds over 4000 entries.  So two or three index blocks are sufficient
 44491 ** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
 44492 ** comparisons (on average) suffice to either locate a frame in the
 44493 ** WAL or to establish that the frame does not exist in the WAL.  This
 44494 ** is much faster than scanning the entire 10MB WAL.
 44495 **
 44496 ** Note that entries are added in order of increasing K.  Hence, one
 44497 ** reader might be using some value K0 and a second reader that started
 44498 ** at a later time (after additional transactions were added to the WAL
 44499 ** and to the wal-index) might be using a different value K1, where K1>K0.
 44500 ** Both readers can use the same hash table and mapping section to get
 44501 ** the correct result.  There may be entries in the hash table with
 44502 ** K>K0 but to the first reader, those entries will appear to be unused
 44503 ** slots in the hash table and so the first reader will get an answer as
 44504 ** if no values greater than K0 had ever been inserted into the hash table
 44505 ** in the first place - which is what reader one wants.  Meanwhile, the
 44506 ** second reader using K1 will see additional values that were inserted
 44507 ** later, which is exactly what reader two wants.  
 44508 **
 44509 ** When a rollback occurs, the value of K is decreased. Hash table entries
 44510 ** that correspond to frames greater than the new K value are removed
 44511 ** from the hash table at this point.
 44512 */
 44513 #ifndef SQLITE_OMIT_WAL
 44516 /*
 44517 ** Trace output macros
 44518 */
 44519 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 44520 SQLITE_PRIVATE int sqlite3WalTrace = 0;
 44521 # define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
 44522 #else
 44523 # define WALTRACE(X)
 44524 #endif
 44526 /*
 44527 ** The maximum (and only) versions of the wal and wal-index formats
 44528 ** that may be interpreted by this version of SQLite.
 44529 **
 44530 ** If a client begins recovering a WAL file and finds that (a) the checksum
 44531 ** values in the wal-header are correct and (b) the version field is not
 44532 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
 44533 **
 44534 ** Similarly, if a client successfully reads a wal-index header (i.e. the 
 44535 ** checksum test is successful) and finds that the version field is not
 44536 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
 44537 ** returns SQLITE_CANTOPEN.
 44538 */
 44539 #define WAL_MAX_VERSION      3007000
 44540 #define WALINDEX_MAX_VERSION 3007000
 44542 /*
 44543 ** Indices of various locking bytes.   WAL_NREADER is the number
 44544 ** of available reader locks and should be at least 3.
 44545 */
 44546 #define WAL_WRITE_LOCK         0
 44547 #define WAL_ALL_BUT_WRITE      1
 44548 #define WAL_CKPT_LOCK          1
 44549 #define WAL_RECOVER_LOCK       2
 44550 #define WAL_READ_LOCK(I)       (3+(I))
 44551 #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
 44554 /* Object declarations */
 44555 typedef struct WalIndexHdr WalIndexHdr;
 44556 typedef struct WalIterator WalIterator;
 44557 typedef struct WalCkptInfo WalCkptInfo;
 44560 /*
 44561 ** The following object holds a copy of the wal-index header content.
 44562 **
 44563 ** The actual header in the wal-index consists of two copies of this
 44564 ** object.
 44565 **
 44566 ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
 44567 ** Or it can be 1 to represent a 65536-byte page.  The latter case was
 44568 ** added in 3.7.1 when support for 64K pages was added.  
 44569 */
 44570 struct WalIndexHdr {
 44571   u32 iVersion;                   /* Wal-index version */
 44572   u32 unused;                     /* Unused (padding) field */
 44573   u32 iChange;                    /* Counter incremented each transaction */
 44574   u8 isInit;                      /* 1 when initialized */
 44575   u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
 44576   u16 szPage;                     /* Database page size in bytes. 1==64K */
 44577   u32 mxFrame;                    /* Index of last valid frame in the WAL */
 44578   u32 nPage;                      /* Size of database in pages */
 44579   u32 aFrameCksum[2];             /* Checksum of last frame in log */
 44580   u32 aSalt[2];                   /* Two salt values copied from WAL header */
 44581   u32 aCksum[2];                  /* Checksum over all prior fields */
 44582 };
 44584 /*
 44585 ** A copy of the following object occurs in the wal-index immediately
 44586 ** following the second copy of the WalIndexHdr.  This object stores
 44587 ** information used by checkpoint.
 44588 **
 44589 ** nBackfill is the number of frames in the WAL that have been written
 44590 ** back into the database. (We call the act of moving content from WAL to
 44591 ** database "backfilling".)  The nBackfill number is never greater than
 44592 ** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
 44593 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
 44594 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
 44595 ** mxFrame back to zero when the WAL is reset.
 44596 **
 44597 ** There is one entry in aReadMark[] for each reader lock.  If a reader
 44598 ** holds read-lock K, then the value in aReadMark[K] is no greater than
 44599 ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
 44600 ** for any aReadMark[] means that entry is unused.  aReadMark[0] is 
 44601 ** a special case; its value is never used and it exists as a place-holder
 44602 ** to avoid having to offset aReadMark[] indexs by one.  Readers holding
 44603 ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
 44604 ** directly from the database.
 44605 **
 44606 ** The value of aReadMark[K] may only be changed by a thread that
 44607 ** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
 44608 ** aReadMark[K] cannot changed while there is a reader is using that mark
 44609 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
 44610 **
 44611 ** The checkpointer may only transfer frames from WAL to database where
 44612 ** the frame numbers are less than or equal to every aReadMark[] that is
 44613 ** in use (that is, every aReadMark[j] for which there is a corresponding
 44614 ** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
 44615 ** largest value and will increase an unused aReadMark[] to mxFrame if there
 44616 ** is not already an aReadMark[] equal to mxFrame.  The exception to the
 44617 ** previous sentence is when nBackfill equals mxFrame (meaning that everything
 44618 ** in the WAL has been backfilled into the database) then new readers
 44619 ** will choose aReadMark[0] which has value 0 and hence such reader will
 44620 ** get all their all content directly from the database file and ignore 
 44621 ** the WAL.
 44622 **
 44623 ** Writers normally append new frames to the end of the WAL.  However,
 44624 ** if nBackfill equals mxFrame (meaning that all WAL content has been
 44625 ** written back into the database) and if no readers are using the WAL
 44626 ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
 44627 ** the writer will first "reset" the WAL back to the beginning and start
 44628 ** writing new content beginning at frame 1.
 44629 **
 44630 ** We assume that 32-bit loads are atomic and so no locks are needed in
 44631 ** order to read from any aReadMark[] entries.
 44632 */
 44633 struct WalCkptInfo {
 44634   u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
 44635   u32 aReadMark[WAL_NREADER];     /* Reader marks */
 44636 };
 44637 #define READMARK_NOT_USED  0xffffffff
 44640 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
 44641 ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
 44642 ** only support mandatory file-locks, we do not read or write data
 44643 ** from the region of the file on which locks are applied.
 44644 */
 44645 #define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
 44646 #define WALINDEX_LOCK_RESERVED 16
 44647 #define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
 44649 /* Size of header before each frame in wal */
 44650 #define WAL_FRAME_HDRSIZE 24
 44652 /* Size of write ahead log header, including checksum. */
 44653 /* #define WAL_HDRSIZE 24 */
 44654 #define WAL_HDRSIZE 32
 44656 /* WAL magic value. Either this value, or the same value with the least
 44657 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
 44658 ** big-endian format in the first 4 bytes of a WAL file.
 44659 **
 44660 ** If the LSB is set, then the checksums for each frame within the WAL
 44661 ** file are calculated by treating all data as an array of 32-bit 
 44662 ** big-endian words. Otherwise, they are calculated by interpreting 
 44663 ** all data as 32-bit little-endian words.
 44664 */
 44665 #define WAL_MAGIC 0x377f0682
 44667 /*
 44668 ** Return the offset of frame iFrame in the write-ahead log file, 
 44669 ** assuming a database page size of szPage bytes. The offset returned
 44670 ** is to the start of the write-ahead log frame-header.
 44671 */
 44672 #define walFrameOffset(iFrame, szPage) (                               \
 44673   WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
 44676 /*
 44677 ** An open write-ahead log file is represented by an instance of the
 44678 ** following object.
 44679 */
 44680 struct Wal {
 44681   sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
 44682   sqlite3_file *pDbFd;       /* File handle for the database file */
 44683   sqlite3_file *pWalFd;      /* File handle for WAL file */
 44684   u32 iCallback;             /* Value to pass to log callback (or 0) */
 44685   i64 mxWalSize;             /* Truncate WAL to this size upon reset */
 44686   int nWiData;               /* Size of array apWiData */
 44687   int szFirstBlock;          /* Size of first block written to WAL file */
 44688   volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
 44689   u32 szPage;                /* Database page size */
 44690   i16 readLock;              /* Which read lock is being held.  -1 for none */
 44691   u8 syncFlags;              /* Flags to use to sync header writes */
 44692   u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
 44693   u8 writeLock;              /* True if in a write transaction */
 44694   u8 ckptLock;               /* True if holding a checkpoint lock */
 44695   u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
 44696   u8 truncateOnCommit;       /* True to truncate WAL file on commit */
 44697   u8 syncHeader;             /* Fsync the WAL header if true */
 44698   u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
 44699   WalIndexHdr hdr;           /* Wal-index header for current transaction */
 44700   const char *zWalName;      /* Name of WAL file */
 44701   u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
 44702 #ifdef SQLITE_DEBUG
 44703   u8 lockError;              /* True if a locking error has occurred */
 44704 #endif
 44705 };
 44707 /*
 44708 ** Candidate values for Wal.exclusiveMode.
 44709 */
 44710 #define WAL_NORMAL_MODE     0
 44711 #define WAL_EXCLUSIVE_MODE  1     
 44712 #define WAL_HEAPMEMORY_MODE 2
 44714 /*
 44715 ** Possible values for WAL.readOnly
 44716 */
 44717 #define WAL_RDWR        0    /* Normal read/write connection */
 44718 #define WAL_RDONLY      1    /* The WAL file is readonly */
 44719 #define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
 44721 /*
 44722 ** Each page of the wal-index mapping contains a hash-table made up of
 44723 ** an array of HASHTABLE_NSLOT elements of the following type.
 44724 */
 44725 typedef u16 ht_slot;
 44727 /*
 44728 ** This structure is used to implement an iterator that loops through
 44729 ** all frames in the WAL in database page order. Where two or more frames
 44730 ** correspond to the same database page, the iterator visits only the 
 44731 ** frame most recently written to the WAL (in other words, the frame with
 44732 ** the largest index).
 44733 **
 44734 ** The internals of this structure are only accessed by:
 44735 **
 44736 **   walIteratorInit() - Create a new iterator,
 44737 **   walIteratorNext() - Step an iterator,
 44738 **   walIteratorFree() - Free an iterator.
 44739 **
 44740 ** This functionality is used by the checkpoint code (see walCheckpoint()).
 44741 */
 44742 struct WalIterator {
 44743   int iPrior;                     /* Last result returned from the iterator */
 44744   int nSegment;                   /* Number of entries in aSegment[] */
 44745   struct WalSegment {
 44746     int iNext;                    /* Next slot in aIndex[] not yet returned */
 44747     ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
 44748     u32 *aPgno;                   /* Array of page numbers. */
 44749     int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
 44750     int iZero;                    /* Frame number associated with aPgno[0] */
 44751   } aSegment[1];                  /* One for every 32KB page in the wal-index */
 44752 };
 44754 /*
 44755 ** Define the parameters of the hash tables in the wal-index file. There
 44756 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
 44757 ** wal-index.
 44758 **
 44759 ** Changing any of these constants will alter the wal-index format and
 44760 ** create incompatibilities.
 44761 */
 44762 #define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
 44763 #define HASHTABLE_HASH_1     383                  /* Should be prime */
 44764 #define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
 44766 /* 
 44767 ** The block of page numbers associated with the first hash-table in a
 44768 ** wal-index is smaller than usual. This is so that there is a complete
 44769 ** hash-table on each aligned 32KB page of the wal-index.
 44770 */
 44771 #define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
 44773 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
 44774 #define WALINDEX_PGSZ   (                                         \
 44775     sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
 44778 /*
 44779 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
 44780 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
 44781 ** numbered from zero.
 44782 **
 44783 ** If this call is successful, *ppPage is set to point to the wal-index
 44784 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 44785 ** then an SQLite error code is returned and *ppPage is set to 0.
 44786 */
 44787 static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
 44788   int rc = SQLITE_OK;
 44790   /* Enlarge the pWal->apWiData[] array if required */
 44791   if( pWal->nWiData<=iPage ){
 44792     int nByte = sizeof(u32*)*(iPage+1);
 44793     volatile u32 **apNew;
 44794     apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
 44795     if( !apNew ){
 44796       *ppPage = 0;
 44797       return SQLITE_NOMEM;
 44799     memset((void*)&apNew[pWal->nWiData], 0,
 44800            sizeof(u32*)*(iPage+1-pWal->nWiData));
 44801     pWal->apWiData = apNew;
 44802     pWal->nWiData = iPage+1;
 44805   /* Request a pointer to the required page from the VFS */
 44806   if( pWal->apWiData[iPage]==0 ){
 44807     if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 44808       pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 44809       if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
 44810     }else{
 44811       rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 44812           pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 44813       );
 44814       if( rc==SQLITE_READONLY ){
 44815         pWal->readOnly |= WAL_SHM_RDONLY;
 44816         rc = SQLITE_OK;
 44821   *ppPage = pWal->apWiData[iPage];
 44822   assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 44823   return rc;
 44826 /*
 44827 ** Return a pointer to the WalCkptInfo structure in the wal-index.
 44828 */
 44829 static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
 44830   assert( pWal->nWiData>0 && pWal->apWiData[0] );
 44831   return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
 44834 /*
 44835 ** Return a pointer to the WalIndexHdr structure in the wal-index.
 44836 */
 44837 static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
 44838   assert( pWal->nWiData>0 && pWal->apWiData[0] );
 44839   return (volatile WalIndexHdr*)pWal->apWiData[0];
 44842 /*
 44843 ** The argument to this macro must be of type u32. On a little-endian
 44844 ** architecture, it returns the u32 value that results from interpreting
 44845 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
 44846 ** returns the value that would be produced by intepreting the 4 bytes
 44847 ** of the input value as a little-endian integer.
 44848 */
 44849 #define BYTESWAP32(x) ( \
 44850     (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
 44851   + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
 44854 /*
 44855 ** Generate or extend an 8 byte checksum based on the data in 
 44856 ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
 44857 ** initial values of 0 and 0 if aIn==NULL).
 44858 **
 44859 ** The checksum is written back into aOut[] before returning.
 44860 **
 44861 ** nByte must be a positive multiple of 8.
 44862 */
 44863 static void walChecksumBytes(
 44864   int nativeCksum, /* True for native byte-order, false for non-native */
 44865   u8 *a,           /* Content to be checksummed */
 44866   int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
 44867   const u32 *aIn,  /* Initial checksum value input */
 44868   u32 *aOut        /* OUT: Final checksum value output */
 44869 ){
 44870   u32 s1, s2;
 44871   u32 *aData = (u32 *)a;
 44872   u32 *aEnd = (u32 *)&a[nByte];
 44874   if( aIn ){
 44875     s1 = aIn[0];
 44876     s2 = aIn[1];
 44877   }else{
 44878     s1 = s2 = 0;
 44881   assert( nByte>=8 );
 44882   assert( (nByte&0x00000007)==0 );
 44884   if( nativeCksum ){
 44885     do {
 44886       s1 += *aData++ + s2;
 44887       s2 += *aData++ + s1;
 44888     }while( aData<aEnd );
 44889   }else{
 44890     do {
 44891       s1 += BYTESWAP32(aData[0]) + s2;
 44892       s2 += BYTESWAP32(aData[1]) + s1;
 44893       aData += 2;
 44894     }while( aData<aEnd );
 44897   aOut[0] = s1;
 44898   aOut[1] = s2;
 44901 static void walShmBarrier(Wal *pWal){
 44902   if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
 44903     sqlite3OsShmBarrier(pWal->pDbFd);
 44907 /*
 44908 ** Write the header information in pWal->hdr into the wal-index.
 44909 **
 44910 ** The checksum on pWal->hdr is updated before it is written.
 44911 */
 44912 static void walIndexWriteHdr(Wal *pWal){
 44913   volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
 44914   const int nCksum = offsetof(WalIndexHdr, aCksum);
 44916   assert( pWal->writeLock );
 44917   pWal->hdr.isInit = 1;
 44918   pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
 44919   walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
 44920   memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
 44921   walShmBarrier(pWal);
 44922   memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
 44925 /*
 44926 ** This function encodes a single frame header and writes it to a buffer
 44927 ** supplied by the caller. A frame-header is made up of a series of 
 44928 ** 4-byte big-endian integers, as follows:
 44929 **
 44930 **     0: Page number.
 44931 **     4: For commit records, the size of the database image in pages 
 44932 **        after the commit. For all other records, zero.
 44933 **     8: Salt-1 (copied from the wal-header)
 44934 **    12: Salt-2 (copied from the wal-header)
 44935 **    16: Checksum-1.
 44936 **    20: Checksum-2.
 44937 */
 44938 static void walEncodeFrame(
 44939   Wal *pWal,                      /* The write-ahead log */
 44940   u32 iPage,                      /* Database page number for frame */
 44941   u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
 44942   u8 *aData,                      /* Pointer to page data */
 44943   u8 *aFrame                      /* OUT: Write encoded frame here */
 44944 ){
 44945   int nativeCksum;                /* True for native byte-order checksums */
 44946   u32 *aCksum = pWal->hdr.aFrameCksum;
 44947   assert( WAL_FRAME_HDRSIZE==24 );
 44948   sqlite3Put4byte(&aFrame[0], iPage);
 44949   sqlite3Put4byte(&aFrame[4], nTruncate);
 44950   memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
 44952   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
 44953   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
 44954   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
 44956   sqlite3Put4byte(&aFrame[16], aCksum[0]);
 44957   sqlite3Put4byte(&aFrame[20], aCksum[1]);
 44960 /*
 44961 ** Check to see if the frame with header in aFrame[] and content
 44962 ** in aData[] is valid.  If it is a valid frame, fill *piPage and
 44963 ** *pnTruncate and return true.  Return if the frame is not valid.
 44964 */
 44965 static int walDecodeFrame(
 44966   Wal *pWal,                      /* The write-ahead log */
 44967   u32 *piPage,                    /* OUT: Database page number for frame */
 44968   u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
 44969   u8 *aData,                      /* Pointer to page data (for checksum) */
 44970   u8 *aFrame                      /* Frame data */
 44971 ){
 44972   int nativeCksum;                /* True for native byte-order checksums */
 44973   u32 *aCksum = pWal->hdr.aFrameCksum;
 44974   u32 pgno;                       /* Page number of the frame */
 44975   assert( WAL_FRAME_HDRSIZE==24 );
 44977   /* A frame is only valid if the salt values in the frame-header
 44978   ** match the salt values in the wal-header. 
 44979   */
 44980   if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
 44981     return 0;
 44984   /* A frame is only valid if the page number is creater than zero.
 44985   */
 44986   pgno = sqlite3Get4byte(&aFrame[0]);
 44987   if( pgno==0 ){
 44988     return 0;
 44991   /* A frame is only valid if a checksum of the WAL header,
 44992   ** all prior frams, the first 16 bytes of this frame-header, 
 44993   ** and the frame-data matches the checksum in the last 8 
 44994   ** bytes of this frame-header.
 44995   */
 44996   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
 44997   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
 44998   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
 44999   if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) 
 45000    || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) 
 45001   ){
 45002     /* Checksum failed. */
 45003     return 0;
 45006   /* If we reach this point, the frame is valid.  Return the page number
 45007   ** and the new database size.
 45008   */
 45009   *piPage = pgno;
 45010   *pnTruncate = sqlite3Get4byte(&aFrame[4]);
 45011   return 1;
 45015 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 45016 /*
 45017 ** Names of locks.  This routine is used to provide debugging output and is not
 45018 ** a part of an ordinary build.
 45019 */
 45020 static const char *walLockName(int lockIdx){
 45021   if( lockIdx==WAL_WRITE_LOCK ){
 45022     return "WRITE-LOCK";
 45023   }else if( lockIdx==WAL_CKPT_LOCK ){
 45024     return "CKPT-LOCK";
 45025   }else if( lockIdx==WAL_RECOVER_LOCK ){
 45026     return "RECOVER-LOCK";
 45027   }else{
 45028     static char zName[15];
 45029     sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
 45030                      lockIdx-WAL_READ_LOCK(0));
 45031     return zName;
 45034 #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
 45037 /*
 45038 ** Set or release locks on the WAL.  Locks are either shared or exclusive.
 45039 ** A lock cannot be moved directly between shared and exclusive - it must go
 45040 ** through the unlocked state first.
 45041 **
 45042 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
 45043 */
 45044 static int walLockShared(Wal *pWal, int lockIdx){
 45045   int rc;
 45046   if( pWal->exclusiveMode ) return SQLITE_OK;
 45047   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
 45048                         SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
 45049   WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
 45050             walLockName(lockIdx), rc ? "failed" : "ok"));
 45051   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
 45052   return rc;
 45054 static void walUnlockShared(Wal *pWal, int lockIdx){
 45055   if( pWal->exclusiveMode ) return;
 45056   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
 45057                          SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
 45058   WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
 45060 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
 45061   int rc;
 45062   if( pWal->exclusiveMode ) return SQLITE_OK;
 45063   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
 45064                         SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
 45065   WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
 45066             walLockName(lockIdx), n, rc ? "failed" : "ok"));
 45067   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
 45068   return rc;
 45070 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
 45071   if( pWal->exclusiveMode ) return;
 45072   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
 45073                          SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
 45074   WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
 45075              walLockName(lockIdx), n));
 45078 /*
 45079 ** Compute a hash on a page number.  The resulting hash value must land
 45080 ** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
 45081 ** the hash to the next value in the event of a collision.
 45082 */
 45083 static int walHash(u32 iPage){
 45084   assert( iPage>0 );
 45085   assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
 45086   return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
 45088 static int walNextHash(int iPriorHash){
 45089   return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
 45092 /* 
 45093 ** Return pointers to the hash table and page number array stored on
 45094 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
 45095 ** numbered starting from 0.
 45096 **
 45097 ** Set output variable *paHash to point to the start of the hash table
 45098 ** in the wal-index file. Set *piZero to one less than the frame 
 45099 ** number of the first frame indexed by this hash table. If a
 45100 ** slot in the hash table is set to N, it refers to frame number 
 45101 ** (*piZero+N) in the log.
 45102 **
 45103 ** Finally, set *paPgno so that *paPgno[1] is the page number of the
 45104 ** first frame indexed by the hash table, frame (*piZero+1).
 45105 */
 45106 static int walHashGet(
 45107   Wal *pWal,                      /* WAL handle */
 45108   int iHash,                      /* Find the iHash'th table */
 45109   volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
 45110   volatile u32 **paPgno,          /* OUT: Pointer to page number array */
 45111   u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
 45112 ){
 45113   int rc;                         /* Return code */
 45114   volatile u32 *aPgno;
 45116   rc = walIndexPage(pWal, iHash, &aPgno);
 45117   assert( rc==SQLITE_OK || iHash>0 );
 45119   if( rc==SQLITE_OK ){
 45120     u32 iZero;
 45121     volatile ht_slot *aHash;
 45123     aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
 45124     if( iHash==0 ){
 45125       aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
 45126       iZero = 0;
 45127     }else{
 45128       iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
 45131     *paPgno = &aPgno[-1];
 45132     *paHash = aHash;
 45133     *piZero = iZero;
 45135   return rc;
 45138 /*
 45139 ** Return the number of the wal-index page that contains the hash-table
 45140 ** and page-number array that contain entries corresponding to WAL frame
 45141 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages 
 45142 ** are numbered starting from 0.
 45143 */
 45144 static int walFramePage(u32 iFrame){
 45145   int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
 45146   assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
 45147        && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
 45148        && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
 45149        && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
 45150        && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
 45151   );
 45152   return iHash;
 45155 /*
 45156 ** Return the page number associated with frame iFrame in this WAL.
 45157 */
 45158 static u32 walFramePgno(Wal *pWal, u32 iFrame){
 45159   int iHash = walFramePage(iFrame);
 45160   if( iHash==0 ){
 45161     return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
 45163   return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
 45166 /*
 45167 ** Remove entries from the hash table that point to WAL slots greater
 45168 ** than pWal->hdr.mxFrame.
 45169 **
 45170 ** This function is called whenever pWal->hdr.mxFrame is decreased due
 45171 ** to a rollback or savepoint.
 45172 **
 45173 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
 45174 ** updated.  Any later hash tables will be automatically cleared when
 45175 ** pWal->hdr.mxFrame advances to the point where those hash tables are
 45176 ** actually needed.
 45177 */
 45178 static void walCleanupHash(Wal *pWal){
 45179   volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
 45180   volatile u32 *aPgno = 0;        /* Page number array for hash table */
 45181   u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
 45182   int iLimit = 0;                 /* Zero values greater than this */
 45183   int nByte;                      /* Number of bytes to zero in aPgno[] */
 45184   int i;                          /* Used to iterate through aHash[] */
 45186   assert( pWal->writeLock );
 45187   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
 45188   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
 45189   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
 45191   if( pWal->hdr.mxFrame==0 ) return;
 45193   /* Obtain pointers to the hash-table and page-number array containing 
 45194   ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
 45195   ** that the page said hash-table and array reside on is already mapped.
 45196   */
 45197   assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
 45198   assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
 45199   walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
 45201   /* Zero all hash-table entries that correspond to frame numbers greater
 45202   ** than pWal->hdr.mxFrame.
 45203   */
 45204   iLimit = pWal->hdr.mxFrame - iZero;
 45205   assert( iLimit>0 );
 45206   for(i=0; i<HASHTABLE_NSLOT; i++){
 45207     if( aHash[i]>iLimit ){
 45208       aHash[i] = 0;
 45212   /* Zero the entries in the aPgno array that correspond to frames with
 45213   ** frame numbers greater than pWal->hdr.mxFrame. 
 45214   */
 45215   nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
 45216   memset((void *)&aPgno[iLimit+1], 0, nByte);
 45218 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 45219   /* Verify that the every entry in the mapping region is still reachable
 45220   ** via the hash table even after the cleanup.
 45221   */
 45222   if( iLimit ){
 45223     int i;           /* Loop counter */
 45224     int iKey;        /* Hash key */
 45225     for(i=1; i<=iLimit; i++){
 45226       for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
 45227         if( aHash[iKey]==i ) break;
 45229       assert( aHash[iKey]==i );
 45232 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
 45236 /*
 45237 ** Set an entry in the wal-index that will map database page number
 45238 ** pPage into WAL frame iFrame.
 45239 */
 45240 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
 45241   int rc;                         /* Return code */
 45242   u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
 45243   volatile u32 *aPgno = 0;        /* Page number array */
 45244   volatile ht_slot *aHash = 0;    /* Hash table */
 45246   rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
 45248   /* Assuming the wal-index file was successfully mapped, populate the
 45249   ** page number array and hash table entry.
 45250   */
 45251   if( rc==SQLITE_OK ){
 45252     int iKey;                     /* Hash table key */
 45253     int idx;                      /* Value to write to hash-table slot */
 45254     int nCollide;                 /* Number of hash collisions */
 45256     idx = iFrame - iZero;
 45257     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
 45259     /* If this is the first entry to be added to this hash-table, zero the
 45260     ** entire hash table and aPgno[] array before proceding. 
 45261     */
 45262     if( idx==1 ){
 45263       int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
 45264       memset((void*)&aPgno[1], 0, nByte);
 45267     /* If the entry in aPgno[] is already set, then the previous writer
 45268     ** must have exited unexpectedly in the middle of a transaction (after
 45269     ** writing one or more dirty pages to the WAL to free up memory). 
 45270     ** Remove the remnants of that writers uncommitted transaction from 
 45271     ** the hash-table before writing any new entries.
 45272     */
 45273     if( aPgno[idx] ){
 45274       walCleanupHash(pWal);
 45275       assert( !aPgno[idx] );
 45278     /* Write the aPgno[] array entry and the hash-table slot. */
 45279     nCollide = idx;
 45280     for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
 45281       if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
 45283     aPgno[idx] = iPage;
 45284     aHash[iKey] = (ht_slot)idx;
 45286 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 45287     /* Verify that the number of entries in the hash table exactly equals
 45288     ** the number of entries in the mapping region.
 45289     */
 45291       int i;           /* Loop counter */
 45292       int nEntry = 0;  /* Number of entries in the hash table */
 45293       for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
 45294       assert( nEntry==idx );
 45297     /* Verify that the every entry in the mapping region is reachable
 45298     ** via the hash table.  This turns out to be a really, really expensive
 45299     ** thing to check, so only do this occasionally - not on every
 45300     ** iteration.
 45301     */
 45302     if( (idx&0x3ff)==0 ){
 45303       int i;           /* Loop counter */
 45304       for(i=1; i<=idx; i++){
 45305         for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
 45306           if( aHash[iKey]==i ) break;
 45308         assert( aHash[iKey]==i );
 45311 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
 45315   return rc;
 45319 /*
 45320 ** Recover the wal-index by reading the write-ahead log file. 
 45321 **
 45322 ** This routine first tries to establish an exclusive lock on the
 45323 ** wal-index to prevent other threads/processes from doing anything
 45324 ** with the WAL or wal-index while recovery is running.  The
 45325 ** WAL_RECOVER_LOCK is also held so that other threads will know
 45326 ** that this thread is running recovery.  If unable to establish
 45327 ** the necessary locks, this routine returns SQLITE_BUSY.
 45328 */
 45329 static int walIndexRecover(Wal *pWal){
 45330   int rc;                         /* Return Code */
 45331   i64 nSize;                      /* Size of log file */
 45332   u32 aFrameCksum[2] = {0, 0};
 45333   int iLock;                      /* Lock offset to lock for checkpoint */
 45334   int nLock;                      /* Number of locks to hold */
 45336   /* Obtain an exclusive lock on all byte in the locking range not already
 45337   ** locked by the caller. The caller is guaranteed to have locked the
 45338   ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
 45339   ** If successful, the same bytes that are locked here are unlocked before
 45340   ** this function returns.
 45341   */
 45342   assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
 45343   assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
 45344   assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
 45345   assert( pWal->writeLock );
 45346   iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
 45347   nLock = SQLITE_SHM_NLOCK - iLock;
 45348   rc = walLockExclusive(pWal, iLock, nLock);
 45349   if( rc ){
 45350     return rc;
 45352   WALTRACE(("WAL%p: recovery begin...\n", pWal));
 45354   memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 45356   rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
 45357   if( rc!=SQLITE_OK ){
 45358     goto recovery_error;
 45361   if( nSize>WAL_HDRSIZE ){
 45362     u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
 45363     u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
 45364     int szFrame;                  /* Number of bytes in buffer aFrame[] */
 45365     u8 *aData;                    /* Pointer to data part of aFrame buffer */
 45366     int iFrame;                   /* Index of last frame read */
 45367     i64 iOffset;                  /* Next offset to read from log file */
 45368     int szPage;                   /* Page size according to the log */
 45369     u32 magic;                    /* Magic value read from WAL header */
 45370     u32 version;                  /* Magic value read from WAL header */
 45371     int isValid;                  /* True if this frame is valid */
 45373     /* Read in the WAL header. */
 45374     rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
 45375     if( rc!=SQLITE_OK ){
 45376       goto recovery_error;
 45379     /* If the database page size is not a power of two, or is greater than
 45380     ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid 
 45381     ** data. Similarly, if the 'magic' value is invalid, ignore the whole
 45382     ** WAL file.
 45383     */
 45384     magic = sqlite3Get4byte(&aBuf[0]);
 45385     szPage = sqlite3Get4byte(&aBuf[8]);
 45386     if( (magic&0xFFFFFFFE)!=WAL_MAGIC 
 45387      || szPage&(szPage-1) 
 45388      || szPage>SQLITE_MAX_PAGE_SIZE 
 45389      || szPage<512 
 45390     ){
 45391       goto finished;
 45393     pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
 45394     pWal->szPage = szPage;
 45395     pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
 45396     memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
 45398     /* Verify that the WAL header checksum is correct */
 45399     walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, 
 45400         aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
 45401     );
 45402     if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
 45403      || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
 45404     ){
 45405       goto finished;
 45408     /* Verify that the version number on the WAL format is one that
 45409     ** are able to understand */
 45410     version = sqlite3Get4byte(&aBuf[4]);
 45411     if( version!=WAL_MAX_VERSION ){
 45412       rc = SQLITE_CANTOPEN_BKPT;
 45413       goto finished;
 45416     /* Malloc a buffer to read frames into. */
 45417     szFrame = szPage + WAL_FRAME_HDRSIZE;
 45418     aFrame = (u8 *)sqlite3_malloc(szFrame);
 45419     if( !aFrame ){
 45420       rc = SQLITE_NOMEM;
 45421       goto recovery_error;
 45423     aData = &aFrame[WAL_FRAME_HDRSIZE];
 45425     /* Read all frames from the log file. */
 45426     iFrame = 0;
 45427     for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
 45428       u32 pgno;                   /* Database page number for frame */
 45429       u32 nTruncate;              /* dbsize field from frame header */
 45431       /* Read and decode the next log frame. */
 45432       iFrame++;
 45433       rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
 45434       if( rc!=SQLITE_OK ) break;
 45435       isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
 45436       if( !isValid ) break;
 45437       rc = walIndexAppend(pWal, iFrame, pgno);
 45438       if( rc!=SQLITE_OK ) break;
 45440       /* If nTruncate is non-zero, this is a commit record. */
 45441       if( nTruncate ){
 45442         pWal->hdr.mxFrame = iFrame;
 45443         pWal->hdr.nPage = nTruncate;
 45444         pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
 45445         testcase( szPage<=32768 );
 45446         testcase( szPage>=65536 );
 45447         aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
 45448         aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
 45452     sqlite3_free(aFrame);
 45455 finished:
 45456   if( rc==SQLITE_OK ){
 45457     volatile WalCkptInfo *pInfo;
 45458     int i;
 45459     pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
 45460     pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
 45461     walIndexWriteHdr(pWal);
 45463     /* Reset the checkpoint-header. This is safe because this thread is 
 45464     ** currently holding locks that exclude all other readers, writers and
 45465     ** checkpointers.
 45466     */
 45467     pInfo = walCkptInfo(pWal);
 45468     pInfo->nBackfill = 0;
 45469     pInfo->aReadMark[0] = 0;
 45470     for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 45471     if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
 45473     /* If more than one frame was recovered from the log file, report an
 45474     ** event via sqlite3_log(). This is to help with identifying performance
 45475     ** problems caused by applications routinely shutting down without
 45476     ** checkpointing the log file.
 45477     */
 45478     if( pWal->hdr.nPage ){
 45479       sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
 45480           pWal->hdr.nPage, pWal->zWalName
 45481       );
 45485 recovery_error:
 45486   WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
 45487   walUnlockExclusive(pWal, iLock, nLock);
 45488   return rc;
 45491 /*
 45492 ** Close an open wal-index.
 45493 */
 45494 static void walIndexClose(Wal *pWal, int isDelete){
 45495   if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 45496     int i;
 45497     for(i=0; i<pWal->nWiData; i++){
 45498       sqlite3_free((void *)pWal->apWiData[i]);
 45499       pWal->apWiData[i] = 0;
 45501   }else{
 45502     sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
 45506 /* 
 45507 ** Open a connection to the WAL file zWalName. The database file must 
 45508 ** already be opened on connection pDbFd. The buffer that zWalName points
 45509 ** to must remain valid for the lifetime of the returned Wal* handle.
 45510 **
 45511 ** A SHARED lock should be held on the database file when this function
 45512 ** is called. The purpose of this SHARED lock is to prevent any other
 45513 ** client from unlinking the WAL or wal-index file. If another process
 45514 ** were to do this just after this client opened one of these files, the
 45515 ** system would be badly broken.
 45516 **
 45517 ** If the log file is successfully opened, SQLITE_OK is returned and 
 45518 ** *ppWal is set to point to a new WAL handle. If an error occurs,
 45519 ** an SQLite error code is returned and *ppWal is left unmodified.
 45520 */
 45521 SQLITE_PRIVATE int sqlite3WalOpen(
 45522   sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
 45523   sqlite3_file *pDbFd,            /* The open database file */
 45524   const char *zWalName,           /* Name of the WAL file */
 45525   int bNoShm,                     /* True to run in heap-memory mode */
 45526   i64 mxWalSize,                  /* Truncate WAL to this size on reset */
 45527   Wal **ppWal                     /* OUT: Allocated Wal handle */
 45528 ){
 45529   int rc;                         /* Return Code */
 45530   Wal *pRet;                      /* Object to allocate and return */
 45531   int flags;                      /* Flags passed to OsOpen() */
 45533   assert( zWalName && zWalName[0] );
 45534   assert( pDbFd );
 45536   /* In the amalgamation, the os_unix.c and os_win.c source files come before
 45537   ** this source file.  Verify that the #defines of the locking byte offsets
 45538   ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
 45539   */
 45540 #ifdef WIN_SHM_BASE
 45541   assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
 45542 #endif
 45543 #ifdef UNIX_SHM_BASE
 45544   assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
 45545 #endif
 45548   /* Allocate an instance of struct Wal to return. */
 45549   *ppWal = 0;
 45550   pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
 45551   if( !pRet ){
 45552     return SQLITE_NOMEM;
 45555   pRet->pVfs = pVfs;
 45556   pRet->pWalFd = (sqlite3_file *)&pRet[1];
 45557   pRet->pDbFd = pDbFd;
 45558   pRet->readLock = -1;
 45559   pRet->mxWalSize = mxWalSize;
 45560   pRet->zWalName = zWalName;
 45561   pRet->syncHeader = 1;
 45562   pRet->padToSectorBoundary = 1;
 45563   pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
 45565   /* Open file handle on the write-ahead log file. */
 45566   flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
 45567   rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
 45568   if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
 45569     pRet->readOnly = WAL_RDONLY;
 45572   if( rc!=SQLITE_OK ){
 45573     walIndexClose(pRet, 0);
 45574     sqlite3OsClose(pRet->pWalFd);
 45575     sqlite3_free(pRet);
 45576   }else{
 45577     int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
 45578     if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
 45579     if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
 45580       pRet->padToSectorBoundary = 0;
 45582     *ppWal = pRet;
 45583     WALTRACE(("WAL%d: opened\n", pRet));
 45585   return rc;
 45588 /*
 45589 ** Change the size to which the WAL file is trucated on each reset.
 45590 */
 45591 SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
 45592   if( pWal ) pWal->mxWalSize = iLimit;
 45595 /*
 45596 ** Find the smallest page number out of all pages held in the WAL that
 45597 ** has not been returned by any prior invocation of this method on the
 45598 ** same WalIterator object.   Write into *piFrame the frame index where
 45599 ** that page was last written into the WAL.  Write into *piPage the page
 45600 ** number.
 45601 **
 45602 ** Return 0 on success.  If there are no pages in the WAL with a page
 45603 ** number larger than *piPage, then return 1.
 45604 */
 45605 static int walIteratorNext(
 45606   WalIterator *p,               /* Iterator */
 45607   u32 *piPage,                  /* OUT: The page number of the next page */
 45608   u32 *piFrame                  /* OUT: Wal frame index of next page */
 45609 ){
 45610   u32 iMin;                     /* Result pgno must be greater than iMin */
 45611   u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
 45612   int i;                        /* For looping through segments */
 45614   iMin = p->iPrior;
 45615   assert( iMin<0xffffffff );
 45616   for(i=p->nSegment-1; i>=0; i--){
 45617     struct WalSegment *pSegment = &p->aSegment[i];
 45618     while( pSegment->iNext<pSegment->nEntry ){
 45619       u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
 45620       if( iPg>iMin ){
 45621         if( iPg<iRet ){
 45622           iRet = iPg;
 45623           *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
 45625         break;
 45627       pSegment->iNext++;
 45631   *piPage = p->iPrior = iRet;
 45632   return (iRet==0xFFFFFFFF);
 45635 /*
 45636 ** This function merges two sorted lists into a single sorted list.
 45637 **
 45638 ** aLeft[] and aRight[] are arrays of indices.  The sort key is
 45639 ** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
 45640 ** is guaranteed for all J<K:
 45641 **
 45642 **        aContent[aLeft[J]] < aContent[aLeft[K]]
 45643 **        aContent[aRight[J]] < aContent[aRight[K]]
 45644 **
 45645 ** This routine overwrites aRight[] with a new (probably longer) sequence
 45646 ** of indices such that the aRight[] contains every index that appears in
 45647 ** either aLeft[] or the old aRight[] and such that the second condition
 45648 ** above is still met.
 45649 **
 45650 ** The aContent[aLeft[X]] values will be unique for all X.  And the
 45651 ** aContent[aRight[X]] values will be unique too.  But there might be
 45652 ** one or more combinations of X and Y such that
 45653 **
 45654 **      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
 45655 **
 45656 ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
 45657 */
 45658 static void walMerge(
 45659   const u32 *aContent,            /* Pages in wal - keys for the sort */
 45660   ht_slot *aLeft,                 /* IN: Left hand input list */
 45661   int nLeft,                      /* IN: Elements in array *paLeft */
 45662   ht_slot **paRight,              /* IN/OUT: Right hand input list */
 45663   int *pnRight,                   /* IN/OUT: Elements in *paRight */
 45664   ht_slot *aTmp                   /* Temporary buffer */
 45665 ){
 45666   int iLeft = 0;                  /* Current index in aLeft */
 45667   int iRight = 0;                 /* Current index in aRight */
 45668   int iOut = 0;                   /* Current index in output buffer */
 45669   int nRight = *pnRight;
 45670   ht_slot *aRight = *paRight;
 45672   assert( nLeft>0 && nRight>0 );
 45673   while( iRight<nRight || iLeft<nLeft ){
 45674     ht_slot logpage;
 45675     Pgno dbpage;
 45677     if( (iLeft<nLeft) 
 45678      && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
 45679     ){
 45680       logpage = aLeft[iLeft++];
 45681     }else{
 45682       logpage = aRight[iRight++];
 45684     dbpage = aContent[logpage];
 45686     aTmp[iOut++] = logpage;
 45687     if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
 45689     assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
 45690     assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
 45693   *paRight = aLeft;
 45694   *pnRight = iOut;
 45695   memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
 45698 /*
 45699 ** Sort the elements in list aList using aContent[] as the sort key.
 45700 ** Remove elements with duplicate keys, preferring to keep the
 45701 ** larger aList[] values.
 45702 **
 45703 ** The aList[] entries are indices into aContent[].  The values in
 45704 ** aList[] are to be sorted so that for all J<K:
 45705 **
 45706 **      aContent[aList[J]] < aContent[aList[K]]
 45707 **
 45708 ** For any X and Y such that
 45709 **
 45710 **      aContent[aList[X]] == aContent[aList[Y]]
 45711 **
 45712 ** Keep the larger of the two values aList[X] and aList[Y] and discard
 45713 ** the smaller.
 45714 */
 45715 static void walMergesort(
 45716   const u32 *aContent,            /* Pages in wal */
 45717   ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
 45718   ht_slot *aList,                 /* IN/OUT: List to sort */
 45719   int *pnList                     /* IN/OUT: Number of elements in aList[] */
 45720 ){
 45721   struct Sublist {
 45722     int nList;                    /* Number of elements in aList */
 45723     ht_slot *aList;               /* Pointer to sub-list content */
 45724   };
 45726   const int nList = *pnList;      /* Size of input list */
 45727   int nMerge = 0;                 /* Number of elements in list aMerge */
 45728   ht_slot *aMerge = 0;            /* List to be merged */
 45729   int iList;                      /* Index into input list */
 45730   int iSub = 0;                   /* Index into aSub array */
 45731   struct Sublist aSub[13];        /* Array of sub-lists */
 45733   memset(aSub, 0, sizeof(aSub));
 45734   assert( nList<=HASHTABLE_NPAGE && nList>0 );
 45735   assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
 45737   for(iList=0; iList<nList; iList++){
 45738     nMerge = 1;
 45739     aMerge = &aList[iList];
 45740     for(iSub=0; iList & (1<<iSub); iSub++){
 45741       struct Sublist *p = &aSub[iSub];
 45742       assert( p->aList && p->nList<=(1<<iSub) );
 45743       assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
 45744       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
 45746     aSub[iSub].aList = aMerge;
 45747     aSub[iSub].nList = nMerge;
 45750   for(iSub++; iSub<ArraySize(aSub); iSub++){
 45751     if( nList & (1<<iSub) ){
 45752       struct Sublist *p = &aSub[iSub];
 45753       assert( p->nList<=(1<<iSub) );
 45754       assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
 45755       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
 45758   assert( aMerge==aList );
 45759   *pnList = nMerge;
 45761 #ifdef SQLITE_DEBUG
 45763     int i;
 45764     for(i=1; i<*pnList; i++){
 45765       assert( aContent[aList[i]] > aContent[aList[i-1]] );
 45768 #endif
 45771 /* 
 45772 ** Free an iterator allocated by walIteratorInit().
 45773 */
 45774 static void walIteratorFree(WalIterator *p){
 45775   sqlite3ScratchFree(p);
 45778 /*
 45779 ** Construct a WalInterator object that can be used to loop over all 
 45780 ** pages in the WAL in ascending order. The caller must hold the checkpoint
 45781 ** lock.
 45782 **
 45783 ** On success, make *pp point to the newly allocated WalInterator object
 45784 ** return SQLITE_OK. Otherwise, return an error code. If this routine
 45785 ** returns an error, the value of *pp is undefined.
 45786 **
 45787 ** The calling routine should invoke walIteratorFree() to destroy the
 45788 ** WalIterator object when it has finished with it.
 45789 */
 45790 static int walIteratorInit(Wal *pWal, WalIterator **pp){
 45791   WalIterator *p;                 /* Return value */
 45792   int nSegment;                   /* Number of segments to merge */
 45793   u32 iLast;                      /* Last frame in log */
 45794   int nByte;                      /* Number of bytes to allocate */
 45795   int i;                          /* Iterator variable */
 45796   ht_slot *aTmp;                  /* Temp space used by merge-sort */
 45797   int rc = SQLITE_OK;             /* Return Code */
 45799   /* This routine only runs while holding the checkpoint lock. And
 45800   ** it only runs if there is actually content in the log (mxFrame>0).
 45801   */
 45802   assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
 45803   iLast = pWal->hdr.mxFrame;
 45805   /* Allocate space for the WalIterator object. */
 45806   nSegment = walFramePage(iLast) + 1;
 45807   nByte = sizeof(WalIterator) 
 45808         + (nSegment-1)*sizeof(struct WalSegment)
 45809         + iLast*sizeof(ht_slot);
 45810   p = (WalIterator *)sqlite3ScratchMalloc(nByte);
 45811   if( !p ){
 45812     return SQLITE_NOMEM;
 45814   memset(p, 0, nByte);
 45815   p->nSegment = nSegment;
 45817   /* Allocate temporary space used by the merge-sort routine. This block
 45818   ** of memory will be freed before this function returns.
 45819   */
 45820   aTmp = (ht_slot *)sqlite3ScratchMalloc(
 45821       sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
 45822   );
 45823   if( !aTmp ){
 45824     rc = SQLITE_NOMEM;
 45827   for(i=0; rc==SQLITE_OK && i<nSegment; i++){
 45828     volatile ht_slot *aHash;
 45829     u32 iZero;
 45830     volatile u32 *aPgno;
 45832     rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
 45833     if( rc==SQLITE_OK ){
 45834       int j;                      /* Counter variable */
 45835       int nEntry;                 /* Number of entries in this segment */
 45836       ht_slot *aIndex;            /* Sorted index for this segment */
 45838       aPgno++;
 45839       if( (i+1)==nSegment ){
 45840         nEntry = (int)(iLast - iZero);
 45841       }else{
 45842         nEntry = (int)((u32*)aHash - (u32*)aPgno);
 45844       aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
 45845       iZero++;
 45847       for(j=0; j<nEntry; j++){
 45848         aIndex[j] = (ht_slot)j;
 45850       walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
 45851       p->aSegment[i].iZero = iZero;
 45852       p->aSegment[i].nEntry = nEntry;
 45853       p->aSegment[i].aIndex = aIndex;
 45854       p->aSegment[i].aPgno = (u32 *)aPgno;
 45857   sqlite3ScratchFree(aTmp);
 45859   if( rc!=SQLITE_OK ){
 45860     walIteratorFree(p);
 45862   *pp = p;
 45863   return rc;
 45866 /*
 45867 ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
 45868 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
 45869 ** busy-handler function. Invoke it and retry the lock until either the
 45870 ** lock is successfully obtained or the busy-handler returns 0.
 45871 */
 45872 static int walBusyLock(
 45873   Wal *pWal,                      /* WAL connection */
 45874   int (*xBusy)(void*),            /* Function to call when busy */
 45875   void *pBusyArg,                 /* Context argument for xBusyHandler */
 45876   int lockIdx,                    /* Offset of first byte to lock */
 45877   int n                           /* Number of bytes to lock */
 45878 ){
 45879   int rc;
 45880   do {
 45881     rc = walLockExclusive(pWal, lockIdx, n);
 45882   }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
 45883   return rc;
 45886 /*
 45887 ** The cache of the wal-index header must be valid to call this function.
 45888 ** Return the page-size in bytes used by the database.
 45889 */
 45890 static int walPagesize(Wal *pWal){
 45891   return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
 45894 /*
 45895 ** Copy as much content as we can from the WAL back into the database file
 45896 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
 45897 **
 45898 ** The amount of information copies from WAL to database might be limited
 45899 ** by active readers.  This routine will never overwrite a database page
 45900 ** that a concurrent reader might be using.
 45901 **
 45902 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
 45903 ** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if 
 45904 ** checkpoints are always run by a background thread or background 
 45905 ** process, foreground threads will never block on a lengthy fsync call.
 45906 **
 45907 ** Fsync is called on the WAL before writing content out of the WAL and
 45908 ** into the database.  This ensures that if the new content is persistent
 45909 ** in the WAL and can be recovered following a power-loss or hard reset.
 45910 **
 45911 ** Fsync is also called on the database file if (and only if) the entire
 45912 ** WAL content is copied into the database file.  This second fsync makes
 45913 ** it safe to delete the WAL since the new content will persist in the
 45914 ** database file.
 45915 **
 45916 ** This routine uses and updates the nBackfill field of the wal-index header.
 45917 ** This is the only routine tha will increase the value of nBackfill.  
 45918 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
 45919 ** its value.)
 45920 **
 45921 ** The caller must be holding sufficient locks to ensure that no other
 45922 ** checkpoint is running (in any other thread or process) at the same
 45923 ** time.
 45924 */
 45925 static int walCheckpoint(
 45926   Wal *pWal,                      /* Wal connection */
 45927   int eMode,                      /* One of PASSIVE, FULL or RESTART */
 45928   int (*xBusyCall)(void*),        /* Function to call when busy */
 45929   void *pBusyArg,                 /* Context argument for xBusyHandler */
 45930   int sync_flags,                 /* Flags for OsSync() (or 0) */
 45931   u8 *zBuf                        /* Temporary buffer to use */
 45932 ){
 45933   int rc;                         /* Return code */
 45934   int szPage;                     /* Database page-size */
 45935   WalIterator *pIter = 0;         /* Wal iterator context */
 45936   u32 iDbpage = 0;                /* Next database page to write */
 45937   u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
 45938   u32 mxSafeFrame;                /* Max frame that can be backfilled */
 45939   u32 mxPage;                     /* Max database page to write */
 45940   int i;                          /* Loop counter */
 45941   volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
 45942   int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
 45944   szPage = walPagesize(pWal);
 45945   testcase( szPage<=32768 );
 45946   testcase( szPage>=65536 );
 45947   pInfo = walCkptInfo(pWal);
 45948   if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
 45950   /* Allocate the iterator */
 45951   rc = walIteratorInit(pWal, &pIter);
 45952   if( rc!=SQLITE_OK ){
 45953     return rc;
 45955   assert( pIter );
 45957   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
 45959   /* Compute in mxSafeFrame the index of the last frame of the WAL that is
 45960   ** safe to write into the database.  Frames beyond mxSafeFrame might
 45961   ** overwrite database pages that are in use by active readers and thus
 45962   ** cannot be backfilled from the WAL.
 45963   */
 45964   mxSafeFrame = pWal->hdr.mxFrame;
 45965   mxPage = pWal->hdr.nPage;
 45966   for(i=1; i<WAL_NREADER; i++){
 45967     u32 y = pInfo->aReadMark[i];
 45968     if( mxSafeFrame>y ){
 45969       assert( y<=pWal->hdr.mxFrame );
 45970       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
 45971       if( rc==SQLITE_OK ){
 45972         pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
 45973         walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 45974       }else if( rc==SQLITE_BUSY ){
 45975         mxSafeFrame = y;
 45976         xBusy = 0;
 45977       }else{
 45978         goto walcheckpoint_out;
 45983   if( pInfo->nBackfill<mxSafeFrame
 45984    && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
 45985   ){
 45986     i64 nSize;                    /* Current size of database file */
 45987     u32 nBackfill = pInfo->nBackfill;
 45989     /* Sync the WAL to disk */
 45990     if( sync_flags ){
 45991       rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
 45994     /* If the database file may grow as a result of this checkpoint, hint
 45995     ** about the eventual size of the db file to the VFS layer. 
 45996     */
 45997     if( rc==SQLITE_OK ){
 45998       i64 nReq = ((i64)mxPage * szPage);
 45999       rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
 46000       if( rc==SQLITE_OK && nSize<nReq ){
 46001         sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
 46005     /* Iterate through the contents of the WAL, copying data to the db file. */
 46006     while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
 46007       i64 iOffset;
 46008       assert( walFramePgno(pWal, iFrame)==iDbpage );
 46009       if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
 46010       iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
 46011       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
 46012       rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
 46013       if( rc!=SQLITE_OK ) break;
 46014       iOffset = (iDbpage-1)*(i64)szPage;
 46015       testcase( IS_BIG_INT(iOffset) );
 46016       rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
 46017       if( rc!=SQLITE_OK ) break;
 46020     /* If work was actually accomplished... */
 46021     if( rc==SQLITE_OK ){
 46022       if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
 46023         i64 szDb = pWal->hdr.nPage*(i64)szPage;
 46024         testcase( IS_BIG_INT(szDb) );
 46025         rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
 46026         if( rc==SQLITE_OK && sync_flags ){
 46027           rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
 46030       if( rc==SQLITE_OK ){
 46031         pInfo->nBackfill = mxSafeFrame;
 46035     /* Release the reader lock held while backfilling */
 46036     walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
 46039   if( rc==SQLITE_BUSY ){
 46040     /* Reset the return code so as not to report a checkpoint failure
 46041     ** just because there are active readers.  */
 46042     rc = SQLITE_OK;
 46045   /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
 46046   ** file has been copied into the database file, then block until all
 46047   ** readers have finished using the wal file. This ensures that the next
 46048   ** process to write to the database restarts the wal file.
 46049   */
 46050   if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
 46051     assert( pWal->writeLock );
 46052     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
 46053       rc = SQLITE_BUSY;
 46054     }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
 46055       assert( mxSafeFrame==pWal->hdr.mxFrame );
 46056       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
 46057       if( rc==SQLITE_OK ){
 46058         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 46063  walcheckpoint_out:
 46064   walIteratorFree(pIter);
 46065   return rc;
 46068 /*
 46069 ** If the WAL file is currently larger than nMax bytes in size, truncate
 46070 ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
 46071 */
 46072 static void walLimitSize(Wal *pWal, i64 nMax){
 46073   i64 sz;
 46074   int rx;
 46075   sqlite3BeginBenignMalloc();
 46076   rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
 46077   if( rx==SQLITE_OK && (sz > nMax ) ){
 46078     rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
 46080   sqlite3EndBenignMalloc();
 46081   if( rx ){
 46082     sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
 46086 /*
 46087 ** Close a connection to a log file.
 46088 */
 46089 SQLITE_PRIVATE int sqlite3WalClose(
 46090   Wal *pWal,                      /* Wal to close */
 46091   int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
 46092   int nBuf,
 46093   u8 *zBuf                        /* Buffer of at least nBuf bytes */
 46094 ){
 46095   int rc = SQLITE_OK;
 46096   if( pWal ){
 46097     int isDelete = 0;             /* True to unlink wal and wal-index files */
 46099     /* If an EXCLUSIVE lock can be obtained on the database file (using the
 46100     ** ordinary, rollback-mode locking methods, this guarantees that the
 46101     ** connection associated with this log file is the only connection to
 46102     ** the database. In this case checkpoint the database and unlink both
 46103     ** the wal and wal-index files.
 46104     **
 46105     ** The EXCLUSIVE lock is not released before returning.
 46106     */
 46107     rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
 46108     if( rc==SQLITE_OK ){
 46109       if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
 46110         pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
 46112       rc = sqlite3WalCheckpoint(
 46113           pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
 46114       );
 46115       if( rc==SQLITE_OK ){
 46116         int bPersist = -1;
 46117         sqlite3OsFileControlHint(
 46118             pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
 46119         );
 46120         if( bPersist!=1 ){
 46121           /* Try to delete the WAL file if the checkpoint completed and
 46122           ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
 46123           ** mode (!bPersist) */
 46124           isDelete = 1;
 46125         }else if( pWal->mxWalSize>=0 ){
 46126           /* Try to truncate the WAL file to zero bytes if the checkpoint
 46127           ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
 46128           ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
 46129           ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
 46130           ** to zero bytes as truncating to the journal_size_limit might
 46131           ** leave a corrupt WAL file on disk. */
 46132           walLimitSize(pWal, 0);
 46137     walIndexClose(pWal, isDelete);
 46138     sqlite3OsClose(pWal->pWalFd);
 46139     if( isDelete ){
 46140       sqlite3BeginBenignMalloc();
 46141       sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
 46142       sqlite3EndBenignMalloc();
 46144     WALTRACE(("WAL%p: closed\n", pWal));
 46145     sqlite3_free((void *)pWal->apWiData);
 46146     sqlite3_free(pWal);
 46148   return rc;
 46151 /*
 46152 ** Try to read the wal-index header.  Return 0 on success and 1 if
 46153 ** there is a problem.
 46154 **
 46155 ** The wal-index is in shared memory.  Another thread or process might
 46156 ** be writing the header at the same time this procedure is trying to
 46157 ** read it, which might result in inconsistency.  A dirty read is detected
 46158 ** by verifying that both copies of the header are the same and also by
 46159 ** a checksum on the header.
 46160 **
 46161 ** If and only if the read is consistent and the header is different from
 46162 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
 46163 ** and *pChanged is set to 1.
 46164 **
 46165 ** If the checksum cannot be verified return non-zero. If the header
 46166 ** is read successfully and the checksum verified, return zero.
 46167 */
 46168 static int walIndexTryHdr(Wal *pWal, int *pChanged){
 46169   u32 aCksum[2];                  /* Checksum on the header content */
 46170   WalIndexHdr h1, h2;             /* Two copies of the header content */
 46171   WalIndexHdr volatile *aHdr;     /* Header in shared memory */
 46173   /* The first page of the wal-index must be mapped at this point. */
 46174   assert( pWal->nWiData>0 && pWal->apWiData[0] );
 46176   /* Read the header. This might happen concurrently with a write to the
 46177   ** same area of shared memory on a different CPU in a SMP,
 46178   ** meaning it is possible that an inconsistent snapshot is read
 46179   ** from the file. If this happens, return non-zero.
 46180   **
 46181   ** There are two copies of the header at the beginning of the wal-index.
 46182   ** When reading, read [0] first then [1].  Writes are in the reverse order.
 46183   ** Memory barriers are used to prevent the compiler or the hardware from
 46184   ** reordering the reads and writes.
 46185   */
 46186   aHdr = walIndexHdr(pWal);
 46187   memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
 46188   walShmBarrier(pWal);
 46189   memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
 46191   if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
 46192     return 1;   /* Dirty read */
 46194   if( h1.isInit==0 ){
 46195     return 1;   /* Malformed header - probably all zeros */
 46197   walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
 46198   if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
 46199     return 1;   /* Checksum does not match */
 46202   if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
 46203     *pChanged = 1;
 46204     memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
 46205     pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
 46206     testcase( pWal->szPage<=32768 );
 46207     testcase( pWal->szPage>=65536 );
 46210   /* The header was successfully read. Return zero. */
 46211   return 0;
 46214 /*
 46215 ** Read the wal-index header from the wal-index and into pWal->hdr.
 46216 ** If the wal-header appears to be corrupt, try to reconstruct the
 46217 ** wal-index from the WAL before returning.
 46218 **
 46219 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
 46220 ** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
 46221 ** to 0.
 46222 **
 46223 ** If the wal-index header is successfully read, return SQLITE_OK. 
 46224 ** Otherwise an SQLite error code.
 46225 */
 46226 static int walIndexReadHdr(Wal *pWal, int *pChanged){
 46227   int rc;                         /* Return code */
 46228   int badHdr;                     /* True if a header read failed */
 46229   volatile u32 *page0;            /* Chunk of wal-index containing header */
 46231   /* Ensure that page 0 of the wal-index (the page that contains the 
 46232   ** wal-index header) is mapped. Return early if an error occurs here.
 46233   */
 46234   assert( pChanged );
 46235   rc = walIndexPage(pWal, 0, &page0);
 46236   if( rc!=SQLITE_OK ){
 46237     return rc;
 46238   };
 46239   assert( page0 || pWal->writeLock==0 );
 46241   /* If the first page of the wal-index has been mapped, try to read the
 46242   ** wal-index header immediately, without holding any lock. This usually
 46243   ** works, but may fail if the wal-index header is corrupt or currently 
 46244   ** being modified by another thread or process.
 46245   */
 46246   badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
 46248   /* If the first attempt failed, it might have been due to a race
 46249   ** with a writer.  So get a WRITE lock and try again.
 46250   */
 46251   assert( badHdr==0 || pWal->writeLock==0 );
 46252   if( badHdr ){
 46253     if( pWal->readOnly & WAL_SHM_RDONLY ){
 46254       if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
 46255         walUnlockShared(pWal, WAL_WRITE_LOCK);
 46256         rc = SQLITE_READONLY_RECOVERY;
 46258     }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
 46259       pWal->writeLock = 1;
 46260       if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
 46261         badHdr = walIndexTryHdr(pWal, pChanged);
 46262         if( badHdr ){
 46263           /* If the wal-index header is still malformed even while holding
 46264           ** a WRITE lock, it can only mean that the header is corrupted and
 46265           ** needs to be reconstructed.  So run recovery to do exactly that.
 46266           */
 46267           rc = walIndexRecover(pWal);
 46268           *pChanged = 1;
 46271       pWal->writeLock = 0;
 46272       walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 46276   /* If the header is read successfully, check the version number to make
 46277   ** sure the wal-index was not constructed with some future format that
 46278   ** this version of SQLite cannot understand.
 46279   */
 46280   if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
 46281     rc = SQLITE_CANTOPEN_BKPT;
 46284   return rc;
 46287 /*
 46288 ** This is the value that walTryBeginRead returns when it needs to
 46289 ** be retried.
 46290 */
 46291 #define WAL_RETRY  (-1)
 46293 /*
 46294 ** Attempt to start a read transaction.  This might fail due to a race or
 46295 ** other transient condition.  When that happens, it returns WAL_RETRY to
 46296 ** indicate to the caller that it is safe to retry immediately.
 46297 **
 46298 ** On success return SQLITE_OK.  On a permanent failure (such an
 46299 ** I/O error or an SQLITE_BUSY because another process is running
 46300 ** recovery) return a positive error code.
 46301 **
 46302 ** The useWal parameter is true to force the use of the WAL and disable
 46303 ** the case where the WAL is bypassed because it has been completely
 46304 ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
 46305 ** to make a copy of the wal-index header into pWal->hdr.  If the 
 46306 ** wal-index header has changed, *pChanged is set to 1 (as an indication 
 46307 ** to the caller that the local paget cache is obsolete and needs to be 
 46308 ** flushed.)  When useWal==1, the wal-index header is assumed to already
 46309 ** be loaded and the pChanged parameter is unused.
 46310 **
 46311 ** The caller must set the cnt parameter to the number of prior calls to
 46312 ** this routine during the current read attempt that returned WAL_RETRY.
 46313 ** This routine will start taking more aggressive measures to clear the
 46314 ** race conditions after multiple WAL_RETRY returns, and after an excessive
 46315 ** number of errors will ultimately return SQLITE_PROTOCOL.  The
 46316 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
 46317 ** and is not honoring the locking protocol.  There is a vanishingly small
 46318 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
 46319 ** bad luck when there is lots of contention for the wal-index, but that
 46320 ** possibility is so small that it can be safely neglected, we believe.
 46321 **
 46322 ** On success, this routine obtains a read lock on 
 46323 ** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
 46324 ** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
 46325 ** that means the Wal does not hold any read lock.  The reader must not
 46326 ** access any database page that is modified by a WAL frame up to and
 46327 ** including frame number aReadMark[pWal->readLock].  The reader will
 46328 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
 46329 ** Or if pWal->readLock==0, then the reader will ignore the WAL
 46330 ** completely and get all content directly from the database file.
 46331 ** If the useWal parameter is 1 then the WAL will never be ignored and
 46332 ** this routine will always set pWal->readLock>0 on success.
 46333 ** When the read transaction is completed, the caller must release the
 46334 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
 46335 **
 46336 ** This routine uses the nBackfill and aReadMark[] fields of the header
 46337 ** to select a particular WAL_READ_LOCK() that strives to let the
 46338 ** checkpoint process do as much work as possible.  This routine might
 46339 ** update values of the aReadMark[] array in the header, but if it does
 46340 ** so it takes care to hold an exclusive lock on the corresponding
 46341 ** WAL_READ_LOCK() while changing values.
 46342 */
 46343 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
 46344   volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
 46345   u32 mxReadMark;                 /* Largest aReadMark[] value */
 46346   int mxI;                        /* Index of largest aReadMark[] value */
 46347   int i;                          /* Loop counter */
 46348   int rc = SQLITE_OK;             /* Return code  */
 46350   assert( pWal->readLock<0 );     /* Not currently locked */
 46352   /* Take steps to avoid spinning forever if there is a protocol error.
 46353   **
 46354   ** Circumstances that cause a RETRY should only last for the briefest
 46355   ** instances of time.  No I/O or other system calls are done while the
 46356   ** locks are held, so the locks should not be held for very long. But 
 46357   ** if we are unlucky, another process that is holding a lock might get
 46358   ** paged out or take a page-fault that is time-consuming to resolve, 
 46359   ** during the few nanoseconds that it is holding the lock.  In that case,
 46360   ** it might take longer than normal for the lock to free.
 46361   **
 46362   ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
 46363   ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
 46364   ** is more of a scheduler yield than an actual delay.  But on the 10th
 46365   ** an subsequent retries, the delays start becoming longer and longer, 
 46366   ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
 46367   ** The total delay time before giving up is less than 1 second.
 46368   */
 46369   if( cnt>5 ){
 46370     int nDelay = 1;                      /* Pause time in microseconds */
 46371     if( cnt>100 ){
 46372       VVA_ONLY( pWal->lockError = 1; )
 46373       return SQLITE_PROTOCOL;
 46375     if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
 46376     sqlite3OsSleep(pWal->pVfs, nDelay);
 46379   if( !useWal ){
 46380     rc = walIndexReadHdr(pWal, pChanged);
 46381     if( rc==SQLITE_BUSY ){
 46382       /* If there is not a recovery running in another thread or process
 46383       ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
 46384       ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
 46385       ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
 46386       ** would be technically correct.  But the race is benign since with
 46387       ** WAL_RETRY this routine will be called again and will probably be
 46388       ** right on the second iteration.
 46389       */
 46390       if( pWal->apWiData[0]==0 ){
 46391         /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
 46392         ** We assume this is a transient condition, so return WAL_RETRY. The
 46393         ** xShmMap() implementation used by the default unix and win32 VFS 
 46394         ** modules may return SQLITE_BUSY due to a race condition in the 
 46395         ** code that determines whether or not the shared-memory region 
 46396         ** must be zeroed before the requested page is returned.
 46397         */
 46398         rc = WAL_RETRY;
 46399       }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
 46400         walUnlockShared(pWal, WAL_RECOVER_LOCK);
 46401         rc = WAL_RETRY;
 46402       }else if( rc==SQLITE_BUSY ){
 46403         rc = SQLITE_BUSY_RECOVERY;
 46406     if( rc!=SQLITE_OK ){
 46407       return rc;
 46411   pInfo = walCkptInfo(pWal);
 46412   if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
 46413     /* The WAL has been completely backfilled (or it is empty).
 46414     ** and can be safely ignored.
 46415     */
 46416     rc = walLockShared(pWal, WAL_READ_LOCK(0));
 46417     walShmBarrier(pWal);
 46418     if( rc==SQLITE_OK ){
 46419       if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
 46420         /* It is not safe to allow the reader to continue here if frames
 46421         ** may have been appended to the log before READ_LOCK(0) was obtained.
 46422         ** When holding READ_LOCK(0), the reader ignores the entire log file,
 46423         ** which implies that the database file contains a trustworthy
 46424         ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
 46425         ** happening, this is usually correct.
 46426         **
 46427         ** However, if frames have been appended to the log (or if the log 
 46428         ** is wrapped and written for that matter) before the READ_LOCK(0)
 46429         ** is obtained, that is not necessarily true. A checkpointer may
 46430         ** have started to backfill the appended frames but crashed before
 46431         ** it finished. Leaving a corrupt image in the database file.
 46432         */
 46433         walUnlockShared(pWal, WAL_READ_LOCK(0));
 46434         return WAL_RETRY;
 46436       pWal->readLock = 0;
 46437       return SQLITE_OK;
 46438     }else if( rc!=SQLITE_BUSY ){
 46439       return rc;
 46443   /* If we get this far, it means that the reader will want to use
 46444   ** the WAL to get at content from recent commits.  The job now is
 46445   ** to select one of the aReadMark[] entries that is closest to
 46446   ** but not exceeding pWal->hdr.mxFrame and lock that entry.
 46447   */
 46448   mxReadMark = 0;
 46449   mxI = 0;
 46450   for(i=1; i<WAL_NREADER; i++){
 46451     u32 thisMark = pInfo->aReadMark[i];
 46452     if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
 46453       assert( thisMark!=READMARK_NOT_USED );
 46454       mxReadMark = thisMark;
 46455       mxI = i;
 46458   /* There was once an "if" here. The extra "{" is to preserve indentation. */
 46460     if( (pWal->readOnly & WAL_SHM_RDONLY)==0
 46461      && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
 46462     ){
 46463       for(i=1; i<WAL_NREADER; i++){
 46464         rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
 46465         if( rc==SQLITE_OK ){
 46466           mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
 46467           mxI = i;
 46468           walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 46469           break;
 46470         }else if( rc!=SQLITE_BUSY ){
 46471           return rc;
 46475     if( mxI==0 ){
 46476       assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
 46477       return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
 46480     rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
 46481     if( rc ){
 46482       return rc==SQLITE_BUSY ? WAL_RETRY : rc;
 46484     /* Now that the read-lock has been obtained, check that neither the
 46485     ** value in the aReadMark[] array or the contents of the wal-index
 46486     ** header have changed.
 46487     **
 46488     ** It is necessary to check that the wal-index header did not change
 46489     ** between the time it was read and when the shared-lock was obtained
 46490     ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
 46491     ** that the log file may have been wrapped by a writer, or that frames
 46492     ** that occur later in the log than pWal->hdr.mxFrame may have been
 46493     ** copied into the database by a checkpointer. If either of these things
 46494     ** happened, then reading the database with the current value of
 46495     ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
 46496     ** instead.
 46497     **
 46498     ** This does not guarantee that the copy of the wal-index header is up to
 46499     ** date before proceeding. That would not be possible without somehow
 46500     ** blocking writers. It only guarantees that a dangerous checkpoint or 
 46501     ** log-wrap (either of which would require an exclusive lock on
 46502     ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
 46503     */
 46504     walShmBarrier(pWal);
 46505     if( pInfo->aReadMark[mxI]!=mxReadMark
 46506      || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
 46507     ){
 46508       walUnlockShared(pWal, WAL_READ_LOCK(mxI));
 46509       return WAL_RETRY;
 46510     }else{
 46511       assert( mxReadMark<=pWal->hdr.mxFrame );
 46512       pWal->readLock = (i16)mxI;
 46515   return rc;
 46518 /*
 46519 ** Begin a read transaction on the database.
 46520 **
 46521 ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
 46522 ** it takes a snapshot of the state of the WAL and wal-index for the current
 46523 ** instant in time.  The current thread will continue to use this snapshot.
 46524 ** Other threads might append new content to the WAL and wal-index but
 46525 ** that extra content is ignored by the current thread.
 46526 **
 46527 ** If the database contents have changes since the previous read
 46528 ** transaction, then *pChanged is set to 1 before returning.  The
 46529 ** Pager layer will use this to know that is cache is stale and
 46530 ** needs to be flushed.
 46531 */
 46532 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
 46533   int rc;                         /* Return code */
 46534   int cnt = 0;                    /* Number of TryBeginRead attempts */
 46536   do{
 46537     rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
 46538   }while( rc==WAL_RETRY );
 46539   testcase( (rc&0xff)==SQLITE_BUSY );
 46540   testcase( (rc&0xff)==SQLITE_IOERR );
 46541   testcase( rc==SQLITE_PROTOCOL );
 46542   testcase( rc==SQLITE_OK );
 46543   return rc;
 46546 /*
 46547 ** Finish with a read transaction.  All this does is release the
 46548 ** read-lock.
 46549 */
 46550 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
 46551   sqlite3WalEndWriteTransaction(pWal);
 46552   if( pWal->readLock>=0 ){
 46553     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
 46554     pWal->readLock = -1;
 46558 /*
 46559 ** Read a page from the WAL, if it is present in the WAL and if the 
 46560 ** current read transaction is configured to use the WAL.  
 46561 **
 46562 ** The *pInWal is set to 1 if the requested page is in the WAL and
 46563 ** has been loaded.  Or *pInWal is set to 0 if the page was not in 
 46564 ** the WAL and needs to be read out of the database.
 46565 */
 46566 SQLITE_PRIVATE int sqlite3WalRead(
 46567   Wal *pWal,                      /* WAL handle */
 46568   Pgno pgno,                      /* Database page number to read data for */
 46569   int *pInWal,                    /* OUT: True if data is read from WAL */
 46570   int nOut,                       /* Size of buffer pOut in bytes */
 46571   u8 *pOut                        /* Buffer to write page data to */
 46572 ){
 46573   u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
 46574   u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
 46575   int iHash;                      /* Used to loop through N hash tables */
 46577   /* This routine is only be called from within a read transaction. */
 46578   assert( pWal->readLock>=0 || pWal->lockError );
 46580   /* If the "last page" field of the wal-index header snapshot is 0, then
 46581   ** no data will be read from the wal under any circumstances. Return early
 46582   ** in this case as an optimization.  Likewise, if pWal->readLock==0, 
 46583   ** then the WAL is ignored by the reader so return early, as if the 
 46584   ** WAL were empty.
 46585   */
 46586   if( iLast==0 || pWal->readLock==0 ){
 46587     *pInWal = 0;
 46588     return SQLITE_OK;
 46591   /* Search the hash table or tables for an entry matching page number
 46592   ** pgno. Each iteration of the following for() loop searches one
 46593   ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
 46594   **
 46595   ** This code might run concurrently to the code in walIndexAppend()
 46596   ** that adds entries to the wal-index (and possibly to this hash 
 46597   ** table). This means the value just read from the hash 
 46598   ** slot (aHash[iKey]) may have been added before or after the 
 46599   ** current read transaction was opened. Values added after the
 46600   ** read transaction was opened may have been written incorrectly -
 46601   ** i.e. these slots may contain garbage data. However, we assume
 46602   ** that any slots written before the current read transaction was
 46603   ** opened remain unmodified.
 46604   **
 46605   ** For the reasons above, the if(...) condition featured in the inner
 46606   ** loop of the following block is more stringent that would be required 
 46607   ** if we had exclusive access to the hash-table:
 46608   **
 46609   **   (aPgno[iFrame]==pgno): 
 46610   **     This condition filters out normal hash-table collisions.
 46611   **
 46612   **   (iFrame<=iLast): 
 46613   **     This condition filters out entries that were added to the hash
 46614   **     table after the current read-transaction had started.
 46615   */
 46616   for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
 46617     volatile ht_slot *aHash;      /* Pointer to hash table */
 46618     volatile u32 *aPgno;          /* Pointer to array of page numbers */
 46619     u32 iZero;                    /* Frame number corresponding to aPgno[0] */
 46620     int iKey;                     /* Hash slot index */
 46621     int nCollide;                 /* Number of hash collisions remaining */
 46622     int rc;                       /* Error code */
 46624     rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
 46625     if( rc!=SQLITE_OK ){
 46626       return rc;
 46628     nCollide = HASHTABLE_NSLOT;
 46629     for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
 46630       u32 iFrame = aHash[iKey] + iZero;
 46631       if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
 46632         /* assert( iFrame>iRead ); -- not true if there is corruption */
 46633         iRead = iFrame;
 46635       if( (nCollide--)==0 ){
 46636         return SQLITE_CORRUPT_BKPT;
 46641 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 46642   /* If expensive assert() statements are available, do a linear search
 46643   ** of the wal-index file content. Make sure the results agree with the
 46644   ** result obtained using the hash indexes above.  */
 46646     u32 iRead2 = 0;
 46647     u32 iTest;
 46648     for(iTest=iLast; iTest>0; iTest--){
 46649       if( walFramePgno(pWal, iTest)==pgno ){
 46650         iRead2 = iTest;
 46651         break;
 46654     assert( iRead==iRead2 );
 46656 #endif
 46658   /* If iRead is non-zero, then it is the log frame number that contains the
 46659   ** required page. Read and return data from the log file.
 46660   */
 46661   if( iRead ){
 46662     int sz;
 46663     i64 iOffset;
 46664     sz = pWal->hdr.szPage;
 46665     sz = (sz&0xfe00) + ((sz&0x0001)<<16);
 46666     testcase( sz<=32768 );
 46667     testcase( sz>=65536 );
 46668     iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
 46669     *pInWal = 1;
 46670     /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
 46671     return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
 46674   *pInWal = 0;
 46675   return SQLITE_OK;
 46679 /* 
 46680 ** Return the size of the database in pages (or zero, if unknown).
 46681 */
 46682 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
 46683   if( pWal && ALWAYS(pWal->readLock>=0) ){
 46684     return pWal->hdr.nPage;
 46686   return 0;
 46690 /* 
 46691 ** This function starts a write transaction on the WAL.
 46692 **
 46693 ** A read transaction must have already been started by a prior call
 46694 ** to sqlite3WalBeginReadTransaction().
 46695 **
 46696 ** If another thread or process has written into the database since
 46697 ** the read transaction was started, then it is not possible for this
 46698 ** thread to write as doing so would cause a fork.  So this routine
 46699 ** returns SQLITE_BUSY in that case and no write transaction is started.
 46700 **
 46701 ** There can only be a single writer active at a time.
 46702 */
 46703 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
 46704   int rc;
 46706   /* Cannot start a write transaction without first holding a read
 46707   ** transaction. */
 46708   assert( pWal->readLock>=0 );
 46710   if( pWal->readOnly ){
 46711     return SQLITE_READONLY;
 46714   /* Only one writer allowed at a time.  Get the write lock.  Return
 46715   ** SQLITE_BUSY if unable.
 46716   */
 46717   rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
 46718   if( rc ){
 46719     return rc;
 46721   pWal->writeLock = 1;
 46723   /* If another connection has written to the database file since the
 46724   ** time the read transaction on this connection was started, then
 46725   ** the write is disallowed.
 46726   */
 46727   if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
 46728     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 46729     pWal->writeLock = 0;
 46730     rc = SQLITE_BUSY;
 46733   return rc;
 46736 /*
 46737 ** End a write transaction.  The commit has already been done.  This
 46738 ** routine merely releases the lock.
 46739 */
 46740 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
 46741   if( pWal->writeLock ){
 46742     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 46743     pWal->writeLock = 0;
 46744     pWal->truncateOnCommit = 0;
 46746   return SQLITE_OK;
 46749 /*
 46750 ** If any data has been written (but not committed) to the log file, this
 46751 ** function moves the write-pointer back to the start of the transaction.
 46752 **
 46753 ** Additionally, the callback function is invoked for each frame written
 46754 ** to the WAL since the start of the transaction. If the callback returns
 46755 ** other than SQLITE_OK, it is not invoked again and the error code is
 46756 ** returned to the caller.
 46757 **
 46758 ** Otherwise, if the callback function does not return an error, this
 46759 ** function returns SQLITE_OK.
 46760 */
 46761 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
 46762   int rc = SQLITE_OK;
 46763   if( ALWAYS(pWal->writeLock) ){
 46764     Pgno iMax = pWal->hdr.mxFrame;
 46765     Pgno iFrame;
 46767     /* Restore the clients cache of the wal-index header to the state it
 46768     ** was in before the client began writing to the database. 
 46769     */
 46770     memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
 46772     for(iFrame=pWal->hdr.mxFrame+1; 
 46773         ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; 
 46774         iFrame++
 46775     ){
 46776       /* This call cannot fail. Unless the page for which the page number
 46777       ** is passed as the second argument is (a) in the cache and 
 46778       ** (b) has an outstanding reference, then xUndo is either a no-op
 46779       ** (if (a) is false) or simply expels the page from the cache (if (b)
 46780       ** is false).
 46781       **
 46782       ** If the upper layer is doing a rollback, it is guaranteed that there
 46783       ** are no outstanding references to any page other than page 1. And
 46784       ** page 1 is never written to the log until the transaction is
 46785       ** committed. As a result, the call to xUndo may not fail.
 46786       */
 46787       assert( walFramePgno(pWal, iFrame)!=1 );
 46788       rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
 46790     if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
 46792   assert( rc==SQLITE_OK );
 46793   return rc;
 46796 /* 
 46797 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 
 46798 ** values. This function populates the array with values required to 
 46799 ** "rollback" the write position of the WAL handle back to the current 
 46800 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
 46801 */
 46802 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
 46803   assert( pWal->writeLock );
 46804   aWalData[0] = pWal->hdr.mxFrame;
 46805   aWalData[1] = pWal->hdr.aFrameCksum[0];
 46806   aWalData[2] = pWal->hdr.aFrameCksum[1];
 46807   aWalData[3] = pWal->nCkpt;
 46810 /* 
 46811 ** Move the write position of the WAL back to the point identified by
 46812 ** the values in the aWalData[] array. aWalData must point to an array
 46813 ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
 46814 ** by a call to WalSavepoint().
 46815 */
 46816 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
 46817   int rc = SQLITE_OK;
 46819   assert( pWal->writeLock );
 46820   assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
 46822   if( aWalData[3]!=pWal->nCkpt ){
 46823     /* This savepoint was opened immediately after the write-transaction
 46824     ** was started. Right after that, the writer decided to wrap around
 46825     ** to the start of the log. Update the savepoint values to match.
 46826     */
 46827     aWalData[0] = 0;
 46828     aWalData[3] = pWal->nCkpt;
 46831   if( aWalData[0]<pWal->hdr.mxFrame ){
 46832     pWal->hdr.mxFrame = aWalData[0];
 46833     pWal->hdr.aFrameCksum[0] = aWalData[1];
 46834     pWal->hdr.aFrameCksum[1] = aWalData[2];
 46835     walCleanupHash(pWal);
 46838   return rc;
 46842 /*
 46843 ** This function is called just before writing a set of frames to the log
 46844 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
 46845 ** to the current log file, it is possible to overwrite the start of the
 46846 ** existing log file with the new frames (i.e. "reset" the log). If so,
 46847 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
 46848 ** unchanged.
 46849 **
 46850 ** SQLITE_OK is returned if no error is encountered (regardless of whether
 46851 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
 46852 ** if an error occurs.
 46853 */
 46854 static int walRestartLog(Wal *pWal){
 46855   int rc = SQLITE_OK;
 46856   int cnt;
 46858   if( pWal->readLock==0 ){
 46859     volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
 46860     assert( pInfo->nBackfill==pWal->hdr.mxFrame );
 46861     if( pInfo->nBackfill>0 ){
 46862       u32 salt1;
 46863       sqlite3_randomness(4, &salt1);
 46864       rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 46865       if( rc==SQLITE_OK ){
 46866         /* If all readers are using WAL_READ_LOCK(0) (in other words if no
 46867         ** readers are currently using the WAL), then the transactions
 46868         ** frames will overwrite the start of the existing log. Update the
 46869         ** wal-index header to reflect this.
 46870         **
 46871         ** In theory it would be Ok to update the cache of the header only
 46872         ** at this point. But updating the actual wal-index header is also
 46873         ** safe and means there is no special case for sqlite3WalUndo()
 46874         ** to handle if this transaction is rolled back.
 46875         */
 46876         int i;                    /* Loop counter */
 46877         u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
 46879         pWal->nCkpt++;
 46880         pWal->hdr.mxFrame = 0;
 46881         sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
 46882         aSalt[1] = salt1;
 46883         walIndexWriteHdr(pWal);
 46884         pInfo->nBackfill = 0;
 46885         pInfo->aReadMark[1] = 0;
 46886         for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 46887         assert( pInfo->aReadMark[0]==0 );
 46888         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 46889       }else if( rc!=SQLITE_BUSY ){
 46890         return rc;
 46893     walUnlockShared(pWal, WAL_READ_LOCK(0));
 46894     pWal->readLock = -1;
 46895     cnt = 0;
 46896     do{
 46897       int notUsed;
 46898       rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
 46899     }while( rc==WAL_RETRY );
 46900     assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
 46901     testcase( (rc&0xff)==SQLITE_IOERR );
 46902     testcase( rc==SQLITE_PROTOCOL );
 46903     testcase( rc==SQLITE_OK );
 46905   return rc;
 46908 /*
 46909 ** Information about the current state of the WAL file and where
 46910 ** the next fsync should occur - passed from sqlite3WalFrames() into
 46911 ** walWriteToLog().
 46912 */
 46913 typedef struct WalWriter {
 46914   Wal *pWal;                   /* The complete WAL information */
 46915   sqlite3_file *pFd;           /* The WAL file to which we write */
 46916   sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
 46917   int syncFlags;               /* Flags for the fsync */
 46918   int szPage;                  /* Size of one page */
 46919 } WalWriter;
 46921 /*
 46922 ** Write iAmt bytes of content into the WAL file beginning at iOffset.
 46923 ** Do a sync when crossing the p->iSyncPoint boundary.
 46924 **
 46925 ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
 46926 ** first write the part before iSyncPoint, then sync, then write the
 46927 ** rest.
 46928 */
 46929 static int walWriteToLog(
 46930   WalWriter *p,              /* WAL to write to */
 46931   void *pContent,            /* Content to be written */
 46932   int iAmt,                  /* Number of bytes to write */
 46933   sqlite3_int64 iOffset      /* Start writing at this offset */
 46934 ){
 46935   int rc;
 46936   if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
 46937     int iFirstAmt = (int)(p->iSyncPoint - iOffset);
 46938     rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
 46939     if( rc ) return rc;
 46940     iOffset += iFirstAmt;
 46941     iAmt -= iFirstAmt;
 46942     pContent = (void*)(iFirstAmt + (char*)pContent);
 46943     assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
 46944     rc = sqlite3OsSync(p->pFd, p->syncFlags);
 46945     if( iAmt==0 || rc ) return rc;
 46947   rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
 46948   return rc;
 46951 /*
 46952 ** Write out a single frame of the WAL
 46953 */
 46954 static int walWriteOneFrame(
 46955   WalWriter *p,               /* Where to write the frame */
 46956   PgHdr *pPage,               /* The page of the frame to be written */
 46957   int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
 46958   sqlite3_int64 iOffset       /* Byte offset at which to write */
 46959 ){
 46960   int rc;                         /* Result code from subfunctions */
 46961   void *pData;                    /* Data actually written */
 46962   u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
 46963 #if defined(SQLITE_HAS_CODEC)
 46964   if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
 46965 #else
 46966   pData = pPage->pData;
 46967 #endif
 46968   walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
 46969   rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
 46970   if( rc ) return rc;
 46971   /* Write the page data */
 46972   rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
 46973   return rc;
 46976 /* 
 46977 ** Write a set of frames to the log. The caller must hold the write-lock
 46978 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
 46979 */
 46980 SQLITE_PRIVATE int sqlite3WalFrames(
 46981   Wal *pWal,                      /* Wal handle to write to */
 46982   int szPage,                     /* Database page-size in bytes */
 46983   PgHdr *pList,                   /* List of dirty pages to write */
 46984   Pgno nTruncate,                 /* Database size after this commit */
 46985   int isCommit,                   /* True if this is a commit */
 46986   int sync_flags                  /* Flags to pass to OsSync() (or 0) */
 46987 ){
 46988   int rc;                         /* Used to catch return codes */
 46989   u32 iFrame;                     /* Next frame address */
 46990   PgHdr *p;                       /* Iterator to run through pList with. */
 46991   PgHdr *pLast = 0;               /* Last frame in list */
 46992   int nExtra = 0;                 /* Number of extra copies of last page */
 46993   int szFrame;                    /* The size of a single frame */
 46994   i64 iOffset;                    /* Next byte to write in WAL file */
 46995   WalWriter w;                    /* The writer */
 46997   assert( pList );
 46998   assert( pWal->writeLock );
 47000   /* If this frame set completes a transaction, then nTruncate>0.  If
 47001   ** nTruncate==0 then this frame set does not complete the transaction. */
 47002   assert( (isCommit!=0)==(nTruncate!=0) );
 47004 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 47005   { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
 47006     WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
 47007               pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
 47009 #endif
 47011   /* See if it is possible to write these frames into the start of the
 47012   ** log file, instead of appending to it at pWal->hdr.mxFrame.
 47013   */
 47014   if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
 47015     return rc;
 47018   /* If this is the first frame written into the log, write the WAL
 47019   ** header to the start of the WAL file. See comments at the top of
 47020   ** this source file for a description of the WAL header format.
 47021   */
 47022   iFrame = pWal->hdr.mxFrame;
 47023   if( iFrame==0 ){
 47024     u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
 47025     u32 aCksum[2];                /* Checksum for wal-header */
 47027     sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
 47028     sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
 47029     sqlite3Put4byte(&aWalHdr[8], szPage);
 47030     sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
 47031     if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
 47032     memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
 47033     walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
 47034     sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
 47035     sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
 47037     pWal->szPage = szPage;
 47038     pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
 47039     pWal->hdr.aFrameCksum[0] = aCksum[0];
 47040     pWal->hdr.aFrameCksum[1] = aCksum[1];
 47041     pWal->truncateOnCommit = 1;
 47043     rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
 47044     WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
 47045     if( rc!=SQLITE_OK ){
 47046       return rc;
 47049     /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
 47050     ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
 47051     ** an out-of-order write following a WAL restart could result in
 47052     ** database corruption.  See the ticket:
 47053     **
 47054     **     http://localhost:591/sqlite/info/ff5be73dee
 47055     */
 47056     if( pWal->syncHeader && sync_flags ){
 47057       rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
 47058       if( rc ) return rc;
 47061   assert( (int)pWal->szPage==szPage );
 47063   /* Setup information needed to write frames into the WAL */
 47064   w.pWal = pWal;
 47065   w.pFd = pWal->pWalFd;
 47066   w.iSyncPoint = 0;
 47067   w.syncFlags = sync_flags;
 47068   w.szPage = szPage;
 47069   iOffset = walFrameOffset(iFrame+1, szPage);
 47070   szFrame = szPage + WAL_FRAME_HDRSIZE;
 47072   /* Write all frames into the log file exactly once */
 47073   for(p=pList; p; p=p->pDirty){
 47074     int nDbSize;   /* 0 normally.  Positive == commit flag */
 47075     iFrame++;
 47076     assert( iOffset==walFrameOffset(iFrame, szPage) );
 47077     nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
 47078     rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
 47079     if( rc ) return rc;
 47080     pLast = p;
 47081     iOffset += szFrame;
 47084   /* If this is the end of a transaction, then we might need to pad
 47085   ** the transaction and/or sync the WAL file.
 47086   **
 47087   ** Padding and syncing only occur if this set of frames complete a
 47088   ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
 47089   ** or synchonous==OFF, then no padding or syncing are needed.
 47090   **
 47091   ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
 47092   ** needed and only the sync is done.  If padding is needed, then the
 47093   ** final frame is repeated (with its commit mark) until the next sector
 47094   ** boundary is crossed.  Only the part of the WAL prior to the last
 47095   ** sector boundary is synced; the part of the last frame that extends
 47096   ** past the sector boundary is written after the sync.
 47097   */
 47098   if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
 47099     if( pWal->padToSectorBoundary ){
 47100       int sectorSize = sqlite3SectorSize(pWal->pWalFd);
 47101       w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
 47102       while( iOffset<w.iSyncPoint ){
 47103         rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
 47104         if( rc ) return rc;
 47105         iOffset += szFrame;
 47106         nExtra++;
 47108     }else{
 47109       rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
 47113   /* If this frame set completes the first transaction in the WAL and
 47114   ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
 47115   ** journal size limit, if possible.
 47116   */
 47117   if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
 47118     i64 sz = pWal->mxWalSize;
 47119     if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
 47120       sz = walFrameOffset(iFrame+nExtra+1, szPage);
 47122     walLimitSize(pWal, sz);
 47123     pWal->truncateOnCommit = 0;
 47126   /* Append data to the wal-index. It is not necessary to lock the 
 47127   ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
 47128   ** guarantees that there are no other writers, and no data that may
 47129   ** be in use by existing readers is being overwritten.
 47130   */
 47131   iFrame = pWal->hdr.mxFrame;
 47132   for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
 47133     iFrame++;
 47134     rc = walIndexAppend(pWal, iFrame, p->pgno);
 47136   while( rc==SQLITE_OK && nExtra>0 ){
 47137     iFrame++;
 47138     nExtra--;
 47139     rc = walIndexAppend(pWal, iFrame, pLast->pgno);
 47142   if( rc==SQLITE_OK ){
 47143     /* Update the private copy of the header. */
 47144     pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
 47145     testcase( szPage<=32768 );
 47146     testcase( szPage>=65536 );
 47147     pWal->hdr.mxFrame = iFrame;
 47148     if( isCommit ){
 47149       pWal->hdr.iChange++;
 47150       pWal->hdr.nPage = nTruncate;
 47152     /* If this is a commit, update the wal-index header too. */
 47153     if( isCommit ){
 47154       walIndexWriteHdr(pWal);
 47155       pWal->iCallback = iFrame;
 47159   WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
 47160   return rc;
 47163 /* 
 47164 ** This routine is called to implement sqlite3_wal_checkpoint() and
 47165 ** related interfaces.
 47166 **
 47167 ** Obtain a CHECKPOINT lock and then backfill as much information as
 47168 ** we can from WAL into the database.
 47169 **
 47170 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
 47171 ** callback. In this case this function runs a blocking checkpoint.
 47172 */
 47173 SQLITE_PRIVATE int sqlite3WalCheckpoint(
 47174   Wal *pWal,                      /* Wal connection */
 47175   int eMode,                      /* PASSIVE, FULL or RESTART */
 47176   int (*xBusy)(void*),            /* Function to call when busy */
 47177   void *pBusyArg,                 /* Context argument for xBusyHandler */
 47178   int sync_flags,                 /* Flags to sync db file with (or 0) */
 47179   int nBuf,                       /* Size of temporary buffer */
 47180   u8 *zBuf,                       /* Temporary buffer to use */
 47181   int *pnLog,                     /* OUT: Number of frames in WAL */
 47182   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 47183 ){
 47184   int rc;                         /* Return code */
 47185   int isChanged = 0;              /* True if a new wal-index header is loaded */
 47186   int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
 47188   assert( pWal->ckptLock==0 );
 47189   assert( pWal->writeLock==0 );
 47191   if( pWal->readOnly ) return SQLITE_READONLY;
 47192   WALTRACE(("WAL%p: checkpoint begins\n", pWal));
 47193   rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
 47194   if( rc ){
 47195     /* Usually this is SQLITE_BUSY meaning that another thread or process
 47196     ** is already running a checkpoint, or maybe a recovery.  But it might
 47197     ** also be SQLITE_IOERR. */
 47198     return rc;
 47200   pWal->ckptLock = 1;
 47202   /* If this is a blocking-checkpoint, then obtain the write-lock as well
 47203   ** to prevent any writers from running while the checkpoint is underway.
 47204   ** This has to be done before the call to walIndexReadHdr() below.
 47205   **
 47206   ** If the writer lock cannot be obtained, then a passive checkpoint is
 47207   ** run instead. Since the checkpointer is not holding the writer lock,
 47208   ** there is no point in blocking waiting for any readers. Assuming no 
 47209   ** other error occurs, this function will return SQLITE_BUSY to the caller.
 47210   */
 47211   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
 47212     rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
 47213     if( rc==SQLITE_OK ){
 47214       pWal->writeLock = 1;
 47215     }else if( rc==SQLITE_BUSY ){
 47216       eMode2 = SQLITE_CHECKPOINT_PASSIVE;
 47217       rc = SQLITE_OK;
 47221   /* Read the wal-index header. */
 47222   if( rc==SQLITE_OK ){
 47223     rc = walIndexReadHdr(pWal, &isChanged);
 47226   /* Copy data from the log to the database file. */
 47227   if( rc==SQLITE_OK ){
 47228     if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
 47229       rc = SQLITE_CORRUPT_BKPT;
 47230     }else{
 47231       rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
 47234     /* If no error occurred, set the output variables. */
 47235     if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
 47236       if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
 47237       if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
 47241   if( isChanged ){
 47242     /* If a new wal-index header was loaded before the checkpoint was 
 47243     ** performed, then the pager-cache associated with pWal is now
 47244     ** out of date. So zero the cached wal-index header to ensure that
 47245     ** next time the pager opens a snapshot on this database it knows that
 47246     ** the cache needs to be reset.
 47247     */
 47248     memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 47251   /* Release the locks. */
 47252   sqlite3WalEndWriteTransaction(pWal);
 47253   walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
 47254   pWal->ckptLock = 0;
 47255   WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
 47256   return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
 47259 /* Return the value to pass to a sqlite3_wal_hook callback, the
 47260 ** number of frames in the WAL at the point of the last commit since
 47261 ** sqlite3WalCallback() was called.  If no commits have occurred since
 47262 ** the last call, then return 0.
 47263 */
 47264 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
 47265   u32 ret = 0;
 47266   if( pWal ){
 47267     ret = pWal->iCallback;
 47268     pWal->iCallback = 0;
 47270   return (int)ret;
 47273 /*
 47274 ** This function is called to change the WAL subsystem into or out
 47275 ** of locking_mode=EXCLUSIVE.
 47276 **
 47277 ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
 47278 ** into locking_mode=NORMAL.  This means that we must acquire a lock
 47279 ** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
 47280 ** or if the acquisition of the lock fails, then return 0.  If the
 47281 ** transition out of exclusive-mode is successful, return 1.  This
 47282 ** operation must occur while the pager is still holding the exclusive
 47283 ** lock on the main database file.
 47284 **
 47285 ** If op is one, then change from locking_mode=NORMAL into 
 47286 ** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
 47287 ** be released.  Return 1 if the transition is made and 0 if the
 47288 ** WAL is already in exclusive-locking mode - meaning that this
 47289 ** routine is a no-op.  The pager must already hold the exclusive lock
 47290 ** on the main database file before invoking this operation.
 47291 **
 47292 ** If op is negative, then do a dry-run of the op==1 case but do
 47293 ** not actually change anything. The pager uses this to see if it
 47294 ** should acquire the database exclusive lock prior to invoking
 47295 ** the op==1 case.
 47296 */
 47297 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
 47298   int rc;
 47299   assert( pWal->writeLock==0 );
 47300   assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
 47302   /* pWal->readLock is usually set, but might be -1 if there was a 
 47303   ** prior error while attempting to acquire are read-lock. This cannot 
 47304   ** happen if the connection is actually in exclusive mode (as no xShmLock
 47305   ** locks are taken in this case). Nor should the pager attempt to
 47306   ** upgrade to exclusive-mode following such an error.
 47307   */
 47308   assert( pWal->readLock>=0 || pWal->lockError );
 47309   assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
 47311   if( op==0 ){
 47312     if( pWal->exclusiveMode ){
 47313       pWal->exclusiveMode = 0;
 47314       if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
 47315         pWal->exclusiveMode = 1;
 47317       rc = pWal->exclusiveMode==0;
 47318     }else{
 47319       /* Already in locking_mode=NORMAL */
 47320       rc = 0;
 47322   }else if( op>0 ){
 47323     assert( pWal->exclusiveMode==0 );
 47324     assert( pWal->readLock>=0 );
 47325     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
 47326     pWal->exclusiveMode = 1;
 47327     rc = 1;
 47328   }else{
 47329     rc = pWal->exclusiveMode==0;
 47331   return rc;
 47334 /* 
 47335 ** Return true if the argument is non-NULL and the WAL module is using
 47336 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
 47337 ** WAL module is using shared-memory, return false. 
 47338 */
 47339 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
 47340   return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
 47343 #ifdef SQLITE_ENABLE_ZIPVFS
 47344 /*
 47345 ** If the argument is not NULL, it points to a Wal object that holds a
 47346 ** read-lock. This function returns the database page-size if it is known,
 47347 ** or zero if it is not (or if pWal is NULL).
 47348 */
 47349 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
 47350   assert( pWal==0 || pWal->readLock>=0 );
 47351   return (pWal ? pWal->szPage : 0);
 47353 #endif
 47355 #endif /* #ifndef SQLITE_OMIT_WAL */
 47357 /************** End of wal.c *************************************************/
 47358 /************** Begin file btmutex.c *****************************************/
 47359 /*
 47360 ** 2007 August 27
 47361 **
 47362 ** The author disclaims copyright to this source code.  In place of
 47363 ** a legal notice, here is a blessing:
 47364 **
 47365 **    May you do good and not evil.
 47366 **    May you find forgiveness for yourself and forgive others.
 47367 **    May you share freely, never taking more than you give.
 47368 **
 47369 *************************************************************************
 47370 **
 47371 ** This file contains code used to implement mutexes on Btree objects.
 47372 ** This code really belongs in btree.c.  But btree.c is getting too
 47373 ** big and we want to break it down some.  This packaged seemed like
 47374 ** a good breakout.
 47375 */
 47376 /************** Include btreeInt.h in the middle of btmutex.c ****************/
 47377 /************** Begin file btreeInt.h ****************************************/
 47378 /*
 47379 ** 2004 April 6
 47380 **
 47381 ** The author disclaims copyright to this source code.  In place of
 47382 ** a legal notice, here is a blessing:
 47383 **
 47384 **    May you do good and not evil.
 47385 **    May you find forgiveness for yourself and forgive others.
 47386 **    May you share freely, never taking more than you give.
 47387 **
 47388 *************************************************************************
 47389 ** This file implements a external (disk-based) database using BTrees.
 47390 ** For a detailed discussion of BTrees, refer to
 47391 **
 47392 **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
 47393 **     "Sorting And Searching", pages 473-480. Addison-Wesley
 47394 **     Publishing Company, Reading, Massachusetts.
 47395 **
 47396 ** The basic idea is that each page of the file contains N database
 47397 ** entries and N+1 pointers to subpages.
 47398 **
 47399 **   ----------------------------------------------------------------
 47400 **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
 47401 **   ----------------------------------------------------------------
 47402 **
 47403 ** All of the keys on the page that Ptr(0) points to have values less
 47404 ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
 47405 ** values greater than Key(0) and less than Key(1).  All of the keys
 47406 ** on Ptr(N) and its subpages have values greater than Key(N-1).  And
 47407 ** so forth.
 47408 **
 47409 ** Finding a particular key requires reading O(log(M)) pages from the 
 47410 ** disk where M is the number of entries in the tree.
 47411 **
 47412 ** In this implementation, a single file can hold one or more separate 
 47413 ** BTrees.  Each BTree is identified by the index of its root page.  The
 47414 ** key and data for any entry are combined to form the "payload".  A
 47415 ** fixed amount of payload can be carried directly on the database
 47416 ** page.  If the payload is larger than the preset amount then surplus
 47417 ** bytes are stored on overflow pages.  The payload for an entry
 47418 ** and the preceding pointer are combined to form a "Cell".  Each 
 47419 ** page has a small header which contains the Ptr(N) pointer and other
 47420 ** information such as the size of key and data.
 47421 **
 47422 ** FORMAT DETAILS
 47423 **
 47424 ** The file is divided into pages.  The first page is called page 1,
 47425 ** the second is page 2, and so forth.  A page number of zero indicates
 47426 ** "no such page".  The page size can be any power of 2 between 512 and 65536.
 47427 ** Each page can be either a btree page, a freelist page, an overflow
 47428 ** page, or a pointer-map page.
 47429 **
 47430 ** The first page is always a btree page.  The first 100 bytes of the first
 47431 ** page contain a special header (the "file header") that describes the file.
 47432 ** The format of the file header is as follows:
 47433 **
 47434 **   OFFSET   SIZE    DESCRIPTION
 47435 **      0      16     Header string: "SQLite format 3\000"
 47436 **     16       2     Page size in bytes.  
 47437 **     18       1     File format write version
 47438 **     19       1     File format read version
 47439 **     20       1     Bytes of unused space at the end of each page
 47440 **     21       1     Max embedded payload fraction
 47441 **     22       1     Min embedded payload fraction
 47442 **     23       1     Min leaf payload fraction
 47443 **     24       4     File change counter
 47444 **     28       4     Reserved for future use
 47445 **     32       4     First freelist page
 47446 **     36       4     Number of freelist pages in the file
 47447 **     40      60     15 4-byte meta values passed to higher layers
 47448 **
 47449 **     40       4     Schema cookie
 47450 **     44       4     File format of schema layer
 47451 **     48       4     Size of page cache
 47452 **     52       4     Largest root-page (auto/incr_vacuum)
 47453 **     56       4     1=UTF-8 2=UTF16le 3=UTF16be
 47454 **     60       4     User version
 47455 **     64       4     Incremental vacuum mode
 47456 **     68       4     unused
 47457 **     72       4     unused
 47458 **     76       4     unused
 47459 **
 47460 ** All of the integer values are big-endian (most significant byte first).
 47461 **
 47462 ** The file change counter is incremented when the database is changed
 47463 ** This counter allows other processes to know when the file has changed
 47464 ** and thus when they need to flush their cache.
 47465 **
 47466 ** The max embedded payload fraction is the amount of the total usable
 47467 ** space in a page that can be consumed by a single cell for standard
 47468 ** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
 47469 ** is to limit the maximum cell size so that at least 4 cells will fit
 47470 ** on one page.  Thus the default max embedded payload fraction is 64.
 47471 **
 47472 ** If the payload for a cell is larger than the max payload, then extra
 47473 ** payload is spilled to overflow pages.  Once an overflow page is allocated,
 47474 ** as many bytes as possible are moved into the overflow pages without letting
 47475 ** the cell size drop below the min embedded payload fraction.
 47476 **
 47477 ** The min leaf payload fraction is like the min embedded payload fraction
 47478 ** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
 47479 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
 47480 ** not specified in the header.
 47481 **
 47482 ** Each btree pages is divided into three sections:  The header, the
 47483 ** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
 47484 ** file header that occurs before the page header.
 47485 **
 47486 **      |----------------|
 47487 **      | file header    |   100 bytes.  Page 1 only.
 47488 **      |----------------|
 47489 **      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
 47490 **      |----------------|
 47491 **      | cell pointer   |   |  2 bytes per cell.  Sorted order.
 47492 **      | array          |   |  Grows downward
 47493 **      |                |   v
 47494 **      |----------------|
 47495 **      | unallocated    |
 47496 **      | space          |
 47497 **      |----------------|   ^  Grows upwards
 47498 **      | cell content   |   |  Arbitrary order interspersed with freeblocks.
 47499 **      | area           |   |  and free space fragments.
 47500 **      |----------------|
 47501 **
 47502 ** The page headers looks like this:
 47503 **
 47504 **   OFFSET   SIZE     DESCRIPTION
 47505 **      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
 47506 **      1       2      byte offset to the first freeblock
 47507 **      3       2      number of cells on this page
 47508 **      5       2      first byte of the cell content area
 47509 **      7       1      number of fragmented free bytes
 47510 **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
 47511 **
 47512 ** The flags define the format of this btree page.  The leaf flag means that
 47513 ** this page has no children.  The zerodata flag means that this page carries
 47514 ** only keys and no data.  The intkey flag means that the key is a integer
 47515 ** which is stored in the key size entry of the cell header rather than in
 47516 ** the payload area.
 47517 **
 47518 ** The cell pointer array begins on the first byte after the page header.
 47519 ** The cell pointer array contains zero or more 2-byte numbers which are
 47520 ** offsets from the beginning of the page to the cell content in the cell
 47521 ** content area.  The cell pointers occur in sorted order.  The system strives
 47522 ** to keep free space after the last cell pointer so that new cells can
 47523 ** be easily added without having to defragment the page.
 47524 **
 47525 ** Cell content is stored at the very end of the page and grows toward the
 47526 ** beginning of the page.
 47527 **
 47528 ** Unused space within the cell content area is collected into a linked list of
 47529 ** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
 47530 ** to the first freeblock is given in the header.  Freeblocks occur in
 47531 ** increasing order.  Because a freeblock must be at least 4 bytes in size,
 47532 ** any group of 3 or fewer unused bytes in the cell content area cannot
 47533 ** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
 47534 ** a fragment.  The total number of bytes in all fragments is recorded.
 47535 ** in the page header at offset 7.
 47536 **
 47537 **    SIZE    DESCRIPTION
 47538 **      2     Byte offset of the next freeblock
 47539 **      2     Bytes in this freeblock
 47540 **
 47541 ** Cells are of variable length.  Cells are stored in the cell content area at
 47542 ** the end of the page.  Pointers to the cells are in the cell pointer array
 47543 ** that immediately follows the page header.  Cells is not necessarily
 47544 ** contiguous or in order, but cell pointers are contiguous and in order.
 47545 **
 47546 ** Cell content makes use of variable length integers.  A variable
 47547 ** length integer is 1 to 9 bytes where the lower 7 bits of each 
 47548 ** byte are used.  The integer consists of all bytes that have bit 8 set and
 47549 ** the first byte with bit 8 clear.  The most significant byte of the integer
 47550 ** appears first.  A variable-length integer may not be more than 9 bytes long.
 47551 ** As a special case, all 8 bytes of the 9th byte are used as data.  This
 47552 ** allows a 64-bit integer to be encoded in 9 bytes.
 47553 **
 47554 **    0x00                      becomes  0x00000000
 47555 **    0x7f                      becomes  0x0000007f
 47556 **    0x81 0x00                 becomes  0x00000080
 47557 **    0x82 0x00                 becomes  0x00000100
 47558 **    0x80 0x7f                 becomes  0x0000007f
 47559 **    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
 47560 **    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
 47561 **
 47562 ** Variable length integers are used for rowids and to hold the number of
 47563 ** bytes of key and data in a btree cell.
 47564 **
 47565 ** The content of a cell looks like this:
 47566 **
 47567 **    SIZE    DESCRIPTION
 47568 **      4     Page number of the left child. Omitted if leaf flag is set.
 47569 **     var    Number of bytes of data. Omitted if the zerodata flag is set.
 47570 **     var    Number of bytes of key. Or the key itself if intkey flag is set.
 47571 **      *     Payload
 47572 **      4     First page of the overflow chain.  Omitted if no overflow
 47573 **
 47574 ** Overflow pages form a linked list.  Each page except the last is completely
 47575 ** filled with data (pagesize - 4 bytes).  The last page can have as little
 47576 ** as 1 byte of data.
 47577 **
 47578 **    SIZE    DESCRIPTION
 47579 **      4     Page number of next overflow page
 47580 **      *     Data
 47581 **
 47582 ** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
 47583 ** file header points to the first in a linked list of trunk page.  Each trunk
 47584 ** page points to multiple leaf pages.  The content of a leaf page is
 47585 ** unspecified.  A trunk page looks like this:
 47586 **
 47587 **    SIZE    DESCRIPTION
 47588 **      4     Page number of next trunk page
 47589 **      4     Number of leaf pointers on this page
 47590 **      *     zero or more pages numbers of leaves
 47591 */
 47594 /* The following value is the maximum cell size assuming a maximum page
 47595 ** size give above.
 47596 */
 47597 #define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
 47599 /* The maximum number of cells on a single page of the database.  This
 47600 ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
 47601 ** plus 2 bytes for the index to the cell in the page header).  Such
 47602 ** small cells will be rare, but they are possible.
 47603 */
 47604 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
 47606 /* Forward declarations */
 47607 typedef struct MemPage MemPage;
 47608 typedef struct BtLock BtLock;
 47610 /*
 47611 ** This is a magic string that appears at the beginning of every
 47612 ** SQLite database in order to identify the file as a real database.
 47613 **
 47614 ** You can change this value at compile-time by specifying a
 47615 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
 47616 ** header must be exactly 16 bytes including the zero-terminator so
 47617 ** the string itself should be 15 characters long.  If you change
 47618 ** the header, then your custom library will not be able to read 
 47619 ** databases generated by the standard tools and the standard tools
 47620 ** will not be able to read databases created by your custom library.
 47621 */
 47622 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
 47623 #  define SQLITE_FILE_HEADER "SQLite format 3"
 47624 #endif
 47626 /*
 47627 ** Page type flags.  An ORed combination of these flags appear as the
 47628 ** first byte of on-disk image of every BTree page.
 47629 */
 47630 #define PTF_INTKEY    0x01
 47631 #define PTF_ZERODATA  0x02
 47632 #define PTF_LEAFDATA  0x04
 47633 #define PTF_LEAF      0x08
 47635 /*
 47636 ** As each page of the file is loaded into memory, an instance of the following
 47637 ** structure is appended and initialized to zero.  This structure stores
 47638 ** information about the page that is decoded from the raw file page.
 47639 **
 47640 ** The pParent field points back to the parent page.  This allows us to
 47641 ** walk up the BTree from any leaf to the root.  Care must be taken to
 47642 ** unref() the parent page pointer when this page is no longer referenced.
 47643 ** The pageDestructor() routine handles that chore.
 47644 **
 47645 ** Access to all fields of this structure is controlled by the mutex
 47646 ** stored in MemPage.pBt->mutex.
 47647 */
 47648 struct MemPage {
 47649   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
 47650   u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
 47651   u8 intKey;           /* True if intkey flag is set */
 47652   u8 leaf;             /* True if leaf flag is set */
 47653   u8 hasData;          /* True if this page stores data */
 47654   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
 47655   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
 47656   u8 max1bytePayload;  /* min(maxLocal,127) */
 47657   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
 47658   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
 47659   u16 cellOffset;      /* Index in aData of first cell pointer */
 47660   u16 nFree;           /* Number of free bytes on the page */
 47661   u16 nCell;           /* Number of cells on this page, local and ovfl */
 47662   u16 maskPage;        /* Mask for page offset */
 47663   u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
 47664                        ** non-overflow cell */
 47665   u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
 47666   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
 47667   u8 *aData;           /* Pointer to disk image of the page data */
 47668   u8 *aDataEnd;        /* One byte past the end of usable data */
 47669   u8 *aCellIdx;        /* The cell index area */
 47670   DbPage *pDbPage;     /* Pager page handle */
 47671   Pgno pgno;           /* Page number for this page */
 47672 };
 47674 /*
 47675 ** The in-memory image of a disk page has the auxiliary information appended
 47676 ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
 47677 ** that extra information.
 47678 */
 47679 #define EXTRA_SIZE sizeof(MemPage)
 47681 /*
 47682 ** A linked list of the following structures is stored at BtShared.pLock.
 47683 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
 47684 ** is opened on the table with root page BtShared.iTable. Locks are removed
 47685 ** from this list when a transaction is committed or rolled back, or when
 47686 ** a btree handle is closed.
 47687 */
 47688 struct BtLock {
 47689   Btree *pBtree;        /* Btree handle holding this lock */
 47690   Pgno iTable;          /* Root page of table */
 47691   u8 eLock;             /* READ_LOCK or WRITE_LOCK */
 47692   BtLock *pNext;        /* Next in BtShared.pLock list */
 47693 };
 47695 /* Candidate values for BtLock.eLock */
 47696 #define READ_LOCK     1
 47697 #define WRITE_LOCK    2
 47699 /* A Btree handle
 47700 **
 47701 ** A database connection contains a pointer to an instance of
 47702 ** this object for every database file that it has open.  This structure
 47703 ** is opaque to the database connection.  The database connection cannot
 47704 ** see the internals of this structure and only deals with pointers to
 47705 ** this structure.
 47706 **
 47707 ** For some database files, the same underlying database cache might be 
 47708 ** shared between multiple connections.  In that case, each connection
 47709 ** has it own instance of this object.  But each instance of this object
 47710 ** points to the same BtShared object.  The database cache and the
 47711 ** schema associated with the database file are all contained within
 47712 ** the BtShared object.
 47713 **
 47714 ** All fields in this structure are accessed under sqlite3.mutex.
 47715 ** The pBt pointer itself may not be changed while there exists cursors 
 47716 ** in the referenced BtShared that point back to this Btree since those
 47717 ** cursors have to go through this Btree to find their BtShared and
 47718 ** they often do so without holding sqlite3.mutex.
 47719 */
 47720 struct Btree {
 47721   sqlite3 *db;       /* The database connection holding this btree */
 47722   BtShared *pBt;     /* Sharable content of this btree */
 47723   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
 47724   u8 sharable;       /* True if we can share pBt with another db */
 47725   u8 locked;         /* True if db currently has pBt locked */
 47726   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
 47727   int nBackup;       /* Number of backup operations reading this btree */
 47728   Btree *pNext;      /* List of other sharable Btrees from the same db */
 47729   Btree *pPrev;      /* Back pointer of the same list */
 47730 #ifndef SQLITE_OMIT_SHARED_CACHE
 47731   BtLock lock;       /* Object used to lock page 1 */
 47732 #endif
 47733 };
 47735 /*
 47736 ** Btree.inTrans may take one of the following values.
 47737 **
 47738 ** If the shared-data extension is enabled, there may be multiple users
 47739 ** of the Btree structure. At most one of these may open a write transaction,
 47740 ** but any number may have active read transactions.
 47741 */
 47742 #define TRANS_NONE  0
 47743 #define TRANS_READ  1
 47744 #define TRANS_WRITE 2
 47746 /*
 47747 ** An instance of this object represents a single database file.
 47748 ** 
 47749 ** A single database file can be in use at the same time by two
 47750 ** or more database connections.  When two or more connections are
 47751 ** sharing the same database file, each connection has it own
 47752 ** private Btree object for the file and each of those Btrees points
 47753 ** to this one BtShared object.  BtShared.nRef is the number of
 47754 ** connections currently sharing this database file.
 47755 **
 47756 ** Fields in this structure are accessed under the BtShared.mutex
 47757 ** mutex, except for nRef and pNext which are accessed under the
 47758 ** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
 47759 ** may not be modified once it is initially set as long as nRef>0.
 47760 ** The pSchema field may be set once under BtShared.mutex and
 47761 ** thereafter is unchanged as long as nRef>0.
 47762 **
 47763 ** isPending:
 47764 **
 47765 **   If a BtShared client fails to obtain a write-lock on a database
 47766 **   table (because there exists one or more read-locks on the table),
 47767 **   the shared-cache enters 'pending-lock' state and isPending is
 47768 **   set to true.
 47769 **
 47770 **   The shared-cache leaves the 'pending lock' state when either of
 47771 **   the following occur:
 47772 **
 47773 **     1) The current writer (BtShared.pWriter) concludes its transaction, OR
 47774 **     2) The number of locks held by other connections drops to zero.
 47775 **
 47776 **   while in the 'pending-lock' state, no connection may start a new
 47777 **   transaction.
 47778 **
 47779 **   This feature is included to help prevent writer-starvation.
 47780 */
 47781 struct BtShared {
 47782   Pager *pPager;        /* The page cache */
 47783   sqlite3 *db;          /* Database connection currently using this Btree */
 47784   BtCursor *pCursor;    /* A list of all open cursors */
 47785   MemPage *pPage1;      /* First page of the database */
 47786   u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
 47787 #ifndef SQLITE_OMIT_AUTOVACUUM
 47788   u8 autoVacuum;        /* True if auto-vacuum is enabled */
 47789   u8 incrVacuum;        /* True if incr-vacuum is enabled */
 47790 #endif
 47791   u8 inTransaction;     /* Transaction state */
 47792   u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
 47793   u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
 47794   u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
 47795   u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
 47796   u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
 47797   u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
 47798   u32 pageSize;         /* Total number of bytes on a page */
 47799   u32 usableSize;       /* Number of usable bytes on each page */
 47800   int nTransaction;     /* Number of open transactions (read + write) */
 47801   u32 nPage;            /* Number of pages in the database */
 47802   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
 47803   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
 47804   sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
 47805   Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
 47806 #ifndef SQLITE_OMIT_SHARED_CACHE
 47807   int nRef;             /* Number of references to this structure */
 47808   BtShared *pNext;      /* Next on a list of sharable BtShared structs */
 47809   BtLock *pLock;        /* List of locks held on this shared-btree struct */
 47810   Btree *pWriter;       /* Btree with currently open write transaction */
 47811 #endif
 47812   u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
 47813 };
 47815 /*
 47816 ** Allowed values for BtShared.btsFlags
 47817 */
 47818 #define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
 47819 #define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
 47820 #define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
 47821 #define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
 47822 #define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
 47823 #define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
 47824 #define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
 47826 /*
 47827 ** An instance of the following structure is used to hold information
 47828 ** about a cell.  The parseCellPtr() function fills in this structure
 47829 ** based on information extract from the raw disk page.
 47830 */
 47831 typedef struct CellInfo CellInfo;
 47832 struct CellInfo {
 47833   i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
 47834   u8 *pCell;     /* Pointer to the start of cell content */
 47835   u32 nData;     /* Number of bytes of data */
 47836   u32 nPayload;  /* Total amount of payload */
 47837   u16 nHeader;   /* Size of the cell content header in bytes */
 47838   u16 nLocal;    /* Amount of payload held locally */
 47839   u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
 47840   u16 nSize;     /* Size of the cell content on the main b-tree page */
 47841 };
 47843 /*
 47844 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
 47845 ** this will be declared corrupt. This value is calculated based on a
 47846 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
 47847 ** root-node and 3 for all other internal nodes.
 47848 **
 47849 ** If a tree that appears to be taller than this is encountered, it is
 47850 ** assumed that the database is corrupt.
 47851 */
 47852 #define BTCURSOR_MAX_DEPTH 20
 47854 /*
 47855 ** A cursor is a pointer to a particular entry within a particular
 47856 ** b-tree within a database file.
 47857 **
 47858 ** The entry is identified by its MemPage and the index in
 47859 ** MemPage.aCell[] of the entry.
 47860 **
 47861 ** A single database file can be shared by two more database connections,
 47862 ** but cursors cannot be shared.  Each cursor is associated with a
 47863 ** particular database connection identified BtCursor.pBtree.db.
 47864 **
 47865 ** Fields in this structure are accessed under the BtShared.mutex
 47866 ** found at self->pBt->mutex. 
 47867 */
 47868 struct BtCursor {
 47869   Btree *pBtree;            /* The Btree to which this cursor belongs */
 47870   BtShared *pBt;            /* The BtShared this cursor points to */
 47871   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
 47872   struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
 47873 #ifndef SQLITE_OMIT_INCRBLOB
 47874   Pgno *aOverflow;          /* Cache of overflow page locations */
 47875 #endif
 47876   Pgno pgnoRoot;            /* The root page of this tree */
 47877   sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
 47878   CellInfo info;            /* A parse of the cell we are pointing at */
 47879   i64 nKey;        /* Size of pKey, or last integer key */
 47880   void *pKey;      /* Saved key that was cursor's last known position */
 47881   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
 47882   u8 wrFlag;                /* True if writable */
 47883   u8 atLast;                /* Cursor pointing to the last entry */
 47884   u8 validNKey;             /* True if info.nKey is valid */
 47885   u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 47886 #ifndef SQLITE_OMIT_INCRBLOB
 47887   u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
 47888 #endif
 47889   u8 hints;                             /* As configured by CursorSetHints() */
 47890   i16 iPage;                            /* Index of current page in apPage */
 47891   u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
 47892   MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
 47893 };
 47895 /*
 47896 ** Potential values for BtCursor.eState.
 47897 **
 47898 ** CURSOR_VALID:
 47899 **   Cursor points to a valid entry. getPayload() etc. may be called.
 47900 **
 47901 ** CURSOR_INVALID:
 47902 **   Cursor does not point to a valid entry. This can happen (for example) 
 47903 **   because the table is empty or because BtreeCursorFirst() has not been
 47904 **   called.
 47905 **
 47906 ** CURSOR_REQUIRESEEK:
 47907 **   The table that this cursor was opened on still exists, but has been 
 47908 **   modified since the cursor was last used. The cursor position is saved
 47909 **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
 47910 **   this state, restoreCursorPosition() can be called to attempt to
 47911 **   seek the cursor to the saved position.
 47912 **
 47913 ** CURSOR_FAULT:
 47914 **   A unrecoverable error (an I/O error or a malloc failure) has occurred
 47915 **   on a different connection that shares the BtShared cache with this
 47916 **   cursor.  The error has left the cache in an inconsistent state.
 47917 **   Do nothing else with this cursor.  Any attempt to use the cursor
 47918 **   should return the error code stored in BtCursor.skip
 47919 */
 47920 #define CURSOR_INVALID           0
 47921 #define CURSOR_VALID             1
 47922 #define CURSOR_REQUIRESEEK       2
 47923 #define CURSOR_FAULT             3
 47925 /* 
 47926 ** The database page the PENDING_BYTE occupies. This page is never used.
 47927 */
 47928 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
 47930 /*
 47931 ** These macros define the location of the pointer-map entry for a 
 47932 ** database page. The first argument to each is the number of usable
 47933 ** bytes on each page of the database (often 1024). The second is the
 47934 ** page number to look up in the pointer map.
 47935 **
 47936 ** PTRMAP_PAGENO returns the database page number of the pointer-map
 47937 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
 47938 ** the offset of the requested map entry.
 47939 **
 47940 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
 47941 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
 47942 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
 47943 ** this test.
 47944 */
 47945 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
 47946 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
 47947 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
 47949 /*
 47950 ** The pointer map is a lookup table that identifies the parent page for
 47951 ** each child page in the database file.  The parent page is the page that
 47952 ** contains a pointer to the child.  Every page in the database contains
 47953 ** 0 or 1 parent pages.  (In this context 'database page' refers
 47954 ** to any page that is not part of the pointer map itself.)  Each pointer map
 47955 ** entry consists of a single byte 'type' and a 4 byte parent page number.
 47956 ** The PTRMAP_XXX identifiers below are the valid types.
 47957 **
 47958 ** The purpose of the pointer map is to facility moving pages from one
 47959 ** position in the file to another as part of autovacuum.  When a page
 47960 ** is moved, the pointer in its parent must be updated to point to the
 47961 ** new location.  The pointer map is used to locate the parent page quickly.
 47962 **
 47963 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
 47964 **                  used in this case.
 47965 **
 47966 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
 47967 **                  is not used in this case.
 47968 **
 47969 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
 47970 **                   overflow pages. The page number identifies the page that
 47971 **                   contains the cell with a pointer to this overflow page.
 47972 **
 47973 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
 47974 **                   overflow pages. The page-number identifies the previous
 47975 **                   page in the overflow page list.
 47976 **
 47977 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
 47978 **               identifies the parent page in the btree.
 47979 */
 47980 #define PTRMAP_ROOTPAGE 1
 47981 #define PTRMAP_FREEPAGE 2
 47982 #define PTRMAP_OVERFLOW1 3
 47983 #define PTRMAP_OVERFLOW2 4
 47984 #define PTRMAP_BTREE 5
 47986 /* A bunch of assert() statements to check the transaction state variables
 47987 ** of handle p (type Btree*) are internally consistent.
 47988 */
 47989 #define btreeIntegrity(p) \
 47990   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
 47991   assert( p->pBt->inTransaction>=p->inTrans ); 
 47994 /*
 47995 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
 47996 ** if the database supports auto-vacuum or not. Because it is used
 47997 ** within an expression that is an argument to another macro 
 47998 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
 47999 ** So, this macro is defined instead.
 48000 */
 48001 #ifndef SQLITE_OMIT_AUTOVACUUM
 48002 #define ISAUTOVACUUM (pBt->autoVacuum)
 48003 #else
 48004 #define ISAUTOVACUUM 0
 48005 #endif
 48008 /*
 48009 ** This structure is passed around through all the sanity checking routines
 48010 ** in order to keep track of some global state information.
 48011 **
 48012 ** The aRef[] array is allocated so that there is 1 bit for each page in
 48013 ** the database. As the integrity-check proceeds, for each page used in
 48014 ** the database the corresponding bit is set. This allows integrity-check to 
 48015 ** detect pages that are used twice and orphaned pages (both of which 
 48016 ** indicate corruption).
 48017 */
 48018 typedef struct IntegrityCk IntegrityCk;
 48019 struct IntegrityCk {
 48020   BtShared *pBt;    /* The tree being checked out */
 48021   Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
 48022   u8 *aPgRef;       /* 1 bit per page in the db (see above) */
 48023   Pgno nPage;       /* Number of pages in the database */
 48024   int mxErr;        /* Stop accumulating errors when this reaches zero */
 48025   int nErr;         /* Number of messages written to zErrMsg so far */
 48026   int mallocFailed; /* A memory allocation error has occurred */
 48027   StrAccum errMsg;  /* Accumulate the error message text here */
 48028 };
 48030 /*
 48031 ** Routines to read or write a two- and four-byte big-endian integer values.
 48032 */
 48033 #define get2byte(x)   ((x)[0]<<8 | (x)[1])
 48034 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
 48035 #define get4byte sqlite3Get4byte
 48036 #define put4byte sqlite3Put4byte
 48038 /************** End of btreeInt.h ********************************************/
 48039 /************** Continuing where we left off in btmutex.c ********************/
 48040 #ifndef SQLITE_OMIT_SHARED_CACHE
 48041 #if SQLITE_THREADSAFE
 48043 /*
 48044 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
 48045 ** set BtShared.db to the database handle associated with p and the
 48046 ** p->locked boolean to true.
 48047 */
 48048 static void lockBtreeMutex(Btree *p){
 48049   assert( p->locked==0 );
 48050   assert( sqlite3_mutex_notheld(p->pBt->mutex) );
 48051   assert( sqlite3_mutex_held(p->db->mutex) );
 48053   sqlite3_mutex_enter(p->pBt->mutex);
 48054   p->pBt->db = p->db;
 48055   p->locked = 1;
 48058 /*
 48059 ** Release the BtShared mutex associated with B-Tree handle p and
 48060 ** clear the p->locked boolean.
 48061 */
 48062 static void unlockBtreeMutex(Btree *p){
 48063   BtShared *pBt = p->pBt;
 48064   assert( p->locked==1 );
 48065   assert( sqlite3_mutex_held(pBt->mutex) );
 48066   assert( sqlite3_mutex_held(p->db->mutex) );
 48067   assert( p->db==pBt->db );
 48069   sqlite3_mutex_leave(pBt->mutex);
 48070   p->locked = 0;
 48073 /*
 48074 ** Enter a mutex on the given BTree object.
 48075 **
 48076 ** If the object is not sharable, then no mutex is ever required
 48077 ** and this routine is a no-op.  The underlying mutex is non-recursive.
 48078 ** But we keep a reference count in Btree.wantToLock so the behavior
 48079 ** of this interface is recursive.
 48080 **
 48081 ** To avoid deadlocks, multiple Btrees are locked in the same order
 48082 ** by all database connections.  The p->pNext is a list of other
 48083 ** Btrees belonging to the same database connection as the p Btree
 48084 ** which need to be locked after p.  If we cannot get a lock on
 48085 ** p, then first unlock all of the others on p->pNext, then wait
 48086 ** for the lock to become available on p, then relock all of the
 48087 ** subsequent Btrees that desire a lock.
 48088 */
 48089 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
 48090   Btree *pLater;
 48092   /* Some basic sanity checking on the Btree.  The list of Btrees
 48093   ** connected by pNext and pPrev should be in sorted order by
 48094   ** Btree.pBt value. All elements of the list should belong to
 48095   ** the same connection. Only shared Btrees are on the list. */
 48096   assert( p->pNext==0 || p->pNext->pBt>p->pBt );
 48097   assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
 48098   assert( p->pNext==0 || p->pNext->db==p->db );
 48099   assert( p->pPrev==0 || p->pPrev->db==p->db );
 48100   assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
 48102   /* Check for locking consistency */
 48103   assert( !p->locked || p->wantToLock>0 );
 48104   assert( p->sharable || p->wantToLock==0 );
 48106   /* We should already hold a lock on the database connection */
 48107   assert( sqlite3_mutex_held(p->db->mutex) );
 48109   /* Unless the database is sharable and unlocked, then BtShared.db
 48110   ** should already be set correctly. */
 48111   assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
 48113   if( !p->sharable ) return;
 48114   p->wantToLock++;
 48115   if( p->locked ) return;
 48117   /* In most cases, we should be able to acquire the lock we
 48118   ** want without having to go throught the ascending lock
 48119   ** procedure that follows.  Just be sure not to block.
 48120   */
 48121   if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
 48122     p->pBt->db = p->db;
 48123     p->locked = 1;
 48124     return;
 48127   /* To avoid deadlock, first release all locks with a larger
 48128   ** BtShared address.  Then acquire our lock.  Then reacquire
 48129   ** the other BtShared locks that we used to hold in ascending
 48130   ** order.
 48131   */
 48132   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
 48133     assert( pLater->sharable );
 48134     assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
 48135     assert( !pLater->locked || pLater->wantToLock>0 );
 48136     if( pLater->locked ){
 48137       unlockBtreeMutex(pLater);
 48140   lockBtreeMutex(p);
 48141   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
 48142     if( pLater->wantToLock ){
 48143       lockBtreeMutex(pLater);
 48148 /*
 48149 ** Exit the recursive mutex on a Btree.
 48150 */
 48151 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
 48152   if( p->sharable ){
 48153     assert( p->wantToLock>0 );
 48154     p->wantToLock--;
 48155     if( p->wantToLock==0 ){
 48156       unlockBtreeMutex(p);
 48161 #ifndef NDEBUG
 48162 /*
 48163 ** Return true if the BtShared mutex is held on the btree, or if the
 48164 ** B-Tree is not marked as sharable.
 48165 **
 48166 ** This routine is used only from within assert() statements.
 48167 */
 48168 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
 48169   assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
 48170   assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
 48171   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
 48172   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
 48174   return (p->sharable==0 || p->locked);
 48176 #endif
 48179 #ifndef SQLITE_OMIT_INCRBLOB
 48180 /*
 48181 ** Enter and leave a mutex on a Btree given a cursor owned by that
 48182 ** Btree.  These entry points are used by incremental I/O and can be
 48183 ** omitted if that module is not used.
 48184 */
 48185 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
 48186   sqlite3BtreeEnter(pCur->pBtree);
 48188 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
 48189   sqlite3BtreeLeave(pCur->pBtree);
 48191 #endif /* SQLITE_OMIT_INCRBLOB */
 48194 /*
 48195 ** Enter the mutex on every Btree associated with a database
 48196 ** connection.  This is needed (for example) prior to parsing
 48197 ** a statement since we will be comparing table and column names
 48198 ** against all schemas and we do not want those schemas being
 48199 ** reset out from under us.
 48200 **
 48201 ** There is a corresponding leave-all procedures.
 48202 **
 48203 ** Enter the mutexes in accending order by BtShared pointer address
 48204 ** to avoid the possibility of deadlock when two threads with
 48205 ** two or more btrees in common both try to lock all their btrees
 48206 ** at the same instant.
 48207 */
 48208 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 48209   int i;
 48210   Btree *p;
 48211   assert( sqlite3_mutex_held(db->mutex) );
 48212   for(i=0; i<db->nDb; i++){
 48213     p = db->aDb[i].pBt;
 48214     if( p ) sqlite3BtreeEnter(p);
 48217 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
 48218   int i;
 48219   Btree *p;
 48220   assert( sqlite3_mutex_held(db->mutex) );
 48221   for(i=0; i<db->nDb; i++){
 48222     p = db->aDb[i].pBt;
 48223     if( p ) sqlite3BtreeLeave(p);
 48227 /*
 48228 ** Return true if a particular Btree requires a lock.  Return FALSE if
 48229 ** no lock is ever required since it is not sharable.
 48230 */
 48231 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
 48232   return p->sharable;
 48235 #ifndef NDEBUG
 48236 /*
 48237 ** Return true if the current thread holds the database connection
 48238 ** mutex and all required BtShared mutexes.
 48239 **
 48240 ** This routine is used inside assert() statements only.
 48241 */
 48242 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
 48243   int i;
 48244   if( !sqlite3_mutex_held(db->mutex) ){
 48245     return 0;
 48247   for(i=0; i<db->nDb; i++){
 48248     Btree *p;
 48249     p = db->aDb[i].pBt;
 48250     if( p && p->sharable &&
 48251          (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
 48252       return 0;
 48255   return 1;
 48257 #endif /* NDEBUG */
 48259 #ifndef NDEBUG
 48260 /*
 48261 ** Return true if the correct mutexes are held for accessing the
 48262 ** db->aDb[iDb].pSchema structure.  The mutexes required for schema
 48263 ** access are:
 48264 **
 48265 **   (1) The mutex on db
 48266 **   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
 48267 **
 48268 ** If pSchema is not NULL, then iDb is computed from pSchema and
 48269 ** db using sqlite3SchemaToIndex().
 48270 */
 48271 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
 48272   Btree *p;
 48273   assert( db!=0 );
 48274   if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
 48275   assert( iDb>=0 && iDb<db->nDb );
 48276   if( !sqlite3_mutex_held(db->mutex) ) return 0;
 48277   if( iDb==1 ) return 1;
 48278   p = db->aDb[iDb].pBt;
 48279   assert( p!=0 );
 48280   return p->sharable==0 || p->locked==1;
 48282 #endif /* NDEBUG */
 48284 #else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
 48285 /*
 48286 ** The following are special cases for mutex enter routines for use
 48287 ** in single threaded applications that use shared cache.  Except for
 48288 ** these two routines, all mutex operations are no-ops in that case and
 48289 ** are null #defines in btree.h.
 48290 **
 48291 ** If shared cache is disabled, then all btree mutex routines, including
 48292 ** the ones below, are no-ops and are null #defines in btree.h.
 48293 */
 48295 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
 48296   p->pBt->db = p->db;
 48298 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 48299   int i;
 48300   for(i=0; i<db->nDb; i++){
 48301     Btree *p = db->aDb[i].pBt;
 48302     if( p ){
 48303       p->pBt->db = p->db;
 48307 #endif /* if SQLITE_THREADSAFE */
 48308 #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
 48310 /************** End of btmutex.c *********************************************/
 48311 /************** Begin file btree.c *******************************************/
 48312 /*
 48313 ** 2004 April 6
 48314 **
 48315 ** The author disclaims copyright to this source code.  In place of
 48316 ** a legal notice, here is a blessing:
 48317 **
 48318 **    May you do good and not evil.
 48319 **    May you find forgiveness for yourself and forgive others.
 48320 **    May you share freely, never taking more than you give.
 48321 **
 48322 *************************************************************************
 48323 ** This file implements a external (disk-based) database using BTrees.
 48324 ** See the header comment on "btreeInt.h" for additional information.
 48325 ** Including a description of file format and an overview of operation.
 48326 */
 48328 /*
 48329 ** The header string that appears at the beginning of every
 48330 ** SQLite database.
 48331 */
 48332 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
 48334 /*
 48335 ** Set this global variable to 1 to enable tracing using the TRACE
 48336 ** macro.
 48337 */
 48338 #if 0
 48339 int sqlite3BtreeTrace=1;  /* True to enable tracing */
 48340 # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
 48341 #else
 48342 # define TRACE(X)
 48343 #endif
 48345 /*
 48346 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
 48347 ** But if the value is zero, make it 65536.
 48348 **
 48349 ** This routine is used to extract the "offset to cell content area" value
 48350 ** from the header of a btree page.  If the page size is 65536 and the page
 48351 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
 48352 ** This routine makes the necessary adjustment to 65536.
 48353 */
 48354 #define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
 48356 #ifndef SQLITE_OMIT_SHARED_CACHE
 48357 /*
 48358 ** A list of BtShared objects that are eligible for participation
 48359 ** in shared cache.  This variable has file scope during normal builds,
 48360 ** but the test harness needs to access it so we make it global for 
 48361 ** test builds.
 48362 **
 48363 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
 48364 */
 48365 #ifdef SQLITE_TEST
 48366 SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
 48367 #else
 48368 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
 48369 #endif
 48370 #endif /* SQLITE_OMIT_SHARED_CACHE */
 48372 #ifndef SQLITE_OMIT_SHARED_CACHE
 48373 /*
 48374 ** Enable or disable the shared pager and schema features.
 48375 **
 48376 ** This routine has no effect on existing database connections.
 48377 ** The shared cache setting effects only future calls to
 48378 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
 48379 */
 48380 SQLITE_API int sqlite3_enable_shared_cache(int enable){
 48381   sqlite3GlobalConfig.sharedCacheEnabled = enable;
 48382   return SQLITE_OK;
 48384 #endif
 48388 #ifdef SQLITE_OMIT_SHARED_CACHE
 48389   /*
 48390   ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
 48391   ** and clearAllSharedCacheTableLocks()
 48392   ** manipulate entries in the BtShared.pLock linked list used to store
 48393   ** shared-cache table level locks. If the library is compiled with the
 48394   ** shared-cache feature disabled, then there is only ever one user
 48395   ** of each BtShared structure and so this locking is not necessary. 
 48396   ** So define the lock related functions as no-ops.
 48397   */
 48398   #define querySharedCacheTableLock(a,b,c) SQLITE_OK
 48399   #define setSharedCacheTableLock(a,b,c) SQLITE_OK
 48400   #define clearAllSharedCacheTableLocks(a)
 48401   #define downgradeAllSharedCacheTableLocks(a)
 48402   #define hasSharedCacheTableLock(a,b,c,d) 1
 48403   #define hasReadConflicts(a, b) 0
 48404 #endif
 48406 #ifndef SQLITE_OMIT_SHARED_CACHE
 48408 #ifdef SQLITE_DEBUG
 48409 /*
 48410 **** This function is only used as part of an assert() statement. ***
 48411 **
 48412 ** Check to see if pBtree holds the required locks to read or write to the 
 48413 ** table with root page iRoot.   Return 1 if it does and 0 if not.
 48414 **
 48415 ** For example, when writing to a table with root-page iRoot via 
 48416 ** Btree connection pBtree:
 48417 **
 48418 **    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
 48419 **
 48420 ** When writing to an index that resides in a sharable database, the 
 48421 ** caller should have first obtained a lock specifying the root page of
 48422 ** the corresponding table. This makes things a bit more complicated,
 48423 ** as this module treats each table as a separate structure. To determine
 48424 ** the table corresponding to the index being written, this
 48425 ** function has to search through the database schema.
 48426 **
 48427 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
 48428 ** hold a write-lock on the schema table (root page 1). This is also
 48429 ** acceptable.
 48430 */
 48431 static int hasSharedCacheTableLock(
 48432   Btree *pBtree,         /* Handle that must hold lock */
 48433   Pgno iRoot,            /* Root page of b-tree */
 48434   int isIndex,           /* True if iRoot is the root of an index b-tree */
 48435   int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
 48436 ){
 48437   Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
 48438   Pgno iTab = 0;
 48439   BtLock *pLock;
 48441   /* If this database is not shareable, or if the client is reading
 48442   ** and has the read-uncommitted flag set, then no lock is required. 
 48443   ** Return true immediately.
 48444   */
 48445   if( (pBtree->sharable==0)
 48446    || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
 48447   ){
 48448     return 1;
 48451   /* If the client is reading  or writing an index and the schema is
 48452   ** not loaded, then it is too difficult to actually check to see if
 48453   ** the correct locks are held.  So do not bother - just return true.
 48454   ** This case does not come up very often anyhow.
 48455   */
 48456   if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
 48457     return 1;
 48460   /* Figure out the root-page that the lock should be held on. For table
 48461   ** b-trees, this is just the root page of the b-tree being read or
 48462   ** written. For index b-trees, it is the root page of the associated
 48463   ** table.  */
 48464   if( isIndex ){
 48465     HashElem *p;
 48466     for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
 48467       Index *pIdx = (Index *)sqliteHashData(p);
 48468       if( pIdx->tnum==(int)iRoot ){
 48469         iTab = pIdx->pTable->tnum;
 48472   }else{
 48473     iTab = iRoot;
 48476   /* Search for the required lock. Either a write-lock on root-page iTab, a 
 48477   ** write-lock on the schema table, or (if the client is reading) a
 48478   ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
 48479   for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
 48480     if( pLock->pBtree==pBtree 
 48481      && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
 48482      && pLock->eLock>=eLockType 
 48483     ){
 48484       return 1;
 48488   /* Failed to find the required lock. */
 48489   return 0;
 48491 #endif /* SQLITE_DEBUG */
 48493 #ifdef SQLITE_DEBUG
 48494 /*
 48495 **** This function may be used as part of assert() statements only. ****
 48496 **
 48497 ** Return true if it would be illegal for pBtree to write into the
 48498 ** table or index rooted at iRoot because other shared connections are
 48499 ** simultaneously reading that same table or index.
 48500 **
 48501 ** It is illegal for pBtree to write if some other Btree object that
 48502 ** shares the same BtShared object is currently reading or writing
 48503 ** the iRoot table.  Except, if the other Btree object has the
 48504 ** read-uncommitted flag set, then it is OK for the other object to
 48505 ** have a read cursor.
 48506 **
 48507 ** For example, before writing to any part of the table or index
 48508 ** rooted at page iRoot, one should call:
 48509 **
 48510 **    assert( !hasReadConflicts(pBtree, iRoot) );
 48511 */
 48512 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
 48513   BtCursor *p;
 48514   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 48515     if( p->pgnoRoot==iRoot 
 48516      && p->pBtree!=pBtree
 48517      && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
 48518     ){
 48519       return 1;
 48522   return 0;
 48524 #endif    /* #ifdef SQLITE_DEBUG */
 48526 /*
 48527 ** Query to see if Btree handle p may obtain a lock of type eLock 
 48528 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
 48529 ** SQLITE_OK if the lock may be obtained (by calling
 48530 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
 48531 */
 48532 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
 48533   BtShared *pBt = p->pBt;
 48534   BtLock *pIter;
 48536   assert( sqlite3BtreeHoldsMutex(p) );
 48537   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
 48538   assert( p->db!=0 );
 48539   assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
 48541   /* If requesting a write-lock, then the Btree must have an open write
 48542   ** transaction on this file. And, obviously, for this to be so there 
 48543   ** must be an open write transaction on the file itself.
 48544   */
 48545   assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
 48546   assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
 48548   /* This routine is a no-op if the shared-cache is not enabled */
 48549   if( !p->sharable ){
 48550     return SQLITE_OK;
 48553   /* If some other connection is holding an exclusive lock, the
 48554   ** requested lock may not be obtained.
 48555   */
 48556   if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
 48557     sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
 48558     return SQLITE_LOCKED_SHAREDCACHE;
 48561   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 48562     /* The condition (pIter->eLock!=eLock) in the following if(...) 
 48563     ** statement is a simplification of:
 48564     **
 48565     **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
 48566     **
 48567     ** since we know that if eLock==WRITE_LOCK, then no other connection
 48568     ** may hold a WRITE_LOCK on any table in this file (since there can
 48569     ** only be a single writer).
 48570     */
 48571     assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
 48572     assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
 48573     if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
 48574       sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
 48575       if( eLock==WRITE_LOCK ){
 48576         assert( p==pBt->pWriter );
 48577         pBt->btsFlags |= BTS_PENDING;
 48579       return SQLITE_LOCKED_SHAREDCACHE;
 48582   return SQLITE_OK;
 48584 #endif /* !SQLITE_OMIT_SHARED_CACHE */
 48586 #ifndef SQLITE_OMIT_SHARED_CACHE
 48587 /*
 48588 ** Add a lock on the table with root-page iTable to the shared-btree used
 48589 ** by Btree handle p. Parameter eLock must be either READ_LOCK or 
 48590 ** WRITE_LOCK.
 48591 **
 48592 ** This function assumes the following:
 48593 **
 48594 **   (a) The specified Btree object p is connected to a sharable
 48595 **       database (one with the BtShared.sharable flag set), and
 48596 **
 48597 **   (b) No other Btree objects hold a lock that conflicts
 48598 **       with the requested lock (i.e. querySharedCacheTableLock() has
 48599 **       already been called and returned SQLITE_OK).
 48600 **
 48601 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 
 48602 ** is returned if a malloc attempt fails.
 48603 */
 48604 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
 48605   BtShared *pBt = p->pBt;
 48606   BtLock *pLock = 0;
 48607   BtLock *pIter;
 48609   assert( sqlite3BtreeHoldsMutex(p) );
 48610   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
 48611   assert( p->db!=0 );
 48613   /* A connection with the read-uncommitted flag set will never try to
 48614   ** obtain a read-lock using this function. The only read-lock obtained
 48615   ** by a connection in read-uncommitted mode is on the sqlite_master 
 48616   ** table, and that lock is obtained in BtreeBeginTrans().  */
 48617   assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
 48619   /* This function should only be called on a sharable b-tree after it 
 48620   ** has been determined that no other b-tree holds a conflicting lock.  */
 48621   assert( p->sharable );
 48622   assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
 48624   /* First search the list for an existing lock on this table. */
 48625   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 48626     if( pIter->iTable==iTable && pIter->pBtree==p ){
 48627       pLock = pIter;
 48628       break;
 48632   /* If the above search did not find a BtLock struct associating Btree p
 48633   ** with table iTable, allocate one and link it into the list.
 48634   */
 48635   if( !pLock ){
 48636     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
 48637     if( !pLock ){
 48638       return SQLITE_NOMEM;
 48640     pLock->iTable = iTable;
 48641     pLock->pBtree = p;
 48642     pLock->pNext = pBt->pLock;
 48643     pBt->pLock = pLock;
 48646   /* Set the BtLock.eLock variable to the maximum of the current lock
 48647   ** and the requested lock. This means if a write-lock was already held
 48648   ** and a read-lock requested, we don't incorrectly downgrade the lock.
 48649   */
 48650   assert( WRITE_LOCK>READ_LOCK );
 48651   if( eLock>pLock->eLock ){
 48652     pLock->eLock = eLock;
 48655   return SQLITE_OK;
 48657 #endif /* !SQLITE_OMIT_SHARED_CACHE */
 48659 #ifndef SQLITE_OMIT_SHARED_CACHE
 48660 /*
 48661 ** Release all the table locks (locks obtained via calls to
 48662 ** the setSharedCacheTableLock() procedure) held by Btree object p.
 48663 **
 48664 ** This function assumes that Btree p has an open read or write 
 48665 ** transaction. If it does not, then the BTS_PENDING flag
 48666 ** may be incorrectly cleared.
 48667 */
 48668 static void clearAllSharedCacheTableLocks(Btree *p){
 48669   BtShared *pBt = p->pBt;
 48670   BtLock **ppIter = &pBt->pLock;
 48672   assert( sqlite3BtreeHoldsMutex(p) );
 48673   assert( p->sharable || 0==*ppIter );
 48674   assert( p->inTrans>0 );
 48676   while( *ppIter ){
 48677     BtLock *pLock = *ppIter;
 48678     assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
 48679     assert( pLock->pBtree->inTrans>=pLock->eLock );
 48680     if( pLock->pBtree==p ){
 48681       *ppIter = pLock->pNext;
 48682       assert( pLock->iTable!=1 || pLock==&p->lock );
 48683       if( pLock->iTable!=1 ){
 48684         sqlite3_free(pLock);
 48686     }else{
 48687       ppIter = &pLock->pNext;
 48691   assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
 48692   if( pBt->pWriter==p ){
 48693     pBt->pWriter = 0;
 48694     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 48695   }else if( pBt->nTransaction==2 ){
 48696     /* This function is called when Btree p is concluding its 
 48697     ** transaction. If there currently exists a writer, and p is not
 48698     ** that writer, then the number of locks held by connections other
 48699     ** than the writer must be about to drop to zero. In this case
 48700     ** set the BTS_PENDING flag to 0.
 48701     **
 48702     ** If there is not currently a writer, then BTS_PENDING must
 48703     ** be zero already. So this next line is harmless in that case.
 48704     */
 48705     pBt->btsFlags &= ~BTS_PENDING;
 48709 /*
 48710 ** This function changes all write-locks held by Btree p into read-locks.
 48711 */
 48712 static void downgradeAllSharedCacheTableLocks(Btree *p){
 48713   BtShared *pBt = p->pBt;
 48714   if( pBt->pWriter==p ){
 48715     BtLock *pLock;
 48716     pBt->pWriter = 0;
 48717     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 48718     for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
 48719       assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
 48720       pLock->eLock = READ_LOCK;
 48725 #endif /* SQLITE_OMIT_SHARED_CACHE */
 48727 static void releasePage(MemPage *pPage);  /* Forward reference */
 48729 /*
 48730 ***** This routine is used inside of assert() only ****
 48731 **
 48732 ** Verify that the cursor holds the mutex on its BtShared
 48733 */
 48734 #ifdef SQLITE_DEBUG
 48735 static int cursorHoldsMutex(BtCursor *p){
 48736   return sqlite3_mutex_held(p->pBt->mutex);
 48738 #endif
 48741 #ifndef SQLITE_OMIT_INCRBLOB
 48742 /*
 48743 ** Invalidate the overflow page-list cache for cursor pCur, if any.
 48744 */
 48745 static void invalidateOverflowCache(BtCursor *pCur){
 48746   assert( cursorHoldsMutex(pCur) );
 48747   sqlite3_free(pCur->aOverflow);
 48748   pCur->aOverflow = 0;
 48751 /*
 48752 ** Invalidate the overflow page-list cache for all cursors opened
 48753 ** on the shared btree structure pBt.
 48754 */
 48755 static void invalidateAllOverflowCache(BtShared *pBt){
 48756   BtCursor *p;
 48757   assert( sqlite3_mutex_held(pBt->mutex) );
 48758   for(p=pBt->pCursor; p; p=p->pNext){
 48759     invalidateOverflowCache(p);
 48763 /*
 48764 ** This function is called before modifying the contents of a table
 48765 ** to invalidate any incrblob cursors that are open on the
 48766 ** row or one of the rows being modified.
 48767 **
 48768 ** If argument isClearTable is true, then the entire contents of the
 48769 ** table is about to be deleted. In this case invalidate all incrblob
 48770 ** cursors open on any row within the table with root-page pgnoRoot.
 48771 **
 48772 ** Otherwise, if argument isClearTable is false, then the row with
 48773 ** rowid iRow is being replaced or deleted. In this case invalidate
 48774 ** only those incrblob cursors open on that specific row.
 48775 */
 48776 static void invalidateIncrblobCursors(
 48777   Btree *pBtree,          /* The database file to check */
 48778   i64 iRow,               /* The rowid that might be changing */
 48779   int isClearTable        /* True if all rows are being deleted */
 48780 ){
 48781   BtCursor *p;
 48782   BtShared *pBt = pBtree->pBt;
 48783   assert( sqlite3BtreeHoldsMutex(pBtree) );
 48784   for(p=pBt->pCursor; p; p=p->pNext){
 48785     if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
 48786       p->eState = CURSOR_INVALID;
 48791 #else
 48792   /* Stub functions when INCRBLOB is omitted */
 48793   #define invalidateOverflowCache(x)
 48794   #define invalidateAllOverflowCache(x)
 48795   #define invalidateIncrblobCursors(x,y,z)
 48796 #endif /* SQLITE_OMIT_INCRBLOB */
 48798 /*
 48799 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
 48800 ** when a page that previously contained data becomes a free-list leaf 
 48801 ** page.
 48802 **
 48803 ** The BtShared.pHasContent bitvec exists to work around an obscure
 48804 ** bug caused by the interaction of two useful IO optimizations surrounding
 48805 ** free-list leaf pages:
 48806 **
 48807 **   1) When all data is deleted from a page and the page becomes
 48808 **      a free-list leaf page, the page is not written to the database
 48809 **      (as free-list leaf pages contain no meaningful data). Sometimes
 48810 **      such a page is not even journalled (as it will not be modified,
 48811 **      why bother journalling it?).
 48812 **
 48813 **   2) When a free-list leaf page is reused, its content is not read
 48814 **      from the database or written to the journal file (why should it
 48815 **      be, if it is not at all meaningful?).
 48816 **
 48817 ** By themselves, these optimizations work fine and provide a handy
 48818 ** performance boost to bulk delete or insert operations. However, if
 48819 ** a page is moved to the free-list and then reused within the same
 48820 ** transaction, a problem comes up. If the page is not journalled when
 48821 ** it is moved to the free-list and it is also not journalled when it
 48822 ** is extracted from the free-list and reused, then the original data
 48823 ** may be lost. In the event of a rollback, it may not be possible
 48824 ** to restore the database to its original configuration.
 48825 **
 48826 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is 
 48827 ** moved to become a free-list leaf page, the corresponding bit is
 48828 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
 48829 ** optimization 2 above is omitted if the corresponding bit is already
 48830 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
 48831 ** at the end of every transaction.
 48832 */
 48833 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
 48834   int rc = SQLITE_OK;
 48835   if( !pBt->pHasContent ){
 48836     assert( pgno<=pBt->nPage );
 48837     pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
 48838     if( !pBt->pHasContent ){
 48839       rc = SQLITE_NOMEM;
 48842   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
 48843     rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
 48845   return rc;
 48848 /*
 48849 ** Query the BtShared.pHasContent vector.
 48850 **
 48851 ** This function is called when a free-list leaf page is removed from the
 48852 ** free-list for reuse. It returns false if it is safe to retrieve the
 48853 ** page from the pager layer with the 'no-content' flag set. True otherwise.
 48854 */
 48855 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
 48856   Bitvec *p = pBt->pHasContent;
 48857   return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
 48860 /*
 48861 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
 48862 ** invoked at the conclusion of each write-transaction.
 48863 */
 48864 static void btreeClearHasContent(BtShared *pBt){
 48865   sqlite3BitvecDestroy(pBt->pHasContent);
 48866   pBt->pHasContent = 0;
 48869 /*
 48870 ** Save the current cursor position in the variables BtCursor.nKey 
 48871 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
 48872 **
 48873 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
 48874 ** prior to calling this routine.  
 48875 */
 48876 static int saveCursorPosition(BtCursor *pCur){
 48877   int rc;
 48879   assert( CURSOR_VALID==pCur->eState );
 48880   assert( 0==pCur->pKey );
 48881   assert( cursorHoldsMutex(pCur) );
 48883   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
 48884   assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
 48886   /* If this is an intKey table, then the above call to BtreeKeySize()
 48887   ** stores the integer key in pCur->nKey. In this case this value is
 48888   ** all that is required. Otherwise, if pCur is not open on an intKey
 48889   ** table, then malloc space for and store the pCur->nKey bytes of key 
 48890   ** data.
 48891   */
 48892   if( 0==pCur->apPage[0]->intKey ){
 48893     void *pKey = sqlite3Malloc( (int)pCur->nKey );
 48894     if( pKey ){
 48895       rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
 48896       if( rc==SQLITE_OK ){
 48897         pCur->pKey = pKey;
 48898       }else{
 48899         sqlite3_free(pKey);
 48901     }else{
 48902       rc = SQLITE_NOMEM;
 48905   assert( !pCur->apPage[0]->intKey || !pCur->pKey );
 48907   if( rc==SQLITE_OK ){
 48908     int i;
 48909     for(i=0; i<=pCur->iPage; i++){
 48910       releasePage(pCur->apPage[i]);
 48911       pCur->apPage[i] = 0;
 48913     pCur->iPage = -1;
 48914     pCur->eState = CURSOR_REQUIRESEEK;
 48917   invalidateOverflowCache(pCur);
 48918   return rc;
 48921 /*
 48922 ** Save the positions of all cursors (except pExcept) that are open on
 48923 ** the table  with root-page iRoot. Usually, this is called just before cursor
 48924 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
 48925 */
 48926 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
 48927   BtCursor *p;
 48928   assert( sqlite3_mutex_held(pBt->mutex) );
 48929   assert( pExcept==0 || pExcept->pBt==pBt );
 48930   for(p=pBt->pCursor; p; p=p->pNext){
 48931     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && 
 48932         p->eState==CURSOR_VALID ){
 48933       int rc = saveCursorPosition(p);
 48934       if( SQLITE_OK!=rc ){
 48935         return rc;
 48939   return SQLITE_OK;
 48942 /*
 48943 ** Clear the current cursor position.
 48944 */
 48945 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
 48946   assert( cursorHoldsMutex(pCur) );
 48947   sqlite3_free(pCur->pKey);
 48948   pCur->pKey = 0;
 48949   pCur->eState = CURSOR_INVALID;
 48952 /*
 48953 ** In this version of BtreeMoveto, pKey is a packed index record
 48954 ** such as is generated by the OP_MakeRecord opcode.  Unpack the
 48955 ** record and then call BtreeMovetoUnpacked() to do the work.
 48956 */
 48957 static int btreeMoveto(
 48958   BtCursor *pCur,     /* Cursor open on the btree to be searched */
 48959   const void *pKey,   /* Packed key if the btree is an index */
 48960   i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
 48961   int bias,           /* Bias search to the high end */
 48962   int *pRes           /* Write search results here */
 48963 ){
 48964   int rc;                    /* Status code */
 48965   UnpackedRecord *pIdxKey;   /* Unpacked index key */
 48966   char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
 48967   char *pFree = 0;
 48969   if( pKey ){
 48970     assert( nKey==(i64)(int)nKey );
 48971     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 48972         pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
 48973     );
 48974     if( pIdxKey==0 ) return SQLITE_NOMEM;
 48975     sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
 48976   }else{
 48977     pIdxKey = 0;
 48979   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
 48980   if( pFree ){
 48981     sqlite3DbFree(pCur->pKeyInfo->db, pFree);
 48983   return rc;
 48986 /*
 48987 ** Restore the cursor to the position it was in (or as close to as possible)
 48988 ** when saveCursorPosition() was called. Note that this call deletes the 
 48989 ** saved position info stored by saveCursorPosition(), so there can be
 48990 ** at most one effective restoreCursorPosition() call after each 
 48991 ** saveCursorPosition().
 48992 */
 48993 static int btreeRestoreCursorPosition(BtCursor *pCur){
 48994   int rc;
 48995   assert( cursorHoldsMutex(pCur) );
 48996   assert( pCur->eState>=CURSOR_REQUIRESEEK );
 48997   if( pCur->eState==CURSOR_FAULT ){
 48998     return pCur->skipNext;
 49000   pCur->eState = CURSOR_INVALID;
 49001   rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
 49002   if( rc==SQLITE_OK ){
 49003     sqlite3_free(pCur->pKey);
 49004     pCur->pKey = 0;
 49005     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
 49007   return rc;
 49010 #define restoreCursorPosition(p) \
 49011   (p->eState>=CURSOR_REQUIRESEEK ? \
 49012          btreeRestoreCursorPosition(p) : \
 49013          SQLITE_OK)
 49015 /*
 49016 ** Determine whether or not a cursor has moved from the position it
 49017 ** was last placed at.  Cursors can move when the row they are pointing
 49018 ** at is deleted out from under them.
 49019 **
 49020 ** This routine returns an error code if something goes wrong.  The
 49021 ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
 49022 */
 49023 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
 49024   int rc;
 49026   rc = restoreCursorPosition(pCur);
 49027   if( rc ){
 49028     *pHasMoved = 1;
 49029     return rc;
 49031   if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
 49032     *pHasMoved = 1;
 49033   }else{
 49034     *pHasMoved = 0;
 49036   return SQLITE_OK;
 49039 #ifndef SQLITE_OMIT_AUTOVACUUM
 49040 /*
 49041 ** Given a page number of a regular database page, return the page
 49042 ** number for the pointer-map page that contains the entry for the
 49043 ** input page number.
 49044 **
 49045 ** Return 0 (not a valid page) for pgno==1 since there is
 49046 ** no pointer map associated with page 1.  The integrity_check logic
 49047 ** requires that ptrmapPageno(*,1)!=1.
 49048 */
 49049 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
 49050   int nPagesPerMapPage;
 49051   Pgno iPtrMap, ret;
 49052   assert( sqlite3_mutex_held(pBt->mutex) );
 49053   if( pgno<2 ) return 0;
 49054   nPagesPerMapPage = (pBt->usableSize/5)+1;
 49055   iPtrMap = (pgno-2)/nPagesPerMapPage;
 49056   ret = (iPtrMap*nPagesPerMapPage) + 2; 
 49057   if( ret==PENDING_BYTE_PAGE(pBt) ){
 49058     ret++;
 49060   return ret;
 49063 /*
 49064 ** Write an entry into the pointer map.
 49065 **
 49066 ** This routine updates the pointer map entry for page number 'key'
 49067 ** so that it maps to type 'eType' and parent page number 'pgno'.
 49068 **
 49069 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
 49070 ** a no-op.  If an error occurs, the appropriate error code is written
 49071 ** into *pRC.
 49072 */
 49073 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
 49074   DbPage *pDbPage;  /* The pointer map page */
 49075   u8 *pPtrmap;      /* The pointer map data */
 49076   Pgno iPtrmap;     /* The pointer map page number */
 49077   int offset;       /* Offset in pointer map page */
 49078   int rc;           /* Return code from subfunctions */
 49080   if( *pRC ) return;
 49082   assert( sqlite3_mutex_held(pBt->mutex) );
 49083   /* The master-journal page number must never be used as a pointer map page */
 49084   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
 49086   assert( pBt->autoVacuum );
 49087   if( key==0 ){
 49088     *pRC = SQLITE_CORRUPT_BKPT;
 49089     return;
 49091   iPtrmap = PTRMAP_PAGENO(pBt, key);
 49092   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
 49093   if( rc!=SQLITE_OK ){
 49094     *pRC = rc;
 49095     return;
 49097   offset = PTRMAP_PTROFFSET(iPtrmap, key);
 49098   if( offset<0 ){
 49099     *pRC = SQLITE_CORRUPT_BKPT;
 49100     goto ptrmap_exit;
 49102   assert( offset <= (int)pBt->usableSize-5 );
 49103   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
 49105   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
 49106     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
 49107     *pRC= rc = sqlite3PagerWrite(pDbPage);
 49108     if( rc==SQLITE_OK ){
 49109       pPtrmap[offset] = eType;
 49110       put4byte(&pPtrmap[offset+1], parent);
 49114 ptrmap_exit:
 49115   sqlite3PagerUnref(pDbPage);
 49118 /*
 49119 ** Read an entry from the pointer map.
 49120 **
 49121 ** This routine retrieves the pointer map entry for page 'key', writing
 49122 ** the type and parent page number to *pEType and *pPgno respectively.
 49123 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
 49124 */
 49125 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
 49126   DbPage *pDbPage;   /* The pointer map page */
 49127   int iPtrmap;       /* Pointer map page index */
 49128   u8 *pPtrmap;       /* Pointer map page data */
 49129   int offset;        /* Offset of entry in pointer map */
 49130   int rc;
 49132   assert( sqlite3_mutex_held(pBt->mutex) );
 49134   iPtrmap = PTRMAP_PAGENO(pBt, key);
 49135   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
 49136   if( rc!=0 ){
 49137     return rc;
 49139   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
 49141   offset = PTRMAP_PTROFFSET(iPtrmap, key);
 49142   if( offset<0 ){
 49143     sqlite3PagerUnref(pDbPage);
 49144     return SQLITE_CORRUPT_BKPT;
 49146   assert( offset <= (int)pBt->usableSize-5 );
 49147   assert( pEType!=0 );
 49148   *pEType = pPtrmap[offset];
 49149   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
 49151   sqlite3PagerUnref(pDbPage);
 49152   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
 49153   return SQLITE_OK;
 49156 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
 49157   #define ptrmapPut(w,x,y,z,rc)
 49158   #define ptrmapGet(w,x,y,z) SQLITE_OK
 49159   #define ptrmapPutOvflPtr(x, y, rc)
 49160 #endif
 49162 /*
 49163 ** Given a btree page and a cell index (0 means the first cell on
 49164 ** the page, 1 means the second cell, and so forth) return a pointer
 49165 ** to the cell content.
 49166 **
 49167 ** This routine works only for pages that do not contain overflow cells.
 49168 */
 49169 #define findCell(P,I) \
 49170   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
 49171 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
 49174 /*
 49175 ** This a more complex version of findCell() that works for
 49176 ** pages that do contain overflow cells.
 49177 */
 49178 static u8 *findOverflowCell(MemPage *pPage, int iCell){
 49179   int i;
 49180   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49181   for(i=pPage->nOverflow-1; i>=0; i--){
 49182     int k;
 49183     k = pPage->aiOvfl[i];
 49184     if( k<=iCell ){
 49185       if( k==iCell ){
 49186         return pPage->apOvfl[i];
 49188       iCell--;
 49191   return findCell(pPage, iCell);
 49194 /*
 49195 ** Parse a cell content block and fill in the CellInfo structure.  There
 49196 ** are two versions of this function.  btreeParseCell() takes a 
 49197 ** cell index as the second argument and btreeParseCellPtr() 
 49198 ** takes a pointer to the body of the cell as its second argument.
 49199 **
 49200 ** Within this file, the parseCell() macro can be called instead of
 49201 ** btreeParseCellPtr(). Using some compilers, this will be faster.
 49202 */
 49203 static void btreeParseCellPtr(
 49204   MemPage *pPage,         /* Page containing the cell */
 49205   u8 *pCell,              /* Pointer to the cell text. */
 49206   CellInfo *pInfo         /* Fill in this structure */
 49207 ){
 49208   u16 n;                  /* Number bytes in cell content header */
 49209   u32 nPayload;           /* Number of bytes of cell payload */
 49211   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49213   pInfo->pCell = pCell;
 49214   assert( pPage->leaf==0 || pPage->leaf==1 );
 49215   n = pPage->childPtrSize;
 49216   assert( n==4-4*pPage->leaf );
 49217   if( pPage->intKey ){
 49218     if( pPage->hasData ){
 49219       n += getVarint32(&pCell[n], nPayload);
 49220     }else{
 49221       nPayload = 0;
 49223     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
 49224     pInfo->nData = nPayload;
 49225   }else{
 49226     pInfo->nData = 0;
 49227     n += getVarint32(&pCell[n], nPayload);
 49228     pInfo->nKey = nPayload;
 49230   pInfo->nPayload = nPayload;
 49231   pInfo->nHeader = n;
 49232   testcase( nPayload==pPage->maxLocal );
 49233   testcase( nPayload==pPage->maxLocal+1 );
 49234   if( likely(nPayload<=pPage->maxLocal) ){
 49235     /* This is the (easy) common case where the entire payload fits
 49236     ** on the local page.  No overflow is required.
 49237     */
 49238     if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
 49239     pInfo->nLocal = (u16)nPayload;
 49240     pInfo->iOverflow = 0;
 49241   }else{
 49242     /* If the payload will not fit completely on the local page, we have
 49243     ** to decide how much to store locally and how much to spill onto
 49244     ** overflow pages.  The strategy is to minimize the amount of unused
 49245     ** space on overflow pages while keeping the amount of local storage
 49246     ** in between minLocal and maxLocal.
 49247     **
 49248     ** Warning:  changing the way overflow payload is distributed in any
 49249     ** way will result in an incompatible file format.
 49250     */
 49251     int minLocal;  /* Minimum amount of payload held locally */
 49252     int maxLocal;  /* Maximum amount of payload held locally */
 49253     int surplus;   /* Overflow payload available for local storage */
 49255     minLocal = pPage->minLocal;
 49256     maxLocal = pPage->maxLocal;
 49257     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
 49258     testcase( surplus==maxLocal );
 49259     testcase( surplus==maxLocal+1 );
 49260     if( surplus <= maxLocal ){
 49261       pInfo->nLocal = (u16)surplus;
 49262     }else{
 49263       pInfo->nLocal = (u16)minLocal;
 49265     pInfo->iOverflow = (u16)(pInfo->nLocal + n);
 49266     pInfo->nSize = pInfo->iOverflow + 4;
 49269 #define parseCell(pPage, iCell, pInfo) \
 49270   btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
 49271 static void btreeParseCell(
 49272   MemPage *pPage,         /* Page containing the cell */
 49273   int iCell,              /* The cell index.  First cell is 0 */
 49274   CellInfo *pInfo         /* Fill in this structure */
 49275 ){
 49276   parseCell(pPage, iCell, pInfo);
 49279 /*
 49280 ** Compute the total number of bytes that a Cell needs in the cell
 49281 ** data area of the btree-page.  The return number includes the cell
 49282 ** data header and the local payload, but not any overflow page or
 49283 ** the space used by the cell pointer.
 49284 */
 49285 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
 49286   u8 *pIter = &pCell[pPage->childPtrSize];
 49287   u32 nSize;
 49289 #ifdef SQLITE_DEBUG
 49290   /* The value returned by this function should always be the same as
 49291   ** the (CellInfo.nSize) value found by doing a full parse of the
 49292   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
 49293   ** this function verifies that this invariant is not violated. */
 49294   CellInfo debuginfo;
 49295   btreeParseCellPtr(pPage, pCell, &debuginfo);
 49296 #endif
 49298   if( pPage->intKey ){
 49299     u8 *pEnd;
 49300     if( pPage->hasData ){
 49301       pIter += getVarint32(pIter, nSize);
 49302     }else{
 49303       nSize = 0;
 49306     /* pIter now points at the 64-bit integer key value, a variable length 
 49307     ** integer. The following block moves pIter to point at the first byte
 49308     ** past the end of the key value. */
 49309     pEnd = &pIter[9];
 49310     while( (*pIter++)&0x80 && pIter<pEnd );
 49311   }else{
 49312     pIter += getVarint32(pIter, nSize);
 49315   testcase( nSize==pPage->maxLocal );
 49316   testcase( nSize==pPage->maxLocal+1 );
 49317   if( nSize>pPage->maxLocal ){
 49318     int minLocal = pPage->minLocal;
 49319     nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
 49320     testcase( nSize==pPage->maxLocal );
 49321     testcase( nSize==pPage->maxLocal+1 );
 49322     if( nSize>pPage->maxLocal ){
 49323       nSize = minLocal;
 49325     nSize += 4;
 49327   nSize += (u32)(pIter - pCell);
 49329   /* The minimum size of any cell is 4 bytes. */
 49330   if( nSize<4 ){
 49331     nSize = 4;
 49334   assert( nSize==debuginfo.nSize );
 49335   return (u16)nSize;
 49338 #ifdef SQLITE_DEBUG
 49339 /* This variation on cellSizePtr() is used inside of assert() statements
 49340 ** only. */
 49341 static u16 cellSize(MemPage *pPage, int iCell){
 49342   return cellSizePtr(pPage, findCell(pPage, iCell));
 49344 #endif
 49346 #ifndef SQLITE_OMIT_AUTOVACUUM
 49347 /*
 49348 ** If the cell pCell, part of page pPage contains a pointer
 49349 ** to an overflow page, insert an entry into the pointer-map
 49350 ** for the overflow page.
 49351 */
 49352 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
 49353   CellInfo info;
 49354   if( *pRC ) return;
 49355   assert( pCell!=0 );
 49356   btreeParseCellPtr(pPage, pCell, &info);
 49357   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
 49358   if( info.iOverflow ){
 49359     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
 49360     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
 49363 #endif
 49366 /*
 49367 ** Defragment the page given.  All Cells are moved to the
 49368 ** end of the page and all free space is collected into one
 49369 ** big FreeBlk that occurs in between the header and cell
 49370 ** pointer array and the cell content area.
 49371 */
 49372 static int defragmentPage(MemPage *pPage){
 49373   int i;                     /* Loop counter */
 49374   int pc;                    /* Address of a i-th cell */
 49375   int hdr;                   /* Offset to the page header */
 49376   int size;                  /* Size of a cell */
 49377   int usableSize;            /* Number of usable bytes on a page */
 49378   int cellOffset;            /* Offset to the cell pointer array */
 49379   int cbrk;                  /* Offset to the cell content area */
 49380   int nCell;                 /* Number of cells on the page */
 49381   unsigned char *data;       /* The page data */
 49382   unsigned char *temp;       /* Temp area for cell content */
 49383   int iCellFirst;            /* First allowable cell index */
 49384   int iCellLast;             /* Last possible cell index */
 49387   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 49388   assert( pPage->pBt!=0 );
 49389   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
 49390   assert( pPage->nOverflow==0 );
 49391   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49392   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
 49393   data = pPage->aData;
 49394   hdr = pPage->hdrOffset;
 49395   cellOffset = pPage->cellOffset;
 49396   nCell = pPage->nCell;
 49397   assert( nCell==get2byte(&data[hdr+3]) );
 49398   usableSize = pPage->pBt->usableSize;
 49399   cbrk = get2byte(&data[hdr+5]);
 49400   memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
 49401   cbrk = usableSize;
 49402   iCellFirst = cellOffset + 2*nCell;
 49403   iCellLast = usableSize - 4;
 49404   for(i=0; i<nCell; i++){
 49405     u8 *pAddr;     /* The i-th cell pointer */
 49406     pAddr = &data[cellOffset + i*2];
 49407     pc = get2byte(pAddr);
 49408     testcase( pc==iCellFirst );
 49409     testcase( pc==iCellLast );
 49410 #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 49411     /* These conditions have already been verified in btreeInitPage()
 49412     ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
 49413     */
 49414     if( pc<iCellFirst || pc>iCellLast ){
 49415       return SQLITE_CORRUPT_BKPT;
 49417 #endif
 49418     assert( pc>=iCellFirst && pc<=iCellLast );
 49419     size = cellSizePtr(pPage, &temp[pc]);
 49420     cbrk -= size;
 49421 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 49422     if( cbrk<iCellFirst ){
 49423       return SQLITE_CORRUPT_BKPT;
 49425 #else
 49426     if( cbrk<iCellFirst || pc+size>usableSize ){
 49427       return SQLITE_CORRUPT_BKPT;
 49429 #endif
 49430     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
 49431     testcase( cbrk+size==usableSize );
 49432     testcase( pc+size==usableSize );
 49433     memcpy(&data[cbrk], &temp[pc], size);
 49434     put2byte(pAddr, cbrk);
 49436   assert( cbrk>=iCellFirst );
 49437   put2byte(&data[hdr+5], cbrk);
 49438   data[hdr+1] = 0;
 49439   data[hdr+2] = 0;
 49440   data[hdr+7] = 0;
 49441   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
 49442   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 49443   if( cbrk-iCellFirst!=pPage->nFree ){
 49444     return SQLITE_CORRUPT_BKPT;
 49446   return SQLITE_OK;
 49449 /*
 49450 ** Allocate nByte bytes of space from within the B-Tree page passed
 49451 ** as the first argument. Write into *pIdx the index into pPage->aData[]
 49452 ** of the first byte of allocated space. Return either SQLITE_OK or
 49453 ** an error code (usually SQLITE_CORRUPT).
 49454 **
 49455 ** The caller guarantees that there is sufficient space to make the
 49456 ** allocation.  This routine might need to defragment in order to bring
 49457 ** all the space together, however.  This routine will avoid using
 49458 ** the first two bytes past the cell pointer area since presumably this
 49459 ** allocation is being made in order to insert a new cell, so we will
 49460 ** also end up needing a new cell pointer.
 49461 */
 49462 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
 49463   const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
 49464   u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
 49465   int nFrag;                           /* Number of fragmented bytes on pPage */
 49466   int top;                             /* First byte of cell content area */
 49467   int gap;        /* First byte of gap between cell pointers and cell content */
 49468   int rc;         /* Integer return code */
 49469   int usableSize; /* Usable size of the page */
 49471   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 49472   assert( pPage->pBt );
 49473   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49474   assert( nByte>=0 );  /* Minimum cell size is 4 */
 49475   assert( pPage->nFree>=nByte );
 49476   assert( pPage->nOverflow==0 );
 49477   usableSize = pPage->pBt->usableSize;
 49478   assert( nByte < usableSize-8 );
 49480   nFrag = data[hdr+7];
 49481   assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
 49482   gap = pPage->cellOffset + 2*pPage->nCell;
 49483   top = get2byteNotZero(&data[hdr+5]);
 49484   if( gap>top ) return SQLITE_CORRUPT_BKPT;
 49485   testcase( gap+2==top );
 49486   testcase( gap+1==top );
 49487   testcase( gap==top );
 49489   if( nFrag>=60 ){
 49490     /* Always defragment highly fragmented pages */
 49491     rc = defragmentPage(pPage);
 49492     if( rc ) return rc;
 49493     top = get2byteNotZero(&data[hdr+5]);
 49494   }else if( gap+2<=top ){
 49495     /* Search the freelist looking for a free slot big enough to satisfy 
 49496     ** the request. The allocation is made from the first free slot in 
 49497     ** the list that is large enough to accomadate it.
 49498     */
 49499     int pc, addr;
 49500     for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
 49501       int size;            /* Size of the free slot */
 49502       if( pc>usableSize-4 || pc<addr+4 ){
 49503         return SQLITE_CORRUPT_BKPT;
 49505       size = get2byte(&data[pc+2]);
 49506       if( size>=nByte ){
 49507         int x = size - nByte;
 49508         testcase( x==4 );
 49509         testcase( x==3 );
 49510         if( x<4 ){
 49511           /* Remove the slot from the free-list. Update the number of
 49512           ** fragmented bytes within the page. */
 49513           memcpy(&data[addr], &data[pc], 2);
 49514           data[hdr+7] = (u8)(nFrag + x);
 49515         }else if( size+pc > usableSize ){
 49516           return SQLITE_CORRUPT_BKPT;
 49517         }else{
 49518           /* The slot remains on the free-list. Reduce its size to account
 49519           ** for the portion used by the new allocation. */
 49520           put2byte(&data[pc+2], x);
 49522         *pIdx = pc + x;
 49523         return SQLITE_OK;
 49528   /* Check to make sure there is enough space in the gap to satisfy
 49529   ** the allocation.  If not, defragment.
 49530   */
 49531   testcase( gap+2+nByte==top );
 49532   if( gap+2+nByte>top ){
 49533     rc = defragmentPage(pPage);
 49534     if( rc ) return rc;
 49535     top = get2byteNotZero(&data[hdr+5]);
 49536     assert( gap+nByte<=top );
 49540   /* Allocate memory from the gap in between the cell pointer array
 49541   ** and the cell content area.  The btreeInitPage() call has already
 49542   ** validated the freelist.  Given that the freelist is valid, there
 49543   ** is no way that the allocation can extend off the end of the page.
 49544   ** The assert() below verifies the previous sentence.
 49545   */
 49546   top -= nByte;
 49547   put2byte(&data[hdr+5], top);
 49548   assert( top+nByte <= (int)pPage->pBt->usableSize );
 49549   *pIdx = top;
 49550   return SQLITE_OK;
 49553 /*
 49554 ** Return a section of the pPage->aData to the freelist.
 49555 ** The first byte of the new free block is pPage->aDisk[start]
 49556 ** and the size of the block is "size" bytes.
 49557 **
 49558 ** Most of the effort here is involved in coalesing adjacent
 49559 ** free blocks into a single big free block.
 49560 */
 49561 static int freeSpace(MemPage *pPage, int start, int size){
 49562   int addr, pbegin, hdr;
 49563   int iLast;                        /* Largest possible freeblock offset */
 49564   unsigned char *data = pPage->aData;
 49566   assert( pPage->pBt!=0 );
 49567   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 49568   assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
 49569   assert( (start + size) <= (int)pPage->pBt->usableSize );
 49570   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49571   assert( size>=0 );   /* Minimum cell size is 4 */
 49573   if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
 49574     /* Overwrite deleted information with zeros when the secure_delete
 49575     ** option is enabled */
 49576     memset(&data[start], 0, size);
 49579   /* Add the space back into the linked list of freeblocks.  Note that
 49580   ** even though the freeblock list was checked by btreeInitPage(),
 49581   ** btreeInitPage() did not detect overlapping cells or
 49582   ** freeblocks that overlapped cells.   Nor does it detect when the
 49583   ** cell content area exceeds the value in the page header.  If these
 49584   ** situations arise, then subsequent insert operations might corrupt
 49585   ** the freelist.  So we do need to check for corruption while scanning
 49586   ** the freelist.
 49587   */
 49588   hdr = pPage->hdrOffset;
 49589   addr = hdr + 1;
 49590   iLast = pPage->pBt->usableSize - 4;
 49591   assert( start<=iLast );
 49592   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
 49593     if( pbegin<addr+4 ){
 49594       return SQLITE_CORRUPT_BKPT;
 49596     addr = pbegin;
 49598   if( pbegin>iLast ){
 49599     return SQLITE_CORRUPT_BKPT;
 49601   assert( pbegin>addr || pbegin==0 );
 49602   put2byte(&data[addr], start);
 49603   put2byte(&data[start], pbegin);
 49604   put2byte(&data[start+2], size);
 49605   pPage->nFree = pPage->nFree + (u16)size;
 49607   /* Coalesce adjacent free blocks */
 49608   addr = hdr + 1;
 49609   while( (pbegin = get2byte(&data[addr]))>0 ){
 49610     int pnext, psize, x;
 49611     assert( pbegin>addr );
 49612     assert( pbegin <= (int)pPage->pBt->usableSize-4 );
 49613     pnext = get2byte(&data[pbegin]);
 49614     psize = get2byte(&data[pbegin+2]);
 49615     if( pbegin + psize + 3 >= pnext && pnext>0 ){
 49616       int frag = pnext - (pbegin+psize);
 49617       if( (frag<0) || (frag>(int)data[hdr+7]) ){
 49618         return SQLITE_CORRUPT_BKPT;
 49620       data[hdr+7] -= (u8)frag;
 49621       x = get2byte(&data[pnext]);
 49622       put2byte(&data[pbegin], x);
 49623       x = pnext + get2byte(&data[pnext+2]) - pbegin;
 49624       put2byte(&data[pbegin+2], x);
 49625     }else{
 49626       addr = pbegin;
 49630   /* If the cell content area begins with a freeblock, remove it. */
 49631   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
 49632     int top;
 49633     pbegin = get2byte(&data[hdr+1]);
 49634     memcpy(&data[hdr+1], &data[pbegin], 2);
 49635     top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
 49636     put2byte(&data[hdr+5], top);
 49638   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 49639   return SQLITE_OK;
 49642 /*
 49643 ** Decode the flags byte (the first byte of the header) for a page
 49644 ** and initialize fields of the MemPage structure accordingly.
 49645 **
 49646 ** Only the following combinations are supported.  Anything different
 49647 ** indicates a corrupt database files:
 49648 **
 49649 **         PTF_ZERODATA
 49650 **         PTF_ZERODATA | PTF_LEAF
 49651 **         PTF_LEAFDATA | PTF_INTKEY
 49652 **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
 49653 */
 49654 static int decodeFlags(MemPage *pPage, int flagByte){
 49655   BtShared *pBt;     /* A copy of pPage->pBt */
 49657   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
 49658   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49659   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
 49660   flagByte &= ~PTF_LEAF;
 49661   pPage->childPtrSize = 4-4*pPage->leaf;
 49662   pBt = pPage->pBt;
 49663   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
 49664     pPage->intKey = 1;
 49665     pPage->hasData = pPage->leaf;
 49666     pPage->maxLocal = pBt->maxLeaf;
 49667     pPage->minLocal = pBt->minLeaf;
 49668   }else if( flagByte==PTF_ZERODATA ){
 49669     pPage->intKey = 0;
 49670     pPage->hasData = 0;
 49671     pPage->maxLocal = pBt->maxLocal;
 49672     pPage->minLocal = pBt->minLocal;
 49673   }else{
 49674     return SQLITE_CORRUPT_BKPT;
 49676   pPage->max1bytePayload = pBt->max1bytePayload;
 49677   return SQLITE_OK;
 49680 /*
 49681 ** Initialize the auxiliary information for a disk block.
 49682 **
 49683 ** Return SQLITE_OK on success.  If we see that the page does
 49684 ** not contain a well-formed database page, then return 
 49685 ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
 49686 ** guarantee that the page is well-formed.  It only shows that
 49687 ** we failed to detect any corruption.
 49688 */
 49689 static int btreeInitPage(MemPage *pPage){
 49691   assert( pPage->pBt!=0 );
 49692   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49693   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
 49694   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
 49695   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
 49697   if( !pPage->isInit ){
 49698     u16 pc;            /* Address of a freeblock within pPage->aData[] */
 49699     u8 hdr;            /* Offset to beginning of page header */
 49700     u8 *data;          /* Equal to pPage->aData */
 49701     BtShared *pBt;        /* The main btree structure */
 49702     int usableSize;    /* Amount of usable space on each page */
 49703     u16 cellOffset;    /* Offset from start of page to first cell pointer */
 49704     int nFree;         /* Number of unused bytes on the page */
 49705     int top;           /* First byte of the cell content area */
 49706     int iCellFirst;    /* First allowable cell or freeblock offset */
 49707     int iCellLast;     /* Last possible cell or freeblock offset */
 49709     pBt = pPage->pBt;
 49711     hdr = pPage->hdrOffset;
 49712     data = pPage->aData;
 49713     if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
 49714     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 49715     pPage->maskPage = (u16)(pBt->pageSize - 1);
 49716     pPage->nOverflow = 0;
 49717     usableSize = pBt->usableSize;
 49718     pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
 49719     pPage->aDataEnd = &data[usableSize];
 49720     pPage->aCellIdx = &data[cellOffset];
 49721     top = get2byteNotZero(&data[hdr+5]);
 49722     pPage->nCell = get2byte(&data[hdr+3]);
 49723     if( pPage->nCell>MX_CELL(pBt) ){
 49724       /* To many cells for a single page.  The page must be corrupt */
 49725       return SQLITE_CORRUPT_BKPT;
 49727     testcase( pPage->nCell==MX_CELL(pBt) );
 49729     /* A malformed database page might cause us to read past the end
 49730     ** of page when parsing a cell.  
 49731     **
 49732     ** The following block of code checks early to see if a cell extends
 49733     ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
 49734     ** returned if it does.
 49735     */
 49736     iCellFirst = cellOffset + 2*pPage->nCell;
 49737     iCellLast = usableSize - 4;
 49738 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 49740       int i;            /* Index into the cell pointer array */
 49741       int sz;           /* Size of a cell */
 49743       if( !pPage->leaf ) iCellLast--;
 49744       for(i=0; i<pPage->nCell; i++){
 49745         pc = get2byte(&data[cellOffset+i*2]);
 49746         testcase( pc==iCellFirst );
 49747         testcase( pc==iCellLast );
 49748         if( pc<iCellFirst || pc>iCellLast ){
 49749           return SQLITE_CORRUPT_BKPT;
 49751         sz = cellSizePtr(pPage, &data[pc]);
 49752         testcase( pc+sz==usableSize );
 49753         if( pc+sz>usableSize ){
 49754           return SQLITE_CORRUPT_BKPT;
 49757       if( !pPage->leaf ) iCellLast++;
 49759 #endif
 49761     /* Compute the total free space on the page */
 49762     pc = get2byte(&data[hdr+1]);
 49763     nFree = data[hdr+7] + top;
 49764     while( pc>0 ){
 49765       u16 next, size;
 49766       if( pc<iCellFirst || pc>iCellLast ){
 49767         /* Start of free block is off the page */
 49768         return SQLITE_CORRUPT_BKPT; 
 49770       next = get2byte(&data[pc]);
 49771       size = get2byte(&data[pc+2]);
 49772       if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
 49773         /* Free blocks must be in ascending order. And the last byte of
 49774         ** the free-block must lie on the database page.  */
 49775         return SQLITE_CORRUPT_BKPT; 
 49777       nFree = nFree + size;
 49778       pc = next;
 49781     /* At this point, nFree contains the sum of the offset to the start
 49782     ** of the cell-content area plus the number of free bytes within
 49783     ** the cell-content area. If this is greater than the usable-size
 49784     ** of the page, then the page must be corrupted. This check also
 49785     ** serves to verify that the offset to the start of the cell-content
 49786     ** area, according to the page header, lies within the page.
 49787     */
 49788     if( nFree>usableSize ){
 49789       return SQLITE_CORRUPT_BKPT; 
 49791     pPage->nFree = (u16)(nFree - iCellFirst);
 49792     pPage->isInit = 1;
 49794   return SQLITE_OK;
 49797 /*
 49798 ** Set up a raw page so that it looks like a database page holding
 49799 ** no entries.
 49800 */
 49801 static void zeroPage(MemPage *pPage, int flags){
 49802   unsigned char *data = pPage->aData;
 49803   BtShared *pBt = pPage->pBt;
 49804   u8 hdr = pPage->hdrOffset;
 49805   u16 first;
 49807   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
 49808   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
 49809   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
 49810   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 49811   assert( sqlite3_mutex_held(pBt->mutex) );
 49812   if( pBt->btsFlags & BTS_SECURE_DELETE ){
 49813     memset(&data[hdr], 0, pBt->usableSize - hdr);
 49815   data[hdr] = (char)flags;
 49816   first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
 49817   memset(&data[hdr+1], 0, 4);
 49818   data[hdr+7] = 0;
 49819   put2byte(&data[hdr+5], pBt->usableSize);
 49820   pPage->nFree = (u16)(pBt->usableSize - first);
 49821   decodeFlags(pPage, flags);
 49822   pPage->hdrOffset = hdr;
 49823   pPage->cellOffset = first;
 49824   pPage->aDataEnd = &data[pBt->usableSize];
 49825   pPage->aCellIdx = &data[first];
 49826   pPage->nOverflow = 0;
 49827   assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 49828   pPage->maskPage = (u16)(pBt->pageSize - 1);
 49829   pPage->nCell = 0;
 49830   pPage->isInit = 1;
 49834 /*
 49835 ** Convert a DbPage obtained from the pager into a MemPage used by
 49836 ** the btree layer.
 49837 */
 49838 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
 49839   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
 49840   pPage->aData = sqlite3PagerGetData(pDbPage);
 49841   pPage->pDbPage = pDbPage;
 49842   pPage->pBt = pBt;
 49843   pPage->pgno = pgno;
 49844   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
 49845   return pPage; 
 49848 /*
 49849 ** Get a page from the pager.  Initialize the MemPage.pBt and
 49850 ** MemPage.aData elements if needed.
 49851 **
 49852 ** If the noContent flag is set, it means that we do not care about
 49853 ** the content of the page at this time.  So do not go to the disk
 49854 ** to fetch the content.  Just fill in the content with zeros for now.
 49855 ** If in the future we call sqlite3PagerWrite() on this page, that
 49856 ** means we have started to be concerned about content and the disk
 49857 ** read should occur at that point.
 49858 */
 49859 static int btreeGetPage(
 49860   BtShared *pBt,       /* The btree */
 49861   Pgno pgno,           /* Number of the page to fetch */
 49862   MemPage **ppPage,    /* Return the page in this parameter */
 49863   int noContent        /* Do not load page content if true */
 49864 ){
 49865   int rc;
 49866   DbPage *pDbPage;
 49868   assert( sqlite3_mutex_held(pBt->mutex) );
 49869   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
 49870   if( rc ) return rc;
 49871   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
 49872   return SQLITE_OK;
 49875 /*
 49876 ** Retrieve a page from the pager cache. If the requested page is not
 49877 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
 49878 ** MemPage.aData elements if needed.
 49879 */
 49880 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
 49881   DbPage *pDbPage;
 49882   assert( sqlite3_mutex_held(pBt->mutex) );
 49883   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
 49884   if( pDbPage ){
 49885     return btreePageFromDbPage(pDbPage, pgno, pBt);
 49887   return 0;
 49890 /*
 49891 ** Return the size of the database file in pages. If there is any kind of
 49892 ** error, return ((unsigned int)-1).
 49893 */
 49894 static Pgno btreePagecount(BtShared *pBt){
 49895   return pBt->nPage;
 49897 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
 49898   assert( sqlite3BtreeHoldsMutex(p) );
 49899   assert( ((p->pBt->nPage)&0x8000000)==0 );
 49900   return (int)btreePagecount(p->pBt);
 49903 /*
 49904 ** Get a page from the pager and initialize it.  This routine is just a
 49905 ** convenience wrapper around separate calls to btreeGetPage() and 
 49906 ** btreeInitPage().
 49907 **
 49908 ** If an error occurs, then the value *ppPage is set to is undefined. It
 49909 ** may remain unchanged, or it may be set to an invalid value.
 49910 */
 49911 static int getAndInitPage(
 49912   BtShared *pBt,          /* The database file */
 49913   Pgno pgno,           /* Number of the page to get */
 49914   MemPage **ppPage     /* Write the page pointer here */
 49915 ){
 49916   int rc;
 49917   assert( sqlite3_mutex_held(pBt->mutex) );
 49919   if( pgno>btreePagecount(pBt) ){
 49920     rc = SQLITE_CORRUPT_BKPT;
 49921   }else{
 49922     rc = btreeGetPage(pBt, pgno, ppPage, 0);
 49923     if( rc==SQLITE_OK ){
 49924       rc = btreeInitPage(*ppPage);
 49925       if( rc!=SQLITE_OK ){
 49926         releasePage(*ppPage);
 49931   testcase( pgno==0 );
 49932   assert( pgno!=0 || rc==SQLITE_CORRUPT );
 49933   return rc;
 49936 /*
 49937 ** Release a MemPage.  This should be called once for each prior
 49938 ** call to btreeGetPage.
 49939 */
 49940 static void releasePage(MemPage *pPage){
 49941   if( pPage ){
 49942     assert( pPage->aData );
 49943     assert( pPage->pBt );
 49944     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
 49945     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
 49946     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49947     sqlite3PagerUnref(pPage->pDbPage);
 49951 /*
 49952 ** During a rollback, when the pager reloads information into the cache
 49953 ** so that the cache is restored to its original state at the start of
 49954 ** the transaction, for each page restored this routine is called.
 49955 **
 49956 ** This routine needs to reset the extra data section at the end of the
 49957 ** page to agree with the restored data.
 49958 */
 49959 static void pageReinit(DbPage *pData){
 49960   MemPage *pPage;
 49961   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
 49962   assert( sqlite3PagerPageRefcount(pData)>0 );
 49963   if( pPage->isInit ){
 49964     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 49965     pPage->isInit = 0;
 49966     if( sqlite3PagerPageRefcount(pData)>1 ){
 49967       /* pPage might not be a btree page;  it might be an overflow page
 49968       ** or ptrmap page or a free page.  In those cases, the following
 49969       ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
 49970       ** But no harm is done by this.  And it is very important that
 49971       ** btreeInitPage() be called on every btree page so we make
 49972       ** the call for every page that comes in for re-initing. */
 49973       btreeInitPage(pPage);
 49978 /*
 49979 ** Invoke the busy handler for a btree.
 49980 */
 49981 static int btreeInvokeBusyHandler(void *pArg){
 49982   BtShared *pBt = (BtShared*)pArg;
 49983   assert( pBt->db );
 49984   assert( sqlite3_mutex_held(pBt->db->mutex) );
 49985   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
 49988 /*
 49989 ** Open a database file.
 49990 ** 
 49991 ** zFilename is the name of the database file.  If zFilename is NULL
 49992 ** then an ephemeral database is created.  The ephemeral database might
 49993 ** be exclusively in memory, or it might use a disk-based memory cache.
 49994 ** Either way, the ephemeral database will be automatically deleted 
 49995 ** when sqlite3BtreeClose() is called.
 49996 **
 49997 ** If zFilename is ":memory:" then an in-memory database is created
 49998 ** that is automatically destroyed when it is closed.
 49999 **
 50000 ** The "flags" parameter is a bitmask that might contain bits like
 50001 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
 50002 **
 50003 ** If the database is already opened in the same database connection
 50004 ** and we are in shared cache mode, then the open will fail with an
 50005 ** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
 50006 ** objects in the same database connection since doing so will lead
 50007 ** to problems with locking.
 50008 */
 50009 SQLITE_PRIVATE int sqlite3BtreeOpen(
 50010   sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
 50011   const char *zFilename,  /* Name of the file containing the BTree database */
 50012   sqlite3 *db,            /* Associated database handle */
 50013   Btree **ppBtree,        /* Pointer to new Btree object written here */
 50014   int flags,              /* Options */
 50015   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
 50016 ){
 50017   BtShared *pBt = 0;             /* Shared part of btree structure */
 50018   Btree *p;                      /* Handle to return */
 50019   sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
 50020   int rc = SQLITE_OK;            /* Result code from this function */
 50021   u8 nReserve;                   /* Byte of unused space on each page */
 50022   unsigned char zDbHeader[100];  /* Database header content */
 50024   /* True if opening an ephemeral, temporary database */
 50025   const int isTempDb = zFilename==0 || zFilename[0]==0;
 50027   /* Set the variable isMemdb to true for an in-memory database, or 
 50028   ** false for a file-based database.
 50029   */
 50030 #ifdef SQLITE_OMIT_MEMORYDB
 50031   const int isMemdb = 0;
 50032 #else
 50033   const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
 50034                        || (isTempDb && sqlite3TempInMemory(db))
 50035                        || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
 50036 #endif
 50038   assert( db!=0 );
 50039   assert( pVfs!=0 );
 50040   assert( sqlite3_mutex_held(db->mutex) );
 50041   assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
 50043   /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
 50044   assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
 50046   /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
 50047   assert( (flags & BTREE_SINGLE)==0 || isTempDb );
 50049   if( isMemdb ){
 50050     flags |= BTREE_MEMORY;
 50052   if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
 50053     vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
 50055   p = sqlite3MallocZero(sizeof(Btree));
 50056   if( !p ){
 50057     return SQLITE_NOMEM;
 50059   p->inTrans = TRANS_NONE;
 50060   p->db = db;
 50061 #ifndef SQLITE_OMIT_SHARED_CACHE
 50062   p->lock.pBtree = p;
 50063   p->lock.iTable = 1;
 50064 #endif
 50066 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 50067   /*
 50068   ** If this Btree is a candidate for shared cache, try to find an
 50069   ** existing BtShared object that we can share with
 50070   */
 50071   if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
 50072     if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
 50073       int nFullPathname = pVfs->mxPathname+1;
 50074       char *zFullPathname = sqlite3Malloc(nFullPathname);
 50075       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 50076       p->sharable = 1;
 50077       if( !zFullPathname ){
 50078         sqlite3_free(p);
 50079         return SQLITE_NOMEM;
 50081       if( isMemdb ){
 50082         memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
 50083       }else{
 50084         rc = sqlite3OsFullPathname(pVfs, zFilename,
 50085                                    nFullPathname, zFullPathname);
 50086         if( rc ){
 50087           sqlite3_free(zFullPathname);
 50088           sqlite3_free(p);
 50089           return rc;
 50092 #if SQLITE_THREADSAFE
 50093       mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
 50094       sqlite3_mutex_enter(mutexOpen);
 50095       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 50096       sqlite3_mutex_enter(mutexShared);
 50097 #endif
 50098       for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
 50099         assert( pBt->nRef>0 );
 50100         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
 50101                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
 50102           int iDb;
 50103           for(iDb=db->nDb-1; iDb>=0; iDb--){
 50104             Btree *pExisting = db->aDb[iDb].pBt;
 50105             if( pExisting && pExisting->pBt==pBt ){
 50106               sqlite3_mutex_leave(mutexShared);
 50107               sqlite3_mutex_leave(mutexOpen);
 50108               sqlite3_free(zFullPathname);
 50109               sqlite3_free(p);
 50110               return SQLITE_CONSTRAINT;
 50113           p->pBt = pBt;
 50114           pBt->nRef++;
 50115           break;
 50118       sqlite3_mutex_leave(mutexShared);
 50119       sqlite3_free(zFullPathname);
 50121 #ifdef SQLITE_DEBUG
 50122     else{
 50123       /* In debug mode, we mark all persistent databases as sharable
 50124       ** even when they are not.  This exercises the locking code and
 50125       ** gives more opportunity for asserts(sqlite3_mutex_held())
 50126       ** statements to find locking problems.
 50127       */
 50128       p->sharable = 1;
 50130 #endif
 50132 #endif
 50133   if( pBt==0 ){
 50134     /*
 50135     ** The following asserts make sure that structures used by the btree are
 50136     ** the right size.  This is to guard against size changes that result
 50137     ** when compiling on a different architecture.
 50138     */
 50139     assert( sizeof(i64)==8 || sizeof(i64)==4 );
 50140     assert( sizeof(u64)==8 || sizeof(u64)==4 );
 50141     assert( sizeof(u32)==4 );
 50142     assert( sizeof(u16)==2 );
 50143     assert( sizeof(Pgno)==4 );
 50145     pBt = sqlite3MallocZero( sizeof(*pBt) );
 50146     if( pBt==0 ){
 50147       rc = SQLITE_NOMEM;
 50148       goto btree_open_out;
 50150     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
 50151                           EXTRA_SIZE, flags, vfsFlags, pageReinit);
 50152     if( rc==SQLITE_OK ){
 50153       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
 50155     if( rc!=SQLITE_OK ){
 50156       goto btree_open_out;
 50158     pBt->openFlags = (u8)flags;
 50159     pBt->db = db;
 50160     sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
 50161     p->pBt = pBt;
 50163     pBt->pCursor = 0;
 50164     pBt->pPage1 = 0;
 50165     if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
 50166 #ifdef SQLITE_SECURE_DELETE
 50167     pBt->btsFlags |= BTS_SECURE_DELETE;
 50168 #endif
 50169     pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
 50170     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
 50171          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
 50172       pBt->pageSize = 0;
 50173 #ifndef SQLITE_OMIT_AUTOVACUUM
 50174       /* If the magic name ":memory:" will create an in-memory database, then
 50175       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
 50176       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
 50177       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
 50178       ** regular file-name. In this case the auto-vacuum applies as per normal.
 50179       */
 50180       if( zFilename && !isMemdb ){
 50181         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
 50182         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
 50184 #endif
 50185       nReserve = 0;
 50186     }else{
 50187       nReserve = zDbHeader[20];
 50188       pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 50189 #ifndef SQLITE_OMIT_AUTOVACUUM
 50190       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
 50191       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
 50192 #endif
 50194     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
 50195     if( rc ) goto btree_open_out;
 50196     pBt->usableSize = pBt->pageSize - nReserve;
 50197     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
 50199 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 50200     /* Add the new BtShared object to the linked list sharable BtShareds.
 50201     */
 50202     if( p->sharable ){
 50203       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 50204       pBt->nRef = 1;
 50205       MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
 50206       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
 50207         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
 50208         if( pBt->mutex==0 ){
 50209           rc = SQLITE_NOMEM;
 50210           db->mallocFailed = 0;
 50211           goto btree_open_out;
 50214       sqlite3_mutex_enter(mutexShared);
 50215       pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
 50216       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
 50217       sqlite3_mutex_leave(mutexShared);
 50219 #endif
 50222 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 50223   /* If the new Btree uses a sharable pBtShared, then link the new
 50224   ** Btree into the list of all sharable Btrees for the same connection.
 50225   ** The list is kept in ascending order by pBt address.
 50226   */
 50227   if( p->sharable ){
 50228     int i;
 50229     Btree *pSib;
 50230     for(i=0; i<db->nDb; i++){
 50231       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
 50232         while( pSib->pPrev ){ pSib = pSib->pPrev; }
 50233         if( p->pBt<pSib->pBt ){
 50234           p->pNext = pSib;
 50235           p->pPrev = 0;
 50236           pSib->pPrev = p;
 50237         }else{
 50238           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
 50239             pSib = pSib->pNext;
 50241           p->pNext = pSib->pNext;
 50242           p->pPrev = pSib;
 50243           if( p->pNext ){
 50244             p->pNext->pPrev = p;
 50246           pSib->pNext = p;
 50248         break;
 50252 #endif
 50253   *ppBtree = p;
 50255 btree_open_out:
 50256   if( rc!=SQLITE_OK ){
 50257     if( pBt && pBt->pPager ){
 50258       sqlite3PagerClose(pBt->pPager);
 50260     sqlite3_free(pBt);
 50261     sqlite3_free(p);
 50262     *ppBtree = 0;
 50263   }else{
 50264     /* If the B-Tree was successfully opened, set the pager-cache size to the
 50265     ** default value. Except, when opening on an existing shared pager-cache,
 50266     ** do not change the pager-cache size.
 50267     */
 50268     if( sqlite3BtreeSchema(p, 0, 0)==0 ){
 50269       sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
 50272   if( mutexOpen ){
 50273     assert( sqlite3_mutex_held(mutexOpen) );
 50274     sqlite3_mutex_leave(mutexOpen);
 50276   return rc;
 50279 /*
 50280 ** Decrement the BtShared.nRef counter.  When it reaches zero,
 50281 ** remove the BtShared structure from the sharing list.  Return
 50282 ** true if the BtShared.nRef counter reaches zero and return
 50283 ** false if it is still positive.
 50284 */
 50285 static int removeFromSharingList(BtShared *pBt){
 50286 #ifndef SQLITE_OMIT_SHARED_CACHE
 50287   MUTEX_LOGIC( sqlite3_mutex *pMaster; )
 50288   BtShared *pList;
 50289   int removed = 0;
 50291   assert( sqlite3_mutex_notheld(pBt->mutex) );
 50292   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 50293   sqlite3_mutex_enter(pMaster);
 50294   pBt->nRef--;
 50295   if( pBt->nRef<=0 ){
 50296     if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
 50297       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
 50298     }else{
 50299       pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
 50300       while( ALWAYS(pList) && pList->pNext!=pBt ){
 50301         pList=pList->pNext;
 50303       if( ALWAYS(pList) ){
 50304         pList->pNext = pBt->pNext;
 50307     if( SQLITE_THREADSAFE ){
 50308       sqlite3_mutex_free(pBt->mutex);
 50310     removed = 1;
 50312   sqlite3_mutex_leave(pMaster);
 50313   return removed;
 50314 #else
 50315   return 1;
 50316 #endif
 50319 /*
 50320 ** Make sure pBt->pTmpSpace points to an allocation of 
 50321 ** MX_CELL_SIZE(pBt) bytes.
 50322 */
 50323 static void allocateTempSpace(BtShared *pBt){
 50324   if( !pBt->pTmpSpace ){
 50325     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
 50329 /*
 50330 ** Free the pBt->pTmpSpace allocation
 50331 */
 50332 static void freeTempSpace(BtShared *pBt){
 50333   sqlite3PageFree( pBt->pTmpSpace);
 50334   pBt->pTmpSpace = 0;
 50337 /*
 50338 ** Close an open database and invalidate all cursors.
 50339 */
 50340 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
 50341   BtShared *pBt = p->pBt;
 50342   BtCursor *pCur;
 50344   /* Close all cursors opened via this handle.  */
 50345   assert( sqlite3_mutex_held(p->db->mutex) );
 50346   sqlite3BtreeEnter(p);
 50347   pCur = pBt->pCursor;
 50348   while( pCur ){
 50349     BtCursor *pTmp = pCur;
 50350     pCur = pCur->pNext;
 50351     if( pTmp->pBtree==p ){
 50352       sqlite3BtreeCloseCursor(pTmp);
 50356   /* Rollback any active transaction and free the handle structure.
 50357   ** The call to sqlite3BtreeRollback() drops any table-locks held by
 50358   ** this handle.
 50359   */
 50360   sqlite3BtreeRollback(p, SQLITE_OK);
 50361   sqlite3BtreeLeave(p);
 50363   /* If there are still other outstanding references to the shared-btree
 50364   ** structure, return now. The remainder of this procedure cleans 
 50365   ** up the shared-btree.
 50366   */
 50367   assert( p->wantToLock==0 && p->locked==0 );
 50368   if( !p->sharable || removeFromSharingList(pBt) ){
 50369     /* The pBt is no longer on the sharing list, so we can access
 50370     ** it without having to hold the mutex.
 50371     **
 50372     ** Clean out and delete the BtShared object.
 50373     */
 50374     assert( !pBt->pCursor );
 50375     sqlite3PagerClose(pBt->pPager);
 50376     if( pBt->xFreeSchema && pBt->pSchema ){
 50377       pBt->xFreeSchema(pBt->pSchema);
 50379     sqlite3DbFree(0, pBt->pSchema);
 50380     freeTempSpace(pBt);
 50381     sqlite3_free(pBt);
 50384 #ifndef SQLITE_OMIT_SHARED_CACHE
 50385   assert( p->wantToLock==0 );
 50386   assert( p->locked==0 );
 50387   if( p->pPrev ) p->pPrev->pNext = p->pNext;
 50388   if( p->pNext ) p->pNext->pPrev = p->pPrev;
 50389 #endif
 50391   sqlite3_free(p);
 50392   return SQLITE_OK;
 50395 /*
 50396 ** Change the limit on the number of pages allowed in the cache.
 50397 **
 50398 ** The maximum number of cache pages is set to the absolute
 50399 ** value of mxPage.  If mxPage is negative, the pager will
 50400 ** operate asynchronously - it will not stop to do fsync()s
 50401 ** to insure data is written to the disk surface before
 50402 ** continuing.  Transactions still work if synchronous is off,
 50403 ** and the database cannot be corrupted if this program
 50404 ** crashes.  But if the operating system crashes or there is
 50405 ** an abrupt power failure when synchronous is off, the database
 50406 ** could be left in an inconsistent and unrecoverable state.
 50407 ** Synchronous is on by default so database corruption is not
 50408 ** normally a worry.
 50409 */
 50410 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
 50411   BtShared *pBt = p->pBt;
 50412   assert( sqlite3_mutex_held(p->db->mutex) );
 50413   sqlite3BtreeEnter(p);
 50414   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
 50415   sqlite3BtreeLeave(p);
 50416   return SQLITE_OK;
 50419 /*
 50420 ** Change the way data is synced to disk in order to increase or decrease
 50421 ** how well the database resists damage due to OS crashes and power
 50422 ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
 50423 ** there is a high probability of damage)  Level 2 is the default.  There
 50424 ** is a very low but non-zero probability of damage.  Level 3 reduces the
 50425 ** probability of damage to near zero but with a write performance reduction.
 50426 */
 50427 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 50428 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
 50429   Btree *p,              /* The btree to set the safety level on */
 50430   int level,             /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
 50431   int fullSync,          /* PRAGMA fullfsync. */
 50432   int ckptFullSync       /* PRAGMA checkpoint_fullfync */
 50433 ){
 50434   BtShared *pBt = p->pBt;
 50435   assert( sqlite3_mutex_held(p->db->mutex) );
 50436   assert( level>=1 && level<=3 );
 50437   sqlite3BtreeEnter(p);
 50438   sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
 50439   sqlite3BtreeLeave(p);
 50440   return SQLITE_OK;
 50442 #endif
 50444 /*
 50445 ** Return TRUE if the given btree is set to safety level 1.  In other
 50446 ** words, return TRUE if no sync() occurs on the disk files.
 50447 */
 50448 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
 50449   BtShared *pBt = p->pBt;
 50450   int rc;
 50451   assert( sqlite3_mutex_held(p->db->mutex) );  
 50452   sqlite3BtreeEnter(p);
 50453   assert( pBt && pBt->pPager );
 50454   rc = sqlite3PagerNosync(pBt->pPager);
 50455   sqlite3BtreeLeave(p);
 50456   return rc;
 50459 /*
 50460 ** Change the default pages size and the number of reserved bytes per page.
 50461 ** Or, if the page size has already been fixed, return SQLITE_READONLY 
 50462 ** without changing anything.
 50463 **
 50464 ** The page size must be a power of 2 between 512 and 65536.  If the page
 50465 ** size supplied does not meet this constraint then the page size is not
 50466 ** changed.
 50467 **
 50468 ** Page sizes are constrained to be a power of two so that the region
 50469 ** of the database file used for locking (beginning at PENDING_BYTE,
 50470 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
 50471 ** at the beginning of a page.
 50472 **
 50473 ** If parameter nReserve is less than zero, then the number of reserved
 50474 ** bytes per page is left unchanged.
 50475 **
 50476 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
 50477 ** and autovacuum mode can no longer be changed.
 50478 */
 50479 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
 50480   int rc = SQLITE_OK;
 50481   BtShared *pBt = p->pBt;
 50482   assert( nReserve>=-1 && nReserve<=255 );
 50483   sqlite3BtreeEnter(p);
 50484   if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
 50485     sqlite3BtreeLeave(p);
 50486     return SQLITE_READONLY;
 50488   if( nReserve<0 ){
 50489     nReserve = pBt->pageSize - pBt->usableSize;
 50491   assert( nReserve>=0 && nReserve<=255 );
 50492   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
 50493         ((pageSize-1)&pageSize)==0 ){
 50494     assert( (pageSize & 7)==0 );
 50495     assert( !pBt->pPage1 && !pBt->pCursor );
 50496     pBt->pageSize = (u32)pageSize;
 50497     freeTempSpace(pBt);
 50499   rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
 50500   pBt->usableSize = pBt->pageSize - (u16)nReserve;
 50501   if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 50502   sqlite3BtreeLeave(p);
 50503   return rc;
 50506 /*
 50507 ** Return the currently defined page size
 50508 */
 50509 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
 50510   return p->pBt->pageSize;
 50513 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
 50514 /*
 50515 ** This function is similar to sqlite3BtreeGetReserve(), except that it
 50516 ** may only be called if it is guaranteed that the b-tree mutex is already
 50517 ** held.
 50518 **
 50519 ** This is useful in one special case in the backup API code where it is
 50520 ** known that the shared b-tree mutex is held, but the mutex on the 
 50521 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
 50522 ** were to be called, it might collide with some other operation on the
 50523 ** database handle that owns *p, causing undefined behaviour.
 50524 */
 50525 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
 50526   assert( sqlite3_mutex_held(p->pBt->mutex) );
 50527   return p->pBt->pageSize - p->pBt->usableSize;
 50529 #endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
 50531 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
 50532 /*
 50533 ** Return the number of bytes of space at the end of every page that
 50534 ** are intentually left unused.  This is the "reserved" space that is
 50535 ** sometimes used by extensions.
 50536 */
 50537 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
 50538   int n;
 50539   sqlite3BtreeEnter(p);
 50540   n = p->pBt->pageSize - p->pBt->usableSize;
 50541   sqlite3BtreeLeave(p);
 50542   return n;
 50545 /*
 50546 ** Set the maximum page count for a database if mxPage is positive.
 50547 ** No changes are made if mxPage is 0 or negative.
 50548 ** Regardless of the value of mxPage, return the maximum page count.
 50549 */
 50550 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
 50551   int n;
 50552   sqlite3BtreeEnter(p);
 50553   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
 50554   sqlite3BtreeLeave(p);
 50555   return n;
 50558 /*
 50559 ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
 50560 ** then make no changes.  Always return the value of the BTS_SECURE_DELETE
 50561 ** setting after the change.
 50562 */
 50563 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
 50564   int b;
 50565   if( p==0 ) return 0;
 50566   sqlite3BtreeEnter(p);
 50567   if( newFlag>=0 ){
 50568     p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
 50569     if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
 50571   b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
 50572   sqlite3BtreeLeave(p);
 50573   return b;
 50575 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
 50577 /*
 50578 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
 50579 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
 50580 ** is disabled. The default value for the auto-vacuum property is 
 50581 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
 50582 */
 50583 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
 50584 #ifdef SQLITE_OMIT_AUTOVACUUM
 50585   return SQLITE_READONLY;
 50586 #else
 50587   BtShared *pBt = p->pBt;
 50588   int rc = SQLITE_OK;
 50589   u8 av = (u8)autoVacuum;
 50591   sqlite3BtreeEnter(p);
 50592   if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
 50593     rc = SQLITE_READONLY;
 50594   }else{
 50595     pBt->autoVacuum = av ?1:0;
 50596     pBt->incrVacuum = av==2 ?1:0;
 50598   sqlite3BtreeLeave(p);
 50599   return rc;
 50600 #endif
 50603 /*
 50604 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
 50605 ** enabled 1 is returned. Otherwise 0.
 50606 */
 50607 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
 50608 #ifdef SQLITE_OMIT_AUTOVACUUM
 50609   return BTREE_AUTOVACUUM_NONE;
 50610 #else
 50611   int rc;
 50612   sqlite3BtreeEnter(p);
 50613   rc = (
 50614     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
 50615     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
 50616     BTREE_AUTOVACUUM_INCR
 50617   );
 50618   sqlite3BtreeLeave(p);
 50619   return rc;
 50620 #endif
 50624 /*
 50625 ** Get a reference to pPage1 of the database file.  This will
 50626 ** also acquire a readlock on that file.
 50627 **
 50628 ** SQLITE_OK is returned on success.  If the file is not a
 50629 ** well-formed database file, then SQLITE_CORRUPT is returned.
 50630 ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
 50631 ** is returned if we run out of memory. 
 50632 */
 50633 static int lockBtree(BtShared *pBt){
 50634   int rc;              /* Result code from subfunctions */
 50635   MemPage *pPage1;     /* Page 1 of the database file */
 50636   int nPage;           /* Number of pages in the database */
 50637   int nPageFile = 0;   /* Number of pages in the database file */
 50638   int nPageHeader;     /* Number of pages in the database according to hdr */
 50640   assert( sqlite3_mutex_held(pBt->mutex) );
 50641   assert( pBt->pPage1==0 );
 50642   rc = sqlite3PagerSharedLock(pBt->pPager);
 50643   if( rc!=SQLITE_OK ) return rc;
 50644   rc = btreeGetPage(pBt, 1, &pPage1, 0);
 50645   if( rc!=SQLITE_OK ) return rc;
 50647   /* Do some checking to help insure the file we opened really is
 50648   ** a valid database file. 
 50649   */
 50650   nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
 50651   sqlite3PagerPagecount(pBt->pPager, &nPageFile);
 50652   if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
 50653     nPage = nPageFile;
 50655   if( nPage>0 ){
 50656     u32 pageSize;
 50657     u32 usableSize;
 50658     u8 *page1 = pPage1->aData;
 50659     rc = SQLITE_NOTADB;
 50660     if( memcmp(page1, zMagicHeader, 16)!=0 ){
 50661       goto page1_init_failed;
 50664 #ifdef SQLITE_OMIT_WAL
 50665     if( page1[18]>1 ){
 50666       pBt->btsFlags |= BTS_READ_ONLY;
 50668     if( page1[19]>1 ){
 50669       goto page1_init_failed;
 50671 #else
 50672     if( page1[18]>2 ){
 50673       pBt->btsFlags |= BTS_READ_ONLY;
 50675     if( page1[19]>2 ){
 50676       goto page1_init_failed;
 50679     /* If the write version is set to 2, this database should be accessed
 50680     ** in WAL mode. If the log is not already open, open it now. Then 
 50681     ** return SQLITE_OK and return without populating BtShared.pPage1.
 50682     ** The caller detects this and calls this function again. This is
 50683     ** required as the version of page 1 currently in the page1 buffer
 50684     ** may not be the latest version - there may be a newer one in the log
 50685     ** file.
 50686     */
 50687     if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
 50688       int isOpen = 0;
 50689       rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
 50690       if( rc!=SQLITE_OK ){
 50691         goto page1_init_failed;
 50692       }else if( isOpen==0 ){
 50693         releasePage(pPage1);
 50694         return SQLITE_OK;
 50696       rc = SQLITE_NOTADB;
 50698 #endif
 50700     /* The maximum embedded fraction must be exactly 25%.  And the minimum
 50701     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
 50702     ** The original design allowed these amounts to vary, but as of
 50703     ** version 3.6.0, we require them to be fixed.
 50704     */
 50705     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
 50706       goto page1_init_failed;
 50708     pageSize = (page1[16]<<8) | (page1[17]<<16);
 50709     if( ((pageSize-1)&pageSize)!=0
 50710      || pageSize>SQLITE_MAX_PAGE_SIZE 
 50711      || pageSize<=256 
 50712     ){
 50713       goto page1_init_failed;
 50715     assert( (pageSize & 7)==0 );
 50716     usableSize = pageSize - page1[20];
 50717     if( (u32)pageSize!=pBt->pageSize ){
 50718       /* After reading the first page of the database assuming a page size
 50719       ** of BtShared.pageSize, we have discovered that the page-size is
 50720       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
 50721       ** zero and return SQLITE_OK. The caller will call this function
 50722       ** again with the correct page-size.
 50723       */
 50724       releasePage(pPage1);
 50725       pBt->usableSize = usableSize;
 50726       pBt->pageSize = pageSize;
 50727       freeTempSpace(pBt);
 50728       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 50729                                    pageSize-usableSize);
 50730       return rc;
 50732     if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
 50733       rc = SQLITE_CORRUPT_BKPT;
 50734       goto page1_init_failed;
 50736     if( usableSize<480 ){
 50737       goto page1_init_failed;
 50739     pBt->pageSize = pageSize;
 50740     pBt->usableSize = usableSize;
 50741 #ifndef SQLITE_OMIT_AUTOVACUUM
 50742     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
 50743     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
 50744 #endif
 50747   /* maxLocal is the maximum amount of payload to store locally for
 50748   ** a cell.  Make sure it is small enough so that at least minFanout
 50749   ** cells can will fit on one page.  We assume a 10-byte page header.
 50750   ** Besides the payload, the cell must store:
 50751   **     2-byte pointer to the cell
 50752   **     4-byte child pointer
 50753   **     9-byte nKey value
 50754   **     4-byte nData value
 50755   **     4-byte overflow page pointer
 50756   ** So a cell consists of a 2-byte pointer, a header which is as much as
 50757   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
 50758   ** page pointer.
 50759   */
 50760   pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
 50761   pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
 50762   pBt->maxLeaf = (u16)(pBt->usableSize - 35);
 50763   pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
 50764   if( pBt->maxLocal>127 ){
 50765     pBt->max1bytePayload = 127;
 50766   }else{
 50767     pBt->max1bytePayload = (u8)pBt->maxLocal;
 50769   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
 50770   pBt->pPage1 = pPage1;
 50771   pBt->nPage = nPage;
 50772   return SQLITE_OK;
 50774 page1_init_failed:
 50775   releasePage(pPage1);
 50776   pBt->pPage1 = 0;
 50777   return rc;
 50780 /*
 50781 ** If there are no outstanding cursors and we are not in the middle
 50782 ** of a transaction but there is a read lock on the database, then
 50783 ** this routine unrefs the first page of the database file which 
 50784 ** has the effect of releasing the read lock.
 50785 **
 50786 ** If there is a transaction in progress, this routine is a no-op.
 50787 */
 50788 static void unlockBtreeIfUnused(BtShared *pBt){
 50789   assert( sqlite3_mutex_held(pBt->mutex) );
 50790   assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
 50791   if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
 50792     assert( pBt->pPage1->aData );
 50793     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
 50794     assert( pBt->pPage1->aData );
 50795     releasePage(pBt->pPage1);
 50796     pBt->pPage1 = 0;
 50800 /*
 50801 ** If pBt points to an empty file then convert that empty file
 50802 ** into a new empty database by initializing the first page of
 50803 ** the database.
 50804 */
 50805 static int newDatabase(BtShared *pBt){
 50806   MemPage *pP1;
 50807   unsigned char *data;
 50808   int rc;
 50810   assert( sqlite3_mutex_held(pBt->mutex) );
 50811   if( pBt->nPage>0 ){
 50812     return SQLITE_OK;
 50814   pP1 = pBt->pPage1;
 50815   assert( pP1!=0 );
 50816   data = pP1->aData;
 50817   rc = sqlite3PagerWrite(pP1->pDbPage);
 50818   if( rc ) return rc;
 50819   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
 50820   assert( sizeof(zMagicHeader)==16 );
 50821   data[16] = (u8)((pBt->pageSize>>8)&0xff);
 50822   data[17] = (u8)((pBt->pageSize>>16)&0xff);
 50823   data[18] = 1;
 50824   data[19] = 1;
 50825   assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
 50826   data[20] = (u8)(pBt->pageSize - pBt->usableSize);
 50827   data[21] = 64;
 50828   data[22] = 32;
 50829   data[23] = 32;
 50830   memset(&data[24], 0, 100-24);
 50831   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
 50832   pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 50833 #ifndef SQLITE_OMIT_AUTOVACUUM
 50834   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
 50835   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
 50836   put4byte(&data[36 + 4*4], pBt->autoVacuum);
 50837   put4byte(&data[36 + 7*4], pBt->incrVacuum);
 50838 #endif
 50839   pBt->nPage = 1;
 50840   data[31] = 1;
 50841   return SQLITE_OK;
 50844 /*
 50845 ** Initialize the first page of the database file (creating a database
 50846 ** consisting of a single page and no schema objects). Return SQLITE_OK
 50847 ** if successful, or an SQLite error code otherwise.
 50848 */
 50849 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
 50850   int rc;
 50851   sqlite3BtreeEnter(p);
 50852   p->pBt->nPage = 0;
 50853   rc = newDatabase(p->pBt);
 50854   sqlite3BtreeLeave(p);
 50855   return rc;
 50858 /*
 50859 ** Attempt to start a new transaction. A write-transaction
 50860 ** is started if the second argument is nonzero, otherwise a read-
 50861 ** transaction.  If the second argument is 2 or more and exclusive
 50862 ** transaction is started, meaning that no other process is allowed
 50863 ** to access the database.  A preexisting transaction may not be
 50864 ** upgraded to exclusive by calling this routine a second time - the
 50865 ** exclusivity flag only works for a new transaction.
 50866 **
 50867 ** A write-transaction must be started before attempting any 
 50868 ** changes to the database.  None of the following routines 
 50869 ** will work unless a transaction is started first:
 50870 **
 50871 **      sqlite3BtreeCreateTable()
 50872 **      sqlite3BtreeCreateIndex()
 50873 **      sqlite3BtreeClearTable()
 50874 **      sqlite3BtreeDropTable()
 50875 **      sqlite3BtreeInsert()
 50876 **      sqlite3BtreeDelete()
 50877 **      sqlite3BtreeUpdateMeta()
 50878 **
 50879 ** If an initial attempt to acquire the lock fails because of lock contention
 50880 ** and the database was previously unlocked, then invoke the busy handler
 50881 ** if there is one.  But if there was previously a read-lock, do not
 50882 ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
 50883 ** returned when there is already a read-lock in order to avoid a deadlock.
 50884 **
 50885 ** Suppose there are two processes A and B.  A has a read lock and B has
 50886 ** a reserved lock.  B tries to promote to exclusive but is blocked because
 50887 ** of A's read lock.  A tries to promote to reserved but is blocked by B.
 50888 ** One or the other of the two processes must give way or there can be
 50889 ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
 50890 ** when A already has a read lock, we encourage A to give up and let B
 50891 ** proceed.
 50892 */
 50893 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
 50894   sqlite3 *pBlock = 0;
 50895   BtShared *pBt = p->pBt;
 50896   int rc = SQLITE_OK;
 50898   sqlite3BtreeEnter(p);
 50899   btreeIntegrity(p);
 50901   /* If the btree is already in a write-transaction, or it
 50902   ** is already in a read-transaction and a read-transaction
 50903   ** is requested, this is a no-op.
 50904   */
 50905   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
 50906     goto trans_begun;
 50909   /* Write transactions are not possible on a read-only database */
 50910   if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
 50911     rc = SQLITE_READONLY;
 50912     goto trans_begun;
 50915 #ifndef SQLITE_OMIT_SHARED_CACHE
 50916   /* If another database handle has already opened a write transaction 
 50917   ** on this shared-btree structure and a second write transaction is
 50918   ** requested, return SQLITE_LOCKED.
 50919   */
 50920   if( (wrflag && pBt->inTransaction==TRANS_WRITE)
 50921    || (pBt->btsFlags & BTS_PENDING)!=0
 50922   ){
 50923     pBlock = pBt->pWriter->db;
 50924   }else if( wrflag>1 ){
 50925     BtLock *pIter;
 50926     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 50927       if( pIter->pBtree!=p ){
 50928         pBlock = pIter->pBtree->db;
 50929         break;
 50933   if( pBlock ){
 50934     sqlite3ConnectionBlocked(p->db, pBlock);
 50935     rc = SQLITE_LOCKED_SHAREDCACHE;
 50936     goto trans_begun;
 50938 #endif
 50940   /* Any read-only or read-write transaction implies a read-lock on 
 50941   ** page 1. So if some other shared-cache client already has a write-lock 
 50942   ** on page 1, the transaction cannot be opened. */
 50943   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
 50944   if( SQLITE_OK!=rc ) goto trans_begun;
 50946   pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
 50947   if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
 50948   do {
 50949     /* Call lockBtree() until either pBt->pPage1 is populated or
 50950     ** lockBtree() returns something other than SQLITE_OK. lockBtree()
 50951     ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
 50952     ** reading page 1 it discovers that the page-size of the database 
 50953     ** file is not pBt->pageSize. In this case lockBtree() will update
 50954     ** pBt->pageSize to the page-size of the file on disk.
 50955     */
 50956     while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
 50958     if( rc==SQLITE_OK && wrflag ){
 50959       if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
 50960         rc = SQLITE_READONLY;
 50961       }else{
 50962         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
 50963         if( rc==SQLITE_OK ){
 50964           rc = newDatabase(pBt);
 50969     if( rc!=SQLITE_OK ){
 50970       unlockBtreeIfUnused(pBt);
 50972   }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
 50973           btreeInvokeBusyHandler(pBt) );
 50975   if( rc==SQLITE_OK ){
 50976     if( p->inTrans==TRANS_NONE ){
 50977       pBt->nTransaction++;
 50978 #ifndef SQLITE_OMIT_SHARED_CACHE
 50979       if( p->sharable ){
 50980         assert( p->lock.pBtree==p && p->lock.iTable==1 );
 50981         p->lock.eLock = READ_LOCK;
 50982         p->lock.pNext = pBt->pLock;
 50983         pBt->pLock = &p->lock;
 50985 #endif
 50987     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
 50988     if( p->inTrans>pBt->inTransaction ){
 50989       pBt->inTransaction = p->inTrans;
 50991     if( wrflag ){
 50992       MemPage *pPage1 = pBt->pPage1;
 50993 #ifndef SQLITE_OMIT_SHARED_CACHE
 50994       assert( !pBt->pWriter );
 50995       pBt->pWriter = p;
 50996       pBt->btsFlags &= ~BTS_EXCLUSIVE;
 50997       if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
 50998 #endif
 51000       /* If the db-size header field is incorrect (as it may be if an old
 51001       ** client has been writing the database file), update it now. Doing
 51002       ** this sooner rather than later means the database size can safely 
 51003       ** re-read the database size from page 1 if a savepoint or transaction
 51004       ** rollback occurs within the transaction.
 51005       */
 51006       if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
 51007         rc = sqlite3PagerWrite(pPage1->pDbPage);
 51008         if( rc==SQLITE_OK ){
 51009           put4byte(&pPage1->aData[28], pBt->nPage);
 51016 trans_begun:
 51017   if( rc==SQLITE_OK && wrflag ){
 51018     /* This call makes sure that the pager has the correct number of
 51019     ** open savepoints. If the second parameter is greater than 0 and
 51020     ** the sub-journal is not already open, then it will be opened here.
 51021     */
 51022     rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
 51025   btreeIntegrity(p);
 51026   sqlite3BtreeLeave(p);
 51027   return rc;
 51030 #ifndef SQLITE_OMIT_AUTOVACUUM
 51032 /*
 51033 ** Set the pointer-map entries for all children of page pPage. Also, if
 51034 ** pPage contains cells that point to overflow pages, set the pointer
 51035 ** map entries for the overflow pages as well.
 51036 */
 51037 static int setChildPtrmaps(MemPage *pPage){
 51038   int i;                             /* Counter variable */
 51039   int nCell;                         /* Number of cells in page pPage */
 51040   int rc;                            /* Return code */
 51041   BtShared *pBt = pPage->pBt;
 51042   u8 isInitOrig = pPage->isInit;
 51043   Pgno pgno = pPage->pgno;
 51045   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 51046   rc = btreeInitPage(pPage);
 51047   if( rc!=SQLITE_OK ){
 51048     goto set_child_ptrmaps_out;
 51050   nCell = pPage->nCell;
 51052   for(i=0; i<nCell; i++){
 51053     u8 *pCell = findCell(pPage, i);
 51055     ptrmapPutOvflPtr(pPage, pCell, &rc);
 51057     if( !pPage->leaf ){
 51058       Pgno childPgno = get4byte(pCell);
 51059       ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
 51063   if( !pPage->leaf ){
 51064     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 51065     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
 51068 set_child_ptrmaps_out:
 51069   pPage->isInit = isInitOrig;
 51070   return rc;
 51073 /*
 51074 ** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
 51075 ** that it points to iTo. Parameter eType describes the type of pointer to
 51076 ** be modified, as  follows:
 51077 **
 51078 ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
 51079 **                   page of pPage.
 51080 **
 51081 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
 51082 **                   page pointed to by one of the cells on pPage.
 51083 **
 51084 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
 51085 **                   overflow page in the list.
 51086 */
 51087 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
 51088   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 51089   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 51090   if( eType==PTRMAP_OVERFLOW2 ){
 51091     /* The pointer is always the first 4 bytes of the page in this case.  */
 51092     if( get4byte(pPage->aData)!=iFrom ){
 51093       return SQLITE_CORRUPT_BKPT;
 51095     put4byte(pPage->aData, iTo);
 51096   }else{
 51097     u8 isInitOrig = pPage->isInit;
 51098     int i;
 51099     int nCell;
 51101     btreeInitPage(pPage);
 51102     nCell = pPage->nCell;
 51104     for(i=0; i<nCell; i++){
 51105       u8 *pCell = findCell(pPage, i);
 51106       if( eType==PTRMAP_OVERFLOW1 ){
 51107         CellInfo info;
 51108         btreeParseCellPtr(pPage, pCell, &info);
 51109         if( info.iOverflow
 51110          && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
 51111          && iFrom==get4byte(&pCell[info.iOverflow])
 51112         ){
 51113           put4byte(&pCell[info.iOverflow], iTo);
 51114           break;
 51116       }else{
 51117         if( get4byte(pCell)==iFrom ){
 51118           put4byte(pCell, iTo);
 51119           break;
 51124     if( i==nCell ){
 51125       if( eType!=PTRMAP_BTREE || 
 51126           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
 51127         return SQLITE_CORRUPT_BKPT;
 51129       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
 51132     pPage->isInit = isInitOrig;
 51134   return SQLITE_OK;
 51138 /*
 51139 ** Move the open database page pDbPage to location iFreePage in the 
 51140 ** database. The pDbPage reference remains valid.
 51141 **
 51142 ** The isCommit flag indicates that there is no need to remember that
 51143 ** the journal needs to be sync()ed before database page pDbPage->pgno 
 51144 ** can be written to. The caller has already promised not to write to that
 51145 ** page.
 51146 */
 51147 static int relocatePage(
 51148   BtShared *pBt,           /* Btree */
 51149   MemPage *pDbPage,        /* Open page to move */
 51150   u8 eType,                /* Pointer map 'type' entry for pDbPage */
 51151   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
 51152   Pgno iFreePage,          /* The location to move pDbPage to */
 51153   int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
 51154 ){
 51155   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
 51156   Pgno iDbPage = pDbPage->pgno;
 51157   Pager *pPager = pBt->pPager;
 51158   int rc;
 51160   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
 51161       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
 51162   assert( sqlite3_mutex_held(pBt->mutex) );
 51163   assert( pDbPage->pBt==pBt );
 51165   /* Move page iDbPage from its current location to page number iFreePage */
 51166   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
 51167       iDbPage, iFreePage, iPtrPage, eType));
 51168   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
 51169   if( rc!=SQLITE_OK ){
 51170     return rc;
 51172   pDbPage->pgno = iFreePage;
 51174   /* If pDbPage was a btree-page, then it may have child pages and/or cells
 51175   ** that point to overflow pages. The pointer map entries for all these
 51176   ** pages need to be changed.
 51177   **
 51178   ** If pDbPage is an overflow page, then the first 4 bytes may store a
 51179   ** pointer to a subsequent overflow page. If this is the case, then
 51180   ** the pointer map needs to be updated for the subsequent overflow page.
 51181   */
 51182   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
 51183     rc = setChildPtrmaps(pDbPage);
 51184     if( rc!=SQLITE_OK ){
 51185       return rc;
 51187   }else{
 51188     Pgno nextOvfl = get4byte(pDbPage->aData);
 51189     if( nextOvfl!=0 ){
 51190       ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
 51191       if( rc!=SQLITE_OK ){
 51192         return rc;
 51197   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
 51198   ** that it points at iFreePage. Also fix the pointer map entry for
 51199   ** iPtrPage.
 51200   */
 51201   if( eType!=PTRMAP_ROOTPAGE ){
 51202     rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
 51203     if( rc!=SQLITE_OK ){
 51204       return rc;
 51206     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
 51207     if( rc!=SQLITE_OK ){
 51208       releasePage(pPtrPage);
 51209       return rc;
 51211     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
 51212     releasePage(pPtrPage);
 51213     if( rc==SQLITE_OK ){
 51214       ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
 51217   return rc;
 51220 /* Forward declaration required by incrVacuumStep(). */
 51221 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
 51223 /*
 51224 ** Perform a single step of an incremental-vacuum. If successful,
 51225 ** return SQLITE_OK. If there is no work to do (and therefore no
 51226 ** point in calling this function again), return SQLITE_DONE.
 51227 **
 51228 ** More specificly, this function attempts to re-organize the 
 51229 ** database so that the last page of the file currently in use
 51230 ** is no longer in use.
 51231 **
 51232 ** If the nFin parameter is non-zero, this function assumes
 51233 ** that the caller will keep calling incrVacuumStep() until
 51234 ** it returns SQLITE_DONE or an error, and that nFin is the
 51235 ** number of pages the database file will contain after this 
 51236 ** process is complete.  If nFin is zero, it is assumed that
 51237 ** incrVacuumStep() will be called a finite amount of times
 51238 ** which may or may not empty the freelist.  A full autovacuum
 51239 ** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
 51240 */
 51241 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
 51242   Pgno nFreeList;           /* Number of pages still on the free-list */
 51243   int rc;
 51245   assert( sqlite3_mutex_held(pBt->mutex) );
 51246   assert( iLastPg>nFin );
 51248   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
 51249     u8 eType;
 51250     Pgno iPtrPage;
 51252     nFreeList = get4byte(&pBt->pPage1->aData[36]);
 51253     if( nFreeList==0 ){
 51254       return SQLITE_DONE;
 51257     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
 51258     if( rc!=SQLITE_OK ){
 51259       return rc;
 51261     if( eType==PTRMAP_ROOTPAGE ){
 51262       return SQLITE_CORRUPT_BKPT;
 51265     if( eType==PTRMAP_FREEPAGE ){
 51266       if( nFin==0 ){
 51267         /* Remove the page from the files free-list. This is not required
 51268         ** if nFin is non-zero. In that case, the free-list will be
 51269         ** truncated to zero after this function returns, so it doesn't 
 51270         ** matter if it still contains some garbage entries.
 51271         */
 51272         Pgno iFreePg;
 51273         MemPage *pFreePg;
 51274         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
 51275         if( rc!=SQLITE_OK ){
 51276           return rc;
 51278         assert( iFreePg==iLastPg );
 51279         releasePage(pFreePg);
 51281     } else {
 51282       Pgno iFreePg;             /* Index of free page to move pLastPg to */
 51283       MemPage *pLastPg;
 51285       rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
 51286       if( rc!=SQLITE_OK ){
 51287         return rc;
 51290       /* If nFin is zero, this loop runs exactly once and page pLastPg
 51291       ** is swapped with the first free page pulled off the free list.
 51292       **
 51293       ** On the other hand, if nFin is greater than zero, then keep
 51294       ** looping until a free-page located within the first nFin pages
 51295       ** of the file is found.
 51296       */
 51297       do {
 51298         MemPage *pFreePg;
 51299         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
 51300         if( rc!=SQLITE_OK ){
 51301           releasePage(pLastPg);
 51302           return rc;
 51304         releasePage(pFreePg);
 51305       }while( nFin!=0 && iFreePg>nFin );
 51306       assert( iFreePg<iLastPg );
 51308       rc = sqlite3PagerWrite(pLastPg->pDbPage);
 51309       if( rc==SQLITE_OK ){
 51310         rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
 51312       releasePage(pLastPg);
 51313       if( rc!=SQLITE_OK ){
 51314         return rc;
 51319   if( nFin==0 ){
 51320     iLastPg--;
 51321     while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
 51322       if( PTRMAP_ISPAGE(pBt, iLastPg) ){
 51323         MemPage *pPg;
 51324         rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
 51325         if( rc!=SQLITE_OK ){
 51326           return rc;
 51328         rc = sqlite3PagerWrite(pPg->pDbPage);
 51329         releasePage(pPg);
 51330         if( rc!=SQLITE_OK ){
 51331           return rc;
 51334       iLastPg--;
 51336     sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
 51337     pBt->nPage = iLastPg;
 51339   return SQLITE_OK;
 51342 /*
 51343 ** A write-transaction must be opened before calling this function.
 51344 ** It performs a single unit of work towards an incremental vacuum.
 51345 **
 51346 ** If the incremental vacuum is finished after this function has run,
 51347 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
 51348 ** SQLITE_OK is returned. Otherwise an SQLite error code. 
 51349 */
 51350 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
 51351   int rc;
 51352   BtShared *pBt = p->pBt;
 51354   sqlite3BtreeEnter(p);
 51355   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
 51356   if( !pBt->autoVacuum ){
 51357     rc = SQLITE_DONE;
 51358   }else{
 51359     invalidateAllOverflowCache(pBt);
 51360     rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
 51361     if( rc==SQLITE_OK ){
 51362       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 51363       put4byte(&pBt->pPage1->aData[28], pBt->nPage);
 51366   sqlite3BtreeLeave(p);
 51367   return rc;
 51370 /*
 51371 ** This routine is called prior to sqlite3PagerCommit when a transaction
 51372 ** is commited for an auto-vacuum database.
 51373 **
 51374 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
 51375 ** the database file should be truncated to during the commit process. 
 51376 ** i.e. the database has been reorganized so that only the first *pnTrunc
 51377 ** pages are in use.
 51378 */
 51379 static int autoVacuumCommit(BtShared *pBt){
 51380   int rc = SQLITE_OK;
 51381   Pager *pPager = pBt->pPager;
 51382   VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
 51384   assert( sqlite3_mutex_held(pBt->mutex) );
 51385   invalidateAllOverflowCache(pBt);
 51386   assert(pBt->autoVacuum);
 51387   if( !pBt->incrVacuum ){
 51388     Pgno nFin;         /* Number of pages in database after autovacuuming */
 51389     Pgno nFree;        /* Number of pages on the freelist initially */
 51390     Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
 51391     Pgno iFree;        /* The next page to be freed */
 51392     int nEntry;        /* Number of entries on one ptrmap page */
 51393     Pgno nOrig;        /* Database size before freeing */
 51395     nOrig = btreePagecount(pBt);
 51396     if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
 51397       /* It is not possible to create a database for which the final page
 51398       ** is either a pointer-map page or the pending-byte page. If one
 51399       ** is encountered, this indicates corruption.
 51400       */
 51401       return SQLITE_CORRUPT_BKPT;
 51404     nFree = get4byte(&pBt->pPage1->aData[36]);
 51405     nEntry = pBt->usableSize/5;
 51406     nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
 51407     nFin = nOrig - nFree - nPtrmap;
 51408     if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
 51409       nFin--;
 51411     while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
 51412       nFin--;
 51414     if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
 51416     for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
 51417       rc = incrVacuumStep(pBt, nFin, iFree);
 51419     if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
 51420       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 51421       put4byte(&pBt->pPage1->aData[32], 0);
 51422       put4byte(&pBt->pPage1->aData[36], 0);
 51423       put4byte(&pBt->pPage1->aData[28], nFin);
 51424       sqlite3PagerTruncateImage(pBt->pPager, nFin);
 51425       pBt->nPage = nFin;
 51427     if( rc!=SQLITE_OK ){
 51428       sqlite3PagerRollback(pPager);
 51432   assert( nRef==sqlite3PagerRefcount(pPager) );
 51433   return rc;
 51436 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
 51437 # define setChildPtrmaps(x) SQLITE_OK
 51438 #endif
 51440 /*
 51441 ** This routine does the first phase of a two-phase commit.  This routine
 51442 ** causes a rollback journal to be created (if it does not already exist)
 51443 ** and populated with enough information so that if a power loss occurs
 51444 ** the database can be restored to its original state by playing back
 51445 ** the journal.  Then the contents of the journal are flushed out to
 51446 ** the disk.  After the journal is safely on oxide, the changes to the
 51447 ** database are written into the database file and flushed to oxide.
 51448 ** At the end of this call, the rollback journal still exists on the
 51449 ** disk and we are still holding all locks, so the transaction has not
 51450 ** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
 51451 ** commit process.
 51452 **
 51453 ** This call is a no-op if no write-transaction is currently active on pBt.
 51454 **
 51455 ** Otherwise, sync the database file for the btree pBt. zMaster points to
 51456 ** the name of a master journal file that should be written into the
 51457 ** individual journal file, or is NULL, indicating no master journal file 
 51458 ** (single database transaction).
 51459 **
 51460 ** When this is called, the master journal should already have been
 51461 ** created, populated with this journal pointer and synced to disk.
 51462 **
 51463 ** Once this is routine has returned, the only thing required to commit
 51464 ** the write-transaction for this database file is to delete the journal.
 51465 */
 51466 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
 51467   int rc = SQLITE_OK;
 51468   if( p->inTrans==TRANS_WRITE ){
 51469     BtShared *pBt = p->pBt;
 51470     sqlite3BtreeEnter(p);
 51471 #ifndef SQLITE_OMIT_AUTOVACUUM
 51472     if( pBt->autoVacuum ){
 51473       rc = autoVacuumCommit(pBt);
 51474       if( rc!=SQLITE_OK ){
 51475         sqlite3BtreeLeave(p);
 51476         return rc;
 51479 #endif
 51480     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
 51481     sqlite3BtreeLeave(p);
 51483   return rc;
 51486 /*
 51487 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
 51488 ** at the conclusion of a transaction.
 51489 */
 51490 static void btreeEndTransaction(Btree *p){
 51491   BtShared *pBt = p->pBt;
 51492   assert( sqlite3BtreeHoldsMutex(p) );
 51494   btreeClearHasContent(pBt);
 51495   if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
 51496     /* If there are other active statements that belong to this database
 51497     ** handle, downgrade to a read-only transaction. The other statements
 51498     ** may still be reading from the database.  */
 51499     downgradeAllSharedCacheTableLocks(p);
 51500     p->inTrans = TRANS_READ;
 51501   }else{
 51502     /* If the handle had any kind of transaction open, decrement the 
 51503     ** transaction count of the shared btree. If the transaction count 
 51504     ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
 51505     ** call below will unlock the pager.  */
 51506     if( p->inTrans!=TRANS_NONE ){
 51507       clearAllSharedCacheTableLocks(p);
 51508       pBt->nTransaction--;
 51509       if( 0==pBt->nTransaction ){
 51510         pBt->inTransaction = TRANS_NONE;
 51514     /* Set the current transaction state to TRANS_NONE and unlock the 
 51515     ** pager if this call closed the only read or write transaction.  */
 51516     p->inTrans = TRANS_NONE;
 51517     unlockBtreeIfUnused(pBt);
 51520   btreeIntegrity(p);
 51523 /*
 51524 ** Commit the transaction currently in progress.
 51525 **
 51526 ** This routine implements the second phase of a 2-phase commit.  The
 51527 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
 51528 ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
 51529 ** routine did all the work of writing information out to disk and flushing the
 51530 ** contents so that they are written onto the disk platter.  All this
 51531 ** routine has to do is delete or truncate or zero the header in the
 51532 ** the rollback journal (which causes the transaction to commit) and
 51533 ** drop locks.
 51534 **
 51535 ** Normally, if an error occurs while the pager layer is attempting to 
 51536 ** finalize the underlying journal file, this function returns an error and
 51537 ** the upper layer will attempt a rollback. However, if the second argument
 51538 ** is non-zero then this b-tree transaction is part of a multi-file 
 51539 ** transaction. In this case, the transaction has already been committed 
 51540 ** (by deleting a master journal file) and the caller will ignore this 
 51541 ** functions return code. So, even if an error occurs in the pager layer,
 51542 ** reset the b-tree objects internal state to indicate that the write
 51543 ** transaction has been closed. This is quite safe, as the pager will have
 51544 ** transitioned to the error state.
 51545 **
 51546 ** This will release the write lock on the database file.  If there
 51547 ** are no active cursors, it also releases the read lock.
 51548 */
 51549 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
 51551   if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
 51552   sqlite3BtreeEnter(p);
 51553   btreeIntegrity(p);
 51555   /* If the handle has a write-transaction open, commit the shared-btrees 
 51556   ** transaction and set the shared state to TRANS_READ.
 51557   */
 51558   if( p->inTrans==TRANS_WRITE ){
 51559     int rc;
 51560     BtShared *pBt = p->pBt;
 51561     assert( pBt->inTransaction==TRANS_WRITE );
 51562     assert( pBt->nTransaction>0 );
 51563     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
 51564     if( rc!=SQLITE_OK && bCleanup==0 ){
 51565       sqlite3BtreeLeave(p);
 51566       return rc;
 51568     pBt->inTransaction = TRANS_READ;
 51571   btreeEndTransaction(p);
 51572   sqlite3BtreeLeave(p);
 51573   return SQLITE_OK;
 51576 /*
 51577 ** Do both phases of a commit.
 51578 */
 51579 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
 51580   int rc;
 51581   sqlite3BtreeEnter(p);
 51582   rc = sqlite3BtreeCommitPhaseOne(p, 0);
 51583   if( rc==SQLITE_OK ){
 51584     rc = sqlite3BtreeCommitPhaseTwo(p, 0);
 51586   sqlite3BtreeLeave(p);
 51587   return rc;
 51590 #ifndef NDEBUG
 51591 /*
 51592 ** Return the number of write-cursors open on this handle. This is for use
 51593 ** in assert() expressions, so it is only compiled if NDEBUG is not
 51594 ** defined.
 51595 **
 51596 ** For the purposes of this routine, a write-cursor is any cursor that
 51597 ** is capable of writing to the databse.  That means the cursor was
 51598 ** originally opened for writing and the cursor has not be disabled
 51599 ** by having its state changed to CURSOR_FAULT.
 51600 */
 51601 static int countWriteCursors(BtShared *pBt){
 51602   BtCursor *pCur;
 51603   int r = 0;
 51604   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
 51605     if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; 
 51607   return r;
 51609 #endif
 51611 /*
 51612 ** This routine sets the state to CURSOR_FAULT and the error
 51613 ** code to errCode for every cursor on BtShared that pBtree
 51614 ** references.
 51615 **
 51616 ** Every cursor is tripped, including cursors that belong
 51617 ** to other database connections that happen to be sharing
 51618 ** the cache with pBtree.
 51619 **
 51620 ** This routine gets called when a rollback occurs.
 51621 ** All cursors using the same cache must be tripped
 51622 ** to prevent them from trying to use the btree after
 51623 ** the rollback.  The rollback may have deleted tables
 51624 ** or moved root pages, so it is not sufficient to
 51625 ** save the state of the cursor.  The cursor must be
 51626 ** invalidated.
 51627 */
 51628 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
 51629   BtCursor *p;
 51630   if( pBtree==0 ) return;
 51631   sqlite3BtreeEnter(pBtree);
 51632   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 51633     int i;
 51634     sqlite3BtreeClearCursor(p);
 51635     p->eState = CURSOR_FAULT;
 51636     p->skipNext = errCode;
 51637     for(i=0; i<=p->iPage; i++){
 51638       releasePage(p->apPage[i]);
 51639       p->apPage[i] = 0;
 51642   sqlite3BtreeLeave(pBtree);
 51645 /*
 51646 ** Rollback the transaction in progress.  All cursors will be
 51647 ** invalided by this operation.  Any attempt to use a cursor
 51648 ** that was open at the beginning of this operation will result
 51649 ** in an error.
 51650 **
 51651 ** This will release the write lock on the database file.  If there
 51652 ** are no active cursors, it also releases the read lock.
 51653 */
 51654 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){
 51655   int rc;
 51656   BtShared *pBt = p->pBt;
 51657   MemPage *pPage1;
 51659   sqlite3BtreeEnter(p);
 51660   if( tripCode==SQLITE_OK ){
 51661     rc = tripCode = saveAllCursors(pBt, 0, 0);
 51662   }else{
 51663     rc = SQLITE_OK;
 51665   if( tripCode ){
 51666     sqlite3BtreeTripAllCursors(p, tripCode);
 51668   btreeIntegrity(p);
 51670   if( p->inTrans==TRANS_WRITE ){
 51671     int rc2;
 51673     assert( TRANS_WRITE==pBt->inTransaction );
 51674     rc2 = sqlite3PagerRollback(pBt->pPager);
 51675     if( rc2!=SQLITE_OK ){
 51676       rc = rc2;
 51679     /* The rollback may have destroyed the pPage1->aData value.  So
 51680     ** call btreeGetPage() on page 1 again to make
 51681     ** sure pPage1->aData is set correctly. */
 51682     if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
 51683       int nPage = get4byte(28+(u8*)pPage1->aData);
 51684       testcase( nPage==0 );
 51685       if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
 51686       testcase( pBt->nPage!=nPage );
 51687       pBt->nPage = nPage;
 51688       releasePage(pPage1);
 51690     assert( countWriteCursors(pBt)==0 );
 51691     pBt->inTransaction = TRANS_READ;
 51694   btreeEndTransaction(p);
 51695   sqlite3BtreeLeave(p);
 51696   return rc;
 51699 /*
 51700 ** Start a statement subtransaction. The subtransaction can can be rolled
 51701 ** back independently of the main transaction. You must start a transaction 
 51702 ** before starting a subtransaction. The subtransaction is ended automatically 
 51703 ** if the main transaction commits or rolls back.
 51704 **
 51705 ** Statement subtransactions are used around individual SQL statements
 51706 ** that are contained within a BEGIN...COMMIT block.  If a constraint
 51707 ** error occurs within the statement, the effect of that one statement
 51708 ** can be rolled back without having to rollback the entire transaction.
 51709 **
 51710 ** A statement sub-transaction is implemented as an anonymous savepoint. The
 51711 ** value passed as the second parameter is the total number of savepoints,
 51712 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
 51713 ** are no active savepoints and no other statement-transactions open,
 51714 ** iStatement is 1. This anonymous savepoint can be released or rolled back
 51715 ** using the sqlite3BtreeSavepoint() function.
 51716 */
 51717 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
 51718   int rc;
 51719   BtShared *pBt = p->pBt;
 51720   sqlite3BtreeEnter(p);
 51721   assert( p->inTrans==TRANS_WRITE );
 51722   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 51723   assert( iStatement>0 );
 51724   assert( iStatement>p->db->nSavepoint );
 51725   assert( pBt->inTransaction==TRANS_WRITE );
 51726   /* At the pager level, a statement transaction is a savepoint with
 51727   ** an index greater than all savepoints created explicitly using
 51728   ** SQL statements. It is illegal to open, release or rollback any
 51729   ** such savepoints while the statement transaction savepoint is active.
 51730   */
 51731   rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
 51732   sqlite3BtreeLeave(p);
 51733   return rc;
 51736 /*
 51737 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
 51738 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
 51739 ** savepoint identified by parameter iSavepoint, depending on the value 
 51740 ** of op.
 51741 **
 51742 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
 51743 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 
 51744 ** contents of the entire transaction are rolled back. This is different
 51745 ** from a normal transaction rollback, as no locks are released and the
 51746 ** transaction remains open.
 51747 */
 51748 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
 51749   int rc = SQLITE_OK;
 51750   if( p && p->inTrans==TRANS_WRITE ){
 51751     BtShared *pBt = p->pBt;
 51752     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 51753     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
 51754     sqlite3BtreeEnter(p);
 51755     rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
 51756     if( rc==SQLITE_OK ){
 51757       if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
 51758         pBt->nPage = 0;
 51760       rc = newDatabase(pBt);
 51761       pBt->nPage = get4byte(28 + pBt->pPage1->aData);
 51763       /* The database size was written into the offset 28 of the header
 51764       ** when the transaction started, so we know that the value at offset
 51765       ** 28 is nonzero. */
 51766       assert( pBt->nPage>0 );
 51768     sqlite3BtreeLeave(p);
 51770   return rc;
 51773 /*
 51774 ** Create a new cursor for the BTree whose root is on the page
 51775 ** iTable. If a read-only cursor is requested, it is assumed that
 51776 ** the caller already has at least a read-only transaction open
 51777 ** on the database already. If a write-cursor is requested, then
 51778 ** the caller is assumed to have an open write transaction.
 51779 **
 51780 ** If wrFlag==0, then the cursor can only be used for reading.
 51781 ** If wrFlag==1, then the cursor can be used for reading or for
 51782 ** writing if other conditions for writing are also met.  These
 51783 ** are the conditions that must be met in order for writing to
 51784 ** be allowed:
 51785 **
 51786 ** 1:  The cursor must have been opened with wrFlag==1
 51787 **
 51788 ** 2:  Other database connections that share the same pager cache
 51789 **     but which are not in the READ_UNCOMMITTED state may not have
 51790 **     cursors open with wrFlag==0 on the same table.  Otherwise
 51791 **     the changes made by this write cursor would be visible to
 51792 **     the read cursors in the other database connection.
 51793 **
 51794 ** 3:  The database must be writable (not on read-only media)
 51795 **
 51796 ** 4:  There must be an active transaction.
 51797 **
 51798 ** No checking is done to make sure that page iTable really is the
 51799 ** root page of a b-tree.  If it is not, then the cursor acquired
 51800 ** will not work correctly.
 51801 **
 51802 ** It is assumed that the sqlite3BtreeCursorZero() has been called
 51803 ** on pCur to initialize the memory space prior to invoking this routine.
 51804 */
 51805 static int btreeCursor(
 51806   Btree *p,                              /* The btree */
 51807   int iTable,                            /* Root page of table to open */
 51808   int wrFlag,                            /* 1 to write. 0 read-only */
 51809   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
 51810   BtCursor *pCur                         /* Space for new cursor */
 51811 ){
 51812   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
 51814   assert( sqlite3BtreeHoldsMutex(p) );
 51815   assert( wrFlag==0 || wrFlag==1 );
 51817   /* The following assert statements verify that if this is a sharable 
 51818   ** b-tree database, the connection is holding the required table locks, 
 51819   ** and that no other connection has any open cursor that conflicts with 
 51820   ** this lock.  */
 51821   assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
 51822   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
 51824   /* Assert that the caller has opened the required transaction. */
 51825   assert( p->inTrans>TRANS_NONE );
 51826   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
 51827   assert( pBt->pPage1 && pBt->pPage1->aData );
 51829   if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
 51830     return SQLITE_READONLY;
 51832   if( iTable==1 && btreePagecount(pBt)==0 ){
 51833     assert( wrFlag==0 );
 51834     iTable = 0;
 51837   /* Now that no other errors can occur, finish filling in the BtCursor
 51838   ** variables and link the cursor into the BtShared list.  */
 51839   pCur->pgnoRoot = (Pgno)iTable;
 51840   pCur->iPage = -1;
 51841   pCur->pKeyInfo = pKeyInfo;
 51842   pCur->pBtree = p;
 51843   pCur->pBt = pBt;
 51844   pCur->wrFlag = (u8)wrFlag;
 51845   pCur->pNext = pBt->pCursor;
 51846   if( pCur->pNext ){
 51847     pCur->pNext->pPrev = pCur;
 51849   pBt->pCursor = pCur;
 51850   pCur->eState = CURSOR_INVALID;
 51851   pCur->cachedRowid = 0;
 51852   return SQLITE_OK;
 51854 SQLITE_PRIVATE int sqlite3BtreeCursor(
 51855   Btree *p,                                   /* The btree */
 51856   int iTable,                                 /* Root page of table to open */
 51857   int wrFlag,                                 /* 1 to write. 0 read-only */
 51858   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
 51859   BtCursor *pCur                              /* Write new cursor here */
 51860 ){
 51861   int rc;
 51862   sqlite3BtreeEnter(p);
 51863   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
 51864   sqlite3BtreeLeave(p);
 51865   return rc;
 51868 /*
 51869 ** Return the size of a BtCursor object in bytes.
 51870 **
 51871 ** This interfaces is needed so that users of cursors can preallocate
 51872 ** sufficient storage to hold a cursor.  The BtCursor object is opaque
 51873 ** to users so they cannot do the sizeof() themselves - they must call
 51874 ** this routine.
 51875 */
 51876 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
 51877   return ROUND8(sizeof(BtCursor));
 51880 /*
 51881 ** Initialize memory that will be converted into a BtCursor object.
 51882 **
 51883 ** The simple approach here would be to memset() the entire object
 51884 ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
 51885 ** do not need to be zeroed and they are large, so we can save a lot
 51886 ** of run-time by skipping the initialization of those elements.
 51887 */
 51888 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
 51889   memset(p, 0, offsetof(BtCursor, iPage));
 51892 /*
 51893 ** Set the cached rowid value of every cursor in the same database file
 51894 ** as pCur and having the same root page number as pCur.  The value is
 51895 ** set to iRowid.
 51896 **
 51897 ** Only positive rowid values are considered valid for this cache.
 51898 ** The cache is initialized to zero, indicating an invalid cache.
 51899 ** A btree will work fine with zero or negative rowids.  We just cannot
 51900 ** cache zero or negative rowids, which means tables that use zero or
 51901 ** negative rowids might run a little slower.  But in practice, zero
 51902 ** or negative rowids are very uncommon so this should not be a problem.
 51903 */
 51904 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
 51905   BtCursor *p;
 51906   for(p=pCur->pBt->pCursor; p; p=p->pNext){
 51907     if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
 51909   assert( pCur->cachedRowid==iRowid );
 51912 /*
 51913 ** Return the cached rowid for the given cursor.  A negative or zero
 51914 ** return value indicates that the rowid cache is invalid and should be
 51915 ** ignored.  If the rowid cache has never before been set, then a
 51916 ** zero is returned.
 51917 */
 51918 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
 51919   return pCur->cachedRowid;
 51922 /*
 51923 ** Close a cursor.  The read lock on the database file is released
 51924 ** when the last cursor is closed.
 51925 */
 51926 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
 51927   Btree *pBtree = pCur->pBtree;
 51928   if( pBtree ){
 51929     int i;
 51930     BtShared *pBt = pCur->pBt;
 51931     sqlite3BtreeEnter(pBtree);
 51932     sqlite3BtreeClearCursor(pCur);
 51933     if( pCur->pPrev ){
 51934       pCur->pPrev->pNext = pCur->pNext;
 51935     }else{
 51936       pBt->pCursor = pCur->pNext;
 51938     if( pCur->pNext ){
 51939       pCur->pNext->pPrev = pCur->pPrev;
 51941     for(i=0; i<=pCur->iPage; i++){
 51942       releasePage(pCur->apPage[i]);
 51944     unlockBtreeIfUnused(pBt);
 51945     invalidateOverflowCache(pCur);
 51946     /* sqlite3_free(pCur); */
 51947     sqlite3BtreeLeave(pBtree);
 51949   return SQLITE_OK;
 51952 /*
 51953 ** Make sure the BtCursor* given in the argument has a valid
 51954 ** BtCursor.info structure.  If it is not already valid, call
 51955 ** btreeParseCell() to fill it in.
 51956 **
 51957 ** BtCursor.info is a cache of the information in the current cell.
 51958 ** Using this cache reduces the number of calls to btreeParseCell().
 51959 **
 51960 ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
 51961 ** compiler to crash when getCellInfo() is implemented as a macro.
 51962 ** But there is a measureable speed advantage to using the macro on gcc
 51963 ** (when less compiler optimizations like -Os or -O0 are used and the
 51964 ** compiler is not doing agressive inlining.)  So we use a real function
 51965 ** for MSVC and a macro for everything else.  Ticket #2457.
 51966 */
 51967 #ifndef NDEBUG
 51968   static void assertCellInfo(BtCursor *pCur){
 51969     CellInfo info;
 51970     int iPage = pCur->iPage;
 51971     memset(&info, 0, sizeof(info));
 51972     btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
 51973     assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
 51975 #else
 51976   #define assertCellInfo(x)
 51977 #endif
 51978 #ifdef _MSC_VER
 51979   /* Use a real function in MSVC to work around bugs in that compiler. */
 51980   static void getCellInfo(BtCursor *pCur){
 51981     if( pCur->info.nSize==0 ){
 51982       int iPage = pCur->iPage;
 51983       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
 51984       pCur->validNKey = 1;
 51985     }else{
 51986       assertCellInfo(pCur);
 51989 #else /* if not _MSC_VER */
 51990   /* Use a macro in all other compilers so that the function is inlined */
 51991 #define getCellInfo(pCur)                                                      \
 51992   if( pCur->info.nSize==0 ){                                                   \
 51993     int iPage = pCur->iPage;                                                   \
 51994     btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
 51995     pCur->validNKey = 1;                                                       \
 51996   }else{                                                                       \
 51997     assertCellInfo(pCur);                                                      \
 51999 #endif /* _MSC_VER */
 52001 #ifndef NDEBUG  /* The next routine used only within assert() statements */
 52002 /*
 52003 ** Return true if the given BtCursor is valid.  A valid cursor is one
 52004 ** that is currently pointing to a row in a (non-empty) table.
 52005 ** This is a verification routine is used only within assert() statements.
 52006 */
 52007 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
 52008   return pCur && pCur->eState==CURSOR_VALID;
 52010 #endif /* NDEBUG */
 52012 /*
 52013 ** Set *pSize to the size of the buffer needed to hold the value of
 52014 ** the key for the current entry.  If the cursor is not pointing
 52015 ** to a valid entry, *pSize is set to 0. 
 52016 **
 52017 ** For a table with the INTKEY flag set, this routine returns the key
 52018 ** itself, not the number of bytes in the key.
 52019 **
 52020 ** The caller must position the cursor prior to invoking this routine.
 52021 ** 
 52022 ** This routine cannot fail.  It always returns SQLITE_OK.  
 52023 */
 52024 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
 52025   assert( cursorHoldsMutex(pCur) );
 52026   assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
 52027   if( pCur->eState!=CURSOR_VALID ){
 52028     *pSize = 0;
 52029   }else{
 52030     getCellInfo(pCur);
 52031     *pSize = pCur->info.nKey;
 52033   return SQLITE_OK;
 52036 /*
 52037 ** Set *pSize to the number of bytes of data in the entry the
 52038 ** cursor currently points to.
 52039 **
 52040 ** The caller must guarantee that the cursor is pointing to a non-NULL
 52041 ** valid entry.  In other words, the calling procedure must guarantee
 52042 ** that the cursor has Cursor.eState==CURSOR_VALID.
 52043 **
 52044 ** Failure is not possible.  This function always returns SQLITE_OK.
 52045 ** It might just as well be a procedure (returning void) but we continue
 52046 ** to return an integer result code for historical reasons.
 52047 */
 52048 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
 52049   assert( cursorHoldsMutex(pCur) );
 52050   assert( pCur->eState==CURSOR_VALID );
 52051   getCellInfo(pCur);
 52052   *pSize = pCur->info.nData;
 52053   return SQLITE_OK;
 52056 /*
 52057 ** Given the page number of an overflow page in the database (parameter
 52058 ** ovfl), this function finds the page number of the next page in the 
 52059 ** linked list of overflow pages. If possible, it uses the auto-vacuum
 52060 ** pointer-map data instead of reading the content of page ovfl to do so. 
 52061 **
 52062 ** If an error occurs an SQLite error code is returned. Otherwise:
 52063 **
 52064 ** The page number of the next overflow page in the linked list is 
 52065 ** written to *pPgnoNext. If page ovfl is the last page in its linked 
 52066 ** list, *pPgnoNext is set to zero. 
 52067 **
 52068 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
 52069 ** to page number pOvfl was obtained, then *ppPage is set to point to that
 52070 ** reference. It is the responsibility of the caller to call releasePage()
 52071 ** on *ppPage to free the reference. In no reference was obtained (because
 52072 ** the pointer-map was used to obtain the value for *pPgnoNext), then
 52073 ** *ppPage is set to zero.
 52074 */
 52075 static int getOverflowPage(
 52076   BtShared *pBt,               /* The database file */
 52077   Pgno ovfl,                   /* Current overflow page number */
 52078   MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
 52079   Pgno *pPgnoNext              /* OUT: Next overflow page number */
 52080 ){
 52081   Pgno next = 0;
 52082   MemPage *pPage = 0;
 52083   int rc = SQLITE_OK;
 52085   assert( sqlite3_mutex_held(pBt->mutex) );
 52086   assert(pPgnoNext);
 52088 #ifndef SQLITE_OMIT_AUTOVACUUM
 52089   /* Try to find the next page in the overflow list using the
 52090   ** autovacuum pointer-map pages. Guess that the next page in 
 52091   ** the overflow list is page number (ovfl+1). If that guess turns 
 52092   ** out to be wrong, fall back to loading the data of page 
 52093   ** number ovfl to determine the next page number.
 52094   */
 52095   if( pBt->autoVacuum ){
 52096     Pgno pgno;
 52097     Pgno iGuess = ovfl+1;
 52098     u8 eType;
 52100     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
 52101       iGuess++;
 52104     if( iGuess<=btreePagecount(pBt) ){
 52105       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
 52106       if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
 52107         next = iGuess;
 52108         rc = SQLITE_DONE;
 52112 #endif
 52114   assert( next==0 || rc==SQLITE_DONE );
 52115   if( rc==SQLITE_OK ){
 52116     rc = btreeGetPage(pBt, ovfl, &pPage, 0);
 52117     assert( rc==SQLITE_OK || pPage==0 );
 52118     if( rc==SQLITE_OK ){
 52119       next = get4byte(pPage->aData);
 52123   *pPgnoNext = next;
 52124   if( ppPage ){
 52125     *ppPage = pPage;
 52126   }else{
 52127     releasePage(pPage);
 52129   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
 52132 /*
 52133 ** Copy data from a buffer to a page, or from a page to a buffer.
 52134 **
 52135 ** pPayload is a pointer to data stored on database page pDbPage.
 52136 ** If argument eOp is false, then nByte bytes of data are copied
 52137 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
 52138 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
 52139 ** of data are copied from the buffer pBuf to pPayload.
 52140 **
 52141 ** SQLITE_OK is returned on success, otherwise an error code.
 52142 */
 52143 static int copyPayload(
 52144   void *pPayload,           /* Pointer to page data */
 52145   void *pBuf,               /* Pointer to buffer */
 52146   int nByte,                /* Number of bytes to copy */
 52147   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
 52148   DbPage *pDbPage           /* Page containing pPayload */
 52149 ){
 52150   if( eOp ){
 52151     /* Copy data from buffer to page (a write operation) */
 52152     int rc = sqlite3PagerWrite(pDbPage);
 52153     if( rc!=SQLITE_OK ){
 52154       return rc;
 52156     memcpy(pPayload, pBuf, nByte);
 52157   }else{
 52158     /* Copy data from page to buffer (a read operation) */
 52159     memcpy(pBuf, pPayload, nByte);
 52161   return SQLITE_OK;
 52164 /*
 52165 ** This function is used to read or overwrite payload information
 52166 ** for the entry that the pCur cursor is pointing to. If the eOp
 52167 ** parameter is 0, this is a read operation (data copied into
 52168 ** buffer pBuf). If it is non-zero, a write (data copied from
 52169 ** buffer pBuf).
 52170 **
 52171 ** A total of "amt" bytes are read or written beginning at "offset".
 52172 ** Data is read to or from the buffer pBuf.
 52173 **
 52174 ** The content being read or written might appear on the main page
 52175 ** or be scattered out on multiple overflow pages.
 52176 **
 52177 ** If the BtCursor.isIncrblobHandle flag is set, and the current
 52178 ** cursor entry uses one or more overflow pages, this function
 52179 ** allocates space for and lazily popluates the overflow page-list 
 52180 ** cache array (BtCursor.aOverflow). Subsequent calls use this
 52181 ** cache to make seeking to the supplied offset more efficient.
 52182 **
 52183 ** Once an overflow page-list cache has been allocated, it may be
 52184 ** invalidated if some other cursor writes to the same table, or if
 52185 ** the cursor is moved to a different row. Additionally, in auto-vacuum
 52186 ** mode, the following events may invalidate an overflow page-list cache.
 52187 **
 52188 **   * An incremental vacuum,
 52189 **   * A commit in auto_vacuum="full" mode,
 52190 **   * Creating a table (may require moving an overflow page).
 52191 */
 52192 static int accessPayload(
 52193   BtCursor *pCur,      /* Cursor pointing to entry to read from */
 52194   u32 offset,          /* Begin reading this far into payload */
 52195   u32 amt,             /* Read this many bytes */
 52196   unsigned char *pBuf, /* Write the bytes into this buffer */ 
 52197   int eOp              /* zero to read. non-zero to write. */
 52198 ){
 52199   unsigned char *aPayload;
 52200   int rc = SQLITE_OK;
 52201   u32 nKey;
 52202   int iIdx = 0;
 52203   MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
 52204   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
 52206   assert( pPage );
 52207   assert( pCur->eState==CURSOR_VALID );
 52208   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 52209   assert( cursorHoldsMutex(pCur) );
 52211   getCellInfo(pCur);
 52212   aPayload = pCur->info.pCell + pCur->info.nHeader;
 52213   nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
 52215   if( NEVER(offset+amt > nKey+pCur->info.nData) 
 52216    || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
 52217   ){
 52218     /* Trying to read or write past the end of the data is an error */
 52219     return SQLITE_CORRUPT_BKPT;
 52222   /* Check if data must be read/written to/from the btree page itself. */
 52223   if( offset<pCur->info.nLocal ){
 52224     int a = amt;
 52225     if( a+offset>pCur->info.nLocal ){
 52226       a = pCur->info.nLocal - offset;
 52228     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
 52229     offset = 0;
 52230     pBuf += a;
 52231     amt -= a;
 52232   }else{
 52233     offset -= pCur->info.nLocal;
 52236   if( rc==SQLITE_OK && amt>0 ){
 52237     const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
 52238     Pgno nextPage;
 52240     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
 52242 #ifndef SQLITE_OMIT_INCRBLOB
 52243     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
 52244     ** has not been allocated, allocate it now. The array is sized at
 52245     ** one entry for each overflow page in the overflow chain. The
 52246     ** page number of the first overflow page is stored in aOverflow[0],
 52247     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
 52248     ** (the cache is lazily populated).
 52249     */
 52250     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
 52251       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 52252       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
 52253       /* nOvfl is always positive.  If it were zero, fetchPayload would have
 52254       ** been used instead of this routine. */
 52255       if( ALWAYS(nOvfl) && !pCur->aOverflow ){
 52256         rc = SQLITE_NOMEM;
 52260     /* If the overflow page-list cache has been allocated and the
 52261     ** entry for the first required overflow page is valid, skip
 52262     ** directly to it.
 52263     */
 52264     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
 52265       iIdx = (offset/ovflSize);
 52266       nextPage = pCur->aOverflow[iIdx];
 52267       offset = (offset%ovflSize);
 52269 #endif
 52271     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
 52273 #ifndef SQLITE_OMIT_INCRBLOB
 52274       /* If required, populate the overflow page-list cache. */
 52275       if( pCur->aOverflow ){
 52276         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
 52277         pCur->aOverflow[iIdx] = nextPage;
 52279 #endif
 52281       if( offset>=ovflSize ){
 52282         /* The only reason to read this page is to obtain the page
 52283         ** number for the next page in the overflow chain. The page
 52284         ** data is not required. So first try to lookup the overflow
 52285         ** page-list cache, if any, then fall back to the getOverflowPage()
 52286         ** function.
 52287         */
 52288 #ifndef SQLITE_OMIT_INCRBLOB
 52289         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
 52290           nextPage = pCur->aOverflow[iIdx+1];
 52291         } else 
 52292 #endif
 52293           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 52294         offset -= ovflSize;
 52295       }else{
 52296         /* Need to read this page properly. It contains some of the
 52297         ** range of data that is being read (eOp==0) or written (eOp!=0).
 52298         */
 52299 #ifdef SQLITE_DIRECT_OVERFLOW_READ
 52300         sqlite3_file *fd;
 52301 #endif
 52302         int a = amt;
 52303         if( a + offset > ovflSize ){
 52304           a = ovflSize - offset;
 52307 #ifdef SQLITE_DIRECT_OVERFLOW_READ
 52308         /* If all the following are true:
 52309         **
 52310         **   1) this is a read operation, and 
 52311         **   2) data is required from the start of this overflow page, and
 52312         **   3) the database is file-backed, and
 52313         **   4) there is no open write-transaction, and
 52314         **   5) the database is not a WAL database,
 52315         **
 52316         ** then data can be read directly from the database file into the
 52317         ** output buffer, bypassing the page-cache altogether. This speeds
 52318         ** up loading large records that span many overflow pages.
 52319         */
 52320         if( eOp==0                                             /* (1) */
 52321          && offset==0                                          /* (2) */
 52322          && pBt->inTransaction==TRANS_READ                     /* (4) */
 52323          && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
 52324          && pBt->pPage1->aData[19]==0x01                       /* (5) */
 52325         ){
 52326           u8 aSave[4];
 52327           u8 *aWrite = &pBuf[-4];
 52328           memcpy(aSave, aWrite, 4);
 52329           rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 52330           nextPage = get4byte(aWrite);
 52331           memcpy(aWrite, aSave, 4);
 52332         }else
 52333 #endif
 52336           DbPage *pDbPage;
 52337           rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
 52338           if( rc==SQLITE_OK ){
 52339             aPayload = sqlite3PagerGetData(pDbPage);
 52340             nextPage = get4byte(aPayload);
 52341             rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
 52342             sqlite3PagerUnref(pDbPage);
 52343             offset = 0;
 52346         amt -= a;
 52347         pBuf += a;
 52352   if( rc==SQLITE_OK && amt>0 ){
 52353     return SQLITE_CORRUPT_BKPT;
 52355   return rc;
 52358 /*
 52359 ** Read part of the key associated with cursor pCur.  Exactly
 52360 ** "amt" bytes will be transfered into pBuf[].  The transfer
 52361 ** begins at "offset".
 52362 **
 52363 ** The caller must ensure that pCur is pointing to a valid row
 52364 ** in the table.
 52365 **
 52366 ** Return SQLITE_OK on success or an error code if anything goes
 52367 ** wrong.  An error is returned if "offset+amt" is larger than
 52368 ** the available payload.
 52369 */
 52370 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 52371   assert( cursorHoldsMutex(pCur) );
 52372   assert( pCur->eState==CURSOR_VALID );
 52373   assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 52374   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 52375   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
 52378 /*
 52379 ** Read part of the data associated with cursor pCur.  Exactly
 52380 ** "amt" bytes will be transfered into pBuf[].  The transfer
 52381 ** begins at "offset".
 52382 **
 52383 ** Return SQLITE_OK on success or an error code if anything goes
 52384 ** wrong.  An error is returned if "offset+amt" is larger than
 52385 ** the available payload.
 52386 */
 52387 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 52388   int rc;
 52390 #ifndef SQLITE_OMIT_INCRBLOB
 52391   if ( pCur->eState==CURSOR_INVALID ){
 52392     return SQLITE_ABORT;
 52394 #endif
 52396   assert( cursorHoldsMutex(pCur) );
 52397   rc = restoreCursorPosition(pCur);
 52398   if( rc==SQLITE_OK ){
 52399     assert( pCur->eState==CURSOR_VALID );
 52400     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 52401     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 52402     rc = accessPayload(pCur, offset, amt, pBuf, 0);
 52404   return rc;
 52407 /*
 52408 ** Return a pointer to payload information from the entry that the 
 52409 ** pCur cursor is pointing to.  The pointer is to the beginning of
 52410 ** the key if skipKey==0 and it points to the beginning of data if
 52411 ** skipKey==1.  The number of bytes of available key/data is written
 52412 ** into *pAmt.  If *pAmt==0, then the value returned will not be
 52413 ** a valid pointer.
 52414 **
 52415 ** This routine is an optimization.  It is common for the entire key
 52416 ** and data to fit on the local page and for there to be no overflow
 52417 ** pages.  When that is so, this routine can be used to access the
 52418 ** key and data without making a copy.  If the key and/or data spills
 52419 ** onto overflow pages, then accessPayload() must be used to reassemble
 52420 ** the key/data and copy it into a preallocated buffer.
 52421 **
 52422 ** The pointer returned by this routine looks directly into the cached
 52423 ** page of the database.  The data might change or move the next time
 52424 ** any btree routine is called.
 52425 */
 52426 static const unsigned char *fetchPayload(
 52427   BtCursor *pCur,      /* Cursor pointing to entry to read from */
 52428   int *pAmt,           /* Write the number of available bytes here */
 52429   int skipKey          /* read beginning at data if this is true */
 52430 ){
 52431   unsigned char *aPayload;
 52432   MemPage *pPage;
 52433   u32 nKey;
 52434   u32 nLocal;
 52436   assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
 52437   assert( pCur->eState==CURSOR_VALID );
 52438   assert( cursorHoldsMutex(pCur) );
 52439   pPage = pCur->apPage[pCur->iPage];
 52440   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 52441   if( NEVER(pCur->info.nSize==0) ){
 52442     btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
 52443                    &pCur->info);
 52445   aPayload = pCur->info.pCell;
 52446   aPayload += pCur->info.nHeader;
 52447   if( pPage->intKey ){
 52448     nKey = 0;
 52449   }else{
 52450     nKey = (int)pCur->info.nKey;
 52452   if( skipKey ){
 52453     aPayload += nKey;
 52454     nLocal = pCur->info.nLocal - nKey;
 52455   }else{
 52456     nLocal = pCur->info.nLocal;
 52457     assert( nLocal<=nKey );
 52459   *pAmt = nLocal;
 52460   return aPayload;
 52464 /*
 52465 ** For the entry that cursor pCur is point to, return as
 52466 ** many bytes of the key or data as are available on the local
 52467 ** b-tree page.  Write the number of available bytes into *pAmt.
 52468 **
 52469 ** The pointer returned is ephemeral.  The key/data may move
 52470 ** or be destroyed on the next call to any Btree routine,
 52471 ** including calls from other threads against the same cache.
 52472 ** Hence, a mutex on the BtShared should be held prior to calling
 52473 ** this routine.
 52474 **
 52475 ** These routines is used to get quick access to key and data
 52476 ** in the common case where no overflow pages are used.
 52477 */
 52478 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
 52479   const void *p = 0;
 52480   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 52481   assert( cursorHoldsMutex(pCur) );
 52482   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
 52483     p = (const void*)fetchPayload(pCur, pAmt, 0);
 52485   return p;
 52487 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
 52488   const void *p = 0;
 52489   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 52490   assert( cursorHoldsMutex(pCur) );
 52491   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
 52492     p = (const void*)fetchPayload(pCur, pAmt, 1);
 52494   return p;
 52498 /*
 52499 ** Move the cursor down to a new child page.  The newPgno argument is the
 52500 ** page number of the child page to move to.
 52501 **
 52502 ** This function returns SQLITE_CORRUPT if the page-header flags field of
 52503 ** the new child page does not match the flags field of the parent (i.e.
 52504 ** if an intkey page appears to be the parent of a non-intkey page, or
 52505 ** vice-versa).
 52506 */
 52507 static int moveToChild(BtCursor *pCur, u32 newPgno){
 52508   int rc;
 52509   int i = pCur->iPage;
 52510   MemPage *pNewPage;
 52511   BtShared *pBt = pCur->pBt;
 52513   assert( cursorHoldsMutex(pCur) );
 52514   assert( pCur->eState==CURSOR_VALID );
 52515   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
 52516   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
 52517     return SQLITE_CORRUPT_BKPT;
 52519   rc = getAndInitPage(pBt, newPgno, &pNewPage);
 52520   if( rc ) return rc;
 52521   pCur->apPage[i+1] = pNewPage;
 52522   pCur->aiIdx[i+1] = 0;
 52523   pCur->iPage++;
 52525   pCur->info.nSize = 0;
 52526   pCur->validNKey = 0;
 52527   if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
 52528     return SQLITE_CORRUPT_BKPT;
 52530   return SQLITE_OK;
 52533 #if 0
 52534 /*
 52535 ** Page pParent is an internal (non-leaf) tree page. This function 
 52536 ** asserts that page number iChild is the left-child if the iIdx'th
 52537 ** cell in page pParent. Or, if iIdx is equal to the total number of
 52538 ** cells in pParent, that page number iChild is the right-child of
 52539 ** the page.
 52540 */
 52541 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
 52542   assert( iIdx<=pParent->nCell );
 52543   if( iIdx==pParent->nCell ){
 52544     assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
 52545   }else{
 52546     assert( get4byte(findCell(pParent, iIdx))==iChild );
 52549 #else
 52550 #  define assertParentIndex(x,y,z) 
 52551 #endif
 52553 /*
 52554 ** Move the cursor up to the parent page.
 52555 **
 52556 ** pCur->idx is set to the cell index that contains the pointer
 52557 ** to the page we are coming from.  If we are coming from the
 52558 ** right-most child page then pCur->idx is set to one more than
 52559 ** the largest cell index.
 52560 */
 52561 static void moveToParent(BtCursor *pCur){
 52562   assert( cursorHoldsMutex(pCur) );
 52563   assert( pCur->eState==CURSOR_VALID );
 52564   assert( pCur->iPage>0 );
 52565   assert( pCur->apPage[pCur->iPage] );
 52567   /* UPDATE: It is actually possible for the condition tested by the assert
 52568   ** below to be untrue if the database file is corrupt. This can occur if
 52569   ** one cursor has modified page pParent while a reference to it is held 
 52570   ** by a second cursor. Which can only happen if a single page is linked
 52571   ** into more than one b-tree structure in a corrupt database.  */
 52572 #if 0
 52573   assertParentIndex(
 52574     pCur->apPage[pCur->iPage-1], 
 52575     pCur->aiIdx[pCur->iPage-1], 
 52576     pCur->apPage[pCur->iPage]->pgno
 52577   );
 52578 #endif
 52579   testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
 52581   releasePage(pCur->apPage[pCur->iPage]);
 52582   pCur->iPage--;
 52583   pCur->info.nSize = 0;
 52584   pCur->validNKey = 0;
 52587 /*
 52588 ** Move the cursor to point to the root page of its b-tree structure.
 52589 **
 52590 ** If the table has a virtual root page, then the cursor is moved to point
 52591 ** to the virtual root page instead of the actual root page. A table has a
 52592 ** virtual root page when the actual root page contains no cells and a 
 52593 ** single child page. This can only happen with the table rooted at page 1.
 52594 **
 52595 ** If the b-tree structure is empty, the cursor state is set to 
 52596 ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
 52597 ** cell located on the root (or virtual root) page and the cursor state
 52598 ** is set to CURSOR_VALID.
 52599 **
 52600 ** If this function returns successfully, it may be assumed that the
 52601 ** page-header flags indicate that the [virtual] root-page is the expected 
 52602 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
 52603 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
 52604 ** indicating a table b-tree, or if the caller did specify a KeyInfo 
 52605 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
 52606 ** b-tree).
 52607 */
 52608 static int moveToRoot(BtCursor *pCur){
 52609   MemPage *pRoot;
 52610   int rc = SQLITE_OK;
 52611   Btree *p = pCur->pBtree;
 52612   BtShared *pBt = p->pBt;
 52614   assert( cursorHoldsMutex(pCur) );
 52615   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
 52616   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
 52617   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
 52618   if( pCur->eState>=CURSOR_REQUIRESEEK ){
 52619     if( pCur->eState==CURSOR_FAULT ){
 52620       assert( pCur->skipNext!=SQLITE_OK );
 52621       return pCur->skipNext;
 52623     sqlite3BtreeClearCursor(pCur);
 52626   if( pCur->iPage>=0 ){
 52627     int i;
 52628     for(i=1; i<=pCur->iPage; i++){
 52629       releasePage(pCur->apPage[i]);
 52631     pCur->iPage = 0;
 52632   }else if( pCur->pgnoRoot==0 ){
 52633     pCur->eState = CURSOR_INVALID;
 52634     return SQLITE_OK;
 52635   }else{
 52636     rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
 52637     if( rc!=SQLITE_OK ){
 52638       pCur->eState = CURSOR_INVALID;
 52639       return rc;
 52641     pCur->iPage = 0;
 52643     /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
 52644     ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
 52645     ** NULL, the caller expects a table b-tree. If this is not the case,
 52646     ** return an SQLITE_CORRUPT error.  */
 52647     assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
 52648     if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
 52649       return SQLITE_CORRUPT_BKPT;
 52653   /* Assert that the root page is of the correct type. This must be the
 52654   ** case as the call to this function that loaded the root-page (either
 52655   ** this call or a previous invocation) would have detected corruption 
 52656   ** if the assumption were not true, and it is not possible for the flags 
 52657   ** byte to have been modified while this cursor is holding a reference
 52658   ** to the page.  */
 52659   pRoot = pCur->apPage[0];
 52660   assert( pRoot->pgno==pCur->pgnoRoot );
 52661   assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
 52663   pCur->aiIdx[0] = 0;
 52664   pCur->info.nSize = 0;
 52665   pCur->atLast = 0;
 52666   pCur->validNKey = 0;
 52668   if( pRoot->nCell==0 && !pRoot->leaf ){
 52669     Pgno subpage;
 52670     if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
 52671     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
 52672     pCur->eState = CURSOR_VALID;
 52673     rc = moveToChild(pCur, subpage);
 52674   }else{
 52675     pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
 52677   return rc;
 52680 /*
 52681 ** Move the cursor down to the left-most leaf entry beneath the
 52682 ** entry to which it is currently pointing.
 52683 **
 52684 ** The left-most leaf is the one with the smallest key - the first
 52685 ** in ascending order.
 52686 */
 52687 static int moveToLeftmost(BtCursor *pCur){
 52688   Pgno pgno;
 52689   int rc = SQLITE_OK;
 52690   MemPage *pPage;
 52692   assert( cursorHoldsMutex(pCur) );
 52693   assert( pCur->eState==CURSOR_VALID );
 52694   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
 52695     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 52696     pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
 52697     rc = moveToChild(pCur, pgno);
 52699   return rc;
 52702 /*
 52703 ** Move the cursor down to the right-most leaf entry beneath the
 52704 ** page to which it is currently pointing.  Notice the difference
 52705 ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
 52706 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
 52707 ** finds the right-most entry beneath the *page*.
 52708 **
 52709 ** The right-most entry is the one with the largest key - the last
 52710 ** key in ascending order.
 52711 */
 52712 static int moveToRightmost(BtCursor *pCur){
 52713   Pgno pgno;
 52714   int rc = SQLITE_OK;
 52715   MemPage *pPage = 0;
 52717   assert( cursorHoldsMutex(pCur) );
 52718   assert( pCur->eState==CURSOR_VALID );
 52719   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
 52720     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 52721     pCur->aiIdx[pCur->iPage] = pPage->nCell;
 52722     rc = moveToChild(pCur, pgno);
 52724   if( rc==SQLITE_OK ){
 52725     pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
 52726     pCur->info.nSize = 0;
 52727     pCur->validNKey = 0;
 52729   return rc;
 52732 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
 52733 ** on success.  Set *pRes to 0 if the cursor actually points to something
 52734 ** or set *pRes to 1 if the table is empty.
 52735 */
 52736 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
 52737   int rc;
 52739   assert( cursorHoldsMutex(pCur) );
 52740   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 52741   rc = moveToRoot(pCur);
 52742   if( rc==SQLITE_OK ){
 52743     if( pCur->eState==CURSOR_INVALID ){
 52744       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 52745       *pRes = 1;
 52746     }else{
 52747       assert( pCur->apPage[pCur->iPage]->nCell>0 );
 52748       *pRes = 0;
 52749       rc = moveToLeftmost(pCur);
 52752   return rc;
 52755 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
 52756 ** on success.  Set *pRes to 0 if the cursor actually points to something
 52757 ** or set *pRes to 1 if the table is empty.
 52758 */
 52759 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 52760   int rc;
 52762   assert( cursorHoldsMutex(pCur) );
 52763   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 52765   /* If the cursor already points to the last entry, this is a no-op. */
 52766   if( CURSOR_VALID==pCur->eState && pCur->atLast ){
 52767 #ifdef SQLITE_DEBUG
 52768     /* This block serves to assert() that the cursor really does point 
 52769     ** to the last entry in the b-tree. */
 52770     int ii;
 52771     for(ii=0; ii<pCur->iPage; ii++){
 52772       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
 52774     assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
 52775     assert( pCur->apPage[pCur->iPage]->leaf );
 52776 #endif
 52777     return SQLITE_OK;
 52780   rc = moveToRoot(pCur);
 52781   if( rc==SQLITE_OK ){
 52782     if( CURSOR_INVALID==pCur->eState ){
 52783       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 52784       *pRes = 1;
 52785     }else{
 52786       assert( pCur->eState==CURSOR_VALID );
 52787       *pRes = 0;
 52788       rc = moveToRightmost(pCur);
 52789       pCur->atLast = rc==SQLITE_OK ?1:0;
 52792   return rc;
 52795 /* Move the cursor so that it points to an entry near the key 
 52796 ** specified by pIdxKey or intKey.   Return a success code.
 52797 **
 52798 ** For INTKEY tables, the intKey parameter is used.  pIdxKey 
 52799 ** must be NULL.  For index tables, pIdxKey is used and intKey
 52800 ** is ignored.
 52801 **
 52802 ** If an exact match is not found, then the cursor is always
 52803 ** left pointing at a leaf page which would hold the entry if it
 52804 ** were present.  The cursor might point to an entry that comes
 52805 ** before or after the key.
 52806 **
 52807 ** An integer is written into *pRes which is the result of
 52808 ** comparing the key with the entry to which the cursor is 
 52809 ** pointing.  The meaning of the integer written into
 52810 ** *pRes is as follows:
 52811 **
 52812 **     *pRes<0      The cursor is left pointing at an entry that
 52813 **                  is smaller than intKey/pIdxKey or if the table is empty
 52814 **                  and the cursor is therefore left point to nothing.
 52815 **
 52816 **     *pRes==0     The cursor is left pointing at an entry that
 52817 **                  exactly matches intKey/pIdxKey.
 52818 **
 52819 **     *pRes>0      The cursor is left pointing at an entry that
 52820 **                  is larger than intKey/pIdxKey.
 52821 **
 52822 */
 52823 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
 52824   BtCursor *pCur,          /* The cursor to be moved */
 52825   UnpackedRecord *pIdxKey, /* Unpacked index key */
 52826   i64 intKey,              /* The table key */
 52827   int biasRight,           /* If true, bias the search to the high end */
 52828   int *pRes                /* Write search results here */
 52829 ){
 52830   int rc;
 52832   assert( cursorHoldsMutex(pCur) );
 52833   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 52834   assert( pRes );
 52835   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
 52837   /* If the cursor is already positioned at the point we are trying
 52838   ** to move to, then just return without doing any work */
 52839   if( pCur->eState==CURSOR_VALID && pCur->validNKey 
 52840    && pCur->apPage[0]->intKey 
 52841   ){
 52842     if( pCur->info.nKey==intKey ){
 52843       *pRes = 0;
 52844       return SQLITE_OK;
 52846     if( pCur->atLast && pCur->info.nKey<intKey ){
 52847       *pRes = -1;
 52848       return SQLITE_OK;
 52852   rc = moveToRoot(pCur);
 52853   if( rc ){
 52854     return rc;
 52856   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
 52857   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
 52858   assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
 52859   if( pCur->eState==CURSOR_INVALID ){
 52860     *pRes = -1;
 52861     assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 52862     return SQLITE_OK;
 52864   assert( pCur->apPage[0]->intKey || pIdxKey );
 52865   for(;;){
 52866     int lwr, upr, idx;
 52867     Pgno chldPg;
 52868     MemPage *pPage = pCur->apPage[pCur->iPage];
 52869     int c;
 52871     /* pPage->nCell must be greater than zero. If this is the root-page
 52872     ** the cursor would have been INVALID above and this for(;;) loop
 52873     ** not run. If this is not the root-page, then the moveToChild() routine
 52874     ** would have already detected db corruption. Similarly, pPage must
 52875     ** be the right kind (index or table) of b-tree page. Otherwise
 52876     ** a moveToChild() or moveToRoot() call would have detected corruption.  */
 52877     assert( pPage->nCell>0 );
 52878     assert( pPage->intKey==(pIdxKey==0) );
 52879     lwr = 0;
 52880     upr = pPage->nCell-1;
 52881     if( biasRight ){
 52882       pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
 52883     }else{
 52884       pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
 52886     for(;;){
 52887       u8 *pCell;                          /* Pointer to current cell in pPage */
 52889       assert( idx==pCur->aiIdx[pCur->iPage] );
 52890       pCur->info.nSize = 0;
 52891       pCell = findCell(pPage, idx) + pPage->childPtrSize;
 52892       if( pPage->intKey ){
 52893         i64 nCellKey;
 52894         if( pPage->hasData ){
 52895           u32 dummy;
 52896           pCell += getVarint32(pCell, dummy);
 52898         getVarint(pCell, (u64*)&nCellKey);
 52899         if( nCellKey==intKey ){
 52900           c = 0;
 52901         }else if( nCellKey<intKey ){
 52902           c = -1;
 52903         }else{
 52904           assert( nCellKey>intKey );
 52905           c = +1;
 52907         pCur->validNKey = 1;
 52908         pCur->info.nKey = nCellKey;
 52909       }else{
 52910         /* The maximum supported page-size is 65536 bytes. This means that
 52911         ** the maximum number of record bytes stored on an index B-Tree
 52912         ** page is less than 16384 bytes and may be stored as a 2-byte
 52913         ** varint. This information is used to attempt to avoid parsing 
 52914         ** the entire cell by checking for the cases where the record is 
 52915         ** stored entirely within the b-tree page by inspecting the first 
 52916         ** 2 bytes of the cell.
 52917         */
 52918         int nCell = pCell[0];
 52919         if( nCell<=pPage->max1bytePayload
 52920          /* && (pCell+nCell)<pPage->aDataEnd */
 52921         ){
 52922           /* This branch runs if the record-size field of the cell is a
 52923           ** single byte varint and the record fits entirely on the main
 52924           ** b-tree page.  */
 52925           testcase( pCell+nCell+1==pPage->aDataEnd );
 52926           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
 52927         }else if( !(pCell[1] & 0x80) 
 52928           && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
 52929           /* && (pCell+nCell+2)<=pPage->aDataEnd */
 52930         ){
 52931           /* The record-size field is a 2 byte varint and the record 
 52932           ** fits entirely on the main b-tree page.  */
 52933           testcase( pCell+nCell+2==pPage->aDataEnd );
 52934           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
 52935         }else{
 52936           /* The record flows over onto one or more overflow pages. In
 52937           ** this case the whole cell needs to be parsed, a buffer allocated
 52938           ** and accessPayload() used to retrieve the record into the
 52939           ** buffer before VdbeRecordCompare() can be called. */
 52940           void *pCellKey;
 52941           u8 * const pCellBody = pCell - pPage->childPtrSize;
 52942           btreeParseCellPtr(pPage, pCellBody, &pCur->info);
 52943           nCell = (int)pCur->info.nKey;
 52944           pCellKey = sqlite3Malloc( nCell );
 52945           if( pCellKey==0 ){
 52946             rc = SQLITE_NOMEM;
 52947             goto moveto_finish;
 52949           rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
 52950           if( rc ){
 52951             sqlite3_free(pCellKey);
 52952             goto moveto_finish;
 52954           c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
 52955           sqlite3_free(pCellKey);
 52958       if( c==0 ){
 52959         if( pPage->intKey && !pPage->leaf ){
 52960           lwr = idx;
 52961           break;
 52962         }else{
 52963           *pRes = 0;
 52964           rc = SQLITE_OK;
 52965           goto moveto_finish;
 52968       if( c<0 ){
 52969         lwr = idx+1;
 52970       }else{
 52971         upr = idx-1;
 52973       if( lwr>upr ){
 52974         break;
 52976       pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
 52978     assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
 52979     assert( pPage->isInit );
 52980     if( pPage->leaf ){
 52981       chldPg = 0;
 52982     }else if( lwr>=pPage->nCell ){
 52983       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 52984     }else{
 52985       chldPg = get4byte(findCell(pPage, lwr));
 52987     if( chldPg==0 ){
 52988       assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 52989       *pRes = c;
 52990       rc = SQLITE_OK;
 52991       goto moveto_finish;
 52993     pCur->aiIdx[pCur->iPage] = (u16)lwr;
 52994     pCur->info.nSize = 0;
 52995     pCur->validNKey = 0;
 52996     rc = moveToChild(pCur, chldPg);
 52997     if( rc ) goto moveto_finish;
 52999 moveto_finish:
 53000   return rc;
 53004 /*
 53005 ** Return TRUE if the cursor is not pointing at an entry of the table.
 53006 **
 53007 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
 53008 ** past the last entry in the table or sqlite3BtreePrev() moves past
 53009 ** the first entry.  TRUE is also returned if the table is empty.
 53010 */
 53011 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
 53012   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
 53013   ** have been deleted? This API will need to change to return an error code
 53014   ** as well as the boolean result value.
 53015   */
 53016   return (CURSOR_VALID!=pCur->eState);
 53019 /*
 53020 ** Advance the cursor to the next entry in the database.  If
 53021 ** successful then set *pRes=0.  If the cursor
 53022 ** was already pointing to the last entry in the database before
 53023 ** this routine was called, then set *pRes=1.
 53024 */
 53025 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
 53026   int rc;
 53027   int idx;
 53028   MemPage *pPage;
 53030   assert( cursorHoldsMutex(pCur) );
 53031   rc = restoreCursorPosition(pCur);
 53032   if( rc!=SQLITE_OK ){
 53033     return rc;
 53035   assert( pRes!=0 );
 53036   if( CURSOR_INVALID==pCur->eState ){
 53037     *pRes = 1;
 53038     return SQLITE_OK;
 53040   if( pCur->skipNext>0 ){
 53041     pCur->skipNext = 0;
 53042     *pRes = 0;
 53043     return SQLITE_OK;
 53045   pCur->skipNext = 0;
 53047   pPage = pCur->apPage[pCur->iPage];
 53048   idx = ++pCur->aiIdx[pCur->iPage];
 53049   assert( pPage->isInit );
 53051   /* If the database file is corrupt, it is possible for the value of idx 
 53052   ** to be invalid here. This can only occur if a second cursor modifies
 53053   ** the page while cursor pCur is holding a reference to it. Which can
 53054   ** only happen if the database is corrupt in such a way as to link the
 53055   ** page into more than one b-tree structure. */
 53056   testcase( idx>pPage->nCell );
 53058   pCur->info.nSize = 0;
 53059   pCur->validNKey = 0;
 53060   if( idx>=pPage->nCell ){
 53061     if( !pPage->leaf ){
 53062       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
 53063       if( rc ) return rc;
 53064       rc = moveToLeftmost(pCur);
 53065       *pRes = 0;
 53066       return rc;
 53068     do{
 53069       if( pCur->iPage==0 ){
 53070         *pRes = 1;
 53071         pCur->eState = CURSOR_INVALID;
 53072         return SQLITE_OK;
 53074       moveToParent(pCur);
 53075       pPage = pCur->apPage[pCur->iPage];
 53076     }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
 53077     *pRes = 0;
 53078     if( pPage->intKey ){
 53079       rc = sqlite3BtreeNext(pCur, pRes);
 53080     }else{
 53081       rc = SQLITE_OK;
 53083     return rc;
 53085   *pRes = 0;
 53086   if( pPage->leaf ){
 53087     return SQLITE_OK;
 53089   rc = moveToLeftmost(pCur);
 53090   return rc;
 53094 /*
 53095 ** Step the cursor to the back to the previous entry in the database.  If
 53096 ** successful then set *pRes=0.  If the cursor
 53097 ** was already pointing to the first entry in the database before
 53098 ** this routine was called, then set *pRes=1.
 53099 */
 53100 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
 53101   int rc;
 53102   MemPage *pPage;
 53104   assert( cursorHoldsMutex(pCur) );
 53105   rc = restoreCursorPosition(pCur);
 53106   if( rc!=SQLITE_OK ){
 53107     return rc;
 53109   pCur->atLast = 0;
 53110   if( CURSOR_INVALID==pCur->eState ){
 53111     *pRes = 1;
 53112     return SQLITE_OK;
 53114   if( pCur->skipNext<0 ){
 53115     pCur->skipNext = 0;
 53116     *pRes = 0;
 53117     return SQLITE_OK;
 53119   pCur->skipNext = 0;
 53121   pPage = pCur->apPage[pCur->iPage];
 53122   assert( pPage->isInit );
 53123   if( !pPage->leaf ){
 53124     int idx = pCur->aiIdx[pCur->iPage];
 53125     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
 53126     if( rc ){
 53127       return rc;
 53129     rc = moveToRightmost(pCur);
 53130   }else{
 53131     while( pCur->aiIdx[pCur->iPage]==0 ){
 53132       if( pCur->iPage==0 ){
 53133         pCur->eState = CURSOR_INVALID;
 53134         *pRes = 1;
 53135         return SQLITE_OK;
 53137       moveToParent(pCur);
 53139     pCur->info.nSize = 0;
 53140     pCur->validNKey = 0;
 53142     pCur->aiIdx[pCur->iPage]--;
 53143     pPage = pCur->apPage[pCur->iPage];
 53144     if( pPage->intKey && !pPage->leaf ){
 53145       rc = sqlite3BtreePrevious(pCur, pRes);
 53146     }else{
 53147       rc = SQLITE_OK;
 53150   *pRes = 0;
 53151   return rc;
 53154 /*
 53155 ** Allocate a new page from the database file.
 53156 **
 53157 ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
 53158 ** has already been called on the new page.)  The new page has also
 53159 ** been referenced and the calling routine is responsible for calling
 53160 ** sqlite3PagerUnref() on the new page when it is done.
 53161 **
 53162 ** SQLITE_OK is returned on success.  Any other return value indicates
 53163 ** an error.  *ppPage and *pPgno are undefined in the event of an error.
 53164 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
 53165 **
 53166 ** If the "nearby" parameter is not 0, then a (feeble) effort is made to 
 53167 ** locate a page close to the page number "nearby".  This can be used in an
 53168 ** attempt to keep related pages close to each other in the database file,
 53169 ** which in turn can make database access faster.
 53170 **
 53171 ** If the "exact" parameter is not 0, and the page-number nearby exists 
 53172 ** anywhere on the free-list, then it is guarenteed to be returned. This
 53173 ** is only used by auto-vacuum databases when allocating a new table.
 53174 */
 53175 static int allocateBtreePage(
 53176   BtShared *pBt, 
 53177   MemPage **ppPage, 
 53178   Pgno *pPgno, 
 53179   Pgno nearby,
 53180   u8 exact
 53181 ){
 53182   MemPage *pPage1;
 53183   int rc;
 53184   u32 n;     /* Number of pages on the freelist */
 53185   u32 k;     /* Number of leaves on the trunk of the freelist */
 53186   MemPage *pTrunk = 0;
 53187   MemPage *pPrevTrunk = 0;
 53188   Pgno mxPage;     /* Total size of the database file */
 53190   assert( sqlite3_mutex_held(pBt->mutex) );
 53191   pPage1 = pBt->pPage1;
 53192   mxPage = btreePagecount(pBt);
 53193   n = get4byte(&pPage1->aData[36]);
 53194   testcase( n==mxPage-1 );
 53195   if( n>=mxPage ){
 53196     return SQLITE_CORRUPT_BKPT;
 53198   if( n>0 ){
 53199     /* There are pages on the freelist.  Reuse one of those pages. */
 53200     Pgno iTrunk;
 53201     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
 53203     /* If the 'exact' parameter was true and a query of the pointer-map
 53204     ** shows that the page 'nearby' is somewhere on the free-list, then
 53205     ** the entire-list will be searched for that page.
 53206     */
 53207 #ifndef SQLITE_OMIT_AUTOVACUUM
 53208     if( exact && nearby<=mxPage ){
 53209       u8 eType;
 53210       assert( nearby>0 );
 53211       assert( pBt->autoVacuum );
 53212       rc = ptrmapGet(pBt, nearby, &eType, 0);
 53213       if( rc ) return rc;
 53214       if( eType==PTRMAP_FREEPAGE ){
 53215         searchList = 1;
 53217       *pPgno = nearby;
 53219 #endif
 53221     /* Decrement the free-list count by 1. Set iTrunk to the index of the
 53222     ** first free-list trunk page. iPrevTrunk is initially 1.
 53223     */
 53224     rc = sqlite3PagerWrite(pPage1->pDbPage);
 53225     if( rc ) return rc;
 53226     put4byte(&pPage1->aData[36], n-1);
 53228     /* The code within this loop is run only once if the 'searchList' variable
 53229     ** is not true. Otherwise, it runs once for each trunk-page on the
 53230     ** free-list until the page 'nearby' is located.
 53231     */
 53232     do {
 53233       pPrevTrunk = pTrunk;
 53234       if( pPrevTrunk ){
 53235         iTrunk = get4byte(&pPrevTrunk->aData[0]);
 53236       }else{
 53237         iTrunk = get4byte(&pPage1->aData[32]);
 53239       testcase( iTrunk==mxPage );
 53240       if( iTrunk>mxPage ){
 53241         rc = SQLITE_CORRUPT_BKPT;
 53242       }else{
 53243         rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 53245       if( rc ){
 53246         pTrunk = 0;
 53247         goto end_allocate_page;
 53249       assert( pTrunk!=0 );
 53250       assert( pTrunk->aData!=0 );
 53252       k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
 53253       if( k==0 && !searchList ){
 53254         /* The trunk has no leaves and the list is not being searched. 
 53255         ** So extract the trunk page itself and use it as the newly 
 53256         ** allocated page */
 53257         assert( pPrevTrunk==0 );
 53258         rc = sqlite3PagerWrite(pTrunk->pDbPage);
 53259         if( rc ){
 53260           goto end_allocate_page;
 53262         *pPgno = iTrunk;
 53263         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
 53264         *ppPage = pTrunk;
 53265         pTrunk = 0;
 53266         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
 53267       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
 53268         /* Value of k is out of range.  Database corruption */
 53269         rc = SQLITE_CORRUPT_BKPT;
 53270         goto end_allocate_page;
 53271 #ifndef SQLITE_OMIT_AUTOVACUUM
 53272       }else if( searchList && nearby==iTrunk ){
 53273         /* The list is being searched and this trunk page is the page
 53274         ** to allocate, regardless of whether it has leaves.
 53275         */
 53276         assert( *pPgno==iTrunk );
 53277         *ppPage = pTrunk;
 53278         searchList = 0;
 53279         rc = sqlite3PagerWrite(pTrunk->pDbPage);
 53280         if( rc ){
 53281           goto end_allocate_page;
 53283         if( k==0 ){
 53284           if( !pPrevTrunk ){
 53285             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
 53286           }else{
 53287             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
 53288             if( rc!=SQLITE_OK ){
 53289               goto end_allocate_page;
 53291             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
 53293         }else{
 53294           /* The trunk page is required by the caller but it contains 
 53295           ** pointers to free-list leaves. The first leaf becomes a trunk
 53296           ** page in this case.
 53297           */
 53298           MemPage *pNewTrunk;
 53299           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
 53300           if( iNewTrunk>mxPage ){ 
 53301             rc = SQLITE_CORRUPT_BKPT;
 53302             goto end_allocate_page;
 53304           testcase( iNewTrunk==mxPage );
 53305           rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
 53306           if( rc!=SQLITE_OK ){
 53307             goto end_allocate_page;
 53309           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
 53310           if( rc!=SQLITE_OK ){
 53311             releasePage(pNewTrunk);
 53312             goto end_allocate_page;
 53314           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
 53315           put4byte(&pNewTrunk->aData[4], k-1);
 53316           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
 53317           releasePage(pNewTrunk);
 53318           if( !pPrevTrunk ){
 53319             assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
 53320             put4byte(&pPage1->aData[32], iNewTrunk);
 53321           }else{
 53322             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
 53323             if( rc ){
 53324               goto end_allocate_page;
 53326             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
 53329         pTrunk = 0;
 53330         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
 53331 #endif
 53332       }else if( k>0 ){
 53333         /* Extract a leaf from the trunk */
 53334         u32 closest;
 53335         Pgno iPage;
 53336         unsigned char *aData = pTrunk->aData;
 53337         if( nearby>0 ){
 53338           u32 i;
 53339           int dist;
 53340           closest = 0;
 53341           dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
 53342           for(i=1; i<k; i++){
 53343             int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
 53344             if( d2<dist ){
 53345               closest = i;
 53346               dist = d2;
 53349         }else{
 53350           closest = 0;
 53353         iPage = get4byte(&aData[8+closest*4]);
 53354         testcase( iPage==mxPage );
 53355         if( iPage>mxPage ){
 53356           rc = SQLITE_CORRUPT_BKPT;
 53357           goto end_allocate_page;
 53359         testcase( iPage==mxPage );
 53360         if( !searchList || iPage==nearby ){
 53361           int noContent;
 53362           *pPgno = iPage;
 53363           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
 53364                  ": %d more free pages\n",
 53365                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
 53366           rc = sqlite3PagerWrite(pTrunk->pDbPage);
 53367           if( rc ) goto end_allocate_page;
 53368           if( closest<k-1 ){
 53369             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
 53371           put4byte(&aData[4], k-1);
 53372           noContent = !btreeGetHasContent(pBt, *pPgno);
 53373           rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
 53374           if( rc==SQLITE_OK ){
 53375             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
 53376             if( rc!=SQLITE_OK ){
 53377               releasePage(*ppPage);
 53380           searchList = 0;
 53383       releasePage(pPrevTrunk);
 53384       pPrevTrunk = 0;
 53385     }while( searchList );
 53386   }else{
 53387     /* There are no pages on the freelist, so create a new page at the
 53388     ** end of the file */
 53389     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 53390     if( rc ) return rc;
 53391     pBt->nPage++;
 53392     if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
 53394 #ifndef SQLITE_OMIT_AUTOVACUUM
 53395     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
 53396       /* If *pPgno refers to a pointer-map page, allocate two new pages
 53397       ** at the end of the file instead of one. The first allocated page
 53398       ** becomes a new pointer-map page, the second is used by the caller.
 53399       */
 53400       MemPage *pPg = 0;
 53401       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
 53402       assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
 53403       rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
 53404       if( rc==SQLITE_OK ){
 53405         rc = sqlite3PagerWrite(pPg->pDbPage);
 53406         releasePage(pPg);
 53408       if( rc ) return rc;
 53409       pBt->nPage++;
 53410       if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
 53412 #endif
 53413     put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
 53414     *pPgno = pBt->nPage;
 53416     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
 53417     rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
 53418     if( rc ) return rc;
 53419     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
 53420     if( rc!=SQLITE_OK ){
 53421       releasePage(*ppPage);
 53423     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
 53426   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
 53428 end_allocate_page:
 53429   releasePage(pTrunk);
 53430   releasePage(pPrevTrunk);
 53431   if( rc==SQLITE_OK ){
 53432     if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
 53433       releasePage(*ppPage);
 53434       return SQLITE_CORRUPT_BKPT;
 53436     (*ppPage)->isInit = 0;
 53437   }else{
 53438     *ppPage = 0;
 53440   assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
 53441   return rc;
 53444 /*
 53445 ** This function is used to add page iPage to the database file free-list. 
 53446 ** It is assumed that the page is not already a part of the free-list.
 53447 **
 53448 ** The value passed as the second argument to this function is optional.
 53449 ** If the caller happens to have a pointer to the MemPage object 
 53450 ** corresponding to page iPage handy, it may pass it as the second value. 
 53451 ** Otherwise, it may pass NULL.
 53452 **
 53453 ** If a pointer to a MemPage object is passed as the second argument,
 53454 ** its reference count is not altered by this function.
 53455 */
 53456 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
 53457   MemPage *pTrunk = 0;                /* Free-list trunk page */
 53458   Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
 53459   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
 53460   MemPage *pPage;                     /* Page being freed. May be NULL. */
 53461   int rc;                             /* Return Code */
 53462   int nFree;                          /* Initial number of pages on free-list */
 53464   assert( sqlite3_mutex_held(pBt->mutex) );
 53465   assert( iPage>1 );
 53466   assert( !pMemPage || pMemPage->pgno==iPage );
 53468   if( pMemPage ){
 53469     pPage = pMemPage;
 53470     sqlite3PagerRef(pPage->pDbPage);
 53471   }else{
 53472     pPage = btreePageLookup(pBt, iPage);
 53475   /* Increment the free page count on pPage1 */
 53476   rc = sqlite3PagerWrite(pPage1->pDbPage);
 53477   if( rc ) goto freepage_out;
 53478   nFree = get4byte(&pPage1->aData[36]);
 53479   put4byte(&pPage1->aData[36], nFree+1);
 53481   if( pBt->btsFlags & BTS_SECURE_DELETE ){
 53482     /* If the secure_delete option is enabled, then
 53483     ** always fully overwrite deleted information with zeros.
 53484     */
 53485     if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
 53486      ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
 53487     ){
 53488       goto freepage_out;
 53490     memset(pPage->aData, 0, pPage->pBt->pageSize);
 53493   /* If the database supports auto-vacuum, write an entry in the pointer-map
 53494   ** to indicate that the page is free.
 53495   */
 53496   if( ISAUTOVACUUM ){
 53497     ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
 53498     if( rc ) goto freepage_out;
 53501   /* Now manipulate the actual database free-list structure. There are two
 53502   ** possibilities. If the free-list is currently empty, or if the first
 53503   ** trunk page in the free-list is full, then this page will become a
 53504   ** new free-list trunk page. Otherwise, it will become a leaf of the
 53505   ** first trunk page in the current free-list. This block tests if it
 53506   ** is possible to add the page as a new free-list leaf.
 53507   */
 53508   if( nFree!=0 ){
 53509     u32 nLeaf;                /* Initial number of leaf cells on trunk page */
 53511     iTrunk = get4byte(&pPage1->aData[32]);
 53512     rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 53513     if( rc!=SQLITE_OK ){
 53514       goto freepage_out;
 53517     nLeaf = get4byte(&pTrunk->aData[4]);
 53518     assert( pBt->usableSize>32 );
 53519     if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
 53520       rc = SQLITE_CORRUPT_BKPT;
 53521       goto freepage_out;
 53523     if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
 53524       /* In this case there is room on the trunk page to insert the page
 53525       ** being freed as a new leaf.
 53526       **
 53527       ** Note that the trunk page is not really full until it contains
 53528       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
 53529       ** coded.  But due to a coding error in versions of SQLite prior to
 53530       ** 3.6.0, databases with freelist trunk pages holding more than
 53531       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
 53532       ** to maintain backwards compatibility with older versions of SQLite,
 53533       ** we will continue to restrict the number of entries to usableSize/4 - 8
 53534       ** for now.  At some point in the future (once everyone has upgraded
 53535       ** to 3.6.0 or later) we should consider fixing the conditional above
 53536       ** to read "usableSize/4-2" instead of "usableSize/4-8".
 53537       */
 53538       rc = sqlite3PagerWrite(pTrunk->pDbPage);
 53539       if( rc==SQLITE_OK ){
 53540         put4byte(&pTrunk->aData[4], nLeaf+1);
 53541         put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
 53542         if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
 53543           sqlite3PagerDontWrite(pPage->pDbPage);
 53545         rc = btreeSetHasContent(pBt, iPage);
 53547       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
 53548       goto freepage_out;
 53552   /* If control flows to this point, then it was not possible to add the
 53553   ** the page being freed as a leaf page of the first trunk in the free-list.
 53554   ** Possibly because the free-list is empty, or possibly because the 
 53555   ** first trunk in the free-list is full. Either way, the page being freed
 53556   ** will become the new first trunk page in the free-list.
 53557   */
 53558   if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
 53559     goto freepage_out;
 53561   rc = sqlite3PagerWrite(pPage->pDbPage);
 53562   if( rc!=SQLITE_OK ){
 53563     goto freepage_out;
 53565   put4byte(pPage->aData, iTrunk);
 53566   put4byte(&pPage->aData[4], 0);
 53567   put4byte(&pPage1->aData[32], iPage);
 53568   TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
 53570 freepage_out:
 53571   if( pPage ){
 53572     pPage->isInit = 0;
 53574   releasePage(pPage);
 53575   releasePage(pTrunk);
 53576   return rc;
 53578 static void freePage(MemPage *pPage, int *pRC){
 53579   if( (*pRC)==SQLITE_OK ){
 53580     *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
 53584 /*
 53585 ** Free any overflow pages associated with the given Cell.
 53586 */
 53587 static int clearCell(MemPage *pPage, unsigned char *pCell){
 53588   BtShared *pBt = pPage->pBt;
 53589   CellInfo info;
 53590   Pgno ovflPgno;
 53591   int rc;
 53592   int nOvfl;
 53593   u32 ovflPageSize;
 53595   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 53596   btreeParseCellPtr(pPage, pCell, &info);
 53597   if( info.iOverflow==0 ){
 53598     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 53600   if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
 53601     return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
 53603   ovflPgno = get4byte(&pCell[info.iOverflow]);
 53604   assert( pBt->usableSize > 4 );
 53605   ovflPageSize = pBt->usableSize - 4;
 53606   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
 53607   assert( ovflPgno==0 || nOvfl>0 );
 53608   while( nOvfl-- ){
 53609     Pgno iNext = 0;
 53610     MemPage *pOvfl = 0;
 53611     if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
 53612       /* 0 is not a legal page number and page 1 cannot be an 
 53613       ** overflow page. Therefore if ovflPgno<2 or past the end of the 
 53614       ** file the database must be corrupt. */
 53615       return SQLITE_CORRUPT_BKPT;
 53617     if( nOvfl ){
 53618       rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
 53619       if( rc ) return rc;
 53622     if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
 53623      && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
 53624     ){
 53625       /* There is no reason any cursor should have an outstanding reference 
 53626       ** to an overflow page belonging to a cell that is being deleted/updated.
 53627       ** So if there exists more than one reference to this page, then it 
 53628       ** must not really be an overflow page and the database must be corrupt. 
 53629       ** It is helpful to detect this before calling freePage2(), as 
 53630       ** freePage2() may zero the page contents if secure-delete mode is
 53631       ** enabled. If this 'overflow' page happens to be a page that the
 53632       ** caller is iterating through or using in some other way, this
 53633       ** can be problematic.
 53634       */
 53635       rc = SQLITE_CORRUPT_BKPT;
 53636     }else{
 53637       rc = freePage2(pBt, pOvfl, ovflPgno);
 53640     if( pOvfl ){
 53641       sqlite3PagerUnref(pOvfl->pDbPage);
 53643     if( rc ) return rc;
 53644     ovflPgno = iNext;
 53646   return SQLITE_OK;
 53649 /*
 53650 ** Create the byte sequence used to represent a cell on page pPage
 53651 ** and write that byte sequence into pCell[].  Overflow pages are
 53652 ** allocated and filled in as necessary.  The calling procedure
 53653 ** is responsible for making sure sufficient space has been allocated
 53654 ** for pCell[].
 53655 **
 53656 ** Note that pCell does not necessary need to point to the pPage->aData
 53657 ** area.  pCell might point to some temporary storage.  The cell will
 53658 ** be constructed in this temporary area then copied into pPage->aData
 53659 ** later.
 53660 */
 53661 static int fillInCell(
 53662   MemPage *pPage,                /* The page that contains the cell */
 53663   unsigned char *pCell,          /* Complete text of the cell */
 53664   const void *pKey, i64 nKey,    /* The key */
 53665   const void *pData,int nData,   /* The data */
 53666   int nZero,                     /* Extra zero bytes to append to pData */
 53667   int *pnSize                    /* Write cell size here */
 53668 ){
 53669   int nPayload;
 53670   const u8 *pSrc;
 53671   int nSrc, n, rc;
 53672   int spaceLeft;
 53673   MemPage *pOvfl = 0;
 53674   MemPage *pToRelease = 0;
 53675   unsigned char *pPrior;
 53676   unsigned char *pPayload;
 53677   BtShared *pBt = pPage->pBt;
 53678   Pgno pgnoOvfl = 0;
 53679   int nHeader;
 53680   CellInfo info;
 53682   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 53684   /* pPage is not necessarily writeable since pCell might be auxiliary
 53685   ** buffer space that is separate from the pPage buffer area */
 53686   assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
 53687             || sqlite3PagerIswriteable(pPage->pDbPage) );
 53689   /* Fill in the header. */
 53690   nHeader = 0;
 53691   if( !pPage->leaf ){
 53692     nHeader += 4;
 53694   if( pPage->hasData ){
 53695     nHeader += putVarint(&pCell[nHeader], nData+nZero);
 53696   }else{
 53697     nData = nZero = 0;
 53699   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
 53700   btreeParseCellPtr(pPage, pCell, &info);
 53701   assert( info.nHeader==nHeader );
 53702   assert( info.nKey==nKey );
 53703   assert( info.nData==(u32)(nData+nZero) );
 53705   /* Fill in the payload */
 53706   nPayload = nData + nZero;
 53707   if( pPage->intKey ){
 53708     pSrc = pData;
 53709     nSrc = nData;
 53710     nData = 0;
 53711   }else{ 
 53712     if( NEVER(nKey>0x7fffffff || pKey==0) ){
 53713       return SQLITE_CORRUPT_BKPT;
 53715     nPayload += (int)nKey;
 53716     pSrc = pKey;
 53717     nSrc = (int)nKey;
 53719   *pnSize = info.nSize;
 53720   spaceLeft = info.nLocal;
 53721   pPayload = &pCell[nHeader];
 53722   pPrior = &pCell[info.iOverflow];
 53724   while( nPayload>0 ){
 53725     if( spaceLeft==0 ){
 53726 #ifndef SQLITE_OMIT_AUTOVACUUM
 53727       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
 53728       if( pBt->autoVacuum ){
 53729         do{
 53730           pgnoOvfl++;
 53731         } while( 
 53732           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
 53733         );
 53735 #endif
 53736       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
 53737 #ifndef SQLITE_OMIT_AUTOVACUUM
 53738       /* If the database supports auto-vacuum, and the second or subsequent
 53739       ** overflow page is being allocated, add an entry to the pointer-map
 53740       ** for that page now. 
 53741       **
 53742       ** If this is the first overflow page, then write a partial entry 
 53743       ** to the pointer-map. If we write nothing to this pointer-map slot,
 53744       ** then the optimistic overflow chain processing in clearCell()
 53745       ** may misinterpret the uninitialised values and delete the
 53746       ** wrong pages from the database.
 53747       */
 53748       if( pBt->autoVacuum && rc==SQLITE_OK ){
 53749         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
 53750         ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
 53751         if( rc ){
 53752           releasePage(pOvfl);
 53755 #endif
 53756       if( rc ){
 53757         releasePage(pToRelease);
 53758         return rc;
 53761       /* If pToRelease is not zero than pPrior points into the data area
 53762       ** of pToRelease.  Make sure pToRelease is still writeable. */
 53763       assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
 53765       /* If pPrior is part of the data area of pPage, then make sure pPage
 53766       ** is still writeable */
 53767       assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
 53768             || sqlite3PagerIswriteable(pPage->pDbPage) );
 53770       put4byte(pPrior, pgnoOvfl);
 53771       releasePage(pToRelease);
 53772       pToRelease = pOvfl;
 53773       pPrior = pOvfl->aData;
 53774       put4byte(pPrior, 0);
 53775       pPayload = &pOvfl->aData[4];
 53776       spaceLeft = pBt->usableSize - 4;
 53778     n = nPayload;
 53779     if( n>spaceLeft ) n = spaceLeft;
 53781     /* If pToRelease is not zero than pPayload points into the data area
 53782     ** of pToRelease.  Make sure pToRelease is still writeable. */
 53783     assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
 53785     /* If pPayload is part of the data area of pPage, then make sure pPage
 53786     ** is still writeable */
 53787     assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
 53788             || sqlite3PagerIswriteable(pPage->pDbPage) );
 53790     if( nSrc>0 ){
 53791       if( n>nSrc ) n = nSrc;
 53792       assert( pSrc );
 53793       memcpy(pPayload, pSrc, n);
 53794     }else{
 53795       memset(pPayload, 0, n);
 53797     nPayload -= n;
 53798     pPayload += n;
 53799     pSrc += n;
 53800     nSrc -= n;
 53801     spaceLeft -= n;
 53802     if( nSrc==0 ){
 53803       nSrc = nData;
 53804       pSrc = pData;
 53807   releasePage(pToRelease);
 53808   return SQLITE_OK;
 53811 /*
 53812 ** Remove the i-th cell from pPage.  This routine effects pPage only.
 53813 ** The cell content is not freed or deallocated.  It is assumed that
 53814 ** the cell content has been copied someplace else.  This routine just
 53815 ** removes the reference to the cell from pPage.
 53816 **
 53817 ** "sz" must be the number of bytes in the cell.
 53818 */
 53819 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
 53820   u32 pc;         /* Offset to cell content of cell being deleted */
 53821   u8 *data;       /* pPage->aData */
 53822   u8 *ptr;        /* Used to move bytes around within data[] */
 53823   u8 *endPtr;     /* End of loop */
 53824   int rc;         /* The return code */
 53825   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
 53827   if( *pRC ) return;
 53829   assert( idx>=0 && idx<pPage->nCell );
 53830   assert( sz==cellSize(pPage, idx) );
 53831   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 53832   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 53833   data = pPage->aData;
 53834   ptr = &pPage->aCellIdx[2*idx];
 53835   pc = get2byte(ptr);
 53836   hdr = pPage->hdrOffset;
 53837   testcase( pc==get2byte(&data[hdr+5]) );
 53838   testcase( pc+sz==pPage->pBt->usableSize );
 53839   if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
 53840     *pRC = SQLITE_CORRUPT_BKPT;
 53841     return;
 53843   rc = freeSpace(pPage, pc, sz);
 53844   if( rc ){
 53845     *pRC = rc;
 53846     return;
 53848   endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
 53849   assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
 53850   while( ptr<endPtr ){
 53851     *(u16*)ptr = *(u16*)&ptr[2];
 53852     ptr += 2;
 53854   pPage->nCell--;
 53855   put2byte(&data[hdr+3], pPage->nCell);
 53856   pPage->nFree += 2;
 53859 /*
 53860 ** Insert a new cell on pPage at cell index "i".  pCell points to the
 53861 ** content of the cell.
 53862 **
 53863 ** If the cell content will fit on the page, then put it there.  If it
 53864 ** will not fit, then make a copy of the cell content into pTemp if
 53865 ** pTemp is not null.  Regardless of pTemp, allocate a new entry
 53866 ** in pPage->apOvfl[] and make it point to the cell content (either
 53867 ** in pTemp or the original pCell) and also record its index. 
 53868 ** Allocating a new entry in pPage->aCell[] implies that 
 53869 ** pPage->nOverflow is incremented.
 53870 **
 53871 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
 53872 ** cell. The caller will overwrite them after this function returns. If
 53873 ** nSkip is non-zero, then pCell may not point to an invalid memory location 
 53874 ** (but pCell+nSkip is always valid).
 53875 */
 53876 static void insertCell(
 53877   MemPage *pPage,   /* Page into which we are copying */
 53878   int i,            /* New cell becomes the i-th cell of the page */
 53879   u8 *pCell,        /* Content of the new cell */
 53880   int sz,           /* Bytes of content in pCell */
 53881   u8 *pTemp,        /* Temp storage space for pCell, if needed */
 53882   Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
 53883   int *pRC          /* Read and write return code from here */
 53884 ){
 53885   int idx = 0;      /* Where to write new cell content in data[] */
 53886   int j;            /* Loop counter */
 53887   int end;          /* First byte past the last cell pointer in data[] */
 53888   int ins;          /* Index in data[] where new cell pointer is inserted */
 53889   int cellOffset;   /* Address of first cell pointer in data[] */
 53890   u8 *data;         /* The content of the whole page */
 53891   u8 *ptr;          /* Used for moving information around in data[] */
 53892   u8 *endPtr;       /* End of the loop */
 53894   int nSkip = (iChild ? 4 : 0);
 53896   if( *pRC ) return;
 53898   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
 53899   assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
 53900   assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
 53901   assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
 53902   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 53903   /* The cell should normally be sized correctly.  However, when moving a
 53904   ** malformed cell from a leaf page to an interior page, if the cell size
 53905   ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
 53906   ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
 53907   ** the term after the || in the following assert(). */
 53908   assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
 53909   if( pPage->nOverflow || sz+2>pPage->nFree ){
 53910     if( pTemp ){
 53911       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
 53912       pCell = pTemp;
 53914     if( iChild ){
 53915       put4byte(pCell, iChild);
 53917     j = pPage->nOverflow++;
 53918     assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
 53919     pPage->apOvfl[j] = pCell;
 53920     pPage->aiOvfl[j] = (u16)i;
 53921   }else{
 53922     int rc = sqlite3PagerWrite(pPage->pDbPage);
 53923     if( rc!=SQLITE_OK ){
 53924       *pRC = rc;
 53925       return;
 53927     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 53928     data = pPage->aData;
 53929     cellOffset = pPage->cellOffset;
 53930     end = cellOffset + 2*pPage->nCell;
 53931     ins = cellOffset + 2*i;
 53932     rc = allocateSpace(pPage, sz, &idx);
 53933     if( rc ){ *pRC = rc; return; }
 53934     /* The allocateSpace() routine guarantees the following two properties
 53935     ** if it returns success */
 53936     assert( idx >= end+2 );
 53937     assert( idx+sz <= (int)pPage->pBt->usableSize );
 53938     pPage->nCell++;
 53939     pPage->nFree -= (u16)(2 + sz);
 53940     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
 53941     if( iChild ){
 53942       put4byte(&data[idx], iChild);
 53944     ptr = &data[end];
 53945     endPtr = &data[ins];
 53946     assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
 53947     while( ptr>endPtr ){
 53948       *(u16*)ptr = *(u16*)&ptr[-2];
 53949       ptr -= 2;
 53951     put2byte(&data[ins], idx);
 53952     put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
 53953 #ifndef SQLITE_OMIT_AUTOVACUUM
 53954     if( pPage->pBt->autoVacuum ){
 53955       /* The cell may contain a pointer to an overflow page. If so, write
 53956       ** the entry for the overflow page into the pointer map.
 53957       */
 53958       ptrmapPutOvflPtr(pPage, pCell, pRC);
 53960 #endif
 53964 /*
 53965 ** Add a list of cells to a page.  The page should be initially empty.
 53966 ** The cells are guaranteed to fit on the page.
 53967 */
 53968 static void assemblePage(
 53969   MemPage *pPage,   /* The page to be assemblied */
 53970   int nCell,        /* The number of cells to add to this page */
 53971   u8 **apCell,      /* Pointers to cell bodies */
 53972   u16 *aSize        /* Sizes of the cells */
 53973 ){
 53974   int i;            /* Loop counter */
 53975   u8 *pCellptr;     /* Address of next cell pointer */
 53976   int cellbody;     /* Address of next cell body */
 53977   u8 * const data = pPage->aData;             /* Pointer to data for pPage */
 53978   const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
 53979   const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
 53981   assert( pPage->nOverflow==0 );
 53982   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 53983   assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
 53984             && (int)MX_CELL(pPage->pBt)<=10921);
 53985   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 53987   /* Check that the page has just been zeroed by zeroPage() */
 53988   assert( pPage->nCell==0 );
 53989   assert( get2byteNotZero(&data[hdr+5])==nUsable );
 53991   pCellptr = &pPage->aCellIdx[nCell*2];
 53992   cellbody = nUsable;
 53993   for(i=nCell-1; i>=0; i--){
 53994     u16 sz = aSize[i];
 53995     pCellptr -= 2;
 53996     cellbody -= sz;
 53997     put2byte(pCellptr, cellbody);
 53998     memcpy(&data[cellbody], apCell[i], sz);
 54000   put2byte(&data[hdr+3], nCell);
 54001   put2byte(&data[hdr+5], cellbody);
 54002   pPage->nFree -= (nCell*2 + nUsable - cellbody);
 54003   pPage->nCell = (u16)nCell;
 54006 /*
 54007 ** The following parameters determine how many adjacent pages get involved
 54008 ** in a balancing operation.  NN is the number of neighbors on either side
 54009 ** of the page that participate in the balancing operation.  NB is the
 54010 ** total number of pages that participate, including the target page and
 54011 ** NN neighbors on either side.
 54012 **
 54013 ** The minimum value of NN is 1 (of course).  Increasing NN above 1
 54014 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
 54015 ** in exchange for a larger degradation in INSERT and UPDATE performance.
 54016 ** The value of NN appears to give the best results overall.
 54017 */
 54018 #define NN 1             /* Number of neighbors on either side of pPage */
 54019 #define NB (NN*2+1)      /* Total pages involved in the balance */
 54022 #ifndef SQLITE_OMIT_QUICKBALANCE
 54023 /*
 54024 ** This version of balance() handles the common special case where
 54025 ** a new entry is being inserted on the extreme right-end of the
 54026 ** tree, in other words, when the new entry will become the largest
 54027 ** entry in the tree.
 54028 **
 54029 ** Instead of trying to balance the 3 right-most leaf pages, just add
 54030 ** a new page to the right-hand side and put the one new entry in
 54031 ** that page.  This leaves the right side of the tree somewhat
 54032 ** unbalanced.  But odds are that we will be inserting new entries
 54033 ** at the end soon afterwards so the nearly empty page will quickly
 54034 ** fill up.  On average.
 54035 **
 54036 ** pPage is the leaf page which is the right-most page in the tree.
 54037 ** pParent is its parent.  pPage must have a single overflow entry
 54038 ** which is also the right-most entry on the page.
 54039 **
 54040 ** The pSpace buffer is used to store a temporary copy of the divider
 54041 ** cell that will be inserted into pParent. Such a cell consists of a 4
 54042 ** byte page number followed by a variable length integer. In other
 54043 ** words, at most 13 bytes. Hence the pSpace buffer must be at
 54044 ** least 13 bytes in size.
 54045 */
 54046 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
 54047   BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
 54048   MemPage *pNew;                       /* Newly allocated page */
 54049   int rc;                              /* Return Code */
 54050   Pgno pgnoNew;                        /* Page number of pNew */
 54052   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 54053   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 54054   assert( pPage->nOverflow==1 );
 54056   /* This error condition is now caught prior to reaching this function */
 54057   if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
 54059   /* Allocate a new page. This page will become the right-sibling of 
 54060   ** pPage. Make the parent page writable, so that the new divider cell
 54061   ** may be inserted. If both these operations are successful, proceed.
 54062   */
 54063   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
 54065   if( rc==SQLITE_OK ){
 54067     u8 *pOut = &pSpace[4];
 54068     u8 *pCell = pPage->apOvfl[0];
 54069     u16 szCell = cellSizePtr(pPage, pCell);
 54070     u8 *pStop;
 54072     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
 54073     assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
 54074     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
 54075     assemblePage(pNew, 1, &pCell, &szCell);
 54077     /* If this is an auto-vacuum database, update the pointer map
 54078     ** with entries for the new page, and any pointer from the 
 54079     ** cell on the page to an overflow page. If either of these
 54080     ** operations fails, the return code is set, but the contents
 54081     ** of the parent page are still manipulated by thh code below.
 54082     ** That is Ok, at this point the parent page is guaranteed to
 54083     ** be marked as dirty. Returning an error code will cause a
 54084     ** rollback, undoing any changes made to the parent page.
 54085     */
 54086     if( ISAUTOVACUUM ){
 54087       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
 54088       if( szCell>pNew->minLocal ){
 54089         ptrmapPutOvflPtr(pNew, pCell, &rc);
 54093     /* Create a divider cell to insert into pParent. The divider cell
 54094     ** consists of a 4-byte page number (the page number of pPage) and
 54095     ** a variable length key value (which must be the same value as the
 54096     ** largest key on pPage).
 54097     **
 54098     ** To find the largest key value on pPage, first find the right-most 
 54099     ** cell on pPage. The first two fields of this cell are the 
 54100     ** record-length (a variable length integer at most 32-bits in size)
 54101     ** and the key value (a variable length integer, may have any value).
 54102     ** The first of the while(...) loops below skips over the record-length
 54103     ** field. The second while(...) loop copies the key value from the
 54104     ** cell on pPage into the pSpace buffer.
 54105     */
 54106     pCell = findCell(pPage, pPage->nCell-1);
 54107     pStop = &pCell[9];
 54108     while( (*(pCell++)&0x80) && pCell<pStop );
 54109     pStop = &pCell[9];
 54110     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
 54112     /* Insert the new divider cell into pParent. */
 54113     insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
 54114                0, pPage->pgno, &rc);
 54116     /* Set the right-child pointer of pParent to point to the new page. */
 54117     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
 54119     /* Release the reference to the new page. */
 54120     releasePage(pNew);
 54123   return rc;
 54125 #endif /* SQLITE_OMIT_QUICKBALANCE */
 54127 #if 0
 54128 /*
 54129 ** This function does not contribute anything to the operation of SQLite.
 54130 ** it is sometimes activated temporarily while debugging code responsible 
 54131 ** for setting pointer-map entries.
 54132 */
 54133 static int ptrmapCheckPages(MemPage **apPage, int nPage){
 54134   int i, j;
 54135   for(i=0; i<nPage; i++){
 54136     Pgno n;
 54137     u8 e;
 54138     MemPage *pPage = apPage[i];
 54139     BtShared *pBt = pPage->pBt;
 54140     assert( pPage->isInit );
 54142     for(j=0; j<pPage->nCell; j++){
 54143       CellInfo info;
 54144       u8 *z;
 54146       z = findCell(pPage, j);
 54147       btreeParseCellPtr(pPage, z, &info);
 54148       if( info.iOverflow ){
 54149         Pgno ovfl = get4byte(&z[info.iOverflow]);
 54150         ptrmapGet(pBt, ovfl, &e, &n);
 54151         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
 54153       if( !pPage->leaf ){
 54154         Pgno child = get4byte(z);
 54155         ptrmapGet(pBt, child, &e, &n);
 54156         assert( n==pPage->pgno && e==PTRMAP_BTREE );
 54159     if( !pPage->leaf ){
 54160       Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 54161       ptrmapGet(pBt, child, &e, &n);
 54162       assert( n==pPage->pgno && e==PTRMAP_BTREE );
 54165   return 1;
 54167 #endif
 54169 /*
 54170 ** This function is used to copy the contents of the b-tree node stored 
 54171 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
 54172 ** the pointer-map entries for each child page are updated so that the
 54173 ** parent page stored in the pointer map is page pTo. If pFrom contained
 54174 ** any cells with overflow page pointers, then the corresponding pointer
 54175 ** map entries are also updated so that the parent page is page pTo.
 54176 **
 54177 ** If pFrom is currently carrying any overflow cells (entries in the
 54178 ** MemPage.apOvfl[] array), they are not copied to pTo. 
 54179 **
 54180 ** Before returning, page pTo is reinitialized using btreeInitPage().
 54181 **
 54182 ** The performance of this function is not critical. It is only used by 
 54183 ** the balance_shallower() and balance_deeper() procedures, neither of
 54184 ** which are called often under normal circumstances.
 54185 */
 54186 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
 54187   if( (*pRC)==SQLITE_OK ){
 54188     BtShared * const pBt = pFrom->pBt;
 54189     u8 * const aFrom = pFrom->aData;
 54190     u8 * const aTo = pTo->aData;
 54191     int const iFromHdr = pFrom->hdrOffset;
 54192     int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
 54193     int rc;
 54194     int iData;
 54197     assert( pFrom->isInit );
 54198     assert( pFrom->nFree>=iToHdr );
 54199     assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
 54201     /* Copy the b-tree node content from page pFrom to page pTo. */
 54202     iData = get2byte(&aFrom[iFromHdr+5]);
 54203     memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
 54204     memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
 54206     /* Reinitialize page pTo so that the contents of the MemPage structure
 54207     ** match the new data. The initialization of pTo can actually fail under
 54208     ** fairly obscure circumstances, even though it is a copy of initialized 
 54209     ** page pFrom.
 54210     */
 54211     pTo->isInit = 0;
 54212     rc = btreeInitPage(pTo);
 54213     if( rc!=SQLITE_OK ){
 54214       *pRC = rc;
 54215       return;
 54218     /* If this is an auto-vacuum database, update the pointer-map entries
 54219     ** for any b-tree or overflow pages that pTo now contains the pointers to.
 54220     */
 54221     if( ISAUTOVACUUM ){
 54222       *pRC = setChildPtrmaps(pTo);
 54227 /*
 54228 ** This routine redistributes cells on the iParentIdx'th child of pParent
 54229 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
 54230 ** same amount of free space. Usually a single sibling on either side of the
 54231 ** page are used in the balancing, though both siblings might come from one
 54232 ** side if the page is the first or last child of its parent. If the page 
 54233 ** has fewer than 2 siblings (something which can only happen if the page
 54234 ** is a root page or a child of a root page) then all available siblings
 54235 ** participate in the balancing.
 54236 **
 54237 ** The number of siblings of the page might be increased or decreased by 
 54238 ** one or two in an effort to keep pages nearly full but not over full. 
 54239 **
 54240 ** Note that when this routine is called, some of the cells on the page
 54241 ** might not actually be stored in MemPage.aData[]. This can happen
 54242 ** if the page is overfull. This routine ensures that all cells allocated
 54243 ** to the page and its siblings fit into MemPage.aData[] before returning.
 54244 **
 54245 ** In the course of balancing the page and its siblings, cells may be
 54246 ** inserted into or removed from the parent page (pParent). Doing so
 54247 ** may cause the parent page to become overfull or underfull. If this
 54248 ** happens, it is the responsibility of the caller to invoke the correct
 54249 ** balancing routine to fix this problem (see the balance() routine). 
 54250 **
 54251 ** If this routine fails for any reason, it might leave the database
 54252 ** in a corrupted state. So if this routine fails, the database should
 54253 ** be rolled back.
 54254 **
 54255 ** The third argument to this function, aOvflSpace, is a pointer to a
 54256 ** buffer big enough to hold one page. If while inserting cells into the parent
 54257 ** page (pParent) the parent page becomes overfull, this buffer is
 54258 ** used to store the parent's overflow cells. Because this function inserts
 54259 ** a maximum of four divider cells into the parent page, and the maximum
 54260 ** size of a cell stored within an internal node is always less than 1/4
 54261 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
 54262 ** enough for all overflow cells.
 54263 **
 54264 ** If aOvflSpace is set to a null pointer, this function returns 
 54265 ** SQLITE_NOMEM.
 54266 */
 54267 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
 54268 #pragma optimize("", off)
 54269 #endif
 54270 static int balance_nonroot(
 54271   MemPage *pParent,               /* Parent page of siblings being balanced */
 54272   int iParentIdx,                 /* Index of "the page" in pParent */
 54273   u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
 54274   int isRoot,                     /* True if pParent is a root-page */
 54275   int bBulk                       /* True if this call is part of a bulk load */
 54276 ){
 54277   BtShared *pBt;               /* The whole database */
 54278   int nCell = 0;               /* Number of cells in apCell[] */
 54279   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
 54280   int nNew = 0;                /* Number of pages in apNew[] */
 54281   int nOld;                    /* Number of pages in apOld[] */
 54282   int i, j, k;                 /* Loop counters */
 54283   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
 54284   int rc = SQLITE_OK;          /* The return code */
 54285   u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
 54286   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
 54287   int usableSpace;             /* Bytes in pPage beyond the header */
 54288   int pageFlags;               /* Value of pPage->aData[0] */
 54289   int subtotal;                /* Subtotal of bytes in cells on one page */
 54290   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
 54291   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
 54292   int szScratch;               /* Size of scratch memory requested */
 54293   MemPage *apOld[NB];          /* pPage and up to two siblings */
 54294   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
 54295   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
 54296   u8 *pRight;                  /* Location in parent of right-sibling pointer */
 54297   u8 *apDiv[NB-1];             /* Divider cells in pParent */
 54298   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
 54299   int szNew[NB+2];             /* Combined size of cells place on i-th page */
 54300   u8 **apCell = 0;             /* All cells begin balanced */
 54301   u16 *szCell;                 /* Local size of all cells in apCell[] */
 54302   u8 *aSpace1;                 /* Space for copies of dividers cells */
 54303   Pgno pgno;                   /* Temp var to store a page number in */
 54305   pBt = pParent->pBt;
 54306   assert( sqlite3_mutex_held(pBt->mutex) );
 54307   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 54309 #if 0
 54310   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
 54311 #endif
 54313   /* At this point pParent may have at most one overflow cell. And if
 54314   ** this overflow cell is present, it must be the cell with 
 54315   ** index iParentIdx. This scenario comes about when this function
 54316   ** is called (indirectly) from sqlite3BtreeDelete().
 54317   */
 54318   assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
 54319   assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
 54321   if( !aOvflSpace ){
 54322     return SQLITE_NOMEM;
 54325   /* Find the sibling pages to balance. Also locate the cells in pParent 
 54326   ** that divide the siblings. An attempt is made to find NN siblings on 
 54327   ** either side of pPage. More siblings are taken from one side, however, 
 54328   ** if there are fewer than NN siblings on the other side. If pParent
 54329   ** has NB or fewer children then all children of pParent are taken.  
 54330   **
 54331   ** This loop also drops the divider cells from the parent page. This
 54332   ** way, the remainder of the function does not have to deal with any
 54333   ** overflow cells in the parent page, since if any existed they will
 54334   ** have already been removed.
 54335   */
 54336   i = pParent->nOverflow + pParent->nCell;
 54337   if( i<2 ){
 54338     nxDiv = 0;
 54339   }else{
 54340     assert( bBulk==0 || bBulk==1 );
 54341     if( iParentIdx==0 ){                 
 54342       nxDiv = 0;
 54343     }else if( iParentIdx==i ){
 54344       nxDiv = i-2+bBulk;
 54345     }else{
 54346       assert( bBulk==0 );
 54347       nxDiv = iParentIdx-1;
 54349     i = 2-bBulk;
 54351   nOld = i+1;
 54352   if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
 54353     pRight = &pParent->aData[pParent->hdrOffset+8];
 54354   }else{
 54355     pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
 54357   pgno = get4byte(pRight);
 54358   while( 1 ){
 54359     rc = getAndInitPage(pBt, pgno, &apOld[i]);
 54360     if( rc ){
 54361       memset(apOld, 0, (i+1)*sizeof(MemPage*));
 54362       goto balance_cleanup;
 54364     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
 54365     if( (i--)==0 ) break;
 54367     if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
 54368       apDiv[i] = pParent->apOvfl[0];
 54369       pgno = get4byte(apDiv[i]);
 54370       szNew[i] = cellSizePtr(pParent, apDiv[i]);
 54371       pParent->nOverflow = 0;
 54372     }else{
 54373       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
 54374       pgno = get4byte(apDiv[i]);
 54375       szNew[i] = cellSizePtr(pParent, apDiv[i]);
 54377       /* Drop the cell from the parent page. apDiv[i] still points to
 54378       ** the cell within the parent, even though it has been dropped.
 54379       ** This is safe because dropping a cell only overwrites the first
 54380       ** four bytes of it, and this function does not need the first
 54381       ** four bytes of the divider cell. So the pointer is safe to use
 54382       ** later on.  
 54383       **
 54384       ** But not if we are in secure-delete mode. In secure-delete mode,
 54385       ** the dropCell() routine will overwrite the entire cell with zeroes.
 54386       ** In this case, temporarily copy the cell into the aOvflSpace[]
 54387       ** buffer. It will be copied out again as soon as the aSpace[] buffer
 54388       ** is allocated.  */
 54389       if( pBt->btsFlags & BTS_SECURE_DELETE ){
 54390         int iOff;
 54392         iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
 54393         if( (iOff+szNew[i])>(int)pBt->usableSize ){
 54394           rc = SQLITE_CORRUPT_BKPT;
 54395           memset(apOld, 0, (i+1)*sizeof(MemPage*));
 54396           goto balance_cleanup;
 54397         }else{
 54398           memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
 54399           apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
 54402       dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
 54406   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
 54407   ** alignment */
 54408   nMaxCells = (nMaxCells + 3)&~3;
 54410   /*
 54411   ** Allocate space for memory structures
 54412   */
 54413   k = pBt->pageSize + ROUND8(sizeof(MemPage));
 54414   szScratch =
 54415        nMaxCells*sizeof(u8*)                       /* apCell */
 54416      + nMaxCells*sizeof(u16)                       /* szCell */
 54417      + pBt->pageSize                               /* aSpace1 */
 54418      + k*nOld;                                     /* Page copies (apCopy) */
 54419   apCell = sqlite3ScratchMalloc( szScratch ); 
 54420   if( apCell==0 ){
 54421     rc = SQLITE_NOMEM;
 54422     goto balance_cleanup;
 54424   szCell = (u16*)&apCell[nMaxCells];
 54425   aSpace1 = (u8*)&szCell[nMaxCells];
 54426   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
 54428   /*
 54429   ** Load pointers to all cells on sibling pages and the divider cells
 54430   ** into the local apCell[] array.  Make copies of the divider cells
 54431   ** into space obtained from aSpace1[] and remove the divider cells
 54432   ** from pParent.
 54433   **
 54434   ** If the siblings are on leaf pages, then the child pointers of the
 54435   ** divider cells are stripped from the cells before they are copied
 54436   ** into aSpace1[].  In this way, all cells in apCell[] are without
 54437   ** child pointers.  If siblings are not leaves, then all cell in
 54438   ** apCell[] include child pointers.  Either way, all cells in apCell[]
 54439   ** are alike.
 54440   **
 54441   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
 54442   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
 54443   */
 54444   leafCorrection = apOld[0]->leaf*4;
 54445   leafData = apOld[0]->hasData;
 54446   for(i=0; i<nOld; i++){
 54447     int limit;
 54449     /* Before doing anything else, take a copy of the i'th original sibling
 54450     ** The rest of this function will use data from the copies rather
 54451     ** that the original pages since the original pages will be in the
 54452     ** process of being overwritten.  */
 54453     MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
 54454     memcpy(pOld, apOld[i], sizeof(MemPage));
 54455     pOld->aData = (void*)&pOld[1];
 54456     memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
 54458     limit = pOld->nCell+pOld->nOverflow;
 54459     if( pOld->nOverflow>0 ){
 54460       for(j=0; j<limit; j++){
 54461         assert( nCell<nMaxCells );
 54462         apCell[nCell] = findOverflowCell(pOld, j);
 54463         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 54464         nCell++;
 54466     }else{
 54467       u8 *aData = pOld->aData;
 54468       u16 maskPage = pOld->maskPage;
 54469       u16 cellOffset = pOld->cellOffset;
 54470       for(j=0; j<limit; j++){
 54471         assert( nCell<nMaxCells );
 54472         apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
 54473         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 54474         nCell++;
 54477     if( i<nOld-1 && !leafData){
 54478       u16 sz = (u16)szNew[i];
 54479       u8 *pTemp;
 54480       assert( nCell<nMaxCells );
 54481       szCell[nCell] = sz;
 54482       pTemp = &aSpace1[iSpace1];
 54483       iSpace1 += sz;
 54484       assert( sz<=pBt->maxLocal+23 );
 54485       assert( iSpace1 <= (int)pBt->pageSize );
 54486       memcpy(pTemp, apDiv[i], sz);
 54487       apCell[nCell] = pTemp+leafCorrection;
 54488       assert( leafCorrection==0 || leafCorrection==4 );
 54489       szCell[nCell] = szCell[nCell] - leafCorrection;
 54490       if( !pOld->leaf ){
 54491         assert( leafCorrection==0 );
 54492         assert( pOld->hdrOffset==0 );
 54493         /* The right pointer of the child page pOld becomes the left
 54494         ** pointer of the divider cell */
 54495         memcpy(apCell[nCell], &pOld->aData[8], 4);
 54496       }else{
 54497         assert( leafCorrection==4 );
 54498         if( szCell[nCell]<4 ){
 54499           /* Do not allow any cells smaller than 4 bytes. */
 54500           szCell[nCell] = 4;
 54503       nCell++;
 54507   /*
 54508   ** Figure out the number of pages needed to hold all nCell cells.
 54509   ** Store this number in "k".  Also compute szNew[] which is the total
 54510   ** size of all cells on the i-th page and cntNew[] which is the index
 54511   ** in apCell[] of the cell that divides page i from page i+1.  
 54512   ** cntNew[k] should equal nCell.
 54513   **
 54514   ** Values computed by this block:
 54515   **
 54516   **           k: The total number of sibling pages
 54517   **    szNew[i]: Spaced used on the i-th sibling page.
 54518   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
 54519   **              the right of the i-th sibling page.
 54520   ** usableSpace: Number of bytes of space available on each sibling.
 54521   ** 
 54522   */
 54523   usableSpace = pBt->usableSize - 12 + leafCorrection;
 54524   for(subtotal=k=i=0; i<nCell; i++){
 54525     assert( i<nMaxCells );
 54526     subtotal += szCell[i] + 2;
 54527     if( subtotal > usableSpace ){
 54528       szNew[k] = subtotal - szCell[i];
 54529       cntNew[k] = i;
 54530       if( leafData ){ i--; }
 54531       subtotal = 0;
 54532       k++;
 54533       if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
 54536   szNew[k] = subtotal;
 54537   cntNew[k] = nCell;
 54538   k++;
 54540   /*
 54541   ** The packing computed by the previous block is biased toward the siblings
 54542   ** on the left side.  The left siblings are always nearly full, while the
 54543   ** right-most sibling might be nearly empty.  This block of code attempts
 54544   ** to adjust the packing of siblings to get a better balance.
 54545   **
 54546   ** This adjustment is more than an optimization.  The packing above might
 54547   ** be so out of balance as to be illegal.  For example, the right-most
 54548   ** sibling might be completely empty.  This adjustment is not optional.
 54549   */
 54550   for(i=k-1; i>0; i--){
 54551     int szRight = szNew[i];  /* Size of sibling on the right */
 54552     int szLeft = szNew[i-1]; /* Size of sibling on the left */
 54553     int r;              /* Index of right-most cell in left sibling */
 54554     int d;              /* Index of first cell to the left of right sibling */
 54556     r = cntNew[i-1] - 1;
 54557     d = r + 1 - leafData;
 54558     assert( d<nMaxCells );
 54559     assert( r<nMaxCells );
 54560     while( szRight==0 
 54561        || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
 54562     ){
 54563       szRight += szCell[d] + 2;
 54564       szLeft -= szCell[r] + 2;
 54565       cntNew[i-1]--;
 54566       r = cntNew[i-1] - 1;
 54567       d = r + 1 - leafData;
 54569     szNew[i] = szRight;
 54570     szNew[i-1] = szLeft;
 54573   /* Either we found one or more cells (cntnew[0])>0) or pPage is
 54574   ** a virtual root page.  A virtual root page is when the real root
 54575   ** page is page 1 and we are the only child of that page.
 54576   **
 54577   ** UPDATE:  The assert() below is not necessarily true if the database
 54578   ** file is corrupt.  The corruption will be detected and reported later
 54579   ** in this procedure so there is no need to act upon it now.
 54580   */
 54581 #if 0
 54582   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
 54583 #endif
 54585   TRACE(("BALANCE: old: %d %d %d  ",
 54586     apOld[0]->pgno, 
 54587     nOld>=2 ? apOld[1]->pgno : 0,
 54588     nOld>=3 ? apOld[2]->pgno : 0
 54589   ));
 54591   /*
 54592   ** Allocate k new pages.  Reuse old pages where possible.
 54593   */
 54594   if( apOld[0]->pgno<=1 ){
 54595     rc = SQLITE_CORRUPT_BKPT;
 54596     goto balance_cleanup;
 54598   pageFlags = apOld[0]->aData[0];
 54599   for(i=0; i<k; i++){
 54600     MemPage *pNew;
 54601     if( i<nOld ){
 54602       pNew = apNew[i] = apOld[i];
 54603       apOld[i] = 0;
 54604       rc = sqlite3PagerWrite(pNew->pDbPage);
 54605       nNew++;
 54606       if( rc ) goto balance_cleanup;
 54607     }else{
 54608       assert( i>0 );
 54609       rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
 54610       if( rc ) goto balance_cleanup;
 54611       apNew[i] = pNew;
 54612       nNew++;
 54614       /* Set the pointer-map entry for the new sibling page. */
 54615       if( ISAUTOVACUUM ){
 54616         ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
 54617         if( rc!=SQLITE_OK ){
 54618           goto balance_cleanup;
 54624   /* Free any old pages that were not reused as new pages.
 54625   */
 54626   while( i<nOld ){
 54627     freePage(apOld[i], &rc);
 54628     if( rc ) goto balance_cleanup;
 54629     releasePage(apOld[i]);
 54630     apOld[i] = 0;
 54631     i++;
 54634   /*
 54635   ** Put the new pages in accending order.  This helps to
 54636   ** keep entries in the disk file in order so that a scan
 54637   ** of the table is a linear scan through the file.  That
 54638   ** in turn helps the operating system to deliver pages
 54639   ** from the disk more rapidly.
 54640   **
 54641   ** An O(n^2) insertion sort algorithm is used, but since
 54642   ** n is never more than NB (a small constant), that should
 54643   ** not be a problem.
 54644   **
 54645   ** When NB==3, this one optimization makes the database
 54646   ** about 25% faster for large insertions and deletions.
 54647   */
 54648   for(i=0; i<k-1; i++){
 54649     int minV = apNew[i]->pgno;
 54650     int minI = i;
 54651     for(j=i+1; j<k; j++){
 54652       if( apNew[j]->pgno<(unsigned)minV ){
 54653         minI = j;
 54654         minV = apNew[j]->pgno;
 54657     if( minI>i ){
 54658       MemPage *pT;
 54659       pT = apNew[i];
 54660       apNew[i] = apNew[minI];
 54661       apNew[minI] = pT;
 54664   TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
 54665     apNew[0]->pgno, szNew[0],
 54666     nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
 54667     nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
 54668     nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
 54669     nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
 54671   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 54672   put4byte(pRight, apNew[nNew-1]->pgno);
 54674   /*
 54675   ** Evenly distribute the data in apCell[] across the new pages.
 54676   ** Insert divider cells into pParent as necessary.
 54677   */
 54678   j = 0;
 54679   for(i=0; i<nNew; i++){
 54680     /* Assemble the new sibling page. */
 54681     MemPage *pNew = apNew[i];
 54682     assert( j<nMaxCells );
 54683     zeroPage(pNew, pageFlags);
 54684     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
 54685     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
 54686     assert( pNew->nOverflow==0 );
 54688     j = cntNew[i];
 54690     /* If the sibling page assembled above was not the right-most sibling,
 54691     ** insert a divider cell into the parent page.
 54692     */
 54693     assert( i<nNew-1 || j==nCell );
 54694     if( j<nCell ){
 54695       u8 *pCell;
 54696       u8 *pTemp;
 54697       int sz;
 54699       assert( j<nMaxCells );
 54700       pCell = apCell[j];
 54701       sz = szCell[j] + leafCorrection;
 54702       pTemp = &aOvflSpace[iOvflSpace];
 54703       if( !pNew->leaf ){
 54704         memcpy(&pNew->aData[8], pCell, 4);
 54705       }else if( leafData ){
 54706         /* If the tree is a leaf-data tree, and the siblings are leaves, 
 54707         ** then there is no divider cell in apCell[]. Instead, the divider 
 54708         ** cell consists of the integer key for the right-most cell of 
 54709         ** the sibling-page assembled above only.
 54710         */
 54711         CellInfo info;
 54712         j--;
 54713         btreeParseCellPtr(pNew, apCell[j], &info);
 54714         pCell = pTemp;
 54715         sz = 4 + putVarint(&pCell[4], info.nKey);
 54716         pTemp = 0;
 54717       }else{
 54718         pCell -= 4;
 54719         /* Obscure case for non-leaf-data trees: If the cell at pCell was
 54720         ** previously stored on a leaf node, and its reported size was 4
 54721         ** bytes, then it may actually be smaller than this 
 54722         ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
 54723         ** any cell). But it is important to pass the correct size to 
 54724         ** insertCell(), so reparse the cell now.
 54725         **
 54726         ** Note that this can never happen in an SQLite data file, as all
 54727         ** cells are at least 4 bytes. It only happens in b-trees used
 54728         ** to evaluate "IN (SELECT ...)" and similar clauses.
 54729         */
 54730         if( szCell[j]==4 ){
 54731           assert(leafCorrection==4);
 54732           sz = cellSizePtr(pParent, pCell);
 54735       iOvflSpace += sz;
 54736       assert( sz<=pBt->maxLocal+23 );
 54737       assert( iOvflSpace <= (int)pBt->pageSize );
 54738       insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
 54739       if( rc!=SQLITE_OK ) goto balance_cleanup;
 54740       assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 54742       j++;
 54743       nxDiv++;
 54746   assert( j==nCell );
 54747   assert( nOld>0 );
 54748   assert( nNew>0 );
 54749   if( (pageFlags & PTF_LEAF)==0 ){
 54750     u8 *zChild = &apCopy[nOld-1]->aData[8];
 54751     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
 54754   if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
 54755     /* The root page of the b-tree now contains no cells. The only sibling
 54756     ** page is the right-child of the parent. Copy the contents of the
 54757     ** child page into the parent, decreasing the overall height of the
 54758     ** b-tree structure by one. This is described as the "balance-shallower"
 54759     ** sub-algorithm in some documentation.
 54760     **
 54761     ** If this is an auto-vacuum database, the call to copyNodeContent() 
 54762     ** sets all pointer-map entries corresponding to database image pages 
 54763     ** for which the pointer is stored within the content being copied.
 54764     **
 54765     ** The second assert below verifies that the child page is defragmented
 54766     ** (it must be, as it was just reconstructed using assemblePage()). This
 54767     ** is important if the parent page happens to be page 1 of the database
 54768     ** image.  */
 54769     assert( nNew==1 );
 54770     assert( apNew[0]->nFree == 
 54771         (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) 
 54772     );
 54773     copyNodeContent(apNew[0], pParent, &rc);
 54774     freePage(apNew[0], &rc);
 54775   }else if( ISAUTOVACUUM ){
 54776     /* Fix the pointer-map entries for all the cells that were shifted around. 
 54777     ** There are several different types of pointer-map entries that need to
 54778     ** be dealt with by this routine. Some of these have been set already, but
 54779     ** many have not. The following is a summary:
 54780     **
 54781     **   1) The entries associated with new sibling pages that were not
 54782     **      siblings when this function was called. These have already
 54783     **      been set. We don't need to worry about old siblings that were
 54784     **      moved to the free-list - the freePage() code has taken care
 54785     **      of those.
 54786     **
 54787     **   2) The pointer-map entries associated with the first overflow
 54788     **      page in any overflow chains used by new divider cells. These 
 54789     **      have also already been taken care of by the insertCell() code.
 54790     **
 54791     **   3) If the sibling pages are not leaves, then the child pages of
 54792     **      cells stored on the sibling pages may need to be updated.
 54793     **
 54794     **   4) If the sibling pages are not internal intkey nodes, then any
 54795     **      overflow pages used by these cells may need to be updated
 54796     **      (internal intkey nodes never contain pointers to overflow pages).
 54797     **
 54798     **   5) If the sibling pages are not leaves, then the pointer-map
 54799     **      entries for the right-child pages of each sibling may need
 54800     **      to be updated.
 54801     **
 54802     ** Cases 1 and 2 are dealt with above by other code. The next
 54803     ** block deals with cases 3 and 4 and the one after that, case 5. Since
 54804     ** setting a pointer map entry is a relatively expensive operation, this
 54805     ** code only sets pointer map entries for child or overflow pages that have
 54806     ** actually moved between pages.  */
 54807     MemPage *pNew = apNew[0];
 54808     MemPage *pOld = apCopy[0];
 54809     int nOverflow = pOld->nOverflow;
 54810     int iNextOld = pOld->nCell + nOverflow;
 54811     int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1);
 54812     j = 0;                             /* Current 'old' sibling page */
 54813     k = 0;                             /* Current 'new' sibling page */
 54814     for(i=0; i<nCell; i++){
 54815       int isDivider = 0;
 54816       while( i==iNextOld ){
 54817         /* Cell i is the cell immediately following the last cell on old
 54818         ** sibling page j. If the siblings are not leaf pages of an
 54819         ** intkey b-tree, then cell i was a divider cell. */
 54820         assert( j+1 < ArraySize(apCopy) );
 54821         assert( j+1 < nOld );
 54822         pOld = apCopy[++j];
 54823         iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
 54824         if( pOld->nOverflow ){
 54825           nOverflow = pOld->nOverflow;
 54826           iOverflow = i + !leafData + pOld->aiOvfl[0];
 54828         isDivider = !leafData;  
 54831       assert(nOverflow>0 || iOverflow<i );
 54832       assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1);
 54833       assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1);
 54834       if( i==iOverflow ){
 54835         isDivider = 1;
 54836         if( (--nOverflow)>0 ){
 54837           iOverflow++;
 54841       if( i==cntNew[k] ){
 54842         /* Cell i is the cell immediately following the last cell on new
 54843         ** sibling page k. If the siblings are not leaf pages of an
 54844         ** intkey b-tree, then cell i is a divider cell.  */
 54845         pNew = apNew[++k];
 54846         if( !leafData ) continue;
 54848       assert( j<nOld );
 54849       assert( k<nNew );
 54851       /* If the cell was originally divider cell (and is not now) or
 54852       ** an overflow cell, or if the cell was located on a different sibling
 54853       ** page before the balancing, then the pointer map entries associated
 54854       ** with any child or overflow pages need to be updated.  */
 54855       if( isDivider || pOld->pgno!=pNew->pgno ){
 54856         if( !leafCorrection ){
 54857           ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
 54859         if( szCell[i]>pNew->minLocal ){
 54860           ptrmapPutOvflPtr(pNew, apCell[i], &rc);
 54865     if( !leafCorrection ){
 54866       for(i=0; i<nNew; i++){
 54867         u32 key = get4byte(&apNew[i]->aData[8]);
 54868         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
 54872 #if 0
 54873     /* The ptrmapCheckPages() contains assert() statements that verify that
 54874     ** all pointer map pages are set correctly. This is helpful while 
 54875     ** debugging. This is usually disabled because a corrupt database may
 54876     ** cause an assert() statement to fail.  */
 54877     ptrmapCheckPages(apNew, nNew);
 54878     ptrmapCheckPages(&pParent, 1);
 54879 #endif
 54882   assert( pParent->isInit );
 54883   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
 54884           nOld, nNew, nCell));
 54886   /*
 54887   ** Cleanup before returning.
 54888   */
 54889 balance_cleanup:
 54890   sqlite3ScratchFree(apCell);
 54891   for(i=0; i<nOld; i++){
 54892     releasePage(apOld[i]);
 54894   for(i=0; i<nNew; i++){
 54895     releasePage(apNew[i]);
 54898   return rc;
 54900 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
 54901 #pragma optimize("", on)
 54902 #endif
 54905 /*
 54906 ** This function is called when the root page of a b-tree structure is
 54907 ** overfull (has one or more overflow pages).
 54908 **
 54909 ** A new child page is allocated and the contents of the current root
 54910 ** page, including overflow cells, are copied into the child. The root
 54911 ** page is then overwritten to make it an empty page with the right-child 
 54912 ** pointer pointing to the new page.
 54913 **
 54914 ** Before returning, all pointer-map entries corresponding to pages 
 54915 ** that the new child-page now contains pointers to are updated. The
 54916 ** entry corresponding to the new right-child pointer of the root
 54917 ** page is also updated.
 54918 **
 54919 ** If successful, *ppChild is set to contain a reference to the child 
 54920 ** page and SQLITE_OK is returned. In this case the caller is required
 54921 ** to call releasePage() on *ppChild exactly once. If an error occurs,
 54922 ** an error code is returned and *ppChild is set to 0.
 54923 */
 54924 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
 54925   int rc;                        /* Return value from subprocedures */
 54926   MemPage *pChild = 0;           /* Pointer to a new child page */
 54927   Pgno pgnoChild = 0;            /* Page number of the new child page */
 54928   BtShared *pBt = pRoot->pBt;    /* The BTree */
 54930   assert( pRoot->nOverflow>0 );
 54931   assert( sqlite3_mutex_held(pBt->mutex) );
 54933   /* Make pRoot, the root page of the b-tree, writable. Allocate a new 
 54934   ** page that will become the new right-child of pPage. Copy the contents
 54935   ** of the node stored on pRoot into the new child page.
 54936   */
 54937   rc = sqlite3PagerWrite(pRoot->pDbPage);
 54938   if( rc==SQLITE_OK ){
 54939     rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
 54940     copyNodeContent(pRoot, pChild, &rc);
 54941     if( ISAUTOVACUUM ){
 54942       ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
 54945   if( rc ){
 54946     *ppChild = 0;
 54947     releasePage(pChild);
 54948     return rc;
 54950   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
 54951   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 54952   assert( pChild->nCell==pRoot->nCell );
 54954   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
 54956   /* Copy the overflow cells from pRoot to pChild */
 54957   memcpy(pChild->aiOvfl, pRoot->aiOvfl,
 54958          pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
 54959   memcpy(pChild->apOvfl, pRoot->apOvfl,
 54960          pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
 54961   pChild->nOverflow = pRoot->nOverflow;
 54963   /* Zero the contents of pRoot. Then install pChild as the right-child. */
 54964   zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
 54965   put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
 54967   *ppChild = pChild;
 54968   return SQLITE_OK;
 54971 /*
 54972 ** The page that pCur currently points to has just been modified in
 54973 ** some way. This function figures out if this modification means the
 54974 ** tree needs to be balanced, and if so calls the appropriate balancing 
 54975 ** routine. Balancing routines are:
 54976 **
 54977 **   balance_quick()
 54978 **   balance_deeper()
 54979 **   balance_nonroot()
 54980 */
 54981 static int balance(BtCursor *pCur){
 54982   int rc = SQLITE_OK;
 54983   const int nMin = pCur->pBt->usableSize * 2 / 3;
 54984   u8 aBalanceQuickSpace[13];
 54985   u8 *pFree = 0;
 54987   TESTONLY( int balance_quick_called = 0 );
 54988   TESTONLY( int balance_deeper_called = 0 );
 54990   do {
 54991     int iPage = pCur->iPage;
 54992     MemPage *pPage = pCur->apPage[iPage];
 54994     if( iPage==0 ){
 54995       if( pPage->nOverflow ){
 54996         /* The root page of the b-tree is overfull. In this case call the
 54997         ** balance_deeper() function to create a new child for the root-page
 54998         ** and copy the current contents of the root-page to it. The
 54999         ** next iteration of the do-loop will balance the child page.
 55000         */ 
 55001         assert( (balance_deeper_called++)==0 );
 55002         rc = balance_deeper(pPage, &pCur->apPage[1]);
 55003         if( rc==SQLITE_OK ){
 55004           pCur->iPage = 1;
 55005           pCur->aiIdx[0] = 0;
 55006           pCur->aiIdx[1] = 0;
 55007           assert( pCur->apPage[1]->nOverflow );
 55009       }else{
 55010         break;
 55012     }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
 55013       break;
 55014     }else{
 55015       MemPage * const pParent = pCur->apPage[iPage-1];
 55016       int const iIdx = pCur->aiIdx[iPage-1];
 55018       rc = sqlite3PagerWrite(pParent->pDbPage);
 55019       if( rc==SQLITE_OK ){
 55020 #ifndef SQLITE_OMIT_QUICKBALANCE
 55021         if( pPage->hasData
 55022          && pPage->nOverflow==1
 55023          && pPage->aiOvfl[0]==pPage->nCell
 55024          && pParent->pgno!=1
 55025          && pParent->nCell==iIdx
 55026         ){
 55027           /* Call balance_quick() to create a new sibling of pPage on which
 55028           ** to store the overflow cell. balance_quick() inserts a new cell
 55029           ** into pParent, which may cause pParent overflow. If this
 55030           ** happens, the next interation of the do-loop will balance pParent 
 55031           ** use either balance_nonroot() or balance_deeper(). Until this
 55032           ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
 55033           ** buffer. 
 55034           **
 55035           ** The purpose of the following assert() is to check that only a
 55036           ** single call to balance_quick() is made for each call to this
 55037           ** function. If this were not verified, a subtle bug involving reuse
 55038           ** of the aBalanceQuickSpace[] might sneak in.
 55039           */
 55040           assert( (balance_quick_called++)==0 );
 55041           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
 55042         }else
 55043 #endif
 55045           /* In this case, call balance_nonroot() to redistribute cells
 55046           ** between pPage and up to 2 of its sibling pages. This involves
 55047           ** modifying the contents of pParent, which may cause pParent to
 55048           ** become overfull or underfull. The next iteration of the do-loop
 55049           ** will balance the parent page to correct this.
 55050           ** 
 55051           ** If the parent page becomes overfull, the overflow cell or cells
 55052           ** are stored in the pSpace buffer allocated immediately below. 
 55053           ** A subsequent iteration of the do-loop will deal with this by
 55054           ** calling balance_nonroot() (balance_deeper() may be called first,
 55055           ** but it doesn't deal with overflow cells - just moves them to a
 55056           ** different page). Once this subsequent call to balance_nonroot() 
 55057           ** has completed, it is safe to release the pSpace buffer used by
 55058           ** the previous call, as the overflow cell data will have been 
 55059           ** copied either into the body of a database page or into the new
 55060           ** pSpace buffer passed to the latter call to balance_nonroot().
 55061           */
 55062           u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
 55063           rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
 55064           if( pFree ){
 55065             /* If pFree is not NULL, it points to the pSpace buffer used 
 55066             ** by a previous call to balance_nonroot(). Its contents are
 55067             ** now stored either on real database pages or within the 
 55068             ** new pSpace buffer, so it may be safely freed here. */
 55069             sqlite3PageFree(pFree);
 55072           /* The pSpace buffer will be freed after the next call to
 55073           ** balance_nonroot(), or just before this function returns, whichever
 55074           ** comes first. */
 55075           pFree = pSpace;
 55079       pPage->nOverflow = 0;
 55081       /* The next iteration of the do-loop balances the parent page. */
 55082       releasePage(pPage);
 55083       pCur->iPage--;
 55085   }while( rc==SQLITE_OK );
 55087   if( pFree ){
 55088     sqlite3PageFree(pFree);
 55090   return rc;
 55094 /*
 55095 ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
 55096 ** and the data is given by (pData,nData).  The cursor is used only to
 55097 ** define what table the record should be inserted into.  The cursor
 55098 ** is left pointing at a random location.
 55099 **
 55100 ** For an INTKEY table, only the nKey value of the key is used.  pKey is
 55101 ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
 55102 **
 55103 ** If the seekResult parameter is non-zero, then a successful call to
 55104 ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
 55105 ** been performed. seekResult is the search result returned (a negative
 55106 ** number if pCur points at an entry that is smaller than (pKey, nKey), or
 55107 ** a positive value if pCur points at an etry that is larger than 
 55108 ** (pKey, nKey)). 
 55109 **
 55110 ** If the seekResult parameter is non-zero, then the caller guarantees that
 55111 ** cursor pCur is pointing at the existing copy of a row that is to be
 55112 ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
 55113 ** point to any entry or to no entry at all and so this function has to seek
 55114 ** the cursor before the new key can be inserted.
 55115 */
 55116 SQLITE_PRIVATE int sqlite3BtreeInsert(
 55117   BtCursor *pCur,                /* Insert data into the table of this cursor */
 55118   const void *pKey, i64 nKey,    /* The key of the new record */
 55119   const void *pData, int nData,  /* The data of the new record */
 55120   int nZero,                     /* Number of extra 0 bytes to append to data */
 55121   int appendBias,                /* True if this is likely an append */
 55122   int seekResult                 /* Result of prior MovetoUnpacked() call */
 55123 ){
 55124   int rc;
 55125   int loc = seekResult;          /* -1: before desired location  +1: after */
 55126   int szNew = 0;
 55127   int idx;
 55128   MemPage *pPage;
 55129   Btree *p = pCur->pBtree;
 55130   BtShared *pBt = p->pBt;
 55131   unsigned char *oldCell;
 55132   unsigned char *newCell = 0;
 55134   if( pCur->eState==CURSOR_FAULT ){
 55135     assert( pCur->skipNext!=SQLITE_OK );
 55136     return pCur->skipNext;
 55139   assert( cursorHoldsMutex(pCur) );
 55140   assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
 55141               && (pBt->btsFlags & BTS_READ_ONLY)==0 );
 55142   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 55144   /* Assert that the caller has been consistent. If this cursor was opened
 55145   ** expecting an index b-tree, then the caller should be inserting blob
 55146   ** keys with no associated data. If the cursor was opened expecting an
 55147   ** intkey table, the caller should be inserting integer keys with a
 55148   ** blob of associated data.  */
 55149   assert( (pKey==0)==(pCur->pKeyInfo==0) );
 55151   /* Save the positions of any other cursors open on this table.
 55152   **
 55153   ** In some cases, the call to btreeMoveto() below is a no-op. For
 55154   ** example, when inserting data into a table with auto-generated integer
 55155   ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
 55156   ** integer key to use. It then calls this function to actually insert the 
 55157   ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
 55158   ** that the cursor is already where it needs to be and returns without
 55159   ** doing any work. To avoid thwarting these optimizations, it is important
 55160   ** not to clear the cursor here.
 55161   */
 55162   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 55163   if( rc ) return rc;
 55165   /* If this is an insert into a table b-tree, invalidate any incrblob 
 55166   ** cursors open on the row being replaced (assuming this is a replace
 55167   ** operation - if it is not, the following is a no-op).  */
 55168   if( pCur->pKeyInfo==0 ){
 55169     invalidateIncrblobCursors(p, nKey, 0);
 55172   if( !loc ){
 55173     rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
 55174     if( rc ) return rc;
 55176   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 55178   pPage = pCur->apPage[pCur->iPage];
 55179   assert( pPage->intKey || nKey>=0 );
 55180   assert( pPage->leaf || !pPage->intKey );
 55182   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 55183           pCur->pgnoRoot, nKey, nData, pPage->pgno,
 55184           loc==0 ? "overwrite" : "new entry"));
 55185   assert( pPage->isInit );
 55186   allocateTempSpace(pBt);
 55187   newCell = pBt->pTmpSpace;
 55188   if( newCell==0 ) return SQLITE_NOMEM;
 55189   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
 55190   if( rc ) goto end_insert;
 55191   assert( szNew==cellSizePtr(pPage, newCell) );
 55192   assert( szNew <= MX_CELL_SIZE(pBt) );
 55193   idx = pCur->aiIdx[pCur->iPage];
 55194   if( loc==0 ){
 55195     u16 szOld;
 55196     assert( idx<pPage->nCell );
 55197     rc = sqlite3PagerWrite(pPage->pDbPage);
 55198     if( rc ){
 55199       goto end_insert;
 55201     oldCell = findCell(pPage, idx);
 55202     if( !pPage->leaf ){
 55203       memcpy(newCell, oldCell, 4);
 55205     szOld = cellSizePtr(pPage, oldCell);
 55206     rc = clearCell(pPage, oldCell);
 55207     dropCell(pPage, idx, szOld, &rc);
 55208     if( rc ) goto end_insert;
 55209   }else if( loc<0 && pPage->nCell>0 ){
 55210     assert( pPage->leaf );
 55211     idx = ++pCur->aiIdx[pCur->iPage];
 55212   }else{
 55213     assert( pPage->leaf );
 55215   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
 55216   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
 55218   /* If no error has occured and pPage has an overflow cell, call balance() 
 55219   ** to redistribute the cells within the tree. Since balance() may move
 55220   ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
 55221   ** variables.
 55222   **
 55223   ** Previous versions of SQLite called moveToRoot() to move the cursor
 55224   ** back to the root page as balance() used to invalidate the contents
 55225   ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
 55226   ** set the cursor state to "invalid". This makes common insert operations
 55227   ** slightly faster.
 55228   **
 55229   ** There is a subtle but important optimization here too. When inserting
 55230   ** multiple records into an intkey b-tree using a single cursor (as can
 55231   ** happen while processing an "INSERT INTO ... SELECT" statement), it
 55232   ** is advantageous to leave the cursor pointing to the last entry in
 55233   ** the b-tree if possible. If the cursor is left pointing to the last
 55234   ** entry in the table, and the next row inserted has an integer key
 55235   ** larger than the largest existing key, it is possible to insert the
 55236   ** row without seeking the cursor. This can be a big performance boost.
 55237   */
 55238   pCur->info.nSize = 0;
 55239   pCur->validNKey = 0;
 55240   if( rc==SQLITE_OK && pPage->nOverflow ){
 55241     rc = balance(pCur);
 55243     /* Must make sure nOverflow is reset to zero even if the balance()
 55244     ** fails. Internal data structure corruption will result otherwise. 
 55245     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 55246     ** from trying to save the current position of the cursor.  */
 55247     pCur->apPage[pCur->iPage]->nOverflow = 0;
 55248     pCur->eState = CURSOR_INVALID;
 55250   assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
 55252 end_insert:
 55253   return rc;
 55256 /*
 55257 ** Delete the entry that the cursor is pointing to.  The cursor
 55258 ** is left pointing at a arbitrary location.
 55259 */
 55260 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
 55261   Btree *p = pCur->pBtree;
 55262   BtShared *pBt = p->pBt;              
 55263   int rc;                              /* Return code */
 55264   MemPage *pPage;                      /* Page to delete cell from */
 55265   unsigned char *pCell;                /* Pointer to cell to delete */
 55266   int iCellIdx;                        /* Index of cell to delete */
 55267   int iCellDepth;                      /* Depth of node containing pCell */ 
 55269   assert( cursorHoldsMutex(pCur) );
 55270   assert( pBt->inTransaction==TRANS_WRITE );
 55271   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 55272   assert( pCur->wrFlag );
 55273   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 55274   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 55276   if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
 55277    || NEVER(pCur->eState!=CURSOR_VALID)
 55278   ){
 55279     return SQLITE_ERROR;  /* Something has gone awry. */
 55282   iCellDepth = pCur->iPage;
 55283   iCellIdx = pCur->aiIdx[iCellDepth];
 55284   pPage = pCur->apPage[iCellDepth];
 55285   pCell = findCell(pPage, iCellIdx);
 55287   /* If the page containing the entry to delete is not a leaf page, move
 55288   ** the cursor to the largest entry in the tree that is smaller than
 55289   ** the entry being deleted. This cell will replace the cell being deleted
 55290   ** from the internal node. The 'previous' entry is used for this instead
 55291   ** of the 'next' entry, as the previous entry is always a part of the
 55292   ** sub-tree headed by the child page of the cell being deleted. This makes
 55293   ** balancing the tree following the delete operation easier.  */
 55294   if( !pPage->leaf ){
 55295     int notUsed;
 55296     rc = sqlite3BtreePrevious(pCur, &notUsed);
 55297     if( rc ) return rc;
 55300   /* Save the positions of any other cursors open on this table before
 55301   ** making any modifications. Make the page containing the entry to be 
 55302   ** deleted writable. Then free any overflow pages associated with the 
 55303   ** entry and finally remove the cell itself from within the page.  
 55304   */
 55305   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 55306   if( rc ) return rc;
 55308   /* If this is a delete operation to remove a row from a table b-tree,
 55309   ** invalidate any incrblob cursors open on the row being deleted.  */
 55310   if( pCur->pKeyInfo==0 ){
 55311     invalidateIncrblobCursors(p, pCur->info.nKey, 0);
 55314   rc = sqlite3PagerWrite(pPage->pDbPage);
 55315   if( rc ) return rc;
 55316   rc = clearCell(pPage, pCell);
 55317   dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
 55318   if( rc ) return rc;
 55320   /* If the cell deleted was not located on a leaf page, then the cursor
 55321   ** is currently pointing to the largest entry in the sub-tree headed
 55322   ** by the child-page of the cell that was just deleted from an internal
 55323   ** node. The cell from the leaf node needs to be moved to the internal
 55324   ** node to replace the deleted cell.  */
 55325   if( !pPage->leaf ){
 55326     MemPage *pLeaf = pCur->apPage[pCur->iPage];
 55327     int nCell;
 55328     Pgno n = pCur->apPage[iCellDepth+1]->pgno;
 55329     unsigned char *pTmp;
 55331     pCell = findCell(pLeaf, pLeaf->nCell-1);
 55332     nCell = cellSizePtr(pLeaf, pCell);
 55333     assert( MX_CELL_SIZE(pBt) >= nCell );
 55335     allocateTempSpace(pBt);
 55336     pTmp = pBt->pTmpSpace;
 55338     rc = sqlite3PagerWrite(pLeaf->pDbPage);
 55339     insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
 55340     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
 55341     if( rc ) return rc;
 55344   /* Balance the tree. If the entry deleted was located on a leaf page,
 55345   ** then the cursor still points to that page. In this case the first
 55346   ** call to balance() repairs the tree, and the if(...) condition is
 55347   ** never true.
 55348   **
 55349   ** Otherwise, if the entry deleted was on an internal node page, then
 55350   ** pCur is pointing to the leaf page from which a cell was removed to
 55351   ** replace the cell deleted from the internal node. This is slightly
 55352   ** tricky as the leaf node may be underfull, and the internal node may
 55353   ** be either under or overfull. In this case run the balancing algorithm
 55354   ** on the leaf node first. If the balance proceeds far enough up the
 55355   ** tree that we can be sure that any problem in the internal node has
 55356   ** been corrected, so be it. Otherwise, after balancing the leaf node,
 55357   ** walk the cursor up the tree to the internal node and balance it as 
 55358   ** well.  */
 55359   rc = balance(pCur);
 55360   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
 55361     while( pCur->iPage>iCellDepth ){
 55362       releasePage(pCur->apPage[pCur->iPage--]);
 55364     rc = balance(pCur);
 55367   if( rc==SQLITE_OK ){
 55368     moveToRoot(pCur);
 55370   return rc;
 55373 /*
 55374 ** Create a new BTree table.  Write into *piTable the page
 55375 ** number for the root page of the new table.
 55376 **
 55377 ** The type of type is determined by the flags parameter.  Only the
 55378 ** following values of flags are currently in use.  Other values for
 55379 ** flags might not work:
 55380 **
 55381 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
 55382 **     BTREE_ZERODATA                  Used for SQL indices
 55383 */
 55384 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
 55385   BtShared *pBt = p->pBt;
 55386   MemPage *pRoot;
 55387   Pgno pgnoRoot;
 55388   int rc;
 55389   int ptfFlags;          /* Page-type flage for the root page of new table */
 55391   assert( sqlite3BtreeHoldsMutex(p) );
 55392   assert( pBt->inTransaction==TRANS_WRITE );
 55393   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 55395 #ifdef SQLITE_OMIT_AUTOVACUUM
 55396   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
 55397   if( rc ){
 55398     return rc;
 55400 #else
 55401   if( pBt->autoVacuum ){
 55402     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
 55403     MemPage *pPageMove; /* The page to move to. */
 55405     /* Creating a new table may probably require moving an existing database
 55406     ** to make room for the new tables root page. In case this page turns
 55407     ** out to be an overflow page, delete all overflow page-map caches
 55408     ** held by open cursors.
 55409     */
 55410     invalidateAllOverflowCache(pBt);
 55412     /* Read the value of meta[3] from the database to determine where the
 55413     ** root page of the new table should go. meta[3] is the largest root-page
 55414     ** created so far, so the new root-page is (meta[3]+1).
 55415     */
 55416     sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
 55417     pgnoRoot++;
 55419     /* The new root-page may not be allocated on a pointer-map page, or the
 55420     ** PENDING_BYTE page.
 55421     */
 55422     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
 55423         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
 55424       pgnoRoot++;
 55426     assert( pgnoRoot>=3 );
 55428     /* Allocate a page. The page that currently resides at pgnoRoot will
 55429     ** be moved to the allocated page (unless the allocated page happens
 55430     ** to reside at pgnoRoot).
 55431     */
 55432     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
 55433     if( rc!=SQLITE_OK ){
 55434       return rc;
 55437     if( pgnoMove!=pgnoRoot ){
 55438       /* pgnoRoot is the page that will be used for the root-page of
 55439       ** the new table (assuming an error did not occur). But we were
 55440       ** allocated pgnoMove. If required (i.e. if it was not allocated
 55441       ** by extending the file), the current page at position pgnoMove
 55442       ** is already journaled.
 55443       */
 55444       u8 eType = 0;
 55445       Pgno iPtrPage = 0;
 55447       releasePage(pPageMove);
 55449       /* Move the page currently at pgnoRoot to pgnoMove. */
 55450       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
 55451       if( rc!=SQLITE_OK ){
 55452         return rc;
 55454       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
 55455       if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
 55456         rc = SQLITE_CORRUPT_BKPT;
 55458       if( rc!=SQLITE_OK ){
 55459         releasePage(pRoot);
 55460         return rc;
 55462       assert( eType!=PTRMAP_ROOTPAGE );
 55463       assert( eType!=PTRMAP_FREEPAGE );
 55464       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
 55465       releasePage(pRoot);
 55467       /* Obtain the page at pgnoRoot */
 55468       if( rc!=SQLITE_OK ){
 55469         return rc;
 55471       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
 55472       if( rc!=SQLITE_OK ){
 55473         return rc;
 55475       rc = sqlite3PagerWrite(pRoot->pDbPage);
 55476       if( rc!=SQLITE_OK ){
 55477         releasePage(pRoot);
 55478         return rc;
 55480     }else{
 55481       pRoot = pPageMove;
 55484     /* Update the pointer-map and meta-data with the new root-page number. */
 55485     ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
 55486     if( rc ){
 55487       releasePage(pRoot);
 55488       return rc;
 55491     /* When the new root page was allocated, page 1 was made writable in
 55492     ** order either to increase the database filesize, or to decrement the
 55493     ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
 55494     */
 55495     assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
 55496     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
 55497     if( NEVER(rc) ){
 55498       releasePage(pRoot);
 55499       return rc;
 55502   }else{
 55503     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
 55504     if( rc ) return rc;
 55506 #endif
 55507   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 55508   if( createTabFlags & BTREE_INTKEY ){
 55509     ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
 55510   }else{
 55511     ptfFlags = PTF_ZERODATA | PTF_LEAF;
 55513   zeroPage(pRoot, ptfFlags);
 55514   sqlite3PagerUnref(pRoot->pDbPage);
 55515   assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
 55516   *piTable = (int)pgnoRoot;
 55517   return SQLITE_OK;
 55519 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
 55520   int rc;
 55521   sqlite3BtreeEnter(p);
 55522   rc = btreeCreateTable(p, piTable, flags);
 55523   sqlite3BtreeLeave(p);
 55524   return rc;
 55527 /*
 55528 ** Erase the given database page and all its children.  Return
 55529 ** the page to the freelist.
 55530 */
 55531 static int clearDatabasePage(
 55532   BtShared *pBt,           /* The BTree that contains the table */
 55533   Pgno pgno,               /* Page number to clear */
 55534   int freePageFlag,        /* Deallocate page if true */
 55535   int *pnChange            /* Add number of Cells freed to this counter */
 55536 ){
 55537   MemPage *pPage;
 55538   int rc;
 55539   unsigned char *pCell;
 55540   int i;
 55542   assert( sqlite3_mutex_held(pBt->mutex) );
 55543   if( pgno>btreePagecount(pBt) ){
 55544     return SQLITE_CORRUPT_BKPT;
 55547   rc = getAndInitPage(pBt, pgno, &pPage);
 55548   if( rc ) return rc;
 55549   for(i=0; i<pPage->nCell; i++){
 55550     pCell = findCell(pPage, i);
 55551     if( !pPage->leaf ){
 55552       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
 55553       if( rc ) goto cleardatabasepage_out;
 55555     rc = clearCell(pPage, pCell);
 55556     if( rc ) goto cleardatabasepage_out;
 55558   if( !pPage->leaf ){
 55559     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
 55560     if( rc ) goto cleardatabasepage_out;
 55561   }else if( pnChange ){
 55562     assert( pPage->intKey );
 55563     *pnChange += pPage->nCell;
 55565   if( freePageFlag ){
 55566     freePage(pPage, &rc);
 55567   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
 55568     zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
 55571 cleardatabasepage_out:
 55572   releasePage(pPage);
 55573   return rc;
 55576 /*
 55577 ** Delete all information from a single table in the database.  iTable is
 55578 ** the page number of the root of the table.  After this routine returns,
 55579 ** the root page is empty, but still exists.
 55580 **
 55581 ** This routine will fail with SQLITE_LOCKED if there are any open
 55582 ** read cursors on the table.  Open write cursors are moved to the
 55583 ** root of the table.
 55584 **
 55585 ** If pnChange is not NULL, then table iTable must be an intkey table. The
 55586 ** integer value pointed to by pnChange is incremented by the number of
 55587 ** entries in the table.
 55588 */
 55589 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
 55590   int rc;
 55591   BtShared *pBt = p->pBt;
 55592   sqlite3BtreeEnter(p);
 55593   assert( p->inTrans==TRANS_WRITE );
 55595   rc = saveAllCursors(pBt, (Pgno)iTable, 0);
 55597   if( SQLITE_OK==rc ){
 55598     /* Invalidate all incrblob cursors open on table iTable (assuming iTable
 55599     ** is the root of a table b-tree - if it is not, the following call is
 55600     ** a no-op).  */
 55601     invalidateIncrblobCursors(p, 0, 1);
 55602     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
 55604   sqlite3BtreeLeave(p);
 55605   return rc;
 55608 /*
 55609 ** Erase all information in a table and add the root of the table to
 55610 ** the freelist.  Except, the root of the principle table (the one on
 55611 ** page 1) is never added to the freelist.
 55612 **
 55613 ** This routine will fail with SQLITE_LOCKED if there are any open
 55614 ** cursors on the table.
 55615 **
 55616 ** If AUTOVACUUM is enabled and the page at iTable is not the last
 55617 ** root page in the database file, then the last root page 
 55618 ** in the database file is moved into the slot formerly occupied by
 55619 ** iTable and that last slot formerly occupied by the last root page
 55620 ** is added to the freelist instead of iTable.  In this say, all
 55621 ** root pages are kept at the beginning of the database file, which
 55622 ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
 55623 ** page number that used to be the last root page in the file before
 55624 ** the move.  If no page gets moved, *piMoved is set to 0.
 55625 ** The last root page is recorded in meta[3] and the value of
 55626 ** meta[3] is updated by this procedure.
 55627 */
 55628 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
 55629   int rc;
 55630   MemPage *pPage = 0;
 55631   BtShared *pBt = p->pBt;
 55633   assert( sqlite3BtreeHoldsMutex(p) );
 55634   assert( p->inTrans==TRANS_WRITE );
 55636   /* It is illegal to drop a table if any cursors are open on the
 55637   ** database. This is because in auto-vacuum mode the backend may
 55638   ** need to move another root-page to fill a gap left by the deleted
 55639   ** root page. If an open cursor was using this page a problem would 
 55640   ** occur.
 55641   **
 55642   ** This error is caught long before control reaches this point.
 55643   */
 55644   if( NEVER(pBt->pCursor) ){
 55645     sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
 55646     return SQLITE_LOCKED_SHAREDCACHE;
 55649   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
 55650   if( rc ) return rc;
 55651   rc = sqlite3BtreeClearTable(p, iTable, 0);
 55652   if( rc ){
 55653     releasePage(pPage);
 55654     return rc;
 55657   *piMoved = 0;
 55659   if( iTable>1 ){
 55660 #ifdef SQLITE_OMIT_AUTOVACUUM
 55661     freePage(pPage, &rc);
 55662     releasePage(pPage);
 55663 #else
 55664     if( pBt->autoVacuum ){
 55665       Pgno maxRootPgno;
 55666       sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
 55668       if( iTable==maxRootPgno ){
 55669         /* If the table being dropped is the table with the largest root-page
 55670         ** number in the database, put the root page on the free list. 
 55671         */
 55672         freePage(pPage, &rc);
 55673         releasePage(pPage);
 55674         if( rc!=SQLITE_OK ){
 55675           return rc;
 55677       }else{
 55678         /* The table being dropped does not have the largest root-page
 55679         ** number in the database. So move the page that does into the 
 55680         ** gap left by the deleted root-page.
 55681         */
 55682         MemPage *pMove;
 55683         releasePage(pPage);
 55684         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 55685         if( rc!=SQLITE_OK ){
 55686           return rc;
 55688         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
 55689         releasePage(pMove);
 55690         if( rc!=SQLITE_OK ){
 55691           return rc;
 55693         pMove = 0;
 55694         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 55695         freePage(pMove, &rc);
 55696         releasePage(pMove);
 55697         if( rc!=SQLITE_OK ){
 55698           return rc;
 55700         *piMoved = maxRootPgno;
 55703       /* Set the new 'max-root-page' value in the database header. This
 55704       ** is the old value less one, less one more if that happens to
 55705       ** be a root-page number, less one again if that is the
 55706       ** PENDING_BYTE_PAGE.
 55707       */
 55708       maxRootPgno--;
 55709       while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
 55710              || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
 55711         maxRootPgno--;
 55713       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
 55715       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
 55716     }else{
 55717       freePage(pPage, &rc);
 55718       releasePage(pPage);
 55720 #endif
 55721   }else{
 55722     /* If sqlite3BtreeDropTable was called on page 1.
 55723     ** This really never should happen except in a corrupt
 55724     ** database. 
 55725     */
 55726     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
 55727     releasePage(pPage);
 55729   return rc;  
 55731 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
 55732   int rc;
 55733   sqlite3BtreeEnter(p);
 55734   rc = btreeDropTable(p, iTable, piMoved);
 55735   sqlite3BtreeLeave(p);
 55736   return rc;
 55740 /*
 55741 ** This function may only be called if the b-tree connection already
 55742 ** has a read or write transaction open on the database.
 55743 **
 55744 ** Read the meta-information out of a database file.  Meta[0]
 55745 ** is the number of free pages currently in the database.  Meta[1]
 55746 ** through meta[15] are available for use by higher layers.  Meta[0]
 55747 ** is read-only, the others are read/write.
 55748 ** 
 55749 ** The schema layer numbers meta values differently.  At the schema
 55750 ** layer (and the SetCookie and ReadCookie opcodes) the number of
 55751 ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
 55752 */
 55753 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
 55754   BtShared *pBt = p->pBt;
 55756   sqlite3BtreeEnter(p);
 55757   assert( p->inTrans>TRANS_NONE );
 55758   assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
 55759   assert( pBt->pPage1 );
 55760   assert( idx>=0 && idx<=15 );
 55762   *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
 55764   /* If auto-vacuum is disabled in this build and this is an auto-vacuum
 55765   ** database, mark the database as read-only.  */
 55766 #ifdef SQLITE_OMIT_AUTOVACUUM
 55767   if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
 55768     pBt->btsFlags |= BTS_READ_ONLY;
 55770 #endif
 55772   sqlite3BtreeLeave(p);
 55775 /*
 55776 ** Write meta-information back into the database.  Meta[0] is
 55777 ** read-only and may not be written.
 55778 */
 55779 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
 55780   BtShared *pBt = p->pBt;
 55781   unsigned char *pP1;
 55782   int rc;
 55783   assert( idx>=1 && idx<=15 );
 55784   sqlite3BtreeEnter(p);
 55785   assert( p->inTrans==TRANS_WRITE );
 55786   assert( pBt->pPage1!=0 );
 55787   pP1 = pBt->pPage1->aData;
 55788   rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 55789   if( rc==SQLITE_OK ){
 55790     put4byte(&pP1[36 + idx*4], iMeta);
 55791 #ifndef SQLITE_OMIT_AUTOVACUUM
 55792     if( idx==BTREE_INCR_VACUUM ){
 55793       assert( pBt->autoVacuum || iMeta==0 );
 55794       assert( iMeta==0 || iMeta==1 );
 55795       pBt->incrVacuum = (u8)iMeta;
 55797 #endif
 55799   sqlite3BtreeLeave(p);
 55800   return rc;
 55803 #ifndef SQLITE_OMIT_BTREECOUNT
 55804 /*
 55805 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
 55806 ** number of entries in the b-tree and write the result to *pnEntry.
 55807 **
 55808 ** SQLITE_OK is returned if the operation is successfully executed. 
 55809 ** Otherwise, if an error is encountered (i.e. an IO error or database
 55810 ** corruption) an SQLite error code is returned.
 55811 */
 55812 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
 55813   i64 nEntry = 0;                      /* Value to return in *pnEntry */
 55814   int rc;                              /* Return code */
 55816   if( pCur->pgnoRoot==0 ){
 55817     *pnEntry = 0;
 55818     return SQLITE_OK;
 55820   rc = moveToRoot(pCur);
 55822   /* Unless an error occurs, the following loop runs one iteration for each
 55823   ** page in the B-Tree structure (not including overflow pages). 
 55824   */
 55825   while( rc==SQLITE_OK ){
 55826     int iIdx;                          /* Index of child node in parent */
 55827     MemPage *pPage;                    /* Current page of the b-tree */
 55829     /* If this is a leaf page or the tree is not an int-key tree, then 
 55830     ** this page contains countable entries. Increment the entry counter
 55831     ** accordingly.
 55832     */
 55833     pPage = pCur->apPage[pCur->iPage];
 55834     if( pPage->leaf || !pPage->intKey ){
 55835       nEntry += pPage->nCell;
 55838     /* pPage is a leaf node. This loop navigates the cursor so that it 
 55839     ** points to the first interior cell that it points to the parent of
 55840     ** the next page in the tree that has not yet been visited. The
 55841     ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
 55842     ** of the page, or to the number of cells in the page if the next page
 55843     ** to visit is the right-child of its parent.
 55844     **
 55845     ** If all pages in the tree have been visited, return SQLITE_OK to the
 55846     ** caller.
 55847     */
 55848     if( pPage->leaf ){
 55849       do {
 55850         if( pCur->iPage==0 ){
 55851           /* All pages of the b-tree have been visited. Return successfully. */
 55852           *pnEntry = nEntry;
 55853           return SQLITE_OK;
 55855         moveToParent(pCur);
 55856       }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
 55858       pCur->aiIdx[pCur->iPage]++;
 55859       pPage = pCur->apPage[pCur->iPage];
 55862     /* Descend to the child node of the cell that the cursor currently 
 55863     ** points at. This is the right-child if (iIdx==pPage->nCell).
 55864     */
 55865     iIdx = pCur->aiIdx[pCur->iPage];
 55866     if( iIdx==pPage->nCell ){
 55867       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
 55868     }else{
 55869       rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
 55873   /* An error has occurred. Return an error code. */
 55874   return rc;
 55876 #endif
 55878 /*
 55879 ** Return the pager associated with a BTree.  This routine is used for
 55880 ** testing and debugging only.
 55881 */
 55882 SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
 55883   return p->pBt->pPager;
 55886 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 55887 /*
 55888 ** Append a message to the error message string.
 55889 */
 55890 static void checkAppendMsg(
 55891   IntegrityCk *pCheck,
 55892   char *zMsg1,
 55893   const char *zFormat,
 55894   ...
 55895 ){
 55896   va_list ap;
 55897   if( !pCheck->mxErr ) return;
 55898   pCheck->mxErr--;
 55899   pCheck->nErr++;
 55900   va_start(ap, zFormat);
 55901   if( pCheck->errMsg.nChar ){
 55902     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
 55904   if( zMsg1 ){
 55905     sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
 55907   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
 55908   va_end(ap);
 55909   if( pCheck->errMsg.mallocFailed ){
 55910     pCheck->mallocFailed = 1;
 55913 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 55915 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 55917 /*
 55918 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
 55919 ** corresponds to page iPg is already set.
 55920 */
 55921 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
 55922   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
 55923   return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
 55926 /*
 55927 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
 55928 */
 55929 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
 55930   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
 55931   pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
 55935 /*
 55936 ** Add 1 to the reference count for page iPage.  If this is the second
 55937 ** reference to the page, add an error message to pCheck->zErrMsg.
 55938 ** Return 1 if there are 2 ore more references to the page and 0 if
 55939 ** if this is the first reference to the page.
 55940 **
 55941 ** Also check that the page number is in bounds.
 55942 */
 55943 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
 55944   if( iPage==0 ) return 1;
 55945   if( iPage>pCheck->nPage ){
 55946     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
 55947     return 1;
 55949   if( getPageReferenced(pCheck, iPage) ){
 55950     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
 55951     return 1;
 55953   setPageReferenced(pCheck, iPage);
 55954   return 0;
 55957 #ifndef SQLITE_OMIT_AUTOVACUUM
 55958 /*
 55959 ** Check that the entry in the pointer-map for page iChild maps to 
 55960 ** page iParent, pointer type ptrType. If not, append an error message
 55961 ** to pCheck.
 55962 */
 55963 static void checkPtrmap(
 55964   IntegrityCk *pCheck,   /* Integrity check context */
 55965   Pgno iChild,           /* Child page number */
 55966   u8 eType,              /* Expected pointer map type */
 55967   Pgno iParent,          /* Expected pointer map parent page number */
 55968   char *zContext         /* Context description (used for error msg) */
 55969 ){
 55970   int rc;
 55971   u8 ePtrmapType;
 55972   Pgno iPtrmapParent;
 55974   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
 55975   if( rc!=SQLITE_OK ){
 55976     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
 55977     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
 55978     return;
 55981   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
 55982     checkAppendMsg(pCheck, zContext, 
 55983       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
 55984       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
 55987 #endif
 55989 /*
 55990 ** Check the integrity of the freelist or of an overflow page list.
 55991 ** Verify that the number of pages on the list is N.
 55992 */
 55993 static void checkList(
 55994   IntegrityCk *pCheck,  /* Integrity checking context */
 55995   int isFreeList,       /* True for a freelist.  False for overflow page list */
 55996   int iPage,            /* Page number for first page in the list */
 55997   int N,                /* Expected number of pages in the list */
 55998   char *zContext        /* Context for error messages */
 55999 ){
 56000   int i;
 56001   int expected = N;
 56002   int iFirst = iPage;
 56003   while( N-- > 0 && pCheck->mxErr ){
 56004     DbPage *pOvflPage;
 56005     unsigned char *pOvflData;
 56006     if( iPage<1 ){
 56007       checkAppendMsg(pCheck, zContext,
 56008          "%d of %d pages missing from overflow list starting at %d",
 56009           N+1, expected, iFirst);
 56010       break;
 56012     if( checkRef(pCheck, iPage, zContext) ) break;
 56013     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
 56014       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
 56015       break;
 56017     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
 56018     if( isFreeList ){
 56019       int n = get4byte(&pOvflData[4]);
 56020 #ifndef SQLITE_OMIT_AUTOVACUUM
 56021       if( pCheck->pBt->autoVacuum ){
 56022         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
 56024 #endif
 56025       if( n>(int)pCheck->pBt->usableSize/4-2 ){
 56026         checkAppendMsg(pCheck, zContext,
 56027            "freelist leaf count too big on page %d", iPage);
 56028         N--;
 56029       }else{
 56030         for(i=0; i<n; i++){
 56031           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
 56032 #ifndef SQLITE_OMIT_AUTOVACUUM
 56033           if( pCheck->pBt->autoVacuum ){
 56034             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
 56036 #endif
 56037           checkRef(pCheck, iFreePage, zContext);
 56039         N -= n;
 56042 #ifndef SQLITE_OMIT_AUTOVACUUM
 56043     else{
 56044       /* If this database supports auto-vacuum and iPage is not the last
 56045       ** page in this overflow list, check that the pointer-map entry for
 56046       ** the following page matches iPage.
 56047       */
 56048       if( pCheck->pBt->autoVacuum && N>0 ){
 56049         i = get4byte(pOvflData);
 56050         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
 56053 #endif
 56054     iPage = get4byte(pOvflData);
 56055     sqlite3PagerUnref(pOvflPage);
 56058 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 56060 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 56061 /*
 56062 ** Do various sanity checks on a single page of a tree.  Return
 56063 ** the tree depth.  Root pages return 0.  Parents of root pages
 56064 ** return 1, and so forth.
 56065 ** 
 56066 ** These checks are done:
 56067 **
 56068 **      1.  Make sure that cells and freeblocks do not overlap
 56069 **          but combine to completely cover the page.
 56070 **  NO  2.  Make sure cell keys are in order.
 56071 **  NO  3.  Make sure no key is less than or equal to zLowerBound.
 56072 **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
 56073 **      5.  Check the integrity of overflow pages.
 56074 **      6.  Recursively call checkTreePage on all children.
 56075 **      7.  Verify that the depth of all children is the same.
 56076 **      8.  Make sure this page is at least 33% full or else it is
 56077 **          the root of the tree.
 56078 */
 56079 static int checkTreePage(
 56080   IntegrityCk *pCheck,  /* Context for the sanity check */
 56081   int iPage,            /* Page number of the page to check */
 56082   char *zParentContext, /* Parent context */
 56083   i64 *pnParentMinKey, 
 56084   i64 *pnParentMaxKey
 56085 ){
 56086   MemPage *pPage;
 56087   int i, rc, depth, d2, pgno, cnt;
 56088   int hdr, cellStart;
 56089   int nCell;
 56090   u8 *data;
 56091   BtShared *pBt;
 56092   int usableSize;
 56093   char zContext[100];
 56094   char *hit = 0;
 56095   i64 nMinKey = 0;
 56096   i64 nMaxKey = 0;
 56098   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
 56100   /* Check that the page exists
 56101   */
 56102   pBt = pCheck->pBt;
 56103   usableSize = pBt->usableSize;
 56104   if( iPage==0 ) return 0;
 56105   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
 56106   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
 56107     checkAppendMsg(pCheck, zContext,
 56108        "unable to get the page. error code=%d", rc);
 56109     return 0;
 56112   /* Clear MemPage.isInit to make sure the corruption detection code in
 56113   ** btreeInitPage() is executed.  */
 56114   pPage->isInit = 0;
 56115   if( (rc = btreeInitPage(pPage))!=0 ){
 56116     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
 56117     checkAppendMsg(pCheck, zContext, 
 56118                    "btreeInitPage() returns error code %d", rc);
 56119     releasePage(pPage);
 56120     return 0;
 56123   /* Check out all the cells.
 56124   */
 56125   depth = 0;
 56126   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
 56127     u8 *pCell;
 56128     u32 sz;
 56129     CellInfo info;
 56131     /* Check payload overflow pages
 56132     */
 56133     sqlite3_snprintf(sizeof(zContext), zContext,
 56134              "On tree page %d cell %d: ", iPage, i);
 56135     pCell = findCell(pPage,i);
 56136     btreeParseCellPtr(pPage, pCell, &info);
 56137     sz = info.nData;
 56138     if( !pPage->intKey ) sz += (int)info.nKey;
 56139     /* For intKey pages, check that the keys are in order.
 56140     */
 56141     else if( i==0 ) nMinKey = nMaxKey = info.nKey;
 56142     else{
 56143       if( info.nKey <= nMaxKey ){
 56144         checkAppendMsg(pCheck, zContext, 
 56145             "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
 56147       nMaxKey = info.nKey;
 56149     assert( sz==info.nPayload );
 56150     if( (sz>info.nLocal) 
 56151      && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
 56152     ){
 56153       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
 56154       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
 56155 #ifndef SQLITE_OMIT_AUTOVACUUM
 56156       if( pBt->autoVacuum ){
 56157         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
 56159 #endif
 56160       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
 56163     /* Check sanity of left child page.
 56164     */
 56165     if( !pPage->leaf ){
 56166       pgno = get4byte(pCell);
 56167 #ifndef SQLITE_OMIT_AUTOVACUUM
 56168       if( pBt->autoVacuum ){
 56169         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
 56171 #endif
 56172       d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
 56173       if( i>0 && d2!=depth ){
 56174         checkAppendMsg(pCheck, zContext, "Child page depth differs");
 56176       depth = d2;
 56180   if( !pPage->leaf ){
 56181     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 56182     sqlite3_snprintf(sizeof(zContext), zContext, 
 56183                      "On page %d at right child: ", iPage);
 56184 #ifndef SQLITE_OMIT_AUTOVACUUM
 56185     if( pBt->autoVacuum ){
 56186       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
 56188 #endif
 56189     checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
 56192   /* For intKey leaf pages, check that the min/max keys are in order
 56193   ** with any left/parent/right pages.
 56194   */
 56195   if( pPage->leaf && pPage->intKey ){
 56196     /* if we are a left child page */
 56197     if( pnParentMinKey ){
 56198       /* if we are the left most child page */
 56199       if( !pnParentMaxKey ){
 56200         if( nMaxKey > *pnParentMinKey ){
 56201           checkAppendMsg(pCheck, zContext, 
 56202               "Rowid %lld out of order (max larger than parent min of %lld)",
 56203               nMaxKey, *pnParentMinKey);
 56205       }else{
 56206         if( nMinKey <= *pnParentMinKey ){
 56207           checkAppendMsg(pCheck, zContext, 
 56208               "Rowid %lld out of order (min less than parent min of %lld)",
 56209               nMinKey, *pnParentMinKey);
 56211         if( nMaxKey > *pnParentMaxKey ){
 56212           checkAppendMsg(pCheck, zContext, 
 56213               "Rowid %lld out of order (max larger than parent max of %lld)",
 56214               nMaxKey, *pnParentMaxKey);
 56216         *pnParentMinKey = nMaxKey;
 56218     /* else if we're a right child page */
 56219     } else if( pnParentMaxKey ){
 56220       if( nMinKey <= *pnParentMaxKey ){
 56221         checkAppendMsg(pCheck, zContext, 
 56222             "Rowid %lld out of order (min less than parent max of %lld)",
 56223             nMinKey, *pnParentMaxKey);
 56228   /* Check for complete coverage of the page
 56229   */
 56230   data = pPage->aData;
 56231   hdr = pPage->hdrOffset;
 56232   hit = sqlite3PageMalloc( pBt->pageSize );
 56233   if( hit==0 ){
 56234     pCheck->mallocFailed = 1;
 56235   }else{
 56236     int contentOffset = get2byteNotZero(&data[hdr+5]);
 56237     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
 56238     memset(hit+contentOffset, 0, usableSize-contentOffset);
 56239     memset(hit, 1, contentOffset);
 56240     nCell = get2byte(&data[hdr+3]);
 56241     cellStart = hdr + 12 - 4*pPage->leaf;
 56242     for(i=0; i<nCell; i++){
 56243       int pc = get2byte(&data[cellStart+i*2]);
 56244       u32 size = 65536;
 56245       int j;
 56246       if( pc<=usableSize-4 ){
 56247         size = cellSizePtr(pPage, &data[pc]);
 56249       if( (int)(pc+size-1)>=usableSize ){
 56250         checkAppendMsg(pCheck, 0, 
 56251             "Corruption detected in cell %d on page %d",i,iPage);
 56252       }else{
 56253         for(j=pc+size-1; j>=pc; j--) hit[j]++;
 56256     i = get2byte(&data[hdr+1]);
 56257     while( i>0 ){
 56258       int size, j;
 56259       assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
 56260       size = get2byte(&data[i+2]);
 56261       assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
 56262       for(j=i+size-1; j>=i; j--) hit[j]++;
 56263       j = get2byte(&data[i]);
 56264       assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
 56265       assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
 56266       i = j;
 56268     for(i=cnt=0; i<usableSize; i++){
 56269       if( hit[i]==0 ){
 56270         cnt++;
 56271       }else if( hit[i]>1 ){
 56272         checkAppendMsg(pCheck, 0,
 56273           "Multiple uses for byte %d of page %d", i, iPage);
 56274         break;
 56277     if( cnt!=data[hdr+7] ){
 56278       checkAppendMsg(pCheck, 0, 
 56279           "Fragmentation of %d bytes reported as %d on page %d",
 56280           cnt, data[hdr+7], iPage);
 56283   sqlite3PageFree(hit);
 56284   releasePage(pPage);
 56285   return depth+1;
 56287 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 56289 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 56290 /*
 56291 ** This routine does a complete check of the given BTree file.  aRoot[] is
 56292 ** an array of pages numbers were each page number is the root page of
 56293 ** a table.  nRoot is the number of entries in aRoot.
 56294 **
 56295 ** A read-only or read-write transaction must be opened before calling
 56296 ** this function.
 56297 **
 56298 ** Write the number of error seen in *pnErr.  Except for some memory
 56299 ** allocation errors,  an error message held in memory obtained from
 56300 ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
 56301 ** returned.  If a memory allocation error occurs, NULL is returned.
 56302 */
 56303 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
 56304   Btree *p,     /* The btree to be checked */
 56305   int *aRoot,   /* An array of root pages numbers for individual trees */
 56306   int nRoot,    /* Number of entries in aRoot[] */
 56307   int mxErr,    /* Stop reporting errors after this many */
 56308   int *pnErr    /* Write number of errors seen to this variable */
 56309 ){
 56310   Pgno i;
 56311   int nRef;
 56312   IntegrityCk sCheck;
 56313   BtShared *pBt = p->pBt;
 56314   char zErr[100];
 56316   sqlite3BtreeEnter(p);
 56317   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
 56318   nRef = sqlite3PagerRefcount(pBt->pPager);
 56319   sCheck.pBt = pBt;
 56320   sCheck.pPager = pBt->pPager;
 56321   sCheck.nPage = btreePagecount(sCheck.pBt);
 56322   sCheck.mxErr = mxErr;
 56323   sCheck.nErr = 0;
 56324   sCheck.mallocFailed = 0;
 56325   *pnErr = 0;
 56326   if( sCheck.nPage==0 ){
 56327     sqlite3BtreeLeave(p);
 56328     return 0;
 56331   sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
 56332   if( !sCheck.aPgRef ){
 56333     *pnErr = 1;
 56334     sqlite3BtreeLeave(p);
 56335     return 0;
 56337   i = PENDING_BYTE_PAGE(pBt);
 56338   if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 56339   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
 56340   sCheck.errMsg.useMalloc = 2;
 56342   /* Check the integrity of the freelist
 56343   */
 56344   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
 56345             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
 56347   /* Check all the tables.
 56348   */
 56349   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
 56350     if( aRoot[i]==0 ) continue;
 56351 #ifndef SQLITE_OMIT_AUTOVACUUM
 56352     if( pBt->autoVacuum && aRoot[i]>1 ){
 56353       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
 56355 #endif
 56356     checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
 56359   /* Make sure every page in the file is referenced
 56360   */
 56361   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
 56362 #ifdef SQLITE_OMIT_AUTOVACUUM
 56363     if( getPageReferenced(&sCheck, i)==0 ){
 56364       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 56366 #else
 56367     /* If the database supports auto-vacuum, make sure no tables contain
 56368     ** references to pointer-map pages.
 56369     */
 56370     if( getPageReferenced(&sCheck, i)==0 && 
 56371        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
 56372       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 56374     if( getPageReferenced(&sCheck, i)!=0 && 
 56375        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
 56376       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
 56378 #endif
 56381   /* Make sure this analysis did not leave any unref() pages.
 56382   ** This is an internal consistency check; an integrity check
 56383   ** of the integrity check.
 56384   */
 56385   if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
 56386     checkAppendMsg(&sCheck, 0, 
 56387       "Outstanding page count goes from %d to %d during this analysis",
 56388       nRef, sqlite3PagerRefcount(pBt->pPager)
 56389     );
 56392   /* Clean  up and report errors.
 56393   */
 56394   sqlite3BtreeLeave(p);
 56395   sqlite3_free(sCheck.aPgRef);
 56396   if( sCheck.mallocFailed ){
 56397     sqlite3StrAccumReset(&sCheck.errMsg);
 56398     *pnErr = sCheck.nErr+1;
 56399     return 0;
 56401   *pnErr = sCheck.nErr;
 56402   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
 56403   return sqlite3StrAccumFinish(&sCheck.errMsg);
 56405 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 56407 /*
 56408 ** Return the full pathname of the underlying database file.  Return
 56409 ** an empty string if the database is in-memory or a TEMP database.
 56410 **
 56411 ** The pager filename is invariant as long as the pager is
 56412 ** open so it is safe to access without the BtShared mutex.
 56413 */
 56414 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
 56415   assert( p->pBt->pPager!=0 );
 56416   return sqlite3PagerFilename(p->pBt->pPager, 1);
 56419 /*
 56420 ** Return the pathname of the journal file for this database. The return
 56421 ** value of this routine is the same regardless of whether the journal file
 56422 ** has been created or not.
 56423 **
 56424 ** The pager journal filename is invariant as long as the pager is
 56425 ** open so it is safe to access without the BtShared mutex.
 56426 */
 56427 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
 56428   assert( p->pBt->pPager!=0 );
 56429   return sqlite3PagerJournalname(p->pBt->pPager);
 56432 /*
 56433 ** Return non-zero if a transaction is active.
 56434 */
 56435 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
 56436   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
 56437   return (p && (p->inTrans==TRANS_WRITE));
 56440 #ifndef SQLITE_OMIT_WAL
 56441 /*
 56442 ** Run a checkpoint on the Btree passed as the first argument.
 56443 **
 56444 ** Return SQLITE_LOCKED if this or any other connection has an open 
 56445 ** transaction on the shared-cache the argument Btree is connected to.
 56446 **
 56447 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 56448 */
 56449 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
 56450   int rc = SQLITE_OK;
 56451   if( p ){
 56452     BtShared *pBt = p->pBt;
 56453     sqlite3BtreeEnter(p);
 56454     if( pBt->inTransaction!=TRANS_NONE ){
 56455       rc = SQLITE_LOCKED;
 56456     }else{
 56457       rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
 56459     sqlite3BtreeLeave(p);
 56461   return rc;
 56463 #endif
 56465 /*
 56466 ** Return non-zero if a read (or write) transaction is active.
 56467 */
 56468 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
 56469   assert( p );
 56470   assert( sqlite3_mutex_held(p->db->mutex) );
 56471   return p->inTrans!=TRANS_NONE;
 56474 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
 56475   assert( p );
 56476   assert( sqlite3_mutex_held(p->db->mutex) );
 56477   return p->nBackup!=0;
 56480 /*
 56481 ** This function returns a pointer to a blob of memory associated with
 56482 ** a single shared-btree. The memory is used by client code for its own
 56483 ** purposes (for example, to store a high-level schema associated with 
 56484 ** the shared-btree). The btree layer manages reference counting issues.
 56485 **
 56486 ** The first time this is called on a shared-btree, nBytes bytes of memory
 56487 ** are allocated, zeroed, and returned to the caller. For each subsequent 
 56488 ** call the nBytes parameter is ignored and a pointer to the same blob
 56489 ** of memory returned. 
 56490 **
 56491 ** If the nBytes parameter is 0 and the blob of memory has not yet been
 56492 ** allocated, a null pointer is returned. If the blob has already been
 56493 ** allocated, it is returned as normal.
 56494 **
 56495 ** Just before the shared-btree is closed, the function passed as the 
 56496 ** xFree argument when the memory allocation was made is invoked on the 
 56497 ** blob of allocated memory. The xFree function should not call sqlite3_free()
 56498 ** on the memory, the btree layer does that.
 56499 */
 56500 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
 56501   BtShared *pBt = p->pBt;
 56502   sqlite3BtreeEnter(p);
 56503   if( !pBt->pSchema && nBytes ){
 56504     pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
 56505     pBt->xFreeSchema = xFree;
 56507   sqlite3BtreeLeave(p);
 56508   return pBt->pSchema;
 56511 /*
 56512 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 
 56513 ** btree as the argument handle holds an exclusive lock on the 
 56514 ** sqlite_master table. Otherwise SQLITE_OK.
 56515 */
 56516 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
 56517   int rc;
 56518   assert( sqlite3_mutex_held(p->db->mutex) );
 56519   sqlite3BtreeEnter(p);
 56520   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
 56521   assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
 56522   sqlite3BtreeLeave(p);
 56523   return rc;
 56527 #ifndef SQLITE_OMIT_SHARED_CACHE
 56528 /*
 56529 ** Obtain a lock on the table whose root page is iTab.  The
 56530 ** lock is a write lock if isWritelock is true or a read lock
 56531 ** if it is false.
 56532 */
 56533 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
 56534   int rc = SQLITE_OK;
 56535   assert( p->inTrans!=TRANS_NONE );
 56536   if( p->sharable ){
 56537     u8 lockType = READ_LOCK + isWriteLock;
 56538     assert( READ_LOCK+1==WRITE_LOCK );
 56539     assert( isWriteLock==0 || isWriteLock==1 );
 56541     sqlite3BtreeEnter(p);
 56542     rc = querySharedCacheTableLock(p, iTab, lockType);
 56543     if( rc==SQLITE_OK ){
 56544       rc = setSharedCacheTableLock(p, iTab, lockType);
 56546     sqlite3BtreeLeave(p);
 56548   return rc;
 56550 #endif
 56552 #ifndef SQLITE_OMIT_INCRBLOB
 56553 /*
 56554 ** Argument pCsr must be a cursor opened for writing on an 
 56555 ** INTKEY table currently pointing at a valid table entry. 
 56556 ** This function modifies the data stored as part of that entry.
 56557 **
 56558 ** Only the data content may only be modified, it is not possible to 
 56559 ** change the length of the data stored. If this function is called with
 56560 ** parameters that attempt to write past the end of the existing data,
 56561 ** no modifications are made and SQLITE_CORRUPT is returned.
 56562 */
 56563 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
 56564   int rc;
 56565   assert( cursorHoldsMutex(pCsr) );
 56566   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
 56567   assert( pCsr->isIncrblobHandle );
 56569   rc = restoreCursorPosition(pCsr);
 56570   if( rc!=SQLITE_OK ){
 56571     return rc;
 56573   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
 56574   if( pCsr->eState!=CURSOR_VALID ){
 56575     return SQLITE_ABORT;
 56578   /* Check some assumptions: 
 56579   **   (a) the cursor is open for writing,
 56580   **   (b) there is a read/write transaction open,
 56581   **   (c) the connection holds a write-lock on the table (if required),
 56582   **   (d) there are no conflicting read-locks, and
 56583   **   (e) the cursor points at a valid row of an intKey table.
 56584   */
 56585   if( !pCsr->wrFlag ){
 56586     return SQLITE_READONLY;
 56588   assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
 56589               && pCsr->pBt->inTransaction==TRANS_WRITE );
 56590   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
 56591   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
 56592   assert( pCsr->apPage[pCsr->iPage]->intKey );
 56594   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
 56597 /* 
 56598 ** Set a flag on this cursor to cache the locations of pages from the 
 56599 ** overflow list for the current row. This is used by cursors opened
 56600 ** for incremental blob IO only.
 56601 **
 56602 ** This function sets a flag only. The actual page location cache
 56603 ** (stored in BtCursor.aOverflow[]) is allocated and used by function
 56604 ** accessPayload() (the worker function for sqlite3BtreeData() and
 56605 ** sqlite3BtreePutData()).
 56606 */
 56607 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
 56608   assert( cursorHoldsMutex(pCur) );
 56609   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 56610   invalidateOverflowCache(pCur);
 56611   pCur->isIncrblobHandle = 1;
 56613 #endif
 56615 /*
 56616 ** Set both the "read version" (single byte at byte offset 18) and 
 56617 ** "write version" (single byte at byte offset 19) fields in the database
 56618 ** header to iVersion.
 56619 */
 56620 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
 56621   BtShared *pBt = pBtree->pBt;
 56622   int rc;                         /* Return code */
 56624   assert( iVersion==1 || iVersion==2 );
 56626   /* If setting the version fields to 1, do not automatically open the
 56627   ** WAL connection, even if the version fields are currently set to 2.
 56628   */
 56629   pBt->btsFlags &= ~BTS_NO_WAL;
 56630   if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
 56632   rc = sqlite3BtreeBeginTrans(pBtree, 0);
 56633   if( rc==SQLITE_OK ){
 56634     u8 *aData = pBt->pPage1->aData;
 56635     if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
 56636       rc = sqlite3BtreeBeginTrans(pBtree, 2);
 56637       if( rc==SQLITE_OK ){
 56638         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 56639         if( rc==SQLITE_OK ){
 56640           aData[18] = (u8)iVersion;
 56641           aData[19] = (u8)iVersion;
 56647   pBt->btsFlags &= ~BTS_NO_WAL;
 56648   return rc;
 56651 /*
 56652 ** set the mask of hint flags for cursor pCsr. Currently the only valid
 56653 ** values are 0 and BTREE_BULKLOAD.
 56654 */
 56655 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
 56656   assert( mask==BTREE_BULKLOAD || mask==0 );
 56657   pCsr->hints = mask;
 56660 /************** End of btree.c ***********************************************/
 56661 /************** Begin file backup.c ******************************************/
 56662 /*
 56663 ** 2009 January 28
 56664 **
 56665 ** The author disclaims copyright to this source code.  In place of
 56666 ** a legal notice, here is a blessing:
 56667 **
 56668 **    May you do good and not evil.
 56669 **    May you find forgiveness for yourself and forgive others.
 56670 **    May you share freely, never taking more than you give.
 56671 **
 56672 *************************************************************************
 56673 ** This file contains the implementation of the sqlite3_backup_XXX() 
 56674 ** API functions and the related features.
 56675 */
 56677 /* Macro to find the minimum of two numeric values.
 56678 */
 56679 #ifndef MIN
 56680 # define MIN(x,y) ((x)<(y)?(x):(y))
 56681 #endif
 56683 /*
 56684 ** Structure allocated for each backup operation.
 56685 */
 56686 struct sqlite3_backup {
 56687   sqlite3* pDestDb;        /* Destination database handle */
 56688   Btree *pDest;            /* Destination b-tree file */
 56689   u32 iDestSchema;         /* Original schema cookie in destination */
 56690   int bDestLocked;         /* True once a write-transaction is open on pDest */
 56692   Pgno iNext;              /* Page number of the next source page to copy */
 56693   sqlite3* pSrcDb;         /* Source database handle */
 56694   Btree *pSrc;             /* Source b-tree file */
 56696   int rc;                  /* Backup process error code */
 56698   /* These two variables are set by every call to backup_step(). They are
 56699   ** read by calls to backup_remaining() and backup_pagecount().
 56700   */
 56701   Pgno nRemaining;         /* Number of pages left to copy */
 56702   Pgno nPagecount;         /* Total number of pages to copy */
 56704   int isAttached;          /* True once backup has been registered with pager */
 56705   sqlite3_backup *pNext;   /* Next backup associated with source pager */
 56706 };
 56708 /*
 56709 ** THREAD SAFETY NOTES:
 56710 **
 56711 **   Once it has been created using backup_init(), a single sqlite3_backup
 56712 **   structure may be accessed via two groups of thread-safe entry points:
 56713 **
 56714 **     * Via the sqlite3_backup_XXX() API function backup_step() and 
 56715 **       backup_finish(). Both these functions obtain the source database
 56716 **       handle mutex and the mutex associated with the source BtShared 
 56717 **       structure, in that order.
 56718 **
 56719 **     * Via the BackupUpdate() and BackupRestart() functions, which are
 56720 **       invoked by the pager layer to report various state changes in
 56721 **       the page cache associated with the source database. The mutex
 56722 **       associated with the source database BtShared structure will always 
 56723 **       be held when either of these functions are invoked.
 56724 **
 56725 **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
 56726 **   backup_pagecount() are not thread-safe functions. If they are called
 56727 **   while some other thread is calling backup_step() or backup_finish(),
 56728 **   the values returned may be invalid. There is no way for a call to
 56729 **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
 56730 **   or backup_pagecount().
 56731 **
 56732 **   Depending on the SQLite configuration, the database handles and/or
 56733 **   the Btree objects may have their own mutexes that require locking.
 56734 **   Non-sharable Btrees (in-memory databases for example), do not have
 56735 **   associated mutexes.
 56736 */
 56738 /*
 56739 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
 56740 ** in connection handle pDb. If such a database cannot be found, return
 56741 ** a NULL pointer and write an error message to pErrorDb.
 56742 **
 56743 ** If the "temp" database is requested, it may need to be opened by this 
 56744 ** function. If an error occurs while doing so, return 0 and write an 
 56745 ** error message to pErrorDb.
 56746 */
 56747 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
 56748   int i = sqlite3FindDbName(pDb, zDb);
 56750   if( i==1 ){
 56751     Parse *pParse;
 56752     int rc = 0;
 56753     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
 56754     if( pParse==0 ){
 56755       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
 56756       rc = SQLITE_NOMEM;
 56757     }else{
 56758       pParse->db = pDb;
 56759       if( sqlite3OpenTempDatabase(pParse) ){
 56760         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
 56761         rc = SQLITE_ERROR;
 56763       sqlite3DbFree(pErrorDb, pParse->zErrMsg);
 56764       sqlite3StackFree(pErrorDb, pParse);
 56766     if( rc ){
 56767       return 0;
 56771   if( i<0 ){
 56772     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
 56773     return 0;
 56776   return pDb->aDb[i].pBt;
 56779 /*
 56780 ** Attempt to set the page size of the destination to match the page size
 56781 ** of the source.
 56782 */
 56783 static int setDestPgsz(sqlite3_backup *p){
 56784   int rc;
 56785   rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
 56786   return rc;
 56789 /*
 56790 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
 56791 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
 56792 ** a pointer to the new sqlite3_backup object.
 56793 **
 56794 ** If an error occurs, NULL is returned and an error code and error message
 56795 ** stored in database handle pDestDb.
 56796 */
 56797 SQLITE_API sqlite3_backup *sqlite3_backup_init(
 56798   sqlite3* pDestDb,                     /* Database to write to */
 56799   const char *zDestDb,                  /* Name of database within pDestDb */
 56800   sqlite3* pSrcDb,                      /* Database connection to read from */
 56801   const char *zSrcDb                    /* Name of database within pSrcDb */
 56802 ){
 56803   sqlite3_backup *p;                    /* Value to return */
 56805   /* Lock the source database handle. The destination database
 56806   ** handle is not locked in this routine, but it is locked in
 56807   ** sqlite3_backup_step(). The user is required to ensure that no
 56808   ** other thread accesses the destination handle for the duration
 56809   ** of the backup operation.  Any attempt to use the destination
 56810   ** database connection while a backup is in progress may cause
 56811   ** a malfunction or a deadlock.
 56812   */
 56813   sqlite3_mutex_enter(pSrcDb->mutex);
 56814   sqlite3_mutex_enter(pDestDb->mutex);
 56816   if( pSrcDb==pDestDb ){
 56817     sqlite3Error(
 56818         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
 56819     );
 56820     p = 0;
 56821   }else {
 56822     /* Allocate space for a new sqlite3_backup object...
 56823     ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 56824     ** call to sqlite3_backup_init() and is destroyed by a call to
 56825     ** sqlite3_backup_finish(). */
 56826     p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
 56827     if( !p ){
 56828       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
 56832   /* If the allocation succeeded, populate the new object. */
 56833   if( p ){
 56834     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
 56835     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
 56836     p->pDestDb = pDestDb;
 56837     p->pSrcDb = pSrcDb;
 56838     p->iNext = 1;
 56839     p->isAttached = 0;
 56841     if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
 56842       /* One (or both) of the named databases did not exist or an OOM
 56843       ** error was hit.  The error has already been written into the
 56844       ** pDestDb handle.  All that is left to do here is free the
 56845       ** sqlite3_backup structure.
 56846       */
 56847       sqlite3_free(p);
 56848       p = 0;
 56851   if( p ){
 56852     p->pSrc->nBackup++;
 56855   sqlite3_mutex_leave(pDestDb->mutex);
 56856   sqlite3_mutex_leave(pSrcDb->mutex);
 56857   return p;
 56860 /*
 56861 ** Argument rc is an SQLite error code. Return true if this error is 
 56862 ** considered fatal if encountered during a backup operation. All errors
 56863 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
 56864 */
 56865 static int isFatalError(int rc){
 56866   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
 56869 /*
 56870 ** Parameter zSrcData points to a buffer containing the data for 
 56871 ** page iSrcPg from the source database. Copy this data into the 
 56872 ** destination database.
 56873 */
 56874 static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
 56875   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
 56876   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
 56877   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
 56878   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
 56879   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
 56880 #ifdef SQLITE_HAS_CODEC
 56881   /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
 56882   ** guaranteed that the shared-mutex is held by this thread, handle
 56883   ** p->pSrc may not actually be the owner.  */
 56884   int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
 56885   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
 56886 #endif
 56887   int rc = SQLITE_OK;
 56888   i64 iOff;
 56890   assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
 56891   assert( p->bDestLocked );
 56892   assert( !isFatalError(p->rc) );
 56893   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
 56894   assert( zSrcData );
 56896   /* Catch the case where the destination is an in-memory database and the
 56897   ** page sizes of the source and destination differ. 
 56898   */
 56899   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
 56900     rc = SQLITE_READONLY;
 56903 #ifdef SQLITE_HAS_CODEC
 56904   /* Backup is not possible if the page size of the destination is changing
 56905   ** and a codec is in use.
 56906   */
 56907   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
 56908     rc = SQLITE_READONLY;
 56911   /* Backup is not possible if the number of bytes of reserve space differ
 56912   ** between source and destination.  If there is a difference, try to
 56913   ** fix the destination to agree with the source.  If that is not possible,
 56914   ** then the backup cannot proceed.
 56915   */
 56916   if( nSrcReserve!=nDestReserve ){
 56917     u32 newPgsz = nSrcPgsz;
 56918     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
 56919     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
 56921 #endif
 56923   /* This loop runs once for each destination page spanned by the source 
 56924   ** page. For each iteration, variable iOff is set to the byte offset
 56925   ** of the destination page.
 56926   */
 56927   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
 56928     DbPage *pDestPg = 0;
 56929     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
 56930     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
 56931     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
 56932      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
 56933     ){
 56934       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
 56935       u8 *zDestData = sqlite3PagerGetData(pDestPg);
 56936       u8 *zOut = &zDestData[iOff%nDestPgsz];
 56938       /* Copy the data from the source page into the destination page.
 56939       ** Then clear the Btree layer MemPage.isInit flag. Both this module
 56940       ** and the pager code use this trick (clearing the first byte
 56941       ** of the page 'extra' space to invalidate the Btree layers
 56942       ** cached parse of the page). MemPage.isInit is marked 
 56943       ** "MUST BE FIRST" for this purpose.
 56944       */
 56945       memcpy(zOut, zIn, nCopy);
 56946       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
 56948     sqlite3PagerUnref(pDestPg);
 56951   return rc;
 56954 /*
 56955 ** If pFile is currently larger than iSize bytes, then truncate it to
 56956 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
 56957 ** this function is a no-op.
 56958 **
 56959 ** Return SQLITE_OK if everything is successful, or an SQLite error 
 56960 ** code if an error occurs.
 56961 */
 56962 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
 56963   i64 iCurrent;
 56964   int rc = sqlite3OsFileSize(pFile, &iCurrent);
 56965   if( rc==SQLITE_OK && iCurrent>iSize ){
 56966     rc = sqlite3OsTruncate(pFile, iSize);
 56968   return rc;
 56971 /*
 56972 ** Register this backup object with the associated source pager for
 56973 ** callbacks when pages are changed or the cache invalidated.
 56974 */
 56975 static void attachBackupObject(sqlite3_backup *p){
 56976   sqlite3_backup **pp;
 56977   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
 56978   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
 56979   p->pNext = *pp;
 56980   *pp = p;
 56981   p->isAttached = 1;
 56984 /*
 56985 ** Copy nPage pages from the source b-tree to the destination.
 56986 */
 56987 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
 56988   int rc;
 56989   int destMode;       /* Destination journal mode */
 56990   int pgszSrc = 0;    /* Source page size */
 56991   int pgszDest = 0;   /* Destination page size */
 56993   sqlite3_mutex_enter(p->pSrcDb->mutex);
 56994   sqlite3BtreeEnter(p->pSrc);
 56995   if( p->pDestDb ){
 56996     sqlite3_mutex_enter(p->pDestDb->mutex);
 56999   rc = p->rc;
 57000   if( !isFatalError(rc) ){
 57001     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
 57002     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
 57003     int ii;                            /* Iterator variable */
 57004     int nSrcPage = -1;                 /* Size of source db in pages */
 57005     int bCloseTrans = 0;               /* True if src db requires unlocking */
 57007     /* If the source pager is currently in a write-transaction, return
 57008     ** SQLITE_BUSY immediately.
 57009     */
 57010     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
 57011       rc = SQLITE_BUSY;
 57012     }else{
 57013       rc = SQLITE_OK;
 57016     /* Lock the destination database, if it is not locked already. */
 57017     if( SQLITE_OK==rc && p->bDestLocked==0
 57018      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
 57019     ){
 57020       p->bDestLocked = 1;
 57021       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
 57024     /* If there is no open read-transaction on the source database, open
 57025     ** one now. If a transaction is opened here, then it will be closed
 57026     ** before this function exits.
 57027     */
 57028     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
 57029       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
 57030       bCloseTrans = 1;
 57033     /* Do not allow backup if the destination database is in WAL mode
 57034     ** and the page sizes are different between source and destination */
 57035     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
 57036     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
 57037     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
 57038     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
 57039       rc = SQLITE_READONLY;
 57042     /* Now that there is a read-lock on the source database, query the
 57043     ** source pager for the number of pages in the database.
 57044     */
 57045     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
 57046     assert( nSrcPage>=0 );
 57047     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
 57048       const Pgno iSrcPg = p->iNext;                 /* Source page number */
 57049       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
 57050         DbPage *pSrcPg;                             /* Source page object */
 57051         rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
 57052         if( rc==SQLITE_OK ){
 57053           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
 57054           sqlite3PagerUnref(pSrcPg);
 57057       p->iNext++;
 57059     if( rc==SQLITE_OK ){
 57060       p->nPagecount = nSrcPage;
 57061       p->nRemaining = nSrcPage+1-p->iNext;
 57062       if( p->iNext>(Pgno)nSrcPage ){
 57063         rc = SQLITE_DONE;
 57064       }else if( !p->isAttached ){
 57065         attachBackupObject(p);
 57069     /* Update the schema version field in the destination database. This
 57070     ** is to make sure that the schema-version really does change in
 57071     ** the case where the source and destination databases have the
 57072     ** same schema version.
 57073     */
 57074     if( rc==SQLITE_DONE ){
 57075       if( nSrcPage==0 ){
 57076         rc = sqlite3BtreeNewDb(p->pDest);
 57077         nSrcPage = 1;
 57079       if( rc==SQLITE_OK || rc==SQLITE_DONE ){
 57080         rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
 57082       if( rc==SQLITE_OK ){
 57083         if( p->pDestDb ){
 57084           sqlite3ResetAllSchemasOfConnection(p->pDestDb);
 57086         if( destMode==PAGER_JOURNALMODE_WAL ){
 57087           rc = sqlite3BtreeSetVersion(p->pDest, 2);
 57090       if( rc==SQLITE_OK ){
 57091         int nDestTruncate;
 57092         /* Set nDestTruncate to the final number of pages in the destination
 57093         ** database. The complication here is that the destination page
 57094         ** size may be different to the source page size. 
 57095         **
 57096         ** If the source page size is smaller than the destination page size, 
 57097         ** round up. In this case the call to sqlite3OsTruncate() below will
 57098         ** fix the size of the file. However it is important to call
 57099         ** sqlite3PagerTruncateImage() here so that any pages in the 
 57100         ** destination file that lie beyond the nDestTruncate page mark are
 57101         ** journalled by PagerCommitPhaseOne() before they are destroyed
 57102         ** by the file truncation.
 57103         */
 57104         assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
 57105         assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
 57106         if( pgszSrc<pgszDest ){
 57107           int ratio = pgszDest/pgszSrc;
 57108           nDestTruncate = (nSrcPage+ratio-1)/ratio;
 57109           if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
 57110             nDestTruncate--;
 57112         }else{
 57113           nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
 57115         assert( nDestTruncate>0 );
 57116         sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
 57118         if( pgszSrc<pgszDest ){
 57119           /* If the source page-size is smaller than the destination page-size,
 57120           ** two extra things may need to happen:
 57121           **
 57122           **   * The destination may need to be truncated, and
 57123           **
 57124           **   * Data stored on the pages immediately following the 
 57125           **     pending-byte page in the source database may need to be
 57126           **     copied into the destination database.
 57127           */
 57128           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
 57129           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
 57130           i64 iOff;
 57131           i64 iEnd;
 57133           assert( pFile );
 57134           assert( nDestTruncate==0 
 57135               || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
 57136                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
 57137              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
 57138           ));
 57140           /* This call ensures that all data required to recreate the original
 57141           ** database has been stored in the journal for pDestPager and the
 57142           ** journal synced to disk. So at this point we may safely modify
 57143           ** the database file in any way, knowing that if a power failure
 57144           ** occurs, the original database will be reconstructed from the 
 57145           ** journal file.  */
 57146           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
 57148           /* Write the extra pages and truncate the database file as required */
 57149           iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
 57150           for(
 57151             iOff=PENDING_BYTE+pgszSrc; 
 57152             rc==SQLITE_OK && iOff<iEnd; 
 57153             iOff+=pgszSrc
 57154           ){
 57155             PgHdr *pSrcPg = 0;
 57156             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
 57157             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
 57158             if( rc==SQLITE_OK ){
 57159               u8 *zData = sqlite3PagerGetData(pSrcPg);
 57160               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
 57162             sqlite3PagerUnref(pSrcPg);
 57164           if( rc==SQLITE_OK ){
 57165             rc = backupTruncateFile(pFile, iSize);
 57168           /* Sync the database file to disk. */
 57169           if( rc==SQLITE_OK ){
 57170             rc = sqlite3PagerSync(pDestPager);
 57172         }else{
 57173           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
 57176         /* Finish committing the transaction to the destination database. */
 57177         if( SQLITE_OK==rc
 57178          && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
 57179         ){
 57180           rc = SQLITE_DONE;
 57185     /* If bCloseTrans is true, then this function opened a read transaction
 57186     ** on the source database. Close the read transaction here. There is
 57187     ** no need to check the return values of the btree methods here, as
 57188     ** "committing" a read-only transaction cannot fail.
 57189     */
 57190     if( bCloseTrans ){
 57191       TESTONLY( int rc2 );
 57192       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
 57193       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
 57194       assert( rc2==SQLITE_OK );
 57197     if( rc==SQLITE_IOERR_NOMEM ){
 57198       rc = SQLITE_NOMEM;
 57200     p->rc = rc;
 57202   if( p->pDestDb ){
 57203     sqlite3_mutex_leave(p->pDestDb->mutex);
 57205   sqlite3BtreeLeave(p->pSrc);
 57206   sqlite3_mutex_leave(p->pSrcDb->mutex);
 57207   return rc;
 57210 /*
 57211 ** Release all resources associated with an sqlite3_backup* handle.
 57212 */
 57213 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
 57214   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
 57215   sqlite3 *pSrcDb;                     /* Source database connection */
 57216   int rc;                              /* Value to return */
 57218   /* Enter the mutexes */
 57219   if( p==0 ) return SQLITE_OK;
 57220   pSrcDb = p->pSrcDb;
 57221   sqlite3_mutex_enter(pSrcDb->mutex);
 57222   sqlite3BtreeEnter(p->pSrc);
 57223   if( p->pDestDb ){
 57224     sqlite3_mutex_enter(p->pDestDb->mutex);
 57227   /* Detach this backup from the source pager. */
 57228   if( p->pDestDb ){
 57229     p->pSrc->nBackup--;
 57231   if( p->isAttached ){
 57232     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
 57233     while( *pp!=p ){
 57234       pp = &(*pp)->pNext;
 57236     *pp = p->pNext;
 57239   /* If a transaction is still open on the Btree, roll it back. */
 57240   sqlite3BtreeRollback(p->pDest, SQLITE_OK);
 57242   /* Set the error code of the destination database handle. */
 57243   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
 57244   sqlite3Error(p->pDestDb, rc, 0);
 57246   /* Exit the mutexes and free the backup context structure. */
 57247   if( p->pDestDb ){
 57248     sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
 57250   sqlite3BtreeLeave(p->pSrc);
 57251   if( p->pDestDb ){
 57252     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 57253     ** call to sqlite3_backup_init() and is destroyed by a call to
 57254     ** sqlite3_backup_finish(). */
 57255     sqlite3_free(p);
 57257   sqlite3LeaveMutexAndCloseZombie(pSrcDb);
 57258   return rc;
 57261 /*
 57262 ** Return the number of pages still to be backed up as of the most recent
 57263 ** call to sqlite3_backup_step().
 57264 */
 57265 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
 57266   return p->nRemaining;
 57269 /*
 57270 ** Return the total number of pages in the source database as of the most 
 57271 ** recent call to sqlite3_backup_step().
 57272 */
 57273 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
 57274   return p->nPagecount;
 57277 /*
 57278 ** This function is called after the contents of page iPage of the
 57279 ** source database have been modified. If page iPage has already been 
 57280 ** copied into the destination database, then the data written to the
 57281 ** destination is now invalidated. The destination copy of iPage needs
 57282 ** to be updated with the new data before the backup operation is
 57283 ** complete.
 57284 **
 57285 ** It is assumed that the mutex associated with the BtShared object
 57286 ** corresponding to the source database is held when this function is
 57287 ** called.
 57288 */
 57289 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
 57290   sqlite3_backup *p;                   /* Iterator variable */
 57291   for(p=pBackup; p; p=p->pNext){
 57292     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
 57293     if( !isFatalError(p->rc) && iPage<p->iNext ){
 57294       /* The backup process p has already copied page iPage. But now it
 57295       ** has been modified by a transaction on the source pager. Copy
 57296       ** the new data into the backup.
 57297       */
 57298       int rc;
 57299       assert( p->pDestDb );
 57300       sqlite3_mutex_enter(p->pDestDb->mutex);
 57301       rc = backupOnePage(p, iPage, aData);
 57302       sqlite3_mutex_leave(p->pDestDb->mutex);
 57303       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
 57304       if( rc!=SQLITE_OK ){
 57305         p->rc = rc;
 57311 /*
 57312 ** Restart the backup process. This is called when the pager layer
 57313 ** detects that the database has been modified by an external database
 57314 ** connection. In this case there is no way of knowing which of the
 57315 ** pages that have been copied into the destination database are still 
 57316 ** valid and which are not, so the entire process needs to be restarted.
 57317 **
 57318 ** It is assumed that the mutex associated with the BtShared object
 57319 ** corresponding to the source database is held when this function is
 57320 ** called.
 57321 */
 57322 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
 57323   sqlite3_backup *p;                   /* Iterator variable */
 57324   for(p=pBackup; p; p=p->pNext){
 57325     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
 57326     p->iNext = 1;
 57330 #ifndef SQLITE_OMIT_VACUUM
 57331 /*
 57332 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
 57333 ** must be active for both files.
 57334 **
 57335 ** The size of file pTo may be reduced by this operation. If anything 
 57336 ** goes wrong, the transaction on pTo is rolled back. If successful, the 
 57337 ** transaction is committed before returning.
 57338 */
 57339 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
 57340   int rc;
 57341   sqlite3_file *pFd;              /* File descriptor for database pTo */
 57342   sqlite3_backup b;
 57343   sqlite3BtreeEnter(pTo);
 57344   sqlite3BtreeEnter(pFrom);
 57346   assert( sqlite3BtreeIsInTrans(pTo) );
 57347   pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
 57348   if( pFd->pMethods ){
 57349     i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
 57350     rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
 57351     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 57352     if( rc ) goto copy_finished;
 57355   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
 57356   ** to 0. This is used by the implementations of sqlite3_backup_step()
 57357   ** and sqlite3_backup_finish() to detect that they are being called
 57358   ** from this function, not directly by the user.
 57359   */
 57360   memset(&b, 0, sizeof(b));
 57361   b.pSrcDb = pFrom->db;
 57362   b.pSrc = pFrom;
 57363   b.pDest = pTo;
 57364   b.iNext = 1;
 57366   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
 57367   ** file. By passing this as the number of pages to copy to
 57368   ** sqlite3_backup_step(), we can guarantee that the copy finishes 
 57369   ** within a single call (unless an error occurs). The assert() statement
 57370   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
 57371   ** or an error code.
 57372   */
 57373   sqlite3_backup_step(&b, 0x7FFFFFFF);
 57374   assert( b.rc!=SQLITE_OK );
 57375   rc = sqlite3_backup_finish(&b);
 57376   if( rc==SQLITE_OK ){
 57377     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
 57378   }else{
 57379     sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
 57382   assert( sqlite3BtreeIsInTrans(pTo)==0 );
 57383 copy_finished:
 57384   sqlite3BtreeLeave(pFrom);
 57385   sqlite3BtreeLeave(pTo);
 57386   return rc;
 57388 #endif /* SQLITE_OMIT_VACUUM */
 57390 /************** End of backup.c **********************************************/
 57391 /************** Begin file vdbemem.c *****************************************/
 57392 /*
 57393 ** 2004 May 26
 57394 **
 57395 ** The author disclaims copyright to this source code.  In place of
 57396 ** a legal notice, here is a blessing:
 57397 **
 57398 **    May you do good and not evil.
 57399 **    May you find forgiveness for yourself and forgive others.
 57400 **    May you share freely, never taking more than you give.
 57401 **
 57402 *************************************************************************
 57403 **
 57404 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
 57405 ** stores a single value in the VDBE.  Mem is an opaque structure visible
 57406 ** only within the VDBE.  Interface routines refer to a Mem using the
 57407 ** name sqlite_value
 57408 */
 57410 /*
 57411 ** If pMem is an object with a valid string representation, this routine
 57412 ** ensures the internal encoding for the string representation is
 57413 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
 57414 **
 57415 ** If pMem is not a string object, or the encoding of the string
 57416 ** representation is already stored using the requested encoding, then this
 57417 ** routine is a no-op.
 57418 **
 57419 ** SQLITE_OK is returned if the conversion is successful (or not required).
 57420 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
 57421 ** between formats.
 57422 */
 57423 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
 57424   int rc;
 57425   assert( (pMem->flags&MEM_RowSet)==0 );
 57426   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
 57427            || desiredEnc==SQLITE_UTF16BE );
 57428   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
 57429     return SQLITE_OK;
 57431   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57432 #ifdef SQLITE_OMIT_UTF16
 57433   return SQLITE_ERROR;
 57434 #else
 57436   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
 57437   ** then the encoding of the value may not have changed.
 57438   */
 57439   rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
 57440   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
 57441   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
 57442   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
 57443   return rc;
 57444 #endif
 57447 /*
 57448 ** Make sure pMem->z points to a writable allocation of at least 
 57449 ** n bytes.
 57450 **
 57451 ** If the third argument passed to this function is true, then memory
 57452 ** cell pMem must contain a string or blob. In this case the content is
 57453 ** preserved. Otherwise, if the third parameter to this function is false,
 57454 ** any current string or blob value may be discarded.
 57455 **
 57456 ** This function sets the MEM_Dyn flag and clears any xDel callback.
 57457 ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
 57458 ** not set, Mem.n is zeroed.
 57459 */
 57460 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
 57461   assert( 1 >=
 57462     ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
 57463     (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
 57464     ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
 57465     ((pMem->flags&MEM_Static) ? 1 : 0)
 57466   );
 57467   assert( (pMem->flags&MEM_RowSet)==0 );
 57469   /* If the preserve flag is set to true, then the memory cell must already
 57470   ** contain a valid string or blob value.  */
 57471   assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 57473   if( n<32 ) n = 32;
 57474   if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
 57475     if( preserve && pMem->z==pMem->zMalloc ){
 57476       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 57477       preserve = 0;
 57478     }else{
 57479       sqlite3DbFree(pMem->db, pMem->zMalloc);
 57480       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 57484   if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
 57485     memcpy(pMem->zMalloc, pMem->z, pMem->n);
 57487   if( pMem->flags&MEM_Dyn && pMem->xDel ){
 57488     assert( pMem->xDel!=SQLITE_DYNAMIC );
 57489     pMem->xDel((void *)(pMem->z));
 57492   pMem->z = pMem->zMalloc;
 57493   if( pMem->z==0 ){
 57494     pMem->flags = MEM_Null;
 57495   }else{
 57496     pMem->flags &= ~(MEM_Ephem|MEM_Static);
 57498   pMem->xDel = 0;
 57499   return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
 57502 /*
 57503 ** Make the given Mem object MEM_Dyn.  In other words, make it so
 57504 ** that any TEXT or BLOB content is stored in memory obtained from
 57505 ** malloc().  In this way, we know that the memory is safe to be
 57506 ** overwritten or altered.
 57507 **
 57508 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
 57509 */
 57510 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
 57511   int f;
 57512   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57513   assert( (pMem->flags&MEM_RowSet)==0 );
 57514   ExpandBlob(pMem);
 57515   f = pMem->flags;
 57516   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
 57517     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
 57518       return SQLITE_NOMEM;
 57520     pMem->z[pMem->n] = 0;
 57521     pMem->z[pMem->n+1] = 0;
 57522     pMem->flags |= MEM_Term;
 57523 #ifdef SQLITE_DEBUG
 57524     pMem->pScopyFrom = 0;
 57525 #endif
 57528   return SQLITE_OK;
 57531 /*
 57532 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
 57533 ** blob stored in dynamically allocated space.
 57534 */
 57535 #ifndef SQLITE_OMIT_INCRBLOB
 57536 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
 57537   if( pMem->flags & MEM_Zero ){
 57538     int nByte;
 57539     assert( pMem->flags&MEM_Blob );
 57540     assert( (pMem->flags&MEM_RowSet)==0 );
 57541     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57543     /* Set nByte to the number of bytes required to store the expanded blob. */
 57544     nByte = pMem->n + pMem->u.nZero;
 57545     if( nByte<=0 ){
 57546       nByte = 1;
 57548     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
 57549       return SQLITE_NOMEM;
 57552     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
 57553     pMem->n += pMem->u.nZero;
 57554     pMem->flags &= ~(MEM_Zero|MEM_Term);
 57556   return SQLITE_OK;
 57558 #endif
 57561 /*
 57562 ** Make sure the given Mem is \u0000 terminated.
 57563 */
 57564 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
 57565   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57566   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
 57567     return SQLITE_OK;   /* Nothing to do */
 57569   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
 57570     return SQLITE_NOMEM;
 57572   pMem->z[pMem->n] = 0;
 57573   pMem->z[pMem->n+1] = 0;
 57574   pMem->flags |= MEM_Term;
 57575   return SQLITE_OK;
 57578 /*
 57579 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
 57580 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
 57581 ** is a no-op.
 57582 **
 57583 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
 57584 **
 57585 ** A MEM_Null value will never be passed to this function. This function is
 57586 ** used for converting values to text for returning to the user (i.e. via
 57587 ** sqlite3_value_text()), or for ensuring that values to be used as btree
 57588 ** keys are strings. In the former case a NULL pointer is returned the
 57589 ** user and the later is an internal programming error.
 57590 */
 57591 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
 57592   int rc = SQLITE_OK;
 57593   int fg = pMem->flags;
 57594   const int nByte = 32;
 57596   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57597   assert( !(fg&MEM_Zero) );
 57598   assert( !(fg&(MEM_Str|MEM_Blob)) );
 57599   assert( fg&(MEM_Int|MEM_Real) );
 57600   assert( (pMem->flags&MEM_RowSet)==0 );
 57601   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 57604   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
 57605     return SQLITE_NOMEM;
 57608   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
 57609   ** string representation of the value. Then, if the required encoding
 57610   ** is UTF-16le or UTF-16be do a translation.
 57611   ** 
 57612   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
 57613   */
 57614   if( fg & MEM_Int ){
 57615     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
 57616   }else{
 57617     assert( fg & MEM_Real );
 57618     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
 57620   pMem->n = sqlite3Strlen30(pMem->z);
 57621   pMem->enc = SQLITE_UTF8;
 57622   pMem->flags |= MEM_Str|MEM_Term;
 57623   sqlite3VdbeChangeEncoding(pMem, enc);
 57624   return rc;
 57627 /*
 57628 ** Memory cell pMem contains the context of an aggregate function.
 57629 ** This routine calls the finalize method for that function.  The
 57630 ** result of the aggregate is stored back into pMem.
 57631 **
 57632 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
 57633 ** otherwise.
 57634 */
 57635 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
 57636   int rc = SQLITE_OK;
 57637   if( ALWAYS(pFunc && pFunc->xFinalize) ){
 57638     sqlite3_context ctx;
 57639     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
 57640     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57641     memset(&ctx, 0, sizeof(ctx));
 57642     ctx.s.flags = MEM_Null;
 57643     ctx.s.db = pMem->db;
 57644     ctx.pMem = pMem;
 57645     ctx.pFunc = pFunc;
 57646     pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
 57647     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
 57648     sqlite3DbFree(pMem->db, pMem->zMalloc);
 57649     memcpy(pMem, &ctx.s, sizeof(ctx.s));
 57650     rc = ctx.isError;
 57652   return rc;
 57655 /*
 57656 ** If the memory cell contains a string value that must be freed by
 57657 ** invoking an external callback, free it now. Calling this function
 57658 ** does not free any Mem.zMalloc buffer.
 57659 */
 57660 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
 57661   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
 57662   if( p->flags&MEM_Agg ){
 57663     sqlite3VdbeMemFinalize(p, p->u.pDef);
 57664     assert( (p->flags & MEM_Agg)==0 );
 57665     sqlite3VdbeMemRelease(p);
 57666   }else if( p->flags&MEM_Dyn && p->xDel ){
 57667     assert( (p->flags&MEM_RowSet)==0 );
 57668     assert( p->xDel!=SQLITE_DYNAMIC );
 57669     p->xDel((void *)p->z);
 57670     p->xDel = 0;
 57671   }else if( p->flags&MEM_RowSet ){
 57672     sqlite3RowSetClear(p->u.pRowSet);
 57673   }else if( p->flags&MEM_Frame ){
 57674     sqlite3VdbeMemSetNull(p);
 57678 /*
 57679 ** Release any memory held by the Mem. This may leave the Mem in an
 57680 ** inconsistent state, for example with (Mem.z==0) and
 57681 ** (Mem.type==SQLITE_TEXT).
 57682 */
 57683 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
 57684   VdbeMemRelease(p);
 57685   sqlite3DbFree(p->db, p->zMalloc);
 57686   p->z = 0;
 57687   p->zMalloc = 0;
 57688   p->xDel = 0;
 57691 /*
 57692 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
 57693 ** If the double is too large, return 0x8000000000000000.
 57694 **
 57695 ** Most systems appear to do this simply by assigning
 57696 ** variables and without the extra range tests.  But
 57697 ** there are reports that windows throws an expection
 57698 ** if the floating point value is out of range. (See ticket #2880.)
 57699 ** Because we do not completely understand the problem, we will
 57700 ** take the conservative approach and always do range tests
 57701 ** before attempting the conversion.
 57702 */
 57703 static i64 doubleToInt64(double r){
 57704 #ifdef SQLITE_OMIT_FLOATING_POINT
 57705   /* When floating-point is omitted, double and int64 are the same thing */
 57706   return r;
 57707 #else
 57708   /*
 57709   ** Many compilers we encounter do not define constants for the
 57710   ** minimum and maximum 64-bit integers, or they define them
 57711   ** inconsistently.  And many do not understand the "LL" notation.
 57712   ** So we define our own static constants here using nothing
 57713   ** larger than a 32-bit integer constant.
 57714   */
 57715   static const i64 maxInt = LARGEST_INT64;
 57716   static const i64 minInt = SMALLEST_INT64;
 57718   if( r<(double)minInt ){
 57719     return minInt;
 57720   }else if( r>(double)maxInt ){
 57721     /* minInt is correct here - not maxInt.  It turns out that assigning
 57722     ** a very large positive number to an integer results in a very large
 57723     ** negative integer.  This makes no sense, but it is what x86 hardware
 57724     ** does so for compatibility we will do the same in software. */
 57725     return minInt;
 57726   }else{
 57727     return (i64)r;
 57729 #endif
 57732 /*
 57733 ** Return some kind of integer value which is the best we can do
 57734 ** at representing the value that *pMem describes as an integer.
 57735 ** If pMem is an integer, then the value is exact.  If pMem is
 57736 ** a floating-point then the value returned is the integer part.
 57737 ** If pMem is a string or blob, then we make an attempt to convert
 57738 ** it into a integer and return that.  If pMem represents an
 57739 ** an SQL-NULL value, return 0.
 57740 **
 57741 ** If pMem represents a string value, its encoding might be changed.
 57742 */
 57743 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
 57744   int flags;
 57745   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57746   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 57747   flags = pMem->flags;
 57748   if( flags & MEM_Int ){
 57749     return pMem->u.i;
 57750   }else if( flags & MEM_Real ){
 57751     return doubleToInt64(pMem->r);
 57752   }else if( flags & (MEM_Str|MEM_Blob) ){
 57753     i64 value = 0;
 57754     assert( pMem->z || pMem->n==0 );
 57755     testcase( pMem->z==0 );
 57756     sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
 57757     return value;
 57758   }else{
 57759     return 0;
 57763 /*
 57764 ** Return the best representation of pMem that we can get into a
 57765 ** double.  If pMem is already a double or an integer, return its
 57766 ** value.  If it is a string or blob, try to convert it to a double.
 57767 ** If it is a NULL, return 0.0.
 57768 */
 57769 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
 57770   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57771   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 57772   if( pMem->flags & MEM_Real ){
 57773     return pMem->r;
 57774   }else if( pMem->flags & MEM_Int ){
 57775     return (double)pMem->u.i;
 57776   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
 57777     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 57778     double val = (double)0;
 57779     sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
 57780     return val;
 57781   }else{
 57782     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 57783     return (double)0;
 57787 /*
 57788 ** The MEM structure is already a MEM_Real.  Try to also make it a
 57789 ** MEM_Int if we can.
 57790 */
 57791 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
 57792   assert( pMem->flags & MEM_Real );
 57793   assert( (pMem->flags & MEM_RowSet)==0 );
 57794   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57795   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 57797   pMem->u.i = doubleToInt64(pMem->r);
 57799   /* Only mark the value as an integer if
 57800   **
 57801   **    (1) the round-trip conversion real->int->real is a no-op, and
 57802   **    (2) The integer is neither the largest nor the smallest
 57803   **        possible integer (ticket #3922)
 57804   **
 57805   ** The second and third terms in the following conditional enforces
 57806   ** the second condition under the assumption that addition overflow causes
 57807   ** values to wrap around.  On x86 hardware, the third term is always
 57808   ** true and could be omitted.  But we leave it in because other
 57809   ** architectures might behave differently.
 57810   */
 57811   if( pMem->r==(double)pMem->u.i
 57812    && pMem->u.i>SMALLEST_INT64
 57813 #if defined(__i486__) || defined(__x86_64__)
 57814    && ALWAYS(pMem->u.i<LARGEST_INT64)
 57815 #else
 57816    && pMem->u.i<LARGEST_INT64
 57817 #endif
 57818   ){
 57819     pMem->flags |= MEM_Int;
 57823 /*
 57824 ** Convert pMem to type integer.  Invalidate any prior representations.
 57825 */
 57826 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
 57827   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57828   assert( (pMem->flags & MEM_RowSet)==0 );
 57829   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 57831   pMem->u.i = sqlite3VdbeIntValue(pMem);
 57832   MemSetTypeFlag(pMem, MEM_Int);
 57833   return SQLITE_OK;
 57836 /*
 57837 ** Convert pMem so that it is of type MEM_Real.
 57838 ** Invalidate any prior representations.
 57839 */
 57840 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
 57841   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57842   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 57844   pMem->r = sqlite3VdbeRealValue(pMem);
 57845   MemSetTypeFlag(pMem, MEM_Real);
 57846   return SQLITE_OK;
 57849 /*
 57850 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
 57851 ** Invalidate any prior representations.
 57852 **
 57853 ** Every effort is made to force the conversion, even if the input
 57854 ** is a string that does not look completely like a number.  Convert
 57855 ** as much of the string as we can and ignore the rest.
 57856 */
 57857 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
 57858   if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
 57859     assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
 57860     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 57861     if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
 57862       MemSetTypeFlag(pMem, MEM_Int);
 57863     }else{
 57864       pMem->r = sqlite3VdbeRealValue(pMem);
 57865       MemSetTypeFlag(pMem, MEM_Real);
 57866       sqlite3VdbeIntegerAffinity(pMem);
 57869   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
 57870   pMem->flags &= ~(MEM_Str|MEM_Blob);
 57871   return SQLITE_OK;
 57874 /*
 57875 ** Delete any previous value and set the value stored in *pMem to NULL.
 57876 */
 57877 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
 57878   if( pMem->flags & MEM_Frame ){
 57879     VdbeFrame *pFrame = pMem->u.pFrame;
 57880     pFrame->pParent = pFrame->v->pDelFrame;
 57881     pFrame->v->pDelFrame = pFrame;
 57883   if( pMem->flags & MEM_RowSet ){
 57884     sqlite3RowSetClear(pMem->u.pRowSet);
 57886   MemSetTypeFlag(pMem, MEM_Null);
 57887   pMem->type = SQLITE_NULL;
 57890 /*
 57891 ** Delete any previous value and set the value to be a BLOB of length
 57892 ** n containing all zeros.
 57893 */
 57894 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
 57895   sqlite3VdbeMemRelease(pMem);
 57896   pMem->flags = MEM_Blob|MEM_Zero;
 57897   pMem->type = SQLITE_BLOB;
 57898   pMem->n = 0;
 57899   if( n<0 ) n = 0;
 57900   pMem->u.nZero = n;
 57901   pMem->enc = SQLITE_UTF8;
 57903 #ifdef SQLITE_OMIT_INCRBLOB
 57904   sqlite3VdbeMemGrow(pMem, n, 0);
 57905   if( pMem->z ){
 57906     pMem->n = n;
 57907     memset(pMem->z, 0, n);
 57909 #endif
 57912 /*
 57913 ** Delete any previous value and set the value stored in *pMem to val,
 57914 ** manifest type INTEGER.
 57915 */
 57916 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
 57917   sqlite3VdbeMemRelease(pMem);
 57918   pMem->u.i = val;
 57919   pMem->flags = MEM_Int;
 57920   pMem->type = SQLITE_INTEGER;
 57923 #ifndef SQLITE_OMIT_FLOATING_POINT
 57924 /*
 57925 ** Delete any previous value and set the value stored in *pMem to val,
 57926 ** manifest type REAL.
 57927 */
 57928 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
 57929   if( sqlite3IsNaN(val) ){
 57930     sqlite3VdbeMemSetNull(pMem);
 57931   }else{
 57932     sqlite3VdbeMemRelease(pMem);
 57933     pMem->r = val;
 57934     pMem->flags = MEM_Real;
 57935     pMem->type = SQLITE_FLOAT;
 57938 #endif
 57940 /*
 57941 ** Delete any previous value and set the value of pMem to be an
 57942 ** empty boolean index.
 57943 */
 57944 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
 57945   sqlite3 *db = pMem->db;
 57946   assert( db!=0 );
 57947   assert( (pMem->flags & MEM_RowSet)==0 );
 57948   sqlite3VdbeMemRelease(pMem);
 57949   pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
 57950   if( db->mallocFailed ){
 57951     pMem->flags = MEM_Null;
 57952   }else{
 57953     assert( pMem->zMalloc );
 57954     pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
 57955                                        sqlite3DbMallocSize(db, pMem->zMalloc));
 57956     assert( pMem->u.pRowSet!=0 );
 57957     pMem->flags = MEM_RowSet;
 57961 /*
 57962 ** Return true if the Mem object contains a TEXT or BLOB that is
 57963 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
 57964 */
 57965 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
 57966   assert( p->db!=0 );
 57967   if( p->flags & (MEM_Str|MEM_Blob) ){
 57968     int n = p->n;
 57969     if( p->flags & MEM_Zero ){
 57970       n += p->u.nZero;
 57972     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
 57974   return 0; 
 57977 #ifdef SQLITE_DEBUG
 57978 /*
 57979 ** This routine prepares a memory cell for modication by breaking
 57980 ** its link to a shallow copy and by marking any current shallow
 57981 ** copies of this cell as invalid.
 57982 **
 57983 ** This is used for testing and debugging only - to make sure shallow
 57984 ** copies are not misused.
 57985 */
 57986 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
 57987   int i;
 57988   Mem *pX;
 57989   for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
 57990     if( pX->pScopyFrom==pMem ){
 57991       pX->flags |= MEM_Invalid;
 57992       pX->pScopyFrom = 0;
 57995   pMem->pScopyFrom = 0;
 57997 #endif /* SQLITE_DEBUG */
 57999 /*
 58000 ** Size of struct Mem not including the Mem.zMalloc member.
 58001 */
 58002 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
 58004 /*
 58005 ** Make an shallow copy of pFrom into pTo.  Prior contents of
 58006 ** pTo are freed.  The pFrom->z field is not duplicated.  If
 58007 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
 58008 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
 58009 */
 58010 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
 58011   assert( (pFrom->flags & MEM_RowSet)==0 );
 58012   VdbeMemRelease(pTo);
 58013   memcpy(pTo, pFrom, MEMCELLSIZE);
 58014   pTo->xDel = 0;
 58015   if( (pFrom->flags&MEM_Static)==0 ){
 58016     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
 58017     assert( srcType==MEM_Ephem || srcType==MEM_Static );
 58018     pTo->flags |= srcType;
 58022 /*
 58023 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
 58024 ** freed before the copy is made.
 58025 */
 58026 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
 58027   int rc = SQLITE_OK;
 58029   assert( (pFrom->flags & MEM_RowSet)==0 );
 58030   VdbeMemRelease(pTo);
 58031   memcpy(pTo, pFrom, MEMCELLSIZE);
 58032   pTo->flags &= ~MEM_Dyn;
 58034   if( pTo->flags&(MEM_Str|MEM_Blob) ){
 58035     if( 0==(pFrom->flags&MEM_Static) ){
 58036       pTo->flags |= MEM_Ephem;
 58037       rc = sqlite3VdbeMemMakeWriteable(pTo);
 58041   return rc;
 58044 /*
 58045 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
 58046 ** freed. If pFrom contains ephemeral data, a copy is made.
 58047 **
 58048 ** pFrom contains an SQL NULL when this routine returns.
 58049 */
 58050 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
 58051   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
 58052   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
 58053   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
 58055   sqlite3VdbeMemRelease(pTo);
 58056   memcpy(pTo, pFrom, sizeof(Mem));
 58057   pFrom->flags = MEM_Null;
 58058   pFrom->xDel = 0;
 58059   pFrom->zMalloc = 0;
 58062 /*
 58063 ** Change the value of a Mem to be a string or a BLOB.
 58064 **
 58065 ** The memory management strategy depends on the value of the xDel
 58066 ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
 58067 ** string is copied into a (possibly existing) buffer managed by the 
 58068 ** Mem structure. Otherwise, any existing buffer is freed and the
 58069 ** pointer copied.
 58070 **
 58071 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
 58072 ** size limit) then no memory allocation occurs.  If the string can be
 58073 ** stored without allocating memory, then it is.  If a memory allocation
 58074 ** is required to store the string, then value of pMem is unchanged.  In
 58075 ** either case, SQLITE_TOOBIG is returned.
 58076 */
 58077 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
 58078   Mem *pMem,          /* Memory cell to set to string value */
 58079   const char *z,      /* String pointer */
 58080   int n,              /* Bytes in string, or negative */
 58081   u8 enc,             /* Encoding of z.  0 for BLOBs */
 58082   void (*xDel)(void*) /* Destructor function */
 58083 ){
 58084   int nByte = n;      /* New value for pMem->n */
 58085   int iLimit;         /* Maximum allowed string or blob size */
 58086   u16 flags = 0;      /* New value for pMem->flags */
 58088   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 58089   assert( (pMem->flags & MEM_RowSet)==0 );
 58091   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
 58092   if( !z ){
 58093     sqlite3VdbeMemSetNull(pMem);
 58094     return SQLITE_OK;
 58097   if( pMem->db ){
 58098     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
 58099   }else{
 58100     iLimit = SQLITE_MAX_LENGTH;
 58102   flags = (enc==0?MEM_Blob:MEM_Str);
 58103   if( nByte<0 ){
 58104     assert( enc!=0 );
 58105     if( enc==SQLITE_UTF8 ){
 58106       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
 58107     }else{
 58108       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
 58110     flags |= MEM_Term;
 58113   /* The following block sets the new values of Mem.z and Mem.xDel. It
 58114   ** also sets a flag in local variable "flags" to indicate the memory
 58115   ** management (one of MEM_Dyn or MEM_Static).
 58116   */
 58117   if( xDel==SQLITE_TRANSIENT ){
 58118     int nAlloc = nByte;
 58119     if( flags&MEM_Term ){
 58120       nAlloc += (enc==SQLITE_UTF8?1:2);
 58122     if( nByte>iLimit ){
 58123       return SQLITE_TOOBIG;
 58125     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
 58126       return SQLITE_NOMEM;
 58128     memcpy(pMem->z, z, nAlloc);
 58129   }else if( xDel==SQLITE_DYNAMIC ){
 58130     sqlite3VdbeMemRelease(pMem);
 58131     pMem->zMalloc = pMem->z = (char *)z;
 58132     pMem->xDel = 0;
 58133   }else{
 58134     sqlite3VdbeMemRelease(pMem);
 58135     pMem->z = (char *)z;
 58136     pMem->xDel = xDel;
 58137     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
 58140   pMem->n = nByte;
 58141   pMem->flags = flags;
 58142   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
 58143   pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
 58145 #ifndef SQLITE_OMIT_UTF16
 58146   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
 58147     return SQLITE_NOMEM;
 58149 #endif
 58151   if( nByte>iLimit ){
 58152     return SQLITE_TOOBIG;
 58155   return SQLITE_OK;
 58158 /*
 58159 ** Compare the values contained by the two memory cells, returning
 58160 ** negative, zero or positive if pMem1 is less than, equal to, or greater
 58161 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
 58162 ** and reals) sorted numerically, followed by text ordered by the collating
 58163 ** sequence pColl and finally blob's ordered by memcmp().
 58164 **
 58165 ** Two NULL values are considered equal by this function.
 58166 */
 58167 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
 58168   int rc;
 58169   int f1, f2;
 58170   int combined_flags;
 58172   f1 = pMem1->flags;
 58173   f2 = pMem2->flags;
 58174   combined_flags = f1|f2;
 58175   assert( (combined_flags & MEM_RowSet)==0 );
 58177   /* If one value is NULL, it is less than the other. If both values
 58178   ** are NULL, return 0.
 58179   */
 58180   if( combined_flags&MEM_Null ){
 58181     return (f2&MEM_Null) - (f1&MEM_Null);
 58184   /* If one value is a number and the other is not, the number is less.
 58185   ** If both are numbers, compare as reals if one is a real, or as integers
 58186   ** if both values are integers.
 58187   */
 58188   if( combined_flags&(MEM_Int|MEM_Real) ){
 58189     if( !(f1&(MEM_Int|MEM_Real)) ){
 58190       return 1;
 58192     if( !(f2&(MEM_Int|MEM_Real)) ){
 58193       return -1;
 58195     if( (f1 & f2 & MEM_Int)==0 ){
 58196       double r1, r2;
 58197       if( (f1&MEM_Real)==0 ){
 58198         r1 = (double)pMem1->u.i;
 58199       }else{
 58200         r1 = pMem1->r;
 58202       if( (f2&MEM_Real)==0 ){
 58203         r2 = (double)pMem2->u.i;
 58204       }else{
 58205         r2 = pMem2->r;
 58207       if( r1<r2 ) return -1;
 58208       if( r1>r2 ) return 1;
 58209       return 0;
 58210     }else{
 58211       assert( f1&MEM_Int );
 58212       assert( f2&MEM_Int );
 58213       if( pMem1->u.i < pMem2->u.i ) return -1;
 58214       if( pMem1->u.i > pMem2->u.i ) return 1;
 58215       return 0;
 58219   /* If one value is a string and the other is a blob, the string is less.
 58220   ** If both are strings, compare using the collating functions.
 58221   */
 58222   if( combined_flags&MEM_Str ){
 58223     if( (f1 & MEM_Str)==0 ){
 58224       return 1;
 58226     if( (f2 & MEM_Str)==0 ){
 58227       return -1;
 58230     assert( pMem1->enc==pMem2->enc );
 58231     assert( pMem1->enc==SQLITE_UTF8 || 
 58232             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
 58234     /* The collation sequence must be defined at this point, even if
 58235     ** the user deletes the collation sequence after the vdbe program is
 58236     ** compiled (this was not always the case).
 58237     */
 58238     assert( !pColl || pColl->xCmp );
 58240     if( pColl ){
 58241       if( pMem1->enc==pColl->enc ){
 58242         /* The strings are already in the correct encoding.  Call the
 58243         ** comparison function directly */
 58244         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
 58245       }else{
 58246         const void *v1, *v2;
 58247         int n1, n2;
 58248         Mem c1;
 58249         Mem c2;
 58250         memset(&c1, 0, sizeof(c1));
 58251         memset(&c2, 0, sizeof(c2));
 58252         sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
 58253         sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
 58254         v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
 58255         n1 = v1==0 ? 0 : c1.n;
 58256         v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
 58257         n2 = v2==0 ? 0 : c2.n;
 58258         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
 58259         sqlite3VdbeMemRelease(&c1);
 58260         sqlite3VdbeMemRelease(&c2);
 58261         return rc;
 58264     /* If a NULL pointer was passed as the collate function, fall through
 58265     ** to the blob case and use memcmp().  */
 58268   /* Both values must be blobs.  Compare using memcmp().  */
 58269   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
 58270   if( rc==0 ){
 58271     rc = pMem1->n - pMem2->n;
 58273   return rc;
 58276 /*
 58277 ** Move data out of a btree key or data field and into a Mem structure.
 58278 ** The data or key is taken from the entry that pCur is currently pointing
 58279 ** to.  offset and amt determine what portion of the data or key to retrieve.
 58280 ** key is true to get the key or false to get data.  The result is written
 58281 ** into the pMem element.
 58282 **
 58283 ** The pMem structure is assumed to be uninitialized.  Any prior content
 58284 ** is overwritten without being freed.
 58285 **
 58286 ** If this routine fails for any reason (malloc returns NULL or unable
 58287 ** to read from the disk) then the pMem is left in an inconsistent state.
 58288 */
 58289 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
 58290   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
 58291   int offset,       /* Offset from the start of data to return bytes from. */
 58292   int amt,          /* Number of bytes to return. */
 58293   int key,          /* If true, retrieve from the btree key, not data. */
 58294   Mem *pMem         /* OUT: Return data in this Mem structure. */
 58295 ){
 58296   char *zData;        /* Data from the btree layer */
 58297   int available = 0;  /* Number of bytes available on the local btree page */
 58298   int rc = SQLITE_OK; /* Return code */
 58300   assert( sqlite3BtreeCursorIsValid(pCur) );
 58302   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
 58303   ** that both the BtShared and database handle mutexes are held. */
 58304   assert( (pMem->flags & MEM_RowSet)==0 );
 58305   if( key ){
 58306     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
 58307   }else{
 58308     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
 58310   assert( zData!=0 );
 58312   if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
 58313     sqlite3VdbeMemRelease(pMem);
 58314     pMem->z = &zData[offset];
 58315     pMem->flags = MEM_Blob|MEM_Ephem;
 58316   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
 58317     pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
 58318     pMem->enc = 0;
 58319     pMem->type = SQLITE_BLOB;
 58320     if( key ){
 58321       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
 58322     }else{
 58323       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
 58325     pMem->z[amt] = 0;
 58326     pMem->z[amt+1] = 0;
 58327     if( rc!=SQLITE_OK ){
 58328       sqlite3VdbeMemRelease(pMem);
 58331   pMem->n = amt;
 58333   return rc;
 58336 /* This function is only available internally, it is not part of the
 58337 ** external API. It works in a similar way to sqlite3_value_text(),
 58338 ** except the data returned is in the encoding specified by the second
 58339 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
 58340 ** SQLITE_UTF8.
 58341 **
 58342 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
 58343 ** If that is the case, then the result must be aligned on an even byte
 58344 ** boundary.
 58345 */
 58346 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
 58347   if( !pVal ) return 0;
 58349   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 58350   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 58351   assert( (pVal->flags & MEM_RowSet)==0 );
 58353   if( pVal->flags&MEM_Null ){
 58354     return 0;
 58356   assert( (MEM_Blob>>3) == MEM_Str );
 58357   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
 58358   ExpandBlob(pVal);
 58359   if( pVal->flags&MEM_Str ){
 58360     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
 58361     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
 58362       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
 58363       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
 58364         return 0;
 58367     sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
 58368   }else{
 58369     assert( (pVal->flags&MEM_Blob)==0 );
 58370     sqlite3VdbeMemStringify(pVal, enc);
 58371     assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
 58373   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
 58374               || pVal->db->mallocFailed );
 58375   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
 58376     return pVal->z;
 58377   }else{
 58378     return 0;
 58382 /*
 58383 ** Create a new sqlite3_value object.
 58384 */
 58385 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
 58386   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
 58387   if( p ){
 58388     p->flags = MEM_Null;
 58389     p->type = SQLITE_NULL;
 58390     p->db = db;
 58392   return p;
 58395 /*
 58396 ** Create a new sqlite3_value object, containing the value of pExpr.
 58397 **
 58398 ** This only works for very simple expressions that consist of one constant
 58399 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
 58400 ** be converted directly into a value, then the value is allocated and
 58401 ** a pointer written to *ppVal. The caller is responsible for deallocating
 58402 ** the value by passing it to sqlite3ValueFree() later on. If the expression
 58403 ** cannot be converted to a value, then *ppVal is set to NULL.
 58404 */
 58405 SQLITE_PRIVATE int sqlite3ValueFromExpr(
 58406   sqlite3 *db,              /* The database connection */
 58407   Expr *pExpr,              /* The expression to evaluate */
 58408   u8 enc,                   /* Encoding to use */
 58409   u8 affinity,              /* Affinity to use */
 58410   sqlite3_value **ppVal     /* Write the new value here */
 58411 ){
 58412   int op;
 58413   char *zVal = 0;
 58414   sqlite3_value *pVal = 0;
 58415   int negInt = 1;
 58416   const char *zNeg = "";
 58418   if( !pExpr ){
 58419     *ppVal = 0;
 58420     return SQLITE_OK;
 58422   op = pExpr->op;
 58424   /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
 58425   ** The ifdef here is to enable us to achieve 100% branch test coverage even
 58426   ** when SQLITE_ENABLE_STAT3 is omitted.
 58427   */
 58428 #ifdef SQLITE_ENABLE_STAT3
 58429   if( op==TK_REGISTER ) op = pExpr->op2;
 58430 #else
 58431   if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
 58432 #endif
 58434   /* Handle negative integers in a single step.  This is needed in the
 58435   ** case when the value is -9223372036854775808.
 58436   */
 58437   if( op==TK_UMINUS
 58438    && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
 58439     pExpr = pExpr->pLeft;
 58440     op = pExpr->op;
 58441     negInt = -1;
 58442     zNeg = "-";
 58445   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
 58446     pVal = sqlite3ValueNew(db);
 58447     if( pVal==0 ) goto no_mem;
 58448     if( ExprHasProperty(pExpr, EP_IntValue) ){
 58449       sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
 58450     }else{
 58451       zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
 58452       if( zVal==0 ) goto no_mem;
 58453       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
 58454       if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
 58456     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
 58457       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
 58458     }else{
 58459       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
 58461     if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
 58462     if( enc!=SQLITE_UTF8 ){
 58463       sqlite3VdbeChangeEncoding(pVal, enc);
 58465   }else if( op==TK_UMINUS ) {
 58466     /* This branch happens for multiple negative signs.  Ex: -(-5) */
 58467     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
 58468       sqlite3VdbeMemNumerify(pVal);
 58469       if( pVal->u.i==SMALLEST_INT64 ){
 58470         pVal->flags &= MEM_Int;
 58471         pVal->flags |= MEM_Real;
 58472         pVal->r = (double)LARGEST_INT64;
 58473       }else{
 58474         pVal->u.i = -pVal->u.i;
 58476       pVal->r = -pVal->r;
 58477       sqlite3ValueApplyAffinity(pVal, affinity, enc);
 58479   }else if( op==TK_NULL ){
 58480     pVal = sqlite3ValueNew(db);
 58481     if( pVal==0 ) goto no_mem;
 58483 #ifndef SQLITE_OMIT_BLOB_LITERAL
 58484   else if( op==TK_BLOB ){
 58485     int nVal;
 58486     assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 58487     assert( pExpr->u.zToken[1]=='\'' );
 58488     pVal = sqlite3ValueNew(db);
 58489     if( !pVal ) goto no_mem;
 58490     zVal = &pExpr->u.zToken[2];
 58491     nVal = sqlite3Strlen30(zVal)-1;
 58492     assert( zVal[nVal]=='\'' );
 58493     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
 58494                          0, SQLITE_DYNAMIC);
 58496 #endif
 58498   if( pVal ){
 58499     sqlite3VdbeMemStoreType(pVal);
 58501   *ppVal = pVal;
 58502   return SQLITE_OK;
 58504 no_mem:
 58505   db->mallocFailed = 1;
 58506   sqlite3DbFree(db, zVal);
 58507   sqlite3ValueFree(pVal);
 58508   *ppVal = 0;
 58509   return SQLITE_NOMEM;
 58512 /*
 58513 ** Change the string value of an sqlite3_value object
 58514 */
 58515 SQLITE_PRIVATE void sqlite3ValueSetStr(
 58516   sqlite3_value *v,     /* Value to be set */
 58517   int n,                /* Length of string z */
 58518   const void *z,        /* Text of the new string */
 58519   u8 enc,               /* Encoding to use */
 58520   void (*xDel)(void*)   /* Destructor for the string */
 58521 ){
 58522   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
 58525 /*
 58526 ** Free an sqlite3_value object
 58527 */
 58528 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
 58529   if( !v ) return;
 58530   sqlite3VdbeMemRelease((Mem *)v);
 58531   sqlite3DbFree(((Mem*)v)->db, v);
 58534 /*
 58535 ** Return the number of bytes in the sqlite3_value object assuming
 58536 ** that it uses the encoding "enc"
 58537 */
 58538 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
 58539   Mem *p = (Mem*)pVal;
 58540   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
 58541     if( p->flags & MEM_Zero ){
 58542       return p->n + p->u.nZero;
 58543     }else{
 58544       return p->n;
 58547   return 0;
 58550 /************** End of vdbemem.c *********************************************/
 58551 /************** Begin file vdbeaux.c *****************************************/
 58552 /*
 58553 ** 2003 September 6
 58554 **
 58555 ** The author disclaims copyright to this source code.  In place of
 58556 ** a legal notice, here is a blessing:
 58557 **
 58558 **    May you do good and not evil.
 58559 **    May you find forgiveness for yourself and forgive others.
 58560 **    May you share freely, never taking more than you give.
 58561 **
 58562 *************************************************************************
 58563 ** This file contains code used for creating, destroying, and populating
 58564 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
 58565 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
 58566 ** But that file was getting too big so this subroutines were split out.
 58567 */
 58571 /*
 58572 ** When debugging the code generator in a symbolic debugger, one can
 58573 ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
 58574 ** as they are added to the instruction stream.
 58575 */
 58576 #ifdef SQLITE_DEBUG
 58577 SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
 58578 #endif
 58581 /*
 58582 ** Create a new virtual database engine.
 58583 */
 58584 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
 58585   Vdbe *p;
 58586   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
 58587   if( p==0 ) return 0;
 58588   p->db = db;
 58589   if( db->pVdbe ){
 58590     db->pVdbe->pPrev = p;
 58592   p->pNext = db->pVdbe;
 58593   p->pPrev = 0;
 58594   db->pVdbe = p;
 58595   p->magic = VDBE_MAGIC_INIT;
 58596   return p;
 58599 /*
 58600 ** Remember the SQL string for a prepared statement.
 58601 */
 58602 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
 58603   assert( isPrepareV2==1 || isPrepareV2==0 );
 58604   if( p==0 ) return;
 58605 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
 58606   if( !isPrepareV2 ) return;
 58607 #endif
 58608   assert( p->zSql==0 );
 58609   p->zSql = sqlite3DbStrNDup(p->db, z, n);
 58610   p->isPrepareV2 = (u8)isPrepareV2;
 58613 /*
 58614 ** Return the SQL associated with a prepared statement
 58615 */
 58616 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
 58617   Vdbe *p = (Vdbe *)pStmt;
 58618   return (p && p->isPrepareV2) ? p->zSql : 0;
 58621 /*
 58622 ** Swap all content between two VDBE structures.
 58623 */
 58624 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
 58625   Vdbe tmp, *pTmp;
 58626   char *zTmp;
 58627   tmp = *pA;
 58628   *pA = *pB;
 58629   *pB = tmp;
 58630   pTmp = pA->pNext;
 58631   pA->pNext = pB->pNext;
 58632   pB->pNext = pTmp;
 58633   pTmp = pA->pPrev;
 58634   pA->pPrev = pB->pPrev;
 58635   pB->pPrev = pTmp;
 58636   zTmp = pA->zSql;
 58637   pA->zSql = pB->zSql;
 58638   pB->zSql = zTmp;
 58639   pB->isPrepareV2 = pA->isPrepareV2;
 58642 #ifdef SQLITE_DEBUG
 58643 /*
 58644 ** Turn tracing on or off
 58645 */
 58646 SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
 58647   p->trace = trace;
 58649 #endif
 58651 /*
 58652 ** Resize the Vdbe.aOp array so that it is at least one op larger than 
 58653 ** it was.
 58654 **
 58655 ** If an out-of-memory error occurs while resizing the array, return
 58656 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
 58657 ** unchanged (this is so that any opcodes already allocated can be 
 58658 ** correctly deallocated along with the rest of the Vdbe).
 58659 */
 58660 static int growOpArray(Vdbe *p){
 58661   VdbeOp *pNew;
 58662   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
 58663   pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
 58664   if( pNew ){
 58665     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
 58666     p->aOp = pNew;
 58668   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
 58671 /*
 58672 ** Add a new instruction to the list of instructions current in the
 58673 ** VDBE.  Return the address of the new instruction.
 58674 **
 58675 ** Parameters:
 58676 **
 58677 **    p               Pointer to the VDBE
 58678 **
 58679 **    op              The opcode for this instruction
 58680 **
 58681 **    p1, p2, p3      Operands
 58682 **
 58683 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
 58684 ** the sqlite3VdbeChangeP4() function to change the value of the P4
 58685 ** operand.
 58686 */
 58687 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
 58688   int i;
 58689   VdbeOp *pOp;
 58691   i = p->nOp;
 58692   assert( p->magic==VDBE_MAGIC_INIT );
 58693   assert( op>0 && op<0xff );
 58694   if( p->nOpAlloc<=i ){
 58695     if( growOpArray(p) ){
 58696       return 1;
 58699   p->nOp++;
 58700   pOp = &p->aOp[i];
 58701   pOp->opcode = (u8)op;
 58702   pOp->p5 = 0;
 58703   pOp->p1 = p1;
 58704   pOp->p2 = p2;
 58705   pOp->p3 = p3;
 58706   pOp->p4.p = 0;
 58707   pOp->p4type = P4_NOTUSED;
 58708 #ifdef SQLITE_DEBUG
 58709   pOp->zComment = 0;
 58710   if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
 58711 #endif
 58712 #ifdef VDBE_PROFILE
 58713   pOp->cycles = 0;
 58714   pOp->cnt = 0;
 58715 #endif
 58716   return i;
 58718 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
 58719   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
 58721 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
 58722   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
 58724 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
 58725   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
 58729 /*
 58730 ** Add an opcode that includes the p4 value as a pointer.
 58731 */
 58732 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
 58733   Vdbe *p,            /* Add the opcode to this VM */
 58734   int op,             /* The new opcode */
 58735   int p1,             /* The P1 operand */
 58736   int p2,             /* The P2 operand */
 58737   int p3,             /* The P3 operand */
 58738   const char *zP4,    /* The P4 operand */
 58739   int p4type          /* P4 operand type */
 58740 ){
 58741   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
 58742   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
 58743   return addr;
 58746 /*
 58747 ** Add an OP_ParseSchema opcode.  This routine is broken out from
 58748 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
 58749 ** as having been used.
 58750 **
 58751 ** The zWhere string must have been obtained from sqlite3_malloc().
 58752 ** This routine will take ownership of the allocated memory.
 58753 */
 58754 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
 58755   int j;
 58756   int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
 58757   sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
 58758   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
 58761 /*
 58762 ** Add an opcode that includes the p4 value as an integer.
 58763 */
 58764 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
 58765   Vdbe *p,            /* Add the opcode to this VM */
 58766   int op,             /* The new opcode */
 58767   int p1,             /* The P1 operand */
 58768   int p2,             /* The P2 operand */
 58769   int p3,             /* The P3 operand */
 58770   int p4              /* The P4 operand as an integer */
 58771 ){
 58772   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
 58773   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
 58774   return addr;
 58777 /*
 58778 ** Create a new symbolic label for an instruction that has yet to be
 58779 ** coded.  The symbolic label is really just a negative number.  The
 58780 ** label can be used as the P2 value of an operation.  Later, when
 58781 ** the label is resolved to a specific address, the VDBE will scan
 58782 ** through its operation list and change all values of P2 which match
 58783 ** the label into the resolved address.
 58784 **
 58785 ** The VDBE knows that a P2 value is a label because labels are
 58786 ** always negative and P2 values are suppose to be non-negative.
 58787 ** Hence, a negative P2 value is a label that has yet to be resolved.
 58788 **
 58789 ** Zero is returned if a malloc() fails.
 58790 */
 58791 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
 58792   int i = p->nLabel++;
 58793   assert( p->magic==VDBE_MAGIC_INIT );
 58794   if( (i & (i-1))==0 ){
 58795     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 
 58796                                        (i*2+1)*sizeof(p->aLabel[0]));
 58798   if( p->aLabel ){
 58799     p->aLabel[i] = -1;
 58801   return -1-i;
 58804 /*
 58805 ** Resolve label "x" to be the address of the next instruction to
 58806 ** be inserted.  The parameter "x" must have been obtained from
 58807 ** a prior call to sqlite3VdbeMakeLabel().
 58808 */
 58809 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
 58810   int j = -1-x;
 58811   assert( p->magic==VDBE_MAGIC_INIT );
 58812   assert( j>=0 && j<p->nLabel );
 58813   if( p->aLabel ){
 58814     p->aLabel[j] = p->nOp;
 58818 /*
 58819 ** Mark the VDBE as one that can only be run one time.
 58820 */
 58821 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
 58822   p->runOnlyOnce = 1;
 58825 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
 58827 /*
 58828 ** The following type and function are used to iterate through all opcodes
 58829 ** in a Vdbe main program and each of the sub-programs (triggers) it may 
 58830 ** invoke directly or indirectly. It should be used as follows:
 58831 **
 58832 **   Op *pOp;
 58833 **   VdbeOpIter sIter;
 58834 **
 58835 **   memset(&sIter, 0, sizeof(sIter));
 58836 **   sIter.v = v;                            // v is of type Vdbe* 
 58837 **   while( (pOp = opIterNext(&sIter)) ){
 58838 **     // Do something with pOp
 58839 **   }
 58840 **   sqlite3DbFree(v->db, sIter.apSub);
 58841 ** 
 58842 */
 58843 typedef struct VdbeOpIter VdbeOpIter;
 58844 struct VdbeOpIter {
 58845   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
 58846   SubProgram **apSub;        /* Array of subprograms */
 58847   int nSub;                  /* Number of entries in apSub */
 58848   int iAddr;                 /* Address of next instruction to return */
 58849   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
 58850 };
 58851 static Op *opIterNext(VdbeOpIter *p){
 58852   Vdbe *v = p->v;
 58853   Op *pRet = 0;
 58854   Op *aOp;
 58855   int nOp;
 58857   if( p->iSub<=p->nSub ){
 58859     if( p->iSub==0 ){
 58860       aOp = v->aOp;
 58861       nOp = v->nOp;
 58862     }else{
 58863       aOp = p->apSub[p->iSub-1]->aOp;
 58864       nOp = p->apSub[p->iSub-1]->nOp;
 58866     assert( p->iAddr<nOp );
 58868     pRet = &aOp[p->iAddr];
 58869     p->iAddr++;
 58870     if( p->iAddr==nOp ){
 58871       p->iSub++;
 58872       p->iAddr = 0;
 58875     if( pRet->p4type==P4_SUBPROGRAM ){
 58876       int nByte = (p->nSub+1)*sizeof(SubProgram*);
 58877       int j;
 58878       for(j=0; j<p->nSub; j++){
 58879         if( p->apSub[j]==pRet->p4.pProgram ) break;
 58881       if( j==p->nSub ){
 58882         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
 58883         if( !p->apSub ){
 58884           pRet = 0;
 58885         }else{
 58886           p->apSub[p->nSub++] = pRet->p4.pProgram;
 58892   return pRet;
 58895 /*
 58896 ** Check if the program stored in the VM associated with pParse may
 58897 ** throw an ABORT exception (causing the statement, but not entire transaction
 58898 ** to be rolled back). This condition is true if the main program or any
 58899 ** sub-programs contains any of the following:
 58900 **
 58901 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 58902 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 58903 **   *  OP_Destroy
 58904 **   *  OP_VUpdate
 58905 **   *  OP_VRename
 58906 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
 58907 **
 58908 ** Then check that the value of Parse.mayAbort is true if an
 58909 ** ABORT may be thrown, or false otherwise. Return true if it does
 58910 ** match, or false otherwise. This function is intended to be used as
 58911 ** part of an assert statement in the compiler. Similar to:
 58912 **
 58913 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
 58914 */
 58915 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
 58916   int hasAbort = 0;
 58917   Op *pOp;
 58918   VdbeOpIter sIter;
 58919   memset(&sIter, 0, sizeof(sIter));
 58920   sIter.v = v;
 58922   while( (pOp = opIterNext(&sIter))!=0 ){
 58923     int opcode = pOp->opcode;
 58924     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 58925 #ifndef SQLITE_OMIT_FOREIGN_KEY
 58926      || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) 
 58927 #endif
 58928      || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 58929       && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
 58930     ){
 58931       hasAbort = 1;
 58932       break;
 58935   sqlite3DbFree(v->db, sIter.apSub);
 58937   /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
 58938   ** If malloc failed, then the while() loop above may not have iterated
 58939   ** through all opcodes and hasAbort may be set incorrectly. Return
 58940   ** true for this case to prevent the assert() in the callers frame
 58941   ** from failing.  */
 58942   return ( v->db->mallocFailed || hasAbort==mayAbort );
 58944 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
 58946 /*
 58947 ** Loop through the program looking for P2 values that are negative
 58948 ** on jump instructions.  Each such value is a label.  Resolve the
 58949 ** label by setting the P2 value to its correct non-zero value.
 58950 **
 58951 ** This routine is called once after all opcodes have been inserted.
 58952 **
 58953 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
 58954 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
 58955 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
 58956 **
 58957 ** The Op.opflags field is set on all opcodes.
 58958 */
 58959 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
 58960   int i;
 58961   int nMaxArgs = *pMaxFuncArgs;
 58962   Op *pOp;
 58963   int *aLabel = p->aLabel;
 58964   p->readOnly = 1;
 58965   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
 58966     u8 opcode = pOp->opcode;
 58968     pOp->opflags = sqlite3OpcodeProperty[opcode];
 58969     if( opcode==OP_Function || opcode==OP_AggStep ){
 58970       if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
 58971     }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
 58972       p->readOnly = 0;
 58973 #ifndef SQLITE_OMIT_VIRTUALTABLE
 58974     }else if( opcode==OP_VUpdate ){
 58975       if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
 58976     }else if( opcode==OP_VFilter ){
 58977       int n;
 58978       assert( p->nOp - i >= 3 );
 58979       assert( pOp[-1].opcode==OP_Integer );
 58980       n = pOp[-1].p1;
 58981       if( n>nMaxArgs ) nMaxArgs = n;
 58982 #endif
 58983     }else if( opcode==OP_Next || opcode==OP_SorterNext ){
 58984       pOp->p4.xAdvance = sqlite3BtreeNext;
 58985       pOp->p4type = P4_ADVANCE;
 58986     }else if( opcode==OP_Prev ){
 58987       pOp->p4.xAdvance = sqlite3BtreePrevious;
 58988       pOp->p4type = P4_ADVANCE;
 58991     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
 58992       assert( -1-pOp->p2<p->nLabel );
 58993       pOp->p2 = aLabel[-1-pOp->p2];
 58996   sqlite3DbFree(p->db, p->aLabel);
 58997   p->aLabel = 0;
 58999   *pMaxFuncArgs = nMaxArgs;
 59002 /*
 59003 ** Return the address of the next instruction to be inserted.
 59004 */
 59005 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
 59006   assert( p->magic==VDBE_MAGIC_INIT );
 59007   return p->nOp;
 59010 /*
 59011 ** This function returns a pointer to the array of opcodes associated with
 59012 ** the Vdbe passed as the first argument. It is the callers responsibility
 59013 ** to arrange for the returned array to be eventually freed using the 
 59014 ** vdbeFreeOpArray() function.
 59015 **
 59016 ** Before returning, *pnOp is set to the number of entries in the returned
 59017 ** array. Also, *pnMaxArg is set to the larger of its current value and 
 59018 ** the number of entries in the Vdbe.apArg[] array required to execute the 
 59019 ** returned program.
 59020 */
 59021 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
 59022   VdbeOp *aOp = p->aOp;
 59023   assert( aOp && !p->db->mallocFailed );
 59025   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
 59026   assert( p->btreeMask==0 );
 59028   resolveP2Values(p, pnMaxArg);
 59029   *pnOp = p->nOp;
 59030   p->aOp = 0;
 59031   return aOp;
 59034 /*
 59035 ** Add a whole list of operations to the operation stack.  Return the
 59036 ** address of the first operation added.
 59037 */
 59038 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
 59039   int addr;
 59040   assert( p->magic==VDBE_MAGIC_INIT );
 59041   if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
 59042     return 0;
 59044   addr = p->nOp;
 59045   if( ALWAYS(nOp>0) ){
 59046     int i;
 59047     VdbeOpList const *pIn = aOp;
 59048     for(i=0; i<nOp; i++, pIn++){
 59049       int p2 = pIn->p2;
 59050       VdbeOp *pOut = &p->aOp[i+addr];
 59051       pOut->opcode = pIn->opcode;
 59052       pOut->p1 = pIn->p1;
 59053       if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
 59054         pOut->p2 = addr + ADDR(p2);
 59055       }else{
 59056         pOut->p2 = p2;
 59058       pOut->p3 = pIn->p3;
 59059       pOut->p4type = P4_NOTUSED;
 59060       pOut->p4.p = 0;
 59061       pOut->p5 = 0;
 59062 #ifdef SQLITE_DEBUG
 59063       pOut->zComment = 0;
 59064       if( sqlite3VdbeAddopTrace ){
 59065         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
 59067 #endif
 59069     p->nOp += nOp;
 59071   return addr;
 59074 /*
 59075 ** Change the value of the P1 operand for a specific instruction.
 59076 ** This routine is useful when a large program is loaded from a
 59077 ** static array using sqlite3VdbeAddOpList but we want to make a
 59078 ** few minor changes to the program.
 59079 */
 59080 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
 59081   assert( p!=0 );
 59082   if( ((u32)p->nOp)>addr ){
 59083     p->aOp[addr].p1 = val;
 59087 /*
 59088 ** Change the value of the P2 operand for a specific instruction.
 59089 ** This routine is useful for setting a jump destination.
 59090 */
 59091 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
 59092   assert( p!=0 );
 59093   if( ((u32)p->nOp)>addr ){
 59094     p->aOp[addr].p2 = val;
 59098 /*
 59099 ** Change the value of the P3 operand for a specific instruction.
 59100 */
 59101 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
 59102   assert( p!=0 );
 59103   if( ((u32)p->nOp)>addr ){
 59104     p->aOp[addr].p3 = val;
 59108 /*
 59109 ** Change the value of the P5 operand for the most recently
 59110 ** added operation.
 59111 */
 59112 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
 59113   assert( p!=0 );
 59114   if( p->aOp ){
 59115     assert( p->nOp>0 );
 59116     p->aOp[p->nOp-1].p5 = val;
 59120 /*
 59121 ** Change the P2 operand of instruction addr so that it points to
 59122 ** the address of the next instruction to be coded.
 59123 */
 59124 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
 59125   assert( addr>=0 || p->db->mallocFailed );
 59126   if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
 59130 /*
 59131 ** If the input FuncDef structure is ephemeral, then free it.  If
 59132 ** the FuncDef is not ephermal, then do nothing.
 59133 */
 59134 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
 59135   if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
 59136     sqlite3DbFree(db, pDef);
 59140 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
 59142 /*
 59143 ** Delete a P4 value if necessary.
 59144 */
 59145 static void freeP4(sqlite3 *db, int p4type, void *p4){
 59146   if( p4 ){
 59147     assert( db );
 59148     switch( p4type ){
 59149       case P4_REAL:
 59150       case P4_INT64:
 59151       case P4_DYNAMIC:
 59152       case P4_KEYINFO:
 59153       case P4_INTARRAY:
 59154       case P4_KEYINFO_HANDOFF: {
 59155         sqlite3DbFree(db, p4);
 59156         break;
 59158       case P4_MPRINTF: {
 59159         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
 59160         break;
 59162       case P4_VDBEFUNC: {
 59163         VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
 59164         freeEphemeralFunction(db, pVdbeFunc->pFunc);
 59165         if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
 59166         sqlite3DbFree(db, pVdbeFunc);
 59167         break;
 59169       case P4_FUNCDEF: {
 59170         freeEphemeralFunction(db, (FuncDef*)p4);
 59171         break;
 59173       case P4_MEM: {
 59174         if( db->pnBytesFreed==0 ){
 59175           sqlite3ValueFree((sqlite3_value*)p4);
 59176         }else{
 59177           Mem *p = (Mem*)p4;
 59178           sqlite3DbFree(db, p->zMalloc);
 59179           sqlite3DbFree(db, p);
 59181         break;
 59183       case P4_VTAB : {
 59184         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
 59185         break;
 59191 /*
 59192 ** Free the space allocated for aOp and any p4 values allocated for the
 59193 ** opcodes contained within. If aOp is not NULL it is assumed to contain 
 59194 ** nOp entries. 
 59195 */
 59196 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
 59197   if( aOp ){
 59198     Op *pOp;
 59199     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
 59200       freeP4(db, pOp->p4type, pOp->p4.p);
 59201 #ifdef SQLITE_DEBUG
 59202       sqlite3DbFree(db, pOp->zComment);
 59203 #endif     
 59206   sqlite3DbFree(db, aOp);
 59209 /*
 59210 ** Link the SubProgram object passed as the second argument into the linked
 59211 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
 59212 ** objects when the VM is no longer required.
 59213 */
 59214 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
 59215   p->pNext = pVdbe->pProgram;
 59216   pVdbe->pProgram = p;
 59219 /*
 59220 ** Change the opcode at addr into OP_Noop
 59221 */
 59222 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
 59223   if( p->aOp ){
 59224     VdbeOp *pOp = &p->aOp[addr];
 59225     sqlite3 *db = p->db;
 59226     freeP4(db, pOp->p4type, pOp->p4.p);
 59227     memset(pOp, 0, sizeof(pOp[0]));
 59228     pOp->opcode = OP_Noop;
 59232 /*
 59233 ** Change the value of the P4 operand for a specific instruction.
 59234 ** This routine is useful when a large program is loaded from a
 59235 ** static array using sqlite3VdbeAddOpList but we want to make a
 59236 ** few minor changes to the program.
 59237 **
 59238 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
 59239 ** the string is made into memory obtained from sqlite3_malloc().
 59240 ** A value of n==0 means copy bytes of zP4 up to and including the
 59241 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
 59242 **
 59243 ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
 59244 ** A copy is made of the KeyInfo structure into memory obtained from
 59245 ** sqlite3_malloc, to be freed when the Vdbe is finalized.
 59246 ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
 59247 ** stored in memory that the caller has obtained from sqlite3_malloc. The 
 59248 ** caller should not free the allocation, it will be freed when the Vdbe is
 59249 ** finalized.
 59250 ** 
 59251 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
 59252 ** to a string or structure that is guaranteed to exist for the lifetime of
 59253 ** the Vdbe. In these cases we can just copy the pointer.
 59254 **
 59255 ** If addr<0 then change P4 on the most recently inserted instruction.
 59256 */
 59257 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
 59258   Op *pOp;
 59259   sqlite3 *db;
 59260   assert( p!=0 );
 59261   db = p->db;
 59262   assert( p->magic==VDBE_MAGIC_INIT );
 59263   if( p->aOp==0 || db->mallocFailed ){
 59264     if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
 59265       freeP4(db, n, (void*)*(char**)&zP4);
 59267     return;
 59269   assert( p->nOp>0 );
 59270   assert( addr<p->nOp );
 59271   if( addr<0 ){
 59272     addr = p->nOp - 1;
 59274   pOp = &p->aOp[addr];
 59275   assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
 59276   freeP4(db, pOp->p4type, pOp->p4.p);
 59277   pOp->p4.p = 0;
 59278   if( n==P4_INT32 ){
 59279     /* Note: this cast is safe, because the origin data point was an int
 59280     ** that was cast to a (const char *). */
 59281     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
 59282     pOp->p4type = P4_INT32;
 59283   }else if( zP4==0 ){
 59284     pOp->p4.p = 0;
 59285     pOp->p4type = P4_NOTUSED;
 59286   }else if( n==P4_KEYINFO ){
 59287     KeyInfo *pKeyInfo;
 59288     int nField, nByte;
 59290     nField = ((KeyInfo*)zP4)->nField;
 59291     nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
 59292     pKeyInfo = sqlite3DbMallocRaw(0, nByte);
 59293     pOp->p4.pKeyInfo = pKeyInfo;
 59294     if( pKeyInfo ){
 59295       u8 *aSortOrder;
 59296       memcpy((char*)pKeyInfo, zP4, nByte - nField);
 59297       aSortOrder = pKeyInfo->aSortOrder;
 59298       assert( aSortOrder!=0 );
 59299       pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
 59300       memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
 59301       pOp->p4type = P4_KEYINFO;
 59302     }else{
 59303       p->db->mallocFailed = 1;
 59304       pOp->p4type = P4_NOTUSED;
 59306   }else if( n==P4_KEYINFO_HANDOFF ){
 59307     pOp->p4.p = (void*)zP4;
 59308     pOp->p4type = P4_KEYINFO;
 59309   }else if( n==P4_VTAB ){
 59310     pOp->p4.p = (void*)zP4;
 59311     pOp->p4type = P4_VTAB;
 59312     sqlite3VtabLock((VTable *)zP4);
 59313     assert( ((VTable *)zP4)->db==p->db );
 59314   }else if( n<0 ){
 59315     pOp->p4.p = (void*)zP4;
 59316     pOp->p4type = (signed char)n;
 59317   }else{
 59318     if( n==0 ) n = sqlite3Strlen30(zP4);
 59319     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
 59320     pOp->p4type = P4_DYNAMIC;
 59324 #ifndef NDEBUG
 59325 /*
 59326 ** Change the comment on the most recently coded instruction.  Or
 59327 ** insert a No-op and add the comment to that new instruction.  This
 59328 ** makes the code easier to read during debugging.  None of this happens
 59329 ** in a production build.
 59330 */
 59331 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
 59332   assert( p->nOp>0 || p->aOp==0 );
 59333   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
 59334   if( p->nOp ){
 59335     assert( p->aOp );
 59336     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
 59337     p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
 59340 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
 59341   va_list ap;
 59342   if( p ){
 59343     va_start(ap, zFormat);
 59344     vdbeVComment(p, zFormat, ap);
 59345     va_end(ap);
 59348 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
 59349   va_list ap;
 59350   if( p ){
 59351     sqlite3VdbeAddOp0(p, OP_Noop);
 59352     va_start(ap, zFormat);
 59353     vdbeVComment(p, zFormat, ap);
 59354     va_end(ap);
 59357 #endif  /* NDEBUG */
 59359 /*
 59360 ** Return the opcode for a given address.  If the address is -1, then
 59361 ** return the most recently inserted opcode.
 59362 **
 59363 ** If a memory allocation error has occurred prior to the calling of this
 59364 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
 59365 ** is readable but not writable, though it is cast to a writable value.
 59366 ** The return of a dummy opcode allows the call to continue functioning
 59367 ** after a OOM fault without having to check to see if the return from 
 59368 ** this routine is a valid pointer.  But because the dummy.opcode is 0,
 59369 ** dummy will never be written to.  This is verified by code inspection and
 59370 ** by running with Valgrind.
 59371 **
 59372 ** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
 59373 ** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
 59374 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
 59375 ** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
 59376 ** having to double-check to make sure that the result is non-negative. But
 59377 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
 59378 ** check the value of p->nOp-1 before continuing.
 59379 */
 59380 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
 59381   /* C89 specifies that the constant "dummy" will be initialized to all
 59382   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
 59383   static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
 59384   assert( p->magic==VDBE_MAGIC_INIT );
 59385   if( addr<0 ){
 59386 #ifdef SQLITE_OMIT_TRACE
 59387     if( p->nOp==0 ) return (VdbeOp*)&dummy;
 59388 #endif
 59389     addr = p->nOp - 1;
 59391   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
 59392   if( p->db->mallocFailed ){
 59393     return (VdbeOp*)&dummy;
 59394   }else{
 59395     return &p->aOp[addr];
 59399 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
 59400      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 59401 /*
 59402 ** Compute a string that describes the P4 parameter for an opcode.
 59403 ** Use zTemp for any required temporary buffer space.
 59404 */
 59405 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
 59406   char *zP4 = zTemp;
 59407   assert( nTemp>=20 );
 59408   switch( pOp->p4type ){
 59409     case P4_KEYINFO_STATIC:
 59410     case P4_KEYINFO: {
 59411       int i, j;
 59412       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
 59413       assert( pKeyInfo->aSortOrder!=0 );
 59414       sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
 59415       i = sqlite3Strlen30(zTemp);
 59416       for(j=0; j<pKeyInfo->nField; j++){
 59417         CollSeq *pColl = pKeyInfo->aColl[j];
 59418         const char *zColl = pColl ? pColl->zName : "nil";
 59419         int n = sqlite3Strlen30(zColl);
 59420         if( i+n>nTemp-6 ){
 59421           memcpy(&zTemp[i],",...",4);
 59422           break;
 59424         zTemp[i++] = ',';
 59425         if( pKeyInfo->aSortOrder[j] ){
 59426           zTemp[i++] = '-';
 59428         memcpy(&zTemp[i], zColl, n+1);
 59429         i += n;
 59431       zTemp[i++] = ')';
 59432       zTemp[i] = 0;
 59433       assert( i<nTemp );
 59434       break;
 59436     case P4_COLLSEQ: {
 59437       CollSeq *pColl = pOp->p4.pColl;
 59438       sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
 59439       break;
 59441     case P4_FUNCDEF: {
 59442       FuncDef *pDef = pOp->p4.pFunc;
 59443       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
 59444       break;
 59446     case P4_INT64: {
 59447       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
 59448       break;
 59450     case P4_INT32: {
 59451       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
 59452       break;
 59454     case P4_REAL: {
 59455       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
 59456       break;
 59458     case P4_MEM: {
 59459       Mem *pMem = pOp->p4.pMem;
 59460       if( pMem->flags & MEM_Str ){
 59461         zP4 = pMem->z;
 59462       }else if( pMem->flags & MEM_Int ){
 59463         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
 59464       }else if( pMem->flags & MEM_Real ){
 59465         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
 59466       }else if( pMem->flags & MEM_Null ){
 59467         sqlite3_snprintf(nTemp, zTemp, "NULL");
 59468       }else{
 59469         assert( pMem->flags & MEM_Blob );
 59470         zP4 = "(blob)";
 59472       break;
 59474 #ifndef SQLITE_OMIT_VIRTUALTABLE
 59475     case P4_VTAB: {
 59476       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
 59477       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
 59478       break;
 59480 #endif
 59481     case P4_INTARRAY: {
 59482       sqlite3_snprintf(nTemp, zTemp, "intarray");
 59483       break;
 59485     case P4_SUBPROGRAM: {
 59486       sqlite3_snprintf(nTemp, zTemp, "program");
 59487       break;
 59489     case P4_ADVANCE: {
 59490       zTemp[0] = 0;
 59491       break;
 59493     default: {
 59494       zP4 = pOp->p4.z;
 59495       if( zP4==0 ){
 59496         zP4 = zTemp;
 59497         zTemp[0] = 0;
 59501   assert( zP4!=0 );
 59502   return zP4;
 59504 #endif
 59506 /*
 59507 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
 59508 **
 59509 ** The prepared statements need to know in advance the complete set of
 59510 ** attached databases that will be use.  A mask of these databases
 59511 ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
 59512 ** p->btreeMask of databases that will require a lock.
 59513 */
 59514 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
 59515   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
 59516   assert( i<(int)sizeof(p->btreeMask)*8 );
 59517   p->btreeMask |= ((yDbMask)1)<<i;
 59518   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
 59519     p->lockMask |= ((yDbMask)1)<<i;
 59523 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 59524 /*
 59525 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
 59526 ** this routine obtains the mutex associated with each BtShared structure
 59527 ** that may be accessed by the VM passed as an argument. In doing so it also
 59528 ** sets the BtShared.db member of each of the BtShared structures, ensuring
 59529 ** that the correct busy-handler callback is invoked if required.
 59530 **
 59531 ** If SQLite is not threadsafe but does support shared-cache mode, then
 59532 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
 59533 ** of all of BtShared structures accessible via the database handle 
 59534 ** associated with the VM.
 59535 **
 59536 ** If SQLite is not threadsafe and does not support shared-cache mode, this
 59537 ** function is a no-op.
 59538 **
 59539 ** The p->btreeMask field is a bitmask of all btrees that the prepared 
 59540 ** statement p will ever use.  Let N be the number of bits in p->btreeMask
 59541 ** corresponding to btrees that use shared cache.  Then the runtime of
 59542 ** this routine is N*N.  But as N is rarely more than 1, this should not
 59543 ** be a problem.
 59544 */
 59545 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
 59546   int i;
 59547   yDbMask mask;
 59548   sqlite3 *db;
 59549   Db *aDb;
 59550   int nDb;
 59551   if( p->lockMask==0 ) return;  /* The common case */
 59552   db = p->db;
 59553   aDb = db->aDb;
 59554   nDb = db->nDb;
 59555   for(i=0, mask=1; i<nDb; i++, mask += mask){
 59556     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
 59557       sqlite3BtreeEnter(aDb[i].pBt);
 59561 #endif
 59563 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 59564 /*
 59565 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
 59566 */
 59567 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
 59568   int i;
 59569   yDbMask mask;
 59570   sqlite3 *db;
 59571   Db *aDb;
 59572   int nDb;
 59573   if( p->lockMask==0 ) return;  /* The common case */
 59574   db = p->db;
 59575   aDb = db->aDb;
 59576   nDb = db->nDb;
 59577   for(i=0, mask=1; i<nDb; i++, mask += mask){
 59578     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
 59579       sqlite3BtreeLeave(aDb[i].pBt);
 59583 #endif
 59585 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 59586 /*
 59587 ** Print a single opcode.  This routine is used for debugging only.
 59588 */
 59589 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
 59590   char *zP4;
 59591   char zPtr[50];
 59592   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
 59593   if( pOut==0 ) pOut = stdout;
 59594   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
 59595   fprintf(pOut, zFormat1, pc, 
 59596       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
 59597 #ifdef SQLITE_DEBUG
 59598       pOp->zComment ? pOp->zComment : ""
 59599 #else
 59600       ""
 59601 #endif
 59602   );
 59603   fflush(pOut);
 59605 #endif
 59607 /*
 59608 ** Release an array of N Mem elements
 59609 */
 59610 static void releaseMemArray(Mem *p, int N){
 59611   if( p && N ){
 59612     Mem *pEnd;
 59613     sqlite3 *db = p->db;
 59614     u8 malloc_failed = db->mallocFailed;
 59615     if( db->pnBytesFreed ){
 59616       for(pEnd=&p[N]; p<pEnd; p++){
 59617         sqlite3DbFree(db, p->zMalloc);
 59619       return;
 59621     for(pEnd=&p[N]; p<pEnd; p++){
 59622       assert( (&p[1])==pEnd || p[0].db==p[1].db );
 59624       /* This block is really an inlined version of sqlite3VdbeMemRelease()
 59625       ** that takes advantage of the fact that the memory cell value is 
 59626       ** being set to NULL after releasing any dynamic resources.
 59627       **
 59628       ** The justification for duplicating code is that according to 
 59629       ** callgrind, this causes a certain test case to hit the CPU 4.7 
 59630       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 
 59631       ** sqlite3MemRelease() were called from here. With -O2, this jumps
 59632       ** to 6.6 percent. The test case is inserting 1000 rows into a table 
 59633       ** with no indexes using a single prepared INSERT statement, bind() 
 59634       ** and reset(). Inserts are grouped into a transaction.
 59635       */
 59636       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
 59637         sqlite3VdbeMemRelease(p);
 59638       }else if( p->zMalloc ){
 59639         sqlite3DbFree(db, p->zMalloc);
 59640         p->zMalloc = 0;
 59643       p->flags = MEM_Invalid;
 59645     db->mallocFailed = malloc_failed;
 59649 /*
 59650 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
 59651 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
 59652 */
 59653 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
 59654   int i;
 59655   Mem *aMem = VdbeFrameMem(p);
 59656   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
 59657   for(i=0; i<p->nChildCsr; i++){
 59658     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
 59660   releaseMemArray(aMem, p->nChildMem);
 59661   sqlite3DbFree(p->v->db, p);
 59664 #ifndef SQLITE_OMIT_EXPLAIN
 59665 /*
 59666 ** Give a listing of the program in the virtual machine.
 59667 **
 59668 ** The interface is the same as sqlite3VdbeExec().  But instead of
 59669 ** running the code, it invokes the callback once for each instruction.
 59670 ** This feature is used to implement "EXPLAIN".
 59671 **
 59672 ** When p->explain==1, each instruction is listed.  When
 59673 ** p->explain==2, only OP_Explain instructions are listed and these
 59674 ** are shown in a different format.  p->explain==2 is used to implement
 59675 ** EXPLAIN QUERY PLAN.
 59676 **
 59677 ** When p->explain==1, first the main program is listed, then each of
 59678 ** the trigger subprograms are listed one by one.
 59679 */
 59680 SQLITE_PRIVATE int sqlite3VdbeList(
 59681   Vdbe *p                   /* The VDBE */
 59682 ){
 59683   int nRow;                            /* Stop when row count reaches this */
 59684   int nSub = 0;                        /* Number of sub-vdbes seen so far */
 59685   SubProgram **apSub = 0;              /* Array of sub-vdbes */
 59686   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
 59687   sqlite3 *db = p->db;                 /* The database connection */
 59688   int i;                               /* Loop counter */
 59689   int rc = SQLITE_OK;                  /* Return code */
 59690   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
 59692   assert( p->explain );
 59693   assert( p->magic==VDBE_MAGIC_RUN );
 59694   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
 59696   /* Even though this opcode does not use dynamic strings for
 59697   ** the result, result columns may become dynamic if the user calls
 59698   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
 59699   */
 59700   releaseMemArray(pMem, 8);
 59701   p->pResultSet = 0;
 59703   if( p->rc==SQLITE_NOMEM ){
 59704     /* This happens if a malloc() inside a call to sqlite3_column_text() or
 59705     ** sqlite3_column_text16() failed.  */
 59706     db->mallocFailed = 1;
 59707     return SQLITE_ERROR;
 59710   /* When the number of output rows reaches nRow, that means the
 59711   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
 59712   ** nRow is the sum of the number of rows in the main program, plus
 59713   ** the sum of the number of rows in all trigger subprograms encountered
 59714   ** so far.  The nRow value will increase as new trigger subprograms are
 59715   ** encountered, but p->pc will eventually catch up to nRow.
 59716   */
 59717   nRow = p->nOp;
 59718   if( p->explain==1 ){
 59719     /* The first 8 memory cells are used for the result set.  So we will
 59720     ** commandeer the 9th cell to use as storage for an array of pointers
 59721     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
 59722     ** cells.  */
 59723     assert( p->nMem>9 );
 59724     pSub = &p->aMem[9];
 59725     if( pSub->flags&MEM_Blob ){
 59726       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
 59727       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
 59728       nSub = pSub->n/sizeof(Vdbe*);
 59729       apSub = (SubProgram **)pSub->z;
 59731     for(i=0; i<nSub; i++){
 59732       nRow += apSub[i]->nOp;
 59736   do{
 59737     i = p->pc++;
 59738   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
 59739   if( i>=nRow ){
 59740     p->rc = SQLITE_OK;
 59741     rc = SQLITE_DONE;
 59742   }else if( db->u1.isInterrupted ){
 59743     p->rc = SQLITE_INTERRUPT;
 59744     rc = SQLITE_ERROR;
 59745     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
 59746   }else{
 59747     char *z;
 59748     Op *pOp;
 59749     if( i<p->nOp ){
 59750       /* The output line number is small enough that we are still in the
 59751       ** main program. */
 59752       pOp = &p->aOp[i];
 59753     }else{
 59754       /* We are currently listing subprograms.  Figure out which one and
 59755       ** pick up the appropriate opcode. */
 59756       int j;
 59757       i -= p->nOp;
 59758       for(j=0; i>=apSub[j]->nOp; j++){
 59759         i -= apSub[j]->nOp;
 59761       pOp = &apSub[j]->aOp[i];
 59763     if( p->explain==1 ){
 59764       pMem->flags = MEM_Int;
 59765       pMem->type = SQLITE_INTEGER;
 59766       pMem->u.i = i;                                /* Program counter */
 59767       pMem++;
 59769       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
 59770       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
 59771       assert( pMem->z!=0 );
 59772       pMem->n = sqlite3Strlen30(pMem->z);
 59773       pMem->type = SQLITE_TEXT;
 59774       pMem->enc = SQLITE_UTF8;
 59775       pMem++;
 59777       /* When an OP_Program opcode is encounter (the only opcode that has
 59778       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
 59779       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
 59780       ** has not already been seen.
 59781       */
 59782       if( pOp->p4type==P4_SUBPROGRAM ){
 59783         int nByte = (nSub+1)*sizeof(SubProgram*);
 59784         int j;
 59785         for(j=0; j<nSub; j++){
 59786           if( apSub[j]==pOp->p4.pProgram ) break;
 59788         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
 59789           apSub = (SubProgram **)pSub->z;
 59790           apSub[nSub++] = pOp->p4.pProgram;
 59791           pSub->flags |= MEM_Blob;
 59792           pSub->n = nSub*sizeof(SubProgram*);
 59797     pMem->flags = MEM_Int;
 59798     pMem->u.i = pOp->p1;                          /* P1 */
 59799     pMem->type = SQLITE_INTEGER;
 59800     pMem++;
 59802     pMem->flags = MEM_Int;
 59803     pMem->u.i = pOp->p2;                          /* P2 */
 59804     pMem->type = SQLITE_INTEGER;
 59805     pMem++;
 59807     pMem->flags = MEM_Int;
 59808     pMem->u.i = pOp->p3;                          /* P3 */
 59809     pMem->type = SQLITE_INTEGER;
 59810     pMem++;
 59812     if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
 59813       assert( p->db->mallocFailed );
 59814       return SQLITE_ERROR;
 59816     pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
 59817     z = displayP4(pOp, pMem->z, 32);
 59818     if( z!=pMem->z ){
 59819       sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
 59820     }else{
 59821       assert( pMem->z!=0 );
 59822       pMem->n = sqlite3Strlen30(pMem->z);
 59823       pMem->enc = SQLITE_UTF8;
 59825     pMem->type = SQLITE_TEXT;
 59826     pMem++;
 59828     if( p->explain==1 ){
 59829       if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
 59830         assert( p->db->mallocFailed );
 59831         return SQLITE_ERROR;
 59833       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
 59834       pMem->n = 2;
 59835       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
 59836       pMem->type = SQLITE_TEXT;
 59837       pMem->enc = SQLITE_UTF8;
 59838       pMem++;
 59840 #ifdef SQLITE_DEBUG
 59841       if( pOp->zComment ){
 59842         pMem->flags = MEM_Str|MEM_Term;
 59843         pMem->z = pOp->zComment;
 59844         pMem->n = sqlite3Strlen30(pMem->z);
 59845         pMem->enc = SQLITE_UTF8;
 59846         pMem->type = SQLITE_TEXT;
 59847       }else
 59848 #endif
 59850         pMem->flags = MEM_Null;                       /* Comment */
 59851         pMem->type = SQLITE_NULL;
 59855     p->nResColumn = 8 - 4*(p->explain-1);
 59856     p->pResultSet = &p->aMem[1];
 59857     p->rc = SQLITE_OK;
 59858     rc = SQLITE_ROW;
 59860   return rc;
 59862 #endif /* SQLITE_OMIT_EXPLAIN */
 59864 #ifdef SQLITE_DEBUG
 59865 /*
 59866 ** Print the SQL that was used to generate a VDBE program.
 59867 */
 59868 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
 59869   int nOp = p->nOp;
 59870   VdbeOp *pOp;
 59871   if( nOp<1 ) return;
 59872   pOp = &p->aOp[0];
 59873   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
 59874     const char *z = pOp->p4.z;
 59875     while( sqlite3Isspace(*z) ) z++;
 59876     printf("SQL: [%s]\n", z);
 59879 #endif
 59881 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
 59882 /*
 59883 ** Print an IOTRACE message showing SQL content.
 59884 */
 59885 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
 59886   int nOp = p->nOp;
 59887   VdbeOp *pOp;
 59888   if( sqlite3IoTrace==0 ) return;
 59889   if( nOp<1 ) return;
 59890   pOp = &p->aOp[0];
 59891   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
 59892     int i, j;
 59893     char z[1000];
 59894     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
 59895     for(i=0; sqlite3Isspace(z[i]); i++){}
 59896     for(j=0; z[i]; i++){
 59897       if( sqlite3Isspace(z[i]) ){
 59898         if( z[i-1]!=' ' ){
 59899           z[j++] = ' ';
 59901       }else{
 59902         z[j++] = z[i];
 59905     z[j] = 0;
 59906     sqlite3IoTrace("SQL %s\n", z);
 59909 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
 59911 /*
 59912 ** Allocate space from a fixed size buffer and return a pointer to
 59913 ** that space.  If insufficient space is available, return NULL.
 59914 **
 59915 ** The pBuf parameter is the initial value of a pointer which will
 59916 ** receive the new memory.  pBuf is normally NULL.  If pBuf is not
 59917 ** NULL, it means that memory space has already been allocated and that
 59918 ** this routine should not allocate any new memory.  When pBuf is not
 59919 ** NULL simply return pBuf.  Only allocate new memory space when pBuf
 59920 ** is NULL.
 59921 **
 59922 ** nByte is the number of bytes of space needed.
 59923 **
 59924 ** *ppFrom points to available space and pEnd points to the end of the
 59925 ** available space.  When space is allocated, *ppFrom is advanced past
 59926 ** the end of the allocated space.
 59927 **
 59928 ** *pnByte is a counter of the number of bytes of space that have failed
 59929 ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
 59930 ** request, then increment *pnByte by the amount of the request.
 59931 */
 59932 static void *allocSpace(
 59933   void *pBuf,          /* Where return pointer will be stored */
 59934   int nByte,           /* Number of bytes to allocate */
 59935   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
 59936   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
 59937   int *pnByte          /* If allocation cannot be made, increment *pnByte */
 59938 ){
 59939   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
 59940   if( pBuf ) return pBuf;
 59941   nByte = ROUND8(nByte);
 59942   if( &(*ppFrom)[nByte] <= pEnd ){
 59943     pBuf = (void*)*ppFrom;
 59944     *ppFrom += nByte;
 59945   }else{
 59946     *pnByte += nByte;
 59948   return pBuf;
 59951 /*
 59952 ** Rewind the VDBE back to the beginning in preparation for
 59953 ** running it.
 59954 */
 59955 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
 59956 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 59957   int i;
 59958 #endif
 59959   assert( p!=0 );
 59960   assert( p->magic==VDBE_MAGIC_INIT );
 59962   /* There should be at least one opcode.
 59963   */
 59964   assert( p->nOp>0 );
 59966   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
 59967   p->magic = VDBE_MAGIC_RUN;
 59969 #ifdef SQLITE_DEBUG
 59970   for(i=1; i<p->nMem; i++){
 59971     assert( p->aMem[i].db==p->db );
 59973 #endif
 59974   p->pc = -1;
 59975   p->rc = SQLITE_OK;
 59976   p->errorAction = OE_Abort;
 59977   p->magic = VDBE_MAGIC_RUN;
 59978   p->nChange = 0;
 59979   p->cacheCtr = 1;
 59980   p->minWriteFileFormat = 255;
 59981   p->iStatement = 0;
 59982   p->nFkConstraint = 0;
 59983 #ifdef VDBE_PROFILE
 59984   for(i=0; i<p->nOp; i++){
 59985     p->aOp[i].cnt = 0;
 59986     p->aOp[i].cycles = 0;
 59988 #endif
 59991 /*
 59992 ** Prepare a virtual machine for execution for the first time after
 59993 ** creating the virtual machine.  This involves things such
 59994 ** as allocating stack space and initializing the program counter.
 59995 ** After the VDBE has be prepped, it can be executed by one or more
 59996 ** calls to sqlite3VdbeExec().  
 59997 **
 59998 ** This function may be called exact once on a each virtual machine.
 59999 ** After this routine is called the VM has been "packaged" and is ready
 60000 ** to run.  After this routine is called, futher calls to 
 60001 ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
 60002 ** the Vdbe from the Parse object that helped generate it so that the
 60003 ** the Vdbe becomes an independent entity and the Parse object can be
 60004 ** destroyed.
 60005 **
 60006 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
 60007 ** to its initial state after it has been run.
 60008 */
 60009 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
 60010   Vdbe *p,                       /* The VDBE */
 60011   Parse *pParse                  /* Parsing context */
 60012 ){
 60013   sqlite3 *db;                   /* The database connection */
 60014   int nVar;                      /* Number of parameters */
 60015   int nMem;                      /* Number of VM memory registers */
 60016   int nCursor;                   /* Number of cursors required */
 60017   int nArg;                      /* Number of arguments in subprograms */
 60018   int nOnce;                     /* Number of OP_Once instructions */
 60019   int n;                         /* Loop counter */
 60020   u8 *zCsr;                      /* Memory available for allocation */
 60021   u8 *zEnd;                      /* First byte past allocated memory */
 60022   int nByte;                     /* How much extra memory is needed */
 60024   assert( p!=0 );
 60025   assert( p->nOp>0 );
 60026   assert( pParse!=0 );
 60027   assert( p->magic==VDBE_MAGIC_INIT );
 60028   db = p->db;
 60029   assert( db->mallocFailed==0 );
 60030   nVar = pParse->nVar;
 60031   nMem = pParse->nMem;
 60032   nCursor = pParse->nTab;
 60033   nArg = pParse->nMaxArg;
 60034   nOnce = pParse->nOnce;
 60035   if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
 60037   /* For each cursor required, also allocate a memory cell. Memory
 60038   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
 60039   ** the vdbe program. Instead they are used to allocate space for
 60040   ** VdbeCursor/BtCursor structures. The blob of memory associated with 
 60041   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
 60042   ** stores the blob of memory associated with cursor 1, etc.
 60043   **
 60044   ** See also: allocateCursor().
 60045   */
 60046   nMem += nCursor;
 60048   /* Allocate space for memory registers, SQL variables, VDBE cursors and 
 60049   ** an array to marshal SQL function arguments in.
 60050   */
 60051   zCsr = (u8*)&p->aOp[p->nOp];       /* Memory avaliable for allocation */
 60052   zEnd = (u8*)&p->aOp[p->nOpAlloc];  /* First byte past end of zCsr[] */
 60054   resolveP2Values(p, &nArg);
 60055   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
 60056   if( pParse->explain && nMem<10 ){
 60057     nMem = 10;
 60059   memset(zCsr, 0, zEnd-zCsr);
 60060   zCsr += (zCsr - (u8*)0)&7;
 60061   assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
 60062   p->expired = 0;
 60064   /* Memory for registers, parameters, cursor, etc, is allocated in two
 60065   ** passes.  On the first pass, we try to reuse unused space at the 
 60066   ** end of the opcode array.  If we are unable to satisfy all memory
 60067   ** requirements by reusing the opcode array tail, then the second
 60068   ** pass will fill in the rest using a fresh allocation.  
 60069   **
 60070   ** This two-pass approach that reuses as much memory as possible from
 60071   ** the leftover space at the end of the opcode array can significantly
 60072   ** reduce the amount of memory held by a prepared statement.
 60073   */
 60074   do {
 60075     nByte = 0;
 60076     p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
 60077     p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
 60078     p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
 60079     p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
 60080     p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
 60081                           &zCsr, zEnd, &nByte);
 60082     p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
 60083     if( nByte ){
 60084       p->pFree = sqlite3DbMallocZero(db, nByte);
 60086     zCsr = p->pFree;
 60087     zEnd = &zCsr[nByte];
 60088   }while( nByte && !db->mallocFailed );
 60090   p->nCursor = (u16)nCursor;
 60091   p->nOnceFlag = nOnce;
 60092   if( p->aVar ){
 60093     p->nVar = (ynVar)nVar;
 60094     for(n=0; n<nVar; n++){
 60095       p->aVar[n].flags = MEM_Null;
 60096       p->aVar[n].db = db;
 60099   if( p->azVar ){
 60100     p->nzVar = pParse->nzVar;
 60101     memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
 60102     memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
 60104   if( p->aMem ){
 60105     p->aMem--;                      /* aMem[] goes from 1..nMem */
 60106     p->nMem = nMem;                 /*       not from 0..nMem-1 */
 60107     for(n=1; n<=nMem; n++){
 60108       p->aMem[n].flags = MEM_Invalid;
 60109       p->aMem[n].db = db;
 60112   p->explain = pParse->explain;
 60113   sqlite3VdbeRewind(p);
 60116 /*
 60117 ** Close a VDBE cursor and release all the resources that cursor 
 60118 ** happens to hold.
 60119 */
 60120 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
 60121   if( pCx==0 ){
 60122     return;
 60124   sqlite3VdbeSorterClose(p->db, pCx);
 60125   if( pCx->pBt ){
 60126     sqlite3BtreeClose(pCx->pBt);
 60127     /* The pCx->pCursor will be close automatically, if it exists, by
 60128     ** the call above. */
 60129   }else if( pCx->pCursor ){
 60130     sqlite3BtreeCloseCursor(pCx->pCursor);
 60132 #ifndef SQLITE_OMIT_VIRTUALTABLE
 60133   if( pCx->pVtabCursor ){
 60134     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
 60135     const sqlite3_module *pModule = pCx->pModule;
 60136     p->inVtabMethod = 1;
 60137     pModule->xClose(pVtabCursor);
 60138     p->inVtabMethod = 0;
 60140 #endif
 60143 /*
 60144 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
 60145 ** is used, for example, when a trigger sub-program is halted to restore
 60146 ** control to the main program.
 60147 */
 60148 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
 60149   Vdbe *v = pFrame->v;
 60150   v->aOnceFlag = pFrame->aOnceFlag;
 60151   v->nOnceFlag = pFrame->nOnceFlag;
 60152   v->aOp = pFrame->aOp;
 60153   v->nOp = pFrame->nOp;
 60154   v->aMem = pFrame->aMem;
 60155   v->nMem = pFrame->nMem;
 60156   v->apCsr = pFrame->apCsr;
 60157   v->nCursor = pFrame->nCursor;
 60158   v->db->lastRowid = pFrame->lastRowid;
 60159   v->nChange = pFrame->nChange;
 60160   return pFrame->pc;
 60163 /*
 60164 ** Close all cursors.
 60165 **
 60166 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
 60167 ** cell array. This is necessary as the memory cell array may contain
 60168 ** pointers to VdbeFrame objects, which may in turn contain pointers to
 60169 ** open cursors.
 60170 */
 60171 static void closeAllCursors(Vdbe *p){
 60172   if( p->pFrame ){
 60173     VdbeFrame *pFrame;
 60174     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 60175     sqlite3VdbeFrameRestore(pFrame);
 60177   p->pFrame = 0;
 60178   p->nFrame = 0;
 60180   if( p->apCsr ){
 60181     int i;
 60182     for(i=0; i<p->nCursor; i++){
 60183       VdbeCursor *pC = p->apCsr[i];
 60184       if( pC ){
 60185         sqlite3VdbeFreeCursor(p, pC);
 60186         p->apCsr[i] = 0;
 60190   if( p->aMem ){
 60191     releaseMemArray(&p->aMem[1], p->nMem);
 60193   while( p->pDelFrame ){
 60194     VdbeFrame *pDel = p->pDelFrame;
 60195     p->pDelFrame = pDel->pParent;
 60196     sqlite3VdbeFrameDelete(pDel);
 60200 /*
 60201 ** Clean up the VM after execution.
 60202 **
 60203 ** This routine will automatically close any cursors, lists, and/or
 60204 ** sorters that were left open.  It also deletes the values of
 60205 ** variables in the aVar[] array.
 60206 */
 60207 static void Cleanup(Vdbe *p){
 60208   sqlite3 *db = p->db;
 60210 #ifdef SQLITE_DEBUG
 60211   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
 60212   ** Vdbe.aMem[] arrays have already been cleaned up.  */
 60213   int i;
 60214   if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
 60215   if( p->aMem ){
 60216     for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
 60218 #endif
 60220   sqlite3DbFree(db, p->zErrMsg);
 60221   p->zErrMsg = 0;
 60222   p->pResultSet = 0;
 60225 /*
 60226 ** Set the number of result columns that will be returned by this SQL
 60227 ** statement. This is now set at compile time, rather than during
 60228 ** execution of the vdbe program so that sqlite3_column_count() can
 60229 ** be called on an SQL statement before sqlite3_step().
 60230 */
 60231 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
 60232   Mem *pColName;
 60233   int n;
 60234   sqlite3 *db = p->db;
 60236   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 60237   sqlite3DbFree(db, p->aColName);
 60238   n = nResColumn*COLNAME_N;
 60239   p->nResColumn = (u16)nResColumn;
 60240   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
 60241   if( p->aColName==0 ) return;
 60242   while( n-- > 0 ){
 60243     pColName->flags = MEM_Null;
 60244     pColName->db = p->db;
 60245     pColName++;
 60249 /*
 60250 ** Set the name of the idx'th column to be returned by the SQL statement.
 60251 ** zName must be a pointer to a nul terminated string.
 60252 **
 60253 ** This call must be made after a call to sqlite3VdbeSetNumCols().
 60254 **
 60255 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
 60256 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
 60257 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
 60258 */
 60259 SQLITE_PRIVATE int sqlite3VdbeSetColName(
 60260   Vdbe *p,                         /* Vdbe being configured */
 60261   int idx,                         /* Index of column zName applies to */
 60262   int var,                         /* One of the COLNAME_* constants */
 60263   const char *zName,               /* Pointer to buffer containing name */
 60264   void (*xDel)(void*)              /* Memory management strategy for zName */
 60265 ){
 60266   int rc;
 60267   Mem *pColName;
 60268   assert( idx<p->nResColumn );
 60269   assert( var<COLNAME_N );
 60270   if( p->db->mallocFailed ){
 60271     assert( !zName || xDel!=SQLITE_DYNAMIC );
 60272     return SQLITE_NOMEM;
 60274   assert( p->aColName!=0 );
 60275   pColName = &(p->aColName[idx+var*p->nResColumn]);
 60276   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
 60277   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
 60278   return rc;
 60281 /*
 60282 ** A read or write transaction may or may not be active on database handle
 60283 ** db. If a transaction is active, commit it. If there is a
 60284 ** write-transaction spanning more than one database file, this routine
 60285 ** takes care of the master journal trickery.
 60286 */
 60287 static int vdbeCommit(sqlite3 *db, Vdbe *p){
 60288   int i;
 60289   int nTrans = 0;  /* Number of databases with an active write-transaction */
 60290   int rc = SQLITE_OK;
 60291   int needXcommit = 0;
 60293 #ifdef SQLITE_OMIT_VIRTUALTABLE
 60294   /* With this option, sqlite3VtabSync() is defined to be simply 
 60295   ** SQLITE_OK so p is not used. 
 60296   */
 60297   UNUSED_PARAMETER(p);
 60298 #endif
 60300   /* Before doing anything else, call the xSync() callback for any
 60301   ** virtual module tables written in this transaction. This has to
 60302   ** be done before determining whether a master journal file is 
 60303   ** required, as an xSync() callback may add an attached database
 60304   ** to the transaction.
 60305   */
 60306   rc = sqlite3VtabSync(db, &p->zErrMsg);
 60308   /* This loop determines (a) if the commit hook should be invoked and
 60309   ** (b) how many database files have open write transactions, not 
 60310   ** including the temp database. (b) is important because if more than 
 60311   ** one database file has an open write transaction, a master journal
 60312   ** file is required for an atomic commit.
 60313   */ 
 60314   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
 60315     Btree *pBt = db->aDb[i].pBt;
 60316     if( sqlite3BtreeIsInTrans(pBt) ){
 60317       needXcommit = 1;
 60318       if( i!=1 ) nTrans++;
 60319       sqlite3BtreeEnter(pBt);
 60320       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
 60321       sqlite3BtreeLeave(pBt);
 60324   if( rc!=SQLITE_OK ){
 60325     return rc;
 60328   /* If there are any write-transactions at all, invoke the commit hook */
 60329   if( needXcommit && db->xCommitCallback ){
 60330     rc = db->xCommitCallback(db->pCommitArg);
 60331     if( rc ){
 60332       return SQLITE_CONSTRAINT;
 60336   /* The simple case - no more than one database file (not counting the
 60337   ** TEMP database) has a transaction active.   There is no need for the
 60338   ** master-journal.
 60339   **
 60340   ** If the return value of sqlite3BtreeGetFilename() is a zero length
 60341   ** string, it means the main database is :memory: or a temp file.  In 
 60342   ** that case we do not support atomic multi-file commits, so use the 
 60343   ** simple case then too.
 60344   */
 60345   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
 60346    || nTrans<=1
 60347   ){
 60348     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 60349       Btree *pBt = db->aDb[i].pBt;
 60350       if( pBt ){
 60351         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
 60355     /* Do the commit only if all databases successfully complete phase 1. 
 60356     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
 60357     ** IO error while deleting or truncating a journal file. It is unlikely,
 60358     ** but could happen. In this case abandon processing and return the error.
 60359     */
 60360     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 60361       Btree *pBt = db->aDb[i].pBt;
 60362       if( pBt ){
 60363         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
 60366     if( rc==SQLITE_OK ){
 60367       sqlite3VtabCommit(db);
 60371   /* The complex case - There is a multi-file write-transaction active.
 60372   ** This requires a master journal file to ensure the transaction is
 60373   ** committed atomicly.
 60374   */
 60375 #ifndef SQLITE_OMIT_DISKIO
 60376   else{
 60377     sqlite3_vfs *pVfs = db->pVfs;
 60378     int needSync = 0;
 60379     char *zMaster = 0;   /* File-name for the master journal */
 60380     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
 60381     sqlite3_file *pMaster = 0;
 60382     i64 offset = 0;
 60383     int res;
 60384     int retryCount = 0;
 60385     int nMainFile;
 60387     /* Select a master journal file name */
 60388     nMainFile = sqlite3Strlen30(zMainFile);
 60389     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
 60390     if( zMaster==0 ) return SQLITE_NOMEM;
 60391     do {
 60392       u32 iRandom;
 60393       if( retryCount ){
 60394         if( retryCount>100 ){
 60395           sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
 60396           sqlite3OsDelete(pVfs, zMaster, 0);
 60397           break;
 60398         }else if( retryCount==1 ){
 60399           sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
 60402       retryCount++;
 60403       sqlite3_randomness(sizeof(iRandom), &iRandom);
 60404       sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
 60405                                (iRandom>>8)&0xffffff, iRandom&0xff);
 60406       /* The antipenultimate character of the master journal name must
 60407       ** be "9" to avoid name collisions when using 8+3 filenames. */
 60408       assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
 60409       sqlite3FileSuffix3(zMainFile, zMaster);
 60410       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
 60411     }while( rc==SQLITE_OK && res );
 60412     if( rc==SQLITE_OK ){
 60413       /* Open the master journal. */
 60414       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
 60415           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
 60416           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
 60417       );
 60419     if( rc!=SQLITE_OK ){
 60420       sqlite3DbFree(db, zMaster);
 60421       return rc;
 60424     /* Write the name of each database file in the transaction into the new
 60425     ** master journal file. If an error occurs at this point close
 60426     ** and delete the master journal file. All the individual journal files
 60427     ** still have 'null' as the master journal pointer, so they will roll
 60428     ** back independently if a failure occurs.
 60429     */
 60430     for(i=0; i<db->nDb; i++){
 60431       Btree *pBt = db->aDb[i].pBt;
 60432       if( sqlite3BtreeIsInTrans(pBt) ){
 60433         char const *zFile = sqlite3BtreeGetJournalname(pBt);
 60434         if( zFile==0 ){
 60435           continue;  /* Ignore TEMP and :memory: databases */
 60437         assert( zFile[0]!=0 );
 60438         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
 60439           needSync = 1;
 60441         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
 60442         offset += sqlite3Strlen30(zFile)+1;
 60443         if( rc!=SQLITE_OK ){
 60444           sqlite3OsCloseFree(pMaster);
 60445           sqlite3OsDelete(pVfs, zMaster, 0);
 60446           sqlite3DbFree(db, zMaster);
 60447           return rc;
 60452     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
 60453     ** flag is set this is not required.
 60454     */
 60455     if( needSync 
 60456      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
 60457      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
 60458     ){
 60459       sqlite3OsCloseFree(pMaster);
 60460       sqlite3OsDelete(pVfs, zMaster, 0);
 60461       sqlite3DbFree(db, zMaster);
 60462       return rc;
 60465     /* Sync all the db files involved in the transaction. The same call
 60466     ** sets the master journal pointer in each individual journal. If
 60467     ** an error occurs here, do not delete the master journal file.
 60468     **
 60469     ** If the error occurs during the first call to
 60470     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
 60471     ** master journal file will be orphaned. But we cannot delete it,
 60472     ** in case the master journal file name was written into the journal
 60473     ** file before the failure occurred.
 60474     */
 60475     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
 60476       Btree *pBt = db->aDb[i].pBt;
 60477       if( pBt ){
 60478         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
 60481     sqlite3OsCloseFree(pMaster);
 60482     assert( rc!=SQLITE_BUSY );
 60483     if( rc!=SQLITE_OK ){
 60484       sqlite3DbFree(db, zMaster);
 60485       return rc;
 60488     /* Delete the master journal file. This commits the transaction. After
 60489     ** doing this the directory is synced again before any individual
 60490     ** transaction files are deleted.
 60491     */
 60492     rc = sqlite3OsDelete(pVfs, zMaster, 1);
 60493     sqlite3DbFree(db, zMaster);
 60494     zMaster = 0;
 60495     if( rc ){
 60496       return rc;
 60499     /* All files and directories have already been synced, so the following
 60500     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
 60501     ** deleting or truncating journals. If something goes wrong while
 60502     ** this is happening we don't really care. The integrity of the
 60503     ** transaction is already guaranteed, but some stray 'cold' journals
 60504     ** may be lying around. Returning an error code won't help matters.
 60505     */
 60506     disable_simulated_io_errors();
 60507     sqlite3BeginBenignMalloc();
 60508     for(i=0; i<db->nDb; i++){ 
 60509       Btree *pBt = db->aDb[i].pBt;
 60510       if( pBt ){
 60511         sqlite3BtreeCommitPhaseTwo(pBt, 1);
 60514     sqlite3EndBenignMalloc();
 60515     enable_simulated_io_errors();
 60517     sqlite3VtabCommit(db);
 60519 #endif
 60521   return rc;
 60524 /* 
 60525 ** This routine checks that the sqlite3.activeVdbeCnt count variable
 60526 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
 60527 ** currently active. An assertion fails if the two counts do not match.
 60528 ** This is an internal self-check only - it is not an essential processing
 60529 ** step.
 60530 **
 60531 ** This is a no-op if NDEBUG is defined.
 60532 */
 60533 #ifndef NDEBUG
 60534 static void checkActiveVdbeCnt(sqlite3 *db){
 60535   Vdbe *p;
 60536   int cnt = 0;
 60537   int nWrite = 0;
 60538   p = db->pVdbe;
 60539   while( p ){
 60540     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
 60541       cnt++;
 60542       if( p->readOnly==0 ) nWrite++;
 60544     p = p->pNext;
 60546   assert( cnt==db->activeVdbeCnt );
 60547   assert( nWrite==db->writeVdbeCnt );
 60549 #else
 60550 #define checkActiveVdbeCnt(x)
 60551 #endif
 60553 /*
 60554 ** If the Vdbe passed as the first argument opened a statement-transaction,
 60555 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
 60556 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
 60557 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
 60558 ** statement transaction is commtted.
 60559 **
 60560 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
 60561 ** Otherwise SQLITE_OK.
 60562 */
 60563 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
 60564   sqlite3 *const db = p->db;
 60565   int rc = SQLITE_OK;
 60567   /* If p->iStatement is greater than zero, then this Vdbe opened a 
 60568   ** statement transaction that should be closed here. The only exception
 60569   ** is that an IO error may have occured, causing an emergency rollback.
 60570   ** In this case (db->nStatement==0), and there is nothing to do.
 60571   */
 60572   if( db->nStatement && p->iStatement ){
 60573     int i;
 60574     const int iSavepoint = p->iStatement-1;
 60576     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
 60577     assert( db->nStatement>0 );
 60578     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
 60580     for(i=0; i<db->nDb; i++){ 
 60581       int rc2 = SQLITE_OK;
 60582       Btree *pBt = db->aDb[i].pBt;
 60583       if( pBt ){
 60584         if( eOp==SAVEPOINT_ROLLBACK ){
 60585           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
 60587         if( rc2==SQLITE_OK ){
 60588           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
 60590         if( rc==SQLITE_OK ){
 60591           rc = rc2;
 60595     db->nStatement--;
 60596     p->iStatement = 0;
 60598     if( rc==SQLITE_OK ){
 60599       if( eOp==SAVEPOINT_ROLLBACK ){
 60600         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
 60602       if( rc==SQLITE_OK ){
 60603         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
 60607     /* If the statement transaction is being rolled back, also restore the 
 60608     ** database handles deferred constraint counter to the value it had when 
 60609     ** the statement transaction was opened.  */
 60610     if( eOp==SAVEPOINT_ROLLBACK ){
 60611       db->nDeferredCons = p->nStmtDefCons;
 60614   return rc;
 60617 /*
 60618 ** This function is called when a transaction opened by the database 
 60619 ** handle associated with the VM passed as an argument is about to be 
 60620 ** committed. If there are outstanding deferred foreign key constraint
 60621 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
 60622 **
 60623 ** If there are outstanding FK violations and this function returns 
 60624 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
 60625 ** an error message to it. Then return SQLITE_ERROR.
 60626 */
 60627 #ifndef SQLITE_OMIT_FOREIGN_KEY
 60628 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
 60629   sqlite3 *db = p->db;
 60630   if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
 60631     p->rc = SQLITE_CONSTRAINT;
 60632     p->errorAction = OE_Abort;
 60633     sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
 60634     return SQLITE_ERROR;
 60636   return SQLITE_OK;
 60638 #endif
 60640 /*
 60641 ** This routine is called the when a VDBE tries to halt.  If the VDBE
 60642 ** has made changes and is in autocommit mode, then commit those
 60643 ** changes.  If a rollback is needed, then do the rollback.
 60644 **
 60645 ** This routine is the only way to move the state of a VM from
 60646 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
 60647 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
 60648 **
 60649 ** Return an error code.  If the commit could not complete because of
 60650 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
 60651 ** means the close did not happen and needs to be repeated.
 60652 */
 60653 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
 60654   int rc;                         /* Used to store transient return codes */
 60655   sqlite3 *db = p->db;
 60657   /* This function contains the logic that determines if a statement or
 60658   ** transaction will be committed or rolled back as a result of the
 60659   ** execution of this virtual machine. 
 60660   **
 60661   ** If any of the following errors occur:
 60662   **
 60663   **     SQLITE_NOMEM
 60664   **     SQLITE_IOERR
 60665   **     SQLITE_FULL
 60666   **     SQLITE_INTERRUPT
 60667   **
 60668   ** Then the internal cache might have been left in an inconsistent
 60669   ** state.  We need to rollback the statement transaction, if there is
 60670   ** one, or the complete transaction if there is no statement transaction.
 60671   */
 60673   if( p->db->mallocFailed ){
 60674     p->rc = SQLITE_NOMEM;
 60676   if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
 60677   closeAllCursors(p);
 60678   if( p->magic!=VDBE_MAGIC_RUN ){
 60679     return SQLITE_OK;
 60681   checkActiveVdbeCnt(db);
 60683   /* No commit or rollback needed if the program never started */
 60684   if( p->pc>=0 ){
 60685     int mrc;   /* Primary error code from p->rc */
 60686     int eStatementOp = 0;
 60687     int isSpecialError;            /* Set to true if a 'special' error */
 60689     /* Lock all btrees used by the statement */
 60690     sqlite3VdbeEnter(p);
 60692     /* Check for one of the special errors */
 60693     mrc = p->rc & 0xff;
 60694     assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
 60695     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
 60696                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
 60697     if( isSpecialError ){
 60698       /* If the query was read-only and the error code is SQLITE_INTERRUPT, 
 60699       ** no rollback is necessary. Otherwise, at least a savepoint 
 60700       ** transaction must be rolled back to restore the database to a 
 60701       ** consistent state.
 60702       **
 60703       ** Even if the statement is read-only, it is important to perform
 60704       ** a statement or transaction rollback operation. If the error 
 60705       ** occured while writing to the journal, sub-journal or database
 60706       ** file as part of an effort to free up cache space (see function
 60707       ** pagerStress() in pager.c), the rollback is required to restore 
 60708       ** the pager to a consistent state.
 60709       */
 60710       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
 60711         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
 60712           eStatementOp = SAVEPOINT_ROLLBACK;
 60713         }else{
 60714           /* We are forced to roll back the active transaction. Before doing
 60715           ** so, abort any other statements this handle currently has active.
 60716           */
 60717           sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 60718           sqlite3CloseSavepoints(db);
 60719           db->autoCommit = 1;
 60724     /* Check for immediate foreign key violations. */
 60725     if( p->rc==SQLITE_OK ){
 60726       sqlite3VdbeCheckFk(p, 0);
 60729     /* If the auto-commit flag is set and this is the only active writer 
 60730     ** VM, then we do either a commit or rollback of the current transaction. 
 60731     **
 60732     ** Note: This block also runs if one of the special errors handled 
 60733     ** above has occurred. 
 60734     */
 60735     if( !sqlite3VtabInSync(db) 
 60736      && db->autoCommit 
 60737      && db->writeVdbeCnt==(p->readOnly==0) 
 60738     ){
 60739       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
 60740         rc = sqlite3VdbeCheckFk(p, 1);
 60741         if( rc!=SQLITE_OK ){
 60742           if( NEVER(p->readOnly) ){
 60743             sqlite3VdbeLeave(p);
 60744             return SQLITE_ERROR;
 60746           rc = SQLITE_CONSTRAINT;
 60747         }else{ 
 60748           /* The auto-commit flag is true, the vdbe program was successful 
 60749           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
 60750           ** key constraints to hold up the transaction. This means a commit 
 60751           ** is required. */
 60752           rc = vdbeCommit(db, p);
 60754         if( rc==SQLITE_BUSY && p->readOnly ){
 60755           sqlite3VdbeLeave(p);
 60756           return SQLITE_BUSY;
 60757         }else if( rc!=SQLITE_OK ){
 60758           p->rc = rc;
 60759           sqlite3RollbackAll(db, SQLITE_OK);
 60760         }else{
 60761           db->nDeferredCons = 0;
 60762           sqlite3CommitInternalChanges(db);
 60764       }else{
 60765         sqlite3RollbackAll(db, SQLITE_OK);
 60767       db->nStatement = 0;
 60768     }else if( eStatementOp==0 ){
 60769       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
 60770         eStatementOp = SAVEPOINT_RELEASE;
 60771       }else if( p->errorAction==OE_Abort ){
 60772         eStatementOp = SAVEPOINT_ROLLBACK;
 60773       }else{
 60774         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 60775         sqlite3CloseSavepoints(db);
 60776         db->autoCommit = 1;
 60780     /* If eStatementOp is non-zero, then a statement transaction needs to
 60781     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
 60782     ** do so. If this operation returns an error, and the current statement
 60783     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
 60784     ** current statement error code.
 60785     */
 60786     if( eStatementOp ){
 60787       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
 60788       if( rc ){
 60789         if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
 60790           p->rc = rc;
 60791           sqlite3DbFree(db, p->zErrMsg);
 60792           p->zErrMsg = 0;
 60794         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 60795         sqlite3CloseSavepoints(db);
 60796         db->autoCommit = 1;
 60800     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
 60801     ** has been rolled back, update the database connection change-counter. 
 60802     */
 60803     if( p->changeCntOn ){
 60804       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
 60805         sqlite3VdbeSetChanges(db, p->nChange);
 60806       }else{
 60807         sqlite3VdbeSetChanges(db, 0);
 60809       p->nChange = 0;
 60812     /* Release the locks */
 60813     sqlite3VdbeLeave(p);
 60816   /* We have successfully halted and closed the VM.  Record this fact. */
 60817   if( p->pc>=0 ){
 60818     db->activeVdbeCnt--;
 60819     if( !p->readOnly ){
 60820       db->writeVdbeCnt--;
 60822     assert( db->activeVdbeCnt>=db->writeVdbeCnt );
 60824   p->magic = VDBE_MAGIC_HALT;
 60825   checkActiveVdbeCnt(db);
 60826   if( p->db->mallocFailed ){
 60827     p->rc = SQLITE_NOMEM;
 60830   /* If the auto-commit flag is set to true, then any locks that were held
 60831   ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 
 60832   ** to invoke any required unlock-notify callbacks.
 60833   */
 60834   if( db->autoCommit ){
 60835     sqlite3ConnectionUnlocked(db);
 60838   assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
 60839   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
 60843 /*
 60844 ** Each VDBE holds the result of the most recent sqlite3_step() call
 60845 ** in p->rc.  This routine sets that result back to SQLITE_OK.
 60846 */
 60847 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
 60848   p->rc = SQLITE_OK;
 60851 /*
 60852 ** Copy the error code and error message belonging to the VDBE passed
 60853 ** as the first argument to its database handle (so that they will be 
 60854 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
 60855 **
 60856 ** This function does not clear the VDBE error code or message, just
 60857 ** copies them to the database handle.
 60858 */
 60859 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
 60860   sqlite3 *db = p->db;
 60861   int rc = p->rc;
 60862   if( p->zErrMsg ){
 60863     u8 mallocFailed = db->mallocFailed;
 60864     sqlite3BeginBenignMalloc();
 60865     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
 60866     sqlite3EndBenignMalloc();
 60867     db->mallocFailed = mallocFailed;
 60868     db->errCode = rc;
 60869   }else{
 60870     sqlite3Error(db, rc, 0);
 60872   return rc;
 60875 #ifdef SQLITE_ENABLE_SQLLOG
 60876 /*
 60877 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
 60878 ** invoke it.
 60879 */
 60880 static void vdbeInvokeSqllog(Vdbe *v){
 60881   if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
 60882     char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
 60883     assert( v->db->init.busy==0 );
 60884     if( zExpanded ){
 60885       sqlite3GlobalConfig.xSqllog(
 60886           sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
 60887       );
 60888       sqlite3DbFree(v->db, zExpanded);
 60892 #else
 60893 # define vdbeInvokeSqllog(x)
 60894 #endif
 60896 /*
 60897 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
 60898 ** Write any error messages into *pzErrMsg.  Return the result code.
 60899 **
 60900 ** After this routine is run, the VDBE should be ready to be executed
 60901 ** again.
 60902 **
 60903 ** To look at it another way, this routine resets the state of the
 60904 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
 60905 ** VDBE_MAGIC_INIT.
 60906 */
 60907 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
 60908   sqlite3 *db;
 60909   db = p->db;
 60911   /* If the VM did not run to completion or if it encountered an
 60912   ** error, then it might not have been halted properly.  So halt
 60913   ** it now.
 60914   */
 60915   sqlite3VdbeHalt(p);
 60917   /* If the VDBE has be run even partially, then transfer the error code
 60918   ** and error message from the VDBE into the main database structure.  But
 60919   ** if the VDBE has just been set to run but has not actually executed any
 60920   ** instructions yet, leave the main database error information unchanged.
 60921   */
 60922   if( p->pc>=0 ){
 60923     vdbeInvokeSqllog(p);
 60924     sqlite3VdbeTransferError(p);
 60925     sqlite3DbFree(db, p->zErrMsg);
 60926     p->zErrMsg = 0;
 60927     if( p->runOnlyOnce ) p->expired = 1;
 60928   }else if( p->rc && p->expired ){
 60929     /* The expired flag was set on the VDBE before the first call
 60930     ** to sqlite3_step(). For consistency (since sqlite3_step() was
 60931     ** called), set the database error in this case as well.
 60932     */
 60933     sqlite3Error(db, p->rc, 0);
 60934     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
 60935     sqlite3DbFree(db, p->zErrMsg);
 60936     p->zErrMsg = 0;
 60939   /* Reclaim all memory used by the VDBE
 60940   */
 60941   Cleanup(p);
 60943   /* Save profiling information from this VDBE run.
 60944   */
 60945 #ifdef VDBE_PROFILE
 60947     FILE *out = fopen("vdbe_profile.out", "a");
 60948     if( out ){
 60949       int i;
 60950       fprintf(out, "---- ");
 60951       for(i=0; i<p->nOp; i++){
 60952         fprintf(out, "%02x", p->aOp[i].opcode);
 60954       fprintf(out, "\n");
 60955       for(i=0; i<p->nOp; i++){
 60956         fprintf(out, "%6d %10lld %8lld ",
 60957            p->aOp[i].cnt,
 60958            p->aOp[i].cycles,
 60959            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
 60960         );
 60961         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
 60963       fclose(out);
 60966 #endif
 60967   p->magic = VDBE_MAGIC_INIT;
 60968   return p->rc & db->errMask;
 60971 /*
 60972 ** Clean up and delete a VDBE after execution.  Return an integer which is
 60973 ** the result code.  Write any error message text into *pzErrMsg.
 60974 */
 60975 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
 60976   int rc = SQLITE_OK;
 60977   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
 60978     rc = sqlite3VdbeReset(p);
 60979     assert( (rc & p->db->errMask)==rc );
 60981   sqlite3VdbeDelete(p);
 60982   return rc;
 60985 /*
 60986 ** Call the destructor for each auxdata entry in pVdbeFunc for which
 60987 ** the corresponding bit in mask is clear.  Auxdata entries beyond 31
 60988 ** are always destroyed.  To destroy all auxdata entries, call this
 60989 ** routine with mask==0.
 60990 */
 60991 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
 60992   int i;
 60993   for(i=0; i<pVdbeFunc->nAux; i++){
 60994     struct AuxData *pAux = &pVdbeFunc->apAux[i];
 60995     if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
 60996       if( pAux->xDelete ){
 60997         pAux->xDelete(pAux->pAux);
 60999       pAux->pAux = 0;
 61004 /*
 61005 ** Free all memory associated with the Vdbe passed as the second argument,
 61006 ** except for object itself, which is preserved.
 61007 **
 61008 ** The difference between this function and sqlite3VdbeDelete() is that
 61009 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
 61010 ** the database connection and frees the object itself.
 61011 */
 61012 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
 61013   SubProgram *pSub, *pNext;
 61014   int i;
 61015   assert( p->db==0 || p->db==db );
 61016   releaseMemArray(p->aVar, p->nVar);
 61017   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 61018   for(pSub=p->pProgram; pSub; pSub=pNext){
 61019     pNext = pSub->pNext;
 61020     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
 61021     sqlite3DbFree(db, pSub);
 61023   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
 61024   vdbeFreeOpArray(db, p->aOp, p->nOp);
 61025   sqlite3DbFree(db, p->aLabel);
 61026   sqlite3DbFree(db, p->aColName);
 61027   sqlite3DbFree(db, p->zSql);
 61028   sqlite3DbFree(db, p->pFree);
 61029 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 61030   sqlite3_free(p->zExplain);
 61031   sqlite3DbFree(db, p->pExplain);
 61032 #endif
 61035 /*
 61036 ** Delete an entire VDBE.
 61037 */
 61038 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
 61039   sqlite3 *db;
 61041   if( NEVER(p==0) ) return;
 61042   db = p->db;
 61043   assert( sqlite3_mutex_held(db->mutex) );
 61044   sqlite3VdbeClearObject(db, p);
 61045   if( p->pPrev ){
 61046     p->pPrev->pNext = p->pNext;
 61047   }else{
 61048     assert( db->pVdbe==p );
 61049     db->pVdbe = p->pNext;
 61051   if( p->pNext ){
 61052     p->pNext->pPrev = p->pPrev;
 61054   p->magic = VDBE_MAGIC_DEAD;
 61055   p->db = 0;
 61056   sqlite3DbFree(db, p);
 61059 /*
 61060 ** Make sure the cursor p is ready to read or write the row to which it
 61061 ** was last positioned.  Return an error code if an OOM fault or I/O error
 61062 ** prevents us from positioning the cursor to its correct position.
 61063 **
 61064 ** If a MoveTo operation is pending on the given cursor, then do that
 61065 ** MoveTo now.  If no move is pending, check to see if the row has been
 61066 ** deleted out from under the cursor and if it has, mark the row as
 61067 ** a NULL row.
 61068 **
 61069 ** If the cursor is already pointing to the correct row and that row has
 61070 ** not been deleted out from under the cursor, then this routine is a no-op.
 61071 */
 61072 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
 61073   if( p->deferredMoveto ){
 61074     int res, rc;
 61075 #ifdef SQLITE_TEST
 61076     extern int sqlite3_search_count;
 61077 #endif
 61078     assert( p->isTable );
 61079     rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
 61080     if( rc ) return rc;
 61081     p->lastRowid = p->movetoTarget;
 61082     if( res!=0 ) return SQLITE_CORRUPT_BKPT;
 61083     p->rowidIsValid = 1;
 61084 #ifdef SQLITE_TEST
 61085     sqlite3_search_count++;
 61086 #endif
 61087     p->deferredMoveto = 0;
 61088     p->cacheStatus = CACHE_STALE;
 61089   }else if( ALWAYS(p->pCursor) ){
 61090     int hasMoved;
 61091     int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
 61092     if( rc ) return rc;
 61093     if( hasMoved ){
 61094       p->cacheStatus = CACHE_STALE;
 61095       p->nullRow = 1;
 61098   return SQLITE_OK;
 61101 /*
 61102 ** The following functions:
 61103 **
 61104 ** sqlite3VdbeSerialType()
 61105 ** sqlite3VdbeSerialTypeLen()
 61106 ** sqlite3VdbeSerialLen()
 61107 ** sqlite3VdbeSerialPut()
 61108 ** sqlite3VdbeSerialGet()
 61109 **
 61110 ** encapsulate the code that serializes values for storage in SQLite
 61111 ** data and index records. Each serialized value consists of a
 61112 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
 61113 ** integer, stored as a varint.
 61114 **
 61115 ** In an SQLite index record, the serial type is stored directly before
 61116 ** the blob of data that it corresponds to. In a table record, all serial
 61117 ** types are stored at the start of the record, and the blobs of data at
 61118 ** the end. Hence these functions allow the caller to handle the
 61119 ** serial-type and data blob seperately.
 61120 **
 61121 ** The following table describes the various storage classes for data:
 61122 **
 61123 **   serial type        bytes of data      type
 61124 **   --------------     ---------------    ---------------
 61125 **      0                     0            NULL
 61126 **      1                     1            signed integer
 61127 **      2                     2            signed integer
 61128 **      3                     3            signed integer
 61129 **      4                     4            signed integer
 61130 **      5                     6            signed integer
 61131 **      6                     8            signed integer
 61132 **      7                     8            IEEE float
 61133 **      8                     0            Integer constant 0
 61134 **      9                     0            Integer constant 1
 61135 **     10,11                               reserved for expansion
 61136 **    N>=12 and even       (N-12)/2        BLOB
 61137 **    N>=13 and odd        (N-13)/2        text
 61138 **
 61139 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
 61140 ** of SQLite will not understand those serial types.
 61141 */
 61143 /*
 61144 ** Return the serial-type for the value stored in pMem.
 61145 */
 61146 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
 61147   int flags = pMem->flags;
 61148   int n;
 61150   if( flags&MEM_Null ){
 61151     return 0;
 61153   if( flags&MEM_Int ){
 61154     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
 61155 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
 61156     i64 i = pMem->u.i;
 61157     u64 u;
 61158     if( i<0 ){
 61159       if( i<(-MAX_6BYTE) ) return 6;
 61160       /* Previous test prevents:  u = -(-9223372036854775808) */
 61161       u = -i;
 61162     }else{
 61163       u = i;
 61165     if( u<=127 ){
 61166       return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
 61168     if( u<=32767 ) return 2;
 61169     if( u<=8388607 ) return 3;
 61170     if( u<=2147483647 ) return 4;
 61171     if( u<=MAX_6BYTE ) return 5;
 61172     return 6;
 61174   if( flags&MEM_Real ){
 61175     return 7;
 61177   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
 61178   n = pMem->n;
 61179   if( flags & MEM_Zero ){
 61180     n += pMem->u.nZero;
 61182   assert( n>=0 );
 61183   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
 61186 /*
 61187 ** Return the length of the data corresponding to the supplied serial-type.
 61188 */
 61189 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
 61190   if( serial_type>=12 ){
 61191     return (serial_type-12)/2;
 61192   }else{
 61193     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
 61194     return aSize[serial_type];
 61198 /*
 61199 ** If we are on an architecture with mixed-endian floating 
 61200 ** points (ex: ARM7) then swap the lower 4 bytes with the 
 61201 ** upper 4 bytes.  Return the result.
 61202 **
 61203 ** For most architectures, this is a no-op.
 61204 **
 61205 ** (later):  It is reported to me that the mixed-endian problem
 61206 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
 61207 ** that early versions of GCC stored the two words of a 64-bit
 61208 ** float in the wrong order.  And that error has been propagated
 61209 ** ever since.  The blame is not necessarily with GCC, though.
 61210 ** GCC might have just copying the problem from a prior compiler.
 61211 ** I am also told that newer versions of GCC that follow a different
 61212 ** ABI get the byte order right.
 61213 **
 61214 ** Developers using SQLite on an ARM7 should compile and run their
 61215 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
 61216 ** enabled, some asserts below will ensure that the byte order of
 61217 ** floating point values is correct.
 61218 **
 61219 ** (2007-08-30)  Frank van Vugt has studied this problem closely
 61220 ** and has send his findings to the SQLite developers.  Frank
 61221 ** writes that some Linux kernels offer floating point hardware
 61222 ** emulation that uses only 32-bit mantissas instead of a full 
 61223 ** 48-bits as required by the IEEE standard.  (This is the
 61224 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
 61225 ** byte swapping becomes very complicated.  To avoid problems,
 61226 ** the necessary byte swapping is carried out using a 64-bit integer
 61227 ** rather than a 64-bit float.  Frank assures us that the code here
 61228 ** works for him.  We, the developers, have no way to independently
 61229 ** verify this, but Frank seems to know what he is talking about
 61230 ** so we trust him.
 61231 */
 61232 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 61233 static u64 floatSwap(u64 in){
 61234   union {
 61235     u64 r;
 61236     u32 i[2];
 61237   } u;
 61238   u32 t;
 61240   u.r = in;
 61241   t = u.i[0];
 61242   u.i[0] = u.i[1];
 61243   u.i[1] = t;
 61244   return u.r;
 61246 # define swapMixedEndianFloat(X)  X = floatSwap(X)
 61247 #else
 61248 # define swapMixedEndianFloat(X)
 61249 #endif
 61251 /*
 61252 ** Write the serialized data blob for the value stored in pMem into 
 61253 ** buf. It is assumed that the caller has allocated sufficient space.
 61254 ** Return the number of bytes written.
 61255 **
 61256 ** nBuf is the amount of space left in buf[].  nBuf must always be
 61257 ** large enough to hold the entire field.  Except, if the field is
 61258 ** a blob with a zero-filled tail, then buf[] might be just the right
 61259 ** size to hold everything except for the zero-filled tail.  If buf[]
 61260 ** is only big enough to hold the non-zero prefix, then only write that
 61261 ** prefix into buf[].  But if buf[] is large enough to hold both the
 61262 ** prefix and the tail then write the prefix and set the tail to all
 61263 ** zeros.
 61264 **
 61265 ** Return the number of bytes actually written into buf[].  The number
 61266 ** of bytes in the zero-filled tail is included in the return value only
 61267 ** if those bytes were zeroed in buf[].
 61268 */ 
 61269 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
 61270   u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
 61271   u32 len;
 61273   /* Integer and Real */
 61274   if( serial_type<=7 && serial_type>0 ){
 61275     u64 v;
 61276     u32 i;
 61277     if( serial_type==7 ){
 61278       assert( sizeof(v)==sizeof(pMem->r) );
 61279       memcpy(&v, &pMem->r, sizeof(v));
 61280       swapMixedEndianFloat(v);
 61281     }else{
 61282       v = pMem->u.i;
 61284     len = i = sqlite3VdbeSerialTypeLen(serial_type);
 61285     assert( len<=(u32)nBuf );
 61286     while( i-- ){
 61287       buf[i] = (u8)(v&0xFF);
 61288       v >>= 8;
 61290     return len;
 61293   /* String or blob */
 61294   if( serial_type>=12 ){
 61295     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
 61296              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
 61297     assert( pMem->n<=nBuf );
 61298     len = pMem->n;
 61299     memcpy(buf, pMem->z, len);
 61300     if( pMem->flags & MEM_Zero ){
 61301       len += pMem->u.nZero;
 61302       assert( nBuf>=0 );
 61303       if( len > (u32)nBuf ){
 61304         len = (u32)nBuf;
 61306       memset(&buf[pMem->n], 0, len-pMem->n);
 61308     return len;
 61311   /* NULL or constants 0 or 1 */
 61312   return 0;
 61315 /*
 61316 ** Deserialize the data blob pointed to by buf as serial type serial_type
 61317 ** and store the result in pMem.  Return the number of bytes read.
 61318 */ 
 61319 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
 61320   const unsigned char *buf,     /* Buffer to deserialize from */
 61321   u32 serial_type,              /* Serial type to deserialize */
 61322   Mem *pMem                     /* Memory cell to write value into */
 61323 ){
 61324   switch( serial_type ){
 61325     case 10:   /* Reserved for future use */
 61326     case 11:   /* Reserved for future use */
 61327     case 0: {  /* NULL */
 61328       pMem->flags = MEM_Null;
 61329       break;
 61331     case 1: { /* 1-byte signed integer */
 61332       pMem->u.i = (signed char)buf[0];
 61333       pMem->flags = MEM_Int;
 61334       return 1;
 61336     case 2: { /* 2-byte signed integer */
 61337       pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
 61338       pMem->flags = MEM_Int;
 61339       return 2;
 61341     case 3: { /* 3-byte signed integer */
 61342       pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
 61343       pMem->flags = MEM_Int;
 61344       return 3;
 61346     case 4: { /* 4-byte signed integer */
 61347       pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
 61348       pMem->flags = MEM_Int;
 61349       return 4;
 61351     case 5: { /* 6-byte signed integer */
 61352       u64 x = (((signed char)buf[0])<<8) | buf[1];
 61353       u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
 61354       x = (x<<32) | y;
 61355       pMem->u.i = *(i64*)&x;
 61356       pMem->flags = MEM_Int;
 61357       return 6;
 61359     case 6:   /* 8-byte signed integer */
 61360     case 7: { /* IEEE floating point */
 61361       u64 x;
 61362       u32 y;
 61363 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
 61364       /* Verify that integers and floating point values use the same
 61365       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
 61366       ** defined that 64-bit floating point values really are mixed
 61367       ** endian.
 61368       */
 61369       static const u64 t1 = ((u64)0x3ff00000)<<32;
 61370       static const double r1 = 1.0;
 61371       u64 t2 = t1;
 61372       swapMixedEndianFloat(t2);
 61373       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
 61374 #endif
 61376       x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
 61377       y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
 61378       x = (x<<32) | y;
 61379       if( serial_type==6 ){
 61380         pMem->u.i = *(i64*)&x;
 61381         pMem->flags = MEM_Int;
 61382       }else{
 61383         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
 61384         swapMixedEndianFloat(x);
 61385         memcpy(&pMem->r, &x, sizeof(x));
 61386         pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
 61388       return 8;
 61390     case 8:    /* Integer 0 */
 61391     case 9: {  /* Integer 1 */
 61392       pMem->u.i = serial_type-8;
 61393       pMem->flags = MEM_Int;
 61394       return 0;
 61396     default: {
 61397       u32 len = (serial_type-12)/2;
 61398       pMem->z = (char *)buf;
 61399       pMem->n = len;
 61400       pMem->xDel = 0;
 61401       if( serial_type&0x01 ){
 61402         pMem->flags = MEM_Str | MEM_Ephem;
 61403       }else{
 61404         pMem->flags = MEM_Blob | MEM_Ephem;
 61406       return len;
 61409   return 0;
 61412 /*
 61413 ** This routine is used to allocate sufficient space for an UnpackedRecord
 61414 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
 61415 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
 61416 **
 61417 ** The space is either allocated using sqlite3DbMallocRaw() or from within
 61418 ** the unaligned buffer passed via the second and third arguments (presumably
 61419 ** stack space). If the former, then *ppFree is set to a pointer that should
 61420 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
 61421 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
 61422 ** before returning.
 61423 **
 61424 ** If an OOM error occurs, NULL is returned.
 61425 */
 61426 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
 61427   KeyInfo *pKeyInfo,              /* Description of the record */
 61428   char *pSpace,                   /* Unaligned space available */
 61429   int szSpace,                    /* Size of pSpace[] in bytes */
 61430   char **ppFree                   /* OUT: Caller should free this pointer */
 61431 ){
 61432   UnpackedRecord *p;              /* Unpacked record to return */
 61433   int nOff;                       /* Increment pSpace by nOff to align it */
 61434   int nByte;                      /* Number of bytes required for *p */
 61436   /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
 61437   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
 61438   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
 61439   */
 61440   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
 61441   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
 61442   if( nByte>szSpace+nOff ){
 61443     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
 61444     *ppFree = (char *)p;
 61445     if( !p ) return 0;
 61446   }else{
 61447     p = (UnpackedRecord*)&pSpace[nOff];
 61448     *ppFree = 0;
 61451   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
 61452   assert( pKeyInfo->aSortOrder!=0 );
 61453   p->pKeyInfo = pKeyInfo;
 61454   p->nField = pKeyInfo->nField + 1;
 61455   return p;
 61458 /*
 61459 ** Given the nKey-byte encoding of a record in pKey[], populate the 
 61460 ** UnpackedRecord structure indicated by the fourth argument with the
 61461 ** contents of the decoded record.
 61462 */ 
 61463 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
 61464   KeyInfo *pKeyInfo,     /* Information about the record format */
 61465   int nKey,              /* Size of the binary record */
 61466   const void *pKey,      /* The binary record */
 61467   UnpackedRecord *p      /* Populate this structure before returning. */
 61468 ){
 61469   const unsigned char *aKey = (const unsigned char *)pKey;
 61470   int d; 
 61471   u32 idx;                        /* Offset in aKey[] to read from */
 61472   u16 u;                          /* Unsigned loop counter */
 61473   u32 szHdr;
 61474   Mem *pMem = p->aMem;
 61476   p->flags = 0;
 61477   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 61478   idx = getVarint32(aKey, szHdr);
 61479   d = szHdr;
 61480   u = 0;
 61481   while( idx<szHdr && u<p->nField && d<=nKey ){
 61482     u32 serial_type;
 61484     idx += getVarint32(&aKey[idx], serial_type);
 61485     pMem->enc = pKeyInfo->enc;
 61486     pMem->db = pKeyInfo->db;
 61487     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
 61488     pMem->zMalloc = 0;
 61489     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
 61490     pMem++;
 61491     u++;
 61493   assert( u<=pKeyInfo->nField + 1 );
 61494   p->nField = u;
 61497 /*
 61498 ** This function compares the two table rows or index records
 61499 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
 61500 ** or positive integer if key1 is less than, equal to or 
 61501 ** greater than key2.  The {nKey1, pKey1} key must be a blob
 61502 ** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
 61503 ** key must be a parsed key such as obtained from
 61504 ** sqlite3VdbeParseRecord.
 61505 **
 61506 ** Key1 and Key2 do not have to contain the same number of fields.
 61507 ** The key with fewer fields is usually compares less than the 
 61508 ** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
 61509 ** and the common prefixes are equal, then key1 is less than key2.
 61510 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
 61511 ** equal, then the keys are considered to be equal and
 61512 ** the parts beyond the common prefix are ignored.
 61513 */
 61514 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
 61515   int nKey1, const void *pKey1, /* Left key */
 61516   UnpackedRecord *pPKey2        /* Right key */
 61517 ){
 61518   int d1;            /* Offset into aKey[] of next data element */
 61519   u32 idx1;          /* Offset into aKey[] of next header element */
 61520   u32 szHdr1;        /* Number of bytes in header */
 61521   int i = 0;
 61522   int nField;
 61523   int rc = 0;
 61524   const unsigned char *aKey1 = (const unsigned char *)pKey1;
 61525   KeyInfo *pKeyInfo;
 61526   Mem mem1;
 61528   pKeyInfo = pPKey2->pKeyInfo;
 61529   mem1.enc = pKeyInfo->enc;
 61530   mem1.db = pKeyInfo->db;
 61531   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
 61532   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
 61534   /* Compilers may complain that mem1.u.i is potentially uninitialized.
 61535   ** We could initialize it, as shown here, to silence those complaints.
 61536   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
 61537   ** the unnecessary initialization has a measurable negative performance
 61538   ** impact, since this routine is a very high runner.  And so, we choose
 61539   ** to ignore the compiler warnings and leave this variable uninitialized.
 61540   */
 61541   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
 61543   idx1 = getVarint32(aKey1, szHdr1);
 61544   d1 = szHdr1;
 61545   nField = pKeyInfo->nField;
 61546   assert( pKeyInfo->aSortOrder!=0 );
 61547   while( idx1<szHdr1 && i<pPKey2->nField ){
 61548     u32 serial_type1;
 61550     /* Read the serial types for the next element in each key. */
 61551     idx1 += getVarint32( aKey1+idx1, serial_type1 );
 61552     if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
 61554     /* Extract the values to be compared.
 61555     */
 61556     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
 61558     /* Do the comparison
 61559     */
 61560     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
 61561                            i<nField ? pKeyInfo->aColl[i] : 0);
 61562     if( rc!=0 ){
 61563       assert( mem1.zMalloc==0 );  /* See comment below */
 61565       /* Invert the result if we are using DESC sort order. */
 61566       if( i<nField && pKeyInfo->aSortOrder[i] ){
 61567         rc = -rc;
 61570       /* If the PREFIX_SEARCH flag is set and all fields except the final
 61571       ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
 61572       ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
 61573       ** This is used by the OP_IsUnique opcode.
 61574       */
 61575       if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
 61576         assert( idx1==szHdr1 && rc );
 61577         assert( mem1.flags & MEM_Int );
 61578         pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
 61579         pPKey2->rowid = mem1.u.i;
 61582       return rc;
 61584     i++;
 61587   /* No memory allocation is ever used on mem1.  Prove this using
 61588   ** the following assert().  If the assert() fails, it indicates a
 61589   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
 61590   */
 61591   assert( mem1.zMalloc==0 );
 61593   /* rc==0 here means that one of the keys ran out of fields and
 61594   ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
 61595   ** flag is set, then break the tie by treating key2 as larger.
 61596   ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
 61597   ** are considered to be equal.  Otherwise, the longer key is the 
 61598   ** larger.  As it happens, the pPKey2 will always be the longer
 61599   ** if there is a difference.
 61600   */
 61601   assert( rc==0 );
 61602   if( pPKey2->flags & UNPACKED_INCRKEY ){
 61603     rc = -1;
 61604   }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
 61605     /* Leave rc==0 */
 61606   }else if( idx1<szHdr1 ){
 61607     rc = 1;
 61609   return rc;
 61613 /*
 61614 ** pCur points at an index entry created using the OP_MakeRecord opcode.
 61615 ** Read the rowid (the last field in the record) and store it in *rowid.
 61616 ** Return SQLITE_OK if everything works, or an error code otherwise.
 61617 **
 61618 ** pCur might be pointing to text obtained from a corrupt database file.
 61619 ** So the content cannot be trusted.  Do appropriate checks on the content.
 61620 */
 61621 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
 61622   i64 nCellKey = 0;
 61623   int rc;
 61624   u32 szHdr;        /* Size of the header */
 61625   u32 typeRowid;    /* Serial type of the rowid */
 61626   u32 lenRowid;     /* Size of the rowid */
 61627   Mem m, v;
 61629   UNUSED_PARAMETER(db);
 61631   /* Get the size of the index entry.  Only indices entries of less
 61632   ** than 2GiB are support - anything large must be database corruption.
 61633   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
 61634   ** this code can safely assume that nCellKey is 32-bits  
 61635   */
 61636   assert( sqlite3BtreeCursorIsValid(pCur) );
 61637   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 61638   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
 61639   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
 61641   /* Read in the complete content of the index entry */
 61642   memset(&m, 0, sizeof(m));
 61643   rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
 61644   if( rc ){
 61645     return rc;
 61648   /* The index entry must begin with a header size */
 61649   (void)getVarint32((u8*)m.z, szHdr);
 61650   testcase( szHdr==3 );
 61651   testcase( szHdr==m.n );
 61652   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
 61653     goto idx_rowid_corruption;
 61656   /* The last field of the index should be an integer - the ROWID.
 61657   ** Verify that the last entry really is an integer. */
 61658   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
 61659   testcase( typeRowid==1 );
 61660   testcase( typeRowid==2 );
 61661   testcase( typeRowid==3 );
 61662   testcase( typeRowid==4 );
 61663   testcase( typeRowid==5 );
 61664   testcase( typeRowid==6 );
 61665   testcase( typeRowid==8 );
 61666   testcase( typeRowid==9 );
 61667   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
 61668     goto idx_rowid_corruption;
 61670   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
 61671   testcase( (u32)m.n==szHdr+lenRowid );
 61672   if( unlikely((u32)m.n<szHdr+lenRowid) ){
 61673     goto idx_rowid_corruption;
 61676   /* Fetch the integer off the end of the index record */
 61677   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
 61678   *rowid = v.u.i;
 61679   sqlite3VdbeMemRelease(&m);
 61680   return SQLITE_OK;
 61682   /* Jump here if database corruption is detected after m has been
 61683   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
 61684 idx_rowid_corruption:
 61685   testcase( m.zMalloc!=0 );
 61686   sqlite3VdbeMemRelease(&m);
 61687   return SQLITE_CORRUPT_BKPT;
 61690 /*
 61691 ** Compare the key of the index entry that cursor pC is pointing to against
 61692 ** the key string in pUnpacked.  Write into *pRes a number
 61693 ** that is negative, zero, or positive if pC is less than, equal to,
 61694 ** or greater than pUnpacked.  Return SQLITE_OK on success.
 61695 **
 61696 ** pUnpacked is either created without a rowid or is truncated so that it
 61697 ** omits the rowid at the end.  The rowid at the end of the index entry
 61698 ** is ignored as well.  Hence, this routine only compares the prefixes 
 61699 ** of the keys prior to the final rowid, not the entire key.
 61700 */
 61701 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
 61702   VdbeCursor *pC,             /* The cursor to compare against */
 61703   UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
 61704   int *res                    /* Write the comparison result here */
 61705 ){
 61706   i64 nCellKey = 0;
 61707   int rc;
 61708   BtCursor *pCur = pC->pCursor;
 61709   Mem m;
 61711   assert( sqlite3BtreeCursorIsValid(pCur) );
 61712   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 61713   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
 61714   /* nCellKey will always be between 0 and 0xffffffff because of the say
 61715   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
 61716   if( nCellKey<=0 || nCellKey>0x7fffffff ){
 61717     *res = 0;
 61718     return SQLITE_CORRUPT_BKPT;
 61720   memset(&m, 0, sizeof(m));
 61721   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
 61722   if( rc ){
 61723     return rc;
 61725   assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
 61726   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
 61727   sqlite3VdbeMemRelease(&m);
 61728   return SQLITE_OK;
 61731 /*
 61732 ** This routine sets the value to be returned by subsequent calls to
 61733 ** sqlite3_changes() on the database handle 'db'. 
 61734 */
 61735 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
 61736   assert( sqlite3_mutex_held(db->mutex) );
 61737   db->nChange = nChange;
 61738   db->nTotalChange += nChange;
 61741 /*
 61742 ** Set a flag in the vdbe to update the change counter when it is finalised
 61743 ** or reset.
 61744 */
 61745 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
 61746   v->changeCntOn = 1;
 61749 /*
 61750 ** Mark every prepared statement associated with a database connection
 61751 ** as expired.
 61752 **
 61753 ** An expired statement means that recompilation of the statement is
 61754 ** recommend.  Statements expire when things happen that make their
 61755 ** programs obsolete.  Removing user-defined functions or collating
 61756 ** sequences, or changing an authorization function are the types of
 61757 ** things that make prepared statements obsolete.
 61758 */
 61759 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
 61760   Vdbe *p;
 61761   for(p = db->pVdbe; p; p=p->pNext){
 61762     p->expired = 1;
 61766 /*
 61767 ** Return the database associated with the Vdbe.
 61768 */
 61769 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
 61770   return v->db;
 61773 /*
 61774 ** Return a pointer to an sqlite3_value structure containing the value bound
 61775 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
 61776 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
 61777 ** constants) to the value before returning it.
 61778 **
 61779 ** The returned value must be freed by the caller using sqlite3ValueFree().
 61780 */
 61781 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
 61782   assert( iVar>0 );
 61783   if( v ){
 61784     Mem *pMem = &v->aVar[iVar-1];
 61785     if( 0==(pMem->flags & MEM_Null) ){
 61786       sqlite3_value *pRet = sqlite3ValueNew(v->db);
 61787       if( pRet ){
 61788         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
 61789         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
 61790         sqlite3VdbeMemStoreType((Mem *)pRet);
 61792       return pRet;
 61795   return 0;
 61798 /*
 61799 ** Configure SQL variable iVar so that binding a new value to it signals
 61800 ** to sqlite3_reoptimize() that re-preparing the statement may result
 61801 ** in a better query plan.
 61802 */
 61803 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
 61804   assert( iVar>0 );
 61805   if( iVar>32 ){
 61806     v->expmask = 0xffffffff;
 61807   }else{
 61808     v->expmask |= ((u32)1 << (iVar-1));
 61812 /************** End of vdbeaux.c *********************************************/
 61813 /************** Begin file vdbeapi.c *****************************************/
 61814 /*
 61815 ** 2004 May 26
 61816 **
 61817 ** The author disclaims copyright to this source code.  In place of
 61818 ** a legal notice, here is a blessing:
 61819 **
 61820 **    May you do good and not evil.
 61821 **    May you find forgiveness for yourself and forgive others.
 61822 **    May you share freely, never taking more than you give.
 61823 **
 61824 *************************************************************************
 61825 **
 61826 ** This file contains code use to implement APIs that are part of the
 61827 ** VDBE.
 61828 */
 61830 #ifndef SQLITE_OMIT_DEPRECATED
 61831 /*
 61832 ** Return TRUE (non-zero) of the statement supplied as an argument needs
 61833 ** to be recompiled.  A statement needs to be recompiled whenever the
 61834 ** execution environment changes in a way that would alter the program
 61835 ** that sqlite3_prepare() generates.  For example, if new functions or
 61836 ** collating sequences are registered or if an authorizer function is
 61837 ** added or changed.
 61838 */
 61839 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
 61840   Vdbe *p = (Vdbe*)pStmt;
 61841   return p==0 || p->expired;
 61843 #endif
 61845 /*
 61846 ** Check on a Vdbe to make sure it has not been finalized.  Log
 61847 ** an error and return true if it has been finalized (or is otherwise
 61848 ** invalid).  Return false if it is ok.
 61849 */
 61850 static int vdbeSafety(Vdbe *p){
 61851   if( p->db==0 ){
 61852     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
 61853     return 1;
 61854   }else{
 61855     return 0;
 61858 static int vdbeSafetyNotNull(Vdbe *p){
 61859   if( p==0 ){
 61860     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
 61861     return 1;
 61862   }else{
 61863     return vdbeSafety(p);
 61867 /*
 61868 ** The following routine destroys a virtual machine that is created by
 61869 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
 61870 ** success/failure code that describes the result of executing the virtual
 61871 ** machine.
 61872 **
 61873 ** This routine sets the error code and string returned by
 61874 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 61875 */
 61876 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
 61877   int rc;
 61878   if( pStmt==0 ){
 61879     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
 61880     ** pointer is a harmless no-op. */
 61881     rc = SQLITE_OK;
 61882   }else{
 61883     Vdbe *v = (Vdbe*)pStmt;
 61884     sqlite3 *db = v->db;
 61885     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
 61886     sqlite3_mutex_enter(db->mutex);
 61887     rc = sqlite3VdbeFinalize(v);
 61888     rc = sqlite3ApiExit(db, rc);
 61889     sqlite3LeaveMutexAndCloseZombie(db);
 61891   return rc;
 61894 /*
 61895 ** Terminate the current execution of an SQL statement and reset it
 61896 ** back to its starting state so that it can be reused. A success code from
 61897 ** the prior execution is returned.
 61898 **
 61899 ** This routine sets the error code and string returned by
 61900 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 61901 */
 61902 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
 61903   int rc;
 61904   if( pStmt==0 ){
 61905     rc = SQLITE_OK;
 61906   }else{
 61907     Vdbe *v = (Vdbe*)pStmt;
 61908     sqlite3_mutex_enter(v->db->mutex);
 61909     rc = sqlite3VdbeReset(v);
 61910     sqlite3VdbeRewind(v);
 61911     assert( (rc & (v->db->errMask))==rc );
 61912     rc = sqlite3ApiExit(v->db, rc);
 61913     sqlite3_mutex_leave(v->db->mutex);
 61915   return rc;
 61918 /*
 61919 ** Set all the parameters in the compiled SQL statement to NULL.
 61920 */
 61921 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
 61922   int i;
 61923   int rc = SQLITE_OK;
 61924   Vdbe *p = (Vdbe*)pStmt;
 61925 #if SQLITE_THREADSAFE
 61926   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
 61927 #endif
 61928   sqlite3_mutex_enter(mutex);
 61929   for(i=0; i<p->nVar; i++){
 61930     sqlite3VdbeMemRelease(&p->aVar[i]);
 61931     p->aVar[i].flags = MEM_Null;
 61933   if( p->isPrepareV2 && p->expmask ){
 61934     p->expired = 1;
 61936   sqlite3_mutex_leave(mutex);
 61937   return rc;
 61941 /**************************** sqlite3_value_  *******************************
 61942 ** The following routines extract information from a Mem or sqlite3_value
 61943 ** structure.
 61944 */
 61945 SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
 61946   Mem *p = (Mem*)pVal;
 61947   if( p->flags & (MEM_Blob|MEM_Str) ){
 61948     sqlite3VdbeMemExpandBlob(p);
 61949     p->flags &= ~MEM_Str;
 61950     p->flags |= MEM_Blob;
 61951     return p->n ? p->z : 0;
 61952   }else{
 61953     return sqlite3_value_text(pVal);
 61956 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
 61957   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
 61959 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
 61960   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
 61962 SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
 61963   return sqlite3VdbeRealValue((Mem*)pVal);
 61965 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
 61966   return (int)sqlite3VdbeIntValue((Mem*)pVal);
 61968 SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
 61969   return sqlite3VdbeIntValue((Mem*)pVal);
 61971 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
 61972   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
 61974 #ifndef SQLITE_OMIT_UTF16
 61975 SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
 61976   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
 61978 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
 61979   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
 61981 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
 61982   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
 61984 #endif /* SQLITE_OMIT_UTF16 */
 61985 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
 61986   return pVal->type;
 61989 /**************************** sqlite3_result_  *******************************
 61990 ** The following routines are used by user-defined functions to specify
 61991 ** the function result.
 61992 **
 61993 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
 61994 ** result as a string or blob but if the string or blob is too large, it
 61995 ** then sets the error code to SQLITE_TOOBIG
 61996 */
 61997 static void setResultStrOrError(
 61998   sqlite3_context *pCtx,  /* Function context */
 61999   const char *z,          /* String pointer */
 62000   int n,                  /* Bytes in string, or negative */
 62001   u8 enc,                 /* Encoding of z.  0 for BLOBs */
 62002   void (*xDel)(void*)     /* Destructor function */
 62003 ){
 62004   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
 62005     sqlite3_result_error_toobig(pCtx);
 62008 SQLITE_API void sqlite3_result_blob(
 62009   sqlite3_context *pCtx, 
 62010   const void *z, 
 62011   int n, 
 62012   void (*xDel)(void *)
 62013 ){
 62014   assert( n>=0 );
 62015   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62016   setResultStrOrError(pCtx, z, n, 0, xDel);
 62018 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
 62019   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62020   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
 62022 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
 62023   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62024   pCtx->isError = SQLITE_ERROR;
 62025   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
 62027 #ifndef SQLITE_OMIT_UTF16
 62028 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
 62029   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62030   pCtx->isError = SQLITE_ERROR;
 62031   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
 62033 #endif
 62034 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
 62035   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62036   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
 62038 SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
 62039   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62040   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
 62042 SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
 62043   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62044   sqlite3VdbeMemSetNull(&pCtx->s);
 62046 SQLITE_API void sqlite3_result_text(
 62047   sqlite3_context *pCtx, 
 62048   const char *z, 
 62049   int n,
 62050   void (*xDel)(void *)
 62051 ){
 62052   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62053   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
 62055 #ifndef SQLITE_OMIT_UTF16
 62056 SQLITE_API void sqlite3_result_text16(
 62057   sqlite3_context *pCtx, 
 62058   const void *z, 
 62059   int n, 
 62060   void (*xDel)(void *)
 62061 ){
 62062   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62063   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
 62065 SQLITE_API void sqlite3_result_text16be(
 62066   sqlite3_context *pCtx, 
 62067   const void *z, 
 62068   int n, 
 62069   void (*xDel)(void *)
 62070 ){
 62071   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62072   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
 62074 SQLITE_API void sqlite3_result_text16le(
 62075   sqlite3_context *pCtx, 
 62076   const void *z, 
 62077   int n, 
 62078   void (*xDel)(void *)
 62079 ){
 62080   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62081   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
 62083 #endif /* SQLITE_OMIT_UTF16 */
 62084 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
 62085   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62086   sqlite3VdbeMemCopy(&pCtx->s, pValue);
 62088 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
 62089   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62090   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
 62092 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
 62093   pCtx->isError = errCode;
 62094   if( pCtx->s.flags & MEM_Null ){
 62095     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
 62096                          SQLITE_UTF8, SQLITE_STATIC);
 62100 /* Force an SQLITE_TOOBIG error. */
 62101 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
 62102   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62103   pCtx->isError = SQLITE_TOOBIG;
 62104   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
 62105                        SQLITE_UTF8, SQLITE_STATIC);
 62108 /* An SQLITE_NOMEM error. */
 62109 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
 62110   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62111   sqlite3VdbeMemSetNull(&pCtx->s);
 62112   pCtx->isError = SQLITE_NOMEM;
 62113   pCtx->s.db->mallocFailed = 1;
 62116 /*
 62117 ** This function is called after a transaction has been committed. It 
 62118 ** invokes callbacks registered with sqlite3_wal_hook() as required.
 62119 */
 62120 static int doWalCallbacks(sqlite3 *db){
 62121   int rc = SQLITE_OK;
 62122 #ifndef SQLITE_OMIT_WAL
 62123   int i;
 62124   for(i=0; i<db->nDb; i++){
 62125     Btree *pBt = db->aDb[i].pBt;
 62126     if( pBt ){
 62127       int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
 62128       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
 62129         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
 62133 #endif
 62134   return rc;
 62137 /*
 62138 ** Execute the statement pStmt, either until a row of data is ready, the
 62139 ** statement is completely executed or an error occurs.
 62140 **
 62141 ** This routine implements the bulk of the logic behind the sqlite_step()
 62142 ** API.  The only thing omitted is the automatic recompile if a 
 62143 ** schema change has occurred.  That detail is handled by the
 62144 ** outer sqlite3_step() wrapper procedure.
 62145 */
 62146 static int sqlite3Step(Vdbe *p){
 62147   sqlite3 *db;
 62148   int rc;
 62150   assert(p);
 62151   if( p->magic!=VDBE_MAGIC_RUN ){
 62152     /* We used to require that sqlite3_reset() be called before retrying
 62153     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
 62154     ** with version 3.7.0, we changed this so that sqlite3_reset() would
 62155     ** be called automatically instead of throwing the SQLITE_MISUSE error.
 62156     ** This "automatic-reset" change is not technically an incompatibility, 
 62157     ** since any application that receives an SQLITE_MISUSE is broken by
 62158     ** definition.
 62159     **
 62160     ** Nevertheless, some published applications that were originally written
 62161     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 
 62162     ** returns, and those were broken by the automatic-reset change.  As a
 62163     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
 62164     ** legacy behavior of returning SQLITE_MISUSE for cases where the 
 62165     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
 62166     ** or SQLITE_BUSY error.
 62167     */
 62168 #ifdef SQLITE_OMIT_AUTORESET
 62169     if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
 62170       sqlite3_reset((sqlite3_stmt*)p);
 62171     }else{
 62172       return SQLITE_MISUSE_BKPT;
 62174 #else
 62175     sqlite3_reset((sqlite3_stmt*)p);
 62176 #endif
 62179   /* Check that malloc() has not failed. If it has, return early. */
 62180   db = p->db;
 62181   if( db->mallocFailed ){
 62182     p->rc = SQLITE_NOMEM;
 62183     return SQLITE_NOMEM;
 62186   if( p->pc<=0 && p->expired ){
 62187     p->rc = SQLITE_SCHEMA;
 62188     rc = SQLITE_ERROR;
 62189     goto end_of_step;
 62191   if( p->pc<0 ){
 62192     /* If there are no other statements currently running, then
 62193     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
 62194     ** from interrupting a statement that has not yet started.
 62195     */
 62196     if( db->activeVdbeCnt==0 ){
 62197       db->u1.isInterrupted = 0;
 62200     assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
 62202 #ifndef SQLITE_OMIT_TRACE
 62203     if( db->xProfile && !db->init.busy ){
 62204       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
 62206 #endif
 62208     db->activeVdbeCnt++;
 62209     if( p->readOnly==0 ) db->writeVdbeCnt++;
 62210     p->pc = 0;
 62212 #ifndef SQLITE_OMIT_EXPLAIN
 62213   if( p->explain ){
 62214     rc = sqlite3VdbeList(p);
 62215   }else
 62216 #endif /* SQLITE_OMIT_EXPLAIN */
 62218     db->vdbeExecCnt++;
 62219     rc = sqlite3VdbeExec(p);
 62220     db->vdbeExecCnt--;
 62223 #ifndef SQLITE_OMIT_TRACE
 62224   /* Invoke the profile callback if there is one
 62225   */
 62226   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
 62227     sqlite3_int64 iNow;
 62228     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
 62229     db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
 62231 #endif
 62233   if( rc==SQLITE_DONE ){
 62234     assert( p->rc==SQLITE_OK );
 62235     p->rc = doWalCallbacks(db);
 62236     if( p->rc!=SQLITE_OK ){
 62237       rc = SQLITE_ERROR;
 62241   db->errCode = rc;
 62242   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
 62243     p->rc = SQLITE_NOMEM;
 62245 end_of_step:
 62246   /* At this point local variable rc holds the value that should be 
 62247   ** returned if this statement was compiled using the legacy 
 62248   ** sqlite3_prepare() interface. According to the docs, this can only
 62249   ** be one of the values in the first assert() below. Variable p->rc 
 62250   ** contains the value that would be returned if sqlite3_finalize() 
 62251   ** were called on statement p.
 62252   */
 62253   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR 
 62254        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
 62255   );
 62256   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
 62257   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
 62258     /* If this statement was prepared using sqlite3_prepare_v2(), and an
 62259     ** error has occured, then return the error code in p->rc to the
 62260     ** caller. Set the error code in the database handle to the same value.
 62261     */ 
 62262     rc = sqlite3VdbeTransferError(p);
 62264   return (rc&db->errMask);
 62267 /*
 62268 ** The maximum number of times that a statement will try to reparse
 62269 ** itself before giving up and returning SQLITE_SCHEMA.
 62270 */
 62271 #ifndef SQLITE_MAX_SCHEMA_RETRY
 62272 # define SQLITE_MAX_SCHEMA_RETRY 5
 62273 #endif
 62275 /*
 62276 ** This is the top-level implementation of sqlite3_step().  Call
 62277 ** sqlite3Step() to do most of the work.  If a schema error occurs,
 62278 ** call sqlite3Reprepare() and try again.
 62279 */
 62280 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
 62281   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
 62282   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
 62283   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
 62284   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
 62285   sqlite3 *db;             /* The database connection */
 62287   if( vdbeSafetyNotNull(v) ){
 62288     return SQLITE_MISUSE_BKPT;
 62290   db = v->db;
 62291   sqlite3_mutex_enter(db->mutex);
 62292   v->doingRerun = 0;
 62293   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
 62294          && cnt++ < SQLITE_MAX_SCHEMA_RETRY
 62295          && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
 62296     sqlite3_reset(pStmt);
 62297     v->doingRerun = 1;
 62298     assert( v->expired==0 );
 62300   if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
 62301     /* This case occurs after failing to recompile an sql statement. 
 62302     ** The error message from the SQL compiler has already been loaded 
 62303     ** into the database handle. This block copies the error message 
 62304     ** from the database handle into the statement and sets the statement
 62305     ** program counter to 0 to ensure that when the statement is 
 62306     ** finalized or reset the parser error message is available via
 62307     ** sqlite3_errmsg() and sqlite3_errcode().
 62308     */
 62309     const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
 62310     sqlite3DbFree(db, v->zErrMsg);
 62311     if( !db->mallocFailed ){
 62312       v->zErrMsg = sqlite3DbStrDup(db, zErr);
 62313       v->rc = rc2;
 62314     } else {
 62315       v->zErrMsg = 0;
 62316       v->rc = rc = SQLITE_NOMEM;
 62319   rc = sqlite3ApiExit(db, rc);
 62320   sqlite3_mutex_leave(db->mutex);
 62321   return rc;
 62324 /*
 62325 ** Extract the user data from a sqlite3_context structure and return a
 62326 ** pointer to it.
 62327 */
 62328 SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
 62329   assert( p && p->pFunc );
 62330   return p->pFunc->pUserData;
 62333 /*
 62334 ** Extract the user data from a sqlite3_context structure and return a
 62335 ** pointer to it.
 62336 **
 62337 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
 62338 ** returns a copy of the pointer to the database connection (the 1st
 62339 ** parameter) of the sqlite3_create_function() and
 62340 ** sqlite3_create_function16() routines that originally registered the
 62341 ** application defined function.
 62342 */
 62343 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
 62344   assert( p && p->pFunc );
 62345   return p->s.db;
 62348 /*
 62349 ** The following is the implementation of an SQL function that always
 62350 ** fails with an error message stating that the function is used in the
 62351 ** wrong context.  The sqlite3_overload_function() API might construct
 62352 ** SQL function that use this routine so that the functions will exist
 62353 ** for name resolution but are actually overloaded by the xFindFunction
 62354 ** method of virtual tables.
 62355 */
 62356 SQLITE_PRIVATE void sqlite3InvalidFunction(
 62357   sqlite3_context *context,  /* The function calling context */
 62358   int NotUsed,               /* Number of arguments to the function */
 62359   sqlite3_value **NotUsed2   /* Value of each argument */
 62360 ){
 62361   const char *zName = context->pFunc->zName;
 62362   char *zErr;
 62363   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 62364   zErr = sqlite3_mprintf(
 62365       "unable to use function %s in the requested context", zName);
 62366   sqlite3_result_error(context, zErr, -1);
 62367   sqlite3_free(zErr);
 62370 /*
 62371 ** Allocate or return the aggregate context for a user function.  A new
 62372 ** context is allocated on the first call.  Subsequent calls return the
 62373 ** same context that was returned on prior calls.
 62374 */
 62375 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
 62376   Mem *pMem;
 62377   assert( p && p->pFunc && p->pFunc->xStep );
 62378   assert( sqlite3_mutex_held(p->s.db->mutex) );
 62379   pMem = p->pMem;
 62380   testcase( nByte<0 );
 62381   if( (pMem->flags & MEM_Agg)==0 ){
 62382     if( nByte<=0 ){
 62383       sqlite3VdbeMemReleaseExternal(pMem);
 62384       pMem->flags = MEM_Null;
 62385       pMem->z = 0;
 62386     }else{
 62387       sqlite3VdbeMemGrow(pMem, nByte, 0);
 62388       pMem->flags = MEM_Agg;
 62389       pMem->u.pDef = p->pFunc;
 62390       if( pMem->z ){
 62391         memset(pMem->z, 0, nByte);
 62395   return (void*)pMem->z;
 62398 /*
 62399 ** Return the auxilary data pointer, if any, for the iArg'th argument to
 62400 ** the user-function defined by pCtx.
 62401 */
 62402 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
 62403   VdbeFunc *pVdbeFunc;
 62405   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62406   pVdbeFunc = pCtx->pVdbeFunc;
 62407   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
 62408     return 0;
 62410   return pVdbeFunc->apAux[iArg].pAux;
 62413 /*
 62414 ** Set the auxilary data pointer and delete function, for the iArg'th
 62415 ** argument to the user-function defined by pCtx. Any previous value is
 62416 ** deleted by calling the delete function specified when it was set.
 62417 */
 62418 SQLITE_API void sqlite3_set_auxdata(
 62419   sqlite3_context *pCtx, 
 62420   int iArg, 
 62421   void *pAux, 
 62422   void (*xDelete)(void*)
 62423 ){
 62424   struct AuxData *pAuxData;
 62425   VdbeFunc *pVdbeFunc;
 62426   if( iArg<0 ) goto failed;
 62428   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 62429   pVdbeFunc = pCtx->pVdbeFunc;
 62430   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
 62431     int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
 62432     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
 62433     pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
 62434     if( !pVdbeFunc ){
 62435       goto failed;
 62437     pCtx->pVdbeFunc = pVdbeFunc;
 62438     memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
 62439     pVdbeFunc->nAux = iArg+1;
 62440     pVdbeFunc->pFunc = pCtx->pFunc;
 62443   pAuxData = &pVdbeFunc->apAux[iArg];
 62444   if( pAuxData->pAux && pAuxData->xDelete ){
 62445     pAuxData->xDelete(pAuxData->pAux);
 62447   pAuxData->pAux = pAux;
 62448   pAuxData->xDelete = xDelete;
 62449   return;
 62451 failed:
 62452   if( xDelete ){
 62453     xDelete(pAux);
 62457 #ifndef SQLITE_OMIT_DEPRECATED
 62458 /*
 62459 ** Return the number of times the Step function of a aggregate has been 
 62460 ** called.
 62461 **
 62462 ** This function is deprecated.  Do not use it for new code.  It is
 62463 ** provide only to avoid breaking legacy code.  New aggregate function
 62464 ** implementations should keep their own counts within their aggregate
 62465 ** context.
 62466 */
 62467 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
 62468   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
 62469   return p->pMem->n;
 62471 #endif
 62473 /*
 62474 ** Return the number of columns in the result set for the statement pStmt.
 62475 */
 62476 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
 62477   Vdbe *pVm = (Vdbe *)pStmt;
 62478   return pVm ? pVm->nResColumn : 0;
 62481 /*
 62482 ** Return the number of values available from the current row of the
 62483 ** currently executing statement pStmt.
 62484 */
 62485 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
 62486   Vdbe *pVm = (Vdbe *)pStmt;
 62487   if( pVm==0 || pVm->pResultSet==0 ) return 0;
 62488   return pVm->nResColumn;
 62492 /*
 62493 ** Check to see if column iCol of the given statement is valid.  If
 62494 ** it is, return a pointer to the Mem for the value of that column.
 62495 ** If iCol is not valid, return a pointer to a Mem which has a value
 62496 ** of NULL.
 62497 */
 62498 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
 62499   Vdbe *pVm;
 62500   Mem *pOut;
 62502   pVm = (Vdbe *)pStmt;
 62503   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
 62504     sqlite3_mutex_enter(pVm->db->mutex);
 62505     pOut = &pVm->pResultSet[i];
 62506   }else{
 62507     /* If the value passed as the second argument is out of range, return
 62508     ** a pointer to the following static Mem object which contains the
 62509     ** value SQL NULL. Even though the Mem structure contains an element
 62510     ** of type i64, on certain architectures (x86) with certain compiler
 62511     ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
 62512     ** instead of an 8-byte one. This all works fine, except that when
 62513     ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
 62514     ** that a Mem structure is located on an 8-byte boundary. To prevent
 62515     ** these assert()s from failing, when building with SQLITE_DEBUG defined
 62516     ** using gcc, we force nullMem to be 8-byte aligned using the magical
 62517     ** __attribute__((aligned(8))) macro.  */
 62518     static const Mem nullMem 
 62519 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
 62520       __attribute__((aligned(8))) 
 62521 #endif
 62522       = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
 62523 #ifdef SQLITE_DEBUG
 62524          0, 0,  /* pScopyFrom, pFiller */
 62525 #endif
 62526          0, 0 };
 62528     if( pVm && ALWAYS(pVm->db) ){
 62529       sqlite3_mutex_enter(pVm->db->mutex);
 62530       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
 62532     pOut = (Mem*)&nullMem;
 62534   return pOut;
 62537 /*
 62538 ** This function is called after invoking an sqlite3_value_XXX function on a 
 62539 ** column value (i.e. a value returned by evaluating an SQL expression in the
 62540 ** select list of a SELECT statement) that may cause a malloc() failure. If 
 62541 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
 62542 ** code of statement pStmt set to SQLITE_NOMEM.
 62543 **
 62544 ** Specifically, this is called from within:
 62545 **
 62546 **     sqlite3_column_int()
 62547 **     sqlite3_column_int64()
 62548 **     sqlite3_column_text()
 62549 **     sqlite3_column_text16()
 62550 **     sqlite3_column_real()
 62551 **     sqlite3_column_bytes()
 62552 **     sqlite3_column_bytes16()
 62553 **     sqiite3_column_blob()
 62554 */
 62555 static void columnMallocFailure(sqlite3_stmt *pStmt)
 62557   /* If malloc() failed during an encoding conversion within an
 62558   ** sqlite3_column_XXX API, then set the return code of the statement to
 62559   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
 62560   ** and _finalize() will return NOMEM.
 62561   */
 62562   Vdbe *p = (Vdbe *)pStmt;
 62563   if( p ){
 62564     p->rc = sqlite3ApiExit(p->db, p->rc);
 62565     sqlite3_mutex_leave(p->db->mutex);
 62569 /**************************** sqlite3_column_  *******************************
 62570 ** The following routines are used to access elements of the current row
 62571 ** in the result set.
 62572 */
 62573 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
 62574   const void *val;
 62575   val = sqlite3_value_blob( columnMem(pStmt,i) );
 62576   /* Even though there is no encoding conversion, value_blob() might
 62577   ** need to call malloc() to expand the result of a zeroblob() 
 62578   ** expression. 
 62579   */
 62580   columnMallocFailure(pStmt);
 62581   return val;
 62583 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
 62584   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
 62585   columnMallocFailure(pStmt);
 62586   return val;
 62588 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
 62589   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
 62590   columnMallocFailure(pStmt);
 62591   return val;
 62593 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
 62594   double val = sqlite3_value_double( columnMem(pStmt,i) );
 62595   columnMallocFailure(pStmt);
 62596   return val;
 62598 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
 62599   int val = sqlite3_value_int( columnMem(pStmt,i) );
 62600   columnMallocFailure(pStmt);
 62601   return val;
 62603 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
 62604   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
 62605   columnMallocFailure(pStmt);
 62606   return val;
 62608 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
 62609   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
 62610   columnMallocFailure(pStmt);
 62611   return val;
 62613 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
 62614   Mem *pOut = columnMem(pStmt, i);
 62615   if( pOut->flags&MEM_Static ){
 62616     pOut->flags &= ~MEM_Static;
 62617     pOut->flags |= MEM_Ephem;
 62619   columnMallocFailure(pStmt);
 62620   return (sqlite3_value *)pOut;
 62622 #ifndef SQLITE_OMIT_UTF16
 62623 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
 62624   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
 62625   columnMallocFailure(pStmt);
 62626   return val;
 62628 #endif /* SQLITE_OMIT_UTF16 */
 62629 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
 62630   int iType = sqlite3_value_type( columnMem(pStmt,i) );
 62631   columnMallocFailure(pStmt);
 62632   return iType;
 62635 /* The following function is experimental and subject to change or
 62636 ** removal */
 62637 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
 62638 **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
 62639 **}
 62640 */
 62642 /*
 62643 ** Convert the N-th element of pStmt->pColName[] into a string using
 62644 ** xFunc() then return that string.  If N is out of range, return 0.
 62645 **
 62646 ** There are up to 5 names for each column.  useType determines which
 62647 ** name is returned.  Here are the names:
 62648 **
 62649 **    0      The column name as it should be displayed for output
 62650 **    1      The datatype name for the column
 62651 **    2      The name of the database that the column derives from
 62652 **    3      The name of the table that the column derives from
 62653 **    4      The name of the table column that the result column derives from
 62654 **
 62655 ** If the result is not a simple column reference (if it is an expression
 62656 ** or a constant) then useTypes 2, 3, and 4 return NULL.
 62657 */
 62658 static const void *columnName(
 62659   sqlite3_stmt *pStmt,
 62660   int N,
 62661   const void *(*xFunc)(Mem*),
 62662   int useType
 62663 ){
 62664   const void *ret = 0;
 62665   Vdbe *p = (Vdbe *)pStmt;
 62666   int n;
 62667   sqlite3 *db = p->db;
 62669   assert( db!=0 );
 62670   n = sqlite3_column_count(pStmt);
 62671   if( N<n && N>=0 ){
 62672     N += useType*n;
 62673     sqlite3_mutex_enter(db->mutex);
 62674     assert( db->mallocFailed==0 );
 62675     ret = xFunc(&p->aColName[N]);
 62676      /* A malloc may have failed inside of the xFunc() call. If this
 62677     ** is the case, clear the mallocFailed flag and return NULL.
 62678     */
 62679     if( db->mallocFailed ){
 62680       db->mallocFailed = 0;
 62681       ret = 0;
 62683     sqlite3_mutex_leave(db->mutex);
 62685   return ret;
 62688 /*
 62689 ** Return the name of the Nth column of the result set returned by SQL
 62690 ** statement pStmt.
 62691 */
 62692 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
 62693   return columnName(
 62694       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
 62696 #ifndef SQLITE_OMIT_UTF16
 62697 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
 62698   return columnName(
 62699       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
 62701 #endif
 62703 /*
 62704 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
 62705 ** not define OMIT_DECLTYPE.
 62706 */
 62707 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
 62708 # error "Must not define both SQLITE_OMIT_DECLTYPE \
 62709          and SQLITE_ENABLE_COLUMN_METADATA"
 62710 #endif
 62712 #ifndef SQLITE_OMIT_DECLTYPE
 62713 /*
 62714 ** Return the column declaration type (if applicable) of the 'i'th column
 62715 ** of the result set of SQL statement pStmt.
 62716 */
 62717 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
 62718   return columnName(
 62719       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
 62721 #ifndef SQLITE_OMIT_UTF16
 62722 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
 62723   return columnName(
 62724       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
 62726 #endif /* SQLITE_OMIT_UTF16 */
 62727 #endif /* SQLITE_OMIT_DECLTYPE */
 62729 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 62730 /*
 62731 ** Return the name of the database from which a result column derives.
 62732 ** NULL is returned if the result column is an expression or constant or
 62733 ** anything else which is not an unabiguous reference to a database column.
 62734 */
 62735 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
 62736   return columnName(
 62737       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
 62739 #ifndef SQLITE_OMIT_UTF16
 62740 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
 62741   return columnName(
 62742       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
 62744 #endif /* SQLITE_OMIT_UTF16 */
 62746 /*
 62747 ** Return the name of the table from which a result column derives.
 62748 ** NULL is returned if the result column is an expression or constant or
 62749 ** anything else which is not an unabiguous reference to a database column.
 62750 */
 62751 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
 62752   return columnName(
 62753       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
 62755 #ifndef SQLITE_OMIT_UTF16
 62756 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
 62757   return columnName(
 62758       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
 62760 #endif /* SQLITE_OMIT_UTF16 */
 62762 /*
 62763 ** Return the name of the table column from which a result column derives.
 62764 ** NULL is returned if the result column is an expression or constant or
 62765 ** anything else which is not an unabiguous reference to a database column.
 62766 */
 62767 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
 62768   return columnName(
 62769       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
 62771 #ifndef SQLITE_OMIT_UTF16
 62772 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
 62773   return columnName(
 62774       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
 62776 #endif /* SQLITE_OMIT_UTF16 */
 62777 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
 62780 /******************************* sqlite3_bind_  ***************************
 62781 ** 
 62782 ** Routines used to attach values to wildcards in a compiled SQL statement.
 62783 */
 62784 /*
 62785 ** Unbind the value bound to variable i in virtual machine p. This is the 
 62786 ** the same as binding a NULL value to the column. If the "i" parameter is
 62787 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
 62788 **
 62789 ** A successful evaluation of this routine acquires the mutex on p.
 62790 ** the mutex is released if any kind of error occurs.
 62791 **
 62792 ** The error code stored in database p->db is overwritten with the return
 62793 ** value in any case.
 62794 */
 62795 static int vdbeUnbind(Vdbe *p, int i){
 62796   Mem *pVar;
 62797   if( vdbeSafetyNotNull(p) ){
 62798     return SQLITE_MISUSE_BKPT;
 62800   sqlite3_mutex_enter(p->db->mutex);
 62801   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
 62802     sqlite3Error(p->db, SQLITE_MISUSE, 0);
 62803     sqlite3_mutex_leave(p->db->mutex);
 62804     sqlite3_log(SQLITE_MISUSE, 
 62805         "bind on a busy prepared statement: [%s]", p->zSql);
 62806     return SQLITE_MISUSE_BKPT;
 62808   if( i<1 || i>p->nVar ){
 62809     sqlite3Error(p->db, SQLITE_RANGE, 0);
 62810     sqlite3_mutex_leave(p->db->mutex);
 62811     return SQLITE_RANGE;
 62813   i--;
 62814   pVar = &p->aVar[i];
 62815   sqlite3VdbeMemRelease(pVar);
 62816   pVar->flags = MEM_Null;
 62817   sqlite3Error(p->db, SQLITE_OK, 0);
 62819   /* If the bit corresponding to this variable in Vdbe.expmask is set, then 
 62820   ** binding a new value to this variable invalidates the current query plan.
 62821   **
 62822   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
 62823   ** parameter in the WHERE clause might influence the choice of query plan
 62824   ** for a statement, then the statement will be automatically recompiled,
 62825   ** as if there had been a schema change, on the first sqlite3_step() call
 62826   ** following any change to the bindings of that parameter.
 62827   */
 62828   if( p->isPrepareV2 &&
 62829      ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
 62830   ){
 62831     p->expired = 1;
 62833   return SQLITE_OK;
 62836 /*
 62837 ** Bind a text or BLOB value.
 62838 */
 62839 static int bindText(
 62840   sqlite3_stmt *pStmt,   /* The statement to bind against */
 62841   int i,                 /* Index of the parameter to bind */
 62842   const void *zData,     /* Pointer to the data to be bound */
 62843   int nData,             /* Number of bytes of data to be bound */
 62844   void (*xDel)(void*),   /* Destructor for the data */
 62845   u8 encoding            /* Encoding for the data */
 62846 ){
 62847   Vdbe *p = (Vdbe *)pStmt;
 62848   Mem *pVar;
 62849   int rc;
 62851   rc = vdbeUnbind(p, i);
 62852   if( rc==SQLITE_OK ){
 62853     if( zData!=0 ){
 62854       pVar = &p->aVar[i-1];
 62855       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
 62856       if( rc==SQLITE_OK && encoding!=0 ){
 62857         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
 62859       sqlite3Error(p->db, rc, 0);
 62860       rc = sqlite3ApiExit(p->db, rc);
 62862     sqlite3_mutex_leave(p->db->mutex);
 62863   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
 62864     xDel((void*)zData);
 62866   return rc;
 62870 /*
 62871 ** Bind a blob value to an SQL statement variable.
 62872 */
 62873 SQLITE_API int sqlite3_bind_blob(
 62874   sqlite3_stmt *pStmt, 
 62875   int i, 
 62876   const void *zData, 
 62877   int nData, 
 62878   void (*xDel)(void*)
 62879 ){
 62880   return bindText(pStmt, i, zData, nData, xDel, 0);
 62882 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
 62883   int rc;
 62884   Vdbe *p = (Vdbe *)pStmt;
 62885   rc = vdbeUnbind(p, i);
 62886   if( rc==SQLITE_OK ){
 62887     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
 62888     sqlite3_mutex_leave(p->db->mutex);
 62890   return rc;
 62892 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
 62893   return sqlite3_bind_int64(p, i, (i64)iValue);
 62895 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
 62896   int rc;
 62897   Vdbe *p = (Vdbe *)pStmt;
 62898   rc = vdbeUnbind(p, i);
 62899   if( rc==SQLITE_OK ){
 62900     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
 62901     sqlite3_mutex_leave(p->db->mutex);
 62903   return rc;
 62905 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
 62906   int rc;
 62907   Vdbe *p = (Vdbe*)pStmt;
 62908   rc = vdbeUnbind(p, i);
 62909   if( rc==SQLITE_OK ){
 62910     sqlite3_mutex_leave(p->db->mutex);
 62912   return rc;
 62914 SQLITE_API int sqlite3_bind_text( 
 62915   sqlite3_stmt *pStmt, 
 62916   int i, 
 62917   const char *zData, 
 62918   int nData, 
 62919   void (*xDel)(void*)
 62920 ){
 62921   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
 62923 #ifndef SQLITE_OMIT_UTF16
 62924 SQLITE_API int sqlite3_bind_text16(
 62925   sqlite3_stmt *pStmt, 
 62926   int i, 
 62927   const void *zData, 
 62928   int nData, 
 62929   void (*xDel)(void*)
 62930 ){
 62931   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
 62933 #endif /* SQLITE_OMIT_UTF16 */
 62934 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
 62935   int rc;
 62936   switch( pValue->type ){
 62937     case SQLITE_INTEGER: {
 62938       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
 62939       break;
 62941     case SQLITE_FLOAT: {
 62942       rc = sqlite3_bind_double(pStmt, i, pValue->r);
 62943       break;
 62945     case SQLITE_BLOB: {
 62946       if( pValue->flags & MEM_Zero ){
 62947         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
 62948       }else{
 62949         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
 62951       break;
 62953     case SQLITE_TEXT: {
 62954       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
 62955                               pValue->enc);
 62956       break;
 62958     default: {
 62959       rc = sqlite3_bind_null(pStmt, i);
 62960       break;
 62963   return rc;
 62965 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
 62966   int rc;
 62967   Vdbe *p = (Vdbe *)pStmt;
 62968   rc = vdbeUnbind(p, i);
 62969   if( rc==SQLITE_OK ){
 62970     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
 62971     sqlite3_mutex_leave(p->db->mutex);
 62973   return rc;
 62976 /*
 62977 ** Return the number of wildcards that can be potentially bound to.
 62978 ** This routine is added to support DBD::SQLite.  
 62979 */
 62980 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
 62981   Vdbe *p = (Vdbe*)pStmt;
 62982   return p ? p->nVar : 0;
 62985 /*
 62986 ** Return the name of a wildcard parameter.  Return NULL if the index
 62987 ** is out of range or if the wildcard is unnamed.
 62988 **
 62989 ** The result is always UTF-8.
 62990 */
 62991 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
 62992   Vdbe *p = (Vdbe*)pStmt;
 62993   if( p==0 || i<1 || i>p->nzVar ){
 62994     return 0;
 62996   return p->azVar[i-1];
 62999 /*
 63000 ** Given a wildcard parameter name, return the index of the variable
 63001 ** with that name.  If there is no variable with the given name,
 63002 ** return 0.
 63003 */
 63004 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
 63005   int i;
 63006   if( p==0 ){
 63007     return 0;
 63009   if( zName ){
 63010     for(i=0; i<p->nzVar; i++){
 63011       const char *z = p->azVar[i];
 63012       if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
 63013         return i+1;
 63017   return 0;
 63019 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
 63020   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
 63023 /*
 63024 ** Transfer all bindings from the first statement over to the second.
 63025 */
 63026 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
 63027   Vdbe *pFrom = (Vdbe*)pFromStmt;
 63028   Vdbe *pTo = (Vdbe*)pToStmt;
 63029   int i;
 63030   assert( pTo->db==pFrom->db );
 63031   assert( pTo->nVar==pFrom->nVar );
 63032   sqlite3_mutex_enter(pTo->db->mutex);
 63033   for(i=0; i<pFrom->nVar; i++){
 63034     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
 63036   sqlite3_mutex_leave(pTo->db->mutex);
 63037   return SQLITE_OK;
 63040 #ifndef SQLITE_OMIT_DEPRECATED
 63041 /*
 63042 ** Deprecated external interface.  Internal/core SQLite code
 63043 ** should call sqlite3TransferBindings.
 63044 **
 63045 ** Is is misuse to call this routine with statements from different
 63046 ** database connections.  But as this is a deprecated interface, we
 63047 ** will not bother to check for that condition.
 63048 **
 63049 ** If the two statements contain a different number of bindings, then
 63050 ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
 63051 ** SQLITE_OK is returned.
 63052 */
 63053 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
 63054   Vdbe *pFrom = (Vdbe*)pFromStmt;
 63055   Vdbe *pTo = (Vdbe*)pToStmt;
 63056   if( pFrom->nVar!=pTo->nVar ){
 63057     return SQLITE_ERROR;
 63059   if( pTo->isPrepareV2 && pTo->expmask ){
 63060     pTo->expired = 1;
 63062   if( pFrom->isPrepareV2 && pFrom->expmask ){
 63063     pFrom->expired = 1;
 63065   return sqlite3TransferBindings(pFromStmt, pToStmt);
 63067 #endif
 63069 /*
 63070 ** Return the sqlite3* database handle to which the prepared statement given
 63071 ** in the argument belongs.  This is the same database handle that was
 63072 ** the first argument to the sqlite3_prepare() that was used to create
 63073 ** the statement in the first place.
 63074 */
 63075 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
 63076   return pStmt ? ((Vdbe*)pStmt)->db : 0;
 63079 /*
 63080 ** Return true if the prepared statement is guaranteed to not modify the
 63081 ** database.
 63082 */
 63083 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
 63084   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
 63087 /*
 63088 ** Return true if the prepared statement is in need of being reset.
 63089 */
 63090 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
 63091   Vdbe *v = (Vdbe*)pStmt;
 63092   return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
 63095 /*
 63096 ** Return a pointer to the next prepared statement after pStmt associated
 63097 ** with database connection pDb.  If pStmt is NULL, return the first
 63098 ** prepared statement for the database connection.  Return NULL if there
 63099 ** are no more.
 63100 */
 63101 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
 63102   sqlite3_stmt *pNext;
 63103   sqlite3_mutex_enter(pDb->mutex);
 63104   if( pStmt==0 ){
 63105     pNext = (sqlite3_stmt*)pDb->pVdbe;
 63106   }else{
 63107     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
 63109   sqlite3_mutex_leave(pDb->mutex);
 63110   return pNext;
 63113 /*
 63114 ** Return the value of a status counter for a prepared statement
 63115 */
 63116 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
 63117   Vdbe *pVdbe = (Vdbe*)pStmt;
 63118   int v = pVdbe->aCounter[op-1];
 63119   if( resetFlag ) pVdbe->aCounter[op-1] = 0;
 63120   return v;
 63123 /************** End of vdbeapi.c *********************************************/
 63124 /************** Begin file vdbetrace.c ***************************************/
 63125 /*
 63126 ** 2009 November 25
 63127 **
 63128 ** The author disclaims copyright to this source code.  In place of
 63129 ** a legal notice, here is a blessing:
 63130 **
 63131 **    May you do good and not evil.
 63132 **    May you find forgiveness for yourself and forgive others.
 63133 **    May you share freely, never taking more than you give.
 63134 **
 63135 *************************************************************************
 63136 **
 63137 ** This file contains code used to insert the values of host parameters
 63138 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
 63139 **
 63140 ** The Vdbe parse-tree explainer is also found here.
 63141 */
 63143 #ifndef SQLITE_OMIT_TRACE
 63145 /*
 63146 ** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
 63147 ** bytes in this text up to but excluding the first character in
 63148 ** a host parameter.  If the text contains no host parameters, return
 63149 ** the total number of bytes in the text.
 63150 */
 63151 static int findNextHostParameter(const char *zSql, int *pnToken){
 63152   int tokenType;
 63153   int nTotal = 0;
 63154   int n;
 63156   *pnToken = 0;
 63157   while( zSql[0] ){
 63158     n = sqlite3GetToken((u8*)zSql, &tokenType);
 63159     assert( n>0 && tokenType!=TK_ILLEGAL );
 63160     if( tokenType==TK_VARIABLE ){
 63161       *pnToken = n;
 63162       break;
 63164     nTotal += n;
 63165     zSql += n;
 63167   return nTotal;
 63170 /*
 63171 ** This function returns a pointer to a nul-terminated string in memory
 63172 ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
 63173 ** string contains a copy of zRawSql but with host parameters expanded to 
 63174 ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1, 
 63175 ** then the returned string holds a copy of zRawSql with "-- " prepended
 63176 ** to each line of text.
 63177 **
 63178 ** The calling function is responsible for making sure the memory returned
 63179 ** is eventually freed.
 63180 **
 63181 ** ALGORITHM:  Scan the input string looking for host parameters in any of
 63182 ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
 63183 ** string literals, quoted identifier names, and comments.  For text forms,
 63184 ** the host parameter index is found by scanning the perpared
 63185 ** statement for the corresponding OP_Variable opcode.  Once the host
 63186 ** parameter index is known, locate the value in p->aVar[].  Then render
 63187 ** the value as a literal in place of the host parameter name.
 63188 */
 63189 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
 63190   Vdbe *p,                 /* The prepared statement being evaluated */
 63191   const char *zRawSql      /* Raw text of the SQL statement */
 63192 ){
 63193   sqlite3 *db;             /* The database connection */
 63194   int idx = 0;             /* Index of a host parameter */
 63195   int nextIndex = 1;       /* Index of next ? host parameter */
 63196   int n;                   /* Length of a token prefix */
 63197   int nToken;              /* Length of the parameter token */
 63198   int i;                   /* Loop counter */
 63199   Mem *pVar;               /* Value of a host parameter */
 63200   StrAccum out;            /* Accumulate the output here */
 63201   char zBase[100];         /* Initial working space */
 63203   db = p->db;
 63204   sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
 63205                       db->aLimit[SQLITE_LIMIT_LENGTH]);
 63206   out.db = db;
 63207   if( db->vdbeExecCnt>1 ){
 63208     while( *zRawSql ){
 63209       const char *zStart = zRawSql;
 63210       while( *(zRawSql++)!='\n' && *zRawSql );
 63211       sqlite3StrAccumAppend(&out, "-- ", 3);
 63212       sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
 63214   }else{
 63215     while( zRawSql[0] ){
 63216       n = findNextHostParameter(zRawSql, &nToken);
 63217       assert( n>0 );
 63218       sqlite3StrAccumAppend(&out, zRawSql, n);
 63219       zRawSql += n;
 63220       assert( zRawSql[0] || nToken==0 );
 63221       if( nToken==0 ) break;
 63222       if( zRawSql[0]=='?' ){
 63223         if( nToken>1 ){
 63224           assert( sqlite3Isdigit(zRawSql[1]) );
 63225           sqlite3GetInt32(&zRawSql[1], &idx);
 63226         }else{
 63227           idx = nextIndex;
 63229       }else{
 63230         assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
 63231         testcase( zRawSql[0]==':' );
 63232         testcase( zRawSql[0]=='$' );
 63233         testcase( zRawSql[0]=='@' );
 63234         idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
 63235         assert( idx>0 );
 63237       zRawSql += nToken;
 63238       nextIndex = idx + 1;
 63239       assert( idx>0 && idx<=p->nVar );
 63240       pVar = &p->aVar[idx-1];
 63241       if( pVar->flags & MEM_Null ){
 63242         sqlite3StrAccumAppend(&out, "NULL", 4);
 63243       }else if( pVar->flags & MEM_Int ){
 63244         sqlite3XPrintf(&out, "%lld", pVar->u.i);
 63245       }else if( pVar->flags & MEM_Real ){
 63246         sqlite3XPrintf(&out, "%!.15g", pVar->r);
 63247       }else if( pVar->flags & MEM_Str ){
 63248 #ifndef SQLITE_OMIT_UTF16
 63249         u8 enc = ENC(db);
 63250         if( enc!=SQLITE_UTF8 ){
 63251           Mem utf8;
 63252           memset(&utf8, 0, sizeof(utf8));
 63253           utf8.db = db;
 63254           sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
 63255           sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
 63256           sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
 63257           sqlite3VdbeMemRelease(&utf8);
 63258         }else
 63259 #endif
 63261           sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
 63263       }else if( pVar->flags & MEM_Zero ){
 63264         sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
 63265       }else{
 63266         assert( pVar->flags & MEM_Blob );
 63267         sqlite3StrAccumAppend(&out, "x'", 2);
 63268         for(i=0; i<pVar->n; i++){
 63269           sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
 63271         sqlite3StrAccumAppend(&out, "'", 1);
 63275   return sqlite3StrAccumFinish(&out);
 63278 #endif /* #ifndef SQLITE_OMIT_TRACE */
 63280 /*****************************************************************************
 63281 ** The following code implements the data-structure explaining logic
 63282 ** for the Vdbe.
 63283 */
 63285 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 63287 /*
 63288 ** Allocate a new Explain object
 63289 */
 63290 SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
 63291   if( pVdbe ){
 63292     Explain *p;
 63293     sqlite3BeginBenignMalloc();
 63294     p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
 63295     if( p ){
 63296       p->pVdbe = pVdbe;
 63297       sqlite3_free(pVdbe->pExplain);
 63298       pVdbe->pExplain = p;
 63299       sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
 63300                           SQLITE_MAX_LENGTH);
 63301       p->str.useMalloc = 2;
 63302     }else{
 63303       sqlite3EndBenignMalloc();
 63308 /*
 63309 ** Return true if the Explain ends with a new-line.
 63310 */
 63311 static int endsWithNL(Explain *p){
 63312   return p && p->str.zText && p->str.nChar
 63313            && p->str.zText[p->str.nChar-1]=='\n';
 63316 /*
 63317 ** Append text to the indentation
 63318 */
 63319 SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
 63320   Explain *p;
 63321   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
 63322     va_list ap;
 63323     if( p->nIndent && endsWithNL(p) ){
 63324       int n = p->nIndent;
 63325       if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
 63326       sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
 63328     va_start(ap, zFormat);
 63329     sqlite3VXPrintf(&p->str, 1, zFormat, ap);
 63330     va_end(ap);
 63334 /*
 63335 ** Append a '\n' if there is not already one.
 63336 */
 63337 SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
 63338   Explain *p;
 63339   if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
 63340     sqlite3StrAccumAppend(&p->str, "\n", 1);
 63344 /*
 63345 ** Push a new indentation level.  Subsequent lines will be indented
 63346 ** so that they begin at the current cursor position.
 63347 */
 63348 SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
 63349   Explain *p;
 63350   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
 63351     if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
 63352       const char *z = p->str.zText;
 63353       int i = p->str.nChar-1;
 63354       int x;
 63355       while( i>=0 && z[i]!='\n' ){ i--; }
 63356       x = (p->str.nChar - 1) - i;
 63357       if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
 63358         x = p->aIndent[p->nIndent-1];
 63360       p->aIndent[p->nIndent] = x;
 63362     p->nIndent++;
 63366 /*
 63367 ** Pop the indentation stack by one level.
 63368 */
 63369 SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
 63370   if( p && p->pExplain ) p->pExplain->nIndent--;
 63373 /*
 63374 ** Free the indentation structure
 63375 */
 63376 SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
 63377   if( pVdbe && pVdbe->pExplain ){
 63378     sqlite3_free(pVdbe->zExplain);
 63379     sqlite3ExplainNL(pVdbe);
 63380     pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
 63381     sqlite3_free(pVdbe->pExplain);
 63382     pVdbe->pExplain = 0;
 63383     sqlite3EndBenignMalloc();
 63387 /*
 63388 ** Return the explanation of a virtual machine.
 63389 */
 63390 SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
 63391   return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
 63393 #endif /* defined(SQLITE_DEBUG) */
 63395 /************** End of vdbetrace.c *******************************************/
 63396 /************** Begin file vdbe.c ********************************************/
 63397 /*
 63398 ** 2001 September 15
 63399 **
 63400 ** The author disclaims copyright to this source code.  In place of
 63401 ** a legal notice, here is a blessing:
 63402 **
 63403 **    May you do good and not evil.
 63404 **    May you find forgiveness for yourself and forgive others.
 63405 **    May you share freely, never taking more than you give.
 63406 **
 63407 *************************************************************************
 63408 ** The code in this file implements execution method of the 
 63409 ** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
 63410 ** handles housekeeping details such as creating and deleting
 63411 ** VDBE instances.  This file is solely interested in executing
 63412 ** the VDBE program.
 63413 **
 63414 ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
 63415 ** to a VDBE.
 63416 **
 63417 ** The SQL parser generates a program which is then executed by
 63418 ** the VDBE to do the work of the SQL statement.  VDBE programs are 
 63419 ** similar in form to assembly language.  The program consists of
 63420 ** a linear sequence of operations.  Each operation has an opcode 
 63421 ** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4 
 63422 ** is a null-terminated string.  Operand P5 is an unsigned character.
 63423 ** Few opcodes use all 5 operands.
 63424 **
 63425 ** Computation results are stored on a set of registers numbered beginning
 63426 ** with 1 and going up to Vdbe.nMem.  Each register can store
 63427 ** either an integer, a null-terminated string, a floating point
 63428 ** number, or the SQL "NULL" value.  An implicit conversion from one
 63429 ** type to the other occurs as necessary.
 63430 ** 
 63431 ** Most of the code in this file is taken up by the sqlite3VdbeExec()
 63432 ** function which does the work of interpreting a VDBE program.
 63433 ** But other routines are also provided to help in building up
 63434 ** a program instruction by instruction.
 63435 **
 63436 ** Various scripts scan this source file in order to generate HTML
 63437 ** documentation, headers files, or other derived files.  The formatting
 63438 ** of the code in this file is, therefore, important.  See other comments
 63439 ** in this file for details.  If in doubt, do not deviate from existing
 63440 ** commenting and indentation practices when changing or adding code.
 63441 */
 63443 /*
 63444 ** Invoke this macro on memory cells just prior to changing the
 63445 ** value of the cell.  This macro verifies that shallow copies are
 63446 ** not misused.
 63447 */
 63448 #ifdef SQLITE_DEBUG
 63449 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
 63450 #else
 63451 # define memAboutToChange(P,M)
 63452 #endif
 63454 /*
 63455 ** The following global variable is incremented every time a cursor
 63456 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
 63457 ** procedures use this information to make sure that indices are
 63458 ** working correctly.  This variable has no function other than to
 63459 ** help verify the correct operation of the library.
 63460 */
 63461 #ifdef SQLITE_TEST
 63462 SQLITE_API int sqlite3_search_count = 0;
 63463 #endif
 63465 /*
 63466 ** When this global variable is positive, it gets decremented once before
 63467 ** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
 63468 ** field of the sqlite3 structure is set in order to simulate an interrupt.
 63469 **
 63470 ** This facility is used for testing purposes only.  It does not function
 63471 ** in an ordinary build.
 63472 */
 63473 #ifdef SQLITE_TEST
 63474 SQLITE_API int sqlite3_interrupt_count = 0;
 63475 #endif
 63477 /*
 63478 ** The next global variable is incremented each type the OP_Sort opcode
 63479 ** is executed.  The test procedures use this information to make sure that
 63480 ** sorting is occurring or not occurring at appropriate times.   This variable
 63481 ** has no function other than to help verify the correct operation of the
 63482 ** library.
 63483 */
 63484 #ifdef SQLITE_TEST
 63485 SQLITE_API int sqlite3_sort_count = 0;
 63486 #endif
 63488 /*
 63489 ** The next global variable records the size of the largest MEM_Blob
 63490 ** or MEM_Str that has been used by a VDBE opcode.  The test procedures
 63491 ** use this information to make sure that the zero-blob functionality
 63492 ** is working correctly.   This variable has no function other than to
 63493 ** help verify the correct operation of the library.
 63494 */
 63495 #ifdef SQLITE_TEST
 63496 SQLITE_API int sqlite3_max_blobsize = 0;
 63497 static void updateMaxBlobsize(Mem *p){
 63498   if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
 63499     sqlite3_max_blobsize = p->n;
 63502 #endif
 63504 /*
 63505 ** The next global variable is incremented each type the OP_Found opcode
 63506 ** is executed. This is used to test whether or not the foreign key
 63507 ** operation implemented using OP_FkIsZero is working. This variable
 63508 ** has no function other than to help verify the correct operation of the
 63509 ** library.
 63510 */
 63511 #ifdef SQLITE_TEST
 63512 SQLITE_API int sqlite3_found_count = 0;
 63513 #endif
 63515 /*
 63516 ** Test a register to see if it exceeds the current maximum blob size.
 63517 ** If it does, record the new maximum blob size.
 63518 */
 63519 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
 63520 # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
 63521 #else
 63522 # define UPDATE_MAX_BLOBSIZE(P)
 63523 #endif
 63525 /*
 63526 ** Convert the given register into a string if it isn't one
 63527 ** already. Return non-zero if a malloc() fails.
 63528 */
 63529 #define Stringify(P, enc) \
 63530    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
 63531      { goto no_mem; }
 63533 /*
 63534 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
 63535 ** a pointer to a dynamically allocated string where some other entity
 63536 ** is responsible for deallocating that string.  Because the register
 63537 ** does not control the string, it might be deleted without the register
 63538 ** knowing it.
 63539 **
 63540 ** This routine converts an ephemeral string into a dynamically allocated
 63541 ** string that the register itself controls.  In other words, it
 63542 ** converts an MEM_Ephem string into an MEM_Dyn string.
 63543 */
 63544 #define Deephemeralize(P) \
 63545    if( ((P)->flags&MEM_Ephem)!=0 \
 63546        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
 63548 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
 63549 #ifdef SQLITE_OMIT_MERGE_SORT
 63550 # define isSorter(x) 0
 63551 #else
 63552 # define isSorter(x) ((x)->pSorter!=0)
 63553 #endif
 63555 /*
 63556 ** Argument pMem points at a register that will be passed to a
 63557 ** user-defined function or returned to the user as the result of a query.
 63558 ** This routine sets the pMem->type variable used by the sqlite3_value_*() 
 63559 ** routines.
 63560 */
 63561 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
 63562   int flags = pMem->flags;
 63563   if( flags & MEM_Null ){
 63564     pMem->type = SQLITE_NULL;
 63566   else if( flags & MEM_Int ){
 63567     pMem->type = SQLITE_INTEGER;
 63569   else if( flags & MEM_Real ){
 63570     pMem->type = SQLITE_FLOAT;
 63572   else if( flags & MEM_Str ){
 63573     pMem->type = SQLITE_TEXT;
 63574   }else{
 63575     pMem->type = SQLITE_BLOB;
 63579 /*
 63580 ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
 63581 ** if we run out of memory.
 63582 */
 63583 static VdbeCursor *allocateCursor(
 63584   Vdbe *p,              /* The virtual machine */
 63585   int iCur,             /* Index of the new VdbeCursor */
 63586   int nField,           /* Number of fields in the table or index */
 63587   int iDb,              /* Database the cursor belongs to, or -1 */
 63588   int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
 63589 ){
 63590   /* Find the memory cell that will be used to store the blob of memory
 63591   ** required for this VdbeCursor structure. It is convenient to use a 
 63592   ** vdbe memory cell to manage the memory allocation required for a
 63593   ** VdbeCursor structure for the following reasons:
 63594   **
 63595   **   * Sometimes cursor numbers are used for a couple of different
 63596   **     purposes in a vdbe program. The different uses might require
 63597   **     different sized allocations. Memory cells provide growable
 63598   **     allocations.
 63599   **
 63600   **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
 63601   **     be freed lazily via the sqlite3_release_memory() API. This
 63602   **     minimizes the number of malloc calls made by the system.
 63603   **
 63604   ** Memory cells for cursors are allocated at the top of the address
 63605   ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
 63606   ** cursor 1 is managed by memory cell (p->nMem-1), etc.
 63607   */
 63608   Mem *pMem = &p->aMem[p->nMem-iCur];
 63610   int nByte;
 63611   VdbeCursor *pCx = 0;
 63612   nByte = 
 63613       ROUND8(sizeof(VdbeCursor)) + 
 63614       (isBtreeCursor?sqlite3BtreeCursorSize():0) + 
 63615       2*nField*sizeof(u32);
 63617   assert( iCur<p->nCursor );
 63618   if( p->apCsr[iCur] ){
 63619     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
 63620     p->apCsr[iCur] = 0;
 63622   if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
 63623     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
 63624     memset(pCx, 0, sizeof(VdbeCursor));
 63625     pCx->iDb = iDb;
 63626     pCx->nField = nField;
 63627     if( nField ){
 63628       pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
 63630     if( isBtreeCursor ){
 63631       pCx->pCursor = (BtCursor*)
 63632           &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
 63633       sqlite3BtreeCursorZero(pCx->pCursor);
 63636   return pCx;
 63639 /*
 63640 ** Try to convert a value into a numeric representation if we can
 63641 ** do so without loss of information.  In other words, if the string
 63642 ** looks like a number, convert it into a number.  If it does not
 63643 ** look like a number, leave it alone.
 63644 */
 63645 static void applyNumericAffinity(Mem *pRec){
 63646   if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
 63647     double rValue;
 63648     i64 iValue;
 63649     u8 enc = pRec->enc;
 63650     if( (pRec->flags&MEM_Str)==0 ) return;
 63651     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
 63652     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
 63653       pRec->u.i = iValue;
 63654       pRec->flags |= MEM_Int;
 63655     }else{
 63656       pRec->r = rValue;
 63657       pRec->flags |= MEM_Real;
 63662 /*
 63663 ** Processing is determine by the affinity parameter:
 63664 **
 63665 ** SQLITE_AFF_INTEGER:
 63666 ** SQLITE_AFF_REAL:
 63667 ** SQLITE_AFF_NUMERIC:
 63668 **    Try to convert pRec to an integer representation or a 
 63669 **    floating-point representation if an integer representation
 63670 **    is not possible.  Note that the integer representation is
 63671 **    always preferred, even if the affinity is REAL, because
 63672 **    an integer representation is more space efficient on disk.
 63673 **
 63674 ** SQLITE_AFF_TEXT:
 63675 **    Convert pRec to a text representation.
 63676 **
 63677 ** SQLITE_AFF_NONE:
 63678 **    No-op.  pRec is unchanged.
 63679 */
 63680 static void applyAffinity(
 63681   Mem *pRec,          /* The value to apply affinity to */
 63682   char affinity,      /* The affinity to be applied */
 63683   u8 enc              /* Use this text encoding */
 63684 ){
 63685   if( affinity==SQLITE_AFF_TEXT ){
 63686     /* Only attempt the conversion to TEXT if there is an integer or real
 63687     ** representation (blob and NULL do not get converted) but no string
 63688     ** representation.
 63689     */
 63690     if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
 63691       sqlite3VdbeMemStringify(pRec, enc);
 63693     pRec->flags &= ~(MEM_Real|MEM_Int);
 63694   }else if( affinity!=SQLITE_AFF_NONE ){
 63695     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
 63696              || affinity==SQLITE_AFF_NUMERIC );
 63697     applyNumericAffinity(pRec);
 63698     if( pRec->flags & MEM_Real ){
 63699       sqlite3VdbeIntegerAffinity(pRec);
 63704 /*
 63705 ** Try to convert the type of a function argument or a result column
 63706 ** into a numeric representation.  Use either INTEGER or REAL whichever
 63707 ** is appropriate.  But only do the conversion if it is possible without
 63708 ** loss of information and return the revised type of the argument.
 63709 */
 63710 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
 63711   Mem *pMem = (Mem*)pVal;
 63712   if( pMem->type==SQLITE_TEXT ){
 63713     applyNumericAffinity(pMem);
 63714     sqlite3VdbeMemStoreType(pMem);
 63716   return pMem->type;
 63719 /*
 63720 ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
 63721 ** not the internal Mem* type.
 63722 */
 63723 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
 63724   sqlite3_value *pVal, 
 63725   u8 affinity, 
 63726   u8 enc
 63727 ){
 63728   applyAffinity((Mem *)pVal, affinity, enc);
 63731 #ifdef SQLITE_DEBUG
 63732 /*
 63733 ** Write a nice string representation of the contents of cell pMem
 63734 ** into buffer zBuf, length nBuf.
 63735 */
 63736 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
 63737   char *zCsr = zBuf;
 63738   int f = pMem->flags;
 63740   static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
 63742   if( f&MEM_Blob ){
 63743     int i;
 63744     char c;
 63745     if( f & MEM_Dyn ){
 63746       c = 'z';
 63747       assert( (f & (MEM_Static|MEM_Ephem))==0 );
 63748     }else if( f & MEM_Static ){
 63749       c = 't';
 63750       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
 63751     }else if( f & MEM_Ephem ){
 63752       c = 'e';
 63753       assert( (f & (MEM_Static|MEM_Dyn))==0 );
 63754     }else{
 63755       c = 's';
 63758     sqlite3_snprintf(100, zCsr, "%c", c);
 63759     zCsr += sqlite3Strlen30(zCsr);
 63760     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
 63761     zCsr += sqlite3Strlen30(zCsr);
 63762     for(i=0; i<16 && i<pMem->n; i++){
 63763       sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
 63764       zCsr += sqlite3Strlen30(zCsr);
 63766     for(i=0; i<16 && i<pMem->n; i++){
 63767       char z = pMem->z[i];
 63768       if( z<32 || z>126 ) *zCsr++ = '.';
 63769       else *zCsr++ = z;
 63772     sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
 63773     zCsr += sqlite3Strlen30(zCsr);
 63774     if( f & MEM_Zero ){
 63775       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
 63776       zCsr += sqlite3Strlen30(zCsr);
 63778     *zCsr = '\0';
 63779   }else if( f & MEM_Str ){
 63780     int j, k;
 63781     zBuf[0] = ' ';
 63782     if( f & MEM_Dyn ){
 63783       zBuf[1] = 'z';
 63784       assert( (f & (MEM_Static|MEM_Ephem))==0 );
 63785     }else if( f & MEM_Static ){
 63786       zBuf[1] = 't';
 63787       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
 63788     }else if( f & MEM_Ephem ){
 63789       zBuf[1] = 'e';
 63790       assert( (f & (MEM_Static|MEM_Dyn))==0 );
 63791     }else{
 63792       zBuf[1] = 's';
 63794     k = 2;
 63795     sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
 63796     k += sqlite3Strlen30(&zBuf[k]);
 63797     zBuf[k++] = '[';
 63798     for(j=0; j<15 && j<pMem->n; j++){
 63799       u8 c = pMem->z[j];
 63800       if( c>=0x20 && c<0x7f ){
 63801         zBuf[k++] = c;
 63802       }else{
 63803         zBuf[k++] = '.';
 63806     zBuf[k++] = ']';
 63807     sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
 63808     k += sqlite3Strlen30(&zBuf[k]);
 63809     zBuf[k++] = 0;
 63812 #endif
 63814 #ifdef SQLITE_DEBUG
 63815 /*
 63816 ** Print the value of a register for tracing purposes:
 63817 */
 63818 static void memTracePrint(FILE *out, Mem *p){
 63819   if( p->flags & MEM_Invalid ){
 63820     fprintf(out, " undefined");
 63821   }else if( p->flags & MEM_Null ){
 63822     fprintf(out, " NULL");
 63823   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
 63824     fprintf(out, " si:%lld", p->u.i);
 63825   }else if( p->flags & MEM_Int ){
 63826     fprintf(out, " i:%lld", p->u.i);
 63827 #ifndef SQLITE_OMIT_FLOATING_POINT
 63828   }else if( p->flags & MEM_Real ){
 63829     fprintf(out, " r:%g", p->r);
 63830 #endif
 63831   }else if( p->flags & MEM_RowSet ){
 63832     fprintf(out, " (rowset)");
 63833   }else{
 63834     char zBuf[200];
 63835     sqlite3VdbeMemPrettyPrint(p, zBuf);
 63836     fprintf(out, " ");
 63837     fprintf(out, "%s", zBuf);
 63840 static void registerTrace(FILE *out, int iReg, Mem *p){
 63841   fprintf(out, "REG[%d] = ", iReg);
 63842   memTracePrint(out, p);
 63843   fprintf(out, "\n");
 63845 #endif
 63847 #ifdef SQLITE_DEBUG
 63848 #  define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
 63849 #else
 63850 #  define REGISTER_TRACE(R,M)
 63851 #endif
 63854 #ifdef VDBE_PROFILE
 63856 /* 
 63857 ** hwtime.h contains inline assembler code for implementing 
 63858 ** high-performance timing routines.
 63859 */
 63860 /************** Include hwtime.h in the middle of vdbe.c *********************/
 63861 /************** Begin file hwtime.h ******************************************/
 63862 /*
 63863 ** 2008 May 27
 63864 **
 63865 ** The author disclaims copyright to this source code.  In place of
 63866 ** a legal notice, here is a blessing:
 63867 **
 63868 **    May you do good and not evil.
 63869 **    May you find forgiveness for yourself and forgive others.
 63870 **    May you share freely, never taking more than you give.
 63871 **
 63872 ******************************************************************************
 63873 **
 63874 ** This file contains inline asm code for retrieving "high-performance"
 63875 ** counters for x86 class CPUs.
 63876 */
 63877 #ifndef _HWTIME_H_
 63878 #define _HWTIME_H_
 63880 /*
 63881 ** The following routine only works on pentium-class (or newer) processors.
 63882 ** It uses the RDTSC opcode to read the cycle count value out of the
 63883 ** processor and returns that value.  This can be used for high-res
 63884 ** profiling.
 63885 */
 63886 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
 63887       (defined(i386) || defined(__i386__) || defined(_M_IX86))
 63889   #if defined(__GNUC__)
 63891   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 63892      unsigned int lo, hi;
 63893      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 63894      return (sqlite_uint64)hi << 32 | lo;
 63897   #elif defined(_MSC_VER)
 63899   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 63900      __asm {
 63901         rdtsc
 63902         ret       ; return value at EDX:EAX
 63906   #endif
 63908 #elif (defined(__GNUC__) && defined(__x86_64__))
 63910   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 63911       unsigned long val;
 63912       __asm__ __volatile__ ("rdtsc" : "=A" (val));
 63913       return val;
 63916 #elif (defined(__GNUC__) && defined(__ppc__))
 63918   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 63919       unsigned long long retval;
 63920       unsigned long junk;
 63921       __asm__ __volatile__ ("\n\
 63922           1:      mftbu   %1\n\
 63923                   mftb    %L0\n\
 63924                   mftbu   %0\n\
 63925                   cmpw    %0,%1\n\
 63926                   bne     1b"
 63927                   : "=r" (retval), "=r" (junk));
 63928       return retval;
 63931 #else
 63933   #error Need implementation of sqlite3Hwtime() for your platform.
 63935   /*
 63936   ** To compile without implementing sqlite3Hwtime() for your platform,
 63937   ** you can remove the above #error and use the following
 63938   ** stub function.  You will lose timing support for many
 63939   ** of the debugging and testing utilities, but it should at
 63940   ** least compile and run.
 63941   */
 63942 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 63944 #endif
 63946 #endif /* !defined(_HWTIME_H_) */
 63948 /************** End of hwtime.h **********************************************/
 63949 /************** Continuing where we left off in vdbe.c ***********************/
 63951 #endif
 63953 /*
 63954 ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
 63955 ** sqlite3_interrupt() routine has been called.  If it has been, then
 63956 ** processing of the VDBE program is interrupted.
 63957 **
 63958 ** This macro added to every instruction that does a jump in order to
 63959 ** implement a loop.  This test used to be on every single instruction,
 63960 ** but that meant we more testing than we needed.  By only testing the
 63961 ** flag on jump instructions, we get a (small) speed improvement.
 63962 */
 63963 #define CHECK_FOR_INTERRUPT \
 63964    if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 63967 #ifndef NDEBUG
 63968 /*
 63969 ** This function is only called from within an assert() expression. It
 63970 ** checks that the sqlite3.nTransaction variable is correctly set to
 63971 ** the number of non-transaction savepoints currently in the 
 63972 ** linked list starting at sqlite3.pSavepoint.
 63973 ** 
 63974 ** Usage:
 63975 **
 63976 **     assert( checkSavepointCount(db) );
 63977 */
 63978 static int checkSavepointCount(sqlite3 *db){
 63979   int n = 0;
 63980   Savepoint *p;
 63981   for(p=db->pSavepoint; p; p=p->pNext) n++;
 63982   assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
 63983   return 1;
 63985 #endif
 63987 /*
 63988 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
 63989 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
 63990 ** in memory obtained from sqlite3DbMalloc).
 63991 */
 63992 static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
 63993   sqlite3 *db = p->db;
 63994   sqlite3DbFree(db, p->zErrMsg);
 63995   p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
 63996   sqlite3_free(pVtab->zErrMsg);
 63997   pVtab->zErrMsg = 0;
 64001 /*
 64002 ** Execute as much of a VDBE program as we can then return.
 64003 **
 64004 ** sqlite3VdbeMakeReady() must be called before this routine in order to
 64005 ** close the program with a final OP_Halt and to set up the callbacks
 64006 ** and the error message pointer.
 64007 **
 64008 ** Whenever a row or result data is available, this routine will either
 64009 ** invoke the result callback (if there is one) or return with
 64010 ** SQLITE_ROW.
 64011 **
 64012 ** If an attempt is made to open a locked database, then this routine
 64013 ** will either invoke the busy callback (if there is one) or it will
 64014 ** return SQLITE_BUSY.
 64015 **
 64016 ** If an error occurs, an error message is written to memory obtained
 64017 ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
 64018 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
 64019 **
 64020 ** If the callback ever returns non-zero, then the program exits
 64021 ** immediately.  There will be no error message but the p->rc field is
 64022 ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
 64023 **
 64024 ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
 64025 ** routine to return SQLITE_ERROR.
 64026 **
 64027 ** Other fatal errors return SQLITE_ERROR.
 64028 **
 64029 ** After this routine has finished, sqlite3VdbeFinalize() should be
 64030 ** used to clean up the mess that was left behind.
 64031 */
 64032 SQLITE_PRIVATE int sqlite3VdbeExec(
 64033   Vdbe *p                    /* The VDBE */
 64034 ){
 64035   int pc=0;                  /* The program counter */
 64036   Op *aOp = p->aOp;          /* Copy of p->aOp */
 64037   Op *pOp;                   /* Current operation */
 64038   int rc = SQLITE_OK;        /* Value to return */
 64039   sqlite3 *db = p->db;       /* The database */
 64040   u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
 64041   u8 encoding = ENC(db);     /* The database encoding */
 64042 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 64043   int checkProgress;         /* True if progress callbacks are enabled */
 64044   int nProgressOps = 0;      /* Opcodes executed since progress callback. */
 64045 #endif
 64046   Mem *aMem = p->aMem;       /* Copy of p->aMem */
 64047   Mem *pIn1 = 0;             /* 1st input operand */
 64048   Mem *pIn2 = 0;             /* 2nd input operand */
 64049   Mem *pIn3 = 0;             /* 3rd input operand */
 64050   Mem *pOut = 0;             /* Output operand */
 64051   int iCompare = 0;          /* Result of last OP_Compare operation */
 64052   int *aPermute = 0;         /* Permutation of columns for OP_Compare */
 64053   i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
 64054 #ifdef VDBE_PROFILE
 64055   u64 start;                 /* CPU clock count at start of opcode */
 64056   int origPc;                /* Program counter at start of opcode */
 64057 #endif
 64058   /********************************************************************
 64059   ** Automatically generated code
 64060   **
 64061   ** The following union is automatically generated by the
 64062   ** vdbe-compress.tcl script.  The purpose of this union is to
 64063   ** reduce the amount of stack space required by this function.
 64064   ** See comments in the vdbe-compress.tcl script for details.
 64065   */
 64066   union vdbeExecUnion {
 64067     struct OP_Yield_stack_vars {
 64068       int pcDest;
 64069     } aa;
 64070     struct OP_Null_stack_vars {
 64071       int cnt;
 64072       u16 nullFlag;
 64073     } ab;
 64074     struct OP_Variable_stack_vars {
 64075       Mem *pVar;       /* Value being transferred */
 64076     } ac;
 64077     struct OP_Move_stack_vars {
 64078       char *zMalloc;   /* Holding variable for allocated memory */
 64079       int n;           /* Number of registers left to copy */
 64080       int p1;          /* Register to copy from */
 64081       int p2;          /* Register to copy to */
 64082     } ad;
 64083     struct OP_Copy_stack_vars {
 64084       int n;
 64085     } ae;
 64086     struct OP_ResultRow_stack_vars {
 64087       Mem *pMem;
 64088       int i;
 64089     } af;
 64090     struct OP_Concat_stack_vars {
 64091       i64 nByte;
 64092     } ag;
 64093     struct OP_Remainder_stack_vars {
 64094       char bIntint;   /* Started out as two integer operands */
 64095       int flags;      /* Combined MEM_* flags from both inputs */
 64096       i64 iA;         /* Integer value of left operand */
 64097       i64 iB;         /* Integer value of right operand */
 64098       double rA;      /* Real value of left operand */
 64099       double rB;      /* Real value of right operand */
 64100     } ah;
 64101     struct OP_Function_stack_vars {
 64102       int i;
 64103       Mem *pArg;
 64104       sqlite3_context ctx;
 64105       sqlite3_value **apVal;
 64106       int n;
 64107     } ai;
 64108     struct OP_ShiftRight_stack_vars {
 64109       i64 iA;
 64110       u64 uA;
 64111       i64 iB;
 64112       u8 op;
 64113     } aj;
 64114     struct OP_Ge_stack_vars {
 64115       int res;            /* Result of the comparison of pIn1 against pIn3 */
 64116       char affinity;      /* Affinity to use for comparison */
 64117       u16 flags1;         /* Copy of initial value of pIn1->flags */
 64118       u16 flags3;         /* Copy of initial value of pIn3->flags */
 64119     } ak;
 64120     struct OP_Compare_stack_vars {
 64121       int n;
 64122       int i;
 64123       int p1;
 64124       int p2;
 64125       const KeyInfo *pKeyInfo;
 64126       int idx;
 64127       CollSeq *pColl;    /* Collating sequence to use on this term */
 64128       int bRev;          /* True for DESCENDING sort order */
 64129     } al;
 64130     struct OP_Or_stack_vars {
 64131       int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 64132       int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 64133     } am;
 64134     struct OP_IfNot_stack_vars {
 64135       int c;
 64136     } an;
 64137     struct OP_Column_stack_vars {
 64138       u32 payloadSize;   /* Number of bytes in the record */
 64139       i64 payloadSize64; /* Number of bytes in the record */
 64140       int p1;            /* P1 value of the opcode */
 64141       int p2;            /* column number to retrieve */
 64142       VdbeCursor *pC;    /* The VDBE cursor */
 64143       char *zRec;        /* Pointer to complete record-data */
 64144       BtCursor *pCrsr;   /* The BTree cursor */
 64145       u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
 64146       u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
 64147       int nField;        /* number of fields in the record */
 64148       int len;           /* The length of the serialized data for the column */
 64149       int i;             /* Loop counter */
 64150       char *zData;       /* Part of the record being decoded */
 64151       Mem *pDest;        /* Where to write the extracted value */
 64152       Mem sMem;          /* For storing the record being decoded */
 64153       u8 *zIdx;          /* Index into header */
 64154       u8 *zEndHdr;       /* Pointer to first byte after the header */
 64155       u32 offset;        /* Offset into the data */
 64156       u32 szField;       /* Number of bytes in the content of a field */
 64157       int szHdr;         /* Size of the header size field at start of record */
 64158       int avail;         /* Number of bytes of available data */
 64159       u32 t;             /* A type code from the record header */
 64160       Mem *pReg;         /* PseudoTable input register */
 64161     } ao;
 64162     struct OP_Affinity_stack_vars {
 64163       const char *zAffinity;   /* The affinity to be applied */
 64164       char cAff;               /* A single character of affinity */
 64165     } ap;
 64166     struct OP_MakeRecord_stack_vars {
 64167       u8 *zNewRecord;        /* A buffer to hold the data for the new record */
 64168       Mem *pRec;             /* The new record */
 64169       u64 nData;             /* Number of bytes of data space */
 64170       int nHdr;              /* Number of bytes of header space */
 64171       i64 nByte;             /* Data space required for this record */
 64172       int nZero;             /* Number of zero bytes at the end of the record */
 64173       int nVarint;           /* Number of bytes in a varint */
 64174       u32 serial_type;       /* Type field */
 64175       Mem *pData0;           /* First field to be combined into the record */
 64176       Mem *pLast;            /* Last field of the record */
 64177       int nField;            /* Number of fields in the record */
 64178       char *zAffinity;       /* The affinity string for the record */
 64179       int file_format;       /* File format to use for encoding */
 64180       int i;                 /* Space used in zNewRecord[] */
 64181       int len;               /* Length of a field */
 64182     } aq;
 64183     struct OP_Count_stack_vars {
 64184       i64 nEntry;
 64185       BtCursor *pCrsr;
 64186     } ar;
 64187     struct OP_Savepoint_stack_vars {
 64188       int p1;                         /* Value of P1 operand */
 64189       char *zName;                    /* Name of savepoint */
 64190       int nName;
 64191       Savepoint *pNew;
 64192       Savepoint *pSavepoint;
 64193       Savepoint *pTmp;
 64194       int iSavepoint;
 64195       int ii;
 64196     } as;
 64197     struct OP_AutoCommit_stack_vars {
 64198       int desiredAutoCommit;
 64199       int iRollback;
 64200       int turnOnAC;
 64201     } at;
 64202     struct OP_Transaction_stack_vars {
 64203       Btree *pBt;
 64204     } au;
 64205     struct OP_ReadCookie_stack_vars {
 64206       int iMeta;
 64207       int iDb;
 64208       int iCookie;
 64209     } av;
 64210     struct OP_SetCookie_stack_vars {
 64211       Db *pDb;
 64212     } aw;
 64213     struct OP_VerifyCookie_stack_vars {
 64214       int iMeta;
 64215       int iGen;
 64216       Btree *pBt;
 64217     } ax;
 64218     struct OP_OpenWrite_stack_vars {
 64219       int nField;
 64220       KeyInfo *pKeyInfo;
 64221       int p2;
 64222       int iDb;
 64223       int wrFlag;
 64224       Btree *pX;
 64225       VdbeCursor *pCur;
 64226       Db *pDb;
 64227     } ay;
 64228     struct OP_OpenEphemeral_stack_vars {
 64229       VdbeCursor *pCx;
 64230     } az;
 64231     struct OP_SorterOpen_stack_vars {
 64232       VdbeCursor *pCx;
 64233     } ba;
 64234     struct OP_OpenPseudo_stack_vars {
 64235       VdbeCursor *pCx;
 64236     } bb;
 64237     struct OP_SeekGt_stack_vars {
 64238       int res;
 64239       int oc;
 64240       VdbeCursor *pC;
 64241       UnpackedRecord r;
 64242       int nField;
 64243       i64 iKey;      /* The rowid we are to seek to */
 64244     } bc;
 64245     struct OP_Seek_stack_vars {
 64246       VdbeCursor *pC;
 64247     } bd;
 64248     struct OP_Found_stack_vars {
 64249       int alreadyExists;
 64250       VdbeCursor *pC;
 64251       int res;
 64252       char *pFree;
 64253       UnpackedRecord *pIdxKey;
 64254       UnpackedRecord r;
 64255       char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
 64256     } be;
 64257     struct OP_IsUnique_stack_vars {
 64258       u16 ii;
 64259       VdbeCursor *pCx;
 64260       BtCursor *pCrsr;
 64261       u16 nField;
 64262       Mem *aMx;
 64263       UnpackedRecord r;                  /* B-Tree index search key */
 64264       i64 R;                             /* Rowid stored in register P3 */
 64265     } bf;
 64266     struct OP_NotExists_stack_vars {
 64267       VdbeCursor *pC;
 64268       BtCursor *pCrsr;
 64269       int res;
 64270       u64 iKey;
 64271     } bg;
 64272     struct OP_NewRowid_stack_vars {
 64273       i64 v;                 /* The new rowid */
 64274       VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 64275       int res;               /* Result of an sqlite3BtreeLast() */
 64276       int cnt;               /* Counter to limit the number of searches */
 64277       Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 64278       VdbeFrame *pFrame;     /* Root frame of VDBE */
 64279     } bh;
 64280     struct OP_InsertInt_stack_vars {
 64281       Mem *pData;       /* MEM cell holding data for the record to be inserted */
 64282       Mem *pKey;        /* MEM cell holding key  for the record */
 64283       i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 64284       VdbeCursor *pC;   /* Cursor to table into which insert is written */
 64285       int nZero;        /* Number of zero-bytes to append */
 64286       int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 64287       const char *zDb;  /* database name - used by the update hook */
 64288       const char *zTbl; /* Table name - used by the opdate hook */
 64289       int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 64290     } bi;
 64291     struct OP_Delete_stack_vars {
 64292       i64 iKey;
 64293       VdbeCursor *pC;
 64294     } bj;
 64295     struct OP_SorterCompare_stack_vars {
 64296       VdbeCursor *pC;
 64297       int res;
 64298     } bk;
 64299     struct OP_SorterData_stack_vars {
 64300       VdbeCursor *pC;
 64301     } bl;
 64302     struct OP_RowData_stack_vars {
 64303       VdbeCursor *pC;
 64304       BtCursor *pCrsr;
 64305       u32 n;
 64306       i64 n64;
 64307     } bm;
 64308     struct OP_Rowid_stack_vars {
 64309       VdbeCursor *pC;
 64310       i64 v;
 64311       sqlite3_vtab *pVtab;
 64312       const sqlite3_module *pModule;
 64313     } bn;
 64314     struct OP_NullRow_stack_vars {
 64315       VdbeCursor *pC;
 64316     } bo;
 64317     struct OP_Last_stack_vars {
 64318       VdbeCursor *pC;
 64319       BtCursor *pCrsr;
 64320       int res;
 64321     } bp;
 64322     struct OP_Rewind_stack_vars {
 64323       VdbeCursor *pC;
 64324       BtCursor *pCrsr;
 64325       int res;
 64326     } bq;
 64327     struct OP_Next_stack_vars {
 64328       VdbeCursor *pC;
 64329       int res;
 64330     } br;
 64331     struct OP_IdxInsert_stack_vars {
 64332       VdbeCursor *pC;
 64333       BtCursor *pCrsr;
 64334       int nKey;
 64335       const char *zKey;
 64336     } bs;
 64337     struct OP_IdxDelete_stack_vars {
 64338       VdbeCursor *pC;
 64339       BtCursor *pCrsr;
 64340       int res;
 64341       UnpackedRecord r;
 64342     } bt;
 64343     struct OP_IdxRowid_stack_vars {
 64344       BtCursor *pCrsr;
 64345       VdbeCursor *pC;
 64346       i64 rowid;
 64347     } bu;
 64348     struct OP_IdxGE_stack_vars {
 64349       VdbeCursor *pC;
 64350       int res;
 64351       UnpackedRecord r;
 64352     } bv;
 64353     struct OP_Destroy_stack_vars {
 64354       int iMoved;
 64355       int iCnt;
 64356       Vdbe *pVdbe;
 64357       int iDb;
 64358     } bw;
 64359     struct OP_Clear_stack_vars {
 64360       int nChange;
 64361     } bx;
 64362     struct OP_CreateTable_stack_vars {
 64363       int pgno;
 64364       int flags;
 64365       Db *pDb;
 64366     } by;
 64367     struct OP_ParseSchema_stack_vars {
 64368       int iDb;
 64369       const char *zMaster;
 64370       char *zSql;
 64371       InitData initData;
 64372     } bz;
 64373     struct OP_IntegrityCk_stack_vars {
 64374       int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 64375       int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 64376       int j;          /* Loop counter */
 64377       int nErr;       /* Number of errors reported */
 64378       char *z;        /* Text of the error report */
 64379       Mem *pnErr;     /* Register keeping track of errors remaining */
 64380     } ca;
 64381     struct OP_RowSetRead_stack_vars {
 64382       i64 val;
 64383     } cb;
 64384     struct OP_RowSetTest_stack_vars {
 64385       int iSet;
 64386       int exists;
 64387     } cc;
 64388     struct OP_Program_stack_vars {
 64389       int nMem;               /* Number of memory registers for sub-program */
 64390       int nByte;              /* Bytes of runtime space required for sub-program */
 64391       Mem *pRt;               /* Register to allocate runtime space */
 64392       Mem *pMem;              /* Used to iterate through memory cells */
 64393       Mem *pEnd;              /* Last memory cell in new array */
 64394       VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 64395       SubProgram *pProgram;   /* Sub-program to execute */
 64396       void *t;                /* Token identifying trigger */
 64397     } cd;
 64398     struct OP_Param_stack_vars {
 64399       VdbeFrame *pFrame;
 64400       Mem *pIn;
 64401     } ce;
 64402     struct OP_MemMax_stack_vars {
 64403       Mem *pIn1;
 64404       VdbeFrame *pFrame;
 64405     } cf;
 64406     struct OP_AggStep_stack_vars {
 64407       int n;
 64408       int i;
 64409       Mem *pMem;
 64410       Mem *pRec;
 64411       sqlite3_context ctx;
 64412       sqlite3_value **apVal;
 64413     } cg;
 64414     struct OP_AggFinal_stack_vars {
 64415       Mem *pMem;
 64416     } ch;
 64417     struct OP_Checkpoint_stack_vars {
 64418       int i;                          /* Loop counter */
 64419       int aRes[3];                    /* Results */
 64420       Mem *pMem;                      /* Write results here */
 64421     } ci;
 64422     struct OP_JournalMode_stack_vars {
 64423       Btree *pBt;                     /* Btree to change journal mode of */
 64424       Pager *pPager;                  /* Pager associated with pBt */
 64425       int eNew;                       /* New journal mode */
 64426       int eOld;                       /* The old journal mode */
 64427 #ifndef SQLITE_OMIT_WAL
 64428       const char *zFilename;          /* Name of database file for pPager */
 64429 #endif
 64430     } cj;
 64431     struct OP_IncrVacuum_stack_vars {
 64432       Btree *pBt;
 64433     } ck;
 64434     struct OP_VBegin_stack_vars {
 64435       VTable *pVTab;
 64436     } cl;
 64437     struct OP_VOpen_stack_vars {
 64438       VdbeCursor *pCur;
 64439       sqlite3_vtab_cursor *pVtabCursor;
 64440       sqlite3_vtab *pVtab;
 64441       sqlite3_module *pModule;
 64442     } cm;
 64443     struct OP_VFilter_stack_vars {
 64444       int nArg;
 64445       int iQuery;
 64446       const sqlite3_module *pModule;
 64447       Mem *pQuery;
 64448       Mem *pArgc;
 64449       sqlite3_vtab_cursor *pVtabCursor;
 64450       sqlite3_vtab *pVtab;
 64451       VdbeCursor *pCur;
 64452       int res;
 64453       int i;
 64454       Mem **apArg;
 64455     } cn;
 64456     struct OP_VColumn_stack_vars {
 64457       sqlite3_vtab *pVtab;
 64458       const sqlite3_module *pModule;
 64459       Mem *pDest;
 64460       sqlite3_context sContext;
 64461     } co;
 64462     struct OP_VNext_stack_vars {
 64463       sqlite3_vtab *pVtab;
 64464       const sqlite3_module *pModule;
 64465       int res;
 64466       VdbeCursor *pCur;
 64467     } cp;
 64468     struct OP_VRename_stack_vars {
 64469       sqlite3_vtab *pVtab;
 64470       Mem *pName;
 64471     } cq;
 64472     struct OP_VUpdate_stack_vars {
 64473       sqlite3_vtab *pVtab;
 64474       sqlite3_module *pModule;
 64475       int nArg;
 64476       int i;
 64477       sqlite_int64 rowid;
 64478       Mem **apArg;
 64479       Mem *pX;
 64480     } cr;
 64481     struct OP_Trace_stack_vars {
 64482       char *zTrace;
 64483       char *z;
 64484     } cs;
 64485   } u;
 64486   /* End automatically generated code
 64487   ********************************************************************/
 64489   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 64490   sqlite3VdbeEnter(p);
 64491   if( p->rc==SQLITE_NOMEM ){
 64492     /* This happens if a malloc() inside a call to sqlite3_column_text() or
 64493     ** sqlite3_column_text16() failed.  */
 64494     goto no_mem;
 64496   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
 64497   p->rc = SQLITE_OK;
 64498   assert( p->explain==0 );
 64499   p->pResultSet = 0;
 64500   db->busyHandler.nBusy = 0;
 64501   CHECK_FOR_INTERRUPT;
 64502   sqlite3VdbeIOTraceSql(p);
 64503 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 64504   checkProgress = db->xProgress!=0;
 64505 #endif
 64506 #ifdef SQLITE_DEBUG
 64507   sqlite3BeginBenignMalloc();
 64508   if( p->pc==0  && (p->db->flags & SQLITE_VdbeListing)!=0 ){
 64509     int i;
 64510     printf("VDBE Program Listing:\n");
 64511     sqlite3VdbePrintSql(p);
 64512     for(i=0; i<p->nOp; i++){
 64513       sqlite3VdbePrintOp(stdout, i, &aOp[i]);
 64516   sqlite3EndBenignMalloc();
 64517 #endif
 64518   for(pc=p->pc; rc==SQLITE_OK; pc++){
 64519     assert( pc>=0 && pc<p->nOp );
 64520     if( db->mallocFailed ) goto no_mem;
 64521 #ifdef VDBE_PROFILE
 64522     origPc = pc;
 64523     start = sqlite3Hwtime();
 64524 #endif
 64525     pOp = &aOp[pc];
 64527     /* Only allow tracing if SQLITE_DEBUG is defined.
 64528     */
 64529 #ifdef SQLITE_DEBUG
 64530     if( p->trace ){
 64531       if( pc==0 ){
 64532         printf("VDBE Execution Trace:\n");
 64533         sqlite3VdbePrintSql(p);
 64535       sqlite3VdbePrintOp(p->trace, pc, pOp);
 64537 #endif
 64540     /* Check to see if we need to simulate an interrupt.  This only happens
 64541     ** if we have a special test build.
 64542     */
 64543 #ifdef SQLITE_TEST
 64544     if( sqlite3_interrupt_count>0 ){
 64545       sqlite3_interrupt_count--;
 64546       if( sqlite3_interrupt_count==0 ){
 64547         sqlite3_interrupt(db);
 64550 #endif
 64552 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 64553     /* Call the progress callback if it is configured and the required number
 64554     ** of VDBE ops have been executed (either since this invocation of
 64555     ** sqlite3VdbeExec() or since last time the progress callback was called).
 64556     ** If the progress callback returns non-zero, exit the virtual machine with
 64557     ** a return code SQLITE_ABORT.
 64558     */
 64559     if( checkProgress ){
 64560       if( db->nProgressOps==nProgressOps ){
 64561         int prc;
 64562         prc = db->xProgress(db->pProgressArg);
 64563         if( prc!=0 ){
 64564           rc = SQLITE_INTERRUPT;
 64565           goto vdbe_error_halt;
 64567         nProgressOps = 0;
 64569       nProgressOps++;
 64571 #endif
 64573     /* On any opcode with the "out2-prerelease" tag, free any
 64574     ** external allocations out of mem[p2] and set mem[p2] to be
 64575     ** an undefined integer.  Opcodes will either fill in the integer
 64576     ** value or convert mem[p2] to a different type.
 64577     */
 64578     assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
 64579     if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
 64580       assert( pOp->p2>0 );
 64581       assert( pOp->p2<=p->nMem );
 64582       pOut = &aMem[pOp->p2];
 64583       memAboutToChange(p, pOut);
 64584       VdbeMemRelease(pOut);
 64585       pOut->flags = MEM_Int;
 64588     /* Sanity checking on other operands */
 64589 #ifdef SQLITE_DEBUG
 64590     if( (pOp->opflags & OPFLG_IN1)!=0 ){
 64591       assert( pOp->p1>0 );
 64592       assert( pOp->p1<=p->nMem );
 64593       assert( memIsValid(&aMem[pOp->p1]) );
 64594       REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
 64596     if( (pOp->opflags & OPFLG_IN2)!=0 ){
 64597       assert( pOp->p2>0 );
 64598       assert( pOp->p2<=p->nMem );
 64599       assert( memIsValid(&aMem[pOp->p2]) );
 64600       REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
 64602     if( (pOp->opflags & OPFLG_IN3)!=0 ){
 64603       assert( pOp->p3>0 );
 64604       assert( pOp->p3<=p->nMem );
 64605       assert( memIsValid(&aMem[pOp->p3]) );
 64606       REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
 64608     if( (pOp->opflags & OPFLG_OUT2)!=0 ){
 64609       assert( pOp->p2>0 );
 64610       assert( pOp->p2<=p->nMem );
 64611       memAboutToChange(p, &aMem[pOp->p2]);
 64613     if( (pOp->opflags & OPFLG_OUT3)!=0 ){
 64614       assert( pOp->p3>0 );
 64615       assert( pOp->p3<=p->nMem );
 64616       memAboutToChange(p, &aMem[pOp->p3]);
 64618 #endif
 64620     switch( pOp->opcode ){
 64622 /*****************************************************************************
 64623 ** What follows is a massive switch statement where each case implements a
 64624 ** separate instruction in the virtual machine.  If we follow the usual
 64625 ** indentation conventions, each case should be indented by 6 spaces.  But
 64626 ** that is a lot of wasted space on the left margin.  So the code within
 64627 ** the switch statement will break with convention and be flush-left. Another
 64628 ** big comment (similar to this one) will mark the point in the code where
 64629 ** we transition back to normal indentation.
 64630 **
 64631 ** The formatting of each case is important.  The makefile for SQLite
 64632 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
 64633 ** file looking for lines that begin with "case OP_".  The opcodes.h files
 64634 ** will be filled with #defines that give unique integer values to each
 64635 ** opcode and the opcodes.c file is filled with an array of strings where
 64636 ** each string is the symbolic name for the corresponding opcode.  If the
 64637 ** case statement is followed by a comment of the form "/# same as ... #/"
 64638 ** that comment is used to determine the particular value of the opcode.
 64639 **
 64640 ** Other keywords in the comment that follows each case are used to
 64641 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
 64642 ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
 64643 ** the mkopcodeh.awk script for additional information.
 64644 **
 64645 ** Documentation about VDBE opcodes is generated by scanning this file
 64646 ** for lines of that contain "Opcode:".  That line and all subsequent
 64647 ** comment lines are used in the generation of the opcode.html documentation
 64648 ** file.
 64649 **
 64650 ** SUMMARY:
 64651 **
 64652 **     Formatting is important to scripts that scan this file.
 64653 **     Do not deviate from the formatting style currently in use.
 64654 **
 64655 *****************************************************************************/
 64657 /* Opcode:  Goto * P2 * * *
 64658 **
 64659 ** An unconditional jump to address P2.
 64660 ** The next instruction executed will be 
 64661 ** the one at index P2 from the beginning of
 64662 ** the program.
 64663 */
 64664 case OP_Goto: {             /* jump */
 64665   CHECK_FOR_INTERRUPT;
 64666   pc = pOp->p2 - 1;
 64667   break;
 64670 /* Opcode:  Gosub P1 P2 * * *
 64671 **
 64672 ** Write the current address onto register P1
 64673 ** and then jump to address P2.
 64674 */
 64675 case OP_Gosub: {            /* jump */
 64676   assert( pOp->p1>0 && pOp->p1<=p->nMem );
 64677   pIn1 = &aMem[pOp->p1];
 64678   assert( (pIn1->flags & MEM_Dyn)==0 );
 64679   memAboutToChange(p, pIn1);
 64680   pIn1->flags = MEM_Int;
 64681   pIn1->u.i = pc;
 64682   REGISTER_TRACE(pOp->p1, pIn1);
 64683   pc = pOp->p2 - 1;
 64684   break;
 64687 /* Opcode:  Return P1 * * * *
 64688 **
 64689 ** Jump to the next instruction after the address in register P1.
 64690 */
 64691 case OP_Return: {           /* in1 */
 64692   pIn1 = &aMem[pOp->p1];
 64693   assert( pIn1->flags & MEM_Int );
 64694   pc = (int)pIn1->u.i;
 64695   break;
 64698 /* Opcode:  Yield P1 * * * *
 64699 **
 64700 ** Swap the program counter with the value in register P1.
 64701 */
 64702 case OP_Yield: {            /* in1 */
 64703 #if 0  /* local variables moved into u.aa */
 64704   int pcDest;
 64705 #endif /* local variables moved into u.aa */
 64706   pIn1 = &aMem[pOp->p1];
 64707   assert( (pIn1->flags & MEM_Dyn)==0 );
 64708   pIn1->flags = MEM_Int;
 64709   u.aa.pcDest = (int)pIn1->u.i;
 64710   pIn1->u.i = pc;
 64711   REGISTER_TRACE(pOp->p1, pIn1);
 64712   pc = u.aa.pcDest;
 64713   break;
 64716 /* Opcode:  HaltIfNull  P1 P2 P3 P4 *
 64717 **
 64718 ** Check the value in register P3.  If it is NULL then Halt using
 64719 ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
 64720 ** value in register P3 is not NULL, then this routine is a no-op.
 64721 */
 64722 case OP_HaltIfNull: {      /* in3 */
 64723   pIn3 = &aMem[pOp->p3];
 64724   if( (pIn3->flags & MEM_Null)==0 ) break;
 64725   /* Fall through into OP_Halt */
 64728 /* Opcode:  Halt P1 P2 * P4 *
 64729 **
 64730 ** Exit immediately.  All open cursors, etc are closed
 64731 ** automatically.
 64732 **
 64733 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
 64734 ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
 64735 ** For errors, it can be some other value.  If P1!=0 then P2 will determine
 64736 ** whether or not to rollback the current transaction.  Do not rollback
 64737 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
 64738 ** then back out all changes that have occurred during this execution of the
 64739 ** VDBE, but do not rollback the transaction. 
 64740 **
 64741 ** If P4 is not null then it is an error message string.
 64742 **
 64743 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
 64744 ** every program.  So a jump past the last instruction of the program
 64745 ** is the same as executing Halt.
 64746 */
 64747 case OP_Halt: {
 64748   if( pOp->p1==SQLITE_OK && p->pFrame ){
 64749     /* Halt the sub-program. Return control to the parent frame. */
 64750     VdbeFrame *pFrame = p->pFrame;
 64751     p->pFrame = pFrame->pParent;
 64752     p->nFrame--;
 64753     sqlite3VdbeSetChanges(db, p->nChange);
 64754     pc = sqlite3VdbeFrameRestore(pFrame);
 64755     lastRowid = db->lastRowid;
 64756     if( pOp->p2==OE_Ignore ){
 64757       /* Instruction pc is the OP_Program that invoked the sub-program 
 64758       ** currently being halted. If the p2 instruction of this OP_Halt
 64759       ** instruction is set to OE_Ignore, then the sub-program is throwing
 64760       ** an IGNORE exception. In this case jump to the address specified
 64761       ** as the p2 of the calling OP_Program.  */
 64762       pc = p->aOp[pc].p2-1;
 64764     aOp = p->aOp;
 64765     aMem = p->aMem;
 64766     break;
 64769   p->rc = pOp->p1;
 64770   p->errorAction = (u8)pOp->p2;
 64771   p->pc = pc;
 64772   if( pOp->p4.z ){
 64773     assert( p->rc!=SQLITE_OK );
 64774     sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
 64775     testcase( sqlite3GlobalConfig.xLog!=0 );
 64776     sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
 64777   }else if( p->rc ){
 64778     testcase( sqlite3GlobalConfig.xLog!=0 );
 64779     sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
 64781   rc = sqlite3VdbeHalt(p);
 64782   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
 64783   if( rc==SQLITE_BUSY ){
 64784     p->rc = rc = SQLITE_BUSY;
 64785   }else{
 64786     assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
 64787     assert( rc==SQLITE_OK || db->nDeferredCons>0 );
 64788     rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
 64790   goto vdbe_return;
 64793 /* Opcode: Integer P1 P2 * * *
 64794 **
 64795 ** The 32-bit integer value P1 is written into register P2.
 64796 */
 64797 case OP_Integer: {         /* out2-prerelease */
 64798   pOut->u.i = pOp->p1;
 64799   break;
 64802 /* Opcode: Int64 * P2 * P4 *
 64803 **
 64804 ** P4 is a pointer to a 64-bit integer value.
 64805 ** Write that value into register P2.
 64806 */
 64807 case OP_Int64: {           /* out2-prerelease */
 64808   assert( pOp->p4.pI64!=0 );
 64809   pOut->u.i = *pOp->p4.pI64;
 64810   break;
 64813 #ifndef SQLITE_OMIT_FLOATING_POINT
 64814 /* Opcode: Real * P2 * P4 *
 64815 **
 64816 ** P4 is a pointer to a 64-bit floating point value.
 64817 ** Write that value into register P2.
 64818 */
 64819 case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
 64820   pOut->flags = MEM_Real;
 64821   assert( !sqlite3IsNaN(*pOp->p4.pReal) );
 64822   pOut->r = *pOp->p4.pReal;
 64823   break;
 64825 #endif
 64827 /* Opcode: String8 * P2 * P4 *
 64828 **
 64829 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
 64830 ** into an OP_String before it is executed for the first time.
 64831 */
 64832 case OP_String8: {         /* same as TK_STRING, out2-prerelease */
 64833   assert( pOp->p4.z!=0 );
 64834   pOp->opcode = OP_String;
 64835   pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 64837 #ifndef SQLITE_OMIT_UTF16
 64838   if( encoding!=SQLITE_UTF8 ){
 64839     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 64840     if( rc==SQLITE_TOOBIG ) goto too_big;
 64841     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
 64842     assert( pOut->zMalloc==pOut->z );
 64843     assert( pOut->flags & MEM_Dyn );
 64844     pOut->zMalloc = 0;
 64845     pOut->flags |= MEM_Static;
 64846     pOut->flags &= ~MEM_Dyn;
 64847     if( pOp->p4type==P4_DYNAMIC ){
 64848       sqlite3DbFree(db, pOp->p4.z);
 64850     pOp->p4type = P4_DYNAMIC;
 64851     pOp->p4.z = pOut->z;
 64852     pOp->p1 = pOut->n;
 64854 #endif
 64855   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 64856     goto too_big;
 64858   /* Fall through to the next case, OP_String */
 64861 /* Opcode: String P1 P2 * P4 *
 64862 **
 64863 ** The string value P4 of length P1 (bytes) is stored in register P2.
 64864 */
 64865 case OP_String: {          /* out2-prerelease */
 64866   assert( pOp->p4.z!=0 );
 64867   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 64868   pOut->z = pOp->p4.z;
 64869   pOut->n = pOp->p1;
 64870   pOut->enc = encoding;
 64871   UPDATE_MAX_BLOBSIZE(pOut);
 64872   break;
 64875 /* Opcode: Null P1 P2 P3 * *
 64876 **
 64877 ** Write a NULL into registers P2.  If P3 greater than P2, then also write
 64878 ** NULL into register P3 and every register in between P2 and P3.  If P3
 64879 ** is less than P2 (typically P3 is zero) then only register P2 is
 64880 ** set to NULL.
 64881 **
 64882 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
 64883 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
 64884 ** OP_Ne or OP_Eq.
 64885 */
 64886 case OP_Null: {           /* out2-prerelease */
 64887 #if 0  /* local variables moved into u.ab */
 64888   int cnt;
 64889   u16 nullFlag;
 64890 #endif /* local variables moved into u.ab */
 64891   u.ab.cnt = pOp->p3-pOp->p2;
 64892   assert( pOp->p3<=p->nMem );
 64893   pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
 64894   while( u.ab.cnt>0 ){
 64895     pOut++;
 64896     memAboutToChange(p, pOut);
 64897     VdbeMemRelease(pOut);
 64898     pOut->flags = u.ab.nullFlag;
 64899     u.ab.cnt--;
 64901   break;
 64905 /* Opcode: Blob P1 P2 * P4
 64906 **
 64907 ** P4 points to a blob of data P1 bytes long.  Store this
 64908 ** blob in register P2.
 64909 */
 64910 case OP_Blob: {                /* out2-prerelease */
 64911   assert( pOp->p1 <= SQLITE_MAX_LENGTH );
 64912   sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
 64913   pOut->enc = encoding;
 64914   UPDATE_MAX_BLOBSIZE(pOut);
 64915   break;
 64918 /* Opcode: Variable P1 P2 * P4 *
 64919 **
 64920 ** Transfer the values of bound parameter P1 into register P2
 64921 **
 64922 ** If the parameter is named, then its name appears in P4 and P3==1.
 64923 ** The P4 value is used by sqlite3_bind_parameter_name().
 64924 */
 64925 case OP_Variable: {            /* out2-prerelease */
 64926 #if 0  /* local variables moved into u.ac */
 64927   Mem *pVar;       /* Value being transferred */
 64928 #endif /* local variables moved into u.ac */
 64930   assert( pOp->p1>0 && pOp->p1<=p->nVar );
 64931   assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
 64932   u.ac.pVar = &p->aVar[pOp->p1 - 1];
 64933   if( sqlite3VdbeMemTooBig(u.ac.pVar) ){
 64934     goto too_big;
 64936   sqlite3VdbeMemShallowCopy(pOut, u.ac.pVar, MEM_Static);
 64937   UPDATE_MAX_BLOBSIZE(pOut);
 64938   break;
 64941 /* Opcode: Move P1 P2 P3 * *
 64942 **
 64943 ** Move the values in register P1..P1+P3 over into
 64944 ** registers P2..P2+P3.  Registers P1..P1+P3 are
 64945 ** left holding a NULL.  It is an error for register ranges
 64946 ** P1..P1+P3 and P2..P2+P3 to overlap.
 64947 */
 64948 case OP_Move: {
 64949 #if 0  /* local variables moved into u.ad */
 64950   char *zMalloc;   /* Holding variable for allocated memory */
 64951   int n;           /* Number of registers left to copy */
 64952   int p1;          /* Register to copy from */
 64953   int p2;          /* Register to copy to */
 64954 #endif /* local variables moved into u.ad */
 64956   u.ad.n = pOp->p3 + 1;
 64957   u.ad.p1 = pOp->p1;
 64958   u.ad.p2 = pOp->p2;
 64959   assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
 64960   assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
 64962   pIn1 = &aMem[u.ad.p1];
 64963   pOut = &aMem[u.ad.p2];
 64964   while( u.ad.n-- ){
 64965     assert( pOut<=&aMem[p->nMem] );
 64966     assert( pIn1<=&aMem[p->nMem] );
 64967     assert( memIsValid(pIn1) );
 64968     memAboutToChange(p, pOut);
 64969     u.ad.zMalloc = pOut->zMalloc;
 64970     pOut->zMalloc = 0;
 64971     sqlite3VdbeMemMove(pOut, pIn1);
 64972 #ifdef SQLITE_DEBUG
 64973     if( pOut->pScopyFrom>=&aMem[u.ad.p1] && pOut->pScopyFrom<&aMem[u.ad.p1+pOp->p3] ){
 64974       pOut->pScopyFrom += u.ad.p1 - pOp->p2;
 64976 #endif
 64977     pIn1->zMalloc = u.ad.zMalloc;
 64978     REGISTER_TRACE(u.ad.p2++, pOut);
 64979     pIn1++;
 64980     pOut++;
 64982   break;
 64985 /* Opcode: Copy P1 P2 P3 * *
 64986 **
 64987 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
 64988 **
 64989 ** This instruction makes a deep copy of the value.  A duplicate
 64990 ** is made of any string or blob constant.  See also OP_SCopy.
 64991 */
 64992 case OP_Copy: {
 64993 #if 0  /* local variables moved into u.ae */
 64994   int n;
 64995 #endif /* local variables moved into u.ae */
 64997   u.ae.n = pOp->p3;
 64998   pIn1 = &aMem[pOp->p1];
 64999   pOut = &aMem[pOp->p2];
 65000   assert( pOut!=pIn1 );
 65001   while( 1 ){
 65002     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
 65003     Deephemeralize(pOut);
 65004 #ifdef SQLITE_DEBUG
 65005     pOut->pScopyFrom = 0;
 65006 #endif
 65007     REGISTER_TRACE(pOp->p2+pOp->p3-u.ae.n, pOut);
 65008     if( (u.ae.n--)==0 ) break;
 65009     pOut++;
 65010     pIn1++;
 65012   break;
 65015 /* Opcode: SCopy P1 P2 * * *
 65016 **
 65017 ** Make a shallow copy of register P1 into register P2.
 65018 **
 65019 ** This instruction makes a shallow copy of the value.  If the value
 65020 ** is a string or blob, then the copy is only a pointer to the
 65021 ** original and hence if the original changes so will the copy.
 65022 ** Worse, if the original is deallocated, the copy becomes invalid.
 65023 ** Thus the program must guarantee that the original will not change
 65024 ** during the lifetime of the copy.  Use OP_Copy to make a complete
 65025 ** copy.
 65026 */
 65027 case OP_SCopy: {            /* in1, out2 */
 65028   pIn1 = &aMem[pOp->p1];
 65029   pOut = &aMem[pOp->p2];
 65030   assert( pOut!=pIn1 );
 65031   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
 65032 #ifdef SQLITE_DEBUG
 65033   if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
 65034 #endif
 65035   REGISTER_TRACE(pOp->p2, pOut);
 65036   break;
 65039 /* Opcode: ResultRow P1 P2 * * *
 65040 **
 65041 ** The registers P1 through P1+P2-1 contain a single row of
 65042 ** results. This opcode causes the sqlite3_step() call to terminate
 65043 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
 65044 ** structure to provide access to the top P1 values as the result
 65045 ** row.
 65046 */
 65047 case OP_ResultRow: {
 65048 #if 0  /* local variables moved into u.af */
 65049   Mem *pMem;
 65050   int i;
 65051 #endif /* local variables moved into u.af */
 65052   assert( p->nResColumn==pOp->p2 );
 65053   assert( pOp->p1>0 );
 65054   assert( pOp->p1+pOp->p2<=p->nMem+1 );
 65056   /* If this statement has violated immediate foreign key constraints, do
 65057   ** not return the number of rows modified. And do not RELEASE the statement
 65058   ** transaction. It needs to be rolled back.  */
 65059   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
 65060     assert( db->flags&SQLITE_CountRows );
 65061     assert( p->usesStmtJournal );
 65062     break;
 65065   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
 65066   ** DML statements invoke this opcode to return the number of rows
 65067   ** modified to the user. This is the only way that a VM that
 65068   ** opens a statement transaction may invoke this opcode.
 65069   **
 65070   ** In case this is such a statement, close any statement transaction
 65071   ** opened by this VM before returning control to the user. This is to
 65072   ** ensure that statement-transactions are always nested, not overlapping.
 65073   ** If the open statement-transaction is not closed here, then the user
 65074   ** may step another VM that opens its own statement transaction. This
 65075   ** may lead to overlapping statement transactions.
 65076   **
 65077   ** The statement transaction is never a top-level transaction.  Hence
 65078   ** the RELEASE call below can never fail.
 65079   */
 65080   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
 65081   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
 65082   if( NEVER(rc!=SQLITE_OK) ){
 65083     break;
 65086   /* Invalidate all ephemeral cursor row caches */
 65087   p->cacheCtr = (p->cacheCtr + 2)|1;
 65089   /* Make sure the results of the current row are \000 terminated
 65090   ** and have an assigned type.  The results are de-ephemeralized as
 65091   ** a side effect.
 65092   */
 65093   u.af.pMem = p->pResultSet = &aMem[pOp->p1];
 65094   for(u.af.i=0; u.af.i<pOp->p2; u.af.i++){
 65095     assert( memIsValid(&u.af.pMem[u.af.i]) );
 65096     Deephemeralize(&u.af.pMem[u.af.i]);
 65097     assert( (u.af.pMem[u.af.i].flags & MEM_Ephem)==0
 65098             || (u.af.pMem[u.af.i].flags & (MEM_Str|MEM_Blob))==0 );
 65099     sqlite3VdbeMemNulTerminate(&u.af.pMem[u.af.i]);
 65100     sqlite3VdbeMemStoreType(&u.af.pMem[u.af.i]);
 65101     REGISTER_TRACE(pOp->p1+u.af.i, &u.af.pMem[u.af.i]);
 65103   if( db->mallocFailed ) goto no_mem;
 65105   /* Return SQLITE_ROW
 65106   */
 65107   p->pc = pc + 1;
 65108   rc = SQLITE_ROW;
 65109   goto vdbe_return;
 65112 /* Opcode: Concat P1 P2 P3 * *
 65113 **
 65114 ** Add the text in register P1 onto the end of the text in
 65115 ** register P2 and store the result in register P3.
 65116 ** If either the P1 or P2 text are NULL then store NULL in P3.
 65117 **
 65118 **   P3 = P2 || P1
 65119 **
 65120 ** It is illegal for P1 and P3 to be the same register. Sometimes,
 65121 ** if P3 is the same register as P2, the implementation is able
 65122 ** to avoid a memcpy().
 65123 */
 65124 case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
 65125 #if 0  /* local variables moved into u.ag */
 65126   i64 nByte;
 65127 #endif /* local variables moved into u.ag */
 65129   pIn1 = &aMem[pOp->p1];
 65130   pIn2 = &aMem[pOp->p2];
 65131   pOut = &aMem[pOp->p3];
 65132   assert( pIn1!=pOut );
 65133   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
 65134     sqlite3VdbeMemSetNull(pOut);
 65135     break;
 65137   if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
 65138   Stringify(pIn1, encoding);
 65139   Stringify(pIn2, encoding);
 65140   u.ag.nByte = pIn1->n + pIn2->n;
 65141   if( u.ag.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 65142     goto too_big;
 65144   MemSetTypeFlag(pOut, MEM_Str);
 65145   if( sqlite3VdbeMemGrow(pOut, (int)u.ag.nByte+2, pOut==pIn2) ){
 65146     goto no_mem;
 65148   if( pOut!=pIn2 ){
 65149     memcpy(pOut->z, pIn2->z, pIn2->n);
 65151   memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
 65152   pOut->z[u.ag.nByte] = 0;
 65153   pOut->z[u.ag.nByte+1] = 0;
 65154   pOut->flags |= MEM_Term;
 65155   pOut->n = (int)u.ag.nByte;
 65156   pOut->enc = encoding;
 65157   UPDATE_MAX_BLOBSIZE(pOut);
 65158   break;
 65161 /* Opcode: Add P1 P2 P3 * *
 65162 **
 65163 ** Add the value in register P1 to the value in register P2
 65164 ** and store the result in register P3.
 65165 ** If either input is NULL, the result is NULL.
 65166 */
 65167 /* Opcode: Multiply P1 P2 P3 * *
 65168 **
 65169 **
 65170 ** Multiply the value in register P1 by the value in register P2
 65171 ** and store the result in register P3.
 65172 ** If either input is NULL, the result is NULL.
 65173 */
 65174 /* Opcode: Subtract P1 P2 P3 * *
 65175 **
 65176 ** Subtract the value in register P1 from the value in register P2
 65177 ** and store the result in register P3.
 65178 ** If either input is NULL, the result is NULL.
 65179 */
 65180 /* Opcode: Divide P1 P2 P3 * *
 65181 **
 65182 ** Divide the value in register P1 by the value in register P2
 65183 ** and store the result in register P3 (P3=P2/P1). If the value in 
 65184 ** register P1 is zero, then the result is NULL. If either input is 
 65185 ** NULL, the result is NULL.
 65186 */
 65187 /* Opcode: Remainder P1 P2 P3 * *
 65188 **
 65189 ** Compute the remainder after integer division of the value in
 65190 ** register P1 by the value in register P2 and store the result in P3. 
 65191 ** If the value in register P2 is zero the result is NULL.
 65192 ** If either operand is NULL, the result is NULL.
 65193 */
 65194 case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
 65195 case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
 65196 case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
 65197 case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
 65198 case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
 65199 #if 0  /* local variables moved into u.ah */
 65200   char bIntint;   /* Started out as two integer operands */
 65201   int flags;      /* Combined MEM_* flags from both inputs */
 65202   i64 iA;         /* Integer value of left operand */
 65203   i64 iB;         /* Integer value of right operand */
 65204   double rA;      /* Real value of left operand */
 65205   double rB;      /* Real value of right operand */
 65206 #endif /* local variables moved into u.ah */
 65208   pIn1 = &aMem[pOp->p1];
 65209   applyNumericAffinity(pIn1);
 65210   pIn2 = &aMem[pOp->p2];
 65211   applyNumericAffinity(pIn2);
 65212   pOut = &aMem[pOp->p3];
 65213   u.ah.flags = pIn1->flags | pIn2->flags;
 65214   if( (u.ah.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
 65215   if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
 65216     u.ah.iA = pIn1->u.i;
 65217     u.ah.iB = pIn2->u.i;
 65218     u.ah.bIntint = 1;
 65219     switch( pOp->opcode ){
 65220       case OP_Add:       if( sqlite3AddInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
 65221       case OP_Subtract:  if( sqlite3SubInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
 65222       case OP_Multiply:  if( sqlite3MulInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
 65223       case OP_Divide: {
 65224         if( u.ah.iA==0 ) goto arithmetic_result_is_null;
 65225         if( u.ah.iA==-1 && u.ah.iB==SMALLEST_INT64 ) goto fp_math;
 65226         u.ah.iB /= u.ah.iA;
 65227         break;
 65229       default: {
 65230         if( u.ah.iA==0 ) goto arithmetic_result_is_null;
 65231         if( u.ah.iA==-1 ) u.ah.iA = 1;
 65232         u.ah.iB %= u.ah.iA;
 65233         break;
 65236     pOut->u.i = u.ah.iB;
 65237     MemSetTypeFlag(pOut, MEM_Int);
 65238   }else{
 65239     u.ah.bIntint = 0;
 65240 fp_math:
 65241     u.ah.rA = sqlite3VdbeRealValue(pIn1);
 65242     u.ah.rB = sqlite3VdbeRealValue(pIn2);
 65243     switch( pOp->opcode ){
 65244       case OP_Add:         u.ah.rB += u.ah.rA;       break;
 65245       case OP_Subtract:    u.ah.rB -= u.ah.rA;       break;
 65246       case OP_Multiply:    u.ah.rB *= u.ah.rA;       break;
 65247       case OP_Divide: {
 65248         /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 65249         if( u.ah.rA==(double)0 ) goto arithmetic_result_is_null;
 65250         u.ah.rB /= u.ah.rA;
 65251         break;
 65253       default: {
 65254         u.ah.iA = (i64)u.ah.rA;
 65255         u.ah.iB = (i64)u.ah.rB;
 65256         if( u.ah.iA==0 ) goto arithmetic_result_is_null;
 65257         if( u.ah.iA==-1 ) u.ah.iA = 1;
 65258         u.ah.rB = (double)(u.ah.iB % u.ah.iA);
 65259         break;
 65262 #ifdef SQLITE_OMIT_FLOATING_POINT
 65263     pOut->u.i = u.ah.rB;
 65264     MemSetTypeFlag(pOut, MEM_Int);
 65265 #else
 65266     if( sqlite3IsNaN(u.ah.rB) ){
 65267       goto arithmetic_result_is_null;
 65269     pOut->r = u.ah.rB;
 65270     MemSetTypeFlag(pOut, MEM_Real);
 65271     if( (u.ah.flags & MEM_Real)==0 && !u.ah.bIntint ){
 65272       sqlite3VdbeIntegerAffinity(pOut);
 65274 #endif
 65276   break;
 65278 arithmetic_result_is_null:
 65279   sqlite3VdbeMemSetNull(pOut);
 65280   break;
 65283 /* Opcode: CollSeq P1 * * P4
 65284 **
 65285 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
 65286 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
 65287 ** be returned. This is used by the built-in min(), max() and nullif()
 65288 ** functions.
 65289 **
 65290 ** If P1 is not zero, then it is a register that a subsequent min() or
 65291 ** max() aggregate will set to 1 if the current row is not the minimum or
 65292 ** maximum.  The P1 register is initialized to 0 by this instruction.
 65293 **
 65294 ** The interface used by the implementation of the aforementioned functions
 65295 ** to retrieve the collation sequence set by this opcode is not available
 65296 ** publicly, only to user functions defined in func.c.
 65297 */
 65298 case OP_CollSeq: {
 65299   assert( pOp->p4type==P4_COLLSEQ );
 65300   if( pOp->p1 ){
 65301     sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
 65303   break;
 65306 /* Opcode: Function P1 P2 P3 P4 P5
 65307 **
 65308 ** Invoke a user function (P4 is a pointer to a Function structure that
 65309 ** defines the function) with P5 arguments taken from register P2 and
 65310 ** successors.  The result of the function is stored in register P3.
 65311 ** Register P3 must not be one of the function inputs.
 65312 **
 65313 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 
 65314 ** function was determined to be constant at compile time. If the first
 65315 ** argument was constant then bit 0 of P1 is set. This is used to determine
 65316 ** whether meta data associated with a user function argument using the
 65317 ** sqlite3_set_auxdata() API may be safely retained until the next
 65318 ** invocation of this opcode.
 65319 **
 65320 ** See also: AggStep and AggFinal
 65321 */
 65322 case OP_Function: {
 65323 #if 0  /* local variables moved into u.ai */
 65324   int i;
 65325   Mem *pArg;
 65326   sqlite3_context ctx;
 65327   sqlite3_value **apVal;
 65328   int n;
 65329 #endif /* local variables moved into u.ai */
 65331   u.ai.n = pOp->p5;
 65332   u.ai.apVal = p->apArg;
 65333   assert( u.ai.apVal || u.ai.n==0 );
 65334   assert( pOp->p3>0 && pOp->p3<=p->nMem );
 65335   pOut = &aMem[pOp->p3];
 65336   memAboutToChange(p, pOut);
 65338   assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
 65339   assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
 65340   u.ai.pArg = &aMem[pOp->p2];
 65341   for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
 65342     assert( memIsValid(u.ai.pArg) );
 65343     u.ai.apVal[u.ai.i] = u.ai.pArg;
 65344     Deephemeralize(u.ai.pArg);
 65345     sqlite3VdbeMemStoreType(u.ai.pArg);
 65346     REGISTER_TRACE(pOp->p2+u.ai.i, u.ai.pArg);
 65349   assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
 65350   if( pOp->p4type==P4_FUNCDEF ){
 65351     u.ai.ctx.pFunc = pOp->p4.pFunc;
 65352     u.ai.ctx.pVdbeFunc = 0;
 65353   }else{
 65354     u.ai.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
 65355     u.ai.ctx.pFunc = u.ai.ctx.pVdbeFunc->pFunc;
 65358   u.ai.ctx.s.flags = MEM_Null;
 65359   u.ai.ctx.s.db = db;
 65360   u.ai.ctx.s.xDel = 0;
 65361   u.ai.ctx.s.zMalloc = 0;
 65363   /* The output cell may already have a buffer allocated. Move
 65364   ** the pointer to u.ai.ctx.s so in case the user-function can use
 65365   ** the already allocated buffer instead of allocating a new one.
 65366   */
 65367   sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
 65368   MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
 65370   u.ai.ctx.isError = 0;
 65371   if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 65372     assert( pOp>aOp );
 65373     assert( pOp[-1].p4type==P4_COLLSEQ );
 65374     assert( pOp[-1].opcode==OP_CollSeq );
 65375     u.ai.ctx.pColl = pOp[-1].p4.pColl;
 65377   db->lastRowid = lastRowid;
 65378   (*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
 65379   lastRowid = db->lastRowid;
 65381   /* If any auxiliary data functions have been called by this user function,
 65382   ** immediately call the destructor for any non-static values.
 65383   */
 65384   if( u.ai.ctx.pVdbeFunc ){
 65385     sqlite3VdbeDeleteAuxData(u.ai.ctx.pVdbeFunc, pOp->p1);
 65386     pOp->p4.pVdbeFunc = u.ai.ctx.pVdbeFunc;
 65387     pOp->p4type = P4_VDBEFUNC;
 65390   if( db->mallocFailed ){
 65391     /* Even though a malloc() has failed, the implementation of the
 65392     ** user function may have called an sqlite3_result_XXX() function
 65393     ** to return a value. The following call releases any resources
 65394     ** associated with such a value.
 65395     */
 65396     sqlite3VdbeMemRelease(&u.ai.ctx.s);
 65397     goto no_mem;
 65400   /* If the function returned an error, throw an exception */
 65401   if( u.ai.ctx.isError ){
 65402     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
 65403     rc = u.ai.ctx.isError;
 65406   /* Copy the result of the function into register P3 */
 65407   sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
 65408   sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
 65409   if( sqlite3VdbeMemTooBig(pOut) ){
 65410     goto too_big;
 65413 #if 0
 65414   /* The app-defined function has done something that as caused this
 65415   ** statement to expire.  (Perhaps the function called sqlite3_exec()
 65416   ** with a CREATE TABLE statement.)
 65417   */
 65418   if( p->expired ) rc = SQLITE_ABORT;
 65419 #endif
 65421   REGISTER_TRACE(pOp->p3, pOut);
 65422   UPDATE_MAX_BLOBSIZE(pOut);
 65423   break;
 65426 /* Opcode: BitAnd P1 P2 P3 * *
 65427 **
 65428 ** Take the bit-wise AND of the values in register P1 and P2 and
 65429 ** store the result in register P3.
 65430 ** If either input is NULL, the result is NULL.
 65431 */
 65432 /* Opcode: BitOr P1 P2 P3 * *
 65433 **
 65434 ** Take the bit-wise OR of the values in register P1 and P2 and
 65435 ** store the result in register P3.
 65436 ** If either input is NULL, the result is NULL.
 65437 */
 65438 /* Opcode: ShiftLeft P1 P2 P3 * *
 65439 **
 65440 ** Shift the integer value in register P2 to the left by the
 65441 ** number of bits specified by the integer in register P1.
 65442 ** Store the result in register P3.
 65443 ** If either input is NULL, the result is NULL.
 65444 */
 65445 /* Opcode: ShiftRight P1 P2 P3 * *
 65446 **
 65447 ** Shift the integer value in register P2 to the right by the
 65448 ** number of bits specified by the integer in register P1.
 65449 ** Store the result in register P3.
 65450 ** If either input is NULL, the result is NULL.
 65451 */
 65452 case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
 65453 case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
 65454 case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
 65455 case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
 65456 #if 0  /* local variables moved into u.aj */
 65457   i64 iA;
 65458   u64 uA;
 65459   i64 iB;
 65460   u8 op;
 65461 #endif /* local variables moved into u.aj */
 65463   pIn1 = &aMem[pOp->p1];
 65464   pIn2 = &aMem[pOp->p2];
 65465   pOut = &aMem[pOp->p3];
 65466   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
 65467     sqlite3VdbeMemSetNull(pOut);
 65468     break;
 65470   u.aj.iA = sqlite3VdbeIntValue(pIn2);
 65471   u.aj.iB = sqlite3VdbeIntValue(pIn1);
 65472   u.aj.op = pOp->opcode;
 65473   if( u.aj.op==OP_BitAnd ){
 65474     u.aj.iA &= u.aj.iB;
 65475   }else if( u.aj.op==OP_BitOr ){
 65476     u.aj.iA |= u.aj.iB;
 65477   }else if( u.aj.iB!=0 ){
 65478     assert( u.aj.op==OP_ShiftRight || u.aj.op==OP_ShiftLeft );
 65480     /* If shifting by a negative amount, shift in the other direction */
 65481     if( u.aj.iB<0 ){
 65482       assert( OP_ShiftRight==OP_ShiftLeft+1 );
 65483       u.aj.op = 2*OP_ShiftLeft + 1 - u.aj.op;
 65484       u.aj.iB = u.aj.iB>(-64) ? -u.aj.iB : 64;
 65487     if( u.aj.iB>=64 ){
 65488       u.aj.iA = (u.aj.iA>=0 || u.aj.op==OP_ShiftLeft) ? 0 : -1;
 65489     }else{
 65490       memcpy(&u.aj.uA, &u.aj.iA, sizeof(u.aj.uA));
 65491       if( u.aj.op==OP_ShiftLeft ){
 65492         u.aj.uA <<= u.aj.iB;
 65493       }else{
 65494         u.aj.uA >>= u.aj.iB;
 65495         /* Sign-extend on a right shift of a negative number */
 65496         if( u.aj.iA<0 ) u.aj.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.aj.iB);
 65498       memcpy(&u.aj.iA, &u.aj.uA, sizeof(u.aj.iA));
 65501   pOut->u.i = u.aj.iA;
 65502   MemSetTypeFlag(pOut, MEM_Int);
 65503   break;
 65506 /* Opcode: AddImm  P1 P2 * * *
 65507 ** 
 65508 ** Add the constant P2 to the value in register P1.
 65509 ** The result is always an integer.
 65510 **
 65511 ** To force any register to be an integer, just add 0.
 65512 */
 65513 case OP_AddImm: {            /* in1 */
 65514   pIn1 = &aMem[pOp->p1];
 65515   memAboutToChange(p, pIn1);
 65516   sqlite3VdbeMemIntegerify(pIn1);
 65517   pIn1->u.i += pOp->p2;
 65518   break;
 65521 /* Opcode: MustBeInt P1 P2 * * *
 65522 ** 
 65523 ** Force the value in register P1 to be an integer.  If the value
 65524 ** in P1 is not an integer and cannot be converted into an integer
 65525 ** without data loss, then jump immediately to P2, or if P2==0
 65526 ** raise an SQLITE_MISMATCH exception.
 65527 */
 65528 case OP_MustBeInt: {            /* jump, in1 */
 65529   pIn1 = &aMem[pOp->p1];
 65530   applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
 65531   if( (pIn1->flags & MEM_Int)==0 ){
 65532     if( pOp->p2==0 ){
 65533       rc = SQLITE_MISMATCH;
 65534       goto abort_due_to_error;
 65535     }else{
 65536       pc = pOp->p2 - 1;
 65538   }else{
 65539     MemSetTypeFlag(pIn1, MEM_Int);
 65541   break;
 65544 #ifndef SQLITE_OMIT_FLOATING_POINT
 65545 /* Opcode: RealAffinity P1 * * * *
 65546 **
 65547 ** If register P1 holds an integer convert it to a real value.
 65548 **
 65549 ** This opcode is used when extracting information from a column that
 65550 ** has REAL affinity.  Such column values may still be stored as
 65551 ** integers, for space efficiency, but after extraction we want them
 65552 ** to have only a real value.
 65553 */
 65554 case OP_RealAffinity: {                  /* in1 */
 65555   pIn1 = &aMem[pOp->p1];
 65556   if( pIn1->flags & MEM_Int ){
 65557     sqlite3VdbeMemRealify(pIn1);
 65559   break;
 65561 #endif
 65563 #ifndef SQLITE_OMIT_CAST
 65564 /* Opcode: ToText P1 * * * *
 65565 **
 65566 ** Force the value in register P1 to be text.
 65567 ** If the value is numeric, convert it to a string using the
 65568 ** equivalent of printf().  Blob values are unchanged and
 65569 ** are afterwards simply interpreted as text.
 65570 **
 65571 ** A NULL value is not changed by this routine.  It remains NULL.
 65572 */
 65573 case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
 65574   pIn1 = &aMem[pOp->p1];
 65575   memAboutToChange(p, pIn1);
 65576   if( pIn1->flags & MEM_Null ) break;
 65577   assert( MEM_Str==(MEM_Blob>>3) );
 65578   pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
 65579   applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
 65580   rc = ExpandBlob(pIn1);
 65581   assert( pIn1->flags & MEM_Str || db->mallocFailed );
 65582   pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
 65583   UPDATE_MAX_BLOBSIZE(pIn1);
 65584   break;
 65587 /* Opcode: ToBlob P1 * * * *
 65588 **
 65589 ** Force the value in register P1 to be a BLOB.
 65590 ** If the value is numeric, convert it to a string first.
 65591 ** Strings are simply reinterpreted as blobs with no change
 65592 ** to the underlying data.
 65593 **
 65594 ** A NULL value is not changed by this routine.  It remains NULL.
 65595 */
 65596 case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
 65597   pIn1 = &aMem[pOp->p1];
 65598   if( pIn1->flags & MEM_Null ) break;
 65599   if( (pIn1->flags & MEM_Blob)==0 ){
 65600     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
 65601     assert( pIn1->flags & MEM_Str || db->mallocFailed );
 65602     MemSetTypeFlag(pIn1, MEM_Blob);
 65603   }else{
 65604     pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
 65606   UPDATE_MAX_BLOBSIZE(pIn1);
 65607   break;
 65610 /* Opcode: ToNumeric P1 * * * *
 65611 **
 65612 ** Force the value in register P1 to be numeric (either an
 65613 ** integer or a floating-point number.)
 65614 ** If the value is text or blob, try to convert it to an using the
 65615 ** equivalent of atoi() or atof() and store 0 if no such conversion 
 65616 ** is possible.
 65617 **
 65618 ** A NULL value is not changed by this routine.  It remains NULL.
 65619 */
 65620 case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
 65621   pIn1 = &aMem[pOp->p1];
 65622   sqlite3VdbeMemNumerify(pIn1);
 65623   break;
 65625 #endif /* SQLITE_OMIT_CAST */
 65627 /* Opcode: ToInt P1 * * * *
 65628 **
 65629 ** Force the value in register P1 to be an integer.  If
 65630 ** The value is currently a real number, drop its fractional part.
 65631 ** If the value is text or blob, try to convert it to an integer using the
 65632 ** equivalent of atoi() and store 0 if no such conversion is possible.
 65633 **
 65634 ** A NULL value is not changed by this routine.  It remains NULL.
 65635 */
 65636 case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
 65637   pIn1 = &aMem[pOp->p1];
 65638   if( (pIn1->flags & MEM_Null)==0 ){
 65639     sqlite3VdbeMemIntegerify(pIn1);
 65641   break;
 65644 #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
 65645 /* Opcode: ToReal P1 * * * *
 65646 **
 65647 ** Force the value in register P1 to be a floating point number.
 65648 ** If The value is currently an integer, convert it.
 65649 ** If the value is text or blob, try to convert it to an integer using the
 65650 ** equivalent of atoi() and store 0.0 if no such conversion is possible.
 65651 **
 65652 ** A NULL value is not changed by this routine.  It remains NULL.
 65653 */
 65654 case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
 65655   pIn1 = &aMem[pOp->p1];
 65656   memAboutToChange(p, pIn1);
 65657   if( (pIn1->flags & MEM_Null)==0 ){
 65658     sqlite3VdbeMemRealify(pIn1);
 65660   break;
 65662 #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
 65664 /* Opcode: Lt P1 P2 P3 P4 P5
 65665 **
 65666 ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
 65667 ** jump to address P2.  
 65668 **
 65669 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
 65670 ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
 65671 ** bit is clear then fall through if either operand is NULL.
 65672 **
 65673 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
 65674 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
 65675 ** to coerce both inputs according to this affinity before the
 65676 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
 65677 ** affinity is used. Note that the affinity conversions are stored
 65678 ** back into the input registers P1 and P3.  So this opcode can cause
 65679 ** persistent changes to registers P1 and P3.
 65680 **
 65681 ** Once any conversions have taken place, and neither value is NULL, 
 65682 ** the values are compared. If both values are blobs then memcmp() is
 65683 ** used to determine the results of the comparison.  If both values
 65684 ** are text, then the appropriate collating function specified in
 65685 ** P4 is  used to do the comparison.  If P4 is not specified then
 65686 ** memcmp() is used to compare text string.  If both values are
 65687 ** numeric, then a numeric comparison is used. If the two values
 65688 ** are of different types, then numbers are considered less than
 65689 ** strings and strings are considered less than blobs.
 65690 **
 65691 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
 65692 ** store a boolean result (either 0, or 1, or NULL) in register P2.
 65693 **
 65694 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
 65695 ** equal to one another, provided that they do not have their MEM_Cleared
 65696 ** bit set.
 65697 */
 65698 /* Opcode: Ne P1 P2 P3 P4 P5
 65699 **
 65700 ** This works just like the Lt opcode except that the jump is taken if
 65701 ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
 65702 ** additional information.
 65703 **
 65704 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 65705 ** true or false and is never NULL.  If both operands are NULL then the result
 65706 ** of comparison is false.  If either operand is NULL then the result is true.
 65707 ** If neither operand is NULL the result is the same as it would be if
 65708 ** the SQLITE_NULLEQ flag were omitted from P5.
 65709 */
 65710 /* Opcode: Eq P1 P2 P3 P4 P5
 65711 **
 65712 ** This works just like the Lt opcode except that the jump is taken if
 65713 ** the operands in registers P1 and P3 are equal.
 65714 ** See the Lt opcode for additional information.
 65715 **
 65716 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 65717 ** true or false and is never NULL.  If both operands are NULL then the result
 65718 ** of comparison is true.  If either operand is NULL then the result is false.
 65719 ** If neither operand is NULL the result is the same as it would be if
 65720 ** the SQLITE_NULLEQ flag were omitted from P5.
 65721 */
 65722 /* Opcode: Le P1 P2 P3 P4 P5
 65723 **
 65724 ** This works just like the Lt opcode except that the jump is taken if
 65725 ** the content of register P3 is less than or equal to the content of
 65726 ** register P1.  See the Lt opcode for additional information.
 65727 */
 65728 /* Opcode: Gt P1 P2 P3 P4 P5
 65729 **
 65730 ** This works just like the Lt opcode except that the jump is taken if
 65731 ** the content of register P3 is greater than the content of
 65732 ** register P1.  See the Lt opcode for additional information.
 65733 */
 65734 /* Opcode: Ge P1 P2 P3 P4 P5
 65735 **
 65736 ** This works just like the Lt opcode except that the jump is taken if
 65737 ** the content of register P3 is greater than or equal to the content of
 65738 ** register P1.  See the Lt opcode for additional information.
 65739 */
 65740 case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
 65741 case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
 65742 case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
 65743 case OP_Le:               /* same as TK_LE, jump, in1, in3 */
 65744 case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
 65745 case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
 65746 #if 0  /* local variables moved into u.ak */
 65747   int res;            /* Result of the comparison of pIn1 against pIn3 */
 65748   char affinity;      /* Affinity to use for comparison */
 65749   u16 flags1;         /* Copy of initial value of pIn1->flags */
 65750   u16 flags3;         /* Copy of initial value of pIn3->flags */
 65751 #endif /* local variables moved into u.ak */
 65753   pIn1 = &aMem[pOp->p1];
 65754   pIn3 = &aMem[pOp->p3];
 65755   u.ak.flags1 = pIn1->flags;
 65756   u.ak.flags3 = pIn3->flags;
 65757   if( (u.ak.flags1 | u.ak.flags3)&MEM_Null ){
 65758     /* One or both operands are NULL */
 65759     if( pOp->p5 & SQLITE_NULLEQ ){
 65760       /* If SQLITE_NULLEQ is set (which will only happen if the operator is
 65761       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
 65762       ** or not both operands are null.
 65763       */
 65764       assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 65765       assert( (u.ak.flags1 & MEM_Cleared)==0 );
 65766       if( (u.ak.flags1&MEM_Null)!=0
 65767        && (u.ak.flags3&MEM_Null)!=0
 65768        && (u.ak.flags3&MEM_Cleared)==0
 65769       ){
 65770         u.ak.res = 0;  /* Results are equal */
 65771       }else{
 65772         u.ak.res = 1;  /* Results are not equal */
 65774     }else{
 65775       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 65776       ** then the result is always NULL.
 65777       ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 65778       */
 65779       if( pOp->p5 & SQLITE_STOREP2 ){
 65780         pOut = &aMem[pOp->p2];
 65781         MemSetTypeFlag(pOut, MEM_Null);
 65782         REGISTER_TRACE(pOp->p2, pOut);
 65783       }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
 65784         pc = pOp->p2-1;
 65786       break;
 65788   }else{
 65789     /* Neither operand is NULL.  Do a comparison. */
 65790     u.ak.affinity = pOp->p5 & SQLITE_AFF_MASK;
 65791     if( u.ak.affinity ){
 65792       applyAffinity(pIn1, u.ak.affinity, encoding);
 65793       applyAffinity(pIn3, u.ak.affinity, encoding);
 65794       if( db->mallocFailed ) goto no_mem;
 65797     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
 65798     ExpandBlob(pIn1);
 65799     ExpandBlob(pIn3);
 65800     u.ak.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
 65802   switch( pOp->opcode ){
 65803     case OP_Eq:    u.ak.res = u.ak.res==0;     break;
 65804     case OP_Ne:    u.ak.res = u.ak.res!=0;     break;
 65805     case OP_Lt:    u.ak.res = u.ak.res<0;      break;
 65806     case OP_Le:    u.ak.res = u.ak.res<=0;     break;
 65807     case OP_Gt:    u.ak.res = u.ak.res>0;      break;
 65808     default:       u.ak.res = u.ak.res>=0;     break;
 65811   if( pOp->p5 & SQLITE_STOREP2 ){
 65812     pOut = &aMem[pOp->p2];
 65813     memAboutToChange(p, pOut);
 65814     MemSetTypeFlag(pOut, MEM_Int);
 65815     pOut->u.i = u.ak.res;
 65816     REGISTER_TRACE(pOp->p2, pOut);
 65817   }else if( u.ak.res ){
 65818     pc = pOp->p2-1;
 65821   /* Undo any changes made by applyAffinity() to the input registers. */
 65822   pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ak.flags1&MEM_TypeMask);
 65823   pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ak.flags3&MEM_TypeMask);
 65824   break;
 65827 /* Opcode: Permutation * * * P4 *
 65828 **
 65829 ** Set the permutation used by the OP_Compare operator to be the array
 65830 ** of integers in P4.
 65831 **
 65832 ** The permutation is only valid until the next OP_Compare that has
 65833 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
 65834 ** occur immediately prior to the OP_Compare.
 65835 */
 65836 case OP_Permutation: {
 65837   assert( pOp->p4type==P4_INTARRAY );
 65838   assert( pOp->p4.ai );
 65839   aPermute = pOp->p4.ai;
 65840   break;
 65843 /* Opcode: Compare P1 P2 P3 P4 P5
 65844 **
 65845 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
 65846 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
 65847 ** the comparison for use by the next OP_Jump instruct.
 65848 **
 65849 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
 65850 ** determined by the most recent OP_Permutation operator.  If the
 65851 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
 65852 ** order.
 65853 **
 65854 ** P4 is a KeyInfo structure that defines collating sequences and sort
 65855 ** orders for the comparison.  The permutation applies to registers
 65856 ** only.  The KeyInfo elements are used sequentially.
 65857 **
 65858 ** The comparison is a sort comparison, so NULLs compare equal,
 65859 ** NULLs are less than numbers, numbers are less than strings,
 65860 ** and strings are less than blobs.
 65861 */
 65862 case OP_Compare: {
 65863 #if 0  /* local variables moved into u.al */
 65864   int n;
 65865   int i;
 65866   int p1;
 65867   int p2;
 65868   const KeyInfo *pKeyInfo;
 65869   int idx;
 65870   CollSeq *pColl;    /* Collating sequence to use on this term */
 65871   int bRev;          /* True for DESCENDING sort order */
 65872 #endif /* local variables moved into u.al */
 65874   if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
 65875   u.al.n = pOp->p3;
 65876   u.al.pKeyInfo = pOp->p4.pKeyInfo;
 65877   assert( u.al.n>0 );
 65878   assert( u.al.pKeyInfo!=0 );
 65879   u.al.p1 = pOp->p1;
 65880   u.al.p2 = pOp->p2;
 65881 #if SQLITE_DEBUG
 65882   if( aPermute ){
 65883     int k, mx = 0;
 65884     for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
 65885     assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
 65886     assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
 65887   }else{
 65888     assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
 65889     assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
 65891 #endif /* SQLITE_DEBUG */
 65892   for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
 65893     u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
 65894     assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
 65895     assert( memIsValid(&aMem[u.al.p2+u.al.idx]) );
 65896     REGISTER_TRACE(u.al.p1+u.al.idx, &aMem[u.al.p1+u.al.idx]);
 65897     REGISTER_TRACE(u.al.p2+u.al.idx, &aMem[u.al.p2+u.al.idx]);
 65898     assert( u.al.i<u.al.pKeyInfo->nField );
 65899     u.al.pColl = u.al.pKeyInfo->aColl[u.al.i];
 65900     u.al.bRev = u.al.pKeyInfo->aSortOrder[u.al.i];
 65901     iCompare = sqlite3MemCompare(&aMem[u.al.p1+u.al.idx], &aMem[u.al.p2+u.al.idx], u.al.pColl);
 65902     if( iCompare ){
 65903       if( u.al.bRev ) iCompare = -iCompare;
 65904       break;
 65907   aPermute = 0;
 65908   break;
 65911 /* Opcode: Jump P1 P2 P3 * *
 65912 **
 65913 ** Jump to the instruction at address P1, P2, or P3 depending on whether
 65914 ** in the most recent OP_Compare instruction the P1 vector was less than
 65915 ** equal to, or greater than the P2 vector, respectively.
 65916 */
 65917 case OP_Jump: {             /* jump */
 65918   if( iCompare<0 ){
 65919     pc = pOp->p1 - 1;
 65920   }else if( iCompare==0 ){
 65921     pc = pOp->p2 - 1;
 65922   }else{
 65923     pc = pOp->p3 - 1;
 65925   break;
 65928 /* Opcode: And P1 P2 P3 * *
 65929 **
 65930 ** Take the logical AND of the values in registers P1 and P2 and
 65931 ** write the result into register P3.
 65932 **
 65933 ** If either P1 or P2 is 0 (false) then the result is 0 even if
 65934 ** the other input is NULL.  A NULL and true or two NULLs give
 65935 ** a NULL output.
 65936 */
 65937 /* Opcode: Or P1 P2 P3 * *
 65938 **
 65939 ** Take the logical OR of the values in register P1 and P2 and
 65940 ** store the answer in register P3.
 65941 **
 65942 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
 65943 ** even if the other input is NULL.  A NULL and false or two NULLs
 65944 ** give a NULL output.
 65945 */
 65946 case OP_And:              /* same as TK_AND, in1, in2, out3 */
 65947 case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
 65948 #if 0  /* local variables moved into u.am */
 65949   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 65950   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 65951 #endif /* local variables moved into u.am */
 65953   pIn1 = &aMem[pOp->p1];
 65954   if( pIn1->flags & MEM_Null ){
 65955     u.am.v1 = 2;
 65956   }else{
 65957     u.am.v1 = sqlite3VdbeIntValue(pIn1)!=0;
 65959   pIn2 = &aMem[pOp->p2];
 65960   if( pIn2->flags & MEM_Null ){
 65961     u.am.v2 = 2;
 65962   }else{
 65963     u.am.v2 = sqlite3VdbeIntValue(pIn2)!=0;
 65965   if( pOp->opcode==OP_And ){
 65966     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
 65967     u.am.v1 = and_logic[u.am.v1*3+u.am.v2];
 65968   }else{
 65969     static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
 65970     u.am.v1 = or_logic[u.am.v1*3+u.am.v2];
 65972   pOut = &aMem[pOp->p3];
 65973   if( u.am.v1==2 ){
 65974     MemSetTypeFlag(pOut, MEM_Null);
 65975   }else{
 65976     pOut->u.i = u.am.v1;
 65977     MemSetTypeFlag(pOut, MEM_Int);
 65979   break;
 65982 /* Opcode: Not P1 P2 * * *
 65983 **
 65984 ** Interpret the value in register P1 as a boolean value.  Store the
 65985 ** boolean complement in register P2.  If the value in register P1 is 
 65986 ** NULL, then a NULL is stored in P2.
 65987 */
 65988 case OP_Not: {                /* same as TK_NOT, in1, out2 */
 65989   pIn1 = &aMem[pOp->p1];
 65990   pOut = &aMem[pOp->p2];
 65991   if( pIn1->flags & MEM_Null ){
 65992     sqlite3VdbeMemSetNull(pOut);
 65993   }else{
 65994     sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
 65996   break;
 65999 /* Opcode: BitNot P1 P2 * * *
 66000 **
 66001 ** Interpret the content of register P1 as an integer.  Store the
 66002 ** ones-complement of the P1 value into register P2.  If P1 holds
 66003 ** a NULL then store a NULL in P2.
 66004 */
 66005 case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
 66006   pIn1 = &aMem[pOp->p1];
 66007   pOut = &aMem[pOp->p2];
 66008   if( pIn1->flags & MEM_Null ){
 66009     sqlite3VdbeMemSetNull(pOut);
 66010   }else{
 66011     sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
 66013   break;
 66016 /* Opcode: Once P1 P2 * * *
 66017 **
 66018 ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
 66019 ** set the flag and fall through to the next instruction.
 66020 */
 66021 case OP_Once: {             /* jump */
 66022   assert( pOp->p1<p->nOnceFlag );
 66023   if( p->aOnceFlag[pOp->p1] ){
 66024     pc = pOp->p2-1;
 66025   }else{
 66026     p->aOnceFlag[pOp->p1] = 1;
 66028   break;
 66031 /* Opcode: If P1 P2 P3 * *
 66032 **
 66033 ** Jump to P2 if the value in register P1 is true.  The value
 66034 ** is considered true if it is numeric and non-zero.  If the value
 66035 ** in P1 is NULL then take the jump if P3 is non-zero.
 66036 */
 66037 /* Opcode: IfNot P1 P2 P3 * *
 66038 **
 66039 ** Jump to P2 if the value in register P1 is False.  The value
 66040 ** is considered false if it has a numeric value of zero.  If the value
 66041 ** in P1 is NULL then take the jump if P3 is zero.
 66042 */
 66043 case OP_If:                 /* jump, in1 */
 66044 case OP_IfNot: {            /* jump, in1 */
 66045 #if 0  /* local variables moved into u.an */
 66046   int c;
 66047 #endif /* local variables moved into u.an */
 66048   pIn1 = &aMem[pOp->p1];
 66049   if( pIn1->flags & MEM_Null ){
 66050     u.an.c = pOp->p3;
 66051   }else{
 66052 #ifdef SQLITE_OMIT_FLOATING_POINT
 66053     u.an.c = sqlite3VdbeIntValue(pIn1)!=0;
 66054 #else
 66055     u.an.c = sqlite3VdbeRealValue(pIn1)!=0.0;
 66056 #endif
 66057     if( pOp->opcode==OP_IfNot ) u.an.c = !u.an.c;
 66059   if( u.an.c ){
 66060     pc = pOp->p2-1;
 66062   break;
 66065 /* Opcode: IsNull P1 P2 * * *
 66066 **
 66067 ** Jump to P2 if the value in register P1 is NULL.
 66068 */
 66069 case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
 66070   pIn1 = &aMem[pOp->p1];
 66071   if( (pIn1->flags & MEM_Null)!=0 ){
 66072     pc = pOp->p2 - 1;
 66074   break;
 66077 /* Opcode: NotNull P1 P2 * * *
 66078 **
 66079 ** Jump to P2 if the value in register P1 is not NULL.  
 66080 */
 66081 case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
 66082   pIn1 = &aMem[pOp->p1];
 66083   if( (pIn1->flags & MEM_Null)==0 ){
 66084     pc = pOp->p2 - 1;
 66086   break;
 66089 /* Opcode: Column P1 P2 P3 P4 P5
 66090 **
 66091 ** Interpret the data that cursor P1 points to as a structure built using
 66092 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
 66093 ** information about the format of the data.)  Extract the P2-th column
 66094 ** from this record.  If there are less that (P2+1) 
 66095 ** values in the record, extract a NULL.
 66096 **
 66097 ** The value extracted is stored in register P3.
 66098 **
 66099 ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
 66100 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
 66101 ** the result.
 66102 **
 66103 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
 66104 ** then the cache of the cursor is reset prior to extracting the column.
 66105 ** The first OP_Column against a pseudo-table after the value of the content
 66106 ** register has changed should have this bit set.
 66107 **
 66108 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
 66109 ** the result is guaranteed to only be used as the argument of a length()
 66110 ** or typeof() function, respectively.  The loading of large blobs can be
 66111 ** skipped for length() and all content loading can be skipped for typeof().
 66112 */
 66113 case OP_Column: {
 66114 #if 0  /* local variables moved into u.ao */
 66115   u32 payloadSize;   /* Number of bytes in the record */
 66116   i64 payloadSize64; /* Number of bytes in the record */
 66117   int p1;            /* P1 value of the opcode */
 66118   int p2;            /* column number to retrieve */
 66119   VdbeCursor *pC;    /* The VDBE cursor */
 66120   char *zRec;        /* Pointer to complete record-data */
 66121   BtCursor *pCrsr;   /* The BTree cursor */
 66122   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
 66123   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
 66124   int nField;        /* number of fields in the record */
 66125   int len;           /* The length of the serialized data for the column */
 66126   int i;             /* Loop counter */
 66127   char *zData;       /* Part of the record being decoded */
 66128   Mem *pDest;        /* Where to write the extracted value */
 66129   Mem sMem;          /* For storing the record being decoded */
 66130   u8 *zIdx;          /* Index into header */
 66131   u8 *zEndHdr;       /* Pointer to first byte after the header */
 66132   u32 offset;        /* Offset into the data */
 66133   u32 szField;       /* Number of bytes in the content of a field */
 66134   int szHdr;         /* Size of the header size field at start of record */
 66135   int avail;         /* Number of bytes of available data */
 66136   u32 t;             /* A type code from the record header */
 66137   Mem *pReg;         /* PseudoTable input register */
 66138 #endif /* local variables moved into u.ao */
 66141   u.ao.p1 = pOp->p1;
 66142   u.ao.p2 = pOp->p2;
 66143   u.ao.pC = 0;
 66144   memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
 66145   assert( u.ao.p1<p->nCursor );
 66146   assert( pOp->p3>0 && pOp->p3<=p->nMem );
 66147   u.ao.pDest = &aMem[pOp->p3];
 66148   memAboutToChange(p, u.ao.pDest);
 66149   u.ao.zRec = 0;
 66151   /* This block sets the variable u.ao.payloadSize to be the total number of
 66152   ** bytes in the record.
 66153   **
 66154   ** u.ao.zRec is set to be the complete text of the record if it is available.
 66155   ** The complete record text is always available for pseudo-tables
 66156   ** If the record is stored in a cursor, the complete record text
 66157   ** might be available in the  u.ao.pC->aRow cache.  Or it might not be.
 66158   ** If the data is unavailable,  u.ao.zRec is set to NULL.
 66159   **
 66160   ** We also compute the number of columns in the record.  For cursors,
 66161   ** the number of columns is stored in the VdbeCursor.nField element.
 66162   */
 66163   u.ao.pC = p->apCsr[u.ao.p1];
 66164   assert( u.ao.pC!=0 );
 66165 #ifndef SQLITE_OMIT_VIRTUALTABLE
 66166   assert( u.ao.pC->pVtabCursor==0 );
 66167 #endif
 66168   u.ao.pCrsr = u.ao.pC->pCursor;
 66169   if( u.ao.pCrsr!=0 ){
 66170     /* The record is stored in a B-Tree */
 66171     rc = sqlite3VdbeCursorMoveto(u.ao.pC);
 66172     if( rc ) goto abort_due_to_error;
 66173     if( u.ao.pC->nullRow ){
 66174       u.ao.payloadSize = 0;
 66175     }else if( u.ao.pC->cacheStatus==p->cacheCtr ){
 66176       u.ao.payloadSize = u.ao.pC->payloadSize;
 66177       u.ao.zRec = (char*)u.ao.pC->aRow;
 66178     }else if( u.ao.pC->isIndex ){
 66179       assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
 66180       VVA_ONLY(rc =) sqlite3BtreeKeySize(u.ao.pCrsr, &u.ao.payloadSize64);
 66181       assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
 66182       /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
 66183       ** payload size, so it is impossible for u.ao.payloadSize64 to be
 66184       ** larger than 32 bits. */
 66185       assert( (u.ao.payloadSize64 & SQLITE_MAX_U32)==(u64)u.ao.payloadSize64 );
 66186       u.ao.payloadSize = (u32)u.ao.payloadSize64;
 66187     }else{
 66188       assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
 66189       VVA_ONLY(rc =) sqlite3BtreeDataSize(u.ao.pCrsr, &u.ao.payloadSize);
 66190       assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
 66192   }else if( ALWAYS(u.ao.pC->pseudoTableReg>0) ){
 66193     u.ao.pReg = &aMem[u.ao.pC->pseudoTableReg];
 66194     if( u.ao.pC->multiPseudo ){
 66195       sqlite3VdbeMemShallowCopy(u.ao.pDest, u.ao.pReg+u.ao.p2, MEM_Ephem);
 66196       Deephemeralize(u.ao.pDest);
 66197       goto op_column_out;
 66199     assert( u.ao.pReg->flags & MEM_Blob );
 66200     assert( memIsValid(u.ao.pReg) );
 66201     u.ao.payloadSize = u.ao.pReg->n;
 66202     u.ao.zRec = u.ao.pReg->z;
 66203     u.ao.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
 66204     assert( u.ao.payloadSize==0 || u.ao.zRec!=0 );
 66205   }else{
 66206     /* Consider the row to be NULL */
 66207     u.ao.payloadSize = 0;
 66210   /* If u.ao.payloadSize is 0, then just store a NULL.  This can happen because of
 66211   ** nullRow or because of a corrupt database. */
 66212   if( u.ao.payloadSize==0 ){
 66213     MemSetTypeFlag(u.ao.pDest, MEM_Null);
 66214     goto op_column_out;
 66216   assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
 66217   if( u.ao.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 66218     goto too_big;
 66221   u.ao.nField = u.ao.pC->nField;
 66222   assert( u.ao.p2<u.ao.nField );
 66224   /* Read and parse the table header.  Store the results of the parse
 66225   ** into the record header cache fields of the cursor.
 66226   */
 66227   u.ao.aType = u.ao.pC->aType;
 66228   if( u.ao.pC->cacheStatus==p->cacheCtr ){
 66229     u.ao.aOffset = u.ao.pC->aOffset;
 66230   }else{
 66231     assert(u.ao.aType);
 66232     u.ao.avail = 0;
 66233     u.ao.pC->aOffset = u.ao.aOffset = &u.ao.aType[u.ao.nField];
 66234     u.ao.pC->payloadSize = u.ao.payloadSize;
 66235     u.ao.pC->cacheStatus = p->cacheCtr;
 66237     /* Figure out how many bytes are in the header */
 66238     if( u.ao.zRec ){
 66239       u.ao.zData = u.ao.zRec;
 66240     }else{
 66241       if( u.ao.pC->isIndex ){
 66242         u.ao.zData = (char*)sqlite3BtreeKeyFetch(u.ao.pCrsr, &u.ao.avail);
 66243       }else{
 66244         u.ao.zData = (char*)sqlite3BtreeDataFetch(u.ao.pCrsr, &u.ao.avail);
 66246       /* If KeyFetch()/DataFetch() managed to get the entire payload,
 66247       ** save the payload in the u.ao.pC->aRow cache.  That will save us from
 66248       ** having to make additional calls to fetch the content portion of
 66249       ** the record.
 66250       */
 66251       assert( u.ao.avail>=0 );
 66252       if( u.ao.payloadSize <= (u32)u.ao.avail ){
 66253         u.ao.zRec = u.ao.zData;
 66254         u.ao.pC->aRow = (u8*)u.ao.zData;
 66255       }else{
 66256         u.ao.pC->aRow = 0;
 66259     /* The following assert is true in all cases except when
 66260     ** the database file has been corrupted externally.
 66261     **    assert( u.ao.zRec!=0 || u.ao.avail>=u.ao.payloadSize || u.ao.avail>=9 ); */
 66262     u.ao.szHdr = getVarint32((u8*)u.ao.zData, u.ao.offset);
 66264     /* Make sure a corrupt database has not given us an oversize header.
 66265     ** Do this now to avoid an oversize memory allocation.
 66266     **
 66267     ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
 66268     ** types use so much data space that there can only be 4096 and 32 of
 66269     ** them, respectively.  So the maximum header length results from a
 66270     ** 3-byte type for each of the maximum of 32768 columns plus three
 66271     ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
 66272     */
 66273     if( u.ao.offset > 98307 ){
 66274       rc = SQLITE_CORRUPT_BKPT;
 66275       goto op_column_out;
 66278     /* Compute in u.ao.len the number of bytes of data we need to read in order
 66279     ** to get u.ao.nField type values.  u.ao.offset is an upper bound on this.  But
 66280     ** u.ao.nField might be significantly less than the true number of columns
 66281     ** in the table, and in that case, 5*u.ao.nField+3 might be smaller than u.ao.offset.
 66282     ** We want to minimize u.ao.len in order to limit the size of the memory
 66283     ** allocation, especially if a corrupt database file has caused u.ao.offset
 66284     ** to be oversized. Offset is limited to 98307 above.  But 98307 might
 66285     ** still exceed Robson memory allocation limits on some configurations.
 66286     ** On systems that cannot tolerate large memory allocations, u.ao.nField*5+3
 66287     ** will likely be much smaller since u.ao.nField will likely be less than
 66288     ** 20 or so.  This insures that Robson memory allocation limits are
 66289     ** not exceeded even for corrupt database files.
 66290     */
 66291     u.ao.len = u.ao.nField*5 + 3;
 66292     if( u.ao.len > (int)u.ao.offset ) u.ao.len = (int)u.ao.offset;
 66294     /* The KeyFetch() or DataFetch() above are fast and will get the entire
 66295     ** record header in most cases.  But they will fail to get the complete
 66296     ** record header if the record header does not fit on a single page
 66297     ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
 66298     ** acquire the complete header text.
 66299     */
 66300     if( !u.ao.zRec && u.ao.avail<u.ao.len ){
 66301       u.ao.sMem.flags = 0;
 66302       u.ao.sMem.db = 0;
 66303       rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, 0, u.ao.len, u.ao.pC->isIndex, &u.ao.sMem);
 66304       if( rc!=SQLITE_OK ){
 66305         goto op_column_out;
 66307       u.ao.zData = u.ao.sMem.z;
 66309     u.ao.zEndHdr = (u8 *)&u.ao.zData[u.ao.len];
 66310     u.ao.zIdx = (u8 *)&u.ao.zData[u.ao.szHdr];
 66312     /* Scan the header and use it to fill in the u.ao.aType[] and u.ao.aOffset[]
 66313     ** arrays.  u.ao.aType[u.ao.i] will contain the type integer for the u.ao.i-th
 66314     ** column and u.ao.aOffset[u.ao.i] will contain the u.ao.offset from the beginning
 66315     ** of the record to the start of the data for the u.ao.i-th column
 66316     */
 66317     for(u.ao.i=0; u.ao.i<u.ao.nField; u.ao.i++){
 66318       if( u.ao.zIdx<u.ao.zEndHdr ){
 66319         u.ao.aOffset[u.ao.i] = u.ao.offset;
 66320         if( u.ao.zIdx[0]<0x80 ){
 66321           u.ao.t = u.ao.zIdx[0];
 66322           u.ao.zIdx++;
 66323         }else{
 66324           u.ao.zIdx += sqlite3GetVarint32(u.ao.zIdx, &u.ao.t);
 66326         u.ao.aType[u.ao.i] = u.ao.t;
 66327         u.ao.szField = sqlite3VdbeSerialTypeLen(u.ao.t);
 66328         u.ao.offset += u.ao.szField;
 66329         if( u.ao.offset<u.ao.szField ){  /* True if u.ao.offset overflows */
 66330           u.ao.zIdx = &u.ao.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
 66331           break;
 66333       }else{
 66334         /* If u.ao.i is less that u.ao.nField, then there are fewer fields in this
 66335         ** record than SetNumColumns indicated there are columns in the
 66336         ** table. Set the u.ao.offset for any extra columns not present in
 66337         ** the record to 0. This tells code below to store the default value
 66338         ** for the column instead of deserializing a value from the record.
 66339         */
 66340         u.ao.aOffset[u.ao.i] = 0;
 66343     sqlite3VdbeMemRelease(&u.ao.sMem);
 66344     u.ao.sMem.flags = MEM_Null;
 66346     /* If we have read more header data than was contained in the header,
 66347     ** or if the end of the last field appears to be past the end of the
 66348     ** record, or if the end of the last field appears to be before the end
 66349     ** of the record (when all fields present), then we must be dealing
 66350     ** with a corrupt database.
 66351     */
 66352     if( (u.ao.zIdx > u.ao.zEndHdr) || (u.ao.offset > u.ao.payloadSize)
 66353          || (u.ao.zIdx==u.ao.zEndHdr && u.ao.offset!=u.ao.payloadSize) ){
 66354       rc = SQLITE_CORRUPT_BKPT;
 66355       goto op_column_out;
 66359   /* Get the column information. If u.ao.aOffset[u.ao.p2] is non-zero, then
 66360   ** deserialize the value from the record. If u.ao.aOffset[u.ao.p2] is zero,
 66361   ** then there are not enough fields in the record to satisfy the
 66362   ** request.  In this case, set the value NULL or to P4 if P4 is
 66363   ** a pointer to a Mem object.
 66364   */
 66365   if( u.ao.aOffset[u.ao.p2] ){
 66366     assert( rc==SQLITE_OK );
 66367     if( u.ao.zRec ){
 66368       /* This is the common case where the whole row fits on a single page */
 66369       VdbeMemRelease(u.ao.pDest);
 66370       sqlite3VdbeSerialGet((u8 *)&u.ao.zRec[u.ao.aOffset[u.ao.p2]], u.ao.aType[u.ao.p2], u.ao.pDest);
 66371     }else{
 66372       /* This branch happens only when the row overflows onto multiple pages */
 66373       u.ao.t = u.ao.aType[u.ao.p2];
 66374       if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
 66375        && ((u.ao.t>=12 && (u.ao.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
 66376       ){
 66377         /* Content is irrelevant for the typeof() function and for
 66378         ** the length(X) function if X is a blob.  So we might as well use
 66379         ** bogus content rather than reading content from disk.  NULL works
 66380         ** for text and blob and whatever is in the u.ao.payloadSize64 variable
 66381         ** will work for everything else. */
 66382         u.ao.zData = u.ao.t<12 ? (char*)&u.ao.payloadSize64 : 0;
 66383       }else{
 66384         u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.t);
 66385         sqlite3VdbeMemMove(&u.ao.sMem, u.ao.pDest);
 66386         rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, u.ao.aOffset[u.ao.p2], u.ao.len,  u.ao.pC->isIndex,
 66387                                      &u.ao.sMem);
 66388         if( rc!=SQLITE_OK ){
 66389           goto op_column_out;
 66391         u.ao.zData = u.ao.sMem.z;
 66393       sqlite3VdbeSerialGet((u8*)u.ao.zData, u.ao.t, u.ao.pDest);
 66395     u.ao.pDest->enc = encoding;
 66396   }else{
 66397     if( pOp->p4type==P4_MEM ){
 66398       sqlite3VdbeMemShallowCopy(u.ao.pDest, pOp->p4.pMem, MEM_Static);
 66399     }else{
 66400       MemSetTypeFlag(u.ao.pDest, MEM_Null);
 66404   /* If we dynamically allocated space to hold the data (in the
 66405   ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
 66406   ** dynamically allocated space over to the u.ao.pDest structure.
 66407   ** This prevents a memory copy.
 66408   */
 66409   if( u.ao.sMem.zMalloc ){
 66410     assert( u.ao.sMem.z==u.ao.sMem.zMalloc );
 66411     assert( !(u.ao.pDest->flags & MEM_Dyn) );
 66412     assert( !(u.ao.pDest->flags & (MEM_Blob|MEM_Str)) || u.ao.pDest->z==u.ao.sMem.z );
 66413     u.ao.pDest->flags &= ~(MEM_Ephem|MEM_Static);
 66414     u.ao.pDest->flags |= MEM_Term;
 66415     u.ao.pDest->z = u.ao.sMem.z;
 66416     u.ao.pDest->zMalloc = u.ao.sMem.zMalloc;
 66419   rc = sqlite3VdbeMemMakeWriteable(u.ao.pDest);
 66421 op_column_out:
 66422   UPDATE_MAX_BLOBSIZE(u.ao.pDest);
 66423   REGISTER_TRACE(pOp->p3, u.ao.pDest);
 66424   break;
 66427 /* Opcode: Affinity P1 P2 * P4 *
 66428 **
 66429 ** Apply affinities to a range of P2 registers starting with P1.
 66430 **
 66431 ** P4 is a string that is P2 characters long. The nth character of the
 66432 ** string indicates the column affinity that should be used for the nth
 66433 ** memory cell in the range.
 66434 */
 66435 case OP_Affinity: {
 66436 #if 0  /* local variables moved into u.ap */
 66437   const char *zAffinity;   /* The affinity to be applied */
 66438   char cAff;               /* A single character of affinity */
 66439 #endif /* local variables moved into u.ap */
 66441   u.ap.zAffinity = pOp->p4.z;
 66442   assert( u.ap.zAffinity!=0 );
 66443   assert( u.ap.zAffinity[pOp->p2]==0 );
 66444   pIn1 = &aMem[pOp->p1];
 66445   while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
 66446     assert( pIn1 <= &p->aMem[p->nMem] );
 66447     assert( memIsValid(pIn1) );
 66448     ExpandBlob(pIn1);
 66449     applyAffinity(pIn1, u.ap.cAff, encoding);
 66450     pIn1++;
 66452   break;
 66455 /* Opcode: MakeRecord P1 P2 P3 P4 *
 66456 **
 66457 ** Convert P2 registers beginning with P1 into the [record format]
 66458 ** use as a data record in a database table or as a key
 66459 ** in an index.  The OP_Column opcode can decode the record later.
 66460 **
 66461 ** P4 may be a string that is P2 characters long.  The nth character of the
 66462 ** string indicates the column affinity that should be used for the nth
 66463 ** field of the index key.
 66464 **
 66465 ** The mapping from character to affinity is given by the SQLITE_AFF_
 66466 ** macros defined in sqliteInt.h.
 66467 **
 66468 ** If P4 is NULL then all index fields have the affinity NONE.
 66469 */
 66470 case OP_MakeRecord: {
 66471 #if 0  /* local variables moved into u.aq */
 66472   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
 66473   Mem *pRec;             /* The new record */
 66474   u64 nData;             /* Number of bytes of data space */
 66475   int nHdr;              /* Number of bytes of header space */
 66476   i64 nByte;             /* Data space required for this record */
 66477   int nZero;             /* Number of zero bytes at the end of the record */
 66478   int nVarint;           /* Number of bytes in a varint */
 66479   u32 serial_type;       /* Type field */
 66480   Mem *pData0;           /* First field to be combined into the record */
 66481   Mem *pLast;            /* Last field of the record */
 66482   int nField;            /* Number of fields in the record */
 66483   char *zAffinity;       /* The affinity string for the record */
 66484   int file_format;       /* File format to use for encoding */
 66485   int i;                 /* Space used in zNewRecord[] */
 66486   int len;               /* Length of a field */
 66487 #endif /* local variables moved into u.aq */
 66489   /* Assuming the record contains N fields, the record format looks
 66490   ** like this:
 66491   **
 66492   ** ------------------------------------------------------------------------
 66493   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
 66494   ** ------------------------------------------------------------------------
 66495   **
 66496   ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
 66497   ** and so froth.
 66498   **
 66499   ** Each type field is a varint representing the serial type of the
 66500   ** corresponding data element (see sqlite3VdbeSerialType()). The
 66501   ** hdr-size field is also a varint which is the offset from the beginning
 66502   ** of the record to data0.
 66503   */
 66504   u.aq.nData = 0;         /* Number of bytes of data space */
 66505   u.aq.nHdr = 0;          /* Number of bytes of header space */
 66506   u.aq.nZero = 0;         /* Number of zero bytes at the end of the record */
 66507   u.aq.nField = pOp->p1;
 66508   u.aq.zAffinity = pOp->p4.z;
 66509   assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
 66510   u.aq.pData0 = &aMem[u.aq.nField];
 66511   u.aq.nField = pOp->p2;
 66512   u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
 66513   u.aq.file_format = p->minWriteFileFormat;
 66515   /* Identify the output register */
 66516   assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
 66517   pOut = &aMem[pOp->p3];
 66518   memAboutToChange(p, pOut);
 66520   /* Loop through the elements that will make up the record to figure
 66521   ** out how much space is required for the new record.
 66522   */
 66523   for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
 66524     assert( memIsValid(u.aq.pRec) );
 66525     if( u.aq.zAffinity ){
 66526       applyAffinity(u.aq.pRec, u.aq.zAffinity[u.aq.pRec-u.aq.pData0], encoding);
 66528     if( u.aq.pRec->flags&MEM_Zero && u.aq.pRec->n>0 ){
 66529       sqlite3VdbeMemExpandBlob(u.aq.pRec);
 66531     u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
 66532     u.aq.len = sqlite3VdbeSerialTypeLen(u.aq.serial_type);
 66533     u.aq.nData += u.aq.len;
 66534     u.aq.nHdr += sqlite3VarintLen(u.aq.serial_type);
 66535     if( u.aq.pRec->flags & MEM_Zero ){
 66536       /* Only pure zero-filled BLOBs can be input to this Opcode.
 66537       ** We do not allow blobs with a prefix and a zero-filled tail. */
 66538       u.aq.nZero += u.aq.pRec->u.nZero;
 66539     }else if( u.aq.len ){
 66540       u.aq.nZero = 0;
 66544   /* Add the initial header varint and total the size */
 66545   u.aq.nHdr += u.aq.nVarint = sqlite3VarintLen(u.aq.nHdr);
 66546   if( u.aq.nVarint<sqlite3VarintLen(u.aq.nHdr) ){
 66547     u.aq.nHdr++;
 66549   u.aq.nByte = u.aq.nHdr+u.aq.nData-u.aq.nZero;
 66550   if( u.aq.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 66551     goto too_big;
 66554   /* Make sure the output register has a buffer large enough to store
 66555   ** the new record. The output register (pOp->p3) is not allowed to
 66556   ** be one of the input registers (because the following call to
 66557   ** sqlite3VdbeMemGrow() could clobber the value before it is used).
 66558   */
 66559   if( sqlite3VdbeMemGrow(pOut, (int)u.aq.nByte, 0) ){
 66560     goto no_mem;
 66562   u.aq.zNewRecord = (u8 *)pOut->z;
 66564   /* Write the record */
 66565   u.aq.i = putVarint32(u.aq.zNewRecord, u.aq.nHdr);
 66566   for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
 66567     u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
 66568     u.aq.i += putVarint32(&u.aq.zNewRecord[u.aq.i], u.aq.serial_type);      /* serial type */
 66570   for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){  /* serial data */
 66571     u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
 66573   assert( u.aq.i==u.aq.nByte );
 66575   assert( pOp->p3>0 && pOp->p3<=p->nMem );
 66576   pOut->n = (int)u.aq.nByte;
 66577   pOut->flags = MEM_Blob | MEM_Dyn;
 66578   pOut->xDel = 0;
 66579   if( u.aq.nZero ){
 66580     pOut->u.nZero = u.aq.nZero;
 66581     pOut->flags |= MEM_Zero;
 66583   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
 66584   REGISTER_TRACE(pOp->p3, pOut);
 66585   UPDATE_MAX_BLOBSIZE(pOut);
 66586   break;
 66589 /* Opcode: Count P1 P2 * * *
 66590 **
 66591 ** Store the number of entries (an integer value) in the table or index 
 66592 ** opened by cursor P1 in register P2
 66593 */
 66594 #ifndef SQLITE_OMIT_BTREECOUNT
 66595 case OP_Count: {         /* out2-prerelease */
 66596 #if 0  /* local variables moved into u.ar */
 66597   i64 nEntry;
 66598   BtCursor *pCrsr;
 66599 #endif /* local variables moved into u.ar */
 66601   u.ar.pCrsr = p->apCsr[pOp->p1]->pCursor;
 66602   if( ALWAYS(u.ar.pCrsr) ){
 66603     rc = sqlite3BtreeCount(u.ar.pCrsr, &u.ar.nEntry);
 66604   }else{
 66605     u.ar.nEntry = 0;
 66607   pOut->u.i = u.ar.nEntry;
 66608   break;
 66610 #endif
 66612 /* Opcode: Savepoint P1 * * P4 *
 66613 **
 66614 ** Open, release or rollback the savepoint named by parameter P4, depending
 66615 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
 66616 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
 66617 */
 66618 case OP_Savepoint: {
 66619 #if 0  /* local variables moved into u.as */
 66620   int p1;                         /* Value of P1 operand */
 66621   char *zName;                    /* Name of savepoint */
 66622   int nName;
 66623   Savepoint *pNew;
 66624   Savepoint *pSavepoint;
 66625   Savepoint *pTmp;
 66626   int iSavepoint;
 66627   int ii;
 66628 #endif /* local variables moved into u.as */
 66630   u.as.p1 = pOp->p1;
 66631   u.as.zName = pOp->p4.z;
 66633   /* Assert that the u.as.p1 parameter is valid. Also that if there is no open
 66634   ** transaction, then there cannot be any savepoints.
 66635   */
 66636   assert( db->pSavepoint==0 || db->autoCommit==0 );
 66637   assert( u.as.p1==SAVEPOINT_BEGIN||u.as.p1==SAVEPOINT_RELEASE||u.as.p1==SAVEPOINT_ROLLBACK );
 66638   assert( db->pSavepoint || db->isTransactionSavepoint==0 );
 66639   assert( checkSavepointCount(db) );
 66641   if( u.as.p1==SAVEPOINT_BEGIN ){
 66642     if( db->writeVdbeCnt>0 ){
 66643       /* A new savepoint cannot be created if there are active write
 66644       ** statements (i.e. open read/write incremental blob handles).
 66645       */
 66646       sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
 66647         "SQL statements in progress");
 66648       rc = SQLITE_BUSY;
 66649     }else{
 66650       u.as.nName = sqlite3Strlen30(u.as.zName);
 66652 #ifndef SQLITE_OMIT_VIRTUALTABLE
 66653       /* This call is Ok even if this savepoint is actually a transaction
 66654       ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
 66655       ** If this is a transaction savepoint being opened, it is guaranteed
 66656       ** that the db->aVTrans[] array is empty.  */
 66657       assert( db->autoCommit==0 || db->nVTrans==0 );
 66658       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
 66659                                 db->nStatement+db->nSavepoint);
 66660       if( rc!=SQLITE_OK ) goto abort_due_to_error;
 66661 #endif
 66663       /* Create a new savepoint structure. */
 66664       u.as.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.as.nName+1);
 66665       if( u.as.pNew ){
 66666         u.as.pNew->zName = (char *)&u.as.pNew[1];
 66667         memcpy(u.as.pNew->zName, u.as.zName, u.as.nName+1);
 66669         /* If there is no open transaction, then mark this as a special
 66670         ** "transaction savepoint". */
 66671         if( db->autoCommit ){
 66672           db->autoCommit = 0;
 66673           db->isTransactionSavepoint = 1;
 66674         }else{
 66675           db->nSavepoint++;
 66678         /* Link the new savepoint into the database handle's list. */
 66679         u.as.pNew->pNext = db->pSavepoint;
 66680         db->pSavepoint = u.as.pNew;
 66681         u.as.pNew->nDeferredCons = db->nDeferredCons;
 66684   }else{
 66685     u.as.iSavepoint = 0;
 66687     /* Find the named savepoint. If there is no such savepoint, then an
 66688     ** an error is returned to the user.  */
 66689     for(
 66690       u.as.pSavepoint = db->pSavepoint;
 66691       u.as.pSavepoint && sqlite3StrICmp(u.as.pSavepoint->zName, u.as.zName);
 66692       u.as.pSavepoint = u.as.pSavepoint->pNext
 66693     ){
 66694       u.as.iSavepoint++;
 66696     if( !u.as.pSavepoint ){
 66697       sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.as.zName);
 66698       rc = SQLITE_ERROR;
 66699     }else if( db->writeVdbeCnt>0 && u.as.p1==SAVEPOINT_RELEASE ){
 66700       /* It is not possible to release (commit) a savepoint if there are
 66701       ** active write statements.
 66702       */
 66703       sqlite3SetString(&p->zErrMsg, db,
 66704         "cannot release savepoint - SQL statements in progress"
 66705       );
 66706       rc = SQLITE_BUSY;
 66707     }else{
 66709       /* Determine whether or not this is a transaction savepoint. If so,
 66710       ** and this is a RELEASE command, then the current transaction
 66711       ** is committed.
 66712       */
 66713       int isTransaction = u.as.pSavepoint->pNext==0 && db->isTransactionSavepoint;
 66714       if( isTransaction && u.as.p1==SAVEPOINT_RELEASE ){
 66715         if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 66716           goto vdbe_return;
 66718         db->autoCommit = 1;
 66719         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 66720           p->pc = pc;
 66721           db->autoCommit = 0;
 66722           p->rc = rc = SQLITE_BUSY;
 66723           goto vdbe_return;
 66725         db->isTransactionSavepoint = 0;
 66726         rc = p->rc;
 66727       }else{
 66728         u.as.iSavepoint = db->nSavepoint - u.as.iSavepoint - 1;
 66729         if( u.as.p1==SAVEPOINT_ROLLBACK ){
 66730           for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
 66731             sqlite3BtreeTripAllCursors(db->aDb[u.as.ii].pBt, SQLITE_ABORT);
 66734         for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
 66735           rc = sqlite3BtreeSavepoint(db->aDb[u.as.ii].pBt, u.as.p1, u.as.iSavepoint);
 66736           if( rc!=SQLITE_OK ){
 66737             goto abort_due_to_error;
 66740         if( u.as.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
 66741           sqlite3ExpirePreparedStatements(db);
 66742           sqlite3ResetAllSchemasOfConnection(db);
 66743           db->flags = (db->flags | SQLITE_InternChanges);
 66747       /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
 66748       ** savepoints nested inside of the savepoint being operated on. */
 66749       while( db->pSavepoint!=u.as.pSavepoint ){
 66750         u.as.pTmp = db->pSavepoint;
 66751         db->pSavepoint = u.as.pTmp->pNext;
 66752         sqlite3DbFree(db, u.as.pTmp);
 66753         db->nSavepoint--;
 66756       /* If it is a RELEASE, then destroy the savepoint being operated on
 66757       ** too. If it is a ROLLBACK TO, then set the number of deferred
 66758       ** constraint violations present in the database to the value stored
 66759       ** when the savepoint was created.  */
 66760       if( u.as.p1==SAVEPOINT_RELEASE ){
 66761         assert( u.as.pSavepoint==db->pSavepoint );
 66762         db->pSavepoint = u.as.pSavepoint->pNext;
 66763         sqlite3DbFree(db, u.as.pSavepoint);
 66764         if( !isTransaction ){
 66765           db->nSavepoint--;
 66767       }else{
 66768         db->nDeferredCons = u.as.pSavepoint->nDeferredCons;
 66771       if( !isTransaction ){
 66772         rc = sqlite3VtabSavepoint(db, u.as.p1, u.as.iSavepoint);
 66773         if( rc!=SQLITE_OK ) goto abort_due_to_error;
 66778   break;
 66781 /* Opcode: AutoCommit P1 P2 * * *
 66782 **
 66783 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
 66784 ** back any currently active btree transactions. If there are any active
 66785 ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
 66786 ** there are active writing VMs or active VMs that use shared cache.
 66787 **
 66788 ** This instruction causes the VM to halt.
 66789 */
 66790 case OP_AutoCommit: {
 66791 #if 0  /* local variables moved into u.at */
 66792   int desiredAutoCommit;
 66793   int iRollback;
 66794   int turnOnAC;
 66795 #endif /* local variables moved into u.at */
 66797   u.at.desiredAutoCommit = pOp->p1;
 66798   u.at.iRollback = pOp->p2;
 66799   u.at.turnOnAC = u.at.desiredAutoCommit && !db->autoCommit;
 66800   assert( u.at.desiredAutoCommit==1 || u.at.desiredAutoCommit==0 );
 66801   assert( u.at.desiredAutoCommit==1 || u.at.iRollback==0 );
 66802   assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
 66804 #if 0
 66805   if( u.at.turnOnAC && u.at.iRollback && db->activeVdbeCnt>1 ){
 66806     /* If this instruction implements a ROLLBACK and other VMs are
 66807     ** still running, and a transaction is active, return an error indicating
 66808     ** that the other VMs must complete first.
 66809     */
 66810     sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
 66811         "SQL statements in progress");
 66812     rc = SQLITE_BUSY;
 66813   }else
 66814 #endif
 66815   if( u.at.turnOnAC && !u.at.iRollback && db->writeVdbeCnt>0 ){
 66816     /* If this instruction implements a COMMIT and other VMs are writing
 66817     ** return an error indicating that the other VMs must complete first.
 66818     */
 66819     sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
 66820         "SQL statements in progress");
 66821     rc = SQLITE_BUSY;
 66822   }else if( u.at.desiredAutoCommit!=db->autoCommit ){
 66823     if( u.at.iRollback ){
 66824       assert( u.at.desiredAutoCommit==1 );
 66825       sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 66826       db->autoCommit = 1;
 66827     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 66828       goto vdbe_return;
 66829     }else{
 66830       db->autoCommit = (u8)u.at.desiredAutoCommit;
 66831       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 66832         p->pc = pc;
 66833         db->autoCommit = (u8)(1-u.at.desiredAutoCommit);
 66834         p->rc = rc = SQLITE_BUSY;
 66835         goto vdbe_return;
 66838     assert( db->nStatement==0 );
 66839     sqlite3CloseSavepoints(db);
 66840     if( p->rc==SQLITE_OK ){
 66841       rc = SQLITE_DONE;
 66842     }else{
 66843       rc = SQLITE_ERROR;
 66845     goto vdbe_return;
 66846   }else{
 66847     sqlite3SetString(&p->zErrMsg, db,
 66848         (!u.at.desiredAutoCommit)?"cannot start a transaction within a transaction":(
 66849         (u.at.iRollback)?"cannot rollback - no transaction is active":
 66850                    "cannot commit - no transaction is active"));
 66852     rc = SQLITE_ERROR;
 66854   break;
 66857 /* Opcode: Transaction P1 P2 * * *
 66858 **
 66859 ** Begin a transaction.  The transaction ends when a Commit or Rollback
 66860 ** opcode is encountered.  Depending on the ON CONFLICT setting, the
 66861 ** transaction might also be rolled back if an error is encountered.
 66862 **
 66863 ** P1 is the index of the database file on which the transaction is
 66864 ** started.  Index 0 is the main database file and index 1 is the
 66865 ** file used for temporary tables.  Indices of 2 or more are used for
 66866 ** attached databases.
 66867 **
 66868 ** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
 66869 ** obtained on the database file when a write-transaction is started.  No
 66870 ** other process can start another write transaction while this transaction is
 66871 ** underway.  Starting a write transaction also creates a rollback journal. A
 66872 ** write transaction must be started before any changes can be made to the
 66873 ** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
 66874 ** on the file.
 66875 **
 66876 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
 66877 ** true (this flag is set if the Vdbe may modify more than one row and may
 66878 ** throw an ABORT exception), a statement transaction may also be opened.
 66879 ** More specifically, a statement transaction is opened iff the database
 66880 ** connection is currently not in autocommit mode, or if there are other
 66881 ** active statements. A statement transaction allows the changes made by this
 66882 ** VDBE to be rolled back after an error without having to roll back the
 66883 ** entire transaction. If no error is encountered, the statement transaction
 66884 ** will automatically commit when the VDBE halts.
 66885 **
 66886 ** If P2 is zero, then a read-lock is obtained on the database file.
 66887 */
 66888 case OP_Transaction: {
 66889 #if 0  /* local variables moved into u.au */
 66890   Btree *pBt;
 66891 #endif /* local variables moved into u.au */
 66893   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 66894   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 66895   u.au.pBt = db->aDb[pOp->p1].pBt;
 66897   if( u.au.pBt ){
 66898     rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2);
 66899     if( rc==SQLITE_BUSY ){
 66900       p->pc = pc;
 66901       p->rc = rc = SQLITE_BUSY;
 66902       goto vdbe_return;
 66904     if( rc!=SQLITE_OK ){
 66905       goto abort_due_to_error;
 66908     if( pOp->p2 && p->usesStmtJournal
 66909      && (db->autoCommit==0 || db->activeVdbeCnt>1)
 66910     ){
 66911       assert( sqlite3BtreeIsInTrans(u.au.pBt) );
 66912       if( p->iStatement==0 ){
 66913         assert( db->nStatement>=0 && db->nSavepoint>=0 );
 66914         db->nStatement++;
 66915         p->iStatement = db->nSavepoint + db->nStatement;
 66918       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
 66919       if( rc==SQLITE_OK ){
 66920         rc = sqlite3BtreeBeginStmt(u.au.pBt, p->iStatement);
 66923       /* Store the current value of the database handles deferred constraint
 66924       ** counter. If the statement transaction needs to be rolled back,
 66925       ** the value of this counter needs to be restored too.  */
 66926       p->nStmtDefCons = db->nDeferredCons;
 66929   break;
 66932 /* Opcode: ReadCookie P1 P2 P3 * *
 66933 **
 66934 ** Read cookie number P3 from database P1 and write it into register P2.
 66935 ** P3==1 is the schema version.  P3==2 is the database format.
 66936 ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
 66937 ** the main database file and P1==1 is the database file used to store
 66938 ** temporary tables.
 66939 **
 66940 ** There must be a read-lock on the database (either a transaction
 66941 ** must be started or there must be an open cursor) before
 66942 ** executing this instruction.
 66943 */
 66944 case OP_ReadCookie: {               /* out2-prerelease */
 66945 #if 0  /* local variables moved into u.av */
 66946   int iMeta;
 66947   int iDb;
 66948   int iCookie;
 66949 #endif /* local variables moved into u.av */
 66951   u.av.iDb = pOp->p1;
 66952   u.av.iCookie = pOp->p3;
 66953   assert( pOp->p3<SQLITE_N_BTREE_META );
 66954   assert( u.av.iDb>=0 && u.av.iDb<db->nDb );
 66955   assert( db->aDb[u.av.iDb].pBt!=0 );
 66956   assert( (p->btreeMask & (((yDbMask)1)<<u.av.iDb))!=0 );
 66958   sqlite3BtreeGetMeta(db->aDb[u.av.iDb].pBt, u.av.iCookie, (u32 *)&u.av.iMeta);
 66959   pOut->u.i = u.av.iMeta;
 66960   break;
 66963 /* Opcode: SetCookie P1 P2 P3 * *
 66964 **
 66965 ** Write the content of register P3 (interpreted as an integer)
 66966 ** into cookie number P2 of database P1.  P2==1 is the schema version.  
 66967 ** P2==2 is the database format. P2==3 is the recommended pager cache 
 66968 ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
 66969 ** database file used to store temporary tables.
 66970 **
 66971 ** A transaction must be started before executing this opcode.
 66972 */
 66973 case OP_SetCookie: {       /* in3 */
 66974 #if 0  /* local variables moved into u.aw */
 66975   Db *pDb;
 66976 #endif /* local variables moved into u.aw */
 66977   assert( pOp->p2<SQLITE_N_BTREE_META );
 66978   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 66979   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 66980   u.aw.pDb = &db->aDb[pOp->p1];
 66981   assert( u.aw.pDb->pBt!=0 );
 66982   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 66983   pIn3 = &aMem[pOp->p3];
 66984   sqlite3VdbeMemIntegerify(pIn3);
 66985   /* See note about index shifting on OP_ReadCookie */
 66986   rc = sqlite3BtreeUpdateMeta(u.aw.pDb->pBt, pOp->p2, (int)pIn3->u.i);
 66987   if( pOp->p2==BTREE_SCHEMA_VERSION ){
 66988     /* When the schema cookie changes, record the new cookie internally */
 66989     u.aw.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
 66990     db->flags |= SQLITE_InternChanges;
 66991   }else if( pOp->p2==BTREE_FILE_FORMAT ){
 66992     /* Record changes in the file format */
 66993     u.aw.pDb->pSchema->file_format = (u8)pIn3->u.i;
 66995   if( pOp->p1==1 ){
 66996     /* Invalidate all prepared statements whenever the TEMP database
 66997     ** schema is changed.  Ticket #1644 */
 66998     sqlite3ExpirePreparedStatements(db);
 66999     p->expired = 0;
 67001   break;
 67004 /* Opcode: VerifyCookie P1 P2 P3 * *
 67005 **
 67006 ** Check the value of global database parameter number 0 (the
 67007 ** schema version) and make sure it is equal to P2 and that the
 67008 ** generation counter on the local schema parse equals P3.
 67009 **
 67010 ** P1 is the database number which is 0 for the main database file
 67011 ** and 1 for the file holding temporary tables and some higher number
 67012 ** for auxiliary databases.
 67013 **
 67014 ** The cookie changes its value whenever the database schema changes.
 67015 ** This operation is used to detect when that the cookie has changed
 67016 ** and that the current process needs to reread the schema.
 67017 **
 67018 ** Either a transaction needs to have been started or an OP_Open needs
 67019 ** to be executed (to establish a read lock) before this opcode is
 67020 ** invoked.
 67021 */
 67022 case OP_VerifyCookie: {
 67023 #if 0  /* local variables moved into u.ax */
 67024   int iMeta;
 67025   int iGen;
 67026   Btree *pBt;
 67027 #endif /* local variables moved into u.ax */
 67029   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 67030   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 67031   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 67032   u.ax.pBt = db->aDb[pOp->p1].pBt;
 67033   if( u.ax.pBt ){
 67034     sqlite3BtreeGetMeta(u.ax.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.ax.iMeta);
 67035     u.ax.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
 67036   }else{
 67037     u.ax.iGen = u.ax.iMeta = 0;
 67039   if( u.ax.iMeta!=pOp->p2 || u.ax.iGen!=pOp->p3 ){
 67040     sqlite3DbFree(db, p->zErrMsg);
 67041     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
 67042     /* If the schema-cookie from the database file matches the cookie
 67043     ** stored with the in-memory representation of the schema, do
 67044     ** not reload the schema from the database file.
 67045     **
 67046     ** If virtual-tables are in use, this is not just an optimization.
 67047     ** Often, v-tables store their data in other SQLite tables, which
 67048     ** are queried from within xNext() and other v-table methods using
 67049     ** prepared queries. If such a query is out-of-date, we do not want to
 67050     ** discard the database schema, as the user code implementing the
 67051     ** v-table would have to be ready for the sqlite3_vtab structure itself
 67052     ** to be invalidated whenever sqlite3_step() is called from within
 67053     ** a v-table method.
 67054     */
 67055     if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.ax.iMeta ){
 67056       sqlite3ResetOneSchema(db, pOp->p1);
 67059     p->expired = 1;
 67060     rc = SQLITE_SCHEMA;
 67062   break;
 67065 /* Opcode: OpenRead P1 P2 P3 P4 P5
 67066 **
 67067 ** Open a read-only cursor for the database table whose root page is
 67068 ** P2 in a database file.  The database file is determined by P3. 
 67069 ** P3==0 means the main database, P3==1 means the database used for 
 67070 ** temporary tables, and P3>1 means used the corresponding attached
 67071 ** database.  Give the new cursor an identifier of P1.  The P1
 67072 ** values need not be contiguous but all P1 values should be small integers.
 67073 ** It is an error for P1 to be negative.
 67074 **
 67075 ** If P5!=0 then use the content of register P2 as the root page, not
 67076 ** the value of P2 itself.
 67077 **
 67078 ** There will be a read lock on the database whenever there is an
 67079 ** open cursor.  If the database was unlocked prior to this instruction
 67080 ** then a read lock is acquired as part of this instruction.  A read
 67081 ** lock allows other processes to read the database but prohibits
 67082 ** any other process from modifying the database.  The read lock is
 67083 ** released when all cursors are closed.  If this instruction attempts
 67084 ** to get a read lock but fails, the script terminates with an
 67085 ** SQLITE_BUSY error code.
 67086 **
 67087 ** The P4 value may be either an integer (P4_INT32) or a pointer to
 67088 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
 67089 ** structure, then said structure defines the content and collating 
 67090 ** sequence of the index being opened. Otherwise, if P4 is an integer 
 67091 ** value, it is set to the number of columns in the table.
 67092 **
 67093 ** See also OpenWrite.
 67094 */
 67095 /* Opcode: OpenWrite P1 P2 P3 P4 P5
 67096 **
 67097 ** Open a read/write cursor named P1 on the table or index whose root
 67098 ** page is P2.  Or if P5!=0 use the content of register P2 to find the
 67099 ** root page.
 67100 **
 67101 ** The P4 value may be either an integer (P4_INT32) or a pointer to
 67102 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
 67103 ** structure, then said structure defines the content and collating 
 67104 ** sequence of the index being opened. Otherwise, if P4 is an integer 
 67105 ** value, it is set to the number of columns in the table, or to the
 67106 ** largest index of any column of the table that is actually used.
 67107 **
 67108 ** This instruction works just like OpenRead except that it opens the cursor
 67109 ** in read/write mode.  For a given table, there can be one or more read-only
 67110 ** cursors or a single read/write cursor but not both.
 67111 **
 67112 ** See also OpenRead.
 67113 */
 67114 case OP_OpenRead:
 67115 case OP_OpenWrite: {
 67116 #if 0  /* local variables moved into u.ay */
 67117   int nField;
 67118   KeyInfo *pKeyInfo;
 67119   int p2;
 67120   int iDb;
 67121   int wrFlag;
 67122   Btree *pX;
 67123   VdbeCursor *pCur;
 67124   Db *pDb;
 67125 #endif /* local variables moved into u.ay */
 67127   assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
 67128   assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
 67130   if( p->expired ){
 67131     rc = SQLITE_ABORT;
 67132     break;
 67135   u.ay.nField = 0;
 67136   u.ay.pKeyInfo = 0;
 67137   u.ay.p2 = pOp->p2;
 67138   u.ay.iDb = pOp->p3;
 67139   assert( u.ay.iDb>=0 && u.ay.iDb<db->nDb );
 67140   assert( (p->btreeMask & (((yDbMask)1)<<u.ay.iDb))!=0 );
 67141   u.ay.pDb = &db->aDb[u.ay.iDb];
 67142   u.ay.pX = u.ay.pDb->pBt;
 67143   assert( u.ay.pX!=0 );
 67144   if( pOp->opcode==OP_OpenWrite ){
 67145     u.ay.wrFlag = 1;
 67146     assert( sqlite3SchemaMutexHeld(db, u.ay.iDb, 0) );
 67147     if( u.ay.pDb->pSchema->file_format < p->minWriteFileFormat ){
 67148       p->minWriteFileFormat = u.ay.pDb->pSchema->file_format;
 67150   }else{
 67151     u.ay.wrFlag = 0;
 67153   if( pOp->p5 & OPFLAG_P2ISREG ){
 67154     assert( u.ay.p2>0 );
 67155     assert( u.ay.p2<=p->nMem );
 67156     pIn2 = &aMem[u.ay.p2];
 67157     assert( memIsValid(pIn2) );
 67158     assert( (pIn2->flags & MEM_Int)!=0 );
 67159     sqlite3VdbeMemIntegerify(pIn2);
 67160     u.ay.p2 = (int)pIn2->u.i;
 67161     /* The u.ay.p2 value always comes from a prior OP_CreateTable opcode and
 67162     ** that opcode will always set the u.ay.p2 value to 2 or more or else fail.
 67163     ** If there were a failure, the prepared statement would have halted
 67164     ** before reaching this instruction. */
 67165     if( NEVER(u.ay.p2<2) ) {
 67166       rc = SQLITE_CORRUPT_BKPT;
 67167       goto abort_due_to_error;
 67170   if( pOp->p4type==P4_KEYINFO ){
 67171     u.ay.pKeyInfo = pOp->p4.pKeyInfo;
 67172     u.ay.pKeyInfo->enc = ENC(p->db);
 67173     u.ay.nField = u.ay.pKeyInfo->nField+1;
 67174   }else if( pOp->p4type==P4_INT32 ){
 67175     u.ay.nField = pOp->p4.i;
 67177   assert( pOp->p1>=0 );
 67178   u.ay.pCur = allocateCursor(p, pOp->p1, u.ay.nField, u.ay.iDb, 1);
 67179   if( u.ay.pCur==0 ) goto no_mem;
 67180   u.ay.pCur->nullRow = 1;
 67181   u.ay.pCur->isOrdered = 1;
 67182   rc = sqlite3BtreeCursor(u.ay.pX, u.ay.p2, u.ay.wrFlag, u.ay.pKeyInfo, u.ay.pCur->pCursor);
 67183   u.ay.pCur->pKeyInfo = u.ay.pKeyInfo;
 67184   assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
 67185   sqlite3BtreeCursorHints(u.ay.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
 67187   /* Since it performs no memory allocation or IO, the only value that
 67188   ** sqlite3BtreeCursor() may return is SQLITE_OK. */
 67189   assert( rc==SQLITE_OK );
 67191   /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
 67192   ** SQLite used to check if the root-page flags were sane at this point
 67193   ** and report database corruption if they were not, but this check has
 67194   ** since moved into the btree layer.  */
 67195   u.ay.pCur->isTable = pOp->p4type!=P4_KEYINFO;
 67196   u.ay.pCur->isIndex = !u.ay.pCur->isTable;
 67197   break;
 67200 /* Opcode: OpenEphemeral P1 P2 * P4 P5
 67201 **
 67202 ** Open a new cursor P1 to a transient table.
 67203 ** The cursor is always opened read/write even if 
 67204 ** the main database is read-only.  The ephemeral
 67205 ** table is deleted automatically when the cursor is closed.
 67206 **
 67207 ** P2 is the number of columns in the ephemeral table.
 67208 ** The cursor points to a BTree table if P4==0 and to a BTree index
 67209 ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
 67210 ** that defines the format of keys in the index.
 67211 **
 67212 ** This opcode was once called OpenTemp.  But that created
 67213 ** confusion because the term "temp table", might refer either
 67214 ** to a TEMP table at the SQL level, or to a table opened by
 67215 ** this opcode.  Then this opcode was call OpenVirtual.  But
 67216 ** that created confusion with the whole virtual-table idea.
 67217 **
 67218 ** The P5 parameter can be a mask of the BTREE_* flags defined
 67219 ** in btree.h.  These flags control aspects of the operation of
 67220 ** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
 67221 ** added automatically.
 67222 */
 67223 /* Opcode: OpenAutoindex P1 P2 * P4 *
 67224 **
 67225 ** This opcode works the same as OP_OpenEphemeral.  It has a
 67226 ** different name to distinguish its use.  Tables created using
 67227 ** by this opcode will be used for automatically created transient
 67228 ** indices in joins.
 67229 */
 67230 case OP_OpenAutoindex: 
 67231 case OP_OpenEphemeral: {
 67232 #if 0  /* local variables moved into u.az */
 67233   VdbeCursor *pCx;
 67234 #endif /* local variables moved into u.az */
 67235   static const int vfsFlags =
 67236       SQLITE_OPEN_READWRITE |
 67237       SQLITE_OPEN_CREATE |
 67238       SQLITE_OPEN_EXCLUSIVE |
 67239       SQLITE_OPEN_DELETEONCLOSE |
 67240       SQLITE_OPEN_TRANSIENT_DB;
 67242   assert( pOp->p1>=0 );
 67243   u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 67244   if( u.az.pCx==0 ) goto no_mem;
 67245   u.az.pCx->nullRow = 1;
 67246   rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.az.pCx->pBt,
 67247                         BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
 67248   if( rc==SQLITE_OK ){
 67249     rc = sqlite3BtreeBeginTrans(u.az.pCx->pBt, 1);
 67251   if( rc==SQLITE_OK ){
 67252     /* If a transient index is required, create it by calling
 67253     ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
 67254     ** opening it. If a transient table is required, just use the
 67255     ** automatically created table with root-page 1 (an BLOB_INTKEY table).
 67256     */
 67257     if( pOp->p4.pKeyInfo ){
 67258       int pgno;
 67259       assert( pOp->p4type==P4_KEYINFO );
 67260       rc = sqlite3BtreeCreateTable(u.az.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
 67261       if( rc==SQLITE_OK ){
 67262         assert( pgno==MASTER_ROOT+1 );
 67263         rc = sqlite3BtreeCursor(u.az.pCx->pBt, pgno, 1,
 67264                                 (KeyInfo*)pOp->p4.z, u.az.pCx->pCursor);
 67265         u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
 67266         u.az.pCx->pKeyInfo->enc = ENC(p->db);
 67268       u.az.pCx->isTable = 0;
 67269     }else{
 67270       rc = sqlite3BtreeCursor(u.az.pCx->pBt, MASTER_ROOT, 1, 0, u.az.pCx->pCursor);
 67271       u.az.pCx->isTable = 1;
 67274   u.az.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
 67275   u.az.pCx->isIndex = !u.az.pCx->isTable;
 67276   break;
 67279 /* Opcode: SorterOpen P1 P2 * P4 *
 67280 **
 67281 ** This opcode works like OP_OpenEphemeral except that it opens
 67282 ** a transient index that is specifically designed to sort large
 67283 ** tables using an external merge-sort algorithm.
 67284 */
 67285 case OP_SorterOpen: {
 67286 #if 0  /* local variables moved into u.ba */
 67287   VdbeCursor *pCx;
 67288 #endif /* local variables moved into u.ba */
 67290 #ifndef SQLITE_OMIT_MERGE_SORT
 67291   u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 67292   if( u.ba.pCx==0 ) goto no_mem;
 67293   u.ba.pCx->pKeyInfo = pOp->p4.pKeyInfo;
 67294   u.ba.pCx->pKeyInfo->enc = ENC(p->db);
 67295   u.ba.pCx->isSorter = 1;
 67296   rc = sqlite3VdbeSorterInit(db, u.ba.pCx);
 67297 #else
 67298   pOp->opcode = OP_OpenEphemeral;
 67299   pc--;
 67300 #endif
 67301   break;
 67304 /* Opcode: OpenPseudo P1 P2 P3 * P5
 67305 **
 67306 ** Open a new cursor that points to a fake table that contains a single
 67307 ** row of data.  The content of that one row in the content of memory
 67308 ** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the 
 67309 ** MEM_Blob content contained in register P2.  When P5==1, then the
 67310 ** row is represented by P3 consecutive registers beginning with P2.
 67311 **
 67312 ** A pseudo-table created by this opcode is used to hold a single
 67313 ** row output from the sorter so that the row can be decomposed into
 67314 ** individual columns using the OP_Column opcode.  The OP_Column opcode
 67315 ** is the only cursor opcode that works with a pseudo-table.
 67316 **
 67317 ** P3 is the number of fields in the records that will be stored by
 67318 ** the pseudo-table.
 67319 */
 67320 case OP_OpenPseudo: {
 67321 #if 0  /* local variables moved into u.bb */
 67322   VdbeCursor *pCx;
 67323 #endif /* local variables moved into u.bb */
 67325   assert( pOp->p1>=0 );
 67326   u.bb.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
 67327   if( u.bb.pCx==0 ) goto no_mem;
 67328   u.bb.pCx->nullRow = 1;
 67329   u.bb.pCx->pseudoTableReg = pOp->p2;
 67330   u.bb.pCx->isTable = 1;
 67331   u.bb.pCx->isIndex = 0;
 67332   u.bb.pCx->multiPseudo = pOp->p5;
 67333   break;
 67336 /* Opcode: Close P1 * * * *
 67337 **
 67338 ** Close a cursor previously opened as P1.  If P1 is not
 67339 ** currently open, this instruction is a no-op.
 67340 */
 67341 case OP_Close: {
 67342   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67343   sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
 67344   p->apCsr[pOp->p1] = 0;
 67345   break;
 67348 /* Opcode: SeekGe P1 P2 P3 P4 *
 67349 **
 67350 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 67351 ** use the value in register P3 as the key.  If cursor P1 refers 
 67352 ** to an SQL index, then P3 is the first in an array of P4 registers 
 67353 ** that are used as an unpacked index key. 
 67354 **
 67355 ** Reposition cursor P1 so that  it points to the smallest entry that 
 67356 ** is greater than or equal to the key value. If there are no records 
 67357 ** greater than or equal to the key and P2 is not zero, then jump to P2.
 67358 **
 67359 ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
 67360 */
 67361 /* Opcode: SeekGt P1 P2 P3 P4 *
 67362 **
 67363 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 67364 ** use the value in register P3 as a key. If cursor P1 refers 
 67365 ** to an SQL index, then P3 is the first in an array of P4 registers 
 67366 ** that are used as an unpacked index key. 
 67367 **
 67368 ** Reposition cursor P1 so that  it points to the smallest entry that 
 67369 ** is greater than the key value. If there are no records greater than 
 67370 ** the key and P2 is not zero, then jump to P2.
 67371 **
 67372 ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
 67373 */
 67374 /* Opcode: SeekLt P1 P2 P3 P4 * 
 67375 **
 67376 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 67377 ** use the value in register P3 as a key. If cursor P1 refers 
 67378 ** to an SQL index, then P3 is the first in an array of P4 registers 
 67379 ** that are used as an unpacked index key. 
 67380 **
 67381 ** Reposition cursor P1 so that  it points to the largest entry that 
 67382 ** is less than the key value. If there are no records less than 
 67383 ** the key and P2 is not zero, then jump to P2.
 67384 **
 67385 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
 67386 */
 67387 /* Opcode: SeekLe P1 P2 P3 P4 *
 67388 **
 67389 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 67390 ** use the value in register P3 as a key. If cursor P1 refers 
 67391 ** to an SQL index, then P3 is the first in an array of P4 registers 
 67392 ** that are used as an unpacked index key. 
 67393 **
 67394 ** Reposition cursor P1 so that it points to the largest entry that 
 67395 ** is less than or equal to the key value. If there are no records 
 67396 ** less than or equal to the key and P2 is not zero, then jump to P2.
 67397 **
 67398 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
 67399 */
 67400 case OP_SeekLt:         /* jump, in3 */
 67401 case OP_SeekLe:         /* jump, in3 */
 67402 case OP_SeekGe:         /* jump, in3 */
 67403 case OP_SeekGt: {       /* jump, in3 */
 67404 #if 0  /* local variables moved into u.bc */
 67405   int res;
 67406   int oc;
 67407   VdbeCursor *pC;
 67408   UnpackedRecord r;
 67409   int nField;
 67410   i64 iKey;      /* The rowid we are to seek to */
 67411 #endif /* local variables moved into u.bc */
 67413   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67414   assert( pOp->p2!=0 );
 67415   u.bc.pC = p->apCsr[pOp->p1];
 67416   assert( u.bc.pC!=0 );
 67417   assert( u.bc.pC->pseudoTableReg==0 );
 67418   assert( OP_SeekLe == OP_SeekLt+1 );
 67419   assert( OP_SeekGe == OP_SeekLt+2 );
 67420   assert( OP_SeekGt == OP_SeekLt+3 );
 67421   assert( u.bc.pC->isOrdered );
 67422   if( ALWAYS(u.bc.pC->pCursor!=0) ){
 67423     u.bc.oc = pOp->opcode;
 67424     u.bc.pC->nullRow = 0;
 67425     if( u.bc.pC->isTable ){
 67426       /* The input value in P3 might be of any type: integer, real, string,
 67427       ** blob, or NULL.  But it needs to be an integer before we can do
 67428       ** the seek, so covert it. */
 67429       pIn3 = &aMem[pOp->p3];
 67430       applyNumericAffinity(pIn3);
 67431       u.bc.iKey = sqlite3VdbeIntValue(pIn3);
 67432       u.bc.pC->rowidIsValid = 0;
 67434       /* If the P3 value could not be converted into an integer without
 67435       ** loss of information, then special processing is required... */
 67436       if( (pIn3->flags & MEM_Int)==0 ){
 67437         if( (pIn3->flags & MEM_Real)==0 ){
 67438           /* If the P3 value cannot be converted into any kind of a number,
 67439           ** then the seek is not possible, so jump to P2 */
 67440           pc = pOp->p2 - 1;
 67441           break;
 67443         /* If we reach this point, then the P3 value must be a floating
 67444         ** point number. */
 67445         assert( (pIn3->flags & MEM_Real)!=0 );
 67447         if( u.bc.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bc.iKey || pIn3->r>0) ){
 67448           /* The P3 value is too large in magnitude to be expressed as an
 67449           ** integer. */
 67450           u.bc.res = 1;
 67451           if( pIn3->r<0 ){
 67452             if( u.bc.oc>=OP_SeekGe ){  assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
 67453               rc = sqlite3BtreeFirst(u.bc.pC->pCursor, &u.bc.res);
 67454               if( rc!=SQLITE_OK ) goto abort_due_to_error;
 67456           }else{
 67457             if( u.bc.oc<=OP_SeekLe ){  assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
 67458               rc = sqlite3BtreeLast(u.bc.pC->pCursor, &u.bc.res);
 67459               if( rc!=SQLITE_OK ) goto abort_due_to_error;
 67462           if( u.bc.res ){
 67463             pc = pOp->p2 - 1;
 67465           break;
 67466         }else if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekGe ){
 67467           /* Use the ceiling() function to convert real->int */
 67468           if( pIn3->r > (double)u.bc.iKey ) u.bc.iKey++;
 67469         }else{
 67470           /* Use the floor() function to convert real->int */
 67471           assert( u.bc.oc==OP_SeekLe || u.bc.oc==OP_SeekGt );
 67472           if( pIn3->r < (double)u.bc.iKey ) u.bc.iKey--;
 67475       rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, 0, (u64)u.bc.iKey, 0, &u.bc.res);
 67476       if( rc!=SQLITE_OK ){
 67477         goto abort_due_to_error;
 67479       if( u.bc.res==0 ){
 67480         u.bc.pC->rowidIsValid = 1;
 67481         u.bc.pC->lastRowid = u.bc.iKey;
 67483     }else{
 67484       u.bc.nField = pOp->p4.i;
 67485       assert( pOp->p4type==P4_INT32 );
 67486       assert( u.bc.nField>0 );
 67487       u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
 67488       u.bc.r.nField = (u16)u.bc.nField;
 67490       /* The next line of code computes as follows, only faster:
 67491       **   if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekLe ){
 67492       **     u.bc.r.flags = UNPACKED_INCRKEY;
 67493       **   }else{
 67494       **     u.bc.r.flags = 0;
 67495       **   }
 67496       */
 67497       u.bc.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
 67498       assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
 67499       assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
 67500       assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
 67501       assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
 67503       u.bc.r.aMem = &aMem[pOp->p3];
 67504 #ifdef SQLITE_DEBUG
 67505       { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
 67506 #endif
 67507       ExpandBlob(u.bc.r.aMem);
 67508       rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, &u.bc.r, 0, 0, &u.bc.res);
 67509       if( rc!=SQLITE_OK ){
 67510         goto abort_due_to_error;
 67512       u.bc.pC->rowidIsValid = 0;
 67514     u.bc.pC->deferredMoveto = 0;
 67515     u.bc.pC->cacheStatus = CACHE_STALE;
 67516 #ifdef SQLITE_TEST
 67517     sqlite3_search_count++;
 67518 #endif
 67519     if( u.bc.oc>=OP_SeekGe ){  assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
 67520       if( u.bc.res<0 || (u.bc.res==0 && u.bc.oc==OP_SeekGt) ){
 67521         rc = sqlite3BtreeNext(u.bc.pC->pCursor, &u.bc.res);
 67522         if( rc!=SQLITE_OK ) goto abort_due_to_error;
 67523         u.bc.pC->rowidIsValid = 0;
 67524       }else{
 67525         u.bc.res = 0;
 67527     }else{
 67528       assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
 67529       if( u.bc.res>0 || (u.bc.res==0 && u.bc.oc==OP_SeekLt) ){
 67530         rc = sqlite3BtreePrevious(u.bc.pC->pCursor, &u.bc.res);
 67531         if( rc!=SQLITE_OK ) goto abort_due_to_error;
 67532         u.bc.pC->rowidIsValid = 0;
 67533       }else{
 67534         /* u.bc.res might be negative because the table is empty.  Check to
 67535         ** see if this is the case.
 67536         */
 67537         u.bc.res = sqlite3BtreeEof(u.bc.pC->pCursor);
 67540     assert( pOp->p2>0 );
 67541     if( u.bc.res ){
 67542       pc = pOp->p2 - 1;
 67544   }else{
 67545     /* This happens when attempting to open the sqlite3_master table
 67546     ** for read access returns SQLITE_EMPTY. In this case always
 67547     ** take the jump (since there are no records in the table).
 67548     */
 67549     pc = pOp->p2 - 1;
 67551   break;
 67554 /* Opcode: Seek P1 P2 * * *
 67555 **
 67556 ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
 67557 ** for P1 to move so that it points to the rowid given by P2.
 67558 **
 67559 ** This is actually a deferred seek.  Nothing actually happens until
 67560 ** the cursor is used to read a record.  That way, if no reads
 67561 ** occur, no unnecessary I/O happens.
 67562 */
 67563 case OP_Seek: {    /* in2 */
 67564 #if 0  /* local variables moved into u.bd */
 67565   VdbeCursor *pC;
 67566 #endif /* local variables moved into u.bd */
 67568   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67569   u.bd.pC = p->apCsr[pOp->p1];
 67570   assert( u.bd.pC!=0 );
 67571   if( ALWAYS(u.bd.pC->pCursor!=0) ){
 67572     assert( u.bd.pC->isTable );
 67573     u.bd.pC->nullRow = 0;
 67574     pIn2 = &aMem[pOp->p2];
 67575     u.bd.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
 67576     u.bd.pC->rowidIsValid = 0;
 67577     u.bd.pC->deferredMoveto = 1;
 67579   break;
 67583 /* Opcode: Found P1 P2 P3 P4 *
 67584 **
 67585 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 67586 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
 67587 ** record.
 67588 **
 67589 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
 67590 ** is a prefix of any entry in P1 then a jump is made to P2 and
 67591 ** P1 is left pointing at the matching entry.
 67592 */
 67593 /* Opcode: NotFound P1 P2 P3 P4 *
 67594 **
 67595 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 67596 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
 67597 ** record.
 67598 ** 
 67599 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
 67600 ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1 
 67601 ** does contain an entry whose prefix matches the P3/P4 record then control
 67602 ** falls through to the next instruction and P1 is left pointing at the
 67603 ** matching entry.
 67604 **
 67605 ** See also: Found, NotExists, IsUnique
 67606 */
 67607 case OP_NotFound:       /* jump, in3 */
 67608 case OP_Found: {        /* jump, in3 */
 67609 #if 0  /* local variables moved into u.be */
 67610   int alreadyExists;
 67611   VdbeCursor *pC;
 67612   int res;
 67613   char *pFree;
 67614   UnpackedRecord *pIdxKey;
 67615   UnpackedRecord r;
 67616   char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
 67617 #endif /* local variables moved into u.be */
 67619 #ifdef SQLITE_TEST
 67620   sqlite3_found_count++;
 67621 #endif
 67623   u.be.alreadyExists = 0;
 67624   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67625   assert( pOp->p4type==P4_INT32 );
 67626   u.be.pC = p->apCsr[pOp->p1];
 67627   assert( u.be.pC!=0 );
 67628   pIn3 = &aMem[pOp->p3];
 67629   if( ALWAYS(u.be.pC->pCursor!=0) ){
 67631     assert( u.be.pC->isTable==0 );
 67632     if( pOp->p4.i>0 ){
 67633       u.be.r.pKeyInfo = u.be.pC->pKeyInfo;
 67634       u.be.r.nField = (u16)pOp->p4.i;
 67635       u.be.r.aMem = pIn3;
 67636 #ifdef SQLITE_DEBUG
 67637       { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
 67638 #endif
 67639       u.be.r.flags = UNPACKED_PREFIX_MATCH;
 67640       u.be.pIdxKey = &u.be.r;
 67641     }else{
 67642       u.be.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 67643           u.be.pC->pKeyInfo, u.be.aTempRec, sizeof(u.be.aTempRec), &u.be.pFree
 67644       );
 67645       if( u.be.pIdxKey==0 ) goto no_mem;
 67646       assert( pIn3->flags & MEM_Blob );
 67647       assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
 67648       sqlite3VdbeRecordUnpack(u.be.pC->pKeyInfo, pIn3->n, pIn3->z, u.be.pIdxKey);
 67649       u.be.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
 67651     rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, u.be.pIdxKey, 0, 0, &u.be.res);
 67652     if( pOp->p4.i==0 ){
 67653       sqlite3DbFree(db, u.be.pFree);
 67655     if( rc!=SQLITE_OK ){
 67656       break;
 67658     u.be.alreadyExists = (u.be.res==0);
 67659     u.be.pC->deferredMoveto = 0;
 67660     u.be.pC->cacheStatus = CACHE_STALE;
 67662   if( pOp->opcode==OP_Found ){
 67663     if( u.be.alreadyExists ) pc = pOp->p2 - 1;
 67664   }else{
 67665     if( !u.be.alreadyExists ) pc = pOp->p2 - 1;
 67667   break;
 67670 /* Opcode: IsUnique P1 P2 P3 P4 *
 67671 **
 67672 ** Cursor P1 is open on an index b-tree - that is to say, a btree which
 67673 ** no data and where the key are records generated by OP_MakeRecord with
 67674 ** the list field being the integer ROWID of the entry that the index
 67675 ** entry refers to.
 67676 **
 67677 ** The P3 register contains an integer record number. Call this record 
 67678 ** number R. Register P4 is the first in a set of N contiguous registers
 67679 ** that make up an unpacked index key that can be used with cursor P1.
 67680 ** The value of N can be inferred from the cursor. N includes the rowid
 67681 ** value appended to the end of the index record. This rowid value may
 67682 ** or may not be the same as R.
 67683 **
 67684 ** If any of the N registers beginning with register P4 contains a NULL
 67685 ** value, jump immediately to P2.
 67686 **
 67687 ** Otherwise, this instruction checks if cursor P1 contains an entry
 67688 ** where the first (N-1) fields match but the rowid value at the end
 67689 ** of the index entry is not R. If there is no such entry, control jumps
 67690 ** to instruction P2. Otherwise, the rowid of the conflicting index
 67691 ** entry is copied to register P3 and control falls through to the next
 67692 ** instruction.
 67693 **
 67694 ** See also: NotFound, NotExists, Found
 67695 */
 67696 case OP_IsUnique: {        /* jump, in3 */
 67697 #if 0  /* local variables moved into u.bf */
 67698   u16 ii;
 67699   VdbeCursor *pCx;
 67700   BtCursor *pCrsr;
 67701   u16 nField;
 67702   Mem *aMx;
 67703   UnpackedRecord r;                  /* B-Tree index search key */
 67704   i64 R;                             /* Rowid stored in register P3 */
 67705 #endif /* local variables moved into u.bf */
 67707   pIn3 = &aMem[pOp->p3];
 67708   u.bf.aMx = &aMem[pOp->p4.i];
 67709   /* Assert that the values of parameters P1 and P4 are in range. */
 67710   assert( pOp->p4type==P4_INT32 );
 67711   assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
 67712   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67714   /* Find the index cursor. */
 67715   u.bf.pCx = p->apCsr[pOp->p1];
 67716   assert( u.bf.pCx->deferredMoveto==0 );
 67717   u.bf.pCx->seekResult = 0;
 67718   u.bf.pCx->cacheStatus = CACHE_STALE;
 67719   u.bf.pCrsr = u.bf.pCx->pCursor;
 67721   /* If any of the values are NULL, take the jump. */
 67722   u.bf.nField = u.bf.pCx->pKeyInfo->nField;
 67723   for(u.bf.ii=0; u.bf.ii<u.bf.nField; u.bf.ii++){
 67724     if( u.bf.aMx[u.bf.ii].flags & MEM_Null ){
 67725       pc = pOp->p2 - 1;
 67726       u.bf.pCrsr = 0;
 67727       break;
 67730   assert( (u.bf.aMx[u.bf.nField].flags & MEM_Null)==0 );
 67732   if( u.bf.pCrsr!=0 ){
 67733     /* Populate the index search key. */
 67734     u.bf.r.pKeyInfo = u.bf.pCx->pKeyInfo;
 67735     u.bf.r.nField = u.bf.nField + 1;
 67736     u.bf.r.flags = UNPACKED_PREFIX_SEARCH;
 67737     u.bf.r.aMem = u.bf.aMx;
 67738 #ifdef SQLITE_DEBUG
 67739     { int i; for(i=0; i<u.bf.r.nField; i++) assert( memIsValid(&u.bf.r.aMem[i]) ); }
 67740 #endif
 67742     /* Extract the value of u.bf.R from register P3. */
 67743     sqlite3VdbeMemIntegerify(pIn3);
 67744     u.bf.R = pIn3->u.i;
 67746     /* Search the B-Tree index. If no conflicting record is found, jump
 67747     ** to P2. Otherwise, copy the rowid of the conflicting record to
 67748     ** register P3 and fall through to the next instruction.  */
 67749     rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, &u.bf.r, 0, 0, &u.bf.pCx->seekResult);
 67750     if( (u.bf.r.flags & UNPACKED_PREFIX_SEARCH) || u.bf.r.rowid==u.bf.R ){
 67751       pc = pOp->p2 - 1;
 67752     }else{
 67753       pIn3->u.i = u.bf.r.rowid;
 67756   break;
 67759 /* Opcode: NotExists P1 P2 P3 * *
 67760 **
 67761 ** Use the content of register P3 as an integer key.  If a record 
 67762 ** with that key does not exist in table of P1, then jump to P2. 
 67763 ** If the record does exist, then fall through.  The cursor is left 
 67764 ** pointing to the record if it exists.
 67765 **
 67766 ** The difference between this operation and NotFound is that this
 67767 ** operation assumes the key is an integer and that P1 is a table whereas
 67768 ** NotFound assumes key is a blob constructed from MakeRecord and
 67769 ** P1 is an index.
 67770 **
 67771 ** See also: Found, NotFound, IsUnique
 67772 */
 67773 case OP_NotExists: {        /* jump, in3 */
 67774 #if 0  /* local variables moved into u.bg */
 67775   VdbeCursor *pC;
 67776   BtCursor *pCrsr;
 67777   int res;
 67778   u64 iKey;
 67779 #endif /* local variables moved into u.bg */
 67781   pIn3 = &aMem[pOp->p3];
 67782   assert( pIn3->flags & MEM_Int );
 67783   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67784   u.bg.pC = p->apCsr[pOp->p1];
 67785   assert( u.bg.pC!=0 );
 67786   assert( u.bg.pC->isTable );
 67787   assert( u.bg.pC->pseudoTableReg==0 );
 67788   u.bg.pCrsr = u.bg.pC->pCursor;
 67789   if( ALWAYS(u.bg.pCrsr!=0) ){
 67790     u.bg.res = 0;
 67791     u.bg.iKey = pIn3->u.i;
 67792     rc = sqlite3BtreeMovetoUnpacked(u.bg.pCrsr, 0, u.bg.iKey, 0, &u.bg.res);
 67793     u.bg.pC->lastRowid = pIn3->u.i;
 67794     u.bg.pC->rowidIsValid = u.bg.res==0 ?1:0;
 67795     u.bg.pC->nullRow = 0;
 67796     u.bg.pC->cacheStatus = CACHE_STALE;
 67797     u.bg.pC->deferredMoveto = 0;
 67798     if( u.bg.res!=0 ){
 67799       pc = pOp->p2 - 1;
 67800       assert( u.bg.pC->rowidIsValid==0 );
 67802     u.bg.pC->seekResult = u.bg.res;
 67803   }else{
 67804     /* This happens when an attempt to open a read cursor on the
 67805     ** sqlite_master table returns SQLITE_EMPTY.
 67806     */
 67807     pc = pOp->p2 - 1;
 67808     assert( u.bg.pC->rowidIsValid==0 );
 67809     u.bg.pC->seekResult = 0;
 67811   break;
 67814 /* Opcode: Sequence P1 P2 * * *
 67815 **
 67816 ** Find the next available sequence number for cursor P1.
 67817 ** Write the sequence number into register P2.
 67818 ** The sequence number on the cursor is incremented after this
 67819 ** instruction.  
 67820 */
 67821 case OP_Sequence: {           /* out2-prerelease */
 67822   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67823   assert( p->apCsr[pOp->p1]!=0 );
 67824   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
 67825   break;
 67829 /* Opcode: NewRowid P1 P2 P3 * *
 67830 **
 67831 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
 67832 ** The record number is not previously used as a key in the database
 67833 ** table that cursor P1 points to.  The new record number is written
 67834 ** written to register P2.
 67835 **
 67836 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
 67837 ** the largest previously generated record number. No new record numbers are
 67838 ** allowed to be less than this value. When this value reaches its maximum, 
 67839 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
 67840 ** generated record number. This P3 mechanism is used to help implement the
 67841 ** AUTOINCREMENT feature.
 67842 */
 67843 case OP_NewRowid: {           /* out2-prerelease */
 67844 #if 0  /* local variables moved into u.bh */
 67845   i64 v;                 /* The new rowid */
 67846   VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 67847   int res;               /* Result of an sqlite3BtreeLast() */
 67848   int cnt;               /* Counter to limit the number of searches */
 67849   Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 67850   VdbeFrame *pFrame;     /* Root frame of VDBE */
 67851 #endif /* local variables moved into u.bh */
 67853   u.bh.v = 0;
 67854   u.bh.res = 0;
 67855   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67856   u.bh.pC = p->apCsr[pOp->p1];
 67857   assert( u.bh.pC!=0 );
 67858   if( NEVER(u.bh.pC->pCursor==0) ){
 67859     /* The zero initialization above is all that is needed */
 67860   }else{
 67861     /* The next rowid or record number (different terms for the same
 67862     ** thing) is obtained in a two-step algorithm.
 67863     **
 67864     ** First we attempt to find the largest existing rowid and add one
 67865     ** to that.  But if the largest existing rowid is already the maximum
 67866     ** positive integer, we have to fall through to the second
 67867     ** probabilistic algorithm
 67868     **
 67869     ** The second algorithm is to select a rowid at random and see if
 67870     ** it already exists in the table.  If it does not exist, we have
 67871     ** succeeded.  If the random rowid does exist, we select a new one
 67872     ** and try again, up to 100 times.
 67873     */
 67874     assert( u.bh.pC->isTable );
 67876 #ifdef SQLITE_32BIT_ROWID
 67877 #   define MAX_ROWID 0x7fffffff
 67878 #else
 67879     /* Some compilers complain about constants of the form 0x7fffffffffffffff.
 67880     ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
 67881     ** to provide the constant while making all compilers happy.
 67882     */
 67883 #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
 67884 #endif
 67886     if( !u.bh.pC->useRandomRowid ){
 67887       u.bh.v = sqlite3BtreeGetCachedRowid(u.bh.pC->pCursor);
 67888       if( u.bh.v==0 ){
 67889         rc = sqlite3BtreeLast(u.bh.pC->pCursor, &u.bh.res);
 67890         if( rc!=SQLITE_OK ){
 67891           goto abort_due_to_error;
 67893         if( u.bh.res ){
 67894           u.bh.v = 1;   /* IMP: R-61914-48074 */
 67895         }else{
 67896           assert( sqlite3BtreeCursorIsValid(u.bh.pC->pCursor) );
 67897           rc = sqlite3BtreeKeySize(u.bh.pC->pCursor, &u.bh.v);
 67898           assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
 67899           if( u.bh.v>=MAX_ROWID ){
 67900             u.bh.pC->useRandomRowid = 1;
 67901           }else{
 67902             u.bh.v++;   /* IMP: R-29538-34987 */
 67907 #ifndef SQLITE_OMIT_AUTOINCREMENT
 67908       if( pOp->p3 ){
 67909         /* Assert that P3 is a valid memory cell. */
 67910         assert( pOp->p3>0 );
 67911         if( p->pFrame ){
 67912           for(u.bh.pFrame=p->pFrame; u.bh.pFrame->pParent; u.bh.pFrame=u.bh.pFrame->pParent);
 67913           /* Assert that P3 is a valid memory cell. */
 67914           assert( pOp->p3<=u.bh.pFrame->nMem );
 67915           u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
 67916         }else{
 67917           /* Assert that P3 is a valid memory cell. */
 67918           assert( pOp->p3<=p->nMem );
 67919           u.bh.pMem = &aMem[pOp->p3];
 67920           memAboutToChange(p, u.bh.pMem);
 67922         assert( memIsValid(u.bh.pMem) );
 67924         REGISTER_TRACE(pOp->p3, u.bh.pMem);
 67925         sqlite3VdbeMemIntegerify(u.bh.pMem);
 67926         assert( (u.bh.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
 67927         if( u.bh.pMem->u.i==MAX_ROWID || u.bh.pC->useRandomRowid ){
 67928           rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
 67929           goto abort_due_to_error;
 67931         if( u.bh.v<u.bh.pMem->u.i+1 ){
 67932           u.bh.v = u.bh.pMem->u.i + 1;
 67934         u.bh.pMem->u.i = u.bh.v;
 67936 #endif
 67938       sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, u.bh.v<MAX_ROWID ? u.bh.v+1 : 0);
 67940     if( u.bh.pC->useRandomRowid ){
 67941       /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
 67942       ** largest possible integer (9223372036854775807) then the database
 67943       ** engine starts picking positive candidate ROWIDs at random until
 67944       ** it finds one that is not previously used. */
 67945       assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
 67946                              ** an AUTOINCREMENT table. */
 67947       /* on the first attempt, simply do one more than previous */
 67948       u.bh.v = lastRowid;
 67949       u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 67950       u.bh.v++; /* ensure non-zero */
 67951       u.bh.cnt = 0;
 67952       while(   ((rc = sqlite3BtreeMovetoUnpacked(u.bh.pC->pCursor, 0, (u64)u.bh.v,
 67953                                                  0, &u.bh.res))==SQLITE_OK)
 67954             && (u.bh.res==0)
 67955             && (++u.bh.cnt<100)){
 67956         /* collision - try another random rowid */
 67957         sqlite3_randomness(sizeof(u.bh.v), &u.bh.v);
 67958         if( u.bh.cnt<5 ){
 67959           /* try "small" random rowids for the initial attempts */
 67960           u.bh.v &= 0xffffff;
 67961         }else{
 67962           u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 67964         u.bh.v++; /* ensure non-zero */
 67966       if( rc==SQLITE_OK && u.bh.res==0 ){
 67967         rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
 67968         goto abort_due_to_error;
 67970       assert( u.bh.v>0 );  /* EV: R-40812-03570 */
 67972     u.bh.pC->rowidIsValid = 0;
 67973     u.bh.pC->deferredMoveto = 0;
 67974     u.bh.pC->cacheStatus = CACHE_STALE;
 67976   pOut->u.i = u.bh.v;
 67977   break;
 67980 /* Opcode: Insert P1 P2 P3 P4 P5
 67981 **
 67982 ** Write an entry into the table of cursor P1.  A new entry is
 67983 ** created if it doesn't already exist or the data for an existing
 67984 ** entry is overwritten.  The data is the value MEM_Blob stored in register
 67985 ** number P2. The key is stored in register P3. The key must
 67986 ** be a MEM_Int.
 67987 **
 67988 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
 67989 ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
 67990 ** then rowid is stored for subsequent return by the
 67991 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
 67992 **
 67993 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
 67994 ** the last seek operation (OP_NotExists) was a success, then this
 67995 ** operation will not attempt to find the appropriate row before doing
 67996 ** the insert but will instead overwrite the row that the cursor is
 67997 ** currently pointing to.  Presumably, the prior OP_NotExists opcode
 67998 ** has already positioned the cursor correctly.  This is an optimization
 67999 ** that boosts performance by avoiding redundant seeks.
 68000 **
 68001 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
 68002 ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
 68003 ** is part of an INSERT operation.  The difference is only important to
 68004 ** the update hook.
 68005 **
 68006 ** Parameter P4 may point to a string containing the table-name, or
 68007 ** may be NULL. If it is not NULL, then the update-hook 
 68008 ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
 68009 **
 68010 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
 68011 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
 68012 ** and register P2 becomes ephemeral.  If the cursor is changed, the
 68013 ** value of register P2 will then change.  Make sure this does not
 68014 ** cause any problems.)
 68015 **
 68016 ** This instruction only works on tables.  The equivalent instruction
 68017 ** for indices is OP_IdxInsert.
 68018 */
 68019 /* Opcode: InsertInt P1 P2 P3 P4 P5
 68020 **
 68021 ** This works exactly like OP_Insert except that the key is the
 68022 ** integer value P3, not the value of the integer stored in register P3.
 68023 */
 68024 case OP_Insert: 
 68025 case OP_InsertInt: {
 68026 #if 0  /* local variables moved into u.bi */
 68027   Mem *pData;       /* MEM cell holding data for the record to be inserted */
 68028   Mem *pKey;        /* MEM cell holding key  for the record */
 68029   i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 68030   VdbeCursor *pC;   /* Cursor to table into which insert is written */
 68031   int nZero;        /* Number of zero-bytes to append */
 68032   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 68033   const char *zDb;  /* database name - used by the update hook */
 68034   const char *zTbl; /* Table name - used by the opdate hook */
 68035   int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 68036 #endif /* local variables moved into u.bi */
 68038   u.bi.pData = &aMem[pOp->p2];
 68039   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68040   assert( memIsValid(u.bi.pData) );
 68041   u.bi.pC = p->apCsr[pOp->p1];
 68042   assert( u.bi.pC!=0 );
 68043   assert( u.bi.pC->pCursor!=0 );
 68044   assert( u.bi.pC->pseudoTableReg==0 );
 68045   assert( u.bi.pC->isTable );
 68046   REGISTER_TRACE(pOp->p2, u.bi.pData);
 68048   if( pOp->opcode==OP_Insert ){
 68049     u.bi.pKey = &aMem[pOp->p3];
 68050     assert( u.bi.pKey->flags & MEM_Int );
 68051     assert( memIsValid(u.bi.pKey) );
 68052     REGISTER_TRACE(pOp->p3, u.bi.pKey);
 68053     u.bi.iKey = u.bi.pKey->u.i;
 68054   }else{
 68055     assert( pOp->opcode==OP_InsertInt );
 68056     u.bi.iKey = pOp->p3;
 68059   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 68060   if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bi.iKey;
 68061   if( u.bi.pData->flags & MEM_Null ){
 68062     u.bi.pData->z = 0;
 68063     u.bi.pData->n = 0;
 68064   }else{
 68065     assert( u.bi.pData->flags & (MEM_Blob|MEM_Str) );
 68067   u.bi.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bi.pC->seekResult : 0);
 68068   if( u.bi.pData->flags & MEM_Zero ){
 68069     u.bi.nZero = u.bi.pData->u.nZero;
 68070   }else{
 68071     u.bi.nZero = 0;
 68073   sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
 68074   rc = sqlite3BtreeInsert(u.bi.pC->pCursor, 0, u.bi.iKey,
 68075                           u.bi.pData->z, u.bi.pData->n, u.bi.nZero,
 68076                           pOp->p5 & OPFLAG_APPEND, u.bi.seekResult
 68077   );
 68078   u.bi.pC->rowidIsValid = 0;
 68079   u.bi.pC->deferredMoveto = 0;
 68080   u.bi.pC->cacheStatus = CACHE_STALE;
 68082   /* Invoke the update-hook if required. */
 68083   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
 68084     u.bi.zDb = db->aDb[u.bi.pC->iDb].zName;
 68085     u.bi.zTbl = pOp->p4.z;
 68086     u.bi.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 68087     assert( u.bi.pC->isTable );
 68088     db->xUpdateCallback(db->pUpdateArg, u.bi.op, u.bi.zDb, u.bi.zTbl, u.bi.iKey);
 68089     assert( u.bi.pC->iDb>=0 );
 68091   break;
 68094 /* Opcode: Delete P1 P2 * P4 *
 68095 **
 68096 ** Delete the record at which the P1 cursor is currently pointing.
 68097 **
 68098 ** The cursor will be left pointing at either the next or the previous
 68099 ** record in the table. If it is left pointing at the next record, then
 68100 ** the next Next instruction will be a no-op.  Hence it is OK to delete
 68101 ** a record from within an Next loop.
 68102 **
 68103 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
 68104 ** incremented (otherwise not).
 68105 **
 68106 ** P1 must not be pseudo-table.  It has to be a real table with
 68107 ** multiple rows.
 68108 **
 68109 ** If P4 is not NULL, then it is the name of the table that P1 is
 68110 ** pointing to.  The update hook will be invoked, if it exists.
 68111 ** If P4 is not NULL then the P1 cursor must have been positioned
 68112 ** using OP_NotFound prior to invoking this opcode.
 68113 */
 68114 case OP_Delete: {
 68115 #if 0  /* local variables moved into u.bj */
 68116   i64 iKey;
 68117   VdbeCursor *pC;
 68118 #endif /* local variables moved into u.bj */
 68120   u.bj.iKey = 0;
 68121   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68122   u.bj.pC = p->apCsr[pOp->p1];
 68123   assert( u.bj.pC!=0 );
 68124   assert( u.bj.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
 68126   /* If the update-hook will be invoked, set u.bj.iKey to the rowid of the
 68127   ** row being deleted.
 68128   */
 68129   if( db->xUpdateCallback && pOp->p4.z ){
 68130     assert( u.bj.pC->isTable );
 68131     assert( u.bj.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
 68132     u.bj.iKey = u.bj.pC->lastRowid;
 68135   /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
 68136   ** OP_Column on the same table without any intervening operations that
 68137   ** might move or invalidate the cursor.  Hence cursor u.bj.pC is always pointing
 68138   ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
 68139   ** below is always a no-op and cannot fail.  We will run it anyhow, though,
 68140   ** to guard against future changes to the code generator.
 68141   **/
 68142   assert( u.bj.pC->deferredMoveto==0 );
 68143   rc = sqlite3VdbeCursorMoveto(u.bj.pC);
 68144   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 68146   sqlite3BtreeSetCachedRowid(u.bj.pC->pCursor, 0);
 68147   rc = sqlite3BtreeDelete(u.bj.pC->pCursor);
 68148   u.bj.pC->cacheStatus = CACHE_STALE;
 68150   /* Invoke the update-hook if required. */
 68151   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
 68152     const char *zDb = db->aDb[u.bj.pC->iDb].zName;
 68153     const char *zTbl = pOp->p4.z;
 68154     db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bj.iKey);
 68155     assert( u.bj.pC->iDb>=0 );
 68157   if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
 68158   break;
 68160 /* Opcode: ResetCount * * * * *
 68161 **
 68162 ** The value of the change counter is copied to the database handle
 68163 ** change counter (returned by subsequent calls to sqlite3_changes()).
 68164 ** Then the VMs internal change counter resets to 0.
 68165 ** This is used by trigger programs.
 68166 */
 68167 case OP_ResetCount: {
 68168   sqlite3VdbeSetChanges(db, p->nChange);
 68169   p->nChange = 0;
 68170   break;
 68173 /* Opcode: SorterCompare P1 P2 P3
 68174 **
 68175 ** P1 is a sorter cursor. This instruction compares the record blob in 
 68176 ** register P3 with the entry that the sorter cursor currently points to.
 68177 ** If, excluding the rowid fields at the end, the two records are a match,
 68178 ** fall through to the next instruction. Otherwise, jump to instruction P2.
 68179 */
 68180 case OP_SorterCompare: {
 68181 #if 0  /* local variables moved into u.bk */
 68182   VdbeCursor *pC;
 68183   int res;
 68184 #endif /* local variables moved into u.bk */
 68186   u.bk.pC = p->apCsr[pOp->p1];
 68187   assert( isSorter(u.bk.pC) );
 68188   pIn3 = &aMem[pOp->p3];
 68189   rc = sqlite3VdbeSorterCompare(u.bk.pC, pIn3, &u.bk.res);
 68190   if( u.bk.res ){
 68191     pc = pOp->p2-1;
 68193   break;
 68194 };
 68196 /* Opcode: SorterData P1 P2 * * *
 68197 **
 68198 ** Write into register P2 the current sorter data for sorter cursor P1.
 68199 */
 68200 case OP_SorterData: {
 68201 #if 0  /* local variables moved into u.bl */
 68202   VdbeCursor *pC;
 68203 #endif /* local variables moved into u.bl */
 68205 #ifndef SQLITE_OMIT_MERGE_SORT
 68206   pOut = &aMem[pOp->p2];
 68207   u.bl.pC = p->apCsr[pOp->p1];
 68208   assert( u.bl.pC->isSorter );
 68209   rc = sqlite3VdbeSorterRowkey(u.bl.pC, pOut);
 68210 #else
 68211   pOp->opcode = OP_RowKey;
 68212   pc--;
 68213 #endif
 68214   break;
 68217 /* Opcode: RowData P1 P2 * * *
 68218 **
 68219 ** Write into register P2 the complete row data for cursor P1.
 68220 ** There is no interpretation of the data.  
 68221 ** It is just copied onto the P2 register exactly as 
 68222 ** it is found in the database file.
 68223 **
 68224 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 68225 ** of a real table, not a pseudo-table.
 68226 */
 68227 /* Opcode: RowKey P1 P2 * * *
 68228 **
 68229 ** Write into register P2 the complete row key for cursor P1.
 68230 ** There is no interpretation of the data.  
 68231 ** The key is copied onto the P3 register exactly as 
 68232 ** it is found in the database file.
 68233 **
 68234 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 68235 ** of a real table, not a pseudo-table.
 68236 */
 68237 case OP_RowKey:
 68238 case OP_RowData: {
 68239 #if 0  /* local variables moved into u.bm */
 68240   VdbeCursor *pC;
 68241   BtCursor *pCrsr;
 68242   u32 n;
 68243   i64 n64;
 68244 #endif /* local variables moved into u.bm */
 68246   pOut = &aMem[pOp->p2];
 68247   memAboutToChange(p, pOut);
 68249   /* Note that RowKey and RowData are really exactly the same instruction */
 68250   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68251   u.bm.pC = p->apCsr[pOp->p1];
 68252   assert( u.bm.pC->isSorter==0 );
 68253   assert( u.bm.pC->isTable || pOp->opcode!=OP_RowData );
 68254   assert( u.bm.pC->isIndex || pOp->opcode==OP_RowData );
 68255   assert( u.bm.pC!=0 );
 68256   assert( u.bm.pC->nullRow==0 );
 68257   assert( u.bm.pC->pseudoTableReg==0 );
 68258   assert( u.bm.pC->pCursor!=0 );
 68259   u.bm.pCrsr = u.bm.pC->pCursor;
 68260   assert( sqlite3BtreeCursorIsValid(u.bm.pCrsr) );
 68262   /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
 68263   ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
 68264   ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
 68265   ** a no-op and can never fail.  But we leave it in place as a safety.
 68266   */
 68267   assert( u.bm.pC->deferredMoveto==0 );
 68268   rc = sqlite3VdbeCursorMoveto(u.bm.pC);
 68269   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 68271   if( u.bm.pC->isIndex ){
 68272     assert( !u.bm.pC->isTable );
 68273     VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bm.pCrsr, &u.bm.n64);
 68274     assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
 68275     if( u.bm.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 68276       goto too_big;
 68278     u.bm.n = (u32)u.bm.n64;
 68279   }else{
 68280     VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bm.pCrsr, &u.bm.n);
 68281     assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
 68282     if( u.bm.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 68283       goto too_big;
 68286   if( sqlite3VdbeMemGrow(pOut, u.bm.n, 0) ){
 68287     goto no_mem;
 68289   pOut->n = u.bm.n;
 68290   MemSetTypeFlag(pOut, MEM_Blob);
 68291   if( u.bm.pC->isIndex ){
 68292     rc = sqlite3BtreeKey(u.bm.pCrsr, 0, u.bm.n, pOut->z);
 68293   }else{
 68294     rc = sqlite3BtreeData(u.bm.pCrsr, 0, u.bm.n, pOut->z);
 68296   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
 68297   UPDATE_MAX_BLOBSIZE(pOut);
 68298   break;
 68301 /* Opcode: Rowid P1 P2 * * *
 68302 **
 68303 ** Store in register P2 an integer which is the key of the table entry that
 68304 ** P1 is currently point to.
 68305 **
 68306 ** P1 can be either an ordinary table or a virtual table.  There used to
 68307 ** be a separate OP_VRowid opcode for use with virtual tables, but this
 68308 ** one opcode now works for both table types.
 68309 */
 68310 case OP_Rowid: {                 /* out2-prerelease */
 68311 #if 0  /* local variables moved into u.bn */
 68312   VdbeCursor *pC;
 68313   i64 v;
 68314   sqlite3_vtab *pVtab;
 68315   const sqlite3_module *pModule;
 68316 #endif /* local variables moved into u.bn */
 68318   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68319   u.bn.pC = p->apCsr[pOp->p1];
 68320   assert( u.bn.pC!=0 );
 68321   assert( u.bn.pC->pseudoTableReg==0 || u.bn.pC->nullRow );
 68322   if( u.bn.pC->nullRow ){
 68323     pOut->flags = MEM_Null;
 68324     break;
 68325   }else if( u.bn.pC->deferredMoveto ){
 68326     u.bn.v = u.bn.pC->movetoTarget;
 68327 #ifndef SQLITE_OMIT_VIRTUALTABLE
 68328   }else if( u.bn.pC->pVtabCursor ){
 68329     u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
 68330     u.bn.pModule = u.bn.pVtab->pModule;
 68331     assert( u.bn.pModule->xRowid );
 68332     rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
 68333     importVtabErrMsg(p, u.bn.pVtab);
 68334 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 68335   }else{
 68336     assert( u.bn.pC->pCursor!=0 );
 68337     rc = sqlite3VdbeCursorMoveto(u.bn.pC);
 68338     if( rc ) goto abort_due_to_error;
 68339     if( u.bn.pC->rowidIsValid ){
 68340       u.bn.v = u.bn.pC->lastRowid;
 68341     }else{
 68342       rc = sqlite3BtreeKeySize(u.bn.pC->pCursor, &u.bn.v);
 68343       assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
 68346   pOut->u.i = u.bn.v;
 68347   break;
 68350 /* Opcode: NullRow P1 * * * *
 68351 **
 68352 ** Move the cursor P1 to a null row.  Any OP_Column operations
 68353 ** that occur while the cursor is on the null row will always
 68354 ** write a NULL.
 68355 */
 68356 case OP_NullRow: {
 68357 #if 0  /* local variables moved into u.bo */
 68358   VdbeCursor *pC;
 68359 #endif /* local variables moved into u.bo */
 68361   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68362   u.bo.pC = p->apCsr[pOp->p1];
 68363   assert( u.bo.pC!=0 );
 68364   u.bo.pC->nullRow = 1;
 68365   u.bo.pC->rowidIsValid = 0;
 68366   assert( u.bo.pC->pCursor || u.bo.pC->pVtabCursor );
 68367   if( u.bo.pC->pCursor ){
 68368     sqlite3BtreeClearCursor(u.bo.pC->pCursor);
 68370   break;
 68373 /* Opcode: Last P1 P2 * * *
 68374 **
 68375 ** The next use of the Rowid or Column or Next instruction for P1 
 68376 ** will refer to the last entry in the database table or index.
 68377 ** If the table or index is empty and P2>0, then jump immediately to P2.
 68378 ** If P2 is 0 or if the table or index is not empty, fall through
 68379 ** to the following instruction.
 68380 */
 68381 case OP_Last: {        /* jump */
 68382 #if 0  /* local variables moved into u.bp */
 68383   VdbeCursor *pC;
 68384   BtCursor *pCrsr;
 68385   int res;
 68386 #endif /* local variables moved into u.bp */
 68388   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68389   u.bp.pC = p->apCsr[pOp->p1];
 68390   assert( u.bp.pC!=0 );
 68391   u.bp.pCrsr = u.bp.pC->pCursor;
 68392   u.bp.res = 0;
 68393   if( ALWAYS(u.bp.pCrsr!=0) ){
 68394     rc = sqlite3BtreeLast(u.bp.pCrsr, &u.bp.res);
 68396   u.bp.pC->nullRow = (u8)u.bp.res;
 68397   u.bp.pC->deferredMoveto = 0;
 68398   u.bp.pC->rowidIsValid = 0;
 68399   u.bp.pC->cacheStatus = CACHE_STALE;
 68400   if( pOp->p2>0 && u.bp.res ){
 68401     pc = pOp->p2 - 1;
 68403   break;
 68407 /* Opcode: Sort P1 P2 * * *
 68408 **
 68409 ** This opcode does exactly the same thing as OP_Rewind except that
 68410 ** it increments an undocumented global variable used for testing.
 68411 **
 68412 ** Sorting is accomplished by writing records into a sorting index,
 68413 ** then rewinding that index and playing it back from beginning to
 68414 ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
 68415 ** rewinding so that the global variable will be incremented and
 68416 ** regression tests can determine whether or not the optimizer is
 68417 ** correctly optimizing out sorts.
 68418 */
 68419 case OP_SorterSort:    /* jump */
 68420 #ifdef SQLITE_OMIT_MERGE_SORT
 68421   pOp->opcode = OP_Sort;
 68422 #endif
 68423 case OP_Sort: {        /* jump */
 68424 #ifdef SQLITE_TEST
 68425   sqlite3_sort_count++;
 68426   sqlite3_search_count--;
 68427 #endif
 68428   p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
 68429   /* Fall through into OP_Rewind */
 68431 /* Opcode: Rewind P1 P2 * * *
 68432 **
 68433 ** The next use of the Rowid or Column or Next instruction for P1 
 68434 ** will refer to the first entry in the database table or index.
 68435 ** If the table or index is empty and P2>0, then jump immediately to P2.
 68436 ** If P2 is 0 or if the table or index is not empty, fall through
 68437 ** to the following instruction.
 68438 */
 68439 case OP_Rewind: {        /* jump */
 68440 #if 0  /* local variables moved into u.bq */
 68441   VdbeCursor *pC;
 68442   BtCursor *pCrsr;
 68443   int res;
 68444 #endif /* local variables moved into u.bq */
 68446   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68447   u.bq.pC = p->apCsr[pOp->p1];
 68448   assert( u.bq.pC!=0 );
 68449   assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterSort) );
 68450   u.bq.res = 1;
 68451   if( isSorter(u.bq.pC) ){
 68452     rc = sqlite3VdbeSorterRewind(db, u.bq.pC, &u.bq.res);
 68453   }else{
 68454     u.bq.pCrsr = u.bq.pC->pCursor;
 68455     assert( u.bq.pCrsr );
 68456     rc = sqlite3BtreeFirst(u.bq.pCrsr, &u.bq.res);
 68457     u.bq.pC->atFirst = u.bq.res==0 ?1:0;
 68458     u.bq.pC->deferredMoveto = 0;
 68459     u.bq.pC->cacheStatus = CACHE_STALE;
 68460     u.bq.pC->rowidIsValid = 0;
 68462   u.bq.pC->nullRow = (u8)u.bq.res;
 68463   assert( pOp->p2>0 && pOp->p2<p->nOp );
 68464   if( u.bq.res ){
 68465     pc = pOp->p2 - 1;
 68467   break;
 68470 /* Opcode: Next P1 P2 * P4 P5
 68471 **
 68472 ** Advance cursor P1 so that it points to the next key/data pair in its
 68473 ** table or index.  If there are no more key/value pairs then fall through
 68474 ** to the following instruction.  But if the cursor advance was successful,
 68475 ** jump immediately to P2.
 68476 **
 68477 ** The P1 cursor must be for a real table, not a pseudo-table.
 68478 **
 68479 ** P4 is always of type P4_ADVANCE. The function pointer points to
 68480 ** sqlite3BtreeNext().
 68481 **
 68482 ** If P5 is positive and the jump is taken, then event counter
 68483 ** number P5-1 in the prepared statement is incremented.
 68484 **
 68485 ** See also: Prev
 68486 */
 68487 /* Opcode: Prev P1 P2 * * P5
 68488 **
 68489 ** Back up cursor P1 so that it points to the previous key/data pair in its
 68490 ** table or index.  If there is no previous key/value pairs then fall through
 68491 ** to the following instruction.  But if the cursor backup was successful,
 68492 ** jump immediately to P2.
 68493 **
 68494 ** The P1 cursor must be for a real table, not a pseudo-table.
 68495 **
 68496 ** P4 is always of type P4_ADVANCE. The function pointer points to
 68497 ** sqlite3BtreePrevious().
 68498 **
 68499 ** If P5 is positive and the jump is taken, then event counter
 68500 ** number P5-1 in the prepared statement is incremented.
 68501 */
 68502 case OP_SorterNext:    /* jump */
 68503 #ifdef SQLITE_OMIT_MERGE_SORT
 68504   pOp->opcode = OP_Next;
 68505 #endif
 68506 case OP_Prev:          /* jump */
 68507 case OP_Next: {        /* jump */
 68508 #if 0  /* local variables moved into u.br */
 68509   VdbeCursor *pC;
 68510   int res;
 68511 #endif /* local variables moved into u.br */
 68513   CHECK_FOR_INTERRUPT;
 68514   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68515   assert( pOp->p5<=ArraySize(p->aCounter) );
 68516   u.br.pC = p->apCsr[pOp->p1];
 68517   if( u.br.pC==0 ){
 68518     break;  /* See ticket #2273 */
 68520   assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
 68521   if( isSorter(u.br.pC) ){
 68522     assert( pOp->opcode==OP_SorterNext );
 68523     rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
 68524   }else{
 68525     u.br.res = 1;
 68526     assert( u.br.pC->deferredMoveto==0 );
 68527     assert( u.br.pC->pCursor );
 68528     assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
 68529     assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
 68530     rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
 68532   u.br.pC->nullRow = (u8)u.br.res;
 68533   u.br.pC->cacheStatus = CACHE_STALE;
 68534   if( u.br.res==0 ){
 68535     pc = pOp->p2 - 1;
 68536     if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
 68537 #ifdef SQLITE_TEST
 68538     sqlite3_search_count++;
 68539 #endif
 68541   u.br.pC->rowidIsValid = 0;
 68542   break;
 68545 /* Opcode: IdxInsert P1 P2 P3 * P5
 68546 **
 68547 ** Register P2 holds an SQL index key made using the
 68548 ** MakeRecord instructions.  This opcode writes that key
 68549 ** into the index P1.  Data for the entry is nil.
 68550 **
 68551 ** P3 is a flag that provides a hint to the b-tree layer that this
 68552 ** insert is likely to be an append.
 68553 **
 68554 ** This instruction only works for indices.  The equivalent instruction
 68555 ** for tables is OP_Insert.
 68556 */
 68557 case OP_SorterInsert:       /* in2 */
 68558 #ifdef SQLITE_OMIT_MERGE_SORT
 68559   pOp->opcode = OP_IdxInsert;
 68560 #endif
 68561 case OP_IdxInsert: {        /* in2 */
 68562 #if 0  /* local variables moved into u.bs */
 68563   VdbeCursor *pC;
 68564   BtCursor *pCrsr;
 68565   int nKey;
 68566   const char *zKey;
 68567 #endif /* local variables moved into u.bs */
 68569   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68570   u.bs.pC = p->apCsr[pOp->p1];
 68571   assert( u.bs.pC!=0 );
 68572   assert( u.bs.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
 68573   pIn2 = &aMem[pOp->p2];
 68574   assert( pIn2->flags & MEM_Blob );
 68575   u.bs.pCrsr = u.bs.pC->pCursor;
 68576   if( ALWAYS(u.bs.pCrsr!=0) ){
 68577     assert( u.bs.pC->isTable==0 );
 68578     rc = ExpandBlob(pIn2);
 68579     if( rc==SQLITE_OK ){
 68580       if( isSorter(u.bs.pC) ){
 68581         rc = sqlite3VdbeSorterWrite(db, u.bs.pC, pIn2);
 68582       }else{
 68583         u.bs.nKey = pIn2->n;
 68584         u.bs.zKey = pIn2->z;
 68585         rc = sqlite3BtreeInsert(u.bs.pCrsr, u.bs.zKey, u.bs.nKey, "", 0, 0, pOp->p3,
 68586             ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bs.pC->seekResult : 0)
 68587             );
 68588         assert( u.bs.pC->deferredMoveto==0 );
 68589         u.bs.pC->cacheStatus = CACHE_STALE;
 68593   break;
 68596 /* Opcode: IdxDelete P1 P2 P3 * *
 68597 **
 68598 ** The content of P3 registers starting at register P2 form
 68599 ** an unpacked index key. This opcode removes that entry from the 
 68600 ** index opened by cursor P1.
 68601 */
 68602 case OP_IdxDelete: {
 68603 #if 0  /* local variables moved into u.bt */
 68604   VdbeCursor *pC;
 68605   BtCursor *pCrsr;
 68606   int res;
 68607   UnpackedRecord r;
 68608 #endif /* local variables moved into u.bt */
 68610   assert( pOp->p3>0 );
 68611   assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
 68612   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68613   u.bt.pC = p->apCsr[pOp->p1];
 68614   assert( u.bt.pC!=0 );
 68615   u.bt.pCrsr = u.bt.pC->pCursor;
 68616   if( ALWAYS(u.bt.pCrsr!=0) ){
 68617     u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
 68618     u.bt.r.nField = (u16)pOp->p3;
 68619     u.bt.r.flags = 0;
 68620     u.bt.r.aMem = &aMem[pOp->p2];
 68621 #ifdef SQLITE_DEBUG
 68622     { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
 68623 #endif
 68624     rc = sqlite3BtreeMovetoUnpacked(u.bt.pCrsr, &u.bt.r, 0, 0, &u.bt.res);
 68625     if( rc==SQLITE_OK && u.bt.res==0 ){
 68626       rc = sqlite3BtreeDelete(u.bt.pCrsr);
 68628     assert( u.bt.pC->deferredMoveto==0 );
 68629     u.bt.pC->cacheStatus = CACHE_STALE;
 68631   break;
 68634 /* Opcode: IdxRowid P1 P2 * * *
 68635 **
 68636 ** Write into register P2 an integer which is the last entry in the record at
 68637 ** the end of the index key pointed to by cursor P1.  This integer should be
 68638 ** the rowid of the table entry to which this index entry points.
 68639 **
 68640 ** See also: Rowid, MakeRecord.
 68641 */
 68642 case OP_IdxRowid: {              /* out2-prerelease */
 68643 #if 0  /* local variables moved into u.bu */
 68644   BtCursor *pCrsr;
 68645   VdbeCursor *pC;
 68646   i64 rowid;
 68647 #endif /* local variables moved into u.bu */
 68649   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68650   u.bu.pC = p->apCsr[pOp->p1];
 68651   assert( u.bu.pC!=0 );
 68652   u.bu.pCrsr = u.bu.pC->pCursor;
 68653   pOut->flags = MEM_Null;
 68654   if( ALWAYS(u.bu.pCrsr!=0) ){
 68655     rc = sqlite3VdbeCursorMoveto(u.bu.pC);
 68656     if( NEVER(rc) ) goto abort_due_to_error;
 68657     assert( u.bu.pC->deferredMoveto==0 );
 68658     assert( u.bu.pC->isTable==0 );
 68659     if( !u.bu.pC->nullRow ){
 68660       rc = sqlite3VdbeIdxRowid(db, u.bu.pCrsr, &u.bu.rowid);
 68661       if( rc!=SQLITE_OK ){
 68662         goto abort_due_to_error;
 68664       pOut->u.i = u.bu.rowid;
 68665       pOut->flags = MEM_Int;
 68668   break;
 68671 /* Opcode: IdxGE P1 P2 P3 P4 P5
 68672 **
 68673 ** The P4 register values beginning with P3 form an unpacked index 
 68674 ** key that omits the ROWID.  Compare this key value against the index 
 68675 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
 68676 **
 68677 ** If the P1 index entry is greater than or equal to the key value
 68678 ** then jump to P2.  Otherwise fall through to the next instruction.
 68679 **
 68680 ** If P5 is non-zero then the key value is increased by an epsilon 
 68681 ** prior to the comparison.  This make the opcode work like IdxGT except
 68682 ** that if the key from register P3 is a prefix of the key in the cursor,
 68683 ** the result is false whereas it would be true with IdxGT.
 68684 */
 68685 /* Opcode: IdxLT P1 P2 P3 P4 P5
 68686 **
 68687 ** The P4 register values beginning with P3 form an unpacked index 
 68688 ** key that omits the ROWID.  Compare this key value against the index 
 68689 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
 68690 **
 68691 ** If the P1 index entry is less than the key value then jump to P2.
 68692 ** Otherwise fall through to the next instruction.
 68693 **
 68694 ** If P5 is non-zero then the key value is increased by an epsilon prior 
 68695 ** to the comparison.  This makes the opcode work like IdxLE.
 68696 */
 68697 case OP_IdxLT:          /* jump */
 68698 case OP_IdxGE: {        /* jump */
 68699 #if 0  /* local variables moved into u.bv */
 68700   VdbeCursor *pC;
 68701   int res;
 68702   UnpackedRecord r;
 68703 #endif /* local variables moved into u.bv */
 68705   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68706   u.bv.pC = p->apCsr[pOp->p1];
 68707   assert( u.bv.pC!=0 );
 68708   assert( u.bv.pC->isOrdered );
 68709   if( ALWAYS(u.bv.pC->pCursor!=0) ){
 68710     assert( u.bv.pC->deferredMoveto==0 );
 68711     assert( pOp->p5==0 || pOp->p5==1 );
 68712     assert( pOp->p4type==P4_INT32 );
 68713     u.bv.r.pKeyInfo = u.bv.pC->pKeyInfo;
 68714     u.bv.r.nField = (u16)pOp->p4.i;
 68715     if( pOp->p5 ){
 68716       u.bv.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
 68717     }else{
 68718       u.bv.r.flags = UNPACKED_PREFIX_MATCH;
 68720     u.bv.r.aMem = &aMem[pOp->p3];
 68721 #ifdef SQLITE_DEBUG
 68722     { int i; for(i=0; i<u.bv.r.nField; i++) assert( memIsValid(&u.bv.r.aMem[i]) ); }
 68723 #endif
 68724     rc = sqlite3VdbeIdxKeyCompare(u.bv.pC, &u.bv.r, &u.bv.res);
 68725     if( pOp->opcode==OP_IdxLT ){
 68726       u.bv.res = -u.bv.res;
 68727     }else{
 68728       assert( pOp->opcode==OP_IdxGE );
 68729       u.bv.res++;
 68731     if( u.bv.res>0 ){
 68732       pc = pOp->p2 - 1 ;
 68735   break;
 68738 /* Opcode: Destroy P1 P2 P3 * *
 68739 **
 68740 ** Delete an entire database table or index whose root page in the database
 68741 ** file is given by P1.
 68742 **
 68743 ** The table being destroyed is in the main database file if P3==0.  If
 68744 ** P3==1 then the table to be clear is in the auxiliary database file
 68745 ** that is used to store tables create using CREATE TEMPORARY TABLE.
 68746 **
 68747 ** If AUTOVACUUM is enabled then it is possible that another root page
 68748 ** might be moved into the newly deleted root page in order to keep all
 68749 ** root pages contiguous at the beginning of the database.  The former
 68750 ** value of the root page that moved - its value before the move occurred -
 68751 ** is stored in register P2.  If no page 
 68752 ** movement was required (because the table being dropped was already 
 68753 ** the last one in the database) then a zero is stored in register P2.
 68754 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
 68755 **
 68756 ** See also: Clear
 68757 */
 68758 case OP_Destroy: {     /* out2-prerelease */
 68759 #if 0  /* local variables moved into u.bw */
 68760   int iMoved;
 68761   int iCnt;
 68762   Vdbe *pVdbe;
 68763   int iDb;
 68764 #endif /* local variables moved into u.bw */
 68766 #ifndef SQLITE_OMIT_VIRTUALTABLE
 68767   u.bw.iCnt = 0;
 68768   for(u.bw.pVdbe=db->pVdbe; u.bw.pVdbe; u.bw.pVdbe = u.bw.pVdbe->pNext){
 68769     if( u.bw.pVdbe->magic==VDBE_MAGIC_RUN && u.bw.pVdbe->inVtabMethod<2 && u.bw.pVdbe->pc>=0 ){
 68770       u.bw.iCnt++;
 68773 #else
 68774   u.bw.iCnt = db->activeVdbeCnt;
 68775 #endif
 68776   pOut->flags = MEM_Null;
 68777   if( u.bw.iCnt>1 ){
 68778     rc = SQLITE_LOCKED;
 68779     p->errorAction = OE_Abort;
 68780   }else{
 68781     u.bw.iDb = pOp->p3;
 68782     assert( u.bw.iCnt==1 );
 68783     assert( (p->btreeMask & (((yDbMask)1)<<u.bw.iDb))!=0 );
 68784     rc = sqlite3BtreeDropTable(db->aDb[u.bw.iDb].pBt, pOp->p1, &u.bw.iMoved);
 68785     pOut->flags = MEM_Int;
 68786     pOut->u.i = u.bw.iMoved;
 68787 #ifndef SQLITE_OMIT_AUTOVACUUM
 68788     if( rc==SQLITE_OK && u.bw.iMoved!=0 ){
 68789       sqlite3RootPageMoved(db, u.bw.iDb, u.bw.iMoved, pOp->p1);
 68790       /* All OP_Destroy operations occur on the same btree */
 68791       assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bw.iDb+1 );
 68792       resetSchemaOnFault = u.bw.iDb+1;
 68794 #endif
 68796   break;
 68799 /* Opcode: Clear P1 P2 P3
 68800 **
 68801 ** Delete all contents of the database table or index whose root page
 68802 ** in the database file is given by P1.  But, unlike Destroy, do not
 68803 ** remove the table or index from the database file.
 68804 **
 68805 ** The table being clear is in the main database file if P2==0.  If
 68806 ** P2==1 then the table to be clear is in the auxiliary database file
 68807 ** that is used to store tables create using CREATE TEMPORARY TABLE.
 68808 **
 68809 ** If the P3 value is non-zero, then the table referred to must be an
 68810 ** intkey table (an SQL table, not an index). In this case the row change 
 68811 ** count is incremented by the number of rows in the table being cleared. 
 68812 ** If P3 is greater than zero, then the value stored in register P3 is
 68813 ** also incremented by the number of rows in the table being cleared.
 68814 **
 68815 ** See also: Destroy
 68816 */
 68817 case OP_Clear: {
 68818 #if 0  /* local variables moved into u.bx */
 68819   int nChange;
 68820 #endif /* local variables moved into u.bx */
 68822   u.bx.nChange = 0;
 68823   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
 68824   rc = sqlite3BtreeClearTable(
 68825       db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
 68826   );
 68827   if( pOp->p3 ){
 68828     p->nChange += u.bx.nChange;
 68829     if( pOp->p3>0 ){
 68830       assert( memIsValid(&aMem[pOp->p3]) );
 68831       memAboutToChange(p, &aMem[pOp->p3]);
 68832       aMem[pOp->p3].u.i += u.bx.nChange;
 68835   break;
 68838 /* Opcode: CreateTable P1 P2 * * *
 68839 **
 68840 ** Allocate a new table in the main database file if P1==0 or in the
 68841 ** auxiliary database file if P1==1 or in an attached database if
 68842 ** P1>1.  Write the root page number of the new table into
 68843 ** register P2
 68844 **
 68845 ** The difference between a table and an index is this:  A table must
 68846 ** have a 4-byte integer key and can have arbitrary data.  An index
 68847 ** has an arbitrary key but no data.
 68848 **
 68849 ** See also: CreateIndex
 68850 */
 68851 /* Opcode: CreateIndex P1 P2 * * *
 68852 **
 68853 ** Allocate a new index in the main database file if P1==0 or in the
 68854 ** auxiliary database file if P1==1 or in an attached database if
 68855 ** P1>1.  Write the root page number of the new table into
 68856 ** register P2.
 68857 **
 68858 ** See documentation on OP_CreateTable for additional information.
 68859 */
 68860 case OP_CreateIndex:            /* out2-prerelease */
 68861 case OP_CreateTable: {          /* out2-prerelease */
 68862 #if 0  /* local variables moved into u.by */
 68863   int pgno;
 68864   int flags;
 68865   Db *pDb;
 68866 #endif /* local variables moved into u.by */
 68868   u.by.pgno = 0;
 68869   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 68870   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 68871   u.by.pDb = &db->aDb[pOp->p1];
 68872   assert( u.by.pDb->pBt!=0 );
 68873   if( pOp->opcode==OP_CreateTable ){
 68874     /* u.by.flags = BTREE_INTKEY; */
 68875     u.by.flags = BTREE_INTKEY;
 68876   }else{
 68877     u.by.flags = BTREE_BLOBKEY;
 68879   rc = sqlite3BtreeCreateTable(u.by.pDb->pBt, &u.by.pgno, u.by.flags);
 68880   pOut->u.i = u.by.pgno;
 68881   break;
 68884 /* Opcode: ParseSchema P1 * * P4 *
 68885 **
 68886 ** Read and parse all entries from the SQLITE_MASTER table of database P1
 68887 ** that match the WHERE clause P4. 
 68888 **
 68889 ** This opcode invokes the parser to create a new virtual machine,
 68890 ** then runs the new virtual machine.  It is thus a re-entrant opcode.
 68891 */
 68892 case OP_ParseSchema: {
 68893 #if 0  /* local variables moved into u.bz */
 68894   int iDb;
 68895   const char *zMaster;
 68896   char *zSql;
 68897   InitData initData;
 68898 #endif /* local variables moved into u.bz */
 68900   /* Any prepared statement that invokes this opcode will hold mutexes
 68901   ** on every btree.  This is a prerequisite for invoking
 68902   ** sqlite3InitCallback().
 68903   */
 68904 #ifdef SQLITE_DEBUG
 68905   for(u.bz.iDb=0; u.bz.iDb<db->nDb; u.bz.iDb++){
 68906     assert( u.bz.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bz.iDb].pBt) );
 68908 #endif
 68910   u.bz.iDb = pOp->p1;
 68911   assert( u.bz.iDb>=0 && u.bz.iDb<db->nDb );
 68912   assert( DbHasProperty(db, u.bz.iDb, DB_SchemaLoaded) );
 68913   /* Used to be a conditional */ {
 68914     u.bz.zMaster = SCHEMA_TABLE(u.bz.iDb);
 68915     u.bz.initData.db = db;
 68916     u.bz.initData.iDb = pOp->p1;
 68917     u.bz.initData.pzErrMsg = &p->zErrMsg;
 68918     u.bz.zSql = sqlite3MPrintf(db,
 68919        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 68920        db->aDb[u.bz.iDb].zName, u.bz.zMaster, pOp->p4.z);
 68921     if( u.bz.zSql==0 ){
 68922       rc = SQLITE_NOMEM;
 68923     }else{
 68924       assert( db->init.busy==0 );
 68925       db->init.busy = 1;
 68926       u.bz.initData.rc = SQLITE_OK;
 68927       assert( !db->mallocFailed );
 68928       rc = sqlite3_exec(db, u.bz.zSql, sqlite3InitCallback, &u.bz.initData, 0);
 68929       if( rc==SQLITE_OK ) rc = u.bz.initData.rc;
 68930       sqlite3DbFree(db, u.bz.zSql);
 68931       db->init.busy = 0;
 68934   if( rc ) sqlite3ResetAllSchemasOfConnection(db);
 68935   if( rc==SQLITE_NOMEM ){
 68936     goto no_mem;
 68938   break;
 68941 #if !defined(SQLITE_OMIT_ANALYZE)
 68942 /* Opcode: LoadAnalysis P1 * * * *
 68943 **
 68944 ** Read the sqlite_stat1 table for database P1 and load the content
 68945 ** of that table into the internal index hash table.  This will cause
 68946 ** the analysis to be used when preparing all subsequent queries.
 68947 */
 68948 case OP_LoadAnalysis: {
 68949   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 68950   rc = sqlite3AnalysisLoad(db, pOp->p1);
 68951   break;  
 68953 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
 68955 /* Opcode: DropTable P1 * * P4 *
 68956 **
 68957 ** Remove the internal (in-memory) data structures that describe
 68958 ** the table named P4 in database P1.  This is called after a table
 68959 ** is dropped in order to keep the internal representation of the
 68960 ** schema consistent with what is on disk.
 68961 */
 68962 case OP_DropTable: {
 68963   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
 68964   break;
 68967 /* Opcode: DropIndex P1 * * P4 *
 68968 **
 68969 ** Remove the internal (in-memory) data structures that describe
 68970 ** the index named P4 in database P1.  This is called after an index
 68971 ** is dropped in order to keep the internal representation of the
 68972 ** schema consistent with what is on disk.
 68973 */
 68974 case OP_DropIndex: {
 68975   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
 68976   break;
 68979 /* Opcode: DropTrigger P1 * * P4 *
 68980 **
 68981 ** Remove the internal (in-memory) data structures that describe
 68982 ** the trigger named P4 in database P1.  This is called after a trigger
 68983 ** is dropped in order to keep the internal representation of the
 68984 ** schema consistent with what is on disk.
 68985 */
 68986 case OP_DropTrigger: {
 68987   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
 68988   break;
 68992 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 68993 /* Opcode: IntegrityCk P1 P2 P3 * P5
 68994 **
 68995 ** Do an analysis of the currently open database.  Store in
 68996 ** register P1 the text of an error message describing any problems.
 68997 ** If no problems are found, store a NULL in register P1.
 68998 **
 68999 ** The register P3 contains the maximum number of allowed errors.
 69000 ** At most reg(P3) errors will be reported.
 69001 ** In other words, the analysis stops as soon as reg(P1) errors are 
 69002 ** seen.  Reg(P1) is updated with the number of errors remaining.
 69003 **
 69004 ** The root page numbers of all tables in the database are integer
 69005 ** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
 69006 ** total.
 69007 **
 69008 ** If P5 is not zero, the check is done on the auxiliary database
 69009 ** file, not the main database file.
 69010 **
 69011 ** This opcode is used to implement the integrity_check pragma.
 69012 */
 69013 case OP_IntegrityCk: {
 69014 #if 0  /* local variables moved into u.ca */
 69015   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 69016   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 69017   int j;          /* Loop counter */
 69018   int nErr;       /* Number of errors reported */
 69019   char *z;        /* Text of the error report */
 69020   Mem *pnErr;     /* Register keeping track of errors remaining */
 69021 #endif /* local variables moved into u.ca */
 69023   u.ca.nRoot = pOp->p2;
 69024   assert( u.ca.nRoot>0 );
 69025   u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
 69026   if( u.ca.aRoot==0 ) goto no_mem;
 69027   assert( pOp->p3>0 && pOp->p3<=p->nMem );
 69028   u.ca.pnErr = &aMem[pOp->p3];
 69029   assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
 69030   assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 69031   pIn1 = &aMem[pOp->p1];
 69032   for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
 69033     u.ca.aRoot[u.ca.j] = (int)sqlite3VdbeIntValue(&pIn1[u.ca.j]);
 69035   u.ca.aRoot[u.ca.j] = 0;
 69036   assert( pOp->p5<db->nDb );
 69037   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
 69038   u.ca.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.ca.aRoot, u.ca.nRoot,
 69039                                  (int)u.ca.pnErr->u.i, &u.ca.nErr);
 69040   sqlite3DbFree(db, u.ca.aRoot);
 69041   u.ca.pnErr->u.i -= u.ca.nErr;
 69042   sqlite3VdbeMemSetNull(pIn1);
 69043   if( u.ca.nErr==0 ){
 69044     assert( u.ca.z==0 );
 69045   }else if( u.ca.z==0 ){
 69046     goto no_mem;
 69047   }else{
 69048     sqlite3VdbeMemSetStr(pIn1, u.ca.z, -1, SQLITE_UTF8, sqlite3_free);
 69050   UPDATE_MAX_BLOBSIZE(pIn1);
 69051   sqlite3VdbeChangeEncoding(pIn1, encoding);
 69052   break;
 69054 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 69056 /* Opcode: RowSetAdd P1 P2 * * *
 69057 **
 69058 ** Insert the integer value held by register P2 into a boolean index
 69059 ** held in register P1.
 69060 **
 69061 ** An assertion fails if P2 is not an integer.
 69062 */
 69063 case OP_RowSetAdd: {       /* in1, in2 */
 69064   pIn1 = &aMem[pOp->p1];
 69065   pIn2 = &aMem[pOp->p2];
 69066   assert( (pIn2->flags & MEM_Int)!=0 );
 69067   if( (pIn1->flags & MEM_RowSet)==0 ){
 69068     sqlite3VdbeMemSetRowSet(pIn1);
 69069     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 69071   sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
 69072   break;
 69075 /* Opcode: RowSetRead P1 P2 P3 * *
 69076 **
 69077 ** Extract the smallest value from boolean index P1 and put that value into
 69078 ** register P3.  Or, if boolean index P1 is initially empty, leave P3
 69079 ** unchanged and jump to instruction P2.
 69080 */
 69081 case OP_RowSetRead: {       /* jump, in1, out3 */
 69082 #if 0  /* local variables moved into u.cb */
 69083   i64 val;
 69084 #endif /* local variables moved into u.cb */
 69085   CHECK_FOR_INTERRUPT;
 69086   pIn1 = &aMem[pOp->p1];
 69087   if( (pIn1->flags & MEM_RowSet)==0
 69088    || sqlite3RowSetNext(pIn1->u.pRowSet, &u.cb.val)==0
 69089   ){
 69090     /* The boolean index is empty */
 69091     sqlite3VdbeMemSetNull(pIn1);
 69092     pc = pOp->p2 - 1;
 69093   }else{
 69094     /* A value was pulled from the index */
 69095     sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.cb.val);
 69097   break;
 69100 /* Opcode: RowSetTest P1 P2 P3 P4
 69101 **
 69102 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
 69103 ** contains a RowSet object and that RowSet object contains
 69104 ** the value held in P3, jump to register P2. Otherwise, insert the
 69105 ** integer in P3 into the RowSet and continue on to the
 69106 ** next opcode.
 69107 **
 69108 ** The RowSet object is optimized for the case where successive sets
 69109 ** of integers, where each set contains no duplicates. Each set
 69110 ** of values is identified by a unique P4 value. The first set
 69111 ** must have P4==0, the final set P4=-1.  P4 must be either -1 or
 69112 ** non-negative.  For non-negative values of P4 only the lower 4
 69113 ** bits are significant.
 69114 **
 69115 ** This allows optimizations: (a) when P4==0 there is no need to test
 69116 ** the rowset object for P3, as it is guaranteed not to contain it,
 69117 ** (b) when P4==-1 there is no need to insert the value, as it will
 69118 ** never be tested for, and (c) when a value that is part of set X is
 69119 ** inserted, there is no need to search to see if the same value was
 69120 ** previously inserted as part of set X (only if it was previously
 69121 ** inserted as part of some other set).
 69122 */
 69123 case OP_RowSetTest: {                     /* jump, in1, in3 */
 69124 #if 0  /* local variables moved into u.cc */
 69125   int iSet;
 69126   int exists;
 69127 #endif /* local variables moved into u.cc */
 69129   pIn1 = &aMem[pOp->p1];
 69130   pIn3 = &aMem[pOp->p3];
 69131   u.cc.iSet = pOp->p4.i;
 69132   assert( pIn3->flags&MEM_Int );
 69134   /* If there is anything other than a rowset object in memory cell P1,
 69135   ** delete it now and initialize P1 with an empty rowset
 69136   */
 69137   if( (pIn1->flags & MEM_RowSet)==0 ){
 69138     sqlite3VdbeMemSetRowSet(pIn1);
 69139     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 69142   assert( pOp->p4type==P4_INT32 );
 69143   assert( u.cc.iSet==-1 || u.cc.iSet>=0 );
 69144   if( u.cc.iSet ){
 69145     u.cc.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
 69146                                (u8)(u.cc.iSet>=0 ? u.cc.iSet & 0xf : 0xff),
 69147                                pIn3->u.i);
 69148     if( u.cc.exists ){
 69149       pc = pOp->p2 - 1;
 69150       break;
 69153   if( u.cc.iSet>=0 ){
 69154     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
 69156   break;
 69160 #ifndef SQLITE_OMIT_TRIGGER
 69162 /* Opcode: Program P1 P2 P3 P4 *
 69163 **
 69164 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
 69165 **
 69166 ** P1 contains the address of the memory cell that contains the first memory 
 69167 ** cell in an array of values used as arguments to the sub-program. P2 
 69168 ** contains the address to jump to if the sub-program throws an IGNORE 
 69169 ** exception using the RAISE() function. Register P3 contains the address 
 69170 ** of a memory cell in this (the parent) VM that is used to allocate the 
 69171 ** memory required by the sub-vdbe at runtime.
 69172 **
 69173 ** P4 is a pointer to the VM containing the trigger program.
 69174 */
 69175 case OP_Program: {        /* jump */
 69176 #if 0  /* local variables moved into u.cd */
 69177   int nMem;               /* Number of memory registers for sub-program */
 69178   int nByte;              /* Bytes of runtime space required for sub-program */
 69179   Mem *pRt;               /* Register to allocate runtime space */
 69180   Mem *pMem;              /* Used to iterate through memory cells */
 69181   Mem *pEnd;              /* Last memory cell in new array */
 69182   VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 69183   SubProgram *pProgram;   /* Sub-program to execute */
 69184   void *t;                /* Token identifying trigger */
 69185 #endif /* local variables moved into u.cd */
 69187   u.cd.pProgram = pOp->p4.pProgram;
 69188   u.cd.pRt = &aMem[pOp->p3];
 69189   assert( u.cd.pProgram->nOp>0 );
 69191   /* If the p5 flag is clear, then recursive invocation of triggers is
 69192   ** disabled for backwards compatibility (p5 is set if this sub-program
 69193   ** is really a trigger, not a foreign key action, and the flag set
 69194   ** and cleared by the "PRAGMA recursive_triggers" command is clear).
 69195   **
 69196   ** It is recursive invocation of triggers, at the SQL level, that is
 69197   ** disabled. In some cases a single trigger may generate more than one
 69198   ** SubProgram (if the trigger may be executed with more than one different
 69199   ** ON CONFLICT algorithm). SubProgram structures associated with a
 69200   ** single trigger all have the same value for the SubProgram.token
 69201   ** variable.  */
 69202   if( pOp->p5 ){
 69203     u.cd.t = u.cd.pProgram->token;
 69204     for(u.cd.pFrame=p->pFrame; u.cd.pFrame && u.cd.pFrame->token!=u.cd.t; u.cd.pFrame=u.cd.pFrame->pParent);
 69205     if( u.cd.pFrame ) break;
 69208   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
 69209     rc = SQLITE_ERROR;
 69210     sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
 69211     break;
 69214   /* Register u.cd.pRt is used to store the memory required to save the state
 69215   ** of the current program, and the memory required at runtime to execute
 69216   ** the trigger program. If this trigger has been fired before, then u.cd.pRt
 69217   ** is already allocated. Otherwise, it must be initialized.  */
 69218   if( (u.cd.pRt->flags&MEM_Frame)==0 ){
 69219     /* SubProgram.nMem is set to the number of memory cells used by the
 69220     ** program stored in SubProgram.aOp. As well as these, one memory
 69221     ** cell is required for each cursor used by the program. Set local
 69222     ** variable u.cd.nMem (and later, VdbeFrame.nChildMem) to this value.
 69223     */
 69224     u.cd.nMem = u.cd.pProgram->nMem + u.cd.pProgram->nCsr;
 69225     u.cd.nByte = ROUND8(sizeof(VdbeFrame))
 69226               + u.cd.nMem * sizeof(Mem)
 69227               + u.cd.pProgram->nCsr * sizeof(VdbeCursor *)
 69228               + u.cd.pProgram->nOnce * sizeof(u8);
 69229     u.cd.pFrame = sqlite3DbMallocZero(db, u.cd.nByte);
 69230     if( !u.cd.pFrame ){
 69231       goto no_mem;
 69233     sqlite3VdbeMemRelease(u.cd.pRt);
 69234     u.cd.pRt->flags = MEM_Frame;
 69235     u.cd.pRt->u.pFrame = u.cd.pFrame;
 69237     u.cd.pFrame->v = p;
 69238     u.cd.pFrame->nChildMem = u.cd.nMem;
 69239     u.cd.pFrame->nChildCsr = u.cd.pProgram->nCsr;
 69240     u.cd.pFrame->pc = pc;
 69241     u.cd.pFrame->aMem = p->aMem;
 69242     u.cd.pFrame->nMem = p->nMem;
 69243     u.cd.pFrame->apCsr = p->apCsr;
 69244     u.cd.pFrame->nCursor = p->nCursor;
 69245     u.cd.pFrame->aOp = p->aOp;
 69246     u.cd.pFrame->nOp = p->nOp;
 69247     u.cd.pFrame->token = u.cd.pProgram->token;
 69248     u.cd.pFrame->aOnceFlag = p->aOnceFlag;
 69249     u.cd.pFrame->nOnceFlag = p->nOnceFlag;
 69251     u.cd.pEnd = &VdbeFrameMem(u.cd.pFrame)[u.cd.pFrame->nChildMem];
 69252     for(u.cd.pMem=VdbeFrameMem(u.cd.pFrame); u.cd.pMem!=u.cd.pEnd; u.cd.pMem++){
 69253       u.cd.pMem->flags = MEM_Invalid;
 69254       u.cd.pMem->db = db;
 69256   }else{
 69257     u.cd.pFrame = u.cd.pRt->u.pFrame;
 69258     assert( u.cd.pProgram->nMem+u.cd.pProgram->nCsr==u.cd.pFrame->nChildMem );
 69259     assert( u.cd.pProgram->nCsr==u.cd.pFrame->nChildCsr );
 69260     assert( pc==u.cd.pFrame->pc );
 69263   p->nFrame++;
 69264   u.cd.pFrame->pParent = p->pFrame;
 69265   u.cd.pFrame->lastRowid = lastRowid;
 69266   u.cd.pFrame->nChange = p->nChange;
 69267   p->nChange = 0;
 69268   p->pFrame = u.cd.pFrame;
 69269   p->aMem = aMem = &VdbeFrameMem(u.cd.pFrame)[-1];
 69270   p->nMem = u.cd.pFrame->nChildMem;
 69271   p->nCursor = (u16)u.cd.pFrame->nChildCsr;
 69272   p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
 69273   p->aOp = aOp = u.cd.pProgram->aOp;
 69274   p->nOp = u.cd.pProgram->nOp;
 69275   p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
 69276   p->nOnceFlag = u.cd.pProgram->nOnce;
 69277   pc = -1;
 69278   memset(p->aOnceFlag, 0, p->nOnceFlag);
 69280   break;
 69283 /* Opcode: Param P1 P2 * * *
 69284 **
 69285 ** This opcode is only ever present in sub-programs called via the 
 69286 ** OP_Program instruction. Copy a value currently stored in a memory 
 69287 ** cell of the calling (parent) frame to cell P2 in the current frames 
 69288 ** address space. This is used by trigger programs to access the new.* 
 69289 ** and old.* values.
 69290 **
 69291 ** The address of the cell in the parent frame is determined by adding
 69292 ** the value of the P1 argument to the value of the P1 argument to the
 69293 ** calling OP_Program instruction.
 69294 */
 69295 case OP_Param: {           /* out2-prerelease */
 69296 #if 0  /* local variables moved into u.ce */
 69297   VdbeFrame *pFrame;
 69298   Mem *pIn;
 69299 #endif /* local variables moved into u.ce */
 69300   u.ce.pFrame = p->pFrame;
 69301   u.ce.pIn = &u.ce.pFrame->aMem[pOp->p1 + u.ce.pFrame->aOp[u.ce.pFrame->pc].p1];
 69302   sqlite3VdbeMemShallowCopy(pOut, u.ce.pIn, MEM_Ephem);
 69303   break;
 69306 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
 69308 #ifndef SQLITE_OMIT_FOREIGN_KEY
 69309 /* Opcode: FkCounter P1 P2 * * *
 69310 **
 69311 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
 69312 ** If P1 is non-zero, the database constraint counter is incremented 
 69313 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
 69314 ** statement counter is incremented (immediate foreign key constraints).
 69315 */
 69316 case OP_FkCounter: {
 69317   if( pOp->p1 ){
 69318     db->nDeferredCons += pOp->p2;
 69319   }else{
 69320     p->nFkConstraint += pOp->p2;
 69322   break;
 69325 /* Opcode: FkIfZero P1 P2 * * *
 69326 **
 69327 ** This opcode tests if a foreign key constraint-counter is currently zero.
 69328 ** If so, jump to instruction P2. Otherwise, fall through to the next 
 69329 ** instruction.
 69330 **
 69331 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
 69332 ** is zero (the one that counts deferred constraint violations). If P1 is
 69333 ** zero, the jump is taken if the statement constraint-counter is zero
 69334 ** (immediate foreign key constraint violations).
 69335 */
 69336 case OP_FkIfZero: {         /* jump */
 69337   if( pOp->p1 ){
 69338     if( db->nDeferredCons==0 ) pc = pOp->p2-1;
 69339   }else{
 69340     if( p->nFkConstraint==0 ) pc = pOp->p2-1;
 69342   break;
 69344 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
 69346 #ifndef SQLITE_OMIT_AUTOINCREMENT
 69347 /* Opcode: MemMax P1 P2 * * *
 69348 **
 69349 ** P1 is a register in the root frame of this VM (the root frame is
 69350 ** different from the current frame if this instruction is being executed
 69351 ** within a sub-program). Set the value of register P1 to the maximum of 
 69352 ** its current value and the value in register P2.
 69353 **
 69354 ** This instruction throws an error if the memory cell is not initially
 69355 ** an integer.
 69356 */
 69357 case OP_MemMax: {        /* in2 */
 69358 #if 0  /* local variables moved into u.cf */
 69359   Mem *pIn1;
 69360   VdbeFrame *pFrame;
 69361 #endif /* local variables moved into u.cf */
 69362   if( p->pFrame ){
 69363     for(u.cf.pFrame=p->pFrame; u.cf.pFrame->pParent; u.cf.pFrame=u.cf.pFrame->pParent);
 69364     u.cf.pIn1 = &u.cf.pFrame->aMem[pOp->p1];
 69365   }else{
 69366     u.cf.pIn1 = &aMem[pOp->p1];
 69368   assert( memIsValid(u.cf.pIn1) );
 69369   sqlite3VdbeMemIntegerify(u.cf.pIn1);
 69370   pIn2 = &aMem[pOp->p2];
 69371   sqlite3VdbeMemIntegerify(pIn2);
 69372   if( u.cf.pIn1->u.i<pIn2->u.i){
 69373     u.cf.pIn1->u.i = pIn2->u.i;
 69375   break;
 69377 #endif /* SQLITE_OMIT_AUTOINCREMENT */
 69379 /* Opcode: IfPos P1 P2 * * *
 69380 **
 69381 ** If the value of register P1 is 1 or greater, jump to P2.
 69382 **
 69383 ** It is illegal to use this instruction on a register that does
 69384 ** not contain an integer.  An assertion fault will result if you try.
 69385 */
 69386 case OP_IfPos: {        /* jump, in1 */
 69387   pIn1 = &aMem[pOp->p1];
 69388   assert( pIn1->flags&MEM_Int );
 69389   if( pIn1->u.i>0 ){
 69390      pc = pOp->p2 - 1;
 69392   break;
 69395 /* Opcode: IfNeg P1 P2 * * *
 69396 **
 69397 ** If the value of register P1 is less than zero, jump to P2. 
 69398 **
 69399 ** It is illegal to use this instruction on a register that does
 69400 ** not contain an integer.  An assertion fault will result if you try.
 69401 */
 69402 case OP_IfNeg: {        /* jump, in1 */
 69403   pIn1 = &aMem[pOp->p1];
 69404   assert( pIn1->flags&MEM_Int );
 69405   if( pIn1->u.i<0 ){
 69406      pc = pOp->p2 - 1;
 69408   break;
 69411 /* Opcode: IfZero P1 P2 P3 * *
 69412 **
 69413 ** The register P1 must contain an integer.  Add literal P3 to the
 69414 ** value in register P1.  If the result is exactly 0, jump to P2. 
 69415 **
 69416 ** It is illegal to use this instruction on a register that does
 69417 ** not contain an integer.  An assertion fault will result if you try.
 69418 */
 69419 case OP_IfZero: {        /* jump, in1 */
 69420   pIn1 = &aMem[pOp->p1];
 69421   assert( pIn1->flags&MEM_Int );
 69422   pIn1->u.i += pOp->p3;
 69423   if( pIn1->u.i==0 ){
 69424      pc = pOp->p2 - 1;
 69426   break;
 69429 /* Opcode: AggStep * P2 P3 P4 P5
 69430 **
 69431 ** Execute the step function for an aggregate.  The
 69432 ** function has P5 arguments.   P4 is a pointer to the FuncDef
 69433 ** structure that specifies the function.  Use register
 69434 ** P3 as the accumulator.
 69435 **
 69436 ** The P5 arguments are taken from register P2 and its
 69437 ** successors.
 69438 */
 69439 case OP_AggStep: {
 69440 #if 0  /* local variables moved into u.cg */
 69441   int n;
 69442   int i;
 69443   Mem *pMem;
 69444   Mem *pRec;
 69445   sqlite3_context ctx;
 69446   sqlite3_value **apVal;
 69447 #endif /* local variables moved into u.cg */
 69449   u.cg.n = pOp->p5;
 69450   assert( u.cg.n>=0 );
 69451   u.cg.pRec = &aMem[pOp->p2];
 69452   u.cg.apVal = p->apArg;
 69453   assert( u.cg.apVal || u.cg.n==0 );
 69454   for(u.cg.i=0; u.cg.i<u.cg.n; u.cg.i++, u.cg.pRec++){
 69455     assert( memIsValid(u.cg.pRec) );
 69456     u.cg.apVal[u.cg.i] = u.cg.pRec;
 69457     memAboutToChange(p, u.cg.pRec);
 69458     sqlite3VdbeMemStoreType(u.cg.pRec);
 69460   u.cg.ctx.pFunc = pOp->p4.pFunc;
 69461   assert( pOp->p3>0 && pOp->p3<=p->nMem );
 69462   u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
 69463   u.cg.pMem->n++;
 69464   u.cg.ctx.s.flags = MEM_Null;
 69465   u.cg.ctx.s.z = 0;
 69466   u.cg.ctx.s.zMalloc = 0;
 69467   u.cg.ctx.s.xDel = 0;
 69468   u.cg.ctx.s.db = db;
 69469   u.cg.ctx.isError = 0;
 69470   u.cg.ctx.pColl = 0;
 69471   u.cg.ctx.skipFlag = 0;
 69472   if( u.cg.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 69473     assert( pOp>p->aOp );
 69474     assert( pOp[-1].p4type==P4_COLLSEQ );
 69475     assert( pOp[-1].opcode==OP_CollSeq );
 69476     u.cg.ctx.pColl = pOp[-1].p4.pColl;
 69478   (u.cg.ctx.pFunc->xStep)(&u.cg.ctx, u.cg.n, u.cg.apVal); /* IMP: R-24505-23230 */
 69479   if( u.cg.ctx.isError ){
 69480     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cg.ctx.s));
 69481     rc = u.cg.ctx.isError;
 69483   if( u.cg.ctx.skipFlag ){
 69484     assert( pOp[-1].opcode==OP_CollSeq );
 69485     u.cg.i = pOp[-1].p1;
 69486     if( u.cg.i ) sqlite3VdbeMemSetInt64(&aMem[u.cg.i], 1);
 69489   sqlite3VdbeMemRelease(&u.cg.ctx.s);
 69491   break;
 69494 /* Opcode: AggFinal P1 P2 * P4 *
 69495 **
 69496 ** Execute the finalizer function for an aggregate.  P1 is
 69497 ** the memory location that is the accumulator for the aggregate.
 69498 **
 69499 ** P2 is the number of arguments that the step function takes and
 69500 ** P4 is a pointer to the FuncDef for this function.  The P2
 69501 ** argument is not used by this opcode.  It is only there to disambiguate
 69502 ** functions that can take varying numbers of arguments.  The
 69503 ** P4 argument is only needed for the degenerate case where
 69504 ** the step function was not previously called.
 69505 */
 69506 case OP_AggFinal: {
 69507 #if 0  /* local variables moved into u.ch */
 69508   Mem *pMem;
 69509 #endif /* local variables moved into u.ch */
 69510   assert( pOp->p1>0 && pOp->p1<=p->nMem );
 69511   u.ch.pMem = &aMem[pOp->p1];
 69512   assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 69513   rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
 69514   if( rc ){
 69515     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
 69517   sqlite3VdbeChangeEncoding(u.ch.pMem, encoding);
 69518   UPDATE_MAX_BLOBSIZE(u.ch.pMem);
 69519   if( sqlite3VdbeMemTooBig(u.ch.pMem) ){
 69520     goto too_big;
 69522   break;
 69525 #ifndef SQLITE_OMIT_WAL
 69526 /* Opcode: Checkpoint P1 P2 P3 * *
 69527 **
 69528 ** Checkpoint database P1. This is a no-op if P1 is not currently in
 69529 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
 69530 ** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
 69531 ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
 69532 ** WAL after the checkpoint into mem[P3+1] and the number of pages
 69533 ** in the WAL that have been checkpointed after the checkpoint
 69534 ** completes into mem[P3+2].  However on an error, mem[P3+1] and
 69535 ** mem[P3+2] are initialized to -1.
 69536 */
 69537 case OP_Checkpoint: {
 69538 #if 0  /* local variables moved into u.ci */
 69539   int i;                          /* Loop counter */
 69540   int aRes[3];                    /* Results */
 69541   Mem *pMem;                      /* Write results here */
 69542 #endif /* local variables moved into u.ci */
 69544   u.ci.aRes[0] = 0;
 69545   u.ci.aRes[1] = u.ci.aRes[2] = -1;
 69546   assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
 69547        || pOp->p2==SQLITE_CHECKPOINT_FULL
 69548        || pOp->p2==SQLITE_CHECKPOINT_RESTART
 69549   );
 69550   rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ci.aRes[1], &u.ci.aRes[2]);
 69551   if( rc==SQLITE_BUSY ){
 69552     rc = SQLITE_OK;
 69553     u.ci.aRes[0] = 1;
 69555   for(u.ci.i=0, u.ci.pMem = &aMem[pOp->p3]; u.ci.i<3; u.ci.i++, u.ci.pMem++){
 69556     sqlite3VdbeMemSetInt64(u.ci.pMem, (i64)u.ci.aRes[u.ci.i]);
 69558   break;
 69559 };  
 69560 #endif
 69562 #ifndef SQLITE_OMIT_PRAGMA
 69563 /* Opcode: JournalMode P1 P2 P3 * P5
 69564 **
 69565 ** Change the journal mode of database P1 to P3. P3 must be one of the
 69566 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
 69567 ** modes (delete, truncate, persist, off and memory), this is a simple
 69568 ** operation. No IO is required.
 69569 **
 69570 ** If changing into or out of WAL mode the procedure is more complicated.
 69571 **
 69572 ** Write a string containing the final journal-mode to register P2.
 69573 */
 69574 case OP_JournalMode: {    /* out2-prerelease */
 69575 #if 0  /* local variables moved into u.cj */
 69576   Btree *pBt;                     /* Btree to change journal mode of */
 69577   Pager *pPager;                  /* Pager associated with pBt */
 69578   int eNew;                       /* New journal mode */
 69579   int eOld;                       /* The old journal mode */
 69580 #ifndef SQLITE_OMIT_WAL
 69581   const char *zFilename;          /* Name of database file for pPager */
 69582 #endif
 69583 #endif /* local variables moved into u.cj */
 69585   u.cj.eNew = pOp->p3;
 69586   assert( u.cj.eNew==PAGER_JOURNALMODE_DELETE
 69587        || u.cj.eNew==PAGER_JOURNALMODE_TRUNCATE
 69588        || u.cj.eNew==PAGER_JOURNALMODE_PERSIST
 69589        || u.cj.eNew==PAGER_JOURNALMODE_OFF
 69590        || u.cj.eNew==PAGER_JOURNALMODE_MEMORY
 69591        || u.cj.eNew==PAGER_JOURNALMODE_WAL
 69592        || u.cj.eNew==PAGER_JOURNALMODE_QUERY
 69593   );
 69594   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 69596   u.cj.pBt = db->aDb[pOp->p1].pBt;
 69597   u.cj.pPager = sqlite3BtreePager(u.cj.pBt);
 69598   u.cj.eOld = sqlite3PagerGetJournalMode(u.cj.pPager);
 69599   if( u.cj.eNew==PAGER_JOURNALMODE_QUERY ) u.cj.eNew = u.cj.eOld;
 69600   if( !sqlite3PagerOkToChangeJournalMode(u.cj.pPager) ) u.cj.eNew = u.cj.eOld;
 69602 #ifndef SQLITE_OMIT_WAL
 69603   u.cj.zFilename = sqlite3PagerFilename(u.cj.pPager, 1);
 69605   /* Do not allow a transition to journal_mode=WAL for a database
 69606   ** in temporary storage or if the VFS does not support shared memory
 69607   */
 69608   if( u.cj.eNew==PAGER_JOURNALMODE_WAL
 69609    && (sqlite3Strlen30(u.cj.zFilename)==0           /* Temp file */
 69610        || !sqlite3PagerWalSupported(u.cj.pPager))   /* No shared-memory support */
 69611   ){
 69612     u.cj.eNew = u.cj.eOld;
 69615   if( (u.cj.eNew!=u.cj.eOld)
 69616    && (u.cj.eOld==PAGER_JOURNALMODE_WAL || u.cj.eNew==PAGER_JOURNALMODE_WAL)
 69617   ){
 69618     if( !db->autoCommit || db->activeVdbeCnt>1 ){
 69619       rc = SQLITE_ERROR;
 69620       sqlite3SetString(&p->zErrMsg, db,
 69621           "cannot change %s wal mode from within a transaction",
 69622           (u.cj.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
 69623       );
 69624       break;
 69625     }else{
 69627       if( u.cj.eOld==PAGER_JOURNALMODE_WAL ){
 69628         /* If leaving WAL mode, close the log file. If successful, the call
 69629         ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
 69630         ** file. An EXCLUSIVE lock may still be held on the database file
 69631         ** after a successful return.
 69632         */
 69633         rc = sqlite3PagerCloseWal(u.cj.pPager);
 69634         if( rc==SQLITE_OK ){
 69635           sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
 69637       }else if( u.cj.eOld==PAGER_JOURNALMODE_MEMORY ){
 69638         /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
 69639         ** as an intermediate */
 69640         sqlite3PagerSetJournalMode(u.cj.pPager, PAGER_JOURNALMODE_OFF);
 69643       /* Open a transaction on the database file. Regardless of the journal
 69644       ** mode, this transaction always uses a rollback journal.
 69645       */
 69646       assert( sqlite3BtreeIsInTrans(u.cj.pBt)==0 );
 69647       if( rc==SQLITE_OK ){
 69648         rc = sqlite3BtreeSetVersion(u.cj.pBt, (u.cj.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
 69652 #endif /* ifndef SQLITE_OMIT_WAL */
 69654   if( rc ){
 69655     u.cj.eNew = u.cj.eOld;
 69657   u.cj.eNew = sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
 69659   pOut = &aMem[pOp->p2];
 69660   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 69661   pOut->z = (char *)sqlite3JournalModename(u.cj.eNew);
 69662   pOut->n = sqlite3Strlen30(pOut->z);
 69663   pOut->enc = SQLITE_UTF8;
 69664   sqlite3VdbeChangeEncoding(pOut, encoding);
 69665   break;
 69666 };
 69667 #endif /* SQLITE_OMIT_PRAGMA */
 69669 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 69670 /* Opcode: Vacuum * * * * *
 69671 **
 69672 ** Vacuum the entire database.  This opcode will cause other virtual
 69673 ** machines to be created and run.  It may not be called from within
 69674 ** a transaction.
 69675 */
 69676 case OP_Vacuum: {
 69677   rc = sqlite3RunVacuum(&p->zErrMsg, db);
 69678   break;
 69680 #endif
 69682 #if !defined(SQLITE_OMIT_AUTOVACUUM)
 69683 /* Opcode: IncrVacuum P1 P2 * * *
 69684 **
 69685 ** Perform a single step of the incremental vacuum procedure on
 69686 ** the P1 database. If the vacuum has finished, jump to instruction
 69687 ** P2. Otherwise, fall through to the next instruction.
 69688 */
 69689 case OP_IncrVacuum: {        /* jump */
 69690 #if 0  /* local variables moved into u.ck */
 69691   Btree *pBt;
 69692 #endif /* local variables moved into u.ck */
 69694   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 69695   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 69696   u.ck.pBt = db->aDb[pOp->p1].pBt;
 69697   rc = sqlite3BtreeIncrVacuum(u.ck.pBt);
 69698   if( rc==SQLITE_DONE ){
 69699     pc = pOp->p2 - 1;
 69700     rc = SQLITE_OK;
 69702   break;
 69704 #endif
 69706 /* Opcode: Expire P1 * * * *
 69707 **
 69708 ** Cause precompiled statements to become expired. An expired statement
 69709 ** fails with an error code of SQLITE_SCHEMA if it is ever executed 
 69710 ** (via sqlite3_step()).
 69711 ** 
 69712 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
 69713 ** then only the currently executing statement is affected. 
 69714 */
 69715 case OP_Expire: {
 69716   if( !pOp->p1 ){
 69717     sqlite3ExpirePreparedStatements(db);
 69718   }else{
 69719     p->expired = 1;
 69721   break;
 69724 #ifndef SQLITE_OMIT_SHARED_CACHE
 69725 /* Opcode: TableLock P1 P2 P3 P4 *
 69726 **
 69727 ** Obtain a lock on a particular table. This instruction is only used when
 69728 ** the shared-cache feature is enabled. 
 69729 **
 69730 ** P1 is the index of the database in sqlite3.aDb[] of the database
 69731 ** on which the lock is acquired.  A readlock is obtained if P3==0 or
 69732 ** a write lock if P3==1.
 69733 **
 69734 ** P2 contains the root-page of the table to lock.
 69735 **
 69736 ** P4 contains a pointer to the name of the table being locked. This is only
 69737 ** used to generate an error message if the lock cannot be obtained.
 69738 */
 69739 case OP_TableLock: {
 69740   u8 isWriteLock = (u8)pOp->p3;
 69741   if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
 69742     int p1 = pOp->p1; 
 69743     assert( p1>=0 && p1<db->nDb );
 69744     assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
 69745     assert( isWriteLock==0 || isWriteLock==1 );
 69746     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
 69747     if( (rc&0xFF)==SQLITE_LOCKED ){
 69748       const char *z = pOp->p4.z;
 69749       sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
 69752   break;
 69754 #endif /* SQLITE_OMIT_SHARED_CACHE */
 69756 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69757 /* Opcode: VBegin * * * P4 *
 69758 **
 69759 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 
 69760 ** xBegin method for that table.
 69761 **
 69762 ** Also, whether or not P4 is set, check that this is not being called from
 69763 ** within a callback to a virtual table xSync() method. If it is, the error
 69764 ** code will be set to SQLITE_LOCKED.
 69765 */
 69766 case OP_VBegin: {
 69767 #if 0  /* local variables moved into u.cl */
 69768   VTable *pVTab;
 69769 #endif /* local variables moved into u.cl */
 69770   u.cl.pVTab = pOp->p4.pVtab;
 69771   rc = sqlite3VtabBegin(db, u.cl.pVTab);
 69772   if( u.cl.pVTab ) importVtabErrMsg(p, u.cl.pVTab->pVtab);
 69773   break;
 69775 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69777 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69778 /* Opcode: VCreate P1 * * P4 *
 69779 **
 69780 ** P4 is the name of a virtual table in database P1. Call the xCreate method
 69781 ** for that table.
 69782 */
 69783 case OP_VCreate: {
 69784   rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
 69785   break;
 69787 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69789 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69790 /* Opcode: VDestroy P1 * * P4 *
 69791 **
 69792 ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
 69793 ** of that table.
 69794 */
 69795 case OP_VDestroy: {
 69796   p->inVtabMethod = 2;
 69797   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
 69798   p->inVtabMethod = 0;
 69799   break;
 69801 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69803 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69804 /* Opcode: VOpen P1 * * P4 *
 69805 **
 69806 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 69807 ** P1 is a cursor number.  This opcode opens a cursor to the virtual
 69808 ** table and stores that cursor in P1.
 69809 */
 69810 case OP_VOpen: {
 69811 #if 0  /* local variables moved into u.cm */
 69812   VdbeCursor *pCur;
 69813   sqlite3_vtab_cursor *pVtabCursor;
 69814   sqlite3_vtab *pVtab;
 69815   sqlite3_module *pModule;
 69816 #endif /* local variables moved into u.cm */
 69818   u.cm.pCur = 0;
 69819   u.cm.pVtabCursor = 0;
 69820   u.cm.pVtab = pOp->p4.pVtab->pVtab;
 69821   u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
 69822   assert(u.cm.pVtab && u.cm.pModule);
 69823   rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
 69824   importVtabErrMsg(p, u.cm.pVtab);
 69825   if( SQLITE_OK==rc ){
 69826     /* Initialize sqlite3_vtab_cursor base class */
 69827     u.cm.pVtabCursor->pVtab = u.cm.pVtab;
 69829     /* Initialise vdbe cursor object */
 69830     u.cm.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 69831     if( u.cm.pCur ){
 69832       u.cm.pCur->pVtabCursor = u.cm.pVtabCursor;
 69833       u.cm.pCur->pModule = u.cm.pVtabCursor->pVtab->pModule;
 69834     }else{
 69835       db->mallocFailed = 1;
 69836       u.cm.pModule->xClose(u.cm.pVtabCursor);
 69839   break;
 69841 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69843 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69844 /* Opcode: VFilter P1 P2 P3 P4 *
 69845 **
 69846 ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
 69847 ** the filtered result set is empty.
 69848 **
 69849 ** P4 is either NULL or a string that was generated by the xBestIndex
 69850 ** method of the module.  The interpretation of the P4 string is left
 69851 ** to the module implementation.
 69852 **
 69853 ** This opcode invokes the xFilter method on the virtual table specified
 69854 ** by P1.  The integer query plan parameter to xFilter is stored in register
 69855 ** P3. Register P3+1 stores the argc parameter to be passed to the
 69856 ** xFilter method. Registers P3+2..P3+1+argc are the argc
 69857 ** additional parameters which are passed to
 69858 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
 69859 **
 69860 ** A jump is made to P2 if the result set after filtering would be empty.
 69861 */
 69862 case OP_VFilter: {   /* jump */
 69863 #if 0  /* local variables moved into u.cn */
 69864   int nArg;
 69865   int iQuery;
 69866   const sqlite3_module *pModule;
 69867   Mem *pQuery;
 69868   Mem *pArgc;
 69869   sqlite3_vtab_cursor *pVtabCursor;
 69870   sqlite3_vtab *pVtab;
 69871   VdbeCursor *pCur;
 69872   int res;
 69873   int i;
 69874   Mem **apArg;
 69875 #endif /* local variables moved into u.cn */
 69877   u.cn.pQuery = &aMem[pOp->p3];
 69878   u.cn.pArgc = &u.cn.pQuery[1];
 69879   u.cn.pCur = p->apCsr[pOp->p1];
 69880   assert( memIsValid(u.cn.pQuery) );
 69881   REGISTER_TRACE(pOp->p3, u.cn.pQuery);
 69882   assert( u.cn.pCur->pVtabCursor );
 69883   u.cn.pVtabCursor = u.cn.pCur->pVtabCursor;
 69884   u.cn.pVtab = u.cn.pVtabCursor->pVtab;
 69885   u.cn.pModule = u.cn.pVtab->pModule;
 69887   /* Grab the index number and argc parameters */
 69888   assert( (u.cn.pQuery->flags&MEM_Int)!=0 && u.cn.pArgc->flags==MEM_Int );
 69889   u.cn.nArg = (int)u.cn.pArgc->u.i;
 69890   u.cn.iQuery = (int)u.cn.pQuery->u.i;
 69892   /* Invoke the xFilter method */
 69894     u.cn.res = 0;
 69895     u.cn.apArg = p->apArg;
 69896     for(u.cn.i = 0; u.cn.i<u.cn.nArg; u.cn.i++){
 69897       u.cn.apArg[u.cn.i] = &u.cn.pArgc[u.cn.i+1];
 69898       sqlite3VdbeMemStoreType(u.cn.apArg[u.cn.i]);
 69901     p->inVtabMethod = 1;
 69902     rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
 69903     p->inVtabMethod = 0;
 69904     importVtabErrMsg(p, u.cn.pVtab);
 69905     if( rc==SQLITE_OK ){
 69906       u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
 69909     if( u.cn.res ){
 69910       pc = pOp->p2 - 1;
 69913   u.cn.pCur->nullRow = 0;
 69915   break;
 69917 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69919 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69920 /* Opcode: VColumn P1 P2 P3 * *
 69921 **
 69922 ** Store the value of the P2-th column of
 69923 ** the row of the virtual-table that the 
 69924 ** P1 cursor is pointing to into register P3.
 69925 */
 69926 case OP_VColumn: {
 69927 #if 0  /* local variables moved into u.co */
 69928   sqlite3_vtab *pVtab;
 69929   const sqlite3_module *pModule;
 69930   Mem *pDest;
 69931   sqlite3_context sContext;
 69932 #endif /* local variables moved into u.co */
 69934   VdbeCursor *pCur = p->apCsr[pOp->p1];
 69935   assert( pCur->pVtabCursor );
 69936   assert( pOp->p3>0 && pOp->p3<=p->nMem );
 69937   u.co.pDest = &aMem[pOp->p3];
 69938   memAboutToChange(p, u.co.pDest);
 69939   if( pCur->nullRow ){
 69940     sqlite3VdbeMemSetNull(u.co.pDest);
 69941     break;
 69943   u.co.pVtab = pCur->pVtabCursor->pVtab;
 69944   u.co.pModule = u.co.pVtab->pModule;
 69945   assert( u.co.pModule->xColumn );
 69946   memset(&u.co.sContext, 0, sizeof(u.co.sContext));
 69948   /* The output cell may already have a buffer allocated. Move
 69949   ** the current contents to u.co.sContext.s so in case the user-function
 69950   ** can use the already allocated buffer instead of allocating a
 69951   ** new one.
 69952   */
 69953   sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
 69954   MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
 69956   rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
 69957   importVtabErrMsg(p, u.co.pVtab);
 69958   if( u.co.sContext.isError ){
 69959     rc = u.co.sContext.isError;
 69962   /* Copy the result of the function to the P3 register. We
 69963   ** do this regardless of whether or not an error occurred to ensure any
 69964   ** dynamic allocation in u.co.sContext.s (a Mem struct) is  released.
 69965   */
 69966   sqlite3VdbeChangeEncoding(&u.co.sContext.s, encoding);
 69967   sqlite3VdbeMemMove(u.co.pDest, &u.co.sContext.s);
 69968   REGISTER_TRACE(pOp->p3, u.co.pDest);
 69969   UPDATE_MAX_BLOBSIZE(u.co.pDest);
 69971   if( sqlite3VdbeMemTooBig(u.co.pDest) ){
 69972     goto too_big;
 69974   break;
 69976 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69978 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69979 /* Opcode: VNext P1 P2 * * *
 69980 **
 69981 ** Advance virtual table P1 to the next row in its result set and
 69982 ** jump to instruction P2.  Or, if the virtual table has reached
 69983 ** the end of its result set, then fall through to the next instruction.
 69984 */
 69985 case OP_VNext: {   /* jump */
 69986 #if 0  /* local variables moved into u.cp */
 69987   sqlite3_vtab *pVtab;
 69988   const sqlite3_module *pModule;
 69989   int res;
 69990   VdbeCursor *pCur;
 69991 #endif /* local variables moved into u.cp */
 69993   u.cp.res = 0;
 69994   u.cp.pCur = p->apCsr[pOp->p1];
 69995   assert( u.cp.pCur->pVtabCursor );
 69996   if( u.cp.pCur->nullRow ){
 69997     break;
 69999   u.cp.pVtab = u.cp.pCur->pVtabCursor->pVtab;
 70000   u.cp.pModule = u.cp.pVtab->pModule;
 70001   assert( u.cp.pModule->xNext );
 70003   /* Invoke the xNext() method of the module. There is no way for the
 70004   ** underlying implementation to return an error if one occurs during
 70005   ** xNext(). Instead, if an error occurs, true is returned (indicating that
 70006   ** data is available) and the error code returned when xColumn or
 70007   ** some other method is next invoked on the save virtual table cursor.
 70008   */
 70009   p->inVtabMethod = 1;
 70010   rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
 70011   p->inVtabMethod = 0;
 70012   importVtabErrMsg(p, u.cp.pVtab);
 70013   if( rc==SQLITE_OK ){
 70014     u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
 70017   if( !u.cp.res ){
 70018     /* If there is data, jump to P2 */
 70019     pc = pOp->p2 - 1;
 70021   break;
 70023 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 70025 #ifndef SQLITE_OMIT_VIRTUALTABLE
 70026 /* Opcode: VRename P1 * * P4 *
 70027 **
 70028 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 70029 ** This opcode invokes the corresponding xRename method. The value
 70030 ** in register P1 is passed as the zName argument to the xRename method.
 70031 */
 70032 case OP_VRename: {
 70033 #if 0  /* local variables moved into u.cq */
 70034   sqlite3_vtab *pVtab;
 70035   Mem *pName;
 70036 #endif /* local variables moved into u.cq */
 70038   u.cq.pVtab = pOp->p4.pVtab->pVtab;
 70039   u.cq.pName = &aMem[pOp->p1];
 70040   assert( u.cq.pVtab->pModule->xRename );
 70041   assert( memIsValid(u.cq.pName) );
 70042   REGISTER_TRACE(pOp->p1, u.cq.pName);
 70043   assert( u.cq.pName->flags & MEM_Str );
 70044   testcase( u.cq.pName->enc==SQLITE_UTF8 );
 70045   testcase( u.cq.pName->enc==SQLITE_UTF16BE );
 70046   testcase( u.cq.pName->enc==SQLITE_UTF16LE );
 70047   rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
 70048   if( rc==SQLITE_OK ){
 70049     rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
 70050     importVtabErrMsg(p, u.cq.pVtab);
 70051     p->expired = 0;
 70053   break;
 70055 #endif
 70057 #ifndef SQLITE_OMIT_VIRTUALTABLE
 70058 /* Opcode: VUpdate P1 P2 P3 P4 *
 70059 **
 70060 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 70061 ** This opcode invokes the corresponding xUpdate method. P2 values
 70062 ** are contiguous memory cells starting at P3 to pass to the xUpdate 
 70063 ** invocation. The value in register (P3+P2-1) corresponds to the 
 70064 ** p2th element of the argv array passed to xUpdate.
 70065 **
 70066 ** The xUpdate method will do a DELETE or an INSERT or both.
 70067 ** The argv[0] element (which corresponds to memory cell P3)
 70068 ** is the rowid of a row to delete.  If argv[0] is NULL then no 
 70069 ** deletion occurs.  The argv[1] element is the rowid of the new 
 70070 ** row.  This can be NULL to have the virtual table select the new 
 70071 ** rowid for itself.  The subsequent elements in the array are 
 70072 ** the values of columns in the new row.
 70073 **
 70074 ** If P2==1 then no insert is performed.  argv[0] is the rowid of
 70075 ** a row to delete.
 70076 **
 70077 ** P1 is a boolean flag. If it is set to true and the xUpdate call
 70078 ** is successful, then the value returned by sqlite3_last_insert_rowid() 
 70079 ** is set to the value of the rowid for the row just inserted.
 70080 */
 70081 case OP_VUpdate: {
 70082 #if 0  /* local variables moved into u.cr */
 70083   sqlite3_vtab *pVtab;
 70084   sqlite3_module *pModule;
 70085   int nArg;
 70086   int i;
 70087   sqlite_int64 rowid;
 70088   Mem **apArg;
 70089   Mem *pX;
 70090 #endif /* local variables moved into u.cr */
 70092   assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
 70093        || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
 70094   );
 70095   u.cr.pVtab = pOp->p4.pVtab->pVtab;
 70096   u.cr.pModule = (sqlite3_module *)u.cr.pVtab->pModule;
 70097   u.cr.nArg = pOp->p2;
 70098   assert( pOp->p4type==P4_VTAB );
 70099   if( ALWAYS(u.cr.pModule->xUpdate) ){
 70100     u8 vtabOnConflict = db->vtabOnConflict;
 70101     u.cr.apArg = p->apArg;
 70102     u.cr.pX = &aMem[pOp->p3];
 70103     for(u.cr.i=0; u.cr.i<u.cr.nArg; u.cr.i++){
 70104       assert( memIsValid(u.cr.pX) );
 70105       memAboutToChange(p, u.cr.pX);
 70106       sqlite3VdbeMemStoreType(u.cr.pX);
 70107       u.cr.apArg[u.cr.i] = u.cr.pX;
 70108       u.cr.pX++;
 70110     db->vtabOnConflict = pOp->p5;
 70111     rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
 70112     db->vtabOnConflict = vtabOnConflict;
 70113     importVtabErrMsg(p, u.cr.pVtab);
 70114     if( rc==SQLITE_OK && pOp->p1 ){
 70115       assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
 70116       db->lastRowid = lastRowid = u.cr.rowid;
 70118     if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
 70119       if( pOp->p5==OE_Ignore ){
 70120         rc = SQLITE_OK;
 70121       }else{
 70122         p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
 70124     }else{
 70125       p->nChange++;
 70128   break;
 70130 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 70132 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
 70133 /* Opcode: Pagecount P1 P2 * * *
 70134 **
 70135 ** Write the current number of pages in database P1 to memory cell P2.
 70136 */
 70137 case OP_Pagecount: {            /* out2-prerelease */
 70138   pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
 70139   break;
 70141 #endif
 70144 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
 70145 /* Opcode: MaxPgcnt P1 P2 P3 * *
 70146 **
 70147 ** Try to set the maximum page count for database P1 to the value in P3.
 70148 ** Do not let the maximum page count fall below the current page count and
 70149 ** do not change the maximum page count value if P3==0.
 70150 **
 70151 ** Store the maximum page count after the change in register P2.
 70152 */
 70153 case OP_MaxPgcnt: {            /* out2-prerelease */
 70154   unsigned int newMax;
 70155   Btree *pBt;
 70157   pBt = db->aDb[pOp->p1].pBt;
 70158   newMax = 0;
 70159   if( pOp->p3 ){
 70160     newMax = sqlite3BtreeLastPage(pBt);
 70161     if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
 70163   pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
 70164   break;
 70166 #endif
 70169 #ifndef SQLITE_OMIT_TRACE
 70170 /* Opcode: Trace * * * P4 *
 70171 **
 70172 ** If tracing is enabled (by the sqlite3_trace()) interface, then
 70173 ** the UTF-8 string contained in P4 is emitted on the trace callback.
 70174 */
 70175 case OP_Trace: {
 70176 #if 0  /* local variables moved into u.cs */
 70177   char *zTrace;
 70178   char *z;
 70179 #endif /* local variables moved into u.cs */
 70181   if( db->xTrace
 70182    && !p->doingRerun
 70183    && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 70184   ){
 70185     u.cs.z = sqlite3VdbeExpandSql(p, u.cs.zTrace);
 70186     db->xTrace(db->pTraceArg, u.cs.z);
 70187     sqlite3DbFree(db, u.cs.z);
 70189 #ifdef SQLITE_DEBUG
 70190   if( (db->flags & SQLITE_SqlTrace)!=0
 70191    && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 70192   ){
 70193     sqlite3DebugPrintf("SQL-trace: %s\n", u.cs.zTrace);
 70195 #endif /* SQLITE_DEBUG */
 70196   break;
 70198 #endif
 70201 /* Opcode: Noop * * * * *
 70202 **
 70203 ** Do nothing.  This instruction is often useful as a jump
 70204 ** destination.
 70205 */
 70206 /*
 70207 ** The magic Explain opcode are only inserted when explain==2 (which
 70208 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
 70209 ** This opcode records information from the optimizer.  It is the
 70210 ** the same as a no-op.  This opcodesnever appears in a real VM program.
 70211 */
 70212 default: {          /* This is really OP_Noop and OP_Explain */
 70213   assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
 70214   break;
 70217 /*****************************************************************************
 70218 ** The cases of the switch statement above this line should all be indented
 70219 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
 70220 ** readability.  From this point on down, the normal indentation rules are
 70221 ** restored.
 70222 *****************************************************************************/
 70225 #ifdef VDBE_PROFILE
 70227       u64 elapsed = sqlite3Hwtime() - start;
 70228       pOp->cycles += elapsed;
 70229       pOp->cnt++;
 70230 #if 0
 70231         fprintf(stdout, "%10llu ", elapsed);
 70232         sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
 70233 #endif
 70235 #endif
 70237     /* The following code adds nothing to the actual functionality
 70238     ** of the program.  It is only here for testing and debugging.
 70239     ** On the other hand, it does burn CPU cycles every time through
 70240     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
 70241     */
 70242 #ifndef NDEBUG
 70243     assert( pc>=-1 && pc<p->nOp );
 70245 #ifdef SQLITE_DEBUG
 70246     if( p->trace ){
 70247       if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
 70248       if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
 70249         registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
 70251       if( pOp->opflags & OPFLG_OUT3 ){
 70252         registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
 70255 #endif  /* SQLITE_DEBUG */
 70256 #endif  /* NDEBUG */
 70257   }  /* The end of the for(;;) loop the loops through opcodes */
 70259   /* If we reach this point, it means that execution is finished with
 70260   ** an error of some kind.
 70261   */
 70262 vdbe_error_halt:
 70263   assert( rc );
 70264   p->rc = rc;
 70265   testcase( sqlite3GlobalConfig.xLog!=0 );
 70266   sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
 70267                    pc, p->zSql, p->zErrMsg);
 70268   sqlite3VdbeHalt(p);
 70269   if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
 70270   rc = SQLITE_ERROR;
 70271   if( resetSchemaOnFault>0 ){
 70272     sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 70275   /* This is the only way out of this procedure.  We have to
 70276   ** release the mutexes on btrees that were acquired at the
 70277   ** top. */
 70278 vdbe_return:
 70279   db->lastRowid = lastRowid;
 70280   sqlite3VdbeLeave(p);
 70281   return rc;
 70283   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
 70284   ** is encountered.
 70285   */
 70286 too_big:
 70287   sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
 70288   rc = SQLITE_TOOBIG;
 70289   goto vdbe_error_halt;
 70291   /* Jump to here if a malloc() fails.
 70292   */
 70293 no_mem:
 70294   db->mallocFailed = 1;
 70295   sqlite3SetString(&p->zErrMsg, db, "out of memory");
 70296   rc = SQLITE_NOMEM;
 70297   goto vdbe_error_halt;
 70299   /* Jump to here for any other kind of fatal error.  The "rc" variable
 70300   ** should hold the error number.
 70301   */
 70302 abort_due_to_error:
 70303   assert( p->zErrMsg==0 );
 70304   if( db->mallocFailed ) rc = SQLITE_NOMEM;
 70305   if( rc!=SQLITE_IOERR_NOMEM ){
 70306     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
 70308   goto vdbe_error_halt;
 70310   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
 70311   ** flag.
 70312   */
 70313 abort_due_to_interrupt:
 70314   assert( db->u1.isInterrupted );
 70315   rc = SQLITE_INTERRUPT;
 70316   p->rc = rc;
 70317   sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
 70318   goto vdbe_error_halt;
 70321 /************** End of vdbe.c ************************************************/
 70322 /************** Begin file vdbeblob.c ****************************************/
 70323 /*
 70324 ** 2007 May 1
 70325 **
 70326 ** The author disclaims copyright to this source code.  In place of
 70327 ** a legal notice, here is a blessing:
 70328 **
 70329 **    May you do good and not evil.
 70330 **    May you find forgiveness for yourself and forgive others.
 70331 **    May you share freely, never taking more than you give.
 70332 **
 70333 *************************************************************************
 70334 **
 70335 ** This file contains code used to implement incremental BLOB I/O.
 70336 */
 70339 #ifndef SQLITE_OMIT_INCRBLOB
 70341 /*
 70342 ** Valid sqlite3_blob* handles point to Incrblob structures.
 70343 */
 70344 typedef struct Incrblob Incrblob;
 70345 struct Incrblob {
 70346   int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
 70347   int nByte;              /* Size of open blob, in bytes */
 70348   int iOffset;            /* Byte offset of blob in cursor data */
 70349   int iCol;               /* Table column this handle is open on */
 70350   BtCursor *pCsr;         /* Cursor pointing at blob row */
 70351   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
 70352   sqlite3 *db;            /* The associated database */
 70353 };
 70356 /*
 70357 ** This function is used by both blob_open() and blob_reopen(). It seeks
 70358 ** the b-tree cursor associated with blob handle p to point to row iRow.
 70359 ** If successful, SQLITE_OK is returned and subsequent calls to
 70360 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
 70361 **
 70362 ** If an error occurs, or if the specified row does not exist or does not
 70363 ** contain a value of type TEXT or BLOB in the column nominated when the
 70364 ** blob handle was opened, then an error code is returned and *pzErr may
 70365 ** be set to point to a buffer containing an error message. It is the
 70366 ** responsibility of the caller to free the error message buffer using
 70367 ** sqlite3DbFree().
 70368 **
 70369 ** If an error does occur, then the b-tree cursor is closed. All subsequent
 70370 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will 
 70371 ** immediately return SQLITE_ABORT.
 70372 */
 70373 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
 70374   int rc;                         /* Error code */
 70375   char *zErr = 0;                 /* Error message */
 70376   Vdbe *v = (Vdbe *)p->pStmt;
 70378   /* Set the value of the SQL statements only variable to integer iRow. 
 70379   ** This is done directly instead of using sqlite3_bind_int64() to avoid 
 70380   ** triggering asserts related to mutexes.
 70381   */
 70382   assert( v->aVar[0].flags&MEM_Int );
 70383   v->aVar[0].u.i = iRow;
 70385   rc = sqlite3_step(p->pStmt);
 70386   if( rc==SQLITE_ROW ){
 70387     u32 type = v->apCsr[0]->aType[p->iCol];
 70388     if( type<12 ){
 70389       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
 70390           type==0?"null": type==7?"real": "integer"
 70391       );
 70392       rc = SQLITE_ERROR;
 70393       sqlite3_finalize(p->pStmt);
 70394       p->pStmt = 0;
 70395     }else{
 70396       p->iOffset = v->apCsr[0]->aOffset[p->iCol];
 70397       p->nByte = sqlite3VdbeSerialTypeLen(type);
 70398       p->pCsr =  v->apCsr[0]->pCursor;
 70399       sqlite3BtreeEnterCursor(p->pCsr);
 70400       sqlite3BtreeCacheOverflow(p->pCsr);
 70401       sqlite3BtreeLeaveCursor(p->pCsr);
 70405   if( rc==SQLITE_ROW ){
 70406     rc = SQLITE_OK;
 70407   }else if( p->pStmt ){
 70408     rc = sqlite3_finalize(p->pStmt);
 70409     p->pStmt = 0;
 70410     if( rc==SQLITE_OK ){
 70411       zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
 70412       rc = SQLITE_ERROR;
 70413     }else{
 70414       zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
 70418   assert( rc!=SQLITE_OK || zErr==0 );
 70419   assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
 70421   *pzErr = zErr;
 70422   return rc;
 70425 /*
 70426 ** Open a blob handle.
 70427 */
 70428 SQLITE_API int sqlite3_blob_open(
 70429   sqlite3* db,            /* The database connection */
 70430   const char *zDb,        /* The attached database containing the blob */
 70431   const char *zTable,     /* The table containing the blob */
 70432   const char *zColumn,    /* The column containing the blob */
 70433   sqlite_int64 iRow,      /* The row containing the glob */
 70434   int flags,              /* True -> read/write access, false -> read-only */
 70435   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
 70436 ){
 70437   int nAttempt = 0;
 70438   int iCol;               /* Index of zColumn in row-record */
 70440   /* This VDBE program seeks a btree cursor to the identified 
 70441   ** db/table/row entry. The reason for using a vdbe program instead
 70442   ** of writing code to use the b-tree layer directly is that the
 70443   ** vdbe program will take advantage of the various transaction,
 70444   ** locking and error handling infrastructure built into the vdbe.
 70445   **
 70446   ** After seeking the cursor, the vdbe executes an OP_ResultRow.
 70447   ** Code external to the Vdbe then "borrows" the b-tree cursor and
 70448   ** uses it to implement the blob_read(), blob_write() and 
 70449   ** blob_bytes() functions.
 70450   **
 70451   ** The sqlite3_blob_close() function finalizes the vdbe program,
 70452   ** which closes the b-tree cursor and (possibly) commits the 
 70453   ** transaction.
 70454   */
 70455   static const VdbeOpList openBlob[] = {
 70456     {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
 70457     {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
 70458     {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
 70460     /* One of the following two instructions is replaced by an OP_Noop. */
 70461     {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
 70462     {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
 70464     {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
 70465     {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
 70466     {OP_Column, 0, 0, 1},          /* 7  */
 70467     {OP_ResultRow, 1, 0, 0},       /* 8  */
 70468     {OP_Goto, 0, 5, 0},            /* 9  */
 70469     {OP_Close, 0, 0, 0},           /* 10 */
 70470     {OP_Halt, 0, 0, 0},            /* 11 */
 70471   };
 70473   int rc = SQLITE_OK;
 70474   char *zErr = 0;
 70475   Table *pTab;
 70476   Parse *pParse = 0;
 70477   Incrblob *pBlob = 0;
 70479   flags = !!flags;                /* flags = (flags ? 1 : 0); */
 70480   *ppBlob = 0;
 70482   sqlite3_mutex_enter(db->mutex);
 70484   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
 70485   if( !pBlob ) goto blob_open_out;
 70486   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
 70487   if( !pParse ) goto blob_open_out;
 70489   do {
 70490     memset(pParse, 0, sizeof(Parse));
 70491     pParse->db = db;
 70492     sqlite3DbFree(db, zErr);
 70493     zErr = 0;
 70495     sqlite3BtreeEnterAll(db);
 70496     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
 70497     if( pTab && IsVirtual(pTab) ){
 70498       pTab = 0;
 70499       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
 70501 #ifndef SQLITE_OMIT_VIEW
 70502     if( pTab && pTab->pSelect ){
 70503       pTab = 0;
 70504       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
 70506 #endif
 70507     if( !pTab ){
 70508       if( pParse->zErrMsg ){
 70509         sqlite3DbFree(db, zErr);
 70510         zErr = pParse->zErrMsg;
 70511         pParse->zErrMsg = 0;
 70513       rc = SQLITE_ERROR;
 70514       sqlite3BtreeLeaveAll(db);
 70515       goto blob_open_out;
 70518     /* Now search pTab for the exact column. */
 70519     for(iCol=0; iCol<pTab->nCol; iCol++) {
 70520       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
 70521         break;
 70524     if( iCol==pTab->nCol ){
 70525       sqlite3DbFree(db, zErr);
 70526       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
 70527       rc = SQLITE_ERROR;
 70528       sqlite3BtreeLeaveAll(db);
 70529       goto blob_open_out;
 70532     /* If the value is being opened for writing, check that the
 70533     ** column is not indexed, and that it is not part of a foreign key. 
 70534     ** It is against the rules to open a column to which either of these
 70535     ** descriptions applies for writing.  */
 70536     if( flags ){
 70537       const char *zFault = 0;
 70538       Index *pIdx;
 70539 #ifndef SQLITE_OMIT_FOREIGN_KEY
 70540       if( db->flags&SQLITE_ForeignKeys ){
 70541         /* Check that the column is not part of an FK child key definition. It
 70542         ** is not necessary to check if it is part of a parent key, as parent
 70543         ** key columns must be indexed. The check below will pick up this 
 70544         ** case.  */
 70545         FKey *pFKey;
 70546         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
 70547           int j;
 70548           for(j=0; j<pFKey->nCol; j++){
 70549             if( pFKey->aCol[j].iFrom==iCol ){
 70550               zFault = "foreign key";
 70555 #endif
 70556       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 70557         int j;
 70558         for(j=0; j<pIdx->nColumn; j++){
 70559           if( pIdx->aiColumn[j]==iCol ){
 70560             zFault = "indexed";
 70564       if( zFault ){
 70565         sqlite3DbFree(db, zErr);
 70566         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
 70567         rc = SQLITE_ERROR;
 70568         sqlite3BtreeLeaveAll(db);
 70569         goto blob_open_out;
 70573     pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
 70574     assert( pBlob->pStmt || db->mallocFailed );
 70575     if( pBlob->pStmt ){
 70576       Vdbe *v = (Vdbe *)pBlob->pStmt;
 70577       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 70579       sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
 70582       /* Configure the OP_Transaction */
 70583       sqlite3VdbeChangeP1(v, 0, iDb);
 70584       sqlite3VdbeChangeP2(v, 0, flags);
 70586       /* Configure the OP_VerifyCookie */
 70587       sqlite3VdbeChangeP1(v, 1, iDb);
 70588       sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
 70589       sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
 70591       /* Make sure a mutex is held on the table to be accessed */
 70592       sqlite3VdbeUsesBtree(v, iDb); 
 70594       /* Configure the OP_TableLock instruction */
 70595 #ifdef SQLITE_OMIT_SHARED_CACHE
 70596       sqlite3VdbeChangeToNoop(v, 2);
 70597 #else
 70598       sqlite3VdbeChangeP1(v, 2, iDb);
 70599       sqlite3VdbeChangeP2(v, 2, pTab->tnum);
 70600       sqlite3VdbeChangeP3(v, 2, flags);
 70601       sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
 70602 #endif
 70604       /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 70605       ** parameter of the other to pTab->tnum.  */
 70606       sqlite3VdbeChangeToNoop(v, 4 - flags);
 70607       sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
 70608       sqlite3VdbeChangeP3(v, 3 + flags, iDb);
 70610       /* Configure the number of columns. Configure the cursor to
 70611       ** think that the table has one more column than it really
 70612       ** does. An OP_Column to retrieve this imaginary column will
 70613       ** always return an SQL NULL. This is useful because it means
 70614       ** we can invoke OP_Column to fill in the vdbe cursors type 
 70615       ** and offset cache without causing any IO.
 70616       */
 70617       sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
 70618       sqlite3VdbeChangeP2(v, 7, pTab->nCol);
 70619       if( !db->mallocFailed ){
 70620         pParse->nVar = 1;
 70621         pParse->nMem = 1;
 70622         pParse->nTab = 1;
 70623         sqlite3VdbeMakeReady(v, pParse);
 70627     pBlob->flags = flags;
 70628     pBlob->iCol = iCol;
 70629     pBlob->db = db;
 70630     sqlite3BtreeLeaveAll(db);
 70631     if( db->mallocFailed ){
 70632       goto blob_open_out;
 70634     sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
 70635     rc = blobSeekToRow(pBlob, iRow, &zErr);
 70636   } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
 70638 blob_open_out:
 70639   if( rc==SQLITE_OK && db->mallocFailed==0 ){
 70640     *ppBlob = (sqlite3_blob *)pBlob;
 70641   }else{
 70642     if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
 70643     sqlite3DbFree(db, pBlob);
 70645   sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
 70646   sqlite3DbFree(db, zErr);
 70647   sqlite3StackFree(db, pParse);
 70648   rc = sqlite3ApiExit(db, rc);
 70649   sqlite3_mutex_leave(db->mutex);
 70650   return rc;
 70653 /*
 70654 ** Close a blob handle that was previously created using
 70655 ** sqlite3_blob_open().
 70656 */
 70657 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
 70658   Incrblob *p = (Incrblob *)pBlob;
 70659   int rc;
 70660   sqlite3 *db;
 70662   if( p ){
 70663     db = p->db;
 70664     sqlite3_mutex_enter(db->mutex);
 70665     rc = sqlite3_finalize(p->pStmt);
 70666     sqlite3DbFree(db, p);
 70667     sqlite3_mutex_leave(db->mutex);
 70668   }else{
 70669     rc = SQLITE_OK;
 70671   return rc;
 70674 /*
 70675 ** Perform a read or write operation on a blob
 70676 */
 70677 static int blobReadWrite(
 70678   sqlite3_blob *pBlob, 
 70679   void *z, 
 70680   int n, 
 70681   int iOffset, 
 70682   int (*xCall)(BtCursor*, u32, u32, void*)
 70683 ){
 70684   int rc;
 70685   Incrblob *p = (Incrblob *)pBlob;
 70686   Vdbe *v;
 70687   sqlite3 *db;
 70689   if( p==0 ) return SQLITE_MISUSE_BKPT;
 70690   db = p->db;
 70691   sqlite3_mutex_enter(db->mutex);
 70692   v = (Vdbe*)p->pStmt;
 70694   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
 70695     /* Request is out of range. Return a transient error. */
 70696     rc = SQLITE_ERROR;
 70697     sqlite3Error(db, SQLITE_ERROR, 0);
 70698   }else if( v==0 ){
 70699     /* If there is no statement handle, then the blob-handle has
 70700     ** already been invalidated. Return SQLITE_ABORT in this case.
 70701     */
 70702     rc = SQLITE_ABORT;
 70703   }else{
 70704     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
 70705     ** returned, clean-up the statement handle.
 70706     */
 70707     assert( db == v->db );
 70708     sqlite3BtreeEnterCursor(p->pCsr);
 70709     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
 70710     sqlite3BtreeLeaveCursor(p->pCsr);
 70711     if( rc==SQLITE_ABORT ){
 70712       sqlite3VdbeFinalize(v);
 70713       p->pStmt = 0;
 70714     }else{
 70715       db->errCode = rc;
 70716       v->rc = rc;
 70719   rc = sqlite3ApiExit(db, rc);
 70720   sqlite3_mutex_leave(db->mutex);
 70721   return rc;
 70724 /*
 70725 ** Read data from a blob handle.
 70726 */
 70727 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
 70728   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
 70731 /*
 70732 ** Write data to a blob handle.
 70733 */
 70734 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
 70735   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
 70738 /*
 70739 ** Query a blob handle for the size of the data.
 70740 **
 70741 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
 70742 ** so no mutex is required for access.
 70743 */
 70744 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
 70745   Incrblob *p = (Incrblob *)pBlob;
 70746   return (p && p->pStmt) ? p->nByte : 0;
 70749 /*
 70750 ** Move an existing blob handle to point to a different row of the same
 70751 ** database table.
 70752 **
 70753 ** If an error occurs, or if the specified row does not exist or does not
 70754 ** contain a blob or text value, then an error code is returned and the
 70755 ** database handle error code and message set. If this happens, then all 
 70756 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) 
 70757 ** immediately return SQLITE_ABORT.
 70758 */
 70759 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
 70760   int rc;
 70761   Incrblob *p = (Incrblob *)pBlob;
 70762   sqlite3 *db;
 70764   if( p==0 ) return SQLITE_MISUSE_BKPT;
 70765   db = p->db;
 70766   sqlite3_mutex_enter(db->mutex);
 70768   if( p->pStmt==0 ){
 70769     /* If there is no statement handle, then the blob-handle has
 70770     ** already been invalidated. Return SQLITE_ABORT in this case.
 70771     */
 70772     rc = SQLITE_ABORT;
 70773   }else{
 70774     char *zErr;
 70775     rc = blobSeekToRow(p, iRow, &zErr);
 70776     if( rc!=SQLITE_OK ){
 70777       sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
 70778       sqlite3DbFree(db, zErr);
 70780     assert( rc!=SQLITE_SCHEMA );
 70783   rc = sqlite3ApiExit(db, rc);
 70784   assert( rc==SQLITE_OK || p->pStmt==0 );
 70785   sqlite3_mutex_leave(db->mutex);
 70786   return rc;
 70789 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
 70791 /************** End of vdbeblob.c ********************************************/
 70792 /************** Begin file vdbesort.c ****************************************/
 70793 /*
 70794 ** 2011 July 9
 70795 **
 70796 ** The author disclaims copyright to this source code.  In place of
 70797 ** a legal notice, here is a blessing:
 70798 **
 70799 **    May you do good and not evil.
 70800 **    May you find forgiveness for yourself and forgive others.
 70801 **    May you share freely, never taking more than you give.
 70802 **
 70803 *************************************************************************
 70804 ** This file contains code for the VdbeSorter object, used in concert with
 70805 ** a VdbeCursor to sort large numbers of keys (as may be required, for
 70806 ** example, by CREATE INDEX statements on tables too large to fit in main
 70807 ** memory).
 70808 */
 70811 #ifndef SQLITE_OMIT_MERGE_SORT
 70813 typedef struct VdbeSorterIter VdbeSorterIter;
 70814 typedef struct SorterRecord SorterRecord;
 70815 typedef struct FileWriter FileWriter;
 70817 /*
 70818 ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
 70819 **
 70820 ** As keys are added to the sorter, they are written to disk in a series
 70821 ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
 70822 ** the same as the cache-size allowed for temporary databases. In order
 70823 ** to allow the caller to extract keys from the sorter in sorted order,
 70824 ** all PMAs currently stored on disk must be merged together. This comment
 70825 ** describes the data structure used to do so. The structure supports 
 70826 ** merging any number of arrays in a single pass with no redundant comparison 
 70827 ** operations.
 70828 **
 70829 ** The aIter[] array contains an iterator for each of the PMAs being merged.
 70830 ** An aIter[] iterator either points to a valid key or else is at EOF. For 
 70831 ** the purposes of the paragraphs below, we assume that the array is actually 
 70832 ** N elements in size, where N is the smallest power of 2 greater to or equal 
 70833 ** to the number of iterators being merged. The extra aIter[] elements are 
 70834 ** treated as if they are empty (always at EOF).
 70835 **
 70836 ** The aTree[] array is also N elements in size. The value of N is stored in
 70837 ** the VdbeSorter.nTree variable.
 70838 **
 70839 ** The final (N/2) elements of aTree[] contain the results of comparing
 70840 ** pairs of iterator keys together. Element i contains the result of 
 70841 ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
 70842 ** aTree element is set to the index of it. 
 70843 **
 70844 ** For the purposes of this comparison, EOF is considered greater than any
 70845 ** other key value. If the keys are equal (only possible with two EOF
 70846 ** values), it doesn't matter which index is stored.
 70847 **
 70848 ** The (N/4) elements of aTree[] that preceed the final (N/2) described 
 70849 ** above contains the index of the smallest of each block of 4 iterators.
 70850 ** And so on. So that aTree[1] contains the index of the iterator that 
 70851 ** currently points to the smallest key value. aTree[0] is unused.
 70852 **
 70853 ** Example:
 70854 **
 70855 **     aIter[0] -> Banana
 70856 **     aIter[1] -> Feijoa
 70857 **     aIter[2] -> Elderberry
 70858 **     aIter[3] -> Currant
 70859 **     aIter[4] -> Grapefruit
 70860 **     aIter[5] -> Apple
 70861 **     aIter[6] -> Durian
 70862 **     aIter[7] -> EOF
 70863 **
 70864 **     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
 70865 **
 70866 ** The current element is "Apple" (the value of the key indicated by 
 70867 ** iterator 5). When the Next() operation is invoked, iterator 5 will
 70868 ** be advanced to the next key in its segment. Say the next key is
 70869 ** "Eggplant":
 70870 **
 70871 **     aIter[5] -> Eggplant
 70872 **
 70873 ** The contents of aTree[] are updated first by comparing the new iterator
 70874 ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
 70875 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
 70876 ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
 70877 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
 70878 ** so the value written into element 1 of the array is 0. As follows:
 70879 **
 70880 **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
 70881 **
 70882 ** In other words, each time we advance to the next sorter element, log2(N)
 70883 ** key comparison operations are required, where N is the number of segments
 70884 ** being merged (rounded up to the next power of 2).
 70885 */
 70886 struct VdbeSorter {
 70887   i64 iWriteOff;                  /* Current write offset within file pTemp1 */
 70888   i64 iReadOff;                   /* Current read offset within file pTemp1 */
 70889   int nInMemory;                  /* Current size of pRecord list as PMA */
 70890   int nTree;                      /* Used size of aTree/aIter (power of 2) */
 70891   int nPMA;                       /* Number of PMAs stored in pTemp1 */
 70892   int mnPmaSize;                  /* Minimum PMA size, in bytes */
 70893   int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
 70894   VdbeSorterIter *aIter;          /* Array of iterators to merge */
 70895   int *aTree;                     /* Current state of incremental merge */
 70896   sqlite3_file *pTemp1;           /* PMA file 1 */
 70897   SorterRecord *pRecord;          /* Head of in-memory record list */
 70898   UnpackedRecord *pUnpacked;      /* Used to unpack keys */
 70899 };
 70901 /*
 70902 ** The following type is an iterator for a PMA. It caches the current key in 
 70903 ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
 70904 */
 70905 struct VdbeSorterIter {
 70906   i64 iReadOff;                   /* Current read offset */
 70907   i64 iEof;                       /* 1 byte past EOF for this iterator */
 70908   int nAlloc;                     /* Bytes of space at aAlloc */
 70909   int nKey;                       /* Number of bytes in key */
 70910   sqlite3_file *pFile;            /* File iterator is reading from */
 70911   u8 *aAlloc;                     /* Allocated space */
 70912   u8 *aKey;                       /* Pointer to current key */
 70913   u8 *aBuffer;                    /* Current read buffer */
 70914   int nBuffer;                    /* Size of read buffer in bytes */
 70915 };
 70917 /*
 70918 ** An instance of this structure is used to organize the stream of records
 70919 ** being written to files by the merge-sort code into aligned, page-sized
 70920 ** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
 70921 ** faster on many operating systems.
 70922 */
 70923 struct FileWriter {
 70924   int eFWErr;                     /* Non-zero if in an error state */
 70925   u8 *aBuffer;                    /* Pointer to write buffer */
 70926   int nBuffer;                    /* Size of write buffer in bytes */
 70927   int iBufStart;                  /* First byte of buffer to write */
 70928   int iBufEnd;                    /* Last byte of buffer to write */
 70929   i64 iWriteOff;                  /* Offset of start of buffer in file */
 70930   sqlite3_file *pFile;            /* File to write to */
 70931 };
 70933 /*
 70934 ** A structure to store a single record. All in-memory records are connected
 70935 ** together into a linked list headed at VdbeSorter.pRecord using the 
 70936 ** SorterRecord.pNext pointer.
 70937 */
 70938 struct SorterRecord {
 70939   void *pVal;
 70940   int nVal;
 70941   SorterRecord *pNext;
 70942 };
 70944 /* Minimum allowable value for the VdbeSorter.nWorking variable */
 70945 #define SORTER_MIN_WORKING 10
 70947 /* Maximum number of segments to merge in a single pass. */
 70948 #define SORTER_MAX_MERGE_COUNT 16
 70950 /*
 70951 ** Free all memory belonging to the VdbeSorterIter object passed as the second
 70952 ** argument. All structure fields are set to zero before returning.
 70953 */
 70954 static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
 70955   sqlite3DbFree(db, pIter->aAlloc);
 70956   sqlite3DbFree(db, pIter->aBuffer);
 70957   memset(pIter, 0, sizeof(VdbeSorterIter));
 70960 /*
 70961 ** Read nByte bytes of data from the stream of data iterated by object p.
 70962 ** If successful, set *ppOut to point to a buffer containing the data
 70963 ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
 70964 ** error code.
 70965 **
 70966 ** The buffer indicated by *ppOut may only be considered valid until the
 70967 ** next call to this function.
 70968 */
 70969 static int vdbeSorterIterRead(
 70970   sqlite3 *db,                    /* Database handle (for malloc) */
 70971   VdbeSorterIter *p,              /* Iterator */
 70972   int nByte,                      /* Bytes of data to read */
 70973   u8 **ppOut                      /* OUT: Pointer to buffer containing data */
 70974 ){
 70975   int iBuf;                       /* Offset within buffer to read from */
 70976   int nAvail;                     /* Bytes of data available in buffer */
 70977   assert( p->aBuffer );
 70979   /* If there is no more data to be read from the buffer, read the next 
 70980   ** p->nBuffer bytes of data from the file into it. Or, if there are less
 70981   ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
 70982   iBuf = p->iReadOff % p->nBuffer;
 70983   if( iBuf==0 ){
 70984     int nRead;                    /* Bytes to read from disk */
 70985     int rc;                       /* sqlite3OsRead() return code */
 70987     /* Determine how many bytes of data to read. */
 70988     if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
 70989       nRead = p->nBuffer;
 70990     }else{
 70991       nRead = (int)(p->iEof - p->iReadOff);
 70993     assert( nRead>0 );
 70995     /* Read data from the file. Return early if an error occurs. */
 70996     rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
 70997     assert( rc!=SQLITE_IOERR_SHORT_READ );
 70998     if( rc!=SQLITE_OK ) return rc;
 71000   nAvail = p->nBuffer - iBuf; 
 71002   if( nByte<=nAvail ){
 71003     /* The requested data is available in the in-memory buffer. In this
 71004     ** case there is no need to make a copy of the data, just return a 
 71005     ** pointer into the buffer to the caller.  */
 71006     *ppOut = &p->aBuffer[iBuf];
 71007     p->iReadOff += nByte;
 71008   }else{
 71009     /* The requested data is not all available in the in-memory buffer.
 71010     ** In this case, allocate space at p->aAlloc[] to copy the requested
 71011     ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
 71012     int nRem;                     /* Bytes remaining to copy */
 71014     /* Extend the p->aAlloc[] allocation if required. */
 71015     if( p->nAlloc<nByte ){
 71016       int nNew = p->nAlloc*2;
 71017       while( nByte>nNew ) nNew = nNew*2;
 71018       p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
 71019       if( !p->aAlloc ) return SQLITE_NOMEM;
 71020       p->nAlloc = nNew;
 71023     /* Copy as much data as is available in the buffer into the start of
 71024     ** p->aAlloc[].  */
 71025     memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
 71026     p->iReadOff += nAvail;
 71027     nRem = nByte - nAvail;
 71029     /* The following loop copies up to p->nBuffer bytes per iteration into
 71030     ** the p->aAlloc[] buffer.  */
 71031     while( nRem>0 ){
 71032       int rc;                     /* vdbeSorterIterRead() return code */
 71033       int nCopy;                  /* Number of bytes to copy */
 71034       u8 *aNext;                  /* Pointer to buffer to copy data from */
 71036       nCopy = nRem;
 71037       if( nRem>p->nBuffer ) nCopy = p->nBuffer;
 71038       rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
 71039       if( rc!=SQLITE_OK ) return rc;
 71040       assert( aNext!=p->aAlloc );
 71041       memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
 71042       nRem -= nCopy;
 71045     *ppOut = p->aAlloc;
 71048   return SQLITE_OK;
 71051 /*
 71052 ** Read a varint from the stream of data accessed by p. Set *pnOut to
 71053 ** the value read.
 71054 */
 71055 static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
 71056   int iBuf;
 71058   iBuf = p->iReadOff % p->nBuffer;
 71059   if( iBuf && (p->nBuffer-iBuf)>=9 ){
 71060     p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
 71061   }else{
 71062     u8 aVarint[16], *a;
 71063     int i = 0, rc;
 71064     do{
 71065       rc = vdbeSorterIterRead(db, p, 1, &a);
 71066       if( rc ) return rc;
 71067       aVarint[(i++)&0xf] = a[0];
 71068     }while( (a[0]&0x80)!=0 );
 71069     sqlite3GetVarint(aVarint, pnOut);
 71072   return SQLITE_OK;
 71076 /*
 71077 ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
 71078 ** no error occurs, or an SQLite error code if one does.
 71079 */
 71080 static int vdbeSorterIterNext(
 71081   sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
 71082   VdbeSorterIter *pIter           /* Iterator to advance */
 71083 ){
 71084   int rc;                         /* Return Code */
 71085   u64 nRec = 0;                   /* Size of record in bytes */
 71087   if( pIter->iReadOff>=pIter->iEof ){
 71088     /* This is an EOF condition */
 71089     vdbeSorterIterZero(db, pIter);
 71090     return SQLITE_OK;
 71093   rc = vdbeSorterIterVarint(db, pIter, &nRec);
 71094   if( rc==SQLITE_OK ){
 71095     pIter->nKey = (int)nRec;
 71096     rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
 71099   return rc;
 71102 /*
 71103 ** Initialize iterator pIter to scan through the PMA stored in file pFile
 71104 ** starting at offset iStart and ending at offset iEof-1. This function 
 71105 ** leaves the iterator pointing to the first key in the PMA (or EOF if the 
 71106 ** PMA is empty).
 71107 */
 71108 static int vdbeSorterIterInit(
 71109   sqlite3 *db,                    /* Database handle */
 71110   const VdbeSorter *pSorter,      /* Sorter object */
 71111   i64 iStart,                     /* Start offset in pFile */
 71112   VdbeSorterIter *pIter,          /* Iterator to populate */
 71113   i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
 71114 ){
 71115   int rc = SQLITE_OK;
 71116   int nBuf;
 71118   nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 71120   assert( pSorter->iWriteOff>iStart );
 71121   assert( pIter->aAlloc==0 );
 71122   assert( pIter->aBuffer==0 );
 71123   pIter->pFile = pSorter->pTemp1;
 71124   pIter->iReadOff = iStart;
 71125   pIter->nAlloc = 128;
 71126   pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
 71127   pIter->nBuffer = nBuf;
 71128   pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 71130   if( !pIter->aBuffer ){
 71131     rc = SQLITE_NOMEM;
 71132   }else{
 71133     int iBuf;
 71135     iBuf = iStart % nBuf;
 71136     if( iBuf ){
 71137       int nRead = nBuf - iBuf;
 71138       if( (iStart + nRead) > pSorter->iWriteOff ){
 71139         nRead = (int)(pSorter->iWriteOff - iStart);
 71141       rc = sqlite3OsRead(
 71142           pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
 71143       );
 71144       assert( rc!=SQLITE_IOERR_SHORT_READ );
 71147     if( rc==SQLITE_OK ){
 71148       u64 nByte;                       /* Size of PMA in bytes */
 71149       pIter->iEof = pSorter->iWriteOff;
 71150       rc = vdbeSorterIterVarint(db, pIter, &nByte);
 71151       pIter->iEof = pIter->iReadOff + nByte;
 71152       *pnByte += nByte;
 71156   if( rc==SQLITE_OK ){
 71157     rc = vdbeSorterIterNext(db, pIter);
 71159   return rc;
 71163 /*
 71164 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
 71165 ** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
 71166 ** used by the comparison. If an error occurs, return an SQLite error code.
 71167 ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
 71168 ** value, depending on whether key1 is smaller, equal to or larger than key2.
 71169 **
 71170 ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
 71171 ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
 71172 ** is true and key1 contains even a single NULL value, it is considered to
 71173 ** be less than key2. Even if key2 also contains NULL values.
 71174 **
 71175 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
 71176 ** has been allocated and contains an unpacked record that is used as key2.
 71177 */
 71178 static void vdbeSorterCompare(
 71179   const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
 71180   int bOmitRowid,                 /* Ignore rowid field at end of keys */
 71181   const void *pKey1, int nKey1,   /* Left side of comparison */
 71182   const void *pKey2, int nKey2,   /* Right side of comparison */
 71183   int *pRes                       /* OUT: Result of comparison */
 71184 ){
 71185   KeyInfo *pKeyInfo = pCsr->pKeyInfo;
 71186   VdbeSorter *pSorter = pCsr->pSorter;
 71187   UnpackedRecord *r2 = pSorter->pUnpacked;
 71188   int i;
 71190   if( pKey2 ){
 71191     sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
 71194   if( bOmitRowid ){
 71195     r2->nField = pKeyInfo->nField;
 71196     assert( r2->nField>0 );
 71197     for(i=0; i<r2->nField; i++){
 71198       if( r2->aMem[i].flags & MEM_Null ){
 71199         *pRes = -1;
 71200         return;
 71203     r2->flags |= UNPACKED_PREFIX_MATCH;
 71206   *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
 71209 /*
 71210 ** This function is called to compare two iterator keys when merging 
 71211 ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
 71212 ** value to recalculate.
 71213 */
 71214 static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
 71215   VdbeSorter *pSorter = pCsr->pSorter;
 71216   int i1;
 71217   int i2;
 71218   int iRes;
 71219   VdbeSorterIter *p1;
 71220   VdbeSorterIter *p2;
 71222   assert( iOut<pSorter->nTree && iOut>0 );
 71224   if( iOut>=(pSorter->nTree/2) ){
 71225     i1 = (iOut - pSorter->nTree/2) * 2;
 71226     i2 = i1 + 1;
 71227   }else{
 71228     i1 = pSorter->aTree[iOut*2];
 71229     i2 = pSorter->aTree[iOut*2+1];
 71232   p1 = &pSorter->aIter[i1];
 71233   p2 = &pSorter->aIter[i2];
 71235   if( p1->pFile==0 ){
 71236     iRes = i2;
 71237   }else if( p2->pFile==0 ){
 71238     iRes = i1;
 71239   }else{
 71240     int res;
 71241     assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
 71242     vdbeSorterCompare(
 71243         pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
 71244     );
 71245     if( res<=0 ){
 71246       iRes = i1;
 71247     }else{
 71248       iRes = i2;
 71252   pSorter->aTree[iOut] = iRes;
 71253   return SQLITE_OK;
 71256 /*
 71257 ** Initialize the temporary index cursor just opened as a sorter cursor.
 71258 */
 71259 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
 71260   int pgsz;                       /* Page size of main database */
 71261   int mxCache;                    /* Cache size */
 71262   VdbeSorter *pSorter;            /* The new sorter */
 71263   char *d;                        /* Dummy */
 71265   assert( pCsr->pKeyInfo && pCsr->pBt==0 );
 71266   pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
 71267   if( pSorter==0 ){
 71268     return SQLITE_NOMEM;
 71271   pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
 71272   if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
 71273   assert( pSorter->pUnpacked==(UnpackedRecord *)d );
 71275   if( !sqlite3TempInMemory(db) ){
 71276     pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 71277     pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
 71278     mxCache = db->aDb[0].pSchema->cache_size;
 71279     if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
 71280     pSorter->mxPmaSize = mxCache * pgsz;
 71283   return SQLITE_OK;
 71286 /*
 71287 ** Free the list of sorted records starting at pRecord.
 71288 */
 71289 static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
 71290   SorterRecord *p;
 71291   SorterRecord *pNext;
 71292   for(p=pRecord; p; p=pNext){
 71293     pNext = p->pNext;
 71294     sqlite3DbFree(db, p);
 71298 /*
 71299 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
 71300 */
 71301 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
 71302   VdbeSorter *pSorter = pCsr->pSorter;
 71303   if( pSorter ){
 71304     if( pSorter->aIter ){
 71305       int i;
 71306       for(i=0; i<pSorter->nTree; i++){
 71307         vdbeSorterIterZero(db, &pSorter->aIter[i]);
 71309       sqlite3DbFree(db, pSorter->aIter);
 71311     if( pSorter->pTemp1 ){
 71312       sqlite3OsCloseFree(pSorter->pTemp1);
 71314     vdbeSorterRecordFree(db, pSorter->pRecord);
 71315     sqlite3DbFree(db, pSorter->pUnpacked);
 71316     sqlite3DbFree(db, pSorter);
 71317     pCsr->pSorter = 0;
 71321 /*
 71322 ** Allocate space for a file-handle and open a temporary file. If successful,
 71323 ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
 71324 ** Otherwise, set *ppFile to 0 and return an SQLite error code.
 71325 */
 71326 static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
 71327   int dummy;
 71328   return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
 71329       SQLITE_OPEN_TEMP_JOURNAL |
 71330       SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
 71331       SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
 71332   );
 71335 /*
 71336 ** Merge the two sorted lists p1 and p2 into a single list.
 71337 ** Set *ppOut to the head of the new list.
 71338 */
 71339 static void vdbeSorterMerge(
 71340   const VdbeCursor *pCsr,         /* For pKeyInfo */
 71341   SorterRecord *p1,               /* First list to merge */
 71342   SorterRecord *p2,               /* Second list to merge */
 71343   SorterRecord **ppOut            /* OUT: Head of merged list */
 71344 ){
 71345   SorterRecord *pFinal = 0;
 71346   SorterRecord **pp = &pFinal;
 71347   void *pVal2 = p2 ? p2->pVal : 0;
 71349   while( p1 && p2 ){
 71350     int res;
 71351     vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
 71352     if( res<=0 ){
 71353       *pp = p1;
 71354       pp = &p1->pNext;
 71355       p1 = p1->pNext;
 71356       pVal2 = 0;
 71357     }else{
 71358       *pp = p2;
 71359        pp = &p2->pNext;
 71360       p2 = p2->pNext;
 71361       if( p2==0 ) break;
 71362       pVal2 = p2->pVal;
 71365   *pp = p1 ? p1 : p2;
 71366   *ppOut = pFinal;
 71369 /*
 71370 ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
 71371 ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
 71372 ** occurs.
 71373 */
 71374 static int vdbeSorterSort(const VdbeCursor *pCsr){
 71375   int i;
 71376   SorterRecord **aSlot;
 71377   SorterRecord *p;
 71378   VdbeSorter *pSorter = pCsr->pSorter;
 71380   aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
 71381   if( !aSlot ){
 71382     return SQLITE_NOMEM;
 71385   p = pSorter->pRecord;
 71386   while( p ){
 71387     SorterRecord *pNext = p->pNext;
 71388     p->pNext = 0;
 71389     for(i=0; aSlot[i]; i++){
 71390       vdbeSorterMerge(pCsr, p, aSlot[i], &p);
 71391       aSlot[i] = 0;
 71393     aSlot[i] = p;
 71394     p = pNext;
 71397   p = 0;
 71398   for(i=0; i<64; i++){
 71399     vdbeSorterMerge(pCsr, p, aSlot[i], &p);
 71401   pSorter->pRecord = p;
 71403   sqlite3_free(aSlot);
 71404   return SQLITE_OK;
 71407 /*
 71408 ** Initialize a file-writer object.
 71409 */
 71410 static void fileWriterInit(
 71411   sqlite3 *db,                    /* Database (for malloc) */
 71412   sqlite3_file *pFile,            /* File to write to */
 71413   FileWriter *p,                  /* Object to populate */
 71414   i64 iStart                      /* Offset of pFile to begin writing at */
 71415 ){
 71416   int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 71418   memset(p, 0, sizeof(FileWriter));
 71419   p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 71420   if( !p->aBuffer ){
 71421     p->eFWErr = SQLITE_NOMEM;
 71422   }else{
 71423     p->iBufEnd = p->iBufStart = (iStart % nBuf);
 71424     p->iWriteOff = iStart - p->iBufStart;
 71425     p->nBuffer = nBuf;
 71426     p->pFile = pFile;
 71430 /*
 71431 ** Write nData bytes of data to the file-write object. Return SQLITE_OK
 71432 ** if successful, or an SQLite error code if an error occurs.
 71433 */
 71434 static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
 71435   int nRem = nData;
 71436   while( nRem>0 && p->eFWErr==0 ){
 71437     int nCopy = nRem;
 71438     if( nCopy>(p->nBuffer - p->iBufEnd) ){
 71439       nCopy = p->nBuffer - p->iBufEnd;
 71442     memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
 71443     p->iBufEnd += nCopy;
 71444     if( p->iBufEnd==p->nBuffer ){
 71445       p->eFWErr = sqlite3OsWrite(p->pFile, 
 71446           &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 71447           p->iWriteOff + p->iBufStart
 71448       );
 71449       p->iBufStart = p->iBufEnd = 0;
 71450       p->iWriteOff += p->nBuffer;
 71452     assert( p->iBufEnd<p->nBuffer );
 71454     nRem -= nCopy;
 71458 /*
 71459 ** Flush any buffered data to disk and clean up the file-writer object.
 71460 ** The results of using the file-writer after this call are undefined.
 71461 ** Return SQLITE_OK if flushing the buffered data succeeds or is not 
 71462 ** required. Otherwise, return an SQLite error code.
 71463 **
 71464 ** Before returning, set *piEof to the offset immediately following the
 71465 ** last byte written to the file.
 71466 */
 71467 static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
 71468   int rc;
 71469   if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
 71470     p->eFWErr = sqlite3OsWrite(p->pFile, 
 71471         &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 71472         p->iWriteOff + p->iBufStart
 71473     );
 71475   *piEof = (p->iWriteOff + p->iBufEnd);
 71476   sqlite3DbFree(db, p->aBuffer);
 71477   rc = p->eFWErr;
 71478   memset(p, 0, sizeof(FileWriter));
 71479   return rc;
 71482 /*
 71483 ** Write value iVal encoded as a varint to the file-write object. Return 
 71484 ** SQLITE_OK if successful, or an SQLite error code if an error occurs.
 71485 */
 71486 static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
 71487   int nByte; 
 71488   u8 aByte[10];
 71489   nByte = sqlite3PutVarint(aByte, iVal);
 71490   fileWriterWrite(p, aByte, nByte);
 71493 /*
 71494 ** Write the current contents of the in-memory linked-list to a PMA. Return
 71495 ** SQLITE_OK if successful, or an SQLite error code otherwise.
 71496 **
 71497 ** The format of a PMA is:
 71498 **
 71499 **     * A varint. This varint contains the total number of bytes of content
 71500 **       in the PMA (not including the varint itself).
 71501 **
 71502 **     * One or more records packed end-to-end in order of ascending keys. 
 71503 **       Each record consists of a varint followed by a blob of data (the 
 71504 **       key). The varint is the number of bytes in the blob of data.
 71505 */
 71506 static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
 71507   int rc = SQLITE_OK;             /* Return code */
 71508   VdbeSorter *pSorter = pCsr->pSorter;
 71509   FileWriter writer;
 71511   memset(&writer, 0, sizeof(FileWriter));
 71513   if( pSorter->nInMemory==0 ){
 71514     assert( pSorter->pRecord==0 );
 71515     return rc;
 71518   rc = vdbeSorterSort(pCsr);
 71520   /* If the first temporary PMA file has not been opened, open it now. */
 71521   if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
 71522     rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
 71523     assert( rc!=SQLITE_OK || pSorter->pTemp1 );
 71524     assert( pSorter->iWriteOff==0 );
 71525     assert( pSorter->nPMA==0 );
 71528   if( rc==SQLITE_OK ){
 71529     SorterRecord *p;
 71530     SorterRecord *pNext = 0;
 71532     fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
 71533     pSorter->nPMA++;
 71534     fileWriterWriteVarint(&writer, pSorter->nInMemory);
 71535     for(p=pSorter->pRecord; p; p=pNext){
 71536       pNext = p->pNext;
 71537       fileWriterWriteVarint(&writer, p->nVal);
 71538       fileWriterWrite(&writer, p->pVal, p->nVal);
 71539       sqlite3DbFree(db, p);
 71541     pSorter->pRecord = p;
 71542     rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
 71545   return rc;
 71548 /*
 71549 ** Add a record to the sorter.
 71550 */
 71551 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
 71552   sqlite3 *db,                    /* Database handle */
 71553   const VdbeCursor *pCsr,               /* Sorter cursor */
 71554   Mem *pVal                       /* Memory cell containing record */
 71555 ){
 71556   VdbeSorter *pSorter = pCsr->pSorter;
 71557   int rc = SQLITE_OK;             /* Return Code */
 71558   SorterRecord *pNew;             /* New list element */
 71560   assert( pSorter );
 71561   pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
 71563   pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
 71564   if( pNew==0 ){
 71565     rc = SQLITE_NOMEM;
 71566   }else{
 71567     pNew->pVal = (void *)&pNew[1];
 71568     memcpy(pNew->pVal, pVal->z, pVal->n);
 71569     pNew->nVal = pVal->n;
 71570     pNew->pNext = pSorter->pRecord;
 71571     pSorter->pRecord = pNew;
 71574   /* See if the contents of the sorter should now be written out. They
 71575   ** are written out when either of the following are true:
 71576   **
 71577   **   * The total memory allocated for the in-memory list is greater 
 71578   **     than (page-size * cache-size), or
 71579   **
 71580   **   * The total memory allocated for the in-memory list is greater 
 71581   **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
 71582   */
 71583   if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
 71584         (pSorter->nInMemory>pSorter->mxPmaSize)
 71585      || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
 71586   )){
 71587 #ifdef SQLITE_DEBUG
 71588     i64 nExpect = pSorter->iWriteOff
 71589                 + sqlite3VarintLen(pSorter->nInMemory)
 71590                 + pSorter->nInMemory;
 71591 #endif
 71592     rc = vdbeSorterListToPMA(db, pCsr);
 71593     pSorter->nInMemory = 0;
 71594     assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
 71597   return rc;
 71600 /*
 71601 ** Helper function for sqlite3VdbeSorterRewind(). 
 71602 */
 71603 static int vdbeSorterInitMerge(
 71604   sqlite3 *db,                    /* Database handle */
 71605   const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
 71606   i64 *pnByte                     /* Sum of bytes in all opened PMAs */
 71607 ){
 71608   VdbeSorter *pSorter = pCsr->pSorter;
 71609   int rc = SQLITE_OK;             /* Return code */
 71610   int i;                          /* Used to iterator through aIter[] */
 71611   i64 nByte = 0;                  /* Total bytes in all opened PMAs */
 71613   /* Initialize the iterators. */
 71614   for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
 71615     VdbeSorterIter *pIter = &pSorter->aIter[i];
 71616     rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
 71617     pSorter->iReadOff = pIter->iEof;
 71618     assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
 71619     if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
 71622   /* Initialize the aTree[] array. */
 71623   for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
 71624     rc = vdbeSorterDoCompare(pCsr, i);
 71627   *pnByte = nByte;
 71628   return rc;
 71631 /*
 71632 ** Once the sorter has been populated, this function is called to prepare
 71633 ** for iterating through its contents in sorted order.
 71634 */
 71635 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 71636   VdbeSorter *pSorter = pCsr->pSorter;
 71637   int rc;                         /* Return code */
 71638   sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
 71639   i64 iWrite2 = 0;                /* Write offset for pTemp2 */
 71640   int nIter;                      /* Number of iterators used */
 71641   int nByte;                      /* Bytes of space required for aIter/aTree */
 71642   int N = 2;                      /* Power of 2 >= nIter */
 71644   assert( pSorter );
 71646   /* If no data has been written to disk, then do not do so now. Instead,
 71647   ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
 71648   ** from the in-memory list.  */
 71649   if( pSorter->nPMA==0 ){
 71650     *pbEof = !pSorter->pRecord;
 71651     assert( pSorter->aTree==0 );
 71652     return vdbeSorterSort(pCsr);
 71655   /* Write the current in-memory list to a PMA. */
 71656   rc = vdbeSorterListToPMA(db, pCsr);
 71657   if( rc!=SQLITE_OK ) return rc;
 71659   /* Allocate space for aIter[] and aTree[]. */
 71660   nIter = pSorter->nPMA;
 71661   if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
 71662   assert( nIter>0 );
 71663   while( N<nIter ) N += N;
 71664   nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
 71665   pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
 71666   if( !pSorter->aIter ) return SQLITE_NOMEM;
 71667   pSorter->aTree = (int *)&pSorter->aIter[N];
 71668   pSorter->nTree = N;
 71670   do {
 71671     int iNew;                     /* Index of new, merged, PMA */
 71673     for(iNew=0; 
 71674         rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
 71675         iNew++
 71676     ){
 71677       int rc2;                    /* Return code from fileWriterFinish() */
 71678       FileWriter writer;          /* Object used to write to disk */
 71679       i64 nWrite;                 /* Number of bytes in new PMA */
 71681       memset(&writer, 0, sizeof(FileWriter));
 71683       /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
 71684       ** initialize an iterator for each of them and break out of the loop.
 71685       ** These iterators will be incrementally merged as the VDBE layer calls
 71686       ** sqlite3VdbeSorterNext().
 71687       **
 71688       ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
 71689       ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
 71690       ** are merged into a single PMA that is written to file pTemp2.
 71691       */
 71692       rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
 71693       assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
 71694       if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
 71695         break;
 71698       /* Open the second temp file, if it is not already open. */
 71699       if( pTemp2==0 ){
 71700         assert( iWrite2==0 );
 71701         rc = vdbeSorterOpenTempFile(db, &pTemp2);
 71704       if( rc==SQLITE_OK ){
 71705         int bEof = 0;
 71706         fileWriterInit(db, pTemp2, &writer, iWrite2);
 71707         fileWriterWriteVarint(&writer, nWrite);
 71708         while( rc==SQLITE_OK && bEof==0 ){
 71709           VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 71710           assert( pIter->pFile );
 71712           fileWriterWriteVarint(&writer, pIter->nKey);
 71713           fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
 71714           rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
 71716         rc2 = fileWriterFinish(db, &writer, &iWrite2);
 71717         if( rc==SQLITE_OK ) rc = rc2;
 71721     if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
 71722       break;
 71723     }else{
 71724       sqlite3_file *pTmp = pSorter->pTemp1;
 71725       pSorter->nPMA = iNew;
 71726       pSorter->pTemp1 = pTemp2;
 71727       pTemp2 = pTmp;
 71728       pSorter->iWriteOff = iWrite2;
 71729       pSorter->iReadOff = 0;
 71730       iWrite2 = 0;
 71732   }while( rc==SQLITE_OK );
 71734   if( pTemp2 ){
 71735     sqlite3OsCloseFree(pTemp2);
 71737   *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 71738   return rc;
 71741 /*
 71742 ** Advance to the next element in the sorter.
 71743 */
 71744 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 71745   VdbeSorter *pSorter = pCsr->pSorter;
 71746   int rc;                         /* Return code */
 71748   if( pSorter->aTree ){
 71749     int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
 71750     int i;                        /* Index of aTree[] to recalculate */
 71752     rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
 71753     for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
 71754       rc = vdbeSorterDoCompare(pCsr, i);
 71757     *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 71758   }else{
 71759     SorterRecord *pFree = pSorter->pRecord;
 71760     pSorter->pRecord = pFree->pNext;
 71761     pFree->pNext = 0;
 71762     vdbeSorterRecordFree(db, pFree);
 71763     *pbEof = !pSorter->pRecord;
 71764     rc = SQLITE_OK;
 71766   return rc;
 71769 /*
 71770 ** Return a pointer to a buffer owned by the sorter that contains the 
 71771 ** current key.
 71772 */
 71773 static void *vdbeSorterRowkey(
 71774   const VdbeSorter *pSorter,      /* Sorter object */
 71775   int *pnKey                      /* OUT: Size of current key in bytes */
 71776 ){
 71777   void *pKey;
 71778   if( pSorter->aTree ){
 71779     VdbeSorterIter *pIter;
 71780     pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 71781     *pnKey = pIter->nKey;
 71782     pKey = pIter->aKey;
 71783   }else{
 71784     *pnKey = pSorter->pRecord->nVal;
 71785     pKey = pSorter->pRecord->pVal;
 71787   return pKey;
 71790 /*
 71791 ** Copy the current sorter key into the memory cell pOut.
 71792 */
 71793 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
 71794   VdbeSorter *pSorter = pCsr->pSorter;
 71795   void *pKey; int nKey;           /* Sorter key to copy into pOut */
 71797   pKey = vdbeSorterRowkey(pSorter, &nKey);
 71798   if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
 71799     return SQLITE_NOMEM;
 71801   pOut->n = nKey;
 71802   MemSetTypeFlag(pOut, MEM_Blob);
 71803   memcpy(pOut->z, pKey, nKey);
 71805   return SQLITE_OK;
 71808 /*
 71809 ** Compare the key in memory cell pVal with the key that the sorter cursor
 71810 ** passed as the first argument currently points to. For the purposes of
 71811 ** the comparison, ignore the rowid field at the end of each record.
 71812 **
 71813 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
 71814 ** Otherwise, set *pRes to a negative, zero or positive value if the
 71815 ** key in pVal is smaller than, equal to or larger than the current sorter
 71816 ** key.
 71817 */
 71818 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
 71819   const VdbeCursor *pCsr,         /* Sorter cursor */
 71820   Mem *pVal,                      /* Value to compare to current sorter key */
 71821   int *pRes                       /* OUT: Result of comparison */
 71822 ){
 71823   VdbeSorter *pSorter = pCsr->pSorter;
 71824   void *pKey; int nKey;           /* Sorter key to compare pVal with */
 71826   pKey = vdbeSorterRowkey(pSorter, &nKey);
 71827   vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
 71828   return SQLITE_OK;
 71831 #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
 71833 /************** End of vdbesort.c ********************************************/
 71834 /************** Begin file journal.c *****************************************/
 71835 /*
 71836 ** 2007 August 22
 71837 **
 71838 ** The author disclaims copyright to this source code.  In place of
 71839 ** a legal notice, here is a blessing:
 71840 **
 71841 **    May you do good and not evil.
 71842 **    May you find forgiveness for yourself and forgive others.
 71843 **    May you share freely, never taking more than you give.
 71844 **
 71845 *************************************************************************
 71846 **
 71847 ** This file implements a special kind of sqlite3_file object used
 71848 ** by SQLite to create journal files if the atomic-write optimization
 71849 ** is enabled.
 71850 **
 71851 ** The distinctive characteristic of this sqlite3_file is that the
 71852 ** actual on disk file is created lazily. When the file is created,
 71853 ** the caller specifies a buffer size for an in-memory buffer to
 71854 ** be used to service read() and write() requests. The actual file
 71855 ** on disk is not created or populated until either:
 71856 **
 71857 **   1) The in-memory representation grows too large for the allocated 
 71858 **      buffer, or
 71859 **   2) The sqlite3JournalCreate() function is called.
 71860 */
 71861 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 71864 /*
 71865 ** A JournalFile object is a subclass of sqlite3_file used by
 71866 ** as an open file handle for journal files.
 71867 */
 71868 struct JournalFile {
 71869   sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
 71870   int nBuf;                       /* Size of zBuf[] in bytes */
 71871   char *zBuf;                     /* Space to buffer journal writes */
 71872   int iSize;                      /* Amount of zBuf[] currently used */
 71873   int flags;                      /* xOpen flags */
 71874   sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
 71875   sqlite3_file *pReal;            /* The "real" underlying file descriptor */
 71876   const char *zJournal;           /* Name of the journal file */
 71877 };
 71878 typedef struct JournalFile JournalFile;
 71880 /*
 71881 ** If it does not already exists, create and populate the on-disk file 
 71882 ** for JournalFile p.
 71883 */
 71884 static int createFile(JournalFile *p){
 71885   int rc = SQLITE_OK;
 71886   if( !p->pReal ){
 71887     sqlite3_file *pReal = (sqlite3_file *)&p[1];
 71888     rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
 71889     if( rc==SQLITE_OK ){
 71890       p->pReal = pReal;
 71891       if( p->iSize>0 ){
 71892         assert(p->iSize<=p->nBuf);
 71893         rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
 71897   return rc;
 71900 /*
 71901 ** Close the file.
 71902 */
 71903 static int jrnlClose(sqlite3_file *pJfd){
 71904   JournalFile *p = (JournalFile *)pJfd;
 71905   if( p->pReal ){
 71906     sqlite3OsClose(p->pReal);
 71908   sqlite3_free(p->zBuf);
 71909   return SQLITE_OK;
 71912 /*
 71913 ** Read data from the file.
 71914 */
 71915 static int jrnlRead(
 71916   sqlite3_file *pJfd,    /* The journal file from which to read */
 71917   void *zBuf,            /* Put the results here */
 71918   int iAmt,              /* Number of bytes to read */
 71919   sqlite_int64 iOfst     /* Begin reading at this offset */
 71920 ){
 71921   int rc = SQLITE_OK;
 71922   JournalFile *p = (JournalFile *)pJfd;
 71923   if( p->pReal ){
 71924     rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
 71925   }else if( (iAmt+iOfst)>p->iSize ){
 71926     rc = SQLITE_IOERR_SHORT_READ;
 71927   }else{
 71928     memcpy(zBuf, &p->zBuf[iOfst], iAmt);
 71930   return rc;
 71933 /*
 71934 ** Write data to the file.
 71935 */
 71936 static int jrnlWrite(
 71937   sqlite3_file *pJfd,    /* The journal file into which to write */
 71938   const void *zBuf,      /* Take data to be written from here */
 71939   int iAmt,              /* Number of bytes to write */
 71940   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
 71941 ){
 71942   int rc = SQLITE_OK;
 71943   JournalFile *p = (JournalFile *)pJfd;
 71944   if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
 71945     rc = createFile(p);
 71947   if( rc==SQLITE_OK ){
 71948     if( p->pReal ){
 71949       rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
 71950     }else{
 71951       memcpy(&p->zBuf[iOfst], zBuf, iAmt);
 71952       if( p->iSize<(iOfst+iAmt) ){
 71953         p->iSize = (iOfst+iAmt);
 71957   return rc;
 71960 /*
 71961 ** Truncate the file.
 71962 */
 71963 static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
 71964   int rc = SQLITE_OK;
 71965   JournalFile *p = (JournalFile *)pJfd;
 71966   if( p->pReal ){
 71967     rc = sqlite3OsTruncate(p->pReal, size);
 71968   }else if( size<p->iSize ){
 71969     p->iSize = size;
 71971   return rc;
 71974 /*
 71975 ** Sync the file.
 71976 */
 71977 static int jrnlSync(sqlite3_file *pJfd, int flags){
 71978   int rc;
 71979   JournalFile *p = (JournalFile *)pJfd;
 71980   if( p->pReal ){
 71981     rc = sqlite3OsSync(p->pReal, flags);
 71982   }else{
 71983     rc = SQLITE_OK;
 71985   return rc;
 71988 /*
 71989 ** Query the size of the file in bytes.
 71990 */
 71991 static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
 71992   int rc = SQLITE_OK;
 71993   JournalFile *p = (JournalFile *)pJfd;
 71994   if( p->pReal ){
 71995     rc = sqlite3OsFileSize(p->pReal, pSize);
 71996   }else{
 71997     *pSize = (sqlite_int64) p->iSize;
 71999   return rc;
 72002 /*
 72003 ** Table of methods for JournalFile sqlite3_file object.
 72004 */
 72005 static struct sqlite3_io_methods JournalFileMethods = {
 72006   1,             /* iVersion */
 72007   jrnlClose,     /* xClose */
 72008   jrnlRead,      /* xRead */
 72009   jrnlWrite,     /* xWrite */
 72010   jrnlTruncate,  /* xTruncate */
 72011   jrnlSync,      /* xSync */
 72012   jrnlFileSize,  /* xFileSize */
 72013   0,             /* xLock */
 72014   0,             /* xUnlock */
 72015   0,             /* xCheckReservedLock */
 72016   0,             /* xFileControl */
 72017   0,             /* xSectorSize */
 72018   0,             /* xDeviceCharacteristics */
 72019   0,             /* xShmMap */
 72020   0,             /* xShmLock */
 72021   0,             /* xShmBarrier */
 72022   0              /* xShmUnmap */
 72023 };
 72025 /* 
 72026 ** Open a journal file.
 72027 */
 72028 SQLITE_PRIVATE int sqlite3JournalOpen(
 72029   sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
 72030   const char *zName,         /* Name of the journal file */
 72031   sqlite3_file *pJfd,        /* Preallocated, blank file handle */
 72032   int flags,                 /* Opening flags */
 72033   int nBuf                   /* Bytes buffered before opening the file */
 72034 ){
 72035   JournalFile *p = (JournalFile *)pJfd;
 72036   memset(p, 0, sqlite3JournalSize(pVfs));
 72037   if( nBuf>0 ){
 72038     p->zBuf = sqlite3MallocZero(nBuf);
 72039     if( !p->zBuf ){
 72040       return SQLITE_NOMEM;
 72042   }else{
 72043     return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
 72045   p->pMethod = &JournalFileMethods;
 72046   p->nBuf = nBuf;
 72047   p->flags = flags;
 72048   p->zJournal = zName;
 72049   p->pVfs = pVfs;
 72050   return SQLITE_OK;
 72053 /*
 72054 ** If the argument p points to a JournalFile structure, and the underlying
 72055 ** file has not yet been created, create it now.
 72056 */
 72057 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
 72058   if( p->pMethods!=&JournalFileMethods ){
 72059     return SQLITE_OK;
 72061   return createFile((JournalFile *)p);
 72064 /*
 72065 ** The file-handle passed as the only argument is guaranteed to be an open
 72066 ** file. It may or may not be of class JournalFile. If the file is a
 72067 ** JournalFile, and the underlying file on disk has not yet been opened,
 72068 ** return 0. Otherwise, return 1.
 72069 */
 72070 SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
 72071   return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
 72074 /* 
 72075 ** Return the number of bytes required to store a JournalFile that uses vfs
 72076 ** pVfs to create the underlying on-disk files.
 72077 */
 72078 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
 72079   return (pVfs->szOsFile+sizeof(JournalFile));
 72081 #endif
 72083 /************** End of journal.c *********************************************/
 72084 /************** Begin file memjournal.c **************************************/
 72085 /*
 72086 ** 2008 October 7
 72087 **
 72088 ** The author disclaims copyright to this source code.  In place of
 72089 ** a legal notice, here is a blessing:
 72090 **
 72091 **    May you do good and not evil.
 72092 **    May you find forgiveness for yourself and forgive others.
 72093 **    May you share freely, never taking more than you give.
 72094 **
 72095 *************************************************************************
 72096 **
 72097 ** This file contains code use to implement an in-memory rollback journal.
 72098 ** The in-memory rollback journal is used to journal transactions for
 72099 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
 72100 */
 72102 /* Forward references to internal structures */
 72103 typedef struct MemJournal MemJournal;
 72104 typedef struct FilePoint FilePoint;
 72105 typedef struct FileChunk FileChunk;
 72107 /* Space to hold the rollback journal is allocated in increments of
 72108 ** this many bytes.
 72109 **
 72110 ** The size chosen is a little less than a power of two.  That way,
 72111 ** the FileChunk object will have a size that almost exactly fills
 72112 ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
 72113 ** memory allocators.
 72114 */
 72115 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
 72117 /* Macro to find the minimum of two numeric values.
 72118 */
 72119 #ifndef MIN
 72120 # define MIN(x,y) ((x)<(y)?(x):(y))
 72121 #endif
 72123 /*
 72124 ** The rollback journal is composed of a linked list of these structures.
 72125 */
 72126 struct FileChunk {
 72127   FileChunk *pNext;               /* Next chunk in the journal */
 72128   u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
 72129 };
 72131 /*
 72132 ** An instance of this object serves as a cursor into the rollback journal.
 72133 ** The cursor can be either for reading or writing.
 72134 */
 72135 struct FilePoint {
 72136   sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
 72137   FileChunk *pChunk;              /* Specific chunk into which cursor points */
 72138 };
 72140 /*
 72141 ** This subclass is a subclass of sqlite3_file.  Each open memory-journal
 72142 ** is an instance of this class.
 72143 */
 72144 struct MemJournal {
 72145   sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
 72146   FileChunk *pFirst;              /* Head of in-memory chunk-list */
 72147   FilePoint endpoint;             /* Pointer to the end of the file */
 72148   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
 72149 };
 72151 /*
 72152 ** Read data from the in-memory journal file.  This is the implementation
 72153 ** of the sqlite3_vfs.xRead method.
 72154 */
 72155 static int memjrnlRead(
 72156   sqlite3_file *pJfd,    /* The journal file from which to read */
 72157   void *zBuf,            /* Put the results here */
 72158   int iAmt,              /* Number of bytes to read */
 72159   sqlite_int64 iOfst     /* Begin reading at this offset */
 72160 ){
 72161   MemJournal *p = (MemJournal *)pJfd;
 72162   u8 *zOut = zBuf;
 72163   int nRead = iAmt;
 72164   int iChunkOffset;
 72165   FileChunk *pChunk;
 72167   /* SQLite never tries to read past the end of a rollback journal file */
 72168   assert( iOfst+iAmt<=p->endpoint.iOffset );
 72170   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
 72171     sqlite3_int64 iOff = 0;
 72172     for(pChunk=p->pFirst; 
 72173         ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
 72174         pChunk=pChunk->pNext
 72175     ){
 72176       iOff += JOURNAL_CHUNKSIZE;
 72178   }else{
 72179     pChunk = p->readpoint.pChunk;
 72182   iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
 72183   do {
 72184     int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
 72185     int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
 72186     memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
 72187     zOut += nCopy;
 72188     nRead -= iSpace;
 72189     iChunkOffset = 0;
 72190   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
 72191   p->readpoint.iOffset = iOfst+iAmt;
 72192   p->readpoint.pChunk = pChunk;
 72194   return SQLITE_OK;
 72197 /*
 72198 ** Write data to the file.
 72199 */
 72200 static int memjrnlWrite(
 72201   sqlite3_file *pJfd,    /* The journal file into which to write */
 72202   const void *zBuf,      /* Take data to be written from here */
 72203   int iAmt,              /* Number of bytes to write */
 72204   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
 72205 ){
 72206   MemJournal *p = (MemJournal *)pJfd;
 72207   int nWrite = iAmt;
 72208   u8 *zWrite = (u8 *)zBuf;
 72210   /* An in-memory journal file should only ever be appended to. Random
 72211   ** access writes are not required by sqlite.
 72212   */
 72213   assert( iOfst==p->endpoint.iOffset );
 72214   UNUSED_PARAMETER(iOfst);
 72216   while( nWrite>0 ){
 72217     FileChunk *pChunk = p->endpoint.pChunk;
 72218     int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
 72219     int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
 72221     if( iChunkOffset==0 ){
 72222       /* New chunk is required to extend the file. */
 72223       FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
 72224       if( !pNew ){
 72225         return SQLITE_IOERR_NOMEM;
 72227       pNew->pNext = 0;
 72228       if( pChunk ){
 72229         assert( p->pFirst );
 72230         pChunk->pNext = pNew;
 72231       }else{
 72232         assert( !p->pFirst );
 72233         p->pFirst = pNew;
 72235       p->endpoint.pChunk = pNew;
 72238     memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
 72239     zWrite += iSpace;
 72240     nWrite -= iSpace;
 72241     p->endpoint.iOffset += iSpace;
 72244   return SQLITE_OK;
 72247 /*
 72248 ** Truncate the file.
 72249 */
 72250 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
 72251   MemJournal *p = (MemJournal *)pJfd;
 72252   FileChunk *pChunk;
 72253   assert(size==0);
 72254   UNUSED_PARAMETER(size);
 72255   pChunk = p->pFirst;
 72256   while( pChunk ){
 72257     FileChunk *pTmp = pChunk;
 72258     pChunk = pChunk->pNext;
 72259     sqlite3_free(pTmp);
 72261   sqlite3MemJournalOpen(pJfd);
 72262   return SQLITE_OK;
 72265 /*
 72266 ** Close the file.
 72267 */
 72268 static int memjrnlClose(sqlite3_file *pJfd){
 72269   memjrnlTruncate(pJfd, 0);
 72270   return SQLITE_OK;
 72274 /*
 72275 ** Sync the file.
 72276 **
 72277 ** Syncing an in-memory journal is a no-op.  And, in fact, this routine
 72278 ** is never called in a working implementation.  This implementation
 72279 ** exists purely as a contingency, in case some malfunction in some other
 72280 ** part of SQLite causes Sync to be called by mistake.
 72281 */
 72282 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
 72283   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 72284   return SQLITE_OK;
 72287 /*
 72288 ** Query the size of the file in bytes.
 72289 */
 72290 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
 72291   MemJournal *p = (MemJournal *)pJfd;
 72292   *pSize = (sqlite_int64) p->endpoint.iOffset;
 72293   return SQLITE_OK;
 72296 /*
 72297 ** Table of methods for MemJournal sqlite3_file object.
 72298 */
 72299 static const struct sqlite3_io_methods MemJournalMethods = {
 72300   1,                /* iVersion */
 72301   memjrnlClose,     /* xClose */
 72302   memjrnlRead,      /* xRead */
 72303   memjrnlWrite,     /* xWrite */
 72304   memjrnlTruncate,  /* xTruncate */
 72305   memjrnlSync,      /* xSync */
 72306   memjrnlFileSize,  /* xFileSize */
 72307   0,                /* xLock */
 72308   0,                /* xUnlock */
 72309   0,                /* xCheckReservedLock */
 72310   0,                /* xFileControl */
 72311   0,                /* xSectorSize */
 72312   0,                /* xDeviceCharacteristics */
 72313   0,                /* xShmMap */
 72314   0,                /* xShmLock */
 72315   0,                /* xShmBarrier */
 72316   0                 /* xShmUnlock */
 72317 };
 72319 /* 
 72320 ** Open a journal file.
 72321 */
 72322 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
 72323   MemJournal *p = (MemJournal *)pJfd;
 72324   assert( EIGHT_BYTE_ALIGNMENT(p) );
 72325   memset(p, 0, sqlite3MemJournalSize());
 72326   p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
 72329 /*
 72330 ** Return true if the file-handle passed as an argument is 
 72331 ** an in-memory journal 
 72332 */
 72333 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
 72334   return pJfd->pMethods==&MemJournalMethods;
 72337 /* 
 72338 ** Return the number of bytes required to store a MemJournal file descriptor.
 72339 */
 72340 SQLITE_PRIVATE int sqlite3MemJournalSize(void){
 72341   return sizeof(MemJournal);
 72344 /************** End of memjournal.c ******************************************/
 72345 /************** Begin file walker.c ******************************************/
 72346 /*
 72347 ** 2008 August 16
 72348 **
 72349 ** The author disclaims copyright to this source code.  In place of
 72350 ** a legal notice, here is a blessing:
 72351 **
 72352 **    May you do good and not evil.
 72353 **    May you find forgiveness for yourself and forgive others.
 72354 **    May you share freely, never taking more than you give.
 72355 **
 72356 *************************************************************************
 72357 ** This file contains routines used for walking the parser tree for
 72358 ** an SQL statement.
 72359 */
 72360 /* #include <stdlib.h> */
 72361 /* #include <string.h> */
 72364 /*
 72365 ** Walk an expression tree.  Invoke the callback once for each node
 72366 ** of the expression, while decending.  (In other words, the callback
 72367 ** is invoked before visiting children.)
 72368 **
 72369 ** The return value from the callback should be one of the WRC_*
 72370 ** constants to specify how to proceed with the walk.
 72371 **
 72372 **    WRC_Continue      Continue descending down the tree.
 72373 **
 72374 **    WRC_Prune         Do not descend into child nodes.  But allow
 72375 **                      the walk to continue with sibling nodes.
 72376 **
 72377 **    WRC_Abort         Do no more callbacks.  Unwind the stack and
 72378 **                      return the top-level walk call.
 72379 **
 72380 ** The return value from this routine is WRC_Abort to abandon the tree walk
 72381 ** and WRC_Continue to continue.
 72382 */
 72383 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
 72384   int rc;
 72385   if( pExpr==0 ) return WRC_Continue;
 72386   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
 72387   testcase( ExprHasProperty(pExpr, EP_Reduced) );
 72388   rc = pWalker->xExprCallback(pWalker, pExpr);
 72389   if( rc==WRC_Continue
 72390               && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
 72391     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
 72392     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
 72393     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 72394       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 72395     }else{
 72396       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 72399   return rc & WRC_Abort;
 72402 /*
 72403 ** Call sqlite3WalkExpr() for every expression in list p or until
 72404 ** an abort request is seen.
 72405 */
 72406 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
 72407   int i;
 72408   struct ExprList_item *pItem;
 72409   if( p ){
 72410     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
 72411       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
 72414   return WRC_Continue;
 72417 /*
 72418 ** Walk all expressions associated with SELECT statement p.  Do
 72419 ** not invoke the SELECT callback on p, but do (of course) invoke
 72420 ** any expr callbacks and SELECT callbacks that come from subqueries.
 72421 ** Return WRC_Abort or WRC_Continue.
 72422 */
 72423 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
 72424   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
 72425   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
 72426   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
 72427   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
 72428   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
 72429   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
 72430   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
 72431   return WRC_Continue;
 72434 /*
 72435 ** Walk the parse trees associated with all subqueries in the
 72436 ** FROM clause of SELECT statement p.  Do not invoke the select
 72437 ** callback on p, but do invoke it on each FROM clause subquery
 72438 ** and on any subqueries further down in the tree.  Return 
 72439 ** WRC_Abort or WRC_Continue;
 72440 */
 72441 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
 72442   SrcList *pSrc;
 72443   int i;
 72444   struct SrcList_item *pItem;
 72446   pSrc = p->pSrc;
 72447   if( ALWAYS(pSrc) ){
 72448     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
 72449       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
 72450         return WRC_Abort;
 72454   return WRC_Continue;
 72457 /*
 72458 ** Call sqlite3WalkExpr() for every expression in Select statement p.
 72459 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
 72460 ** on the compound select chain, p->pPrior.
 72461 **
 72462 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
 72463 ** there is an abort request.
 72464 **
 72465 ** If the Walker does not have an xSelectCallback() then this routine
 72466 ** is a no-op returning WRC_Continue.
 72467 */
 72468 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
 72469   int rc;
 72470   if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
 72471   rc = WRC_Continue;
 72472   pWalker->walkerDepth++;
 72473   while( p ){
 72474     rc = pWalker->xSelectCallback(pWalker, p);
 72475     if( rc ) break;
 72476     if( sqlite3WalkSelectExpr(pWalker, p)
 72477      || sqlite3WalkSelectFrom(pWalker, p)
 72478     ){
 72479       pWalker->walkerDepth--;
 72480       return WRC_Abort;
 72482     p = p->pPrior;
 72484   pWalker->walkerDepth--;
 72485   return rc & WRC_Abort;
 72488 /************** End of walker.c **********************************************/
 72489 /************** Begin file resolve.c *****************************************/
 72490 /*
 72491 ** 2008 August 18
 72492 **
 72493 ** The author disclaims copyright to this source code.  In place of
 72494 ** a legal notice, here is a blessing:
 72495 **
 72496 **    May you do good and not evil.
 72497 **    May you find forgiveness for yourself and forgive others.
 72498 **    May you share freely, never taking more than you give.
 72499 **
 72500 *************************************************************************
 72501 **
 72502 ** This file contains routines used for walking the parser tree and
 72503 ** resolve all identifiers by associating them with a particular
 72504 ** table and column.
 72505 */
 72506 /* #include <stdlib.h> */
 72507 /* #include <string.h> */
 72509 /*
 72510 ** Walk the expression tree pExpr and increase the aggregate function
 72511 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
 72512 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
 72513 ** outer query into an inner subquery.
 72514 **
 72515 ** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
 72516 ** is a helper function - a callback for the tree walker.
 72517 */
 72518 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
 72519   if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
 72520   return WRC_Continue;
 72522 static void incrAggFunctionDepth(Expr *pExpr, int N){
 72523   if( N>0 ){
 72524     Walker w;
 72525     memset(&w, 0, sizeof(w));
 72526     w.xExprCallback = incrAggDepth;
 72527     w.u.i = N;
 72528     sqlite3WalkExpr(&w, pExpr);
 72532 /*
 72533 ** Turn the pExpr expression into an alias for the iCol-th column of the
 72534 ** result set in pEList.
 72535 **
 72536 ** If the result set column is a simple column reference, then this routine
 72537 ** makes an exact copy.  But for any other kind of expression, this
 72538 ** routine make a copy of the result set column as the argument to the
 72539 ** TK_AS operator.  The TK_AS operator causes the expression to be
 72540 ** evaluated just once and then reused for each alias.
 72541 **
 72542 ** The reason for suppressing the TK_AS term when the expression is a simple
 72543 ** column reference is so that the column reference will be recognized as
 72544 ** usable by indices within the WHERE clause processing logic. 
 72545 **
 72546 ** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
 72547 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
 72548 **
 72549 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
 72550 **
 72551 ** Is equivalent to:
 72552 **
 72553 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
 72554 **
 72555 ** The result of random()%5 in the GROUP BY clause is probably different
 72556 ** from the result in the result-set.  We might fix this someday.  Or
 72557 ** then again, we might not...
 72558 **
 72559 ** If the reference is followed by a COLLATE operator, then make sure
 72560 ** the COLLATE operator is preserved.  For example:
 72561 **
 72562 **     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
 72563 **
 72564 ** Should be transformed into:
 72565 **
 72566 **     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
 72567 **
 72568 ** The nSubquery parameter specifies how many levels of subquery the
 72569 ** alias is removed from the original expression.  The usually value is
 72570 ** zero but it might be more if the alias is contained within a subquery
 72571 ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
 72572 ** structures must be increased by the nSubquery amount.
 72573 */
 72574 static void resolveAlias(
 72575   Parse *pParse,         /* Parsing context */
 72576   ExprList *pEList,      /* A result set */
 72577   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
 72578   Expr *pExpr,           /* Transform this into an alias to the result set */
 72579   const char *zType,     /* "GROUP" or "ORDER" or "" */
 72580   int nSubquery          /* Number of subqueries that the label is moving */
 72581 ){
 72582   Expr *pOrig;           /* The iCol-th column of the result set */
 72583   Expr *pDup;            /* Copy of pOrig */
 72584   sqlite3 *db;           /* The database connection */
 72586   assert( iCol>=0 && iCol<pEList->nExpr );
 72587   pOrig = pEList->a[iCol].pExpr;
 72588   assert( pOrig!=0 );
 72589   assert( pOrig->flags & EP_Resolved );
 72590   db = pParse->db;
 72591   pDup = sqlite3ExprDup(db, pOrig, 0);
 72592   if( pDup==0 ) return;
 72593   if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
 72594     incrAggFunctionDepth(pDup, nSubquery);
 72595     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
 72596     if( pDup==0 ) return;
 72597     if( pEList->a[iCol].iAlias==0 ){
 72598       pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
 72600     pDup->iTable = pEList->a[iCol].iAlias;
 72602   if( pExpr->op==TK_COLLATE ){
 72603     pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
 72606   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
 72607   ** prevents ExprDelete() from deleting the Expr structure itself,
 72608   ** allowing it to be repopulated by the memcpy() on the following line.
 72609   ** The pExpr->u.zToken might point into memory that will be freed by the
 72610   ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
 72611   ** make a copy of the token before doing the sqlite3DbFree().
 72612   */
 72613   ExprSetProperty(pExpr, EP_Static);
 72614   sqlite3ExprDelete(db, pExpr);
 72615   memcpy(pExpr, pDup, sizeof(*pExpr));
 72616   if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
 72617     assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
 72618     pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
 72619     pExpr->flags2 |= EP2_MallocedToken;
 72621   sqlite3DbFree(db, pDup);
 72625 /*
 72626 ** Return TRUE if the name zCol occurs anywhere in the USING clause.
 72627 **
 72628 ** Return FALSE if the USING clause is NULL or if it does not contain
 72629 ** zCol.
 72630 */
 72631 static int nameInUsingClause(IdList *pUsing, const char *zCol){
 72632   if( pUsing ){
 72633     int k;
 72634     for(k=0; k<pUsing->nId; k++){
 72635       if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
 72638   return 0;
 72642 /*
 72643 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
 72644 ** that name in the set of source tables in pSrcList and make the pExpr 
 72645 ** expression node refer back to that source column.  The following changes
 72646 ** are made to pExpr:
 72647 **
 72648 **    pExpr->iDb           Set the index in db->aDb[] of the database X
 72649 **                         (even if X is implied).
 72650 **    pExpr->iTable        Set to the cursor number for the table obtained
 72651 **                         from pSrcList.
 72652 **    pExpr->pTab          Points to the Table structure of X.Y (even if
 72653 **                         X and/or Y are implied.)
 72654 **    pExpr->iColumn       Set to the column number within the table.
 72655 **    pExpr->op            Set to TK_COLUMN.
 72656 **    pExpr->pLeft         Any expression this points to is deleted
 72657 **    pExpr->pRight        Any expression this points to is deleted.
 72658 **
 72659 ** The zDb variable is the name of the database (the "X").  This value may be
 72660 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
 72661 ** can be used.  The zTable variable is the name of the table (the "Y").  This
 72662 ** value can be NULL if zDb is also NULL.  If zTable is NULL it
 72663 ** means that the form of the name is Z and that columns from any table
 72664 ** can be used.
 72665 **
 72666 ** If the name cannot be resolved unambiguously, leave an error message
 72667 ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
 72668 */
 72669 static int lookupName(
 72670   Parse *pParse,       /* The parsing context */
 72671   const char *zDb,     /* Name of the database containing table, or NULL */
 72672   const char *zTab,    /* Name of table containing column, or NULL */
 72673   const char *zCol,    /* Name of the column. */
 72674   NameContext *pNC,    /* The name context used to resolve the name */
 72675   Expr *pExpr          /* Make this EXPR node point to the selected column */
 72676 ){
 72677   int i, j;                         /* Loop counters */
 72678   int cnt = 0;                      /* Number of matching column names */
 72679   int cntTab = 0;                   /* Number of matching table names */
 72680   int nSubquery = 0;                /* How many levels of subquery */
 72681   sqlite3 *db = pParse->db;         /* The database connection */
 72682   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
 72683   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
 72684   NameContext *pTopNC = pNC;        /* First namecontext in the list */
 72685   Schema *pSchema = 0;              /* Schema of the expression */
 72686   int isTrigger = 0;
 72688   assert( pNC );     /* the name context cannot be NULL. */
 72689   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 72690   assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 72692   /* Initialize the node to no-match */
 72693   pExpr->iTable = -1;
 72694   pExpr->pTab = 0;
 72695   ExprSetIrreducible(pExpr);
 72697   /* Start at the inner-most context and move outward until a match is found */
 72698   while( pNC && cnt==0 ){
 72699     ExprList *pEList;
 72700     SrcList *pSrcList = pNC->pSrcList;
 72702     if( pSrcList ){
 72703       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
 72704         Table *pTab;
 72705         int iDb;
 72706         Column *pCol;
 72708         pTab = pItem->pTab;
 72709         assert( pTab!=0 && pTab->zName!=0 );
 72710         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 72711         assert( pTab->nCol>0 );
 72712         if( zTab ){
 72713           if( pItem->zAlias ){
 72714             char *zTabName = pItem->zAlias;
 72715             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
 72716           }else{
 72717             char *zTabName = pTab->zName;
 72718             if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
 72719               continue;
 72721             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
 72722               continue;
 72726         if( 0==(cntTab++) ){
 72727           pExpr->iTable = pItem->iCursor;
 72728           pExpr->pTab = pTab;
 72729           pSchema = pTab->pSchema;
 72730           pMatch = pItem;
 72732         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 72733           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 72734             /* If there has been exactly one prior match and this match
 72735             ** is for the right-hand table of a NATURAL JOIN or is in a 
 72736             ** USING clause, then skip this match.
 72737             */
 72738             if( cnt==1 ){
 72739               if( pItem->jointype & JT_NATURAL ) continue;
 72740               if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
 72742             cnt++;
 72743             pExpr->iTable = pItem->iCursor;
 72744             pExpr->pTab = pTab;
 72745             pMatch = pItem;
 72746             pSchema = pTab->pSchema;
 72747             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
 72748             pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 72749             break;
 72755 #ifndef SQLITE_OMIT_TRIGGER
 72756     /* If we have not already resolved the name, then maybe 
 72757     ** it is a new.* or old.* trigger argument reference
 72758     */
 72759     if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
 72760       int op = pParse->eTriggerOp;
 72761       Table *pTab = 0;
 72762       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
 72763       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
 72764         pExpr->iTable = 1;
 72765         pTab = pParse->pTriggerTab;
 72766       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
 72767         pExpr->iTable = 0;
 72768         pTab = pParse->pTriggerTab;
 72771       if( pTab ){ 
 72772         int iCol;
 72773         pSchema = pTab->pSchema;
 72774         cntTab++;
 72775         for(iCol=0; iCol<pTab->nCol; iCol++){
 72776           Column *pCol = &pTab->aCol[iCol];
 72777           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 72778             if( iCol==pTab->iPKey ){
 72779               iCol = -1;
 72781             break;
 72784         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
 72785           iCol = -1;        /* IMP: R-44911-55124 */
 72787         if( iCol<pTab->nCol ){
 72788           cnt++;
 72789           if( iCol<0 ){
 72790             pExpr->affinity = SQLITE_AFF_INTEGER;
 72791           }else if( pExpr->iTable==0 ){
 72792             testcase( iCol==31 );
 72793             testcase( iCol==32 );
 72794             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 72795           }else{
 72796             testcase( iCol==31 );
 72797             testcase( iCol==32 );
 72798             pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 72800           pExpr->iColumn = (i16)iCol;
 72801           pExpr->pTab = pTab;
 72802           isTrigger = 1;
 72806 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
 72808     /*
 72809     ** Perhaps the name is a reference to the ROWID
 72810     */
 72811     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
 72812       cnt = 1;
 72813       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
 72814       pExpr->affinity = SQLITE_AFF_INTEGER;
 72817     /*
 72818     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
 72819     ** might refer to an result-set alias.  This happens, for example, when
 72820     ** we are resolving names in the WHERE clause of the following command:
 72821     **
 72822     **     SELECT a+b AS x FROM table WHERE x<10;
 72823     **
 72824     ** In cases like this, replace pExpr with a copy of the expression that
 72825     ** forms the result set entry ("a+b" in the example) and return immediately.
 72826     ** Note that the expression in the result set should have already been
 72827     ** resolved by the time the WHERE clause is resolved.
 72828     */
 72829     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
 72830       for(j=0; j<pEList->nExpr; j++){
 72831         char *zAs = pEList->a[j].zName;
 72832         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 72833           Expr *pOrig;
 72834           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 72835           assert( pExpr->x.pList==0 );
 72836           assert( pExpr->x.pSelect==0 );
 72837           pOrig = pEList->a[j].pExpr;
 72838           if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
 72839             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
 72840             return WRC_Abort;
 72842           resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
 72843           cnt = 1;
 72844           pMatch = 0;
 72845           assert( zTab==0 && zDb==0 );
 72846           goto lookupname_end;
 72851     /* Advance to the next name context.  The loop will exit when either
 72852     ** we have a match (cnt>0) or when we run out of name contexts.
 72853     */
 72854     if( cnt==0 ){
 72855       pNC = pNC->pNext;
 72856       nSubquery++;
 72860   /*
 72861   ** If X and Y are NULL (in other words if only the column name Z is
 72862   ** supplied) and the value of Z is enclosed in double-quotes, then
 72863   ** Z is a string literal if it doesn't match any column names.  In that
 72864   ** case, we need to return right away and not make any changes to
 72865   ** pExpr.
 72866   **
 72867   ** Because no reference was made to outer contexts, the pNC->nRef
 72868   ** fields are not changed in any context.
 72869   */
 72870   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
 72871     pExpr->op = TK_STRING;
 72872     pExpr->pTab = 0;
 72873     return WRC_Prune;
 72876   /*
 72877   ** cnt==0 means there was not match.  cnt>1 means there were two or
 72878   ** more matches.  Either way, we have an error.
 72879   */
 72880   if( cnt!=1 ){
 72881     const char *zErr;
 72882     zErr = cnt==0 ? "no such column" : "ambiguous column name";
 72883     if( zDb ){
 72884       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
 72885     }else if( zTab ){
 72886       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
 72887     }else{
 72888       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
 72890     pParse->checkSchema = 1;
 72891     pTopNC->nErr++;
 72894   /* If a column from a table in pSrcList is referenced, then record
 72895   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
 72896   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
 72897   ** column number is greater than the number of bits in the bitmask
 72898   ** then set the high-order bit of the bitmask.
 72899   */
 72900   if( pExpr->iColumn>=0 && pMatch!=0 ){
 72901     int n = pExpr->iColumn;
 72902     testcase( n==BMS-1 );
 72903     if( n>=BMS ){
 72904       n = BMS-1;
 72906     assert( pMatch->iCursor==pExpr->iTable );
 72907     pMatch->colUsed |= ((Bitmask)1)<<n;
 72910   /* Clean up and return
 72911   */
 72912   sqlite3ExprDelete(db, pExpr->pLeft);
 72913   pExpr->pLeft = 0;
 72914   sqlite3ExprDelete(db, pExpr->pRight);
 72915   pExpr->pRight = 0;
 72916   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
 72917 lookupname_end:
 72918   if( cnt==1 ){
 72919     assert( pNC!=0 );
 72920     sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
 72921     /* Increment the nRef value on all name contexts from TopNC up to
 72922     ** the point where the name matched. */
 72923     for(;;){
 72924       assert( pTopNC!=0 );
 72925       pTopNC->nRef++;
 72926       if( pTopNC==pNC ) break;
 72927       pTopNC = pTopNC->pNext;
 72929     return WRC_Prune;
 72930   } else {
 72931     return WRC_Abort;
 72935 /*
 72936 ** Allocate and return a pointer to an expression to load the column iCol
 72937 ** from datasource iSrc in SrcList pSrc.
 72938 */
 72939 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
 72940   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
 72941   if( p ){
 72942     struct SrcList_item *pItem = &pSrc->a[iSrc];
 72943     p->pTab = pItem->pTab;
 72944     p->iTable = pItem->iCursor;
 72945     if( p->pTab->iPKey==iCol ){
 72946       p->iColumn = -1;
 72947     }else{
 72948       p->iColumn = (ynVar)iCol;
 72949       testcase( iCol==BMS );
 72950       testcase( iCol==BMS-1 );
 72951       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
 72953     ExprSetProperty(p, EP_Resolved);
 72955   return p;
 72958 /*
 72959 ** This routine is callback for sqlite3WalkExpr().
 72960 **
 72961 ** Resolve symbolic names into TK_COLUMN operators for the current
 72962 ** node in the expression tree.  Return 0 to continue the search down
 72963 ** the tree or 2 to abort the tree walk.
 72964 **
 72965 ** This routine also does error checking and name resolution for
 72966 ** function names.  The operator for aggregate functions is changed
 72967 ** to TK_AGG_FUNCTION.
 72968 */
 72969 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
 72970   NameContext *pNC;
 72971   Parse *pParse;
 72973   pNC = pWalker->u.pNC;
 72974   assert( pNC!=0 );
 72975   pParse = pNC->pParse;
 72976   assert( pParse==pWalker->pParse );
 72978   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
 72979   ExprSetProperty(pExpr, EP_Resolved);
 72980 #ifndef NDEBUG
 72981   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
 72982     SrcList *pSrcList = pNC->pSrcList;
 72983     int i;
 72984     for(i=0; i<pNC->pSrcList->nSrc; i++){
 72985       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
 72988 #endif
 72989   switch( pExpr->op ){
 72991 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 72992     /* The special operator TK_ROW means use the rowid for the first
 72993     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
 72994     ** clause processing on UPDATE and DELETE statements.
 72995     */
 72996     case TK_ROW: {
 72997       SrcList *pSrcList = pNC->pSrcList;
 72998       struct SrcList_item *pItem;
 72999       assert( pSrcList && pSrcList->nSrc==1 );
 73000       pItem = pSrcList->a; 
 73001       pExpr->op = TK_COLUMN;
 73002       pExpr->pTab = pItem->pTab;
 73003       pExpr->iTable = pItem->iCursor;
 73004       pExpr->iColumn = -1;
 73005       pExpr->affinity = SQLITE_AFF_INTEGER;
 73006       break;
 73008 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
 73010     /* A lone identifier is the name of a column.
 73011     */
 73012     case TK_ID: {
 73013       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
 73016     /* A table name and column name:     ID.ID
 73017     ** Or a database, table and column:  ID.ID.ID
 73018     */
 73019     case TK_DOT: {
 73020       const char *zColumn;
 73021       const char *zTable;
 73022       const char *zDb;
 73023       Expr *pRight;
 73025       /* if( pSrcList==0 ) break; */
 73026       pRight = pExpr->pRight;
 73027       if( pRight->op==TK_ID ){
 73028         zDb = 0;
 73029         zTable = pExpr->pLeft->u.zToken;
 73030         zColumn = pRight->u.zToken;
 73031       }else{
 73032         assert( pRight->op==TK_DOT );
 73033         zDb = pExpr->pLeft->u.zToken;
 73034         zTable = pRight->pLeft->u.zToken;
 73035         zColumn = pRight->pRight->u.zToken;
 73037       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 73040     /* Resolve function names
 73041     */
 73042     case TK_CONST_FUNC:
 73043     case TK_FUNCTION: {
 73044       ExprList *pList = pExpr->x.pList;    /* The argument list */
 73045       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
 73046       int no_such_func = 0;       /* True if no such function exists */
 73047       int wrong_num_args = 0;     /* True if wrong number of arguments */
 73048       int is_agg = 0;             /* True if is an aggregate function */
 73049       int auth;                   /* Authorization to use the function */
 73050       int nId;                    /* Number of characters in function name */
 73051       const char *zId;            /* The function name. */
 73052       FuncDef *pDef;              /* Information about the function */
 73053       u8 enc = ENC(pParse->db);   /* The database encoding */
 73055       testcase( pExpr->op==TK_CONST_FUNC );
 73056       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 73057       zId = pExpr->u.zToken;
 73058       nId = sqlite3Strlen30(zId);
 73059       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
 73060       if( pDef==0 ){
 73061         pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
 73062         if( pDef==0 ){
 73063           no_such_func = 1;
 73064         }else{
 73065           wrong_num_args = 1;
 73067       }else{
 73068         is_agg = pDef->xFunc==0;
 73070 #ifndef SQLITE_OMIT_AUTHORIZATION
 73071       if( pDef ){
 73072         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
 73073         if( auth!=SQLITE_OK ){
 73074           if( auth==SQLITE_DENY ){
 73075             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
 73076                                     pDef->zName);
 73077             pNC->nErr++;
 73079           pExpr->op = TK_NULL;
 73080           return WRC_Prune;
 73083 #endif
 73084       if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
 73085         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
 73086         pNC->nErr++;
 73087         is_agg = 0;
 73088       }else if( no_such_func ){
 73089         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
 73090         pNC->nErr++;
 73091       }else if( wrong_num_args ){
 73092         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 73093              nId, zId);
 73094         pNC->nErr++;
 73096       if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
 73097       sqlite3WalkExprList(pWalker, pList);
 73098       if( is_agg ){
 73099         NameContext *pNC2 = pNC;
 73100         pExpr->op = TK_AGG_FUNCTION;
 73101         pExpr->op2 = 0;
 73102         while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
 73103           pExpr->op2++;
 73104           pNC2 = pNC2->pNext;
 73106         if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
 73107         pNC->ncFlags |= NC_AllowAgg;
 73109       /* FIX ME:  Compute pExpr->affinity based on the expected return
 73110       ** type of the function 
 73111       */
 73112       return WRC_Prune;
 73114 #ifndef SQLITE_OMIT_SUBQUERY
 73115     case TK_SELECT:
 73116     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
 73117 #endif
 73118     case TK_IN: {
 73119       testcase( pExpr->op==TK_IN );
 73120       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 73121         int nRef = pNC->nRef;
 73122 #ifndef SQLITE_OMIT_CHECK
 73123         if( (pNC->ncFlags & NC_IsCheck)!=0 ){
 73124           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
 73126 #endif
 73127         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
 73128         assert( pNC->nRef>=nRef );
 73129         if( nRef!=pNC->nRef ){
 73130           ExprSetProperty(pExpr, EP_VarSelect);
 73133       break;
 73135 #ifndef SQLITE_OMIT_CHECK
 73136     case TK_VARIABLE: {
 73137       if( (pNC->ncFlags & NC_IsCheck)!=0 ){
 73138         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
 73140       break;
 73142 #endif
 73144   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
 73147 /*
 73148 ** pEList is a list of expressions which are really the result set of the
 73149 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
 73150 ** This routine checks to see if pE is a simple identifier which corresponds
 73151 ** to the AS-name of one of the terms of the expression list.  If it is,
 73152 ** this routine return an integer between 1 and N where N is the number of
 73153 ** elements in pEList, corresponding to the matching entry.  If there is
 73154 ** no match, or if pE is not a simple identifier, then this routine
 73155 ** return 0.
 73156 **
 73157 ** pEList has been resolved.  pE has not.
 73158 */
 73159 static int resolveAsName(
 73160   Parse *pParse,     /* Parsing context for error messages */
 73161   ExprList *pEList,  /* List of expressions to scan */
 73162   Expr *pE           /* Expression we are trying to match */
 73163 ){
 73164   int i;             /* Loop counter */
 73166   UNUSED_PARAMETER(pParse);
 73168   if( pE->op==TK_ID ){
 73169     char *zCol = pE->u.zToken;
 73170     for(i=0; i<pEList->nExpr; i++){
 73171       char *zAs = pEList->a[i].zName;
 73172       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 73173         return i+1;
 73177   return 0;
 73180 /*
 73181 ** pE is a pointer to an expression which is a single term in the
 73182 ** ORDER BY of a compound SELECT.  The expression has not been
 73183 ** name resolved.
 73184 **
 73185 ** At the point this routine is called, we already know that the
 73186 ** ORDER BY term is not an integer index into the result set.  That
 73187 ** case is handled by the calling routine.
 73188 **
 73189 ** Attempt to match pE against result set columns in the left-most
 73190 ** SELECT statement.  Return the index i of the matching column,
 73191 ** as an indication to the caller that it should sort by the i-th column.
 73192 ** The left-most column is 1.  In other words, the value returned is the
 73193 ** same integer value that would be used in the SQL statement to indicate
 73194 ** the column.
 73195 **
 73196 ** If there is no match, return 0.  Return -1 if an error occurs.
 73197 */
 73198 static int resolveOrderByTermToExprList(
 73199   Parse *pParse,     /* Parsing context for error messages */
 73200   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
 73201   Expr *pE           /* The specific ORDER BY term */
 73202 ){
 73203   int i;             /* Loop counter */
 73204   ExprList *pEList;  /* The columns of the result set */
 73205   NameContext nc;    /* Name context for resolving pE */
 73206   sqlite3 *db;       /* Database connection */
 73207   int rc;            /* Return code from subprocedures */
 73208   u8 savedSuppErr;   /* Saved value of db->suppressErr */
 73210   assert( sqlite3ExprIsInteger(pE, &i)==0 );
 73211   pEList = pSelect->pEList;
 73213   /* Resolve all names in the ORDER BY term expression
 73214   */
 73215   memset(&nc, 0, sizeof(nc));
 73216   nc.pParse = pParse;
 73217   nc.pSrcList = pSelect->pSrc;
 73218   nc.pEList = pEList;
 73219   nc.ncFlags = NC_AllowAgg;
 73220   nc.nErr = 0;
 73221   db = pParse->db;
 73222   savedSuppErr = db->suppressErr;
 73223   db->suppressErr = 1;
 73224   rc = sqlite3ResolveExprNames(&nc, pE);
 73225   db->suppressErr = savedSuppErr;
 73226   if( rc ) return 0;
 73228   /* Try to match the ORDER BY expression against an expression
 73229   ** in the result set.  Return an 1-based index of the matching
 73230   ** result-set entry.
 73231   */
 73232   for(i=0; i<pEList->nExpr; i++){
 73233     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
 73234       return i+1;
 73238   /* If no match, return 0. */
 73239   return 0;
 73242 /*
 73243 ** Generate an ORDER BY or GROUP BY term out-of-range error.
 73244 */
 73245 static void resolveOutOfRangeError(
 73246   Parse *pParse,         /* The error context into which to write the error */
 73247   const char *zType,     /* "ORDER" or "GROUP" */
 73248   int i,                 /* The index (1-based) of the term out of range */
 73249   int mx                 /* Largest permissible value of i */
 73250 ){
 73251   sqlite3ErrorMsg(pParse, 
 73252     "%r %s BY term out of range - should be "
 73253     "between 1 and %d", i, zType, mx);
 73256 /*
 73257 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
 73258 ** each term of the ORDER BY clause is a constant integer between 1
 73259 ** and N where N is the number of columns in the compound SELECT.
 73260 **
 73261 ** ORDER BY terms that are already an integer between 1 and N are
 73262 ** unmodified.  ORDER BY terms that are integers outside the range of
 73263 ** 1 through N generate an error.  ORDER BY terms that are expressions
 73264 ** are matched against result set expressions of compound SELECT
 73265 ** beginning with the left-most SELECT and working toward the right.
 73266 ** At the first match, the ORDER BY expression is transformed into
 73267 ** the integer column number.
 73268 **
 73269 ** Return the number of errors seen.
 73270 */
 73271 static int resolveCompoundOrderBy(
 73272   Parse *pParse,        /* Parsing context.  Leave error messages here */
 73273   Select *pSelect       /* The SELECT statement containing the ORDER BY */
 73274 ){
 73275   int i;
 73276   ExprList *pOrderBy;
 73277   ExprList *pEList;
 73278   sqlite3 *db;
 73279   int moreToDo = 1;
 73281   pOrderBy = pSelect->pOrderBy;
 73282   if( pOrderBy==0 ) return 0;
 73283   db = pParse->db;
 73284 #if SQLITE_MAX_COLUMN
 73285   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 73286     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
 73287     return 1;
 73289 #endif
 73290   for(i=0; i<pOrderBy->nExpr; i++){
 73291     pOrderBy->a[i].done = 0;
 73293   pSelect->pNext = 0;
 73294   while( pSelect->pPrior ){
 73295     pSelect->pPrior->pNext = pSelect;
 73296     pSelect = pSelect->pPrior;
 73298   while( pSelect && moreToDo ){
 73299     struct ExprList_item *pItem;
 73300     moreToDo = 0;
 73301     pEList = pSelect->pEList;
 73302     assert( pEList!=0 );
 73303     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 73304       int iCol = -1;
 73305       Expr *pE, *pDup;
 73306       if( pItem->done ) continue;
 73307       pE = sqlite3ExprSkipCollate(pItem->pExpr);
 73308       if( sqlite3ExprIsInteger(pE, &iCol) ){
 73309         if( iCol<=0 || iCol>pEList->nExpr ){
 73310           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
 73311           return 1;
 73313       }else{
 73314         iCol = resolveAsName(pParse, pEList, pE);
 73315         if( iCol==0 ){
 73316           pDup = sqlite3ExprDup(db, pE, 0);
 73317           if( !db->mallocFailed ){
 73318             assert(pDup);
 73319             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
 73321           sqlite3ExprDelete(db, pDup);
 73324       if( iCol>0 ){
 73325         /* Convert the ORDER BY term into an integer column number iCol,
 73326         ** taking care to preserve the COLLATE clause if it exists */
 73327         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 73328         if( pNew==0 ) return 1;
 73329         pNew->flags |= EP_IntValue;
 73330         pNew->u.iValue = iCol;
 73331         if( pItem->pExpr==pE ){
 73332           pItem->pExpr = pNew;
 73333         }else{
 73334           assert( pItem->pExpr->op==TK_COLLATE );
 73335           assert( pItem->pExpr->pLeft==pE );
 73336           pItem->pExpr->pLeft = pNew;
 73338         sqlite3ExprDelete(db, pE);
 73339         pItem->iOrderByCol = (u16)iCol;
 73340         pItem->done = 1;
 73341       }else{
 73342         moreToDo = 1;
 73345     pSelect = pSelect->pNext;
 73347   for(i=0; i<pOrderBy->nExpr; i++){
 73348     if( pOrderBy->a[i].done==0 ){
 73349       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
 73350             "column in the result set", i+1);
 73351       return 1;
 73354   return 0;
 73357 /*
 73358 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
 73359 ** the SELECT statement pSelect.  If any term is reference to a
 73360 ** result set expression (as determined by the ExprList.a.iCol field)
 73361 ** then convert that term into a copy of the corresponding result set
 73362 ** column.
 73363 **
 73364 ** If any errors are detected, add an error message to pParse and
 73365 ** return non-zero.  Return zero if no errors are seen.
 73366 */
 73367 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
 73368   Parse *pParse,        /* Parsing context.  Leave error messages here */
 73369   Select *pSelect,      /* The SELECT statement containing the clause */
 73370   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
 73371   const char *zType     /* "ORDER" or "GROUP" */
 73372 ){
 73373   int i;
 73374   sqlite3 *db = pParse->db;
 73375   ExprList *pEList;
 73376   struct ExprList_item *pItem;
 73378   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
 73379 #if SQLITE_MAX_COLUMN
 73380   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 73381     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
 73382     return 1;
 73384 #endif
 73385   pEList = pSelect->pEList;
 73386   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
 73387   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 73388     if( pItem->iOrderByCol ){
 73389       if( pItem->iOrderByCol>pEList->nExpr ){
 73390         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
 73391         return 1;
 73393       resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
 73396   return 0;
 73399 /*
 73400 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
 73401 ** The Name context of the SELECT statement is pNC.  zType is either
 73402 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
 73403 **
 73404 ** This routine resolves each term of the clause into an expression.
 73405 ** If the order-by term is an integer I between 1 and N (where N is the
 73406 ** number of columns in the result set of the SELECT) then the expression
 73407 ** in the resolution is a copy of the I-th result-set expression.  If
 73408 ** the order-by term is an identify that corresponds to the AS-name of
 73409 ** a result-set expression, then the term resolves to a copy of the
 73410 ** result-set expression.  Otherwise, the expression is resolved in
 73411 ** the usual way - using sqlite3ResolveExprNames().
 73412 **
 73413 ** This routine returns the number of errors.  If errors occur, then
 73414 ** an appropriate error message might be left in pParse.  (OOM errors
 73415 ** excepted.)
 73416 */
 73417 static int resolveOrderGroupBy(
 73418   NameContext *pNC,     /* The name context of the SELECT statement */
 73419   Select *pSelect,      /* The SELECT statement holding pOrderBy */
 73420   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
 73421   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
 73422 ){
 73423   int i, j;                      /* Loop counters */
 73424   int iCol;                      /* Column number */
 73425   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
 73426   Parse *pParse;                 /* Parsing context */
 73427   int nResult;                   /* Number of terms in the result set */
 73429   if( pOrderBy==0 ) return 0;
 73430   nResult = pSelect->pEList->nExpr;
 73431   pParse = pNC->pParse;
 73432   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 73433     Expr *pE = pItem->pExpr;
 73434     iCol = resolveAsName(pParse, pSelect->pEList, pE);
 73435     if( iCol>0 ){
 73436       /* If an AS-name match is found, mark this ORDER BY column as being
 73437       ** a copy of the iCol-th result-set column.  The subsequent call to
 73438       ** sqlite3ResolveOrderGroupBy() will convert the expression to a
 73439       ** copy of the iCol-th result-set expression. */
 73440       pItem->iOrderByCol = (u16)iCol;
 73441       continue;
 73443     if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
 73444       /* The ORDER BY term is an integer constant.  Again, set the column
 73445       ** number so that sqlite3ResolveOrderGroupBy() will convert the
 73446       ** order-by term to a copy of the result-set expression */
 73447       if( iCol<1 || iCol>0xffff ){
 73448         resolveOutOfRangeError(pParse, zType, i+1, nResult);
 73449         return 1;
 73451       pItem->iOrderByCol = (u16)iCol;
 73452       continue;
 73455     /* Otherwise, treat the ORDER BY term as an ordinary expression */
 73456     pItem->iOrderByCol = 0;
 73457     if( sqlite3ResolveExprNames(pNC, pE) ){
 73458       return 1;
 73460     for(j=0; j<pSelect->pEList->nExpr; j++){
 73461       if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr)==0 ){
 73462         pItem->iOrderByCol = j+1;
 73466   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
 73469 /*
 73470 ** Resolve names in the SELECT statement p and all of its descendents.
 73471 */
 73472 static int resolveSelectStep(Walker *pWalker, Select *p){
 73473   NameContext *pOuterNC;  /* Context that contains this SELECT */
 73474   NameContext sNC;        /* Name context of this SELECT */
 73475   int isCompound;         /* True if p is a compound select */
 73476   int nCompound;          /* Number of compound terms processed so far */
 73477   Parse *pParse;          /* Parsing context */
 73478   ExprList *pEList;       /* Result set expression list */
 73479   int i;                  /* Loop counter */
 73480   ExprList *pGroupBy;     /* The GROUP BY clause */
 73481   Select *pLeftmost;      /* Left-most of SELECT of a compound */
 73482   sqlite3 *db;            /* Database connection */
 73485   assert( p!=0 );
 73486   if( p->selFlags & SF_Resolved ){
 73487     return WRC_Prune;
 73489   pOuterNC = pWalker->u.pNC;
 73490   pParse = pWalker->pParse;
 73491   db = pParse->db;
 73493   /* Normally sqlite3SelectExpand() will be called first and will have
 73494   ** already expanded this SELECT.  However, if this is a subquery within
 73495   ** an expression, sqlite3ResolveExprNames() will be called without a
 73496   ** prior call to sqlite3SelectExpand().  When that happens, let
 73497   ** sqlite3SelectPrep() do all of the processing for this SELECT.
 73498   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
 73499   ** this routine in the correct order.
 73500   */
 73501   if( (p->selFlags & SF_Expanded)==0 ){
 73502     sqlite3SelectPrep(pParse, p, pOuterNC);
 73503     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
 73506   isCompound = p->pPrior!=0;
 73507   nCompound = 0;
 73508   pLeftmost = p;
 73509   while( p ){
 73510     assert( (p->selFlags & SF_Expanded)!=0 );
 73511     assert( (p->selFlags & SF_Resolved)==0 );
 73512     p->selFlags |= SF_Resolved;
 73514     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
 73515     ** are not allowed to refer to any names, so pass an empty NameContext.
 73516     */
 73517     memset(&sNC, 0, sizeof(sNC));
 73518     sNC.pParse = pParse;
 73519     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
 73520         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
 73521       return WRC_Abort;
 73524     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
 73525     ** resolve the result-set expression list.
 73526     */
 73527     sNC.ncFlags = NC_AllowAgg;
 73528     sNC.pSrcList = p->pSrc;
 73529     sNC.pNext = pOuterNC;
 73531     /* Resolve names in the result set. */
 73532     pEList = p->pEList;
 73533     assert( pEList!=0 );
 73534     for(i=0; i<pEList->nExpr; i++){
 73535       Expr *pX = pEList->a[i].pExpr;
 73536       if( sqlite3ResolveExprNames(&sNC, pX) ){
 73537         return WRC_Abort;
 73541     /* Recursively resolve names in all subqueries
 73542     */
 73543     for(i=0; i<p->pSrc->nSrc; i++){
 73544       struct SrcList_item *pItem = &p->pSrc->a[i];
 73545       if( pItem->pSelect ){
 73546         NameContext *pNC;         /* Used to iterate name contexts */
 73547         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
 73548         const char *zSavedContext = pParse->zAuthContext;
 73550         /* Count the total number of references to pOuterNC and all of its
 73551         ** parent contexts. After resolving references to expressions in
 73552         ** pItem->pSelect, check if this value has changed. If so, then
 73553         ** SELECT statement pItem->pSelect must be correlated. Set the
 73554         ** pItem->isCorrelated flag if this is the case. */
 73555         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
 73557         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
 73558         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
 73559         pParse->zAuthContext = zSavedContext;
 73560         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
 73562         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
 73563         assert( pItem->isCorrelated==0 && nRef<=0 );
 73564         pItem->isCorrelated = (nRef!=0);
 73568     /* If there are no aggregate functions in the result-set, and no GROUP BY 
 73569     ** expression, do not allow aggregates in any of the other expressions.
 73570     */
 73571     assert( (p->selFlags & SF_Aggregate)==0 );
 73572     pGroupBy = p->pGroupBy;
 73573     if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
 73574       p->selFlags |= SF_Aggregate;
 73575     }else{
 73576       sNC.ncFlags &= ~NC_AllowAgg;
 73579     /* If a HAVING clause is present, then there must be a GROUP BY clause.
 73580     */
 73581     if( p->pHaving && !pGroupBy ){
 73582       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
 73583       return WRC_Abort;
 73586     /* Add the expression list to the name-context before parsing the
 73587     ** other expressions in the SELECT statement. This is so that
 73588     ** expressions in the WHERE clause (etc.) can refer to expressions by
 73589     ** aliases in the result set.
 73590     **
 73591     ** Minor point: If this is the case, then the expression will be
 73592     ** re-evaluated for each reference to it.
 73593     */
 73594     sNC.pEList = p->pEList;
 73595     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
 73596        sqlite3ResolveExprNames(&sNC, p->pHaving)
 73597     ){
 73598       return WRC_Abort;
 73601     /* The ORDER BY and GROUP BY clauses may not refer to terms in
 73602     ** outer queries 
 73603     */
 73604     sNC.pNext = 0;
 73605     sNC.ncFlags |= NC_AllowAgg;
 73607     /* Process the ORDER BY clause for singleton SELECT statements.
 73608     ** The ORDER BY clause for compounds SELECT statements is handled
 73609     ** below, after all of the result-sets for all of the elements of
 73610     ** the compound have been resolved.
 73611     */
 73612     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
 73613       return WRC_Abort;
 73615     if( db->mallocFailed ){
 73616       return WRC_Abort;
 73619     /* Resolve the GROUP BY clause.  At the same time, make sure 
 73620     ** the GROUP BY clause does not contain aggregate functions.
 73621     */
 73622     if( pGroupBy ){
 73623       struct ExprList_item *pItem;
 73625       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
 73626         return WRC_Abort;
 73628       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
 73629         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
 73630           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
 73631               "the GROUP BY clause");
 73632           return WRC_Abort;
 73637     /* Advance to the next term of the compound
 73638     */
 73639     p = p->pPrior;
 73640     nCompound++;
 73643   /* Resolve the ORDER BY on a compound SELECT after all terms of
 73644   ** the compound have been resolved.
 73645   */
 73646   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
 73647     return WRC_Abort;
 73650   return WRC_Prune;
 73653 /*
 73654 ** This routine walks an expression tree and resolves references to
 73655 ** table columns and result-set columns.  At the same time, do error
 73656 ** checking on function usage and set a flag if any aggregate functions
 73657 ** are seen.
 73658 **
 73659 ** To resolve table columns references we look for nodes (or subtrees) of the 
 73660 ** form X.Y.Z or Y.Z or just Z where
 73661 **
 73662 **      X:   The name of a database.  Ex:  "main" or "temp" or
 73663 **           the symbolic name assigned to an ATTACH-ed database.
 73664 **
 73665 **      Y:   The name of a table in a FROM clause.  Or in a trigger
 73666 **           one of the special names "old" or "new".
 73667 **
 73668 **      Z:   The name of a column in table Y.
 73669 **
 73670 ** The node at the root of the subtree is modified as follows:
 73671 **
 73672 **    Expr.op        Changed to TK_COLUMN
 73673 **    Expr.pTab      Points to the Table object for X.Y
 73674 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
 73675 **    Expr.iTable    The VDBE cursor number for X.Y
 73676 **
 73677 **
 73678 ** To resolve result-set references, look for expression nodes of the
 73679 ** form Z (with no X and Y prefix) where the Z matches the right-hand
 73680 ** size of an AS clause in the result-set of a SELECT.  The Z expression
 73681 ** is replaced by a copy of the left-hand side of the result-set expression.
 73682 ** Table-name and function resolution occurs on the substituted expression
 73683 ** tree.  For example, in:
 73684 **
 73685 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
 73686 **
 73687 ** The "x" term of the order by is replaced by "a+b" to render:
 73688 **
 73689 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
 73690 **
 73691 ** Function calls are checked to make sure that the function is 
 73692 ** defined and that the correct number of arguments are specified.
 73693 ** If the function is an aggregate function, then the NC_HasAgg flag is
 73694 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
 73695 ** If an expression contains aggregate functions then the EP_Agg
 73696 ** property on the expression is set.
 73697 **
 73698 ** An error message is left in pParse if anything is amiss.  The number
 73699 ** if errors is returned.
 73700 */
 73701 SQLITE_PRIVATE int sqlite3ResolveExprNames( 
 73702   NameContext *pNC,       /* Namespace to resolve expressions in. */
 73703   Expr *pExpr             /* The expression to be analyzed. */
 73704 ){
 73705   u8 savedHasAgg;
 73706   Walker w;
 73708   if( pExpr==0 ) return 0;
 73709 #if SQLITE_MAX_EXPR_DEPTH>0
 73711     Parse *pParse = pNC->pParse;
 73712     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
 73713       return 1;
 73715     pParse->nHeight += pExpr->nHeight;
 73717 #endif
 73718   savedHasAgg = pNC->ncFlags & NC_HasAgg;
 73719   pNC->ncFlags &= ~NC_HasAgg;
 73720   w.xExprCallback = resolveExprStep;
 73721   w.xSelectCallback = resolveSelectStep;
 73722   w.pParse = pNC->pParse;
 73723   w.u.pNC = pNC;
 73724   sqlite3WalkExpr(&w, pExpr);
 73725 #if SQLITE_MAX_EXPR_DEPTH>0
 73726   pNC->pParse->nHeight -= pExpr->nHeight;
 73727 #endif
 73728   if( pNC->nErr>0 || w.pParse->nErr>0 ){
 73729     ExprSetProperty(pExpr, EP_Error);
 73731   if( pNC->ncFlags & NC_HasAgg ){
 73732     ExprSetProperty(pExpr, EP_Agg);
 73733   }else if( savedHasAgg ){
 73734     pNC->ncFlags |= NC_HasAgg;
 73736   return ExprHasProperty(pExpr, EP_Error);
 73740 /*
 73741 ** Resolve all names in all expressions of a SELECT and in all
 73742 ** decendents of the SELECT, including compounds off of p->pPrior,
 73743 ** subqueries in expressions, and subqueries used as FROM clause
 73744 ** terms.
 73745 **
 73746 ** See sqlite3ResolveExprNames() for a description of the kinds of
 73747 ** transformations that occur.
 73748 **
 73749 ** All SELECT statements should have been expanded using
 73750 ** sqlite3SelectExpand() prior to invoking this routine.
 73751 */
 73752 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
 73753   Parse *pParse,         /* The parser context */
 73754   Select *p,             /* The SELECT statement being coded. */
 73755   NameContext *pOuterNC  /* Name context for parent SELECT statement */
 73756 ){
 73757   Walker w;
 73759   assert( p!=0 );
 73760   w.xExprCallback = resolveExprStep;
 73761   w.xSelectCallback = resolveSelectStep;
 73762   w.pParse = pParse;
 73763   w.u.pNC = pOuterNC;
 73764   sqlite3WalkSelect(&w, p);
 73767 /************** End of resolve.c *********************************************/
 73768 /************** Begin file expr.c ********************************************/
 73769 /*
 73770 ** 2001 September 15
 73771 **
 73772 ** The author disclaims copyright to this source code.  In place of
 73773 ** a legal notice, here is a blessing:
 73774 **
 73775 **    May you do good and not evil.
 73776 **    May you find forgiveness for yourself and forgive others.
 73777 **    May you share freely, never taking more than you give.
 73778 **
 73779 *************************************************************************
 73780 ** This file contains routines used for analyzing expressions and
 73781 ** for generating VDBE code that evaluates expressions in SQLite.
 73782 */
 73784 /*
 73785 ** Return the 'affinity' of the expression pExpr if any.
 73786 **
 73787 ** If pExpr is a column, a reference to a column via an 'AS' alias,
 73788 ** or a sub-select with a column as the return value, then the 
 73789 ** affinity of that column is returned. Otherwise, 0x00 is returned,
 73790 ** indicating no affinity for the expression.
 73791 **
 73792 ** i.e. the WHERE clause expresssions in the following statements all
 73793 ** have an affinity:
 73794 **
 73795 ** CREATE TABLE t1(a);
 73796 ** SELECT * FROM t1 WHERE a;
 73797 ** SELECT a AS b FROM t1 WHERE b;
 73798 ** SELECT * FROM t1 WHERE (select a from t1);
 73799 */
 73800 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
 73801   int op;
 73802   pExpr = sqlite3ExprSkipCollate(pExpr);
 73803   op = pExpr->op;
 73804   if( op==TK_SELECT ){
 73805     assert( pExpr->flags&EP_xIsSelect );
 73806     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 73808 #ifndef SQLITE_OMIT_CAST
 73809   if( op==TK_CAST ){
 73810     assert( !ExprHasProperty(pExpr, EP_IntValue) );
 73811     return sqlite3AffinityType(pExpr->u.zToken);
 73813 #endif
 73814   if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
 73815    && pExpr->pTab!=0
 73816   ){
 73817     /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
 73818     ** a TK_COLUMN but was previously evaluated and cached in a register */
 73819     int j = pExpr->iColumn;
 73820     if( j<0 ) return SQLITE_AFF_INTEGER;
 73821     assert( pExpr->pTab && j<pExpr->pTab->nCol );
 73822     return pExpr->pTab->aCol[j].affinity;
 73824   return pExpr->affinity;
 73827 /*
 73828 ** Set the collating sequence for expression pExpr to be the collating
 73829 ** sequence named by pToken.   Return a pointer to a new Expr node that
 73830 ** implements the COLLATE operator.
 73831 **
 73832 ** If a memory allocation error occurs, that fact is recorded in pParse->db
 73833 ** and the pExpr parameter is returned unchanged.
 73834 */
 73835 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
 73836   if( pCollName->n>0 ){
 73837     Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
 73838     if( pNew ){
 73839       pNew->pLeft = pExpr;
 73840       pNew->flags |= EP_Collate;
 73841       pExpr = pNew;
 73844   return pExpr;
 73846 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
 73847   Token s;
 73848   assert( zC!=0 );
 73849   s.z = zC;
 73850   s.n = sqlite3Strlen30(s.z);
 73851   return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
 73854 /*
 73855 ** Skip over any TK_COLLATE and/or TK_AS operators at the root of
 73856 ** an expression.
 73857 */
 73858 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
 73859   while( pExpr && (pExpr->op==TK_COLLATE || pExpr->op==TK_AS) ){
 73860     pExpr = pExpr->pLeft;
 73862   return pExpr;
 73865 /*
 73866 ** Return the collation sequence for the expression pExpr. If
 73867 ** there is no defined collating sequence, return NULL.
 73868 **
 73869 ** The collating sequence might be determined by a COLLATE operator
 73870 ** or by the presence of a column with a defined collating sequence.
 73871 ** COLLATE operators take first precedence.  Left operands take
 73872 ** precedence over right operands.
 73873 */
 73874 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 73875   sqlite3 *db = pParse->db;
 73876   CollSeq *pColl = 0;
 73877   Expr *p = pExpr;
 73878   while( p ){
 73879     int op = p->op;
 73880     if( op==TK_CAST || op==TK_UPLUS ){
 73881       p = p->pLeft;
 73882       continue;
 73884     assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
 73885     if( op==TK_COLLATE ){
 73886       if( db->init.busy ){
 73887         /* Do not report errors when parsing while the schema */
 73888         pColl = sqlite3FindCollSeq(db, ENC(db), p->u.zToken, 0);
 73889       }else{
 73890         pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
 73892       break;
 73894     if( p->pTab!=0
 73895      && (op==TK_AGG_COLUMN || op==TK_COLUMN
 73896           || op==TK_REGISTER || op==TK_TRIGGER)
 73897     ){
 73898       /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
 73899       ** a TK_COLUMN but was previously evaluated and cached in a register */
 73900       int j = p->iColumn;
 73901       if( j>=0 ){
 73902         const char *zColl = p->pTab->aCol[j].zColl;
 73903         pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 73905       break;
 73907     if( p->flags & EP_Collate ){
 73908       if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
 73909         p = p->pLeft;
 73910       }else{
 73911         p = p->pRight;
 73913     }else{
 73914       break;
 73917   if( sqlite3CheckCollSeq(pParse, pColl) ){ 
 73918     pColl = 0;
 73920   return pColl;
 73923 /*
 73924 ** pExpr is an operand of a comparison operator.  aff2 is the
 73925 ** type affinity of the other operand.  This routine returns the
 73926 ** type affinity that should be used for the comparison operator.
 73927 */
 73928 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
 73929   char aff1 = sqlite3ExprAffinity(pExpr);
 73930   if( aff1 && aff2 ){
 73931     /* Both sides of the comparison are columns. If one has numeric
 73932     ** affinity, use that. Otherwise use no affinity.
 73933     */
 73934     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
 73935       return SQLITE_AFF_NUMERIC;
 73936     }else{
 73937       return SQLITE_AFF_NONE;
 73939   }else if( !aff1 && !aff2 ){
 73940     /* Neither side of the comparison is a column.  Compare the
 73941     ** results directly.
 73942     */
 73943     return SQLITE_AFF_NONE;
 73944   }else{
 73945     /* One side is a column, the other is not. Use the columns affinity. */
 73946     assert( aff1==0 || aff2==0 );
 73947     return (aff1 + aff2);
 73951 /*
 73952 ** pExpr is a comparison operator.  Return the type affinity that should
 73953 ** be applied to both operands prior to doing the comparison.
 73954 */
 73955 static char comparisonAffinity(Expr *pExpr){
 73956   char aff;
 73957   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
 73958           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
 73959           pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 73960   assert( pExpr->pLeft );
 73961   aff = sqlite3ExprAffinity(pExpr->pLeft);
 73962   if( pExpr->pRight ){
 73963     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 73964   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 73965     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 73966   }else if( !aff ){
 73967     aff = SQLITE_AFF_NONE;
 73969   return aff;
 73972 /*
 73973 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
 73974 ** idx_affinity is the affinity of an indexed column. Return true
 73975 ** if the index with affinity idx_affinity may be used to implement
 73976 ** the comparison in pExpr.
 73977 */
 73978 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
 73979   char aff = comparisonAffinity(pExpr);
 73980   switch( aff ){
 73981     case SQLITE_AFF_NONE:
 73982       return 1;
 73983     case SQLITE_AFF_TEXT:
 73984       return idx_affinity==SQLITE_AFF_TEXT;
 73985     default:
 73986       return sqlite3IsNumericAffinity(idx_affinity);
 73990 /*
 73991 ** Return the P5 value that should be used for a binary comparison
 73992 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
 73993 */
 73994 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
 73995   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
 73996   aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
 73997   return aff;
 74000 /*
 74001 ** Return a pointer to the collation sequence that should be used by
 74002 ** a binary comparison operator comparing pLeft and pRight.
 74003 **
 74004 ** If the left hand expression has a collating sequence type, then it is
 74005 ** used. Otherwise the collation sequence for the right hand expression
 74006 ** is used, or the default (BINARY) if neither expression has a collating
 74007 ** type.
 74008 **
 74009 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
 74010 ** it is not considered.
 74011 */
 74012 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
 74013   Parse *pParse, 
 74014   Expr *pLeft, 
 74015   Expr *pRight
 74016 ){
 74017   CollSeq *pColl;
 74018   assert( pLeft );
 74019   if( pLeft->flags & EP_Collate ){
 74020     pColl = sqlite3ExprCollSeq(pParse, pLeft);
 74021   }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
 74022     pColl = sqlite3ExprCollSeq(pParse, pRight);
 74023   }else{
 74024     pColl = sqlite3ExprCollSeq(pParse, pLeft);
 74025     if( !pColl ){
 74026       pColl = sqlite3ExprCollSeq(pParse, pRight);
 74029   return pColl;
 74032 /*
 74033 ** Generate code for a comparison operator.
 74034 */
 74035 static int codeCompare(
 74036   Parse *pParse,    /* The parsing (and code generating) context */
 74037   Expr *pLeft,      /* The left operand */
 74038   Expr *pRight,     /* The right operand */
 74039   int opcode,       /* The comparison opcode */
 74040   int in1, int in2, /* Register holding operands */
 74041   int dest,         /* Jump here if true.  */
 74042   int jumpIfNull    /* If true, jump if either operand is NULL */
 74043 ){
 74044   int p5;
 74045   int addr;
 74046   CollSeq *p4;
 74048   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
 74049   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
 74050   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
 74051                            (void*)p4, P4_COLLSEQ);
 74052   sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
 74053   return addr;
 74056 #if SQLITE_MAX_EXPR_DEPTH>0
 74057 /*
 74058 ** Check that argument nHeight is less than or equal to the maximum
 74059 ** expression depth allowed. If it is not, leave an error message in
 74060 ** pParse.
 74061 */
 74062 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
 74063   int rc = SQLITE_OK;
 74064   int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
 74065   if( nHeight>mxHeight ){
 74066     sqlite3ErrorMsg(pParse, 
 74067        "Expression tree is too large (maximum depth %d)", mxHeight
 74068     );
 74069     rc = SQLITE_ERROR;
 74071   return rc;
 74074 /* The following three functions, heightOfExpr(), heightOfExprList()
 74075 ** and heightOfSelect(), are used to determine the maximum height
 74076 ** of any expression tree referenced by the structure passed as the
 74077 ** first argument.
 74078 **
 74079 ** If this maximum height is greater than the current value pointed
 74080 ** to by pnHeight, the second parameter, then set *pnHeight to that
 74081 ** value.
 74082 */
 74083 static void heightOfExpr(Expr *p, int *pnHeight){
 74084   if( p ){
 74085     if( p->nHeight>*pnHeight ){
 74086       *pnHeight = p->nHeight;
 74090 static void heightOfExprList(ExprList *p, int *pnHeight){
 74091   if( p ){
 74092     int i;
 74093     for(i=0; i<p->nExpr; i++){
 74094       heightOfExpr(p->a[i].pExpr, pnHeight);
 74098 static void heightOfSelect(Select *p, int *pnHeight){
 74099   if( p ){
 74100     heightOfExpr(p->pWhere, pnHeight);
 74101     heightOfExpr(p->pHaving, pnHeight);
 74102     heightOfExpr(p->pLimit, pnHeight);
 74103     heightOfExpr(p->pOffset, pnHeight);
 74104     heightOfExprList(p->pEList, pnHeight);
 74105     heightOfExprList(p->pGroupBy, pnHeight);
 74106     heightOfExprList(p->pOrderBy, pnHeight);
 74107     heightOfSelect(p->pPrior, pnHeight);
 74111 /*
 74112 ** Set the Expr.nHeight variable in the structure passed as an 
 74113 ** argument. An expression with no children, Expr.pList or 
 74114 ** Expr.pSelect member has a height of 1. Any other expression
 74115 ** has a height equal to the maximum height of any other 
 74116 ** referenced Expr plus one.
 74117 */
 74118 static void exprSetHeight(Expr *p){
 74119   int nHeight = 0;
 74120   heightOfExpr(p->pLeft, &nHeight);
 74121   heightOfExpr(p->pRight, &nHeight);
 74122   if( ExprHasProperty(p, EP_xIsSelect) ){
 74123     heightOfSelect(p->x.pSelect, &nHeight);
 74124   }else{
 74125     heightOfExprList(p->x.pList, &nHeight);
 74127   p->nHeight = nHeight + 1;
 74130 /*
 74131 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
 74132 ** the height is greater than the maximum allowed expression depth,
 74133 ** leave an error in pParse.
 74134 */
 74135 SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
 74136   exprSetHeight(p);
 74137   sqlite3ExprCheckHeight(pParse, p->nHeight);
 74140 /*
 74141 ** Return the maximum height of any expression tree referenced
 74142 ** by the select statement passed as an argument.
 74143 */
 74144 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
 74145   int nHeight = 0;
 74146   heightOfSelect(p, &nHeight);
 74147   return nHeight;
 74149 #else
 74150   #define exprSetHeight(y)
 74151 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
 74153 /*
 74154 ** This routine is the core allocator for Expr nodes.
 74155 **
 74156 ** Construct a new expression node and return a pointer to it.  Memory
 74157 ** for this node and for the pToken argument is a single allocation
 74158 ** obtained from sqlite3DbMalloc().  The calling function
 74159 ** is responsible for making sure the node eventually gets freed.
 74160 **
 74161 ** If dequote is true, then the token (if it exists) is dequoted.
 74162 ** If dequote is false, no dequoting is performance.  The deQuote
 74163 ** parameter is ignored if pToken is NULL or if the token does not
 74164 ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
 74165 ** then the EP_DblQuoted flag is set on the expression node.
 74166 **
 74167 ** Special case:  If op==TK_INTEGER and pToken points to a string that
 74168 ** can be translated into a 32-bit integer, then the token is not
 74169 ** stored in u.zToken.  Instead, the integer values is written
 74170 ** into u.iValue and the EP_IntValue flag is set.  No extra storage
 74171 ** is allocated to hold the integer text and the dequote flag is ignored.
 74172 */
 74173 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
 74174   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
 74175   int op,                 /* Expression opcode */
 74176   const Token *pToken,    /* Token argument.  Might be NULL */
 74177   int dequote             /* True to dequote */
 74178 ){
 74179   Expr *pNew;
 74180   int nExtra = 0;
 74181   int iValue = 0;
 74183   if( pToken ){
 74184     if( op!=TK_INTEGER || pToken->z==0
 74185           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
 74186       nExtra = pToken->n+1;
 74187       assert( iValue>=0 );
 74190   pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
 74191   if( pNew ){
 74192     pNew->op = (u8)op;
 74193     pNew->iAgg = -1;
 74194     if( pToken ){
 74195       if( nExtra==0 ){
 74196         pNew->flags |= EP_IntValue;
 74197         pNew->u.iValue = iValue;
 74198       }else{
 74199         int c;
 74200         pNew->u.zToken = (char*)&pNew[1];
 74201         assert( pToken->z!=0 || pToken->n==0 );
 74202         if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
 74203         pNew->u.zToken[pToken->n] = 0;
 74204         if( dequote && nExtra>=3 
 74205              && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
 74206           sqlite3Dequote(pNew->u.zToken);
 74207           if( c=='"' ) pNew->flags |= EP_DblQuoted;
 74211 #if SQLITE_MAX_EXPR_DEPTH>0
 74212     pNew->nHeight = 1;
 74213 #endif  
 74215   return pNew;
 74218 /*
 74219 ** Allocate a new expression node from a zero-terminated token that has
 74220 ** already been dequoted.
 74221 */
 74222 SQLITE_PRIVATE Expr *sqlite3Expr(
 74223   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
 74224   int op,                 /* Expression opcode */
 74225   const char *zToken      /* Token argument.  Might be NULL */
 74226 ){
 74227   Token x;
 74228   x.z = zToken;
 74229   x.n = zToken ? sqlite3Strlen30(zToken) : 0;
 74230   return sqlite3ExprAlloc(db, op, &x, 0);
 74233 /*
 74234 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
 74235 **
 74236 ** If pRoot==NULL that means that a memory allocation error has occurred.
 74237 ** In that case, delete the subtrees pLeft and pRight.
 74238 */
 74239 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
 74240   sqlite3 *db,
 74241   Expr *pRoot,
 74242   Expr *pLeft,
 74243   Expr *pRight
 74244 ){
 74245   if( pRoot==0 ){
 74246     assert( db->mallocFailed );
 74247     sqlite3ExprDelete(db, pLeft);
 74248     sqlite3ExprDelete(db, pRight);
 74249   }else{
 74250     if( pRight ){
 74251       pRoot->pRight = pRight;
 74252       pRoot->flags |= EP_Collate & pRight->flags;
 74254     if( pLeft ){
 74255       pRoot->pLeft = pLeft;
 74256       pRoot->flags |= EP_Collate & pLeft->flags;
 74258     exprSetHeight(pRoot);
 74262 /*
 74263 ** Allocate a Expr node which joins as many as two subtrees.
 74264 **
 74265 ** One or both of the subtrees can be NULL.  Return a pointer to the new
 74266 ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
 74267 ** free the subtrees and return NULL.
 74268 */
 74269 SQLITE_PRIVATE Expr *sqlite3PExpr(
 74270   Parse *pParse,          /* Parsing context */
 74271   int op,                 /* Expression opcode */
 74272   Expr *pLeft,            /* Left operand */
 74273   Expr *pRight,           /* Right operand */
 74274   const Token *pToken     /* Argument token */
 74275 ){
 74276   Expr *p;
 74277   if( op==TK_AND && pLeft && pRight ){
 74278     /* Take advantage of short-circuit false optimization for AND */
 74279     p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
 74280   }else{
 74281     p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
 74282     sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
 74284   if( p ) {
 74285     sqlite3ExprCheckHeight(pParse, p->nHeight);
 74287   return p;
 74290 /*
 74291 ** Return 1 if an expression must be FALSE in all cases and 0 if the
 74292 ** expression might be true.  This is an optimization.  If is OK to
 74293 ** return 0 here even if the expression really is always false (a 
 74294 ** false negative).  But it is a bug to return 1 if the expression
 74295 ** might be true in some rare circumstances (a false positive.)
 74296 **
 74297 ** Note that if the expression is part of conditional for a
 74298 ** LEFT JOIN, then we cannot determine at compile-time whether or not
 74299 ** is it true or false, so always return 0.
 74300 */
 74301 static int exprAlwaysFalse(Expr *p){
 74302   int v = 0;
 74303   if( ExprHasProperty(p, EP_FromJoin) ) return 0;
 74304   if( !sqlite3ExprIsInteger(p, &v) ) return 0;
 74305   return v==0;
 74308 /*
 74309 ** Join two expressions using an AND operator.  If either expression is
 74310 ** NULL, then just return the other expression.
 74311 **
 74312 ** If one side or the other of the AND is known to be false, then instead
 74313 ** of returning an AND expression, just return a constant expression with
 74314 ** a value of false.
 74315 */
 74316 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
 74317   if( pLeft==0 ){
 74318     return pRight;
 74319   }else if( pRight==0 ){
 74320     return pLeft;
 74321   }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
 74322     sqlite3ExprDelete(db, pLeft);
 74323     sqlite3ExprDelete(db, pRight);
 74324     return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
 74325   }else{
 74326     Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
 74327     sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
 74328     return pNew;
 74332 /*
 74333 ** Construct a new expression node for a function with multiple
 74334 ** arguments.
 74335 */
 74336 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
 74337   Expr *pNew;
 74338   sqlite3 *db = pParse->db;
 74339   assert( pToken );
 74340   pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
 74341   if( pNew==0 ){
 74342     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
 74343     return 0;
 74345   pNew->x.pList = pList;
 74346   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
 74347   sqlite3ExprSetHeight(pParse, pNew);
 74348   return pNew;
 74351 /*
 74352 ** Assign a variable number to an expression that encodes a wildcard
 74353 ** in the original SQL statement.  
 74354 **
 74355 ** Wildcards consisting of a single "?" are assigned the next sequential
 74356 ** variable number.
 74357 **
 74358 ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
 74359 ** sure "nnn" is not too be to avoid a denial of service attack when
 74360 ** the SQL statement comes from an external source.
 74361 **
 74362 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
 74363 ** as the previous instance of the same wildcard.  Or if this is the first
 74364 ** instance of the wildcard, the next sequenial variable number is
 74365 ** assigned.
 74366 */
 74367 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
 74368   sqlite3 *db = pParse->db;
 74369   const char *z;
 74371   if( pExpr==0 ) return;
 74372   assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
 74373   z = pExpr->u.zToken;
 74374   assert( z!=0 );
 74375   assert( z[0]!=0 );
 74376   if( z[1]==0 ){
 74377     /* Wildcard of the form "?".  Assign the next variable number */
 74378     assert( z[0]=='?' );
 74379     pExpr->iColumn = (ynVar)(++pParse->nVar);
 74380   }else{
 74381     ynVar x = 0;
 74382     u32 n = sqlite3Strlen30(z);
 74383     if( z[0]=='?' ){
 74384       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
 74385       ** use it as the variable number */
 74386       i64 i;
 74387       int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
 74388       pExpr->iColumn = x = (ynVar)i;
 74389       testcase( i==0 );
 74390       testcase( i==1 );
 74391       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
 74392       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
 74393       if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 74394         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
 74395             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
 74396         x = 0;
 74398       if( i>pParse->nVar ){
 74399         pParse->nVar = (int)i;
 74401     }else{
 74402       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
 74403       ** number as the prior appearance of the same name, or if the name
 74404       ** has never appeared before, reuse the same variable number
 74405       */
 74406       ynVar i;
 74407       for(i=0; i<pParse->nzVar; i++){
 74408         if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
 74409           pExpr->iColumn = x = (ynVar)i+1;
 74410           break;
 74413       if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
 74415     if( x>0 ){
 74416       if( x>pParse->nzVar ){
 74417         char **a;
 74418         a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
 74419         if( a==0 ) return;  /* Error reported through db->mallocFailed */
 74420         pParse->azVar = a;
 74421         memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
 74422         pParse->nzVar = x;
 74424       if( z[0]!='?' || pParse->azVar[x-1]==0 ){
 74425         sqlite3DbFree(db, pParse->azVar[x-1]);
 74426         pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
 74430   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 74431     sqlite3ErrorMsg(pParse, "too many SQL variables");
 74435 /*
 74436 ** Recursively delete an expression tree.
 74437 */
 74438 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
 74439   if( p==0 ) return;
 74440   /* Sanity check: Assert that the IntValue is non-negative if it exists */
 74441   assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
 74442   if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
 74443     sqlite3ExprDelete(db, p->pLeft);
 74444     sqlite3ExprDelete(db, p->pRight);
 74445     if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
 74446       sqlite3DbFree(db, p->u.zToken);
 74448     if( ExprHasProperty(p, EP_xIsSelect) ){
 74449       sqlite3SelectDelete(db, p->x.pSelect);
 74450     }else{
 74451       sqlite3ExprListDelete(db, p->x.pList);
 74454   if( !ExprHasProperty(p, EP_Static) ){
 74455     sqlite3DbFree(db, p);
 74459 /*
 74460 ** Return the number of bytes allocated for the expression structure 
 74461 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
 74462 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
 74463 */
 74464 static int exprStructSize(Expr *p){
 74465   if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
 74466   if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
 74467   return EXPR_FULLSIZE;
 74470 /*
 74471 ** The dupedExpr*Size() routines each return the number of bytes required
 74472 ** to store a copy of an expression or expression tree.  They differ in
 74473 ** how much of the tree is measured.
 74474 **
 74475 **     dupedExprStructSize()     Size of only the Expr structure 
 74476 **     dupedExprNodeSize()       Size of Expr + space for token
 74477 **     dupedExprSize()           Expr + token + subtree components
 74478 **
 74479 ***************************************************************************
 74480 **
 74481 ** The dupedExprStructSize() function returns two values OR-ed together:  
 74482 ** (1) the space required for a copy of the Expr structure only and 
 74483 ** (2) the EP_xxx flags that indicate what the structure size should be.
 74484 ** The return values is always one of:
 74485 **
 74486 **      EXPR_FULLSIZE
 74487 **      EXPR_REDUCEDSIZE   | EP_Reduced
 74488 **      EXPR_TOKENONLYSIZE | EP_TokenOnly
 74489 **
 74490 ** The size of the structure can be found by masking the return value
 74491 ** of this routine with 0xfff.  The flags can be found by masking the
 74492 ** return value with EP_Reduced|EP_TokenOnly.
 74493 **
 74494 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
 74495 ** (unreduced) Expr objects as they or originally constructed by the parser.
 74496 ** During expression analysis, extra information is computed and moved into
 74497 ** later parts of teh Expr object and that extra information might get chopped
 74498 ** off if the expression is reduced.  Note also that it does not work to
 74499 ** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
 74500 ** to reduce a pristine expression tree from the parser.  The implementation
 74501 ** of dupedExprStructSize() contain multiple assert() statements that attempt
 74502 ** to enforce this constraint.
 74503 */
 74504 static int dupedExprStructSize(Expr *p, int flags){
 74505   int nSize;
 74506   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
 74507   if( 0==(flags&EXPRDUP_REDUCE) ){
 74508     nSize = EXPR_FULLSIZE;
 74509   }else{
 74510     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
 74511     assert( !ExprHasProperty(p, EP_FromJoin) ); 
 74512     assert( (p->flags2 & EP2_MallocedToken)==0 );
 74513     assert( (p->flags2 & EP2_Irreducible)==0 );
 74514     if( p->pLeft || p->pRight || p->x.pList ){
 74515       nSize = EXPR_REDUCEDSIZE | EP_Reduced;
 74516     }else{
 74517       nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
 74520   return nSize;
 74523 /*
 74524 ** This function returns the space in bytes required to store the copy 
 74525 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
 74526 ** string is defined.)
 74527 */
 74528 static int dupedExprNodeSize(Expr *p, int flags){
 74529   int nByte = dupedExprStructSize(p, flags) & 0xfff;
 74530   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 74531     nByte += sqlite3Strlen30(p->u.zToken)+1;
 74533   return ROUND8(nByte);
 74536 /*
 74537 ** Return the number of bytes required to create a duplicate of the 
 74538 ** expression passed as the first argument. The second argument is a
 74539 ** mask containing EXPRDUP_XXX flags.
 74540 **
 74541 ** The value returned includes space to create a copy of the Expr struct
 74542 ** itself and the buffer referred to by Expr.u.zToken, if any.
 74543 **
 74544 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 
 74545 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
 74546 ** and Expr.pRight variables (but not for any structures pointed to or 
 74547 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
 74548 */
 74549 static int dupedExprSize(Expr *p, int flags){
 74550   int nByte = 0;
 74551   if( p ){
 74552     nByte = dupedExprNodeSize(p, flags);
 74553     if( flags&EXPRDUP_REDUCE ){
 74554       nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
 74557   return nByte;
 74560 /*
 74561 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
 74562 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
 74563 ** to store the copy of expression p, the copies of p->u.zToken
 74564 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
 74565 ** if any. Before returning, *pzBuffer is set to the first byte passed the
 74566 ** portion of the buffer copied into by this function.
 74567 */
 74568 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
 74569   Expr *pNew = 0;                      /* Value to return */
 74570   if( p ){
 74571     const int isReduced = (flags&EXPRDUP_REDUCE);
 74572     u8 *zAlloc;
 74573     u32 staticFlag = 0;
 74575     assert( pzBuffer==0 || isReduced );
 74577     /* Figure out where to write the new Expr structure. */
 74578     if( pzBuffer ){
 74579       zAlloc = *pzBuffer;
 74580       staticFlag = EP_Static;
 74581     }else{
 74582       zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
 74584     pNew = (Expr *)zAlloc;
 74586     if( pNew ){
 74587       /* Set nNewSize to the size allocated for the structure pointed to
 74588       ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
 74589       ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
 74590       ** by the copy of the p->u.zToken string (if any).
 74591       */
 74592       const unsigned nStructSize = dupedExprStructSize(p, flags);
 74593       const int nNewSize = nStructSize & 0xfff;
 74594       int nToken;
 74595       if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 74596         nToken = sqlite3Strlen30(p->u.zToken) + 1;
 74597       }else{
 74598         nToken = 0;
 74600       if( isReduced ){
 74601         assert( ExprHasProperty(p, EP_Reduced)==0 );
 74602         memcpy(zAlloc, p, nNewSize);
 74603       }else{
 74604         int nSize = exprStructSize(p);
 74605         memcpy(zAlloc, p, nSize);
 74606         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
 74609       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
 74610       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
 74611       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
 74612       pNew->flags |= staticFlag;
 74614       /* Copy the p->u.zToken string, if any. */
 74615       if( nToken ){
 74616         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
 74617         memcpy(zToken, p->u.zToken, nToken);
 74620       if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
 74621         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
 74622         if( ExprHasProperty(p, EP_xIsSelect) ){
 74623           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
 74624         }else{
 74625           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
 74629       /* Fill in pNew->pLeft and pNew->pRight. */
 74630       if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
 74631         zAlloc += dupedExprNodeSize(p, flags);
 74632         if( ExprHasProperty(pNew, EP_Reduced) ){
 74633           pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
 74634           pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
 74636         if( pzBuffer ){
 74637           *pzBuffer = zAlloc;
 74639       }else{
 74640         pNew->flags2 = 0;
 74641         if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
 74642           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
 74643           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
 74649   return pNew;
 74652 /*
 74653 ** The following group of routines make deep copies of expressions,
 74654 ** expression lists, ID lists, and select statements.  The copies can
 74655 ** be deleted (by being passed to their respective ...Delete() routines)
 74656 ** without effecting the originals.
 74657 **
 74658 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
 74659 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
 74660 ** by subsequent calls to sqlite*ListAppend() routines.
 74661 **
 74662 ** Any tables that the SrcList might point to are not duplicated.
 74663 **
 74664 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
 74665 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
 74666 ** truncated version of the usual Expr structure that will be stored as
 74667 ** part of the in-memory representation of the database schema.
 74668 */
 74669 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
 74670   return exprDup(db, p, flags, 0);
 74672 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
 74673   ExprList *pNew;
 74674   struct ExprList_item *pItem, *pOldItem;
 74675   int i;
 74676   if( p==0 ) return 0;
 74677   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
 74678   if( pNew==0 ) return 0;
 74679   pNew->iECursor = 0;
 74680   pNew->nExpr = i = p->nExpr;
 74681   if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
 74682   pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
 74683   if( pItem==0 ){
 74684     sqlite3DbFree(db, pNew);
 74685     return 0;
 74687   pOldItem = p->a;
 74688   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
 74689     Expr *pOldExpr = pOldItem->pExpr;
 74690     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
 74691     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 74692     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
 74693     pItem->sortOrder = pOldItem->sortOrder;
 74694     pItem->done = 0;
 74695     pItem->iOrderByCol = pOldItem->iOrderByCol;
 74696     pItem->iAlias = pOldItem->iAlias;
 74698   return pNew;
 74701 /*
 74702 ** If cursors, triggers, views and subqueries are all omitted from
 74703 ** the build, then none of the following routines, except for 
 74704 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
 74705 ** called with a NULL argument.
 74706 */
 74707 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
 74708  || !defined(SQLITE_OMIT_SUBQUERY)
 74709 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
 74710   SrcList *pNew;
 74711   int i;
 74712   int nByte;
 74713   if( p==0 ) return 0;
 74714   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
 74715   pNew = sqlite3DbMallocRaw(db, nByte );
 74716   if( pNew==0 ) return 0;
 74717   pNew->nSrc = pNew->nAlloc = p->nSrc;
 74718   for(i=0; i<p->nSrc; i++){
 74719     struct SrcList_item *pNewItem = &pNew->a[i];
 74720     struct SrcList_item *pOldItem = &p->a[i];
 74721     Table *pTab;
 74722     pNewItem->pSchema = pOldItem->pSchema;
 74723     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
 74724     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 74725     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
 74726     pNewItem->jointype = pOldItem->jointype;
 74727     pNewItem->iCursor = pOldItem->iCursor;
 74728     pNewItem->addrFillSub = pOldItem->addrFillSub;
 74729     pNewItem->regReturn = pOldItem->regReturn;
 74730     pNewItem->isCorrelated = pOldItem->isCorrelated;
 74731     pNewItem->viaCoroutine = pOldItem->viaCoroutine;
 74732     pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
 74733     pNewItem->notIndexed = pOldItem->notIndexed;
 74734     pNewItem->pIndex = pOldItem->pIndex;
 74735     pTab = pNewItem->pTab = pOldItem->pTab;
 74736     if( pTab ){
 74737       pTab->nRef++;
 74739     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
 74740     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
 74741     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
 74742     pNewItem->colUsed = pOldItem->colUsed;
 74744   return pNew;
 74746 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
 74747   IdList *pNew;
 74748   int i;
 74749   if( p==0 ) return 0;
 74750   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
 74751   if( pNew==0 ) return 0;
 74752   pNew->nId = p->nId;
 74753   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
 74754   if( pNew->a==0 ){
 74755     sqlite3DbFree(db, pNew);
 74756     return 0;
 74758   /* Note that because the size of the allocation for p->a[] is not
 74759   ** necessarily a power of two, sqlite3IdListAppend() may not be called
 74760   ** on the duplicate created by this function. */
 74761   for(i=0; i<p->nId; i++){
 74762     struct IdList_item *pNewItem = &pNew->a[i];
 74763     struct IdList_item *pOldItem = &p->a[i];
 74764     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 74765     pNewItem->idx = pOldItem->idx;
 74767   return pNew;
 74769 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 74770   Select *pNew, *pPrior;
 74771   if( p==0 ) return 0;
 74772   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
 74773   if( pNew==0 ) return 0;
 74774   pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
 74775   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
 74776   pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
 74777   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
 74778   pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
 74779   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
 74780   pNew->op = p->op;
 74781   pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
 74782   if( pPrior ) pPrior->pNext = pNew;
 74783   pNew->pNext = 0;
 74784   pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 74785   pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 74786   pNew->iLimit = 0;
 74787   pNew->iOffset = 0;
 74788   pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 74789   pNew->pRightmost = 0;
 74790   pNew->addrOpenEphm[0] = -1;
 74791   pNew->addrOpenEphm[1] = -1;
 74792   pNew->addrOpenEphm[2] = -1;
 74793   return pNew;
 74795 #else
 74796 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 74797   assert( p==0 );
 74798   return 0;
 74800 #endif
 74803 /*
 74804 ** Add a new element to the end of an expression list.  If pList is
 74805 ** initially NULL, then create a new expression list.
 74806 **
 74807 ** If a memory allocation error occurs, the entire list is freed and
 74808 ** NULL is returned.  If non-NULL is returned, then it is guaranteed
 74809 ** that the new entry was successfully appended.
 74810 */
 74811 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
 74812   Parse *pParse,          /* Parsing context */
 74813   ExprList *pList,        /* List to which to append. Might be NULL */
 74814   Expr *pExpr             /* Expression to be appended. Might be NULL */
 74815 ){
 74816   sqlite3 *db = pParse->db;
 74817   if( pList==0 ){
 74818     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
 74819     if( pList==0 ){
 74820       goto no_mem;
 74822     pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
 74823     if( pList->a==0 ) goto no_mem;
 74824   }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
 74825     struct ExprList_item *a;
 74826     assert( pList->nExpr>0 );
 74827     a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
 74828     if( a==0 ){
 74829       goto no_mem;
 74831     pList->a = a;
 74833   assert( pList->a!=0 );
 74834   if( 1 ){
 74835     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
 74836     memset(pItem, 0, sizeof(*pItem));
 74837     pItem->pExpr = pExpr;
 74839   return pList;
 74841 no_mem:     
 74842   /* Avoid leaking memory if malloc has failed. */
 74843   sqlite3ExprDelete(db, pExpr);
 74844   sqlite3ExprListDelete(db, pList);
 74845   return 0;
 74848 /*
 74849 ** Set the ExprList.a[].zName element of the most recently added item
 74850 ** on the expression list.
 74851 **
 74852 ** pList might be NULL following an OOM error.  But pName should never be
 74853 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
 74854 ** is set.
 74855 */
 74856 SQLITE_PRIVATE void sqlite3ExprListSetName(
 74857   Parse *pParse,          /* Parsing context */
 74858   ExprList *pList,        /* List to which to add the span. */
 74859   Token *pName,           /* Name to be added */
 74860   int dequote             /* True to cause the name to be dequoted */
 74861 ){
 74862   assert( pList!=0 || pParse->db->mallocFailed!=0 );
 74863   if( pList ){
 74864     struct ExprList_item *pItem;
 74865     assert( pList->nExpr>0 );
 74866     pItem = &pList->a[pList->nExpr-1];
 74867     assert( pItem->zName==0 );
 74868     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
 74869     if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
 74873 /*
 74874 ** Set the ExprList.a[].zSpan element of the most recently added item
 74875 ** on the expression list.
 74876 **
 74877 ** pList might be NULL following an OOM error.  But pSpan should never be
 74878 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
 74879 ** is set.
 74880 */
 74881 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
 74882   Parse *pParse,          /* Parsing context */
 74883   ExprList *pList,        /* List to which to add the span. */
 74884   ExprSpan *pSpan         /* The span to be added */
 74885 ){
 74886   sqlite3 *db = pParse->db;
 74887   assert( pList!=0 || db->mallocFailed!=0 );
 74888   if( pList ){
 74889     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
 74890     assert( pList->nExpr>0 );
 74891     assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
 74892     sqlite3DbFree(db, pItem->zSpan);
 74893     pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
 74894                                     (int)(pSpan->zEnd - pSpan->zStart));
 74898 /*
 74899 ** If the expression list pEList contains more than iLimit elements,
 74900 ** leave an error message in pParse.
 74901 */
 74902 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
 74903   Parse *pParse,
 74904   ExprList *pEList,
 74905   const char *zObject
 74906 ){
 74907   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
 74908   testcase( pEList && pEList->nExpr==mx );
 74909   testcase( pEList && pEList->nExpr==mx+1 );
 74910   if( pEList && pEList->nExpr>mx ){
 74911     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
 74915 /*
 74916 ** Delete an entire expression list.
 74917 */
 74918 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
 74919   int i;
 74920   struct ExprList_item *pItem;
 74921   if( pList==0 ) return;
 74922   assert( pList->a!=0 || pList->nExpr==0 );
 74923   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
 74924     sqlite3ExprDelete(db, pItem->pExpr);
 74925     sqlite3DbFree(db, pItem->zName);
 74926     sqlite3DbFree(db, pItem->zSpan);
 74928   sqlite3DbFree(db, pList->a);
 74929   sqlite3DbFree(db, pList);
 74932 /*
 74933 ** These routines are Walker callbacks.  Walker.u.pi is a pointer
 74934 ** to an integer.  These routines are checking an expression to see
 74935 ** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
 74936 ** not constant.
 74937 **
 74938 ** These callback routines are used to implement the following:
 74939 **
 74940 **     sqlite3ExprIsConstant()
 74941 **     sqlite3ExprIsConstantNotJoin()
 74942 **     sqlite3ExprIsConstantOrFunction()
 74943 **
 74944 */
 74945 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
 74947   /* If pWalker->u.i is 3 then any term of the expression that comes from
 74948   ** the ON or USING clauses of a join disqualifies the expression
 74949   ** from being considered constant. */
 74950   if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
 74951     pWalker->u.i = 0;
 74952     return WRC_Abort;
 74955   switch( pExpr->op ){
 74956     /* Consider functions to be constant if all their arguments are constant
 74957     ** and pWalker->u.i==2 */
 74958     case TK_FUNCTION:
 74959       if( pWalker->u.i==2 ) return 0;
 74960       /* Fall through */
 74961     case TK_ID:
 74962     case TK_COLUMN:
 74963     case TK_AGG_FUNCTION:
 74964     case TK_AGG_COLUMN:
 74965       testcase( pExpr->op==TK_ID );
 74966       testcase( pExpr->op==TK_COLUMN );
 74967       testcase( pExpr->op==TK_AGG_FUNCTION );
 74968       testcase( pExpr->op==TK_AGG_COLUMN );
 74969       pWalker->u.i = 0;
 74970       return WRC_Abort;
 74971     default:
 74972       testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
 74973       testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
 74974       return WRC_Continue;
 74977 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
 74978   UNUSED_PARAMETER(NotUsed);
 74979   pWalker->u.i = 0;
 74980   return WRC_Abort;
 74982 static int exprIsConst(Expr *p, int initFlag){
 74983   Walker w;
 74984   w.u.i = initFlag;
 74985   w.xExprCallback = exprNodeIsConstant;
 74986   w.xSelectCallback = selectNodeIsConstant;
 74987   sqlite3WalkExpr(&w, p);
 74988   return w.u.i;
 74991 /*
 74992 ** Walk an expression tree.  Return 1 if the expression is constant
 74993 ** and 0 if it involves variables or function calls.
 74994 **
 74995 ** For the purposes of this function, a double-quoted string (ex: "abc")
 74996 ** is considered a variable but a single-quoted string (ex: 'abc') is
 74997 ** a constant.
 74998 */
 74999 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
 75000   return exprIsConst(p, 1);
 75003 /*
 75004 ** Walk an expression tree.  Return 1 if the expression is constant
 75005 ** that does no originate from the ON or USING clauses of a join.
 75006 ** Return 0 if it involves variables or function calls or terms from
 75007 ** an ON or USING clause.
 75008 */
 75009 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
 75010   return exprIsConst(p, 3);
 75013 /*
 75014 ** Walk an expression tree.  Return 1 if the expression is constant
 75015 ** or a function call with constant arguments.  Return and 0 if there
 75016 ** are any variables.
 75017 **
 75018 ** For the purposes of this function, a double-quoted string (ex: "abc")
 75019 ** is considered a variable but a single-quoted string (ex: 'abc') is
 75020 ** a constant.
 75021 */
 75022 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
 75023   return exprIsConst(p, 2);
 75026 /*
 75027 ** If the expression p codes a constant integer that is small enough
 75028 ** to fit in a 32-bit integer, return 1 and put the value of the integer
 75029 ** in *pValue.  If the expression is not an integer or if it is too big
 75030 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
 75031 */
 75032 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
 75033   int rc = 0;
 75035   /* If an expression is an integer literal that fits in a signed 32-bit
 75036   ** integer, then the EP_IntValue flag will have already been set */
 75037   assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
 75038            || sqlite3GetInt32(p->u.zToken, &rc)==0 );
 75040   if( p->flags & EP_IntValue ){
 75041     *pValue = p->u.iValue;
 75042     return 1;
 75044   switch( p->op ){
 75045     case TK_UPLUS: {
 75046       rc = sqlite3ExprIsInteger(p->pLeft, pValue);
 75047       break;
 75049     case TK_UMINUS: {
 75050       int v;
 75051       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
 75052         *pValue = -v;
 75053         rc = 1;
 75055       break;
 75057     default: break;
 75059   return rc;
 75062 /*
 75063 ** Return FALSE if there is no chance that the expression can be NULL.
 75064 **
 75065 ** If the expression might be NULL or if the expression is too complex
 75066 ** to tell return TRUE.  
 75067 **
 75068 ** This routine is used as an optimization, to skip OP_IsNull opcodes
 75069 ** when we know that a value cannot be NULL.  Hence, a false positive
 75070 ** (returning TRUE when in fact the expression can never be NULL) might
 75071 ** be a small performance hit but is otherwise harmless.  On the other
 75072 ** hand, a false negative (returning FALSE when the result could be NULL)
 75073 ** will likely result in an incorrect answer.  So when in doubt, return
 75074 ** TRUE.
 75075 */
 75076 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
 75077   u8 op;
 75078   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
 75079   op = p->op;
 75080   if( op==TK_REGISTER ) op = p->op2;
 75081   switch( op ){
 75082     case TK_INTEGER:
 75083     case TK_STRING:
 75084     case TK_FLOAT:
 75085     case TK_BLOB:
 75086       return 0;
 75087     default:
 75088       return 1;
 75092 /*
 75093 ** Generate an OP_IsNull instruction that tests register iReg and jumps
 75094 ** to location iDest if the value in iReg is NULL.  The value in iReg 
 75095 ** was computed by pExpr.  If we can look at pExpr at compile-time and
 75096 ** determine that it can never generate a NULL, then the OP_IsNull operation
 75097 ** can be omitted.
 75098 */
 75099 SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
 75100   Vdbe *v,            /* The VDBE under construction */
 75101   const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
 75102   int iReg,           /* Test the value in this register for NULL */
 75103   int iDest           /* Jump here if the value is null */
 75104 ){
 75105   if( sqlite3ExprCanBeNull(pExpr) ){
 75106     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
 75110 /*
 75111 ** Return TRUE if the given expression is a constant which would be
 75112 ** unchanged by OP_Affinity with the affinity given in the second
 75113 ** argument.
 75114 **
 75115 ** This routine is used to determine if the OP_Affinity operation
 75116 ** can be omitted.  When in doubt return FALSE.  A false negative
 75117 ** is harmless.  A false positive, however, can result in the wrong
 75118 ** answer.
 75119 */
 75120 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
 75121   u8 op;
 75122   if( aff==SQLITE_AFF_NONE ) return 1;
 75123   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
 75124   op = p->op;
 75125   if( op==TK_REGISTER ) op = p->op2;
 75126   switch( op ){
 75127     case TK_INTEGER: {
 75128       return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
 75130     case TK_FLOAT: {
 75131       return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
 75133     case TK_STRING: {
 75134       return aff==SQLITE_AFF_TEXT;
 75136     case TK_BLOB: {
 75137       return 1;
 75139     case TK_COLUMN: {
 75140       assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
 75141       return p->iColumn<0
 75142           && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
 75144     default: {
 75145       return 0;
 75150 /*
 75151 ** Return TRUE if the given string is a row-id column name.
 75152 */
 75153 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
 75154   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
 75155   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
 75156   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
 75157   return 0;
 75160 /*
 75161 ** Return true if we are able to the IN operator optimization on a
 75162 ** query of the form
 75163 **
 75164 **       x IN (SELECT ...)
 75165 **
 75166 ** Where the SELECT... clause is as specified by the parameter to this
 75167 ** routine.
 75168 **
 75169 ** The Select object passed in has already been preprocessed and no
 75170 ** errors have been found.
 75171 */
 75172 #ifndef SQLITE_OMIT_SUBQUERY
 75173 static int isCandidateForInOpt(Select *p){
 75174   SrcList *pSrc;
 75175   ExprList *pEList;
 75176   Table *pTab;
 75177   if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
 75178   if( p->pPrior ) return 0;              /* Not a compound SELECT */
 75179   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 75180     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 75181     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 75182     return 0; /* No DISTINCT keyword and no aggregate functions */
 75184   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
 75185   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
 75186   assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
 75187   if( p->pWhere ) return 0;              /* Has no WHERE clause */
 75188   pSrc = p->pSrc;
 75189   assert( pSrc!=0 );
 75190   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
 75191   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
 75192   pTab = pSrc->a[0].pTab;
 75193   if( NEVER(pTab==0) ) return 0;
 75194   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
 75195   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
 75196   pEList = p->pEList;
 75197   if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
 75198   if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
 75199   return 1;
 75201 #endif /* SQLITE_OMIT_SUBQUERY */
 75203 /*
 75204 ** Code an OP_Once instruction and allocate space for its flag. Return the 
 75205 ** address of the new instruction.
 75206 */
 75207 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
 75208   Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
 75209   return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
 75212 /*
 75213 ** This function is used by the implementation of the IN (...) operator.
 75214 ** The pX parameter is the expression on the RHS of the IN operator, which
 75215 ** might be either a list of expressions or a subquery.
 75216 **
 75217 ** The job of this routine is to find or create a b-tree object that can
 75218 ** be used either to test for membership in the RHS set or to iterate through
 75219 ** all members of the RHS set, skipping duplicates.
 75220 **
 75221 ** A cursor is opened on the b-tree object that the RHS of the IN operator
 75222 ** and pX->iTable is set to the index of that cursor.
 75223 **
 75224 ** The returned value of this function indicates the b-tree type, as follows:
 75225 **
 75226 **   IN_INDEX_ROWID - The cursor was opened on a database table.
 75227 **   IN_INDEX_INDEX - The cursor was opened on a database index.
 75228 **   IN_INDEX_EPH -   The cursor was opened on a specially created and
 75229 **                    populated epheremal table.
 75230 **
 75231 ** An existing b-tree might be used if the RHS expression pX is a simple
 75232 ** subquery such as:
 75233 **
 75234 **     SELECT <column> FROM <table>
 75235 **
 75236 ** If the RHS of the IN operator is a list or a more complex subquery, then
 75237 ** an ephemeral table might need to be generated from the RHS and then
 75238 ** pX->iTable made to point to the ephermeral table instead of an
 75239 ** existing table.  
 75240 **
 75241 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
 75242 ** through the set members, skipping any duplicates. In this case an
 75243 ** epheremal table must be used unless the selected <column> is guaranteed
 75244 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
 75245 ** has a UNIQUE constraint or UNIQUE index.
 75246 **
 75247 ** If the prNotFound parameter is not 0, then the b-tree will be used 
 75248 ** for fast set membership tests. In this case an epheremal table must 
 75249 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
 75250 ** be found with <column> as its left-most column.
 75251 **
 75252 ** When the b-tree is being used for membership tests, the calling function
 75253 ** needs to know whether or not the structure contains an SQL NULL 
 75254 ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
 75255 ** If there is any chance that the (...) might contain a NULL value at
 75256 ** runtime, then a register is allocated and the register number written
 75257 ** to *prNotFound. If there is no chance that the (...) contains a
 75258 ** NULL value, then *prNotFound is left unchanged.
 75259 **
 75260 ** If a register is allocated and its location stored in *prNotFound, then
 75261 ** its initial value is NULL.  If the (...) does not remain constant
 75262 ** for the duration of the query (i.e. the SELECT within the (...)
 75263 ** is a correlated subquery) then the value of the allocated register is
 75264 ** reset to NULL each time the subquery is rerun. This allows the
 75265 ** caller to use vdbe code equivalent to the following:
 75266 **
 75267 **   if( register==NULL ){
 75268 **     has_null = <test if data structure contains null>
 75269 **     register = 1
 75270 **   }
 75271 **
 75272 ** in order to avoid running the <test if data structure contains null>
 75273 ** test more often than is necessary.
 75274 */
 75275 #ifndef SQLITE_OMIT_SUBQUERY
 75276 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
 75277   Select *p;                            /* SELECT to the right of IN operator */
 75278   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
 75279   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
 75280   int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
 75281   Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
 75283   assert( pX->op==TK_IN );
 75285   /* Check to see if an existing table or index can be used to
 75286   ** satisfy the query.  This is preferable to generating a new 
 75287   ** ephemeral table.
 75288   */
 75289   p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
 75290   if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
 75291     sqlite3 *db = pParse->db;              /* Database connection */
 75292     Table *pTab;                           /* Table <table>. */
 75293     Expr *pExpr;                           /* Expression <column> */
 75294     int iCol;                              /* Index of column <column> */
 75295     int iDb;                               /* Database idx for pTab */
 75297     assert( p );                        /* Because of isCandidateForInOpt(p) */
 75298     assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
 75299     assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
 75300     assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
 75301     pTab = p->pSrc->a[0].pTab;
 75302     pExpr = p->pEList->a[0].pExpr;
 75303     iCol = pExpr->iColumn;
 75305     /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
 75306     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 75307     sqlite3CodeVerifySchema(pParse, iDb);
 75308     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 75310     /* This function is only called from two places. In both cases the vdbe
 75311     ** has already been allocated. So assume sqlite3GetVdbe() is always
 75312     ** successful here.
 75313     */
 75314     assert(v);
 75315     if( iCol<0 ){
 75316       int iAddr;
 75318       iAddr = sqlite3CodeOnce(pParse);
 75320       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 75321       eType = IN_INDEX_ROWID;
 75323       sqlite3VdbeJumpHere(v, iAddr);
 75324     }else{
 75325       Index *pIdx;                         /* Iterator variable */
 75327       /* The collation sequence used by the comparison. If an index is to
 75328       ** be used in place of a temp-table, it must be ordered according
 75329       ** to this collation sequence.  */
 75330       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
 75332       /* Check that the affinity that will be used to perform the 
 75333       ** comparison is the same as the affinity of the column. If
 75334       ** it is not, it is not possible to use any index.
 75335       */
 75336       int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 75338       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
 75339         if( (pIdx->aiColumn[0]==iCol)
 75340          && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
 75341          && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
 75342         ){
 75343           int iAddr;
 75344           char *pKey;
 75346           pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
 75347           iAddr = sqlite3CodeOnce(pParse);
 75349           sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
 75350                                pKey,P4_KEYINFO_HANDOFF);
 75351           VdbeComment((v, "%s", pIdx->zName));
 75352           eType = IN_INDEX_INDEX;
 75354           sqlite3VdbeJumpHere(v, iAddr);
 75355           if( prNotFound && !pTab->aCol[iCol].notNull ){
 75356             *prNotFound = ++pParse->nMem;
 75357             sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 75364   if( eType==0 ){
 75365     /* Could not found an existing table or index to use as the RHS b-tree.
 75366     ** We will have to generate an ephemeral table to do the job.
 75367     */
 75368     double savedNQueryLoop = pParse->nQueryLoop;
 75369     int rMayHaveNull = 0;
 75370     eType = IN_INDEX_EPH;
 75371     if( prNotFound ){
 75372       *prNotFound = rMayHaveNull = ++pParse->nMem;
 75373       sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 75374     }else{
 75375       testcase( pParse->nQueryLoop>(double)1 );
 75376       pParse->nQueryLoop = (double)1;
 75377       if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
 75378         eType = IN_INDEX_ROWID;
 75381     sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
 75382     pParse->nQueryLoop = savedNQueryLoop;
 75383   }else{
 75384     pX->iTable = iTab;
 75386   return eType;
 75388 #endif
 75390 /*
 75391 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
 75392 ** or IN operators.  Examples:
 75393 **
 75394 **     (SELECT a FROM b)          -- subquery
 75395 **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
 75396 **     x IN (4,5,11)              -- IN operator with list on right-hand side
 75397 **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
 75398 **
 75399 ** The pExpr parameter describes the expression that contains the IN
 75400 ** operator or subquery.
 75401 **
 75402 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
 75403 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
 75404 ** to some integer key column of a table B-Tree. In this case, use an
 75405 ** intkey B-Tree to store the set of IN(...) values instead of the usual
 75406 ** (slower) variable length keys B-Tree.
 75407 **
 75408 ** If rMayHaveNull is non-zero, that means that the operation is an IN
 75409 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
 75410 ** Furthermore, the IN is in a WHERE clause and that we really want
 75411 ** to iterate over the RHS of the IN operator in order to quickly locate
 75412 ** all corresponding LHS elements.  All this routine does is initialize
 75413 ** the register given by rMayHaveNull to NULL.  Calling routines will take
 75414 ** care of changing this register value to non-NULL if the RHS is NULL-free.
 75415 **
 75416 ** If rMayHaveNull is zero, that means that the subquery is being used
 75417 ** for membership testing only.  There is no need to initialize any
 75418 ** registers to indicate the presense or absence of NULLs on the RHS.
 75419 **
 75420 ** For a SELECT or EXISTS operator, return the register that holds the
 75421 ** result.  For IN operators or if an error occurs, the return value is 0.
 75422 */
 75423 #ifndef SQLITE_OMIT_SUBQUERY
 75424 SQLITE_PRIVATE int sqlite3CodeSubselect(
 75425   Parse *pParse,          /* Parsing context */
 75426   Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
 75427   int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
 75428   int isRowid             /* If true, LHS of IN operator is a rowid */
 75429 ){
 75430   int testAddr = -1;                      /* One-time test address */
 75431   int rReg = 0;                           /* Register storing resulting */
 75432   Vdbe *v = sqlite3GetVdbe(pParse);
 75433   if( NEVER(v==0) ) return 0;
 75434   sqlite3ExprCachePush(pParse);
 75436   /* This code must be run in its entirety every time it is encountered
 75437   ** if any of the following is true:
 75438   **
 75439   **    *  The right-hand side is a correlated subquery
 75440   **    *  The right-hand side is an expression list containing variables
 75441   **    *  We are inside a trigger
 75442   **
 75443   ** If all of the above are false, then we can run this code just once
 75444   ** save the results, and reuse the same result on subsequent invocations.
 75445   */
 75446   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) ){
 75447     testAddr = sqlite3CodeOnce(pParse);
 75450 #ifndef SQLITE_OMIT_EXPLAIN
 75451   if( pParse->explain==2 ){
 75452     char *zMsg = sqlite3MPrintf(
 75453         pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
 75454         pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
 75455     );
 75456     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 75458 #endif
 75460   switch( pExpr->op ){
 75461     case TK_IN: {
 75462       char affinity;              /* Affinity of the LHS of the IN */
 75463       KeyInfo keyInfo;            /* Keyinfo for the generated table */
 75464       static u8 sortOrder = 0;    /* Fake aSortOrder for keyInfo */
 75465       int addr;                   /* Address of OP_OpenEphemeral instruction */
 75466       Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 75468       if( rMayHaveNull ){
 75469         sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
 75472       affinity = sqlite3ExprAffinity(pLeft);
 75474       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
 75475       ** expression it is handled the same way.  An ephemeral table is 
 75476       ** filled with single-field index keys representing the results
 75477       ** from the SELECT or the <exprlist>.
 75478       **
 75479       ** If the 'x' expression is a column value, or the SELECT...
 75480       ** statement returns a column value, then the affinity of that
 75481       ** column is used to build the index keys. If both 'x' and the
 75482       ** SELECT... statement are columns, then numeric affinity is used
 75483       ** if either column has NUMERIC or INTEGER affinity. If neither
 75484       ** 'x' nor the SELECT... statement are columns, then numeric affinity
 75485       ** is used.
 75486       */
 75487       pExpr->iTable = pParse->nTab++;
 75488       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
 75489       if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 75490       memset(&keyInfo, 0, sizeof(keyInfo));
 75491       keyInfo.nField = 1;
 75492       keyInfo.aSortOrder = &sortOrder;
 75494       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 75495         /* Case 1:     expr IN (SELECT ...)
 75496         **
 75497         ** Generate code to write the results of the select into the temporary
 75498         ** table allocated and opened above.
 75499         */
 75500         SelectDest dest;
 75501         ExprList *pEList;
 75503         assert( !isRowid );
 75504         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 75505         dest.affSdst = (u8)affinity;
 75506         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 75507         pExpr->x.pSelect->iLimit = 0;
 75508         if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
 75509           return 0;
 75511         pEList = pExpr->x.pSelect->pEList;
 75512         if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ 
 75513           keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
 75514               pEList->a[0].pExpr);
 75516       }else if( ALWAYS(pExpr->x.pList!=0) ){
 75517         /* Case 2:     expr IN (exprlist)
 75518         **
 75519         ** For each expression, build an index key from the evaluation and
 75520         ** store it in the temporary table. If <expr> is a column, then use
 75521         ** that columns affinity when building index keys. If <expr> is not
 75522         ** a column, use numeric affinity.
 75523         */
 75524         int i;
 75525         ExprList *pList = pExpr->x.pList;
 75526         struct ExprList_item *pItem;
 75527         int r1, r2, r3;
 75529         if( !affinity ){
 75530           affinity = SQLITE_AFF_NONE;
 75532         keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 75533         keyInfo.aSortOrder = &sortOrder;
 75535         /* Loop through each expression in <exprlist>. */
 75536         r1 = sqlite3GetTempReg(pParse);
 75537         r2 = sqlite3GetTempReg(pParse);
 75538         sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
 75539         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
 75540           Expr *pE2 = pItem->pExpr;
 75541           int iValToIns;
 75543           /* If the expression is not constant then we will need to
 75544           ** disable the test that was generated above that makes sure
 75545           ** this code only executes once.  Because for a non-constant
 75546           ** expression we need to rerun this code each time.
 75547           */
 75548           if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
 75549             sqlite3VdbeChangeToNoop(v, testAddr);
 75550             testAddr = -1;
 75553           /* Evaluate the expression and insert it into the temp table */
 75554           if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
 75555             sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
 75556           }else{
 75557             r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
 75558             if( isRowid ){
 75559               sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
 75560                                 sqlite3VdbeCurrentAddr(v)+2);
 75561               sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
 75562             }else{
 75563               sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
 75564               sqlite3ExprCacheAffinityChange(pParse, r3, 1);
 75565               sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
 75569         sqlite3ReleaseTempReg(pParse, r1);
 75570         sqlite3ReleaseTempReg(pParse, r2);
 75572       if( !isRowid ){
 75573         sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
 75575       break;
 75578     case TK_EXISTS:
 75579     case TK_SELECT:
 75580     default: {
 75581       /* If this has to be a scalar SELECT.  Generate code to put the
 75582       ** value of this select in a memory cell and record the number
 75583       ** of the memory cell in iColumn.  If this is an EXISTS, write
 75584       ** an integer 0 (not exists) or 1 (exists) into a memory cell
 75585       ** and record that memory cell in iColumn.
 75586       */
 75587       Select *pSel;                         /* SELECT statement to encode */
 75588       SelectDest dest;                      /* How to deal with SELECt result */
 75590       testcase( pExpr->op==TK_EXISTS );
 75591       testcase( pExpr->op==TK_SELECT );
 75592       assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 75594       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 75595       pSel = pExpr->x.pSelect;
 75596       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
 75597       if( pExpr->op==TK_SELECT ){
 75598         dest.eDest = SRT_Mem;
 75599         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
 75600         VdbeComment((v, "Init subquery result"));
 75601       }else{
 75602         dest.eDest = SRT_Exists;
 75603         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 75604         VdbeComment((v, "Init EXISTS result"));
 75606       sqlite3ExprDelete(pParse->db, pSel->pLimit);
 75607       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
 75608                                   &sqlite3IntTokens[1]);
 75609       pSel->iLimit = 0;
 75610       if( sqlite3Select(pParse, pSel, &dest) ){
 75611         return 0;
 75613       rReg = dest.iSDParm;
 75614       ExprSetIrreducible(pExpr);
 75615       break;
 75619   if( testAddr>=0 ){
 75620     sqlite3VdbeJumpHere(v, testAddr);
 75622   sqlite3ExprCachePop(pParse, 1);
 75624   return rReg;
 75626 #endif /* SQLITE_OMIT_SUBQUERY */
 75628 #ifndef SQLITE_OMIT_SUBQUERY
 75629 /*
 75630 ** Generate code for an IN expression.
 75631 **
 75632 **      x IN (SELECT ...)
 75633 **      x IN (value, value, ...)
 75634 **
 75635 ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
 75636 ** is an array of zero or more values.  The expression is true if the LHS is
 75637 ** contained within the RHS.  The value of the expression is unknown (NULL)
 75638 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
 75639 ** RHS contains one or more NULL values.
 75640 **
 75641 ** This routine generates code will jump to destIfFalse if the LHS is not 
 75642 ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
 75643 ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
 75644 ** within the RHS then fall through.
 75645 */
 75646 static void sqlite3ExprCodeIN(
 75647   Parse *pParse,        /* Parsing and code generating context */
 75648   Expr *pExpr,          /* The IN expression */
 75649   int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
 75650   int destIfNull        /* Jump here if the results are unknown due to NULLs */
 75651 ){
 75652   int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
 75653   char affinity;        /* Comparison affinity to use */
 75654   int eType;            /* Type of the RHS */
 75655   int r1;               /* Temporary use register */
 75656   Vdbe *v;              /* Statement under construction */
 75658   /* Compute the RHS.   After this step, the table with cursor
 75659   ** pExpr->iTable will contains the values that make up the RHS.
 75660   */
 75661   v = pParse->pVdbe;
 75662   assert( v!=0 );       /* OOM detected prior to this routine */
 75663   VdbeNoopComment((v, "begin IN expr"));
 75664   eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
 75666   /* Figure out the affinity to use to create a key from the results
 75667   ** of the expression. affinityStr stores a static string suitable for
 75668   ** P4 of OP_MakeRecord.
 75669   */
 75670   affinity = comparisonAffinity(pExpr);
 75672   /* Code the LHS, the <expr> from "<expr> IN (...)".
 75673   */
 75674   sqlite3ExprCachePush(pParse);
 75675   r1 = sqlite3GetTempReg(pParse);
 75676   sqlite3ExprCode(pParse, pExpr->pLeft, r1);
 75678   /* If the LHS is NULL, then the result is either false or NULL depending
 75679   ** on whether the RHS is empty or not, respectively.
 75680   */
 75681   if( destIfNull==destIfFalse ){
 75682     /* Shortcut for the common case where the false and NULL outcomes are
 75683     ** the same. */
 75684     sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
 75685   }else{
 75686     int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
 75687     sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
 75688     sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
 75689     sqlite3VdbeJumpHere(v, addr1);
 75692   if( eType==IN_INDEX_ROWID ){
 75693     /* In this case, the RHS is the ROWID of table b-tree
 75694     */
 75695     sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
 75696     sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
 75697   }else{
 75698     /* In this case, the RHS is an index b-tree.
 75699     */
 75700     sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
 75702     /* If the set membership test fails, then the result of the 
 75703     ** "x IN (...)" expression must be either 0 or NULL. If the set
 75704     ** contains no NULL values, then the result is 0. If the set 
 75705     ** contains one or more NULL values, then the result of the
 75706     ** expression is also NULL.
 75707     */
 75708     if( rRhsHasNull==0 || destIfFalse==destIfNull ){
 75709       /* This branch runs if it is known at compile time that the RHS
 75710       ** cannot contain NULL values. This happens as the result
 75711       ** of a "NOT NULL" constraint in the database schema.
 75712       **
 75713       ** Also run this branch if NULL is equivalent to FALSE
 75714       ** for this particular IN operator.
 75715       */
 75716       sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
 75718     }else{
 75719       /* In this branch, the RHS of the IN might contain a NULL and
 75720       ** the presence of a NULL on the RHS makes a difference in the
 75721       ** outcome.
 75722       */
 75723       int j1, j2, j3;
 75725       /* First check to see if the LHS is contained in the RHS.  If so,
 75726       ** then the presence of NULLs in the RHS does not matter, so jump
 75727       ** over all of the code that follows.
 75728       */
 75729       j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
 75731       /* Here we begin generating code that runs if the LHS is not
 75732       ** contained within the RHS.  Generate additional code that
 75733       ** tests the RHS for NULLs.  If the RHS contains a NULL then
 75734       ** jump to destIfNull.  If there are no NULLs in the RHS then
 75735       ** jump to destIfFalse.
 75736       */
 75737       j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
 75738       j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
 75739       sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
 75740       sqlite3VdbeJumpHere(v, j3);
 75741       sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
 75742       sqlite3VdbeJumpHere(v, j2);
 75744       /* Jump to the appropriate target depending on whether or not
 75745       ** the RHS contains a NULL
 75746       */
 75747       sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
 75748       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
 75750       /* The OP_Found at the top of this branch jumps here when true, 
 75751       ** causing the overall IN expression evaluation to fall through.
 75752       */
 75753       sqlite3VdbeJumpHere(v, j1);
 75756   sqlite3ReleaseTempReg(pParse, r1);
 75757   sqlite3ExprCachePop(pParse, 1);
 75758   VdbeComment((v, "end IN expr"));
 75760 #endif /* SQLITE_OMIT_SUBQUERY */
 75762 /*
 75763 ** Duplicate an 8-byte value
 75764 */
 75765 static char *dup8bytes(Vdbe *v, const char *in){
 75766   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
 75767   if( out ){
 75768     memcpy(out, in, 8);
 75770   return out;
 75773 #ifndef SQLITE_OMIT_FLOATING_POINT
 75774 /*
 75775 ** Generate an instruction that will put the floating point
 75776 ** value described by z[0..n-1] into register iMem.
 75777 **
 75778 ** The z[] string will probably not be zero-terminated.  But the 
 75779 ** z[n] character is guaranteed to be something that does not look
 75780 ** like the continuation of the number.
 75781 */
 75782 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
 75783   if( ALWAYS(z!=0) ){
 75784     double value;
 75785     char *zV;
 75786     sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 75787     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
 75788     if( negateFlag ) value = -value;
 75789     zV = dup8bytes(v, (char*)&value);
 75790     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
 75793 #endif
 75796 /*
 75797 ** Generate an instruction that will put the integer describe by
 75798 ** text z[0..n-1] into register iMem.
 75799 **
 75800 ** Expr.u.zToken is always UTF8 and zero-terminated.
 75801 */
 75802 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
 75803   Vdbe *v = pParse->pVdbe;
 75804   if( pExpr->flags & EP_IntValue ){
 75805     int i = pExpr->u.iValue;
 75806     assert( i>=0 );
 75807     if( negFlag ) i = -i;
 75808     sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
 75809   }else{
 75810     int c;
 75811     i64 value;
 75812     const char *z = pExpr->u.zToken;
 75813     assert( z!=0 );
 75814     c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 75815     if( c==0 || (c==2 && negFlag) ){
 75816       char *zV;
 75817       if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
 75818       zV = dup8bytes(v, (char*)&value);
 75819       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
 75820     }else{
 75821 #ifdef SQLITE_OMIT_FLOATING_POINT
 75822       sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
 75823 #else
 75824       codeReal(v, z, negFlag, iMem);
 75825 #endif
 75830 /*
 75831 ** Clear a cache entry.
 75832 */
 75833 static void cacheEntryClear(Parse *pParse, struct yColCache *p){
 75834   if( p->tempReg ){
 75835     if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 75836       pParse->aTempReg[pParse->nTempReg++] = p->iReg;
 75838     p->tempReg = 0;
 75843 /*
 75844 ** Record in the column cache that a particular column from a
 75845 ** particular table is stored in a particular register.
 75846 */
 75847 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
 75848   int i;
 75849   int minLru;
 75850   int idxLru;
 75851   struct yColCache *p;
 75853   assert( iReg>0 );  /* Register numbers are always positive */
 75854   assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
 75856   /* The SQLITE_ColumnCache flag disables the column cache.  This is used
 75857   ** for testing only - to verify that SQLite always gets the same answer
 75858   ** with and without the column cache.
 75859   */
 75860   if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
 75862   /* First replace any existing entry.
 75863   **
 75864   ** Actually, the way the column cache is currently used, we are guaranteed
 75865   ** that the object will never already be in cache.  Verify this guarantee.
 75866   */
 75867 #ifndef NDEBUG
 75868   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 75869     assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
 75871 #endif
 75873   /* Find an empty slot and replace it */
 75874   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 75875     if( p->iReg==0 ){
 75876       p->iLevel = pParse->iCacheLevel;
 75877       p->iTable = iTab;
 75878       p->iColumn = iCol;
 75879       p->iReg = iReg;
 75880       p->tempReg = 0;
 75881       p->lru = pParse->iCacheCnt++;
 75882       return;
 75886   /* Replace the last recently used */
 75887   minLru = 0x7fffffff;
 75888   idxLru = -1;
 75889   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 75890     if( p->lru<minLru ){
 75891       idxLru = i;
 75892       minLru = p->lru;
 75895   if( ALWAYS(idxLru>=0) ){
 75896     p = &pParse->aColCache[idxLru];
 75897     p->iLevel = pParse->iCacheLevel;
 75898     p->iTable = iTab;
 75899     p->iColumn = iCol;
 75900     p->iReg = iReg;
 75901     p->tempReg = 0;
 75902     p->lru = pParse->iCacheCnt++;
 75903     return;
 75907 /*
 75908 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
 75909 ** Purge the range of registers from the column cache.
 75910 */
 75911 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
 75912   int i;
 75913   int iLast = iReg + nReg - 1;
 75914   struct yColCache *p;
 75915   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 75916     int r = p->iReg;
 75917     if( r>=iReg && r<=iLast ){
 75918       cacheEntryClear(pParse, p);
 75919       p->iReg = 0;
 75924 /*
 75925 ** Remember the current column cache context.  Any new entries added
 75926 ** added to the column cache after this call are removed when the
 75927 ** corresponding pop occurs.
 75928 */
 75929 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
 75930   pParse->iCacheLevel++;
 75933 /*
 75934 ** Remove from the column cache any entries that were added since the
 75935 ** the previous N Push operations.  In other words, restore the cache
 75936 ** to the state it was in N Pushes ago.
 75937 */
 75938 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
 75939   int i;
 75940   struct yColCache *p;
 75941   assert( N>0 );
 75942   assert( pParse->iCacheLevel>=N );
 75943   pParse->iCacheLevel -= N;
 75944   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 75945     if( p->iReg && p->iLevel>pParse->iCacheLevel ){
 75946       cacheEntryClear(pParse, p);
 75947       p->iReg = 0;
 75952 /*
 75953 ** When a cached column is reused, make sure that its register is
 75954 ** no longer available as a temp register.  ticket #3879:  that same
 75955 ** register might be in the cache in multiple places, so be sure to
 75956 ** get them all.
 75957 */
 75958 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
 75959   int i;
 75960   struct yColCache *p;
 75961   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 75962     if( p->iReg==iReg ){
 75963       p->tempReg = 0;
 75968 /*
 75969 ** Generate code to extract the value of the iCol-th column of a table.
 75970 */
 75971 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
 75972   Vdbe *v,        /* The VDBE under construction */
 75973   Table *pTab,    /* The table containing the value */
 75974   int iTabCur,    /* The cursor for this table */
 75975   int iCol,       /* Index of the column to extract */
 75976   int regOut      /* Extract the valud into this register */
 75977 ){
 75978   if( iCol<0 || iCol==pTab->iPKey ){
 75979     sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
 75980   }else{
 75981     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
 75982     sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
 75984   if( iCol>=0 ){
 75985     sqlite3ColumnDefault(v, pTab, iCol, regOut);
 75989 /*
 75990 ** Generate code that will extract the iColumn-th column from
 75991 ** table pTab and store the column value in a register.  An effort
 75992 ** is made to store the column value in register iReg, but this is
 75993 ** not guaranteed.  The location of the column value is returned.
 75994 **
 75995 ** There must be an open cursor to pTab in iTable when this routine
 75996 ** is called.  If iColumn<0 then code is generated that extracts the rowid.
 75997 */
 75998 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
 75999   Parse *pParse,   /* Parsing and code generating context */
 76000   Table *pTab,     /* Description of the table we are reading from */
 76001   int iColumn,     /* Index of the table column */
 76002   int iTable,      /* The cursor pointing to the table */
 76003   int iReg,        /* Store results here */
 76004   u8 p5            /* P5 value for OP_Column */
 76005 ){
 76006   Vdbe *v = pParse->pVdbe;
 76007   int i;
 76008   struct yColCache *p;
 76010   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 76011     if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
 76012       p->lru = pParse->iCacheCnt++;
 76013       sqlite3ExprCachePinRegister(pParse, p->iReg);
 76014       return p->iReg;
 76017   assert( v!=0 );
 76018   sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
 76019   if( p5 ){
 76020     sqlite3VdbeChangeP5(v, p5);
 76021   }else{   
 76022     sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
 76024   return iReg;
 76027 /*
 76028 ** Clear all column cache entries.
 76029 */
 76030 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
 76031   int i;
 76032   struct yColCache *p;
 76034   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 76035     if( p->iReg ){
 76036       cacheEntryClear(pParse, p);
 76037       p->iReg = 0;
 76042 /*
 76043 ** Record the fact that an affinity change has occurred on iCount
 76044 ** registers starting with iStart.
 76045 */
 76046 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
 76047   sqlite3ExprCacheRemove(pParse, iStart, iCount);
 76050 /*
 76051 ** Generate code to move content from registers iFrom...iFrom+nReg-1
 76052 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
 76053 */
 76054 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
 76055   int i;
 76056   struct yColCache *p;
 76057   assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
 76058   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
 76059   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 76060     int x = p->iReg;
 76061     if( x>=iFrom && x<iFrom+nReg ){
 76062       p->iReg += iTo-iFrom;
 76067 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 76068 /*
 76069 ** Return true if any register in the range iFrom..iTo (inclusive)
 76070 ** is used as part of the column cache.
 76071 **
 76072 ** This routine is used within assert() and testcase() macros only
 76073 ** and does not appear in a normal build.
 76074 */
 76075 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
 76076   int i;
 76077   struct yColCache *p;
 76078   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 76079     int r = p->iReg;
 76080     if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
 76082   return 0;
 76084 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
 76086 /*
 76087 ** Generate code into the current Vdbe to evaluate the given
 76088 ** expression.  Attempt to store the results in register "target".
 76089 ** Return the register where results are stored.
 76090 **
 76091 ** With this routine, there is no guarantee that results will
 76092 ** be stored in target.  The result might be stored in some other
 76093 ** register if it is convenient to do so.  The calling function
 76094 ** must check the return code and move the results to the desired
 76095 ** register.
 76096 */
 76097 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
 76098   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
 76099   int op;                   /* The opcode being coded */
 76100   int inReg = target;       /* Results stored in register inReg */
 76101   int regFree1 = 0;         /* If non-zero free this temporary register */
 76102   int regFree2 = 0;         /* If non-zero free this temporary register */
 76103   int r1, r2, r3, r4;       /* Various register numbers */
 76104   sqlite3 *db = pParse->db; /* The database connection */
 76106   assert( target>0 && target<=pParse->nMem );
 76107   if( v==0 ){
 76108     assert( pParse->db->mallocFailed );
 76109     return 0;
 76112   if( pExpr==0 ){
 76113     op = TK_NULL;
 76114   }else{
 76115     op = pExpr->op;
 76117   switch( op ){
 76118     case TK_AGG_COLUMN: {
 76119       AggInfo *pAggInfo = pExpr->pAggInfo;
 76120       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
 76121       if( !pAggInfo->directMode ){
 76122         assert( pCol->iMem>0 );
 76123         inReg = pCol->iMem;
 76124         break;
 76125       }else if( pAggInfo->useSortingIdx ){
 76126         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
 76127                               pCol->iSorterColumn, target);
 76128         break;
 76130       /* Otherwise, fall thru into the TK_COLUMN case */
 76132     case TK_COLUMN: {
 76133       if( pExpr->iTable<0 ){
 76134         /* This only happens when coding check constraints */
 76135         assert( pParse->ckBase>0 );
 76136         inReg = pExpr->iColumn + pParse->ckBase;
 76137       }else{
 76138         inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 76139                                  pExpr->iColumn, pExpr->iTable, target,
 76140                                  pExpr->op2);
 76142       break;
 76144     case TK_INTEGER: {
 76145       codeInteger(pParse, pExpr, 0, target);
 76146       break;
 76148 #ifndef SQLITE_OMIT_FLOATING_POINT
 76149     case TK_FLOAT: {
 76150       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76151       codeReal(v, pExpr->u.zToken, 0, target);
 76152       break;
 76154 #endif
 76155     case TK_STRING: {
 76156       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76157       sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
 76158       break;
 76160     case TK_NULL: {
 76161       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 76162       break;
 76164 #ifndef SQLITE_OMIT_BLOB_LITERAL
 76165     case TK_BLOB: {
 76166       int n;
 76167       const char *z;
 76168       char *zBlob;
 76169       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76170       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 76171       assert( pExpr->u.zToken[1]=='\'' );
 76172       z = &pExpr->u.zToken[2];
 76173       n = sqlite3Strlen30(z) - 1;
 76174       assert( z[n]=='\'' );
 76175       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
 76176       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
 76177       break;
 76179 #endif
 76180     case TK_VARIABLE: {
 76181       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76182       assert( pExpr->u.zToken!=0 );
 76183       assert( pExpr->u.zToken[0]!=0 );
 76184       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
 76185       if( pExpr->u.zToken[1]!=0 ){
 76186         assert( pExpr->u.zToken[0]=='?' 
 76187              || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
 76188         sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
 76190       break;
 76192     case TK_REGISTER: {
 76193       inReg = pExpr->iTable;
 76194       break;
 76196     case TK_AS: {
 76197       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 76198       break;
 76200 #ifndef SQLITE_OMIT_CAST
 76201     case TK_CAST: {
 76202       /* Expressions of the form:   CAST(pLeft AS token) */
 76203       int aff, to_op;
 76204       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 76205       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76206       aff = sqlite3AffinityType(pExpr->u.zToken);
 76207       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
 76208       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
 76209       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
 76210       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
 76211       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
 76212       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
 76213       testcase( to_op==OP_ToText );
 76214       testcase( to_op==OP_ToBlob );
 76215       testcase( to_op==OP_ToNumeric );
 76216       testcase( to_op==OP_ToInt );
 76217       testcase( to_op==OP_ToReal );
 76218       if( inReg!=target ){
 76219         sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
 76220         inReg = target;
 76222       sqlite3VdbeAddOp1(v, to_op, inReg);
 76223       testcase( usedAsColumnCache(pParse, inReg, inReg) );
 76224       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
 76225       break;
 76227 #endif /* SQLITE_OMIT_CAST */
 76228     case TK_LT:
 76229     case TK_LE:
 76230     case TK_GT:
 76231     case TK_GE:
 76232     case TK_NE:
 76233     case TK_EQ: {
 76234       assert( TK_LT==OP_Lt );
 76235       assert( TK_LE==OP_Le );
 76236       assert( TK_GT==OP_Gt );
 76237       assert( TK_GE==OP_Ge );
 76238       assert( TK_EQ==OP_Eq );
 76239       assert( TK_NE==OP_Ne );
 76240       testcase( op==TK_LT );
 76241       testcase( op==TK_LE );
 76242       testcase( op==TK_GT );
 76243       testcase( op==TK_GE );
 76244       testcase( op==TK_EQ );
 76245       testcase( op==TK_NE );
 76246       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 76247       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 76248       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 76249                   r1, r2, inReg, SQLITE_STOREP2);
 76250       testcase( regFree1==0 );
 76251       testcase( regFree2==0 );
 76252       break;
 76254     case TK_IS:
 76255     case TK_ISNOT: {
 76256       testcase( op==TK_IS );
 76257       testcase( op==TK_ISNOT );
 76258       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 76259       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 76260       op = (op==TK_IS) ? TK_EQ : TK_NE;
 76261       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 76262                   r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
 76263       testcase( regFree1==0 );
 76264       testcase( regFree2==0 );
 76265       break;
 76267     case TK_AND:
 76268     case TK_OR:
 76269     case TK_PLUS:
 76270     case TK_STAR:
 76271     case TK_MINUS:
 76272     case TK_REM:
 76273     case TK_BITAND:
 76274     case TK_BITOR:
 76275     case TK_SLASH:
 76276     case TK_LSHIFT:
 76277     case TK_RSHIFT: 
 76278     case TK_CONCAT: {
 76279       assert( TK_AND==OP_And );
 76280       assert( TK_OR==OP_Or );
 76281       assert( TK_PLUS==OP_Add );
 76282       assert( TK_MINUS==OP_Subtract );
 76283       assert( TK_REM==OP_Remainder );
 76284       assert( TK_BITAND==OP_BitAnd );
 76285       assert( TK_BITOR==OP_BitOr );
 76286       assert( TK_SLASH==OP_Divide );
 76287       assert( TK_LSHIFT==OP_ShiftLeft );
 76288       assert( TK_RSHIFT==OP_ShiftRight );
 76289       assert( TK_CONCAT==OP_Concat );
 76290       testcase( op==TK_AND );
 76291       testcase( op==TK_OR );
 76292       testcase( op==TK_PLUS );
 76293       testcase( op==TK_MINUS );
 76294       testcase( op==TK_REM );
 76295       testcase( op==TK_BITAND );
 76296       testcase( op==TK_BITOR );
 76297       testcase( op==TK_SLASH );
 76298       testcase( op==TK_LSHIFT );
 76299       testcase( op==TK_RSHIFT );
 76300       testcase( op==TK_CONCAT );
 76301       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 76302       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 76303       sqlite3VdbeAddOp3(v, op, r2, r1, target);
 76304       testcase( regFree1==0 );
 76305       testcase( regFree2==0 );
 76306       break;
 76308     case TK_UMINUS: {
 76309       Expr *pLeft = pExpr->pLeft;
 76310       assert( pLeft );
 76311       if( pLeft->op==TK_INTEGER ){
 76312         codeInteger(pParse, pLeft, 1, target);
 76313 #ifndef SQLITE_OMIT_FLOATING_POINT
 76314       }else if( pLeft->op==TK_FLOAT ){
 76315         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76316         codeReal(v, pLeft->u.zToken, 1, target);
 76317 #endif
 76318       }else{
 76319         regFree1 = r1 = sqlite3GetTempReg(pParse);
 76320         sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
 76321         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
 76322         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
 76323         testcase( regFree2==0 );
 76325       inReg = target;
 76326       break;
 76328     case TK_BITNOT:
 76329     case TK_NOT: {
 76330       assert( TK_BITNOT==OP_BitNot );
 76331       assert( TK_NOT==OP_Not );
 76332       testcase( op==TK_BITNOT );
 76333       testcase( op==TK_NOT );
 76334       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 76335       testcase( regFree1==0 );
 76336       inReg = target;
 76337       sqlite3VdbeAddOp2(v, op, r1, inReg);
 76338       break;
 76340     case TK_ISNULL:
 76341     case TK_NOTNULL: {
 76342       int addr;
 76343       assert( TK_ISNULL==OP_IsNull );
 76344       assert( TK_NOTNULL==OP_NotNull );
 76345       testcase( op==TK_ISNULL );
 76346       testcase( op==TK_NOTNULL );
 76347       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 76348       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 76349       testcase( regFree1==0 );
 76350       addr = sqlite3VdbeAddOp1(v, op, r1);
 76351       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
 76352       sqlite3VdbeJumpHere(v, addr);
 76353       break;
 76355     case TK_AGG_FUNCTION: {
 76356       AggInfo *pInfo = pExpr->pAggInfo;
 76357       if( pInfo==0 ){
 76358         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76359         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
 76360       }else{
 76361         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
 76363       break;
 76365     case TK_CONST_FUNC:
 76366     case TK_FUNCTION: {
 76367       ExprList *pFarg;       /* List of function arguments */
 76368       int nFarg;             /* Number of function arguments */
 76369       FuncDef *pDef;         /* The function definition object */
 76370       int nId;               /* Length of the function name in bytes */
 76371       const char *zId;       /* The function name */
 76372       int constMask = 0;     /* Mask of function arguments that are constant */
 76373       int i;                 /* Loop counter */
 76374       u8 enc = ENC(db);      /* The text encoding used by this database */
 76375       CollSeq *pColl = 0;    /* A collating sequence */
 76377       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 76378       testcase( op==TK_CONST_FUNC );
 76379       testcase( op==TK_FUNCTION );
 76380       if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
 76381         pFarg = 0;
 76382       }else{
 76383         pFarg = pExpr->x.pList;
 76385       nFarg = pFarg ? pFarg->nExpr : 0;
 76386       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76387       zId = pExpr->u.zToken;
 76388       nId = sqlite3Strlen30(zId);
 76389       pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
 76390       if( pDef==0 ){
 76391         sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
 76392         break;
 76395       /* Attempt a direct implementation of the built-in COALESCE() and
 76396       ** IFNULL() functions.  This avoids unnecessary evalation of
 76397       ** arguments past the first non-NULL argument.
 76398       */
 76399       if( pDef->flags & SQLITE_FUNC_COALESCE ){
 76400         int endCoalesce = sqlite3VdbeMakeLabel(v);
 76401         assert( nFarg>=2 );
 76402         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
 76403         for(i=1; i<nFarg; i++){
 76404           sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
 76405           sqlite3ExprCacheRemove(pParse, target, 1);
 76406           sqlite3ExprCachePush(pParse);
 76407           sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
 76408           sqlite3ExprCachePop(pParse, 1);
 76410         sqlite3VdbeResolveLabel(v, endCoalesce);
 76411         break;
 76415       if( pFarg ){
 76416         r1 = sqlite3GetTempRange(pParse, nFarg);
 76418         /* For length() and typeof() functions with a column argument,
 76419         ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
 76420         ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
 76421         ** loading.
 76422         */
 76423         if( (pDef->flags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
 76424           u8 exprOp;
 76425           assert( nFarg==1 );
 76426           assert( pFarg->a[0].pExpr!=0 );
 76427           exprOp = pFarg->a[0].pExpr->op;
 76428           if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
 76429             assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
 76430             assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
 76431             testcase( pDef->flags==SQLITE_FUNC_LENGTH );
 76432             pFarg->a[0].pExpr->op2 = pDef->flags;
 76436         sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
 76437         sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
 76438         sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
 76439       }else{
 76440         r1 = 0;
 76442 #ifndef SQLITE_OMIT_VIRTUALTABLE
 76443       /* Possibly overload the function if the first argument is
 76444       ** a virtual table column.
 76445       **
 76446       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
 76447       ** second argument, not the first, as the argument to test to
 76448       ** see if it is a column in a virtual table.  This is done because
 76449       ** the left operand of infix functions (the operand we want to
 76450       ** control overloading) ends up as the second argument to the
 76451       ** function.  The expression "A glob B" is equivalent to 
 76452       ** "glob(B,A).  We want to use the A in "A glob B" to test
 76453       ** for function overloading.  But we use the B term in "glob(B,A)".
 76454       */
 76455       if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
 76456         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
 76457       }else if( nFarg>0 ){
 76458         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
 76460 #endif
 76461       for(i=0; i<nFarg; i++){
 76462         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
 76463           constMask |= (1<<i);
 76465         if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
 76466           pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
 76469       if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
 76470         if( !pColl ) pColl = db->pDfltColl; 
 76471         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
 76473       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
 76474                         (char*)pDef, P4_FUNCDEF);
 76475       sqlite3VdbeChangeP5(v, (u8)nFarg);
 76476       if( nFarg ){
 76477         sqlite3ReleaseTempRange(pParse, r1, nFarg);
 76479       break;
 76481 #ifndef SQLITE_OMIT_SUBQUERY
 76482     case TK_EXISTS:
 76483     case TK_SELECT: {
 76484       testcase( op==TK_EXISTS );
 76485       testcase( op==TK_SELECT );
 76486       inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
 76487       break;
 76489     case TK_IN: {
 76490       int destIfFalse = sqlite3VdbeMakeLabel(v);
 76491       int destIfNull = sqlite3VdbeMakeLabel(v);
 76492       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 76493       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 76494       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 76495       sqlite3VdbeResolveLabel(v, destIfFalse);
 76496       sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
 76497       sqlite3VdbeResolveLabel(v, destIfNull);
 76498       break;
 76500 #endif /* SQLITE_OMIT_SUBQUERY */
 76503     /*
 76504     **    x BETWEEN y AND z
 76505     **
 76506     ** This is equivalent to
 76507     **
 76508     **    x>=y AND x<=z
 76509     **
 76510     ** X is stored in pExpr->pLeft.
 76511     ** Y is stored in pExpr->pList->a[0].pExpr.
 76512     ** Z is stored in pExpr->pList->a[1].pExpr.
 76513     */
 76514     case TK_BETWEEN: {
 76515       Expr *pLeft = pExpr->pLeft;
 76516       struct ExprList_item *pLItem = pExpr->x.pList->a;
 76517       Expr *pRight = pLItem->pExpr;
 76519       r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
 76520       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 76521       testcase( regFree1==0 );
 76522       testcase( regFree2==0 );
 76523       r3 = sqlite3GetTempReg(pParse);
 76524       r4 = sqlite3GetTempReg(pParse);
 76525       codeCompare(pParse, pLeft, pRight, OP_Ge,
 76526                   r1, r2, r3, SQLITE_STOREP2);
 76527       pLItem++;
 76528       pRight = pLItem->pExpr;
 76529       sqlite3ReleaseTempReg(pParse, regFree2);
 76530       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 76531       testcase( regFree2==0 );
 76532       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
 76533       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
 76534       sqlite3ReleaseTempReg(pParse, r3);
 76535       sqlite3ReleaseTempReg(pParse, r4);
 76536       break;
 76538     case TK_COLLATE: 
 76539     case TK_UPLUS: {
 76540       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 76541       break;
 76544     case TK_TRIGGER: {
 76545       /* If the opcode is TK_TRIGGER, then the expression is a reference
 76546       ** to a column in the new.* or old.* pseudo-tables available to
 76547       ** trigger programs. In this case Expr.iTable is set to 1 for the
 76548       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
 76549       ** is set to the column of the pseudo-table to read, or to -1 to
 76550       ** read the rowid field.
 76551       **
 76552       ** The expression is implemented using an OP_Param opcode. The p1
 76553       ** parameter is set to 0 for an old.rowid reference, or to (i+1)
 76554       ** to reference another column of the old.* pseudo-table, where 
 76555       ** i is the index of the column. For a new.rowid reference, p1 is
 76556       ** set to (n+1), where n is the number of columns in each pseudo-table.
 76557       ** For a reference to any other column in the new.* pseudo-table, p1
 76558       ** is set to (n+2+i), where n and i are as defined previously. For
 76559       ** example, if the table on which triggers are being fired is
 76560       ** declared as:
 76561       **
 76562       **   CREATE TABLE t1(a, b);
 76563       **
 76564       ** Then p1 is interpreted as follows:
 76565       **
 76566       **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
 76567       **   p1==1   ->    old.a         p1==4   ->    new.a
 76568       **   p1==2   ->    old.b         p1==5   ->    new.b       
 76569       */
 76570       Table *pTab = pExpr->pTab;
 76571       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
 76573       assert( pExpr->iTable==0 || pExpr->iTable==1 );
 76574       assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 76575       assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 76576       assert( p1>=0 && p1<(pTab->nCol*2+2) );
 76578       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 76579       VdbeComment((v, "%s.%s -> $%d",
 76580         (pExpr->iTable ? "new" : "old"),
 76581         (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
 76582         target
 76583       ));
 76585 #ifndef SQLITE_OMIT_FLOATING_POINT
 76586       /* If the column has REAL affinity, it may currently be stored as an
 76587       ** integer. Use OP_RealAffinity to make sure it is really real.  */
 76588       if( pExpr->iColumn>=0 
 76589        && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
 76590       ){
 76591         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
 76593 #endif
 76594       break;
 76598     /*
 76599     ** Form A:
 76600     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 76601     **
 76602     ** Form B:
 76603     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 76604     **
 76605     ** Form A is can be transformed into the equivalent form B as follows:
 76606     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
 76607     **        WHEN x=eN THEN rN ELSE y END
 76608     **
 76609     ** X (if it exists) is in pExpr->pLeft.
 76610     ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
 76611     ** ELSE clause and no other term matches, then the result of the
 76612     ** exprssion is NULL.
 76613     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
 76614     **
 76615     ** The result of the expression is the Ri for the first matching Ei,
 76616     ** or if there is no matching Ei, the ELSE term Y, or if there is
 76617     ** no ELSE term, NULL.
 76618     */
 76619     default: assert( op==TK_CASE ); {
 76620       int endLabel;                     /* GOTO label for end of CASE stmt */
 76621       int nextCase;                     /* GOTO label for next WHEN clause */
 76622       int nExpr;                        /* 2x number of WHEN terms */
 76623       int i;                            /* Loop counter */
 76624       ExprList *pEList;                 /* List of WHEN terms */
 76625       struct ExprList_item *aListelem;  /* Array of WHEN terms */
 76626       Expr opCompare;                   /* The X==Ei expression */
 76627       Expr cacheX;                      /* Cached expression X */
 76628       Expr *pX;                         /* The X expression */
 76629       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
 76630       VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
 76632       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
 76633       assert((pExpr->x.pList->nExpr % 2) == 0);
 76634       assert(pExpr->x.pList->nExpr > 0);
 76635       pEList = pExpr->x.pList;
 76636       aListelem = pEList->a;
 76637       nExpr = pEList->nExpr;
 76638       endLabel = sqlite3VdbeMakeLabel(v);
 76639       if( (pX = pExpr->pLeft)!=0 ){
 76640         cacheX = *pX;
 76641         testcase( pX->op==TK_COLUMN );
 76642         testcase( pX->op==TK_REGISTER );
 76643         cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
 76644         testcase( regFree1==0 );
 76645         cacheX.op = TK_REGISTER;
 76646         opCompare.op = TK_EQ;
 76647         opCompare.pLeft = &cacheX;
 76648         pTest = &opCompare;
 76649         /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
 76650         ** The value in regFree1 might get SCopy-ed into the file result.
 76651         ** So make sure that the regFree1 register is not reused for other
 76652         ** purposes and possibly overwritten.  */
 76653         regFree1 = 0;
 76655       for(i=0; i<nExpr; i=i+2){
 76656         sqlite3ExprCachePush(pParse);
 76657         if( pX ){
 76658           assert( pTest!=0 );
 76659           opCompare.pRight = aListelem[i].pExpr;
 76660         }else{
 76661           pTest = aListelem[i].pExpr;
 76663         nextCase = sqlite3VdbeMakeLabel(v);
 76664         testcase( pTest->op==TK_COLUMN );
 76665         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
 76666         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
 76667         testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
 76668         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
 76669         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
 76670         sqlite3ExprCachePop(pParse, 1);
 76671         sqlite3VdbeResolveLabel(v, nextCase);
 76673       if( pExpr->pRight ){
 76674         sqlite3ExprCachePush(pParse);
 76675         sqlite3ExprCode(pParse, pExpr->pRight, target);
 76676         sqlite3ExprCachePop(pParse, 1);
 76677       }else{
 76678         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 76680       assert( db->mallocFailed || pParse->nErr>0 
 76681            || pParse->iCacheLevel==iCacheLevel );
 76682       sqlite3VdbeResolveLabel(v, endLabel);
 76683       break;
 76685 #ifndef SQLITE_OMIT_TRIGGER
 76686     case TK_RAISE: {
 76687       assert( pExpr->affinity==OE_Rollback 
 76688            || pExpr->affinity==OE_Abort
 76689            || pExpr->affinity==OE_Fail
 76690            || pExpr->affinity==OE_Ignore
 76691       );
 76692       if( !pParse->pTriggerTab ){
 76693         sqlite3ErrorMsg(pParse,
 76694                        "RAISE() may only be used within a trigger-program");
 76695         return 0;
 76697       if( pExpr->affinity==OE_Abort ){
 76698         sqlite3MayAbort(pParse);
 76700       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 76701       if( pExpr->affinity==OE_Ignore ){
 76702         sqlite3VdbeAddOp4(
 76703             v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
 76704       }else{
 76705         sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
 76708       break;
 76710 #endif
 76712   sqlite3ReleaseTempReg(pParse, regFree1);
 76713   sqlite3ReleaseTempReg(pParse, regFree2);
 76714   return inReg;
 76717 /*
 76718 ** Generate code to evaluate an expression and store the results
 76719 ** into a register.  Return the register number where the results
 76720 ** are stored.
 76721 **
 76722 ** If the register is a temporary register that can be deallocated,
 76723 ** then write its number into *pReg.  If the result register is not
 76724 ** a temporary, then set *pReg to zero.
 76725 */
 76726 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
 76727   int r1 = sqlite3GetTempReg(pParse);
 76728   int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 76729   if( r2==r1 ){
 76730     *pReg = r1;
 76731   }else{
 76732     sqlite3ReleaseTempReg(pParse, r1);
 76733     *pReg = 0;
 76735   return r2;
 76738 /*
 76739 ** Generate code that will evaluate expression pExpr and store the
 76740 ** results in register target.  The results are guaranteed to appear
 76741 ** in register target.
 76742 */
 76743 SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
 76744   int inReg;
 76746   assert( target>0 && target<=pParse->nMem );
 76747   if( pExpr && pExpr->op==TK_REGISTER ){
 76748     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
 76749   }else{
 76750     inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
 76751     assert( pParse->pVdbe || pParse->db->mallocFailed );
 76752     if( inReg!=target && pParse->pVdbe ){
 76753       sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
 76756   return target;
 76759 /*
 76760 ** Generate code that evalutes the given expression and puts the result
 76761 ** in register target.
 76762 **
 76763 ** Also make a copy of the expression results into another "cache" register
 76764 ** and modify the expression so that the next time it is evaluated,
 76765 ** the result is a copy of the cache register.
 76766 **
 76767 ** This routine is used for expressions that are used multiple 
 76768 ** times.  They are evaluated once and the results of the expression
 76769 ** are reused.
 76770 */
 76771 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
 76772   Vdbe *v = pParse->pVdbe;
 76773   int inReg;
 76774   inReg = sqlite3ExprCode(pParse, pExpr, target);
 76775   assert( target>0 );
 76776   /* This routine is called for terms to INSERT or UPDATE.  And the only
 76777   ** other place where expressions can be converted into TK_REGISTER is
 76778   ** in WHERE clause processing.  So as currently implemented, there is
 76779   ** no way for a TK_REGISTER to exist here.  But it seems prudent to
 76780   ** keep the ALWAYS() in case the conditions above change with future
 76781   ** modifications or enhancements. */
 76782   if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
 76783     int iMem;
 76784     iMem = ++pParse->nMem;
 76785     sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
 76786     pExpr->iTable = iMem;
 76787     pExpr->op2 = pExpr->op;
 76788     pExpr->op = TK_REGISTER;
 76790   return inReg;
 76793 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 76794 /*
 76795 ** Generate a human-readable explanation of an expression tree.
 76796 */
 76797 SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
 76798   int op;                   /* The opcode being coded */
 76799   const char *zBinOp = 0;   /* Binary operator */
 76800   const char *zUniOp = 0;   /* Unary operator */
 76801   if( pExpr==0 ){
 76802     op = TK_NULL;
 76803   }else{
 76804     op = pExpr->op;
 76806   switch( op ){
 76807     case TK_AGG_COLUMN: {
 76808       sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
 76809             pExpr->iTable, pExpr->iColumn);
 76810       break;
 76812     case TK_COLUMN: {
 76813       if( pExpr->iTable<0 ){
 76814         /* This only happens when coding check constraints */
 76815         sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
 76816       }else{
 76817         sqlite3ExplainPrintf(pOut, "{%d:%d}",
 76818                              pExpr->iTable, pExpr->iColumn);
 76820       break;
 76822     case TK_INTEGER: {
 76823       if( pExpr->flags & EP_IntValue ){
 76824         sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
 76825       }else{
 76826         sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
 76828       break;
 76830 #ifndef SQLITE_OMIT_FLOATING_POINT
 76831     case TK_FLOAT: {
 76832       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
 76833       break;
 76835 #endif
 76836     case TK_STRING: {
 76837       sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
 76838       break;
 76840     case TK_NULL: {
 76841       sqlite3ExplainPrintf(pOut,"NULL");
 76842       break;
 76844 #ifndef SQLITE_OMIT_BLOB_LITERAL
 76845     case TK_BLOB: {
 76846       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
 76847       break;
 76849 #endif
 76850     case TK_VARIABLE: {
 76851       sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
 76852                            pExpr->u.zToken, pExpr->iColumn);
 76853       break;
 76855     case TK_REGISTER: {
 76856       sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
 76857       break;
 76859     case TK_AS: {
 76860       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 76861       break;
 76863 #ifndef SQLITE_OMIT_CAST
 76864     case TK_CAST: {
 76865       /* Expressions of the form:   CAST(pLeft AS token) */
 76866       const char *zAff = "unk";
 76867       switch( sqlite3AffinityType(pExpr->u.zToken) ){
 76868         case SQLITE_AFF_TEXT:    zAff = "TEXT";     break;
 76869         case SQLITE_AFF_NONE:    zAff = "NONE";     break;
 76870         case SQLITE_AFF_NUMERIC: zAff = "NUMERIC";  break;
 76871         case SQLITE_AFF_INTEGER: zAff = "INTEGER";  break;
 76872         case SQLITE_AFF_REAL:    zAff = "REAL";     break;
 76874       sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
 76875       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 76876       sqlite3ExplainPrintf(pOut, ")");
 76877       break;
 76879 #endif /* SQLITE_OMIT_CAST */
 76880     case TK_LT:      zBinOp = "LT";     break;
 76881     case TK_LE:      zBinOp = "LE";     break;
 76882     case TK_GT:      zBinOp = "GT";     break;
 76883     case TK_GE:      zBinOp = "GE";     break;
 76884     case TK_NE:      zBinOp = "NE";     break;
 76885     case TK_EQ:      zBinOp = "EQ";     break;
 76886     case TK_IS:      zBinOp = "IS";     break;
 76887     case TK_ISNOT:   zBinOp = "ISNOT";  break;
 76888     case TK_AND:     zBinOp = "AND";    break;
 76889     case TK_OR:      zBinOp = "OR";     break;
 76890     case TK_PLUS:    zBinOp = "ADD";    break;
 76891     case TK_STAR:    zBinOp = "MUL";    break;
 76892     case TK_MINUS:   zBinOp = "SUB";    break;
 76893     case TK_REM:     zBinOp = "REM";    break;
 76894     case TK_BITAND:  zBinOp = "BITAND"; break;
 76895     case TK_BITOR:   zBinOp = "BITOR";  break;
 76896     case TK_SLASH:   zBinOp = "DIV";    break;
 76897     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
 76898     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
 76899     case TK_CONCAT:  zBinOp = "CONCAT"; break;
 76901     case TK_UMINUS:  zUniOp = "UMINUS"; break;
 76902     case TK_UPLUS:   zUniOp = "UPLUS";  break;
 76903     case TK_BITNOT:  zUniOp = "BITNOT"; break;
 76904     case TK_NOT:     zUniOp = "NOT";    break;
 76905     case TK_ISNULL:  zUniOp = "ISNULL"; break;
 76906     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
 76908     case TK_COLLATE: {
 76909       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 76910       sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
 76911       break;
 76914     case TK_AGG_FUNCTION:
 76915     case TK_CONST_FUNC:
 76916     case TK_FUNCTION: {
 76917       ExprList *pFarg;       /* List of function arguments */
 76918       if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
 76919         pFarg = 0;
 76920       }else{
 76921         pFarg = pExpr->x.pList;
 76923       if( op==TK_AGG_FUNCTION ){
 76924         sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
 76925                              pExpr->op2, pExpr->u.zToken);
 76926       }else{
 76927         sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
 76929       if( pFarg ){
 76930         sqlite3ExplainExprList(pOut, pFarg);
 76932       sqlite3ExplainPrintf(pOut, ")");
 76933       break;
 76935 #ifndef SQLITE_OMIT_SUBQUERY
 76936     case TK_EXISTS: {
 76937       sqlite3ExplainPrintf(pOut, "EXISTS(");
 76938       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 76939       sqlite3ExplainPrintf(pOut,")");
 76940       break;
 76942     case TK_SELECT: {
 76943       sqlite3ExplainPrintf(pOut, "(");
 76944       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 76945       sqlite3ExplainPrintf(pOut, ")");
 76946       break;
 76948     case TK_IN: {
 76949       sqlite3ExplainPrintf(pOut, "IN(");
 76950       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 76951       sqlite3ExplainPrintf(pOut, ",");
 76952       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 76953         sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 76954       }else{
 76955         sqlite3ExplainExprList(pOut, pExpr->x.pList);
 76957       sqlite3ExplainPrintf(pOut, ")");
 76958       break;
 76960 #endif /* SQLITE_OMIT_SUBQUERY */
 76962     /*
 76963     **    x BETWEEN y AND z
 76964     **
 76965     ** This is equivalent to
 76966     **
 76967     **    x>=y AND x<=z
 76968     **
 76969     ** X is stored in pExpr->pLeft.
 76970     ** Y is stored in pExpr->pList->a[0].pExpr.
 76971     ** Z is stored in pExpr->pList->a[1].pExpr.
 76972     */
 76973     case TK_BETWEEN: {
 76974       Expr *pX = pExpr->pLeft;
 76975       Expr *pY = pExpr->x.pList->a[0].pExpr;
 76976       Expr *pZ = pExpr->x.pList->a[1].pExpr;
 76977       sqlite3ExplainPrintf(pOut, "BETWEEN(");
 76978       sqlite3ExplainExpr(pOut, pX);
 76979       sqlite3ExplainPrintf(pOut, ",");
 76980       sqlite3ExplainExpr(pOut, pY);
 76981       sqlite3ExplainPrintf(pOut, ",");
 76982       sqlite3ExplainExpr(pOut, pZ);
 76983       sqlite3ExplainPrintf(pOut, ")");
 76984       break;
 76986     case TK_TRIGGER: {
 76987       /* If the opcode is TK_TRIGGER, then the expression is a reference
 76988       ** to a column in the new.* or old.* pseudo-tables available to
 76989       ** trigger programs. In this case Expr.iTable is set to 1 for the
 76990       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
 76991       ** is set to the column of the pseudo-table to read, or to -1 to
 76992       ** read the rowid field.
 76993       */
 76994       sqlite3ExplainPrintf(pOut, "%s(%d)", 
 76995           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
 76996       break;
 76998     case TK_CASE: {
 76999       sqlite3ExplainPrintf(pOut, "CASE(");
 77000       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 77001       sqlite3ExplainPrintf(pOut, ",");
 77002       sqlite3ExplainExprList(pOut, pExpr->x.pList);
 77003       break;
 77005 #ifndef SQLITE_OMIT_TRIGGER
 77006     case TK_RAISE: {
 77007       const char *zType = "unk";
 77008       switch( pExpr->affinity ){
 77009         case OE_Rollback:   zType = "rollback";  break;
 77010         case OE_Abort:      zType = "abort";     break;
 77011         case OE_Fail:       zType = "fail";      break;
 77012         case OE_Ignore:     zType = "ignore";    break;
 77014       sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
 77015       break;
 77017 #endif
 77019   if( zBinOp ){
 77020     sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
 77021     sqlite3ExplainExpr(pOut, pExpr->pLeft);
 77022     sqlite3ExplainPrintf(pOut,",");
 77023     sqlite3ExplainExpr(pOut, pExpr->pRight);
 77024     sqlite3ExplainPrintf(pOut,")");
 77025   }else if( zUniOp ){
 77026     sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
 77027     sqlite3ExplainExpr(pOut, pExpr->pLeft);
 77028     sqlite3ExplainPrintf(pOut,")");
 77031 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
 77033 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 77034 /*
 77035 ** Generate a human-readable explanation of an expression list.
 77036 */
 77037 SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
 77038   int i;
 77039   if( pList==0 || pList->nExpr==0 ){
 77040     sqlite3ExplainPrintf(pOut, "(empty-list)");
 77041     return;
 77042   }else if( pList->nExpr==1 ){
 77043     sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
 77044   }else{
 77045     sqlite3ExplainPush(pOut);
 77046     for(i=0; i<pList->nExpr; i++){
 77047       sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
 77048       sqlite3ExplainPush(pOut);
 77049       sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
 77050       sqlite3ExplainPop(pOut);
 77051       if( i<pList->nExpr-1 ){
 77052         sqlite3ExplainNL(pOut);
 77055     sqlite3ExplainPop(pOut);
 77058 #endif /* SQLITE_DEBUG */
 77060 /*
 77061 ** Return TRUE if pExpr is an constant expression that is appropriate
 77062 ** for factoring out of a loop.  Appropriate expressions are:
 77063 **
 77064 **    *  Any expression that evaluates to two or more opcodes.
 77065 **
 77066 **    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, 
 77067 **       or OP_Variable that does not need to be placed in a 
 77068 **       specific register.
 77069 **
 77070 ** There is no point in factoring out single-instruction constant
 77071 ** expressions that need to be placed in a particular register.  
 77072 ** We could factor them out, but then we would end up adding an
 77073 ** OP_SCopy instruction to move the value into the correct register
 77074 ** later.  We might as well just use the original instruction and
 77075 ** avoid the OP_SCopy.
 77076 */
 77077 static int isAppropriateForFactoring(Expr *p){
 77078   if( !sqlite3ExprIsConstantNotJoin(p) ){
 77079     return 0;  /* Only constant expressions are appropriate for factoring */
 77081   if( (p->flags & EP_FixedDest)==0 ){
 77082     return 1;  /* Any constant without a fixed destination is appropriate */
 77084   while( p->op==TK_UPLUS ) p = p->pLeft;
 77085   switch( p->op ){
 77086 #ifndef SQLITE_OMIT_BLOB_LITERAL
 77087     case TK_BLOB:
 77088 #endif
 77089     case TK_VARIABLE:
 77090     case TK_INTEGER:
 77091     case TK_FLOAT:
 77092     case TK_NULL:
 77093     case TK_STRING: {
 77094       testcase( p->op==TK_BLOB );
 77095       testcase( p->op==TK_VARIABLE );
 77096       testcase( p->op==TK_INTEGER );
 77097       testcase( p->op==TK_FLOAT );
 77098       testcase( p->op==TK_NULL );
 77099       testcase( p->op==TK_STRING );
 77100       /* Single-instruction constants with a fixed destination are
 77101       ** better done in-line.  If we factor them, they will just end
 77102       ** up generating an OP_SCopy to move the value to the destination
 77103       ** register. */
 77104       return 0;
 77106     case TK_UMINUS: {
 77107       if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
 77108         return 0;
 77110       break;
 77112     default: {
 77113       break;
 77116   return 1;
 77119 /*
 77120 ** If pExpr is a constant expression that is appropriate for
 77121 ** factoring out of a loop, then evaluate the expression
 77122 ** into a register and convert the expression into a TK_REGISTER
 77123 ** expression.
 77124 */
 77125 static int evalConstExpr(Walker *pWalker, Expr *pExpr){
 77126   Parse *pParse = pWalker->pParse;
 77127   switch( pExpr->op ){
 77128     case TK_IN:
 77129     case TK_REGISTER: {
 77130       return WRC_Prune;
 77132     case TK_COLLATE: {
 77133       return WRC_Continue;
 77135     case TK_FUNCTION:
 77136     case TK_AGG_FUNCTION:
 77137     case TK_CONST_FUNC: {
 77138       /* The arguments to a function have a fixed destination.
 77139       ** Mark them this way to avoid generated unneeded OP_SCopy
 77140       ** instructions. 
 77141       */
 77142       ExprList *pList = pExpr->x.pList;
 77143       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 77144       if( pList ){
 77145         int i = pList->nExpr;
 77146         struct ExprList_item *pItem = pList->a;
 77147         for(; i>0; i--, pItem++){
 77148           if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
 77151       break;
 77154   if( isAppropriateForFactoring(pExpr) ){
 77155     int r1 = ++pParse->nMem;
 77156     int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 77157     /* If r2!=r1, it means that register r1 is never used.  That is harmless
 77158     ** but suboptimal, so we want to know about the situation to fix it.
 77159     ** Hence the following assert: */
 77160     assert( r2==r1 );
 77161     pExpr->op2 = pExpr->op;
 77162     pExpr->op = TK_REGISTER;
 77163     pExpr->iTable = r2;
 77164     return WRC_Prune;
 77166   return WRC_Continue;
 77169 /*
 77170 ** Preevaluate constant subexpressions within pExpr and store the
 77171 ** results in registers.  Modify pExpr so that the constant subexpresions
 77172 ** are TK_REGISTER opcodes that refer to the precomputed values.
 77173 **
 77174 ** This routine is a no-op if the jump to the cookie-check code has
 77175 ** already occur.  Since the cookie-check jump is generated prior to
 77176 ** any other serious processing, this check ensures that there is no
 77177 ** way to accidently bypass the constant initializations.
 77178 **
 77179 ** This routine is also a no-op if the SQLITE_FactorOutConst optimization
 77180 ** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
 77181 ** interface.  This allows test logic to verify that the same answer is
 77182 ** obtained for queries regardless of whether or not constants are
 77183 ** precomputed into registers or if they are inserted in-line.
 77184 */
 77185 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
 77186   Walker w;
 77187   if( pParse->cookieGoto ) return;
 77188   if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
 77189   w.xExprCallback = evalConstExpr;
 77190   w.xSelectCallback = 0;
 77191   w.pParse = pParse;
 77192   sqlite3WalkExpr(&w, pExpr);
 77196 /*
 77197 ** Generate code that pushes the value of every element of the given
 77198 ** expression list into a sequence of registers beginning at target.
 77199 **
 77200 ** Return the number of elements evaluated.
 77201 */
 77202 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
 77203   Parse *pParse,     /* Parsing context */
 77204   ExprList *pList,   /* The expression list to be coded */
 77205   int target,        /* Where to write results */
 77206   int doHardCopy     /* Make a hard copy of every element */
 77207 ){
 77208   struct ExprList_item *pItem;
 77209   int i, n;
 77210   assert( pList!=0 );
 77211   assert( target>0 );
 77212   assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
 77213   n = pList->nExpr;
 77214   for(pItem=pList->a, i=0; i<n; i++, pItem++){
 77215     Expr *pExpr = pItem->pExpr;
 77216     int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
 77217     if( inReg!=target+i ){
 77218       sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
 77219                         inReg, target+i);
 77222   return n;
 77225 /*
 77226 ** Generate code for a BETWEEN operator.
 77227 **
 77228 **    x BETWEEN y AND z
 77229 **
 77230 ** The above is equivalent to 
 77231 **
 77232 **    x>=y AND x<=z
 77233 **
 77234 ** Code it as such, taking care to do the common subexpression
 77235 ** elementation of x.
 77236 */
 77237 static void exprCodeBetween(
 77238   Parse *pParse,    /* Parsing and code generating context */
 77239   Expr *pExpr,      /* The BETWEEN expression */
 77240   int dest,         /* Jump here if the jump is taken */
 77241   int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
 77242   int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
 77243 ){
 77244   Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
 77245   Expr compLeft;    /* The  x>=y  term */
 77246   Expr compRight;   /* The  x<=z  term */
 77247   Expr exprX;       /* The  x  subexpression */
 77248   int regFree1 = 0; /* Temporary use register */
 77250   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 77251   exprX = *pExpr->pLeft;
 77252   exprAnd.op = TK_AND;
 77253   exprAnd.pLeft = &compLeft;
 77254   exprAnd.pRight = &compRight;
 77255   compLeft.op = TK_GE;
 77256   compLeft.pLeft = &exprX;
 77257   compLeft.pRight = pExpr->x.pList->a[0].pExpr;
 77258   compRight.op = TK_LE;
 77259   compRight.pLeft = &exprX;
 77260   compRight.pRight = pExpr->x.pList->a[1].pExpr;
 77261   exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
 77262   exprX.op = TK_REGISTER;
 77263   if( jumpIfTrue ){
 77264     sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
 77265   }else{
 77266     sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
 77268   sqlite3ReleaseTempReg(pParse, regFree1);
 77270   /* Ensure adequate test coverage */
 77271   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
 77272   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
 77273   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
 77274   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
 77275   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
 77276   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
 77277   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
 77278   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
 77281 /*
 77282 ** Generate code for a boolean expression such that a jump is made
 77283 ** to the label "dest" if the expression is true but execution
 77284 ** continues straight thru if the expression is false.
 77285 **
 77286 ** If the expression evaluates to NULL (neither true nor false), then
 77287 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
 77288 **
 77289 ** This code depends on the fact that certain token values (ex: TK_EQ)
 77290 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
 77291 ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
 77292 ** the make process cause these values to align.  Assert()s in the code
 77293 ** below verify that the numbers are aligned correctly.
 77294 */
 77295 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
 77296   Vdbe *v = pParse->pVdbe;
 77297   int op = 0;
 77298   int regFree1 = 0;
 77299   int regFree2 = 0;
 77300   int r1, r2;
 77302   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
 77303   if( NEVER(v==0) )     return;  /* Existance of VDBE checked by caller */
 77304   if( NEVER(pExpr==0) ) return;  /* No way this can happen */
 77305   op = pExpr->op;
 77306   switch( op ){
 77307     case TK_AND: {
 77308       int d2 = sqlite3VdbeMakeLabel(v);
 77309       testcase( jumpIfNull==0 );
 77310       sqlite3ExprCachePush(pParse);
 77311       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
 77312       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 77313       sqlite3VdbeResolveLabel(v, d2);
 77314       sqlite3ExprCachePop(pParse, 1);
 77315       break;
 77317     case TK_OR: {
 77318       testcase( jumpIfNull==0 );
 77319       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 77320       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 77321       break;
 77323     case TK_NOT: {
 77324       testcase( jumpIfNull==0 );
 77325       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 77326       break;
 77328     case TK_LT:
 77329     case TK_LE:
 77330     case TK_GT:
 77331     case TK_GE:
 77332     case TK_NE:
 77333     case TK_EQ: {
 77334       assert( TK_LT==OP_Lt );
 77335       assert( TK_LE==OP_Le );
 77336       assert( TK_GT==OP_Gt );
 77337       assert( TK_GE==OP_Ge );
 77338       assert( TK_EQ==OP_Eq );
 77339       assert( TK_NE==OP_Ne );
 77340       testcase( op==TK_LT );
 77341       testcase( op==TK_LE );
 77342       testcase( op==TK_GT );
 77343       testcase( op==TK_GE );
 77344       testcase( op==TK_EQ );
 77345       testcase( op==TK_NE );
 77346       testcase( jumpIfNull==0 );
 77347       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 77348       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 77349       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 77350                   r1, r2, dest, jumpIfNull);
 77351       testcase( regFree1==0 );
 77352       testcase( regFree2==0 );
 77353       break;
 77355     case TK_IS:
 77356     case TK_ISNOT: {
 77357       testcase( op==TK_IS );
 77358       testcase( op==TK_ISNOT );
 77359       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 77360       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 77361       op = (op==TK_IS) ? TK_EQ : TK_NE;
 77362       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 77363                   r1, r2, dest, SQLITE_NULLEQ);
 77364       testcase( regFree1==0 );
 77365       testcase( regFree2==0 );
 77366       break;
 77368     case TK_ISNULL:
 77369     case TK_NOTNULL: {
 77370       assert( TK_ISNULL==OP_IsNull );
 77371       assert( TK_NOTNULL==OP_NotNull );
 77372       testcase( op==TK_ISNULL );
 77373       testcase( op==TK_NOTNULL );
 77374       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 77375       sqlite3VdbeAddOp2(v, op, r1, dest);
 77376       testcase( regFree1==0 );
 77377       break;
 77379     case TK_BETWEEN: {
 77380       testcase( jumpIfNull==0 );
 77381       exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
 77382       break;
 77384 #ifndef SQLITE_OMIT_SUBQUERY
 77385     case TK_IN: {
 77386       int destIfFalse = sqlite3VdbeMakeLabel(v);
 77387       int destIfNull = jumpIfNull ? dest : destIfFalse;
 77388       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 77389       sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 77390       sqlite3VdbeResolveLabel(v, destIfFalse);
 77391       break;
 77393 #endif
 77394     default: {
 77395       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 77396       sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
 77397       testcase( regFree1==0 );
 77398       testcase( jumpIfNull==0 );
 77399       break;
 77402   sqlite3ReleaseTempReg(pParse, regFree1);
 77403   sqlite3ReleaseTempReg(pParse, regFree2);  
 77406 /*
 77407 ** Generate code for a boolean expression such that a jump is made
 77408 ** to the label "dest" if the expression is false but execution
 77409 ** continues straight thru if the expression is true.
 77410 **
 77411 ** If the expression evaluates to NULL (neither true nor false) then
 77412 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
 77413 ** is 0.
 77414 */
 77415 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
 77416   Vdbe *v = pParse->pVdbe;
 77417   int op = 0;
 77418   int regFree1 = 0;
 77419   int regFree2 = 0;
 77420   int r1, r2;
 77422   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
 77423   if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
 77424   if( pExpr==0 )    return;
 77426   /* The value of pExpr->op and op are related as follows:
 77427   **
 77428   **       pExpr->op            op
 77429   **       ---------          ----------
 77430   **       TK_ISNULL          OP_NotNull
 77431   **       TK_NOTNULL         OP_IsNull
 77432   **       TK_NE              OP_Eq
 77433   **       TK_EQ              OP_Ne
 77434   **       TK_GT              OP_Le
 77435   **       TK_LE              OP_Gt
 77436   **       TK_GE              OP_Lt
 77437   **       TK_LT              OP_Ge
 77438   **
 77439   ** For other values of pExpr->op, op is undefined and unused.
 77440   ** The value of TK_ and OP_ constants are arranged such that we
 77441   ** can compute the mapping above using the following expression.
 77442   ** Assert()s verify that the computation is correct.
 77443   */
 77444   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
 77446   /* Verify correct alignment of TK_ and OP_ constants
 77447   */
 77448   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
 77449   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
 77450   assert( pExpr->op!=TK_NE || op==OP_Eq );
 77451   assert( pExpr->op!=TK_EQ || op==OP_Ne );
 77452   assert( pExpr->op!=TK_LT || op==OP_Ge );
 77453   assert( pExpr->op!=TK_LE || op==OP_Gt );
 77454   assert( pExpr->op!=TK_GT || op==OP_Le );
 77455   assert( pExpr->op!=TK_GE || op==OP_Lt );
 77457   switch( pExpr->op ){
 77458     case TK_AND: {
 77459       testcase( jumpIfNull==0 );
 77460       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 77461       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 77462       break;
 77464     case TK_OR: {
 77465       int d2 = sqlite3VdbeMakeLabel(v);
 77466       testcase( jumpIfNull==0 );
 77467       sqlite3ExprCachePush(pParse);
 77468       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
 77469       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 77470       sqlite3VdbeResolveLabel(v, d2);
 77471       sqlite3ExprCachePop(pParse, 1);
 77472       break;
 77474     case TK_NOT: {
 77475       testcase( jumpIfNull==0 );
 77476       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 77477       break;
 77479     case TK_LT:
 77480     case TK_LE:
 77481     case TK_GT:
 77482     case TK_GE:
 77483     case TK_NE:
 77484     case TK_EQ: {
 77485       testcase( op==TK_LT );
 77486       testcase( op==TK_LE );
 77487       testcase( op==TK_GT );
 77488       testcase( op==TK_GE );
 77489       testcase( op==TK_EQ );
 77490       testcase( op==TK_NE );
 77491       testcase( jumpIfNull==0 );
 77492       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 77493       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 77494       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 77495                   r1, r2, dest, jumpIfNull);
 77496       testcase( regFree1==0 );
 77497       testcase( regFree2==0 );
 77498       break;
 77500     case TK_IS:
 77501     case TK_ISNOT: {
 77502       testcase( pExpr->op==TK_IS );
 77503       testcase( pExpr->op==TK_ISNOT );
 77504       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 77505       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 77506       op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
 77507       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 77508                   r1, r2, dest, SQLITE_NULLEQ);
 77509       testcase( regFree1==0 );
 77510       testcase( regFree2==0 );
 77511       break;
 77513     case TK_ISNULL:
 77514     case TK_NOTNULL: {
 77515       testcase( op==TK_ISNULL );
 77516       testcase( op==TK_NOTNULL );
 77517       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 77518       sqlite3VdbeAddOp2(v, op, r1, dest);
 77519       testcase( regFree1==0 );
 77520       break;
 77522     case TK_BETWEEN: {
 77523       testcase( jumpIfNull==0 );
 77524       exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
 77525       break;
 77527 #ifndef SQLITE_OMIT_SUBQUERY
 77528     case TK_IN: {
 77529       if( jumpIfNull ){
 77530         sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
 77531       }else{
 77532         int destIfNull = sqlite3VdbeMakeLabel(v);
 77533         sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
 77534         sqlite3VdbeResolveLabel(v, destIfNull);
 77536       break;
 77538 #endif
 77539     default: {
 77540       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 77541       sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
 77542       testcase( regFree1==0 );
 77543       testcase( jumpIfNull==0 );
 77544       break;
 77547   sqlite3ReleaseTempReg(pParse, regFree1);
 77548   sqlite3ReleaseTempReg(pParse, regFree2);
 77551 /*
 77552 ** Do a deep comparison of two expression trees.  Return 0 if the two
 77553 ** expressions are completely identical.  Return 1 if they differ only
 77554 ** by a COLLATE operator at the top level.  Return 2 if there are differences
 77555 ** other than the top-level COLLATE operator.
 77556 **
 77557 ** Sometimes this routine will return 2 even if the two expressions
 77558 ** really are equivalent.  If we cannot prove that the expressions are
 77559 ** identical, we return 2 just to be safe.  So if this routine
 77560 ** returns 2, then you do not really know for certain if the two
 77561 ** expressions are the same.  But if you get a 0 or 1 return, then you
 77562 ** can be sure the expressions are the same.  In the places where
 77563 ** this routine is used, it does not hurt to get an extra 2 - that
 77564 ** just might result in some slightly slower code.  But returning
 77565 ** an incorrect 0 or 1 could lead to a malfunction.
 77566 */
 77567 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
 77568   if( pA==0||pB==0 ){
 77569     return pB==pA ? 0 : 2;
 77571   assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
 77572   assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
 77573   if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
 77574     return 2;
 77576   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 77577   if( pA->op!=pB->op ){
 77578     if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB)<2 ){
 77579       return 1;
 77581     if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft)<2 ){
 77582       return 1;
 77584     return 2;
 77586   if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
 77587   if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
 77588   if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
 77589   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
 77590   if( ExprHasProperty(pA, EP_IntValue) ){
 77591     if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
 77592       return 2;
 77594   }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
 77595     if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
 77596     if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 77597       return pA->op==TK_COLLATE ? 1 : 2;
 77600   return 0;
 77603 /*
 77604 ** Compare two ExprList objects.  Return 0 if they are identical and 
 77605 ** non-zero if they differ in any way.
 77606 **
 77607 ** This routine might return non-zero for equivalent ExprLists.  The
 77608 ** only consequence will be disabled optimizations.  But this routine
 77609 ** must never return 0 if the two ExprList objects are different, or
 77610 ** a malfunction will result.
 77611 **
 77612 ** Two NULL pointers are considered to be the same.  But a NULL pointer
 77613 ** always differs from a non-NULL pointer.
 77614 */
 77615 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
 77616   int i;
 77617   if( pA==0 && pB==0 ) return 0;
 77618   if( pA==0 || pB==0 ) return 1;
 77619   if( pA->nExpr!=pB->nExpr ) return 1;
 77620   for(i=0; i<pA->nExpr; i++){
 77621     Expr *pExprA = pA->a[i].pExpr;
 77622     Expr *pExprB = pB->a[i].pExpr;
 77623     if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
 77624     if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
 77626   return 0;
 77629 /*
 77630 ** An instance of the following structure is used by the tree walker
 77631 ** to count references to table columns in the arguments of an 
 77632 ** aggregate function, in order to implement the
 77633 ** sqlite3FunctionThisSrc() routine.
 77634 */
 77635 struct SrcCount {
 77636   SrcList *pSrc;   /* One particular FROM clause in a nested query */
 77637   int nThis;       /* Number of references to columns in pSrcList */
 77638   int nOther;      /* Number of references to columns in other FROM clauses */
 77639 };
 77641 /*
 77642 ** Count the number of references to columns.
 77643 */
 77644 static int exprSrcCount(Walker *pWalker, Expr *pExpr){
 77645   /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
 77646   ** is always called before sqlite3ExprAnalyzeAggregates() and so the
 77647   ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
 77648   ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
 77649   ** NEVER() will need to be removed. */
 77650   if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
 77651     int i;
 77652     struct SrcCount *p = pWalker->u.pSrcCount;
 77653     SrcList *pSrc = p->pSrc;
 77654     for(i=0; i<pSrc->nSrc; i++){
 77655       if( pExpr->iTable==pSrc->a[i].iCursor ) break;
 77657     if( i<pSrc->nSrc ){
 77658       p->nThis++;
 77659     }else{
 77660       p->nOther++;
 77663   return WRC_Continue;
 77666 /*
 77667 ** Determine if any of the arguments to the pExpr Function reference
 77668 ** pSrcList.  Return true if they do.  Also return true if the function
 77669 ** has no arguments or has only constant arguments.  Return false if pExpr
 77670 ** references columns but not columns of tables found in pSrcList.
 77671 */
 77672 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
 77673   Walker w;
 77674   struct SrcCount cnt;
 77675   assert( pExpr->op==TK_AGG_FUNCTION );
 77676   memset(&w, 0, sizeof(w));
 77677   w.xExprCallback = exprSrcCount;
 77678   w.u.pSrcCount = &cnt;
 77679   cnt.pSrc = pSrcList;
 77680   cnt.nThis = 0;
 77681   cnt.nOther = 0;
 77682   sqlite3WalkExprList(&w, pExpr->x.pList);
 77683   return cnt.nThis>0 || cnt.nOther==0;
 77686 /*
 77687 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
 77688 ** the new element.  Return a negative number if malloc fails.
 77689 */
 77690 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
 77691   int i;
 77692   pInfo->aCol = sqlite3ArrayAllocate(
 77693        db,
 77694        pInfo->aCol,
 77695        sizeof(pInfo->aCol[0]),
 77696        &pInfo->nColumn,
 77697        &i
 77698   );
 77699   return i;
 77702 /*
 77703 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
 77704 ** the new element.  Return a negative number if malloc fails.
 77705 */
 77706 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
 77707   int i;
 77708   pInfo->aFunc = sqlite3ArrayAllocate(
 77709        db, 
 77710        pInfo->aFunc,
 77711        sizeof(pInfo->aFunc[0]),
 77712        &pInfo->nFunc,
 77713        &i
 77714   );
 77715   return i;
 77718 /*
 77719 ** This is the xExprCallback for a tree walker.  It is used to
 77720 ** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
 77721 ** for additional information.
 77722 */
 77723 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
 77724   int i;
 77725   NameContext *pNC = pWalker->u.pNC;
 77726   Parse *pParse = pNC->pParse;
 77727   SrcList *pSrcList = pNC->pSrcList;
 77728   AggInfo *pAggInfo = pNC->pAggInfo;
 77730   switch( pExpr->op ){
 77731     case TK_AGG_COLUMN:
 77732     case TK_COLUMN: {
 77733       testcase( pExpr->op==TK_AGG_COLUMN );
 77734       testcase( pExpr->op==TK_COLUMN );
 77735       /* Check to see if the column is in one of the tables in the FROM
 77736       ** clause of the aggregate query */
 77737       if( ALWAYS(pSrcList!=0) ){
 77738         struct SrcList_item *pItem = pSrcList->a;
 77739         for(i=0; i<pSrcList->nSrc; i++, pItem++){
 77740           struct AggInfo_col *pCol;
 77741           assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 77742           if( pExpr->iTable==pItem->iCursor ){
 77743             /* If we reach this point, it means that pExpr refers to a table
 77744             ** that is in the FROM clause of the aggregate query.  
 77745             **
 77746             ** Make an entry for the column in pAggInfo->aCol[] if there
 77747             ** is not an entry there already.
 77748             */
 77749             int k;
 77750             pCol = pAggInfo->aCol;
 77751             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
 77752               if( pCol->iTable==pExpr->iTable &&
 77753                   pCol->iColumn==pExpr->iColumn ){
 77754                 break;
 77757             if( (k>=pAggInfo->nColumn)
 77758              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
 77759             ){
 77760               pCol = &pAggInfo->aCol[k];
 77761               pCol->pTab = pExpr->pTab;
 77762               pCol->iTable = pExpr->iTable;
 77763               pCol->iColumn = pExpr->iColumn;
 77764               pCol->iMem = ++pParse->nMem;
 77765               pCol->iSorterColumn = -1;
 77766               pCol->pExpr = pExpr;
 77767               if( pAggInfo->pGroupBy ){
 77768                 int j, n;
 77769                 ExprList *pGB = pAggInfo->pGroupBy;
 77770                 struct ExprList_item *pTerm = pGB->a;
 77771                 n = pGB->nExpr;
 77772                 for(j=0; j<n; j++, pTerm++){
 77773                   Expr *pE = pTerm->pExpr;
 77774                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
 77775                       pE->iColumn==pExpr->iColumn ){
 77776                     pCol->iSorterColumn = j;
 77777                     break;
 77781               if( pCol->iSorterColumn<0 ){
 77782                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
 77785             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
 77786             ** because it was there before or because we just created it).
 77787             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
 77788             ** pAggInfo->aCol[] entry.
 77789             */
 77790             ExprSetIrreducible(pExpr);
 77791             pExpr->pAggInfo = pAggInfo;
 77792             pExpr->op = TK_AGG_COLUMN;
 77793             pExpr->iAgg = (i16)k;
 77794             break;
 77795           } /* endif pExpr->iTable==pItem->iCursor */
 77796         } /* end loop over pSrcList */
 77798       return WRC_Prune;
 77800     case TK_AGG_FUNCTION: {
 77801       if( (pNC->ncFlags & NC_InAggFunc)==0
 77802        && pWalker->walkerDepth==pExpr->op2
 77803       ){
 77804         /* Check to see if pExpr is a duplicate of another aggregate 
 77805         ** function that is already in the pAggInfo structure
 77806         */
 77807         struct AggInfo_func *pItem = pAggInfo->aFunc;
 77808         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
 77809           if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
 77810             break;
 77813         if( i>=pAggInfo->nFunc ){
 77814           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
 77815           */
 77816           u8 enc = ENC(pParse->db);
 77817           i = addAggInfoFunc(pParse->db, pAggInfo);
 77818           if( i>=0 ){
 77819             assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 77820             pItem = &pAggInfo->aFunc[i];
 77821             pItem->pExpr = pExpr;
 77822             pItem->iMem = ++pParse->nMem;
 77823             assert( !ExprHasProperty(pExpr, EP_IntValue) );
 77824             pItem->pFunc = sqlite3FindFunction(pParse->db,
 77825                    pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
 77826                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
 77827             if( pExpr->flags & EP_Distinct ){
 77828               pItem->iDistinct = pParse->nTab++;
 77829             }else{
 77830               pItem->iDistinct = -1;
 77834         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
 77835         */
 77836         assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 77837         ExprSetIrreducible(pExpr);
 77838         pExpr->iAgg = (i16)i;
 77839         pExpr->pAggInfo = pAggInfo;
 77840         return WRC_Prune;
 77841       }else{
 77842         return WRC_Continue;
 77846   return WRC_Continue;
 77848 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
 77849   UNUSED_PARAMETER(pWalker);
 77850   UNUSED_PARAMETER(pSelect);
 77851   return WRC_Continue;
 77854 /*
 77855 ** Analyze the pExpr expression looking for aggregate functions and
 77856 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
 77857 ** points to.  Additional entries are made on the AggInfo object as
 77858 ** necessary.
 77859 **
 77860 ** This routine should only be called after the expression has been
 77861 ** analyzed by sqlite3ResolveExprNames().
 77862 */
 77863 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
 77864   Walker w;
 77865   memset(&w, 0, sizeof(w));
 77866   w.xExprCallback = analyzeAggregate;
 77867   w.xSelectCallback = analyzeAggregatesInSelect;
 77868   w.u.pNC = pNC;
 77869   assert( pNC->pSrcList!=0 );
 77870   sqlite3WalkExpr(&w, pExpr);
 77873 /*
 77874 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
 77875 ** expression list.  Return the number of errors.
 77876 **
 77877 ** If an error is found, the analysis is cut short.
 77878 */
 77879 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
 77880   struct ExprList_item *pItem;
 77881   int i;
 77882   if( pList ){
 77883     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
 77884       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
 77889 /*
 77890 ** Allocate a single new register for use to hold some intermediate result.
 77891 */
 77892 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
 77893   if( pParse->nTempReg==0 ){
 77894     return ++pParse->nMem;
 77896   return pParse->aTempReg[--pParse->nTempReg];
 77899 /*
 77900 ** Deallocate a register, making available for reuse for some other
 77901 ** purpose.
 77902 **
 77903 ** If a register is currently being used by the column cache, then
 77904 ** the dallocation is deferred until the column cache line that uses
 77905 ** the register becomes stale.
 77906 */
 77907 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
 77908   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 77909     int i;
 77910     struct yColCache *p;
 77911     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 77912       if( p->iReg==iReg ){
 77913         p->tempReg = 1;
 77914         return;
 77917     pParse->aTempReg[pParse->nTempReg++] = iReg;
 77921 /*
 77922 ** Allocate or deallocate a block of nReg consecutive registers
 77923 */
 77924 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
 77925   int i, n;
 77926   i = pParse->iRangeReg;
 77927   n = pParse->nRangeReg;
 77928   if( nReg<=n ){
 77929     assert( !usedAsColumnCache(pParse, i, i+n-1) );
 77930     pParse->iRangeReg += nReg;
 77931     pParse->nRangeReg -= nReg;
 77932   }else{
 77933     i = pParse->nMem+1;
 77934     pParse->nMem += nReg;
 77936   return i;
 77938 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
 77939   sqlite3ExprCacheRemove(pParse, iReg, nReg);
 77940   if( nReg>pParse->nRangeReg ){
 77941     pParse->nRangeReg = nReg;
 77942     pParse->iRangeReg = iReg;
 77946 /*
 77947 ** Mark all temporary registers as being unavailable for reuse.
 77948 */
 77949 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
 77950   pParse->nTempReg = 0;
 77951   pParse->nRangeReg = 0;
 77954 /************** End of expr.c ************************************************/
 77955 /************** Begin file alter.c *******************************************/
 77956 /*
 77957 ** 2005 February 15
 77958 **
 77959 ** The author disclaims copyright to this source code.  In place of
 77960 ** a legal notice, here is a blessing:
 77961 **
 77962 **    May you do good and not evil.
 77963 **    May you find forgiveness for yourself and forgive others.
 77964 **    May you share freely, never taking more than you give.
 77965 **
 77966 *************************************************************************
 77967 ** This file contains C code routines that used to generate VDBE code
 77968 ** that implements the ALTER TABLE command.
 77969 */
 77971 /*
 77972 ** The code in this file only exists if we are not omitting the
 77973 ** ALTER TABLE logic from the build.
 77974 */
 77975 #ifndef SQLITE_OMIT_ALTERTABLE
 77978 /*
 77979 ** This function is used by SQL generated to implement the 
 77980 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
 77981 ** CREATE INDEX command. The second is a table name. The table name in 
 77982 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
 77983 ** argument and the result returned. Examples:
 77984 **
 77985 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
 77986 **     -> 'CREATE TABLE def(a, b, c)'
 77987 **
 77988 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
 77989 **     -> 'CREATE INDEX i ON def(a, b, c)'
 77990 */
 77991 static void renameTableFunc(
 77992   sqlite3_context *context,
 77993   int NotUsed,
 77994   sqlite3_value **argv
 77995 ){
 77996   unsigned char const *zSql = sqlite3_value_text(argv[0]);
 77997   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
 77999   int token;
 78000   Token tname;
 78001   unsigned char const *zCsr = zSql;
 78002   int len = 0;
 78003   char *zRet;
 78005   sqlite3 *db = sqlite3_context_db_handle(context);
 78007   UNUSED_PARAMETER(NotUsed);
 78009   /* The principle used to locate the table name in the CREATE TABLE 
 78010   ** statement is that the table name is the first non-space token that
 78011   ** is immediately followed by a TK_LP or TK_USING token.
 78012   */
 78013   if( zSql ){
 78014     do {
 78015       if( !*zCsr ){
 78016         /* Ran out of input before finding an opening bracket. Return NULL. */
 78017         return;
 78020       /* Store the token that zCsr points to in tname. */
 78021       tname.z = (char*)zCsr;
 78022       tname.n = len;
 78024       /* Advance zCsr to the next token. Store that token type in 'token',
 78025       ** and its length in 'len' (to be used next iteration of this loop).
 78026       */
 78027       do {
 78028         zCsr += len;
 78029         len = sqlite3GetToken(zCsr, &token);
 78030       } while( token==TK_SPACE );
 78031       assert( len>0 );
 78032     } while( token!=TK_LP && token!=TK_USING );
 78034     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
 78035        zTableName, tname.z+tname.n);
 78036     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 78040 /*
 78041 ** This C function implements an SQL user function that is used by SQL code
 78042 ** generated by the ALTER TABLE ... RENAME command to modify the definition
 78043 ** of any foreign key constraints that use the table being renamed as the 
 78044 ** parent table. It is passed three arguments:
 78045 **
 78046 **   1) The complete text of the CREATE TABLE statement being modified,
 78047 **   2) The old name of the table being renamed, and
 78048 **   3) The new name of the table being renamed.
 78049 **
 78050 ** It returns the new CREATE TABLE statement. For example:
 78051 **
 78052 **   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
 78053 **       -> 'CREATE TABLE t1(a REFERENCES t3)'
 78054 */
 78055 #ifndef SQLITE_OMIT_FOREIGN_KEY
 78056 static void renameParentFunc(
 78057   sqlite3_context *context,
 78058   int NotUsed,
 78059   sqlite3_value **argv
 78060 ){
 78061   sqlite3 *db = sqlite3_context_db_handle(context);
 78062   char *zOutput = 0;
 78063   char *zResult;
 78064   unsigned char const *zInput = sqlite3_value_text(argv[0]);
 78065   unsigned char const *zOld = sqlite3_value_text(argv[1]);
 78066   unsigned char const *zNew = sqlite3_value_text(argv[2]);
 78068   unsigned const char *z;         /* Pointer to token */
 78069   int n;                          /* Length of token z */
 78070   int token;                      /* Type of token */
 78072   UNUSED_PARAMETER(NotUsed);
 78073   for(z=zInput; *z; z=z+n){
 78074     n = sqlite3GetToken(z, &token);
 78075     if( token==TK_REFERENCES ){
 78076       char *zParent;
 78077       do {
 78078         z += n;
 78079         n = sqlite3GetToken(z, &token);
 78080       }while( token==TK_SPACE );
 78082       zParent = sqlite3DbStrNDup(db, (const char *)z, n);
 78083       if( zParent==0 ) break;
 78084       sqlite3Dequote(zParent);
 78085       if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
 78086         char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
 78087             (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
 78088         );
 78089         sqlite3DbFree(db, zOutput);
 78090         zOutput = zOut;
 78091         zInput = &z[n];
 78093       sqlite3DbFree(db, zParent);
 78097   zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
 78098   sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
 78099   sqlite3DbFree(db, zOutput);
 78101 #endif
 78103 #ifndef SQLITE_OMIT_TRIGGER
 78104 /* This function is used by SQL generated to implement the
 78105 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
 78106 ** statement. The second is a table name. The table name in the CREATE 
 78107 ** TRIGGER statement is replaced with the third argument and the result 
 78108 ** returned. This is analagous to renameTableFunc() above, except for CREATE
 78109 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
 78110 */
 78111 static void renameTriggerFunc(
 78112   sqlite3_context *context,
 78113   int NotUsed,
 78114   sqlite3_value **argv
 78115 ){
 78116   unsigned char const *zSql = sqlite3_value_text(argv[0]);
 78117   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
 78119   int token;
 78120   Token tname;
 78121   int dist = 3;
 78122   unsigned char const *zCsr = zSql;
 78123   int len = 0;
 78124   char *zRet;
 78125   sqlite3 *db = sqlite3_context_db_handle(context);
 78127   UNUSED_PARAMETER(NotUsed);
 78129   /* The principle used to locate the table name in the CREATE TRIGGER 
 78130   ** statement is that the table name is the first token that is immediatedly
 78131   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
 78132   ** of TK_WHEN, TK_BEGIN or TK_FOR.
 78133   */
 78134   if( zSql ){
 78135     do {
 78137       if( !*zCsr ){
 78138         /* Ran out of input before finding the table name. Return NULL. */
 78139         return;
 78142       /* Store the token that zCsr points to in tname. */
 78143       tname.z = (char*)zCsr;
 78144       tname.n = len;
 78146       /* Advance zCsr to the next token. Store that token type in 'token',
 78147       ** and its length in 'len' (to be used next iteration of this loop).
 78148       */
 78149       do {
 78150         zCsr += len;
 78151         len = sqlite3GetToken(zCsr, &token);
 78152       }while( token==TK_SPACE );
 78153       assert( len>0 );
 78155       /* Variable 'dist' stores the number of tokens read since the most
 78156       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
 78157       ** token is read and 'dist' equals 2, the condition stated above
 78158       ** to be met.
 78159       **
 78160       ** Note that ON cannot be a database, table or column name, so
 78161       ** there is no need to worry about syntax like 
 78162       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
 78163       */
 78164       dist++;
 78165       if( token==TK_DOT || token==TK_ON ){
 78166         dist = 0;
 78168     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
 78170     /* Variable tname now contains the token that is the old table-name
 78171     ** in the CREATE TRIGGER statement.
 78172     */
 78173     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
 78174        zTableName, tname.z+tname.n);
 78175     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 78178 #endif   /* !SQLITE_OMIT_TRIGGER */
 78180 /*
 78181 ** Register built-in functions used to help implement ALTER TABLE
 78182 */
 78183 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
 78184   static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
 78185     FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
 78186 #ifndef SQLITE_OMIT_TRIGGER
 78187     FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
 78188 #endif
 78189 #ifndef SQLITE_OMIT_FOREIGN_KEY
 78190     FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
 78191 #endif
 78192   };
 78193   int i;
 78194   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 78195   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
 78197   for(i=0; i<ArraySize(aAlterTableFuncs); i++){
 78198     sqlite3FuncDefInsert(pHash, &aFunc[i]);
 78202 /*
 78203 ** This function is used to create the text of expressions of the form:
 78204 **
 78205 **   name=<constant1> OR name=<constant2> OR ...
 78206 **
 78207 ** If argument zWhere is NULL, then a pointer string containing the text 
 78208 ** "name=<constant>" is returned, where <constant> is the quoted version
 78209 ** of the string passed as argument zConstant. The returned buffer is
 78210 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
 78211 ** caller to ensure that it is eventually freed.
 78212 **
 78213 ** If argument zWhere is not NULL, then the string returned is 
 78214 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
 78215 ** In this case zWhere is passed to sqlite3DbFree() before returning.
 78216 ** 
 78217 */
 78218 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
 78219   char *zNew;
 78220   if( !zWhere ){
 78221     zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
 78222   }else{
 78223     zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
 78224     sqlite3DbFree(db, zWhere);
 78226   return zNew;
 78229 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 78230 /*
 78231 ** Generate the text of a WHERE expression which can be used to select all
 78232 ** tables that have foreign key constraints that refer to table pTab (i.e.
 78233 ** constraints for which pTab is the parent table) from the sqlite_master
 78234 ** table.
 78235 */
 78236 static char *whereForeignKeys(Parse *pParse, Table *pTab){
 78237   FKey *p;
 78238   char *zWhere = 0;
 78239   for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 78240     zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
 78242   return zWhere;
 78244 #endif
 78246 /*
 78247 ** Generate the text of a WHERE expression which can be used to select all
 78248 ** temporary triggers on table pTab from the sqlite_temp_master table. If
 78249 ** table pTab has no temporary triggers, or is itself stored in the 
 78250 ** temporary database, NULL is returned.
 78251 */
 78252 static char *whereTempTriggers(Parse *pParse, Table *pTab){
 78253   Trigger *pTrig;
 78254   char *zWhere = 0;
 78255   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
 78257   /* If the table is not located in the temp-db (in which case NULL is 
 78258   ** returned, loop through the tables list of triggers. For each trigger
 78259   ** that is not part of the temp-db schema, add a clause to the WHERE 
 78260   ** expression being built up in zWhere.
 78261   */
 78262   if( pTab->pSchema!=pTempSchema ){
 78263     sqlite3 *db = pParse->db;
 78264     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 78265       if( pTrig->pSchema==pTempSchema ){
 78266         zWhere = whereOrName(db, zWhere, pTrig->zName);
 78270   if( zWhere ){
 78271     char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
 78272     sqlite3DbFree(pParse->db, zWhere);
 78273     zWhere = zNew;
 78275   return zWhere;
 78278 /*
 78279 ** Generate code to drop and reload the internal representation of table
 78280 ** pTab from the database, including triggers and temporary triggers.
 78281 ** Argument zName is the name of the table in the database schema at
 78282 ** the time the generated code is executed. This can be different from
 78283 ** pTab->zName if this function is being called to code part of an 
 78284 ** "ALTER TABLE RENAME TO" statement.
 78285 */
 78286 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
 78287   Vdbe *v;
 78288   char *zWhere;
 78289   int iDb;                   /* Index of database containing pTab */
 78290 #ifndef SQLITE_OMIT_TRIGGER
 78291   Trigger *pTrig;
 78292 #endif
 78294   v = sqlite3GetVdbe(pParse);
 78295   if( NEVER(v==0) ) return;
 78296   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 78297   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 78298   assert( iDb>=0 );
 78300 #ifndef SQLITE_OMIT_TRIGGER
 78301   /* Drop any table triggers from the internal schema. */
 78302   for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 78303     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
 78304     assert( iTrigDb==iDb || iTrigDb==1 );
 78305     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
 78307 #endif
 78309   /* Drop the table and index from the internal schema.  */
 78310   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 78312   /* Reload the table, index and permanent trigger schemas. */
 78313   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
 78314   if( !zWhere ) return;
 78315   sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
 78317 #ifndef SQLITE_OMIT_TRIGGER
 78318   /* Now, if the table is not stored in the temp database, reload any temp 
 78319   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
 78320   */
 78321   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 78322     sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
 78324 #endif
 78327 /*
 78328 ** Parameter zName is the name of a table that is about to be altered
 78329 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
 78330 ** If the table is a system table, this function leaves an error message
 78331 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
 78332 **
 78333 ** Or, if zName is not a system table, zero is returned.
 78334 */
 78335 static int isSystemTable(Parse *pParse, const char *zName){
 78336   if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
 78337     sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
 78338     return 1;
 78340   return 0;
 78343 /*
 78344 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
 78345 ** command. 
 78346 */
 78347 SQLITE_PRIVATE void sqlite3AlterRenameTable(
 78348   Parse *pParse,            /* Parser context. */
 78349   SrcList *pSrc,            /* The table to rename. */
 78350   Token *pName              /* The new table name. */
 78351 ){
 78352   int iDb;                  /* Database that contains the table */
 78353   char *zDb;                /* Name of database iDb */
 78354   Table *pTab;              /* Table being renamed */
 78355   char *zName = 0;          /* NULL-terminated version of pName */ 
 78356   sqlite3 *db = pParse->db; /* Database connection */
 78357   int nTabName;             /* Number of UTF-8 characters in zTabName */
 78358   const char *zTabName;     /* Original name of the table */
 78359   Vdbe *v;
 78360 #ifndef SQLITE_OMIT_TRIGGER
 78361   char *zWhere = 0;         /* Where clause to locate temp triggers */
 78362 #endif
 78363   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
 78364   int savedDbFlags;         /* Saved value of db->flags */
 78366   savedDbFlags = db->flags;  
 78367   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
 78368   assert( pSrc->nSrc==1 );
 78369   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 78371   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 78372   if( !pTab ) goto exit_rename_table;
 78373   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 78374   zDb = db->aDb[iDb].zName;
 78375   db->flags |= SQLITE_PreferBuiltin;
 78377   /* Get a NULL terminated version of the new table name. */
 78378   zName = sqlite3NameFromToken(db, pName);
 78379   if( !zName ) goto exit_rename_table;
 78381   /* Check that a table or index named 'zName' does not already exist
 78382   ** in database iDb. If so, this is an error.
 78383   */
 78384   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
 78385     sqlite3ErrorMsg(pParse, 
 78386         "there is already another table or index with this name: %s", zName);
 78387     goto exit_rename_table;
 78390   /* Make sure it is not a system table being altered, or a reserved name
 78391   ** that the table is being renamed to.
 78392   */
 78393   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 78394     goto exit_rename_table;
 78396   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
 78397     exit_rename_table;
 78400 #ifndef SQLITE_OMIT_VIEW
 78401   if( pTab->pSelect ){
 78402     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
 78403     goto exit_rename_table;
 78405 #endif
 78407 #ifndef SQLITE_OMIT_AUTHORIZATION
 78408   /* Invoke the authorization callback. */
 78409   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
 78410     goto exit_rename_table;
 78412 #endif
 78414 #ifndef SQLITE_OMIT_VIRTUALTABLE
 78415   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 78416     goto exit_rename_table;
 78418   if( IsVirtual(pTab) ){
 78419     pVTab = sqlite3GetVTable(db, pTab);
 78420     if( pVTab->pVtab->pModule->xRename==0 ){
 78421       pVTab = 0;
 78424 #endif
 78426   /* Begin a transaction and code the VerifyCookie for database iDb. 
 78427   ** Then modify the schema cookie (since the ALTER TABLE modifies the
 78428   ** schema). Open a statement transaction if the table is a virtual
 78429   ** table.
 78430   */
 78431   v = sqlite3GetVdbe(pParse);
 78432   if( v==0 ){
 78433     goto exit_rename_table;
 78435   sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
 78436   sqlite3ChangeCookie(pParse, iDb);
 78438   /* If this is a virtual table, invoke the xRename() function if
 78439   ** one is defined. The xRename() callback will modify the names
 78440   ** of any resources used by the v-table implementation (including other
 78441   ** SQLite tables) that are identified by the name of the virtual table.
 78442   */
 78443 #ifndef SQLITE_OMIT_VIRTUALTABLE
 78444   if( pVTab ){
 78445     int i = ++pParse->nMem;
 78446     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
 78447     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
 78448     sqlite3MayAbort(pParse);
 78450 #endif
 78452   /* figure out how many UTF-8 characters are in zName */
 78453   zTabName = pTab->zName;
 78454   nTabName = sqlite3Utf8CharLen(zTabName, -1);
 78456 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 78457   if( db->flags&SQLITE_ForeignKeys ){
 78458     /* If foreign-key support is enabled, rewrite the CREATE TABLE 
 78459     ** statements corresponding to all child tables of foreign key constraints
 78460     ** for which the renamed table is the parent table.  */
 78461     if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
 78462       sqlite3NestedParse(pParse, 
 78463           "UPDATE \"%w\".%s SET "
 78464               "sql = sqlite_rename_parent(sql, %Q, %Q) "
 78465               "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
 78466       sqlite3DbFree(db, zWhere);
 78469 #endif
 78471   /* Modify the sqlite_master table to use the new table name. */
 78472   sqlite3NestedParse(pParse,
 78473       "UPDATE %Q.%s SET "
 78474 #ifdef SQLITE_OMIT_TRIGGER
 78475           "sql = sqlite_rename_table(sql, %Q), "
 78476 #else
 78477           "sql = CASE "
 78478             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
 78479             "ELSE sqlite_rename_table(sql, %Q) END, "
 78480 #endif
 78481           "tbl_name = %Q, "
 78482           "name = CASE "
 78483             "WHEN type='table' THEN %Q "
 78484             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
 78485              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
 78486             "ELSE name END "
 78487       "WHERE tbl_name=%Q COLLATE nocase AND "
 78488           "(type='table' OR type='index' OR type='trigger');", 
 78489       zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
 78490 #ifndef SQLITE_OMIT_TRIGGER
 78491       zName,
 78492 #endif
 78493       zName, nTabName, zTabName
 78494   );
 78496 #ifndef SQLITE_OMIT_AUTOINCREMENT
 78497   /* If the sqlite_sequence table exists in this database, then update 
 78498   ** it with the new table name.
 78499   */
 78500   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
 78501     sqlite3NestedParse(pParse,
 78502         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
 78503         zDb, zName, pTab->zName);
 78505 #endif
 78507 #ifndef SQLITE_OMIT_TRIGGER
 78508   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
 78509   ** table. Don't do this if the table being ALTERed is itself located in
 78510   ** the temp database.
 78511   */
 78512   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 78513     sqlite3NestedParse(pParse, 
 78514         "UPDATE sqlite_temp_master SET "
 78515             "sql = sqlite_rename_trigger(sql, %Q), "
 78516             "tbl_name = %Q "
 78517             "WHERE %s;", zName, zName, zWhere);
 78518     sqlite3DbFree(db, zWhere);
 78520 #endif
 78522 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 78523   if( db->flags&SQLITE_ForeignKeys ){
 78524     FKey *p;
 78525     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 78526       Table *pFrom = p->pFrom;
 78527       if( pFrom!=pTab ){
 78528         reloadTableSchema(pParse, p->pFrom, pFrom->zName);
 78532 #endif
 78534   /* Drop and reload the internal table schema. */
 78535   reloadTableSchema(pParse, pTab, zName);
 78537 exit_rename_table:
 78538   sqlite3SrcListDelete(db, pSrc);
 78539   sqlite3DbFree(db, zName);
 78540   db->flags = savedDbFlags;
 78544 /*
 78545 ** Generate code to make sure the file format number is at least minFormat.
 78546 ** The generated code will increase the file format number if necessary.
 78547 */
 78548 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
 78549   Vdbe *v;
 78550   v = sqlite3GetVdbe(pParse);
 78551   /* The VDBE should have been allocated before this routine is called.
 78552   ** If that allocation failed, we would have quit before reaching this
 78553   ** point */
 78554   if( ALWAYS(v) ){
 78555     int r1 = sqlite3GetTempReg(pParse);
 78556     int r2 = sqlite3GetTempReg(pParse);
 78557     int j1;
 78558     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
 78559     sqlite3VdbeUsesBtree(v, iDb);
 78560     sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
 78561     j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
 78562     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
 78563     sqlite3VdbeJumpHere(v, j1);
 78564     sqlite3ReleaseTempReg(pParse, r1);
 78565     sqlite3ReleaseTempReg(pParse, r2);
 78569 /*
 78570 ** This function is called after an "ALTER TABLE ... ADD" statement
 78571 ** has been parsed. Argument pColDef contains the text of the new
 78572 ** column definition.
 78573 **
 78574 ** The Table structure pParse->pNewTable was extended to include
 78575 ** the new column during parsing.
 78576 */
 78577 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
 78578   Table *pNew;              /* Copy of pParse->pNewTable */
 78579   Table *pTab;              /* Table being altered */
 78580   int iDb;                  /* Database number */
 78581   const char *zDb;          /* Database name */
 78582   const char *zTab;         /* Table name */
 78583   char *zCol;               /* Null-terminated column definition */
 78584   Column *pCol;             /* The new column */
 78585   Expr *pDflt;              /* Default value for the new column */
 78586   sqlite3 *db;              /* The database connection; */
 78588   db = pParse->db;
 78589   if( pParse->nErr || db->mallocFailed ) return;
 78590   pNew = pParse->pNewTable;
 78591   assert( pNew );
 78593   assert( sqlite3BtreeHoldsAllMutexes(db) );
 78594   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
 78595   zDb = db->aDb[iDb].zName;
 78596   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
 78597   pCol = &pNew->aCol[pNew->nCol-1];
 78598   pDflt = pCol->pDflt;
 78599   pTab = sqlite3FindTable(db, zTab, zDb);
 78600   assert( pTab );
 78602 #ifndef SQLITE_OMIT_AUTHORIZATION
 78603   /* Invoke the authorization callback. */
 78604   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
 78605     return;
 78607 #endif
 78609   /* If the default value for the new column was specified with a 
 78610   ** literal NULL, then set pDflt to 0. This simplifies checking
 78611   ** for an SQL NULL default below.
 78612   */
 78613   if( pDflt && pDflt->op==TK_NULL ){
 78614     pDflt = 0;
 78617   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
 78618   ** If there is a NOT NULL constraint, then the default value for the
 78619   ** column must not be NULL.
 78620   */
 78621   if( pCol->colFlags & COLFLAG_PRIMKEY ){
 78622     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
 78623     return;
 78625   if( pNew->pIndex ){
 78626     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
 78627     return;
 78629   if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
 78630     sqlite3ErrorMsg(pParse, 
 78631         "Cannot add a REFERENCES column with non-NULL default value");
 78632     return;
 78634   if( pCol->notNull && !pDflt ){
 78635     sqlite3ErrorMsg(pParse, 
 78636         "Cannot add a NOT NULL column with default value NULL");
 78637     return;
 78640   /* Ensure the default expression is something that sqlite3ValueFromExpr()
 78641   ** can handle (i.e. not CURRENT_TIME etc.)
 78642   */
 78643   if( pDflt ){
 78644     sqlite3_value *pVal;
 78645     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
 78646       db->mallocFailed = 1;
 78647       return;
 78649     if( !pVal ){
 78650       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
 78651       return;
 78653     sqlite3ValueFree(pVal);
 78656   /* Modify the CREATE TABLE statement. */
 78657   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
 78658   if( zCol ){
 78659     char *zEnd = &zCol[pColDef->n-1];
 78660     int savedDbFlags = db->flags;
 78661     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
 78662       *zEnd-- = '\0';
 78664     db->flags |= SQLITE_PreferBuiltin;
 78665     sqlite3NestedParse(pParse, 
 78666         "UPDATE \"%w\".%s SET "
 78667           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
 78668         "WHERE type = 'table' AND name = %Q", 
 78669       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
 78670       zTab
 78671     );
 78672     sqlite3DbFree(db, zCol);
 78673     db->flags = savedDbFlags;
 78676   /* If the default value of the new column is NULL, then set the file
 78677   ** format to 2. If the default value of the new column is not NULL,
 78678   ** the file format becomes 3.
 78679   */
 78680   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
 78682   /* Reload the schema of the modified table. */
 78683   reloadTableSchema(pParse, pTab, pTab->zName);
 78686 /*
 78687 ** This function is called by the parser after the table-name in
 78688 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
 78689 ** pSrc is the full-name of the table being altered.
 78690 **
 78691 ** This routine makes a (partial) copy of the Table structure
 78692 ** for the table being altered and sets Parse.pNewTable to point
 78693 ** to it. Routines called by the parser as the column definition
 78694 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
 78695 ** the copy. The copy of the Table structure is deleted by tokenize.c 
 78696 ** after parsing is finished.
 78697 **
 78698 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
 78699 ** coding the "ALTER TABLE ... ADD" statement.
 78700 */
 78701 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
 78702   Table *pNew;
 78703   Table *pTab;
 78704   Vdbe *v;
 78705   int iDb;
 78706   int i;
 78707   int nAlloc;
 78708   sqlite3 *db = pParse->db;
 78710   /* Look up the table being altered. */
 78711   assert( pParse->pNewTable==0 );
 78712   assert( sqlite3BtreeHoldsAllMutexes(db) );
 78713   if( db->mallocFailed ) goto exit_begin_add_column;
 78714   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 78715   if( !pTab ) goto exit_begin_add_column;
 78717 #ifndef SQLITE_OMIT_VIRTUALTABLE
 78718   if( IsVirtual(pTab) ){
 78719     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
 78720     goto exit_begin_add_column;
 78722 #endif
 78724   /* Make sure this is not an attempt to ALTER a view. */
 78725   if( pTab->pSelect ){
 78726     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
 78727     goto exit_begin_add_column;
 78729   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 78730     goto exit_begin_add_column;
 78733   assert( pTab->addColOffset>0 );
 78734   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 78736   /* Put a copy of the Table struct in Parse.pNewTable for the
 78737   ** sqlite3AddColumn() function and friends to modify.  But modify
 78738   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
 78739   ** prefix, we insure that the name will not collide with an existing
 78740   ** table because user table are not allowed to have the "sqlite_"
 78741   ** prefix on their name.
 78742   */
 78743   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
 78744   if( !pNew ) goto exit_begin_add_column;
 78745   pParse->pNewTable = pNew;
 78746   pNew->nRef = 1;
 78747   pNew->nCol = pTab->nCol;
 78748   assert( pNew->nCol>0 );
 78749   nAlloc = (((pNew->nCol-1)/8)*8)+8;
 78750   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
 78751   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
 78752   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
 78753   if( !pNew->aCol || !pNew->zName ){
 78754     db->mallocFailed = 1;
 78755     goto exit_begin_add_column;
 78757   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
 78758   for(i=0; i<pNew->nCol; i++){
 78759     Column *pCol = &pNew->aCol[i];
 78760     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
 78761     pCol->zColl = 0;
 78762     pCol->zType = 0;
 78763     pCol->pDflt = 0;
 78764     pCol->zDflt = 0;
 78766   pNew->pSchema = db->aDb[iDb].pSchema;
 78767   pNew->addColOffset = pTab->addColOffset;
 78768   pNew->nRef = 1;
 78770   /* Begin a transaction and increment the schema cookie.  */
 78771   sqlite3BeginWriteOperation(pParse, 0, iDb);
 78772   v = sqlite3GetVdbe(pParse);
 78773   if( !v ) goto exit_begin_add_column;
 78774   sqlite3ChangeCookie(pParse, iDb);
 78776 exit_begin_add_column:
 78777   sqlite3SrcListDelete(db, pSrc);
 78778   return;
 78780 #endif  /* SQLITE_ALTER_TABLE */
 78782 /************** End of alter.c ***********************************************/
 78783 /************** Begin file analyze.c *****************************************/
 78784 /*
 78785 ** 2005 July 8
 78786 **
 78787 ** The author disclaims copyright to this source code.  In place of
 78788 ** a legal notice, here is a blessing:
 78789 **
 78790 **    May you do good and not evil.
 78791 **    May you find forgiveness for yourself and forgive others.
 78792 **    May you share freely, never taking more than you give.
 78793 **
 78794 *************************************************************************
 78795 ** This file contains code associated with the ANALYZE command.
 78796 **
 78797 ** The ANALYZE command gather statistics about the content of tables
 78798 ** and indices.  These statistics are made available to the query planner
 78799 ** to help it make better decisions about how to perform queries.
 78800 **
 78801 ** The following system tables are or have been supported:
 78802 **
 78803 **    CREATE TABLE sqlite_stat1(tbl, idx, stat);
 78804 **    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
 78805 **    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
 78806 **
 78807 ** Additional tables might be added in future releases of SQLite.
 78808 ** The sqlite_stat2 table is not created or used unless the SQLite version
 78809 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
 78810 ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
 78811 ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
 78812 ** created and used by SQLite versions 3.7.9 and later and with
 78813 ** SQLITE_ENABLE_STAT3 defined.  The fucntionality of sqlite_stat3
 78814 ** is a superset of sqlite_stat2.  
 78815 **
 78816 ** Format of sqlite_stat1:
 78817 **
 78818 ** There is normally one row per index, with the index identified by the
 78819 ** name in the idx column.  The tbl column is the name of the table to
 78820 ** which the index belongs.  In each such row, the stat column will be
 78821 ** a string consisting of a list of integers.  The first integer in this
 78822 ** list is the number of rows in the index and in the table.  The second
 78823 ** integer is the average number of rows in the index that have the same
 78824 ** value in the first column of the index.  The third integer is the average
 78825 ** number of rows in the index that have the same value for the first two
 78826 ** columns.  The N-th integer (for N>1) is the average number of rows in 
 78827 ** the index which have the same value for the first N-1 columns.  For
 78828 ** a K-column index, there will be K+1 integers in the stat column.  If
 78829 ** the index is unique, then the last integer will be 1.
 78830 **
 78831 ** The list of integers in the stat column can optionally be followed
 78832 ** by the keyword "unordered".  The "unordered" keyword, if it is present,
 78833 ** must be separated from the last integer by a single space.  If the
 78834 ** "unordered" keyword is present, then the query planner assumes that
 78835 ** the index is unordered and will not use the index for a range query.
 78836 ** 
 78837 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
 78838 ** column contains a single integer which is the (estimated) number of
 78839 ** rows in the table identified by sqlite_stat1.tbl.
 78840 **
 78841 ** Format of sqlite_stat2:
 78842 **
 78843 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
 78844 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
 78845 ** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
 78846 ** about the distribution of keys within an index.  The index is identified by
 78847 ** the "idx" column and the "tbl" column is the name of the table to which
 78848 ** the index belongs.  There are usually 10 rows in the sqlite_stat2
 78849 ** table for each index.
 78850 **
 78851 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
 78852 ** inclusive are samples of the left-most key value in the index taken at
 78853 ** evenly spaced points along the index.  Let the number of samples be S
 78854 ** (10 in the standard build) and let C be the number of rows in the index.
 78855 ** Then the sampled rows are given by:
 78856 **
 78857 **     rownumber = (i*C*2 + C)/(S*2)
 78858 **
 78859 ** For i between 0 and S-1.  Conceptually, the index space is divided into
 78860 ** S uniform buckets and the samples are the middle row from each bucket.
 78861 **
 78862 ** The format for sqlite_stat2 is recorded here for legacy reference.  This
 78863 ** version of SQLite does not support sqlite_stat2.  It neither reads nor
 78864 ** writes the sqlite_stat2 table.  This version of SQLite only supports
 78865 ** sqlite_stat3.
 78866 **
 78867 ** Format for sqlite_stat3:
 78868 **
 78869 ** The sqlite_stat3 is an enhancement to sqlite_stat2.  A new name is
 78870 ** used to avoid compatibility problems.  
 78871 **
 78872 ** The format of the sqlite_stat3 table is similar to the format of
 78873 ** the sqlite_stat2 table.  There are multiple entries for each index.
 78874 ** The idx column names the index and the tbl column is the table of the
 78875 ** index.  If the idx and tbl columns are the same, then the sample is
 78876 ** of the INTEGER PRIMARY KEY.  The sample column is a value taken from
 78877 ** the left-most column of the index.  The nEq column is the approximate
 78878 ** number of entires in the index whose left-most column exactly matches
 78879 ** the sample.  nLt is the approximate number of entires whose left-most
 78880 ** column is less than the sample.  The nDLt column is the approximate
 78881 ** number of distinct left-most entries in the index that are less than
 78882 ** the sample.
 78883 **
 78884 ** Future versions of SQLite might change to store a string containing
 78885 ** multiple integers values in the nDLt column of sqlite_stat3.  The first
 78886 ** integer will be the number of prior index entires that are distinct in
 78887 ** the left-most column.  The second integer will be the number of prior index
 78888 ** entries that are distinct in the first two columns.  The third integer
 78889 ** will be the number of prior index entries that are distinct in the first
 78890 ** three columns.  And so forth.  With that extension, the nDLt field is
 78891 ** similar in function to the sqlite_stat1.stat field.
 78892 **
 78893 ** There can be an arbitrary number of sqlite_stat3 entries per index.
 78894 ** The ANALYZE command will typically generate sqlite_stat3 tables
 78895 ** that contain between 10 and 40 samples which are distributed across
 78896 ** the key space, though not uniformly, and which include samples with
 78897 ** largest possible nEq values.
 78898 */
 78899 #ifndef SQLITE_OMIT_ANALYZE
 78901 /*
 78902 ** This routine generates code that opens the sqlite_stat1 table for
 78903 ** writing with cursor iStatCur. If the library was built with the
 78904 ** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
 78905 ** opened for writing using cursor (iStatCur+1)
 78906 **
 78907 ** If the sqlite_stat1 tables does not previously exist, it is created.
 78908 ** Similarly, if the sqlite_stat3 table does not exist and the library
 78909 ** is compiled with SQLITE_ENABLE_STAT3 defined, it is created. 
 78910 **
 78911 ** Argument zWhere may be a pointer to a buffer containing a table name,
 78912 ** or it may be a NULL pointer. If it is not NULL, then all entries in
 78913 ** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
 78914 ** with the named table are deleted. If zWhere==0, then code is generated
 78915 ** to delete all stat table entries.
 78916 */
 78917 static void openStatTable(
 78918   Parse *pParse,          /* Parsing context */
 78919   int iDb,                /* The database we are looking in */
 78920   int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
 78921   const char *zWhere,     /* Delete entries for this table or index */
 78922   const char *zWhereType  /* Either "tbl" or "idx" */
 78923 ){
 78924   static const struct {
 78925     const char *zName;
 78926     const char *zCols;
 78927   } aTable[] = {
 78928     { "sqlite_stat1", "tbl,idx,stat" },
 78929 #ifdef SQLITE_ENABLE_STAT3
 78930     { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
 78931 #endif
 78932   };
 78934   int aRoot[] = {0, 0};
 78935   u8 aCreateTbl[] = {0, 0};
 78937   int i;
 78938   sqlite3 *db = pParse->db;
 78939   Db *pDb;
 78940   Vdbe *v = sqlite3GetVdbe(pParse);
 78941   if( v==0 ) return;
 78942   assert( sqlite3BtreeHoldsAllMutexes(db) );
 78943   assert( sqlite3VdbeDb(v)==db );
 78944   pDb = &db->aDb[iDb];
 78946   /* Create new statistic tables if they do not exist, or clear them
 78947   ** if they do already exist.
 78948   */
 78949   for(i=0; i<ArraySize(aTable); i++){
 78950     const char *zTab = aTable[i].zName;
 78951     Table *pStat;
 78952     if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
 78953       /* The sqlite_stat[12] table does not exist. Create it. Note that a 
 78954       ** side-effect of the CREATE TABLE statement is to leave the rootpage 
 78955       ** of the new table in register pParse->regRoot. This is important 
 78956       ** because the OpenWrite opcode below will be needing it. */
 78957       sqlite3NestedParse(pParse,
 78958           "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
 78959       );
 78960       aRoot[i] = pParse->regRoot;
 78961       aCreateTbl[i] = OPFLAG_P2ISREG;
 78962     }else{
 78963       /* The table already exists. If zWhere is not NULL, delete all entries 
 78964       ** associated with the table zWhere. If zWhere is NULL, delete the
 78965       ** entire contents of the table. */
 78966       aRoot[i] = pStat->tnum;
 78967       sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 78968       if( zWhere ){
 78969         sqlite3NestedParse(pParse,
 78970            "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
 78971         );
 78972       }else{
 78973         /* The sqlite_stat[12] table already exists.  Delete all rows. */
 78974         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 78979   /* Open the sqlite_stat[13] tables for writing. */
 78980   for(i=0; i<ArraySize(aTable); i++){
 78981     sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
 78982     sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
 78983     sqlite3VdbeChangeP5(v, aCreateTbl[i]);
 78987 /*
 78988 ** Recommended number of samples for sqlite_stat3
 78989 */
 78990 #ifndef SQLITE_STAT3_SAMPLES
 78991 # define SQLITE_STAT3_SAMPLES 24
 78992 #endif
 78994 /*
 78995 ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
 78996 ** share an instance of the following structure to hold their state
 78997 ** information.
 78998 */
 78999 typedef struct Stat3Accum Stat3Accum;
 79000 struct Stat3Accum {
 79001   tRowcnt nRow;             /* Number of rows in the entire table */
 79002   tRowcnt nPSample;         /* How often to do a periodic sample */
 79003   int iMin;                 /* Index of entry with minimum nEq and hash */
 79004   int mxSample;             /* Maximum number of samples to accumulate */
 79005   int nSample;              /* Current number of samples */
 79006   u32 iPrn;                 /* Pseudo-random number used for sampling */
 79007   struct Stat3Sample {
 79008     i64 iRowid;                /* Rowid in main table of the key */
 79009     tRowcnt nEq;               /* sqlite_stat3.nEq */
 79010     tRowcnt nLt;               /* sqlite_stat3.nLt */
 79011     tRowcnt nDLt;              /* sqlite_stat3.nDLt */
 79012     u8 isPSample;              /* True if a periodic sample */
 79013     u32 iHash;                 /* Tiebreaker hash */
 79014   } *a;                     /* An array of samples */
 79015 };
 79017 #ifdef SQLITE_ENABLE_STAT3
 79018 /*
 79019 ** Implementation of the stat3_init(C,S) SQL function.  The two parameters
 79020 ** are the number of rows in the table or index (C) and the number of samples
 79021 ** to accumulate (S).
 79022 **
 79023 ** This routine allocates the Stat3Accum object.
 79024 **
 79025 ** The return value is the Stat3Accum object (P).
 79026 */
 79027 static void stat3Init(
 79028   sqlite3_context *context,
 79029   int argc,
 79030   sqlite3_value **argv
 79031 ){
 79032   Stat3Accum *p;
 79033   tRowcnt nRow;
 79034   int mxSample;
 79035   int n;
 79037   UNUSED_PARAMETER(argc);
 79038   nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
 79039   mxSample = sqlite3_value_int(argv[1]);
 79040   n = sizeof(*p) + sizeof(p->a[0])*mxSample;
 79041   p = sqlite3MallocZero( n );
 79042   if( p==0 ){
 79043     sqlite3_result_error_nomem(context);
 79044     return;
 79046   p->a = (struct Stat3Sample*)&p[1];
 79047   p->nRow = nRow;
 79048   p->mxSample = mxSample;
 79049   p->nPSample = p->nRow/(mxSample/3+1) + 1;
 79050   sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
 79051   sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
 79053 static const FuncDef stat3InitFuncdef = {
 79054   2,                /* nArg */
 79055   SQLITE_UTF8,      /* iPrefEnc */
 79056   0,                /* flags */
 79057   0,                /* pUserData */
 79058   0,                /* pNext */
 79059   stat3Init,        /* xFunc */
 79060   0,                /* xStep */
 79061   0,                /* xFinalize */
 79062   "stat3_init",     /* zName */
 79063   0,                /* pHash */
 79064   0                 /* pDestructor */
 79065 };
 79068 /*
 79069 ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function.  The
 79070 ** arguments describe a single key instance.  This routine makes the 
 79071 ** decision about whether or not to retain this key for the sqlite_stat3
 79072 ** table.
 79073 **
 79074 ** The return value is NULL.
 79075 */
 79076 static void stat3Push(
 79077   sqlite3_context *context,
 79078   int argc,
 79079   sqlite3_value **argv
 79080 ){
 79081   Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
 79082   tRowcnt nEq = sqlite3_value_int64(argv[0]);
 79083   tRowcnt nLt = sqlite3_value_int64(argv[1]);
 79084   tRowcnt nDLt = sqlite3_value_int64(argv[2]);
 79085   i64 rowid = sqlite3_value_int64(argv[3]);
 79086   u8 isPSample = 0;
 79087   u8 doInsert = 0;
 79088   int iMin = p->iMin;
 79089   struct Stat3Sample *pSample;
 79090   int i;
 79091   u32 h;
 79093   UNUSED_PARAMETER(context);
 79094   UNUSED_PARAMETER(argc);
 79095   if( nEq==0 ) return;
 79096   h = p->iPrn = p->iPrn*1103515245 + 12345;
 79097   if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
 79098     doInsert = isPSample = 1;
 79099   }else if( p->nSample<p->mxSample ){
 79100     doInsert = 1;
 79101   }else{
 79102     if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
 79103       doInsert = 1;
 79106   if( !doInsert ) return;
 79107   if( p->nSample==p->mxSample ){
 79108     assert( p->nSample - iMin - 1 >= 0 );
 79109     memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
 79110     pSample = &p->a[p->nSample-1];
 79111   }else{
 79112     pSample = &p->a[p->nSample++];
 79114   pSample->iRowid = rowid;
 79115   pSample->nEq = nEq;
 79116   pSample->nLt = nLt;
 79117   pSample->nDLt = nDLt;
 79118   pSample->iHash = h;
 79119   pSample->isPSample = isPSample;
 79121   /* Find the new minimum */
 79122   if( p->nSample==p->mxSample ){
 79123     pSample = p->a;
 79124     i = 0;
 79125     while( pSample->isPSample ){
 79126       i++;
 79127       pSample++;
 79128       assert( i<p->nSample );
 79130     nEq = pSample->nEq;
 79131     h = pSample->iHash;
 79132     iMin = i;
 79133     for(i++, pSample++; i<p->nSample; i++, pSample++){
 79134       if( pSample->isPSample ) continue;
 79135       if( pSample->nEq<nEq
 79136        || (pSample->nEq==nEq && pSample->iHash<h)
 79137       ){
 79138         iMin = i;
 79139         nEq = pSample->nEq;
 79140         h = pSample->iHash;
 79143     p->iMin = iMin;
 79146 static const FuncDef stat3PushFuncdef = {
 79147   5,                /* nArg */
 79148   SQLITE_UTF8,      /* iPrefEnc */
 79149   0,                /* flags */
 79150   0,                /* pUserData */
 79151   0,                /* pNext */
 79152   stat3Push,        /* xFunc */
 79153   0,                /* xStep */
 79154   0,                /* xFinalize */
 79155   "stat3_push",     /* zName */
 79156   0,                /* pHash */
 79157   0                 /* pDestructor */
 79158 };
 79160 /*
 79161 ** Implementation of the stat3_get(P,N,...) SQL function.  This routine is
 79162 ** used to query the results.  Content is returned for the Nth sqlite_stat3
 79163 ** row where N is between 0 and S-1 and S is the number of samples.  The
 79164 ** value returned depends on the number of arguments.
 79165 **
 79166 **   argc==2    result:  rowid
 79167 **   argc==3    result:  nEq
 79168 **   argc==4    result:  nLt
 79169 **   argc==5    result:  nDLt
 79170 */
 79171 static void stat3Get(
 79172   sqlite3_context *context,
 79173   int argc,
 79174   sqlite3_value **argv
 79175 ){
 79176   int n = sqlite3_value_int(argv[1]);
 79177   Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
 79179   assert( p!=0 );
 79180   if( p->nSample<=n ) return;
 79181   switch( argc ){
 79182     case 2:  sqlite3_result_int64(context, p->a[n].iRowid); break;
 79183     case 3:  sqlite3_result_int64(context, p->a[n].nEq);    break;
 79184     case 4:  sqlite3_result_int64(context, p->a[n].nLt);    break;
 79185     default: sqlite3_result_int64(context, p->a[n].nDLt);   break;
 79188 static const FuncDef stat3GetFuncdef = {
 79189   -1,               /* nArg */
 79190   SQLITE_UTF8,      /* iPrefEnc */
 79191   0,                /* flags */
 79192   0,                /* pUserData */
 79193   0,                /* pNext */
 79194   stat3Get,         /* xFunc */
 79195   0,                /* xStep */
 79196   0,                /* xFinalize */
 79197   "stat3_get",     /* zName */
 79198   0,                /* pHash */
 79199   0                 /* pDestructor */
 79200 };
 79201 #endif /* SQLITE_ENABLE_STAT3 */
 79206 /*
 79207 ** Generate code to do an analysis of all indices associated with
 79208 ** a single table.
 79209 */
 79210 static void analyzeOneTable(
 79211   Parse *pParse,   /* Parser context */
 79212   Table *pTab,     /* Table whose indices are to be analyzed */
 79213   Index *pOnlyIdx, /* If not NULL, only analyze this one index */
 79214   int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
 79215   int iMem         /* Available memory locations begin here */
 79216 ){
 79217   sqlite3 *db = pParse->db;    /* Database handle */
 79218   Index *pIdx;                 /* An index to being analyzed */
 79219   int iIdxCur;                 /* Cursor open on index being analyzed */
 79220   Vdbe *v;                     /* The virtual machine being built up */
 79221   int i;                       /* Loop counter */
 79222   int topOfLoop;               /* The top of the loop */
 79223   int endOfLoop;               /* The end of the loop */
 79224   int jZeroRows = -1;          /* Jump from here if number of rows is zero */
 79225   int iDb;                     /* Index of database containing pTab */
 79226   int regTabname = iMem++;     /* Register containing table name */
 79227   int regIdxname = iMem++;     /* Register containing index name */
 79228   int regStat1 = iMem++;       /* The stat column of sqlite_stat1 */
 79229 #ifdef SQLITE_ENABLE_STAT3
 79230   int regNumEq = regStat1;     /* Number of instances.  Same as regStat1 */
 79231   int regNumLt = iMem++;       /* Number of keys less than regSample */
 79232   int regNumDLt = iMem++;      /* Number of distinct keys less than regSample */
 79233   int regSample = iMem++;      /* The next sample value */
 79234   int regRowid = regSample;    /* Rowid of a sample */
 79235   int regAccum = iMem++;       /* Register to hold Stat3Accum object */
 79236   int regLoop = iMem++;        /* Loop counter */
 79237   int regCount = iMem++;       /* Number of rows in the table or index */
 79238   int regTemp1 = iMem++;       /* Intermediate register */
 79239   int regTemp2 = iMem++;       /* Intermediate register */
 79240   int once = 1;                /* One-time initialization */
 79241   int shortJump = 0;           /* Instruction address */
 79242   int iTabCur = pParse->nTab++; /* Table cursor */
 79243 #endif
 79244   int regCol = iMem++;         /* Content of a column in analyzed table */
 79245   int regRec = iMem++;         /* Register holding completed record */
 79246   int regTemp = iMem++;        /* Temporary use register */
 79247   int regNewRowid = iMem++;    /* Rowid for the inserted record */
 79250   v = sqlite3GetVdbe(pParse);
 79251   if( v==0 || NEVER(pTab==0) ){
 79252     return;
 79254   if( pTab->tnum==0 ){
 79255     /* Do not gather statistics on views or virtual tables */
 79256     return;
 79258   if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
 79259     /* Do not gather statistics on system tables */
 79260     return;
 79262   assert( sqlite3BtreeHoldsAllMutexes(db) );
 79263   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 79264   assert( iDb>=0 );
 79265   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 79266 #ifndef SQLITE_OMIT_AUTHORIZATION
 79267   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
 79268       db->aDb[iDb].zName ) ){
 79269     return;
 79271 #endif
 79273   /* Establish a read-lock on the table at the shared-cache level. */
 79274   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 79276   iIdxCur = pParse->nTab++;
 79277   sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
 79278   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 79279     int nCol;
 79280     KeyInfo *pKey;
 79281     int addrIfNot = 0;           /* address of OP_IfNot */
 79282     int *aChngAddr;              /* Array of jump instruction addresses */
 79284     if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
 79285     VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
 79286     nCol = pIdx->nColumn;
 79287     aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
 79288     if( aChngAddr==0 ) continue;
 79289     pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 79290     if( iMem+1+(nCol*2)>pParse->nMem ){
 79291       pParse->nMem = iMem+1+(nCol*2);
 79294     /* Open a cursor to the index to be analyzed. */
 79295     assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
 79296     sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
 79297         (char *)pKey, P4_KEYINFO_HANDOFF);
 79298     VdbeComment((v, "%s", pIdx->zName));
 79300     /* Populate the register containing the index name. */
 79301     sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
 79303 #ifdef SQLITE_ENABLE_STAT3
 79304     if( once ){
 79305       once = 0;
 79306       sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
 79308     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
 79309     sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
 79310     sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
 79311     sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
 79312     sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
 79313     sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
 79314     sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
 79315                       (char*)&stat3InitFuncdef, P4_FUNCDEF);
 79316     sqlite3VdbeChangeP5(v, 2);
 79317 #endif /* SQLITE_ENABLE_STAT3 */
 79319     /* The block of memory cells initialized here is used as follows.
 79320     **
 79321     **    iMem:                
 79322     **        The total number of rows in the table.
 79323     **
 79324     **    iMem+1 .. iMem+nCol: 
 79325     **        Number of distinct entries in index considering the 
 79326     **        left-most N columns only, where N is between 1 and nCol, 
 79327     **        inclusive.
 79328     **
 79329     **    iMem+nCol+1 .. Mem+2*nCol:  
 79330     **        Previous value of indexed columns, from left to right.
 79331     **
 79332     ** Cells iMem through iMem+nCol are initialized to 0. The others are 
 79333     ** initialized to contain an SQL NULL.
 79334     */
 79335     for(i=0; i<=nCol; i++){
 79336       sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
 79338     for(i=0; i<nCol; i++){
 79339       sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
 79342     /* Start the analysis loop. This loop runs through all the entries in
 79343     ** the index b-tree.  */
 79344     endOfLoop = sqlite3VdbeMakeLabel(v);
 79345     sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
 79346     topOfLoop = sqlite3VdbeCurrentAddr(v);
 79347     sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);  /* Increment row counter */
 79349     for(i=0; i<nCol; i++){
 79350       CollSeq *pColl;
 79351       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
 79352       if( i==0 ){
 79353         /* Always record the very first row */
 79354         addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
 79356       assert( pIdx->azColl!=0 );
 79357       assert( pIdx->azColl[i]!=0 );
 79358       pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
 79359       aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
 79360                                       (char*)pColl, P4_COLLSEQ);
 79361       sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 79362       VdbeComment((v, "jump if column %d changed", i));
 79363 #ifdef SQLITE_ENABLE_STAT3
 79364       if( i==0 ){
 79365         sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
 79366         VdbeComment((v, "incr repeat count"));
 79368 #endif
 79370     sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
 79371     for(i=0; i<nCol; i++){
 79372       sqlite3VdbeJumpHere(v, aChngAddr[i]);  /* Set jump dest for the OP_Ne */
 79373       if( i==0 ){
 79374         sqlite3VdbeJumpHere(v, addrIfNot);   /* Jump dest for OP_IfNot */
 79375 #ifdef SQLITE_ENABLE_STAT3
 79376         sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
 79377                           (char*)&stat3PushFuncdef, P4_FUNCDEF);
 79378         sqlite3VdbeChangeP5(v, 5);
 79379         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
 79380         sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
 79381         sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
 79382         sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
 79383 #endif        
 79385       sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
 79386       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
 79388     sqlite3DbFree(db, aChngAddr);
 79390     /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
 79391     sqlite3VdbeResolveLabel(v, endOfLoop);
 79393     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
 79394     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 79395 #ifdef SQLITE_ENABLE_STAT3
 79396     sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
 79397                       (char*)&stat3PushFuncdef, P4_FUNCDEF);
 79398     sqlite3VdbeChangeP5(v, 5);
 79399     sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
 79400     shortJump = 
 79401     sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
 79402     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
 79403                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
 79404     sqlite3VdbeChangeP5(v, 2);
 79405     sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
 79406     sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
 79407     sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
 79408     sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
 79409     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
 79410                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
 79411     sqlite3VdbeChangeP5(v, 3);
 79412     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
 79413                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
 79414     sqlite3VdbeChangeP5(v, 4);
 79415     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
 79416                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
 79417     sqlite3VdbeChangeP5(v, 5);
 79418     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
 79419     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
 79420     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
 79421     sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
 79422     sqlite3VdbeJumpHere(v, shortJump+2);
 79423 #endif        
 79425     /* Store the results in sqlite_stat1.
 79426     **
 79427     ** The result is a single row of the sqlite_stat1 table.  The first
 79428     ** two columns are the names of the table and index.  The third column
 79429     ** is a string composed of a list of integer statistics about the
 79430     ** index.  The first integer in the list is the total number of entries
 79431     ** in the index.  There is one additional integer in the list for each
 79432     ** column of the table.  This additional integer is a guess of how many
 79433     ** rows of the table the index will select.  If D is the count of distinct
 79434     ** values and K is the total number of rows, then the integer is computed
 79435     ** as:
 79436     **
 79437     **        I = (K+D-1)/D
 79438     **
 79439     ** If K==0 then no entry is made into the sqlite_stat1 table.  
 79440     ** If K>0 then it is always the case the D>0 so division by zero
 79441     ** is never possible.
 79442     */
 79443     sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
 79444     if( jZeroRows<0 ){
 79445       jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
 79447     for(i=0; i<nCol; i++){
 79448       sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
 79449       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
 79450       sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
 79451       sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
 79452       sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
 79453       sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
 79454       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
 79456     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
 79457     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 79458     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
 79459     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 79462   /* If the table has no indices, create a single sqlite_stat1 entry
 79463   ** containing NULL as the index name and the row count as the content.
 79464   */
 79465   if( pTab->pIndex==0 ){
 79466     sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
 79467     VdbeComment((v, "%s", pTab->zName));
 79468     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
 79469     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 79470     jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
 79471   }else{
 79472     sqlite3VdbeJumpHere(v, jZeroRows);
 79473     jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
 79475   sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
 79476   sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
 79477   sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 79478   sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
 79479   sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 79480   if( pParse->nMem<regRec ) pParse->nMem = regRec;
 79481   sqlite3VdbeJumpHere(v, jZeroRows);
 79485 /*
 79486 ** Generate code that will cause the most recent index analysis to
 79487 ** be loaded into internal hash tables where is can be used.
 79488 */
 79489 static void loadAnalysis(Parse *pParse, int iDb){
 79490   Vdbe *v = sqlite3GetVdbe(pParse);
 79491   if( v ){
 79492     sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
 79496 /*
 79497 ** Generate code that will do an analysis of an entire database
 79498 */
 79499 static void analyzeDatabase(Parse *pParse, int iDb){
 79500   sqlite3 *db = pParse->db;
 79501   Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
 79502   HashElem *k;
 79503   int iStatCur;
 79504   int iMem;
 79506   sqlite3BeginWriteOperation(pParse, 0, iDb);
 79507   iStatCur = pParse->nTab;
 79508   pParse->nTab += 3;
 79509   openStatTable(pParse, iDb, iStatCur, 0, 0);
 79510   iMem = pParse->nMem+1;
 79511   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 79512   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
 79513     Table *pTab = (Table*)sqliteHashData(k);
 79514     analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
 79516   loadAnalysis(pParse, iDb);
 79519 /*
 79520 ** Generate code that will do an analysis of a single table in
 79521 ** a database.  If pOnlyIdx is not NULL then it is a single index
 79522 ** in pTab that should be analyzed.
 79523 */
 79524 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
 79525   int iDb;
 79526   int iStatCur;
 79528   assert( pTab!=0 );
 79529   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 79530   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 79531   sqlite3BeginWriteOperation(pParse, 0, iDb);
 79532   iStatCur = pParse->nTab;
 79533   pParse->nTab += 3;
 79534   if( pOnlyIdx ){
 79535     openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
 79536   }else{
 79537     openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
 79539   analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
 79540   loadAnalysis(pParse, iDb);
 79543 /*
 79544 ** Generate code for the ANALYZE command.  The parser calls this routine
 79545 ** when it recognizes an ANALYZE command.
 79546 **
 79547 **        ANALYZE                            -- 1
 79548 **        ANALYZE  <database>                -- 2
 79549 **        ANALYZE  ?<database>.?<tablename>  -- 3
 79550 **
 79551 ** Form 1 causes all indices in all attached databases to be analyzed.
 79552 ** Form 2 analyzes all indices the single database named.
 79553 ** Form 3 analyzes all indices associated with the named table.
 79554 */
 79555 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
 79556   sqlite3 *db = pParse->db;
 79557   int iDb;
 79558   int i;
 79559   char *z, *zDb;
 79560   Table *pTab;
 79561   Index *pIdx;
 79562   Token *pTableName;
 79564   /* Read the database schema. If an error occurs, leave an error message
 79565   ** and code in pParse and return NULL. */
 79566   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 79567   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 79568     return;
 79571   assert( pName2!=0 || pName1==0 );
 79572   if( pName1==0 ){
 79573     /* Form 1:  Analyze everything */
 79574     for(i=0; i<db->nDb; i++){
 79575       if( i==1 ) continue;  /* Do not analyze the TEMP database */
 79576       analyzeDatabase(pParse, i);
 79578   }else if( pName2->n==0 ){
 79579     /* Form 2:  Analyze the database or table named */
 79580     iDb = sqlite3FindDb(db, pName1);
 79581     if( iDb>=0 ){
 79582       analyzeDatabase(pParse, iDb);
 79583     }else{
 79584       z = sqlite3NameFromToken(db, pName1);
 79585       if( z ){
 79586         if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
 79587           analyzeTable(pParse, pIdx->pTable, pIdx);
 79588         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
 79589           analyzeTable(pParse, pTab, 0);
 79591         sqlite3DbFree(db, z);
 79594   }else{
 79595     /* Form 3: Analyze the fully qualified table name */
 79596     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
 79597     if( iDb>=0 ){
 79598       zDb = db->aDb[iDb].zName;
 79599       z = sqlite3NameFromToken(db, pTableName);
 79600       if( z ){
 79601         if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
 79602           analyzeTable(pParse, pIdx->pTable, pIdx);
 79603         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
 79604           analyzeTable(pParse, pTab, 0);
 79606         sqlite3DbFree(db, z);
 79612 /*
 79613 ** Used to pass information from the analyzer reader through to the
 79614 ** callback routine.
 79615 */
 79616 typedef struct analysisInfo analysisInfo;
 79617 struct analysisInfo {
 79618   sqlite3 *db;
 79619   const char *zDatabase;
 79620 };
 79622 /*
 79623 ** This callback is invoked once for each index when reading the
 79624 ** sqlite_stat1 table.  
 79625 **
 79626 **     argv[0] = name of the table
 79627 **     argv[1] = name of the index (might be NULL)
 79628 **     argv[2] = results of analysis - on integer for each column
 79629 **
 79630 ** Entries for which argv[1]==NULL simply record the number of rows in
 79631 ** the table.
 79632 */
 79633 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
 79634   analysisInfo *pInfo = (analysisInfo*)pData;
 79635   Index *pIndex;
 79636   Table *pTable;
 79637   int i, c, n;
 79638   tRowcnt v;
 79639   const char *z;
 79641   assert( argc==3 );
 79642   UNUSED_PARAMETER2(NotUsed, argc);
 79644   if( argv==0 || argv[0]==0 || argv[2]==0 ){
 79645     return 0;
 79647   pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
 79648   if( pTable==0 ){
 79649     return 0;
 79651   if( argv[1] ){
 79652     pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
 79653   }else{
 79654     pIndex = 0;
 79656   n = pIndex ? pIndex->nColumn : 0;
 79657   z = argv[2];
 79658   for(i=0; *z && i<=n; i++){
 79659     v = 0;
 79660     while( (c=z[0])>='0' && c<='9' ){
 79661       v = v*10 + c - '0';
 79662       z++;
 79664     if( i==0 ) pTable->nRowEst = v;
 79665     if( pIndex==0 ) break;
 79666     pIndex->aiRowEst[i] = v;
 79667     if( *z==' ' ) z++;
 79668     if( memcmp(z, "unordered", 10)==0 ){
 79669       pIndex->bUnordered = 1;
 79670       break;
 79673   return 0;
 79676 /*
 79677 ** If the Index.aSample variable is not NULL, delete the aSample[] array
 79678 ** and its contents.
 79679 */
 79680 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
 79681 #ifdef SQLITE_ENABLE_STAT3
 79682   if( pIdx->aSample ){
 79683     int j;
 79684     for(j=0; j<pIdx->nSample; j++){
 79685       IndexSample *p = &pIdx->aSample[j];
 79686       if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
 79687         sqlite3DbFree(db, p->u.z);
 79690     sqlite3DbFree(db, pIdx->aSample);
 79692   if( db && db->pnBytesFreed==0 ){
 79693     pIdx->nSample = 0;
 79694     pIdx->aSample = 0;
 79696 #else
 79697   UNUSED_PARAMETER(db);
 79698   UNUSED_PARAMETER(pIdx);
 79699 #endif
 79702 #ifdef SQLITE_ENABLE_STAT3
 79703 /*
 79704 ** Load content from the sqlite_stat3 table into the Index.aSample[]
 79705 ** arrays of all indices.
 79706 */
 79707 static int loadStat3(sqlite3 *db, const char *zDb){
 79708   int rc;                       /* Result codes from subroutines */
 79709   sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
 79710   char *zSql;                   /* Text of the SQL statement */
 79711   Index *pPrevIdx = 0;          /* Previous index in the loop */
 79712   int idx = 0;                  /* slot in pIdx->aSample[] for next sample */
 79713   int eType;                    /* Datatype of a sample */
 79714   IndexSample *pSample;         /* A slot in pIdx->aSample[] */
 79716   assert( db->lookaside.bEnabled==0 );
 79717   if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
 79718     return SQLITE_OK;
 79721   zSql = sqlite3MPrintf(db, 
 79722       "SELECT idx,count(*) FROM %Q.sqlite_stat3"
 79723       " GROUP BY idx", zDb);
 79724   if( !zSql ){
 79725     return SQLITE_NOMEM;
 79727   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 79728   sqlite3DbFree(db, zSql);
 79729   if( rc ) return rc;
 79731   while( sqlite3_step(pStmt)==SQLITE_ROW ){
 79732     char *zIndex;   /* Index name */
 79733     Index *pIdx;    /* Pointer to the index object */
 79734     int nSample;    /* Number of samples */
 79736     zIndex = (char *)sqlite3_column_text(pStmt, 0);
 79737     if( zIndex==0 ) continue;
 79738     nSample = sqlite3_column_int(pStmt, 1);
 79739     pIdx = sqlite3FindIndex(db, zIndex, zDb);
 79740     if( pIdx==0 ) continue;
 79741     assert( pIdx->nSample==0 );
 79742     pIdx->nSample = nSample;
 79743     pIdx->aSample = sqlite3DbMallocZero(db, nSample*sizeof(IndexSample));
 79744     pIdx->avgEq = pIdx->aiRowEst[1];
 79745     if( pIdx->aSample==0 ){
 79746       db->mallocFailed = 1;
 79747       sqlite3_finalize(pStmt);
 79748       return SQLITE_NOMEM;
 79751   rc = sqlite3_finalize(pStmt);
 79752   if( rc ) return rc;
 79754   zSql = sqlite3MPrintf(db, 
 79755       "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
 79756   if( !zSql ){
 79757     return SQLITE_NOMEM;
 79759   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 79760   sqlite3DbFree(db, zSql);
 79761   if( rc ) return rc;
 79763   while( sqlite3_step(pStmt)==SQLITE_ROW ){
 79764     char *zIndex;   /* Index name */
 79765     Index *pIdx;    /* Pointer to the index object */
 79766     int i;          /* Loop counter */
 79767     tRowcnt sumEq;  /* Sum of the nEq values */
 79769     zIndex = (char *)sqlite3_column_text(pStmt, 0);
 79770     if( zIndex==0 ) continue;
 79771     pIdx = sqlite3FindIndex(db, zIndex, zDb);
 79772     if( pIdx==0 ) continue;
 79773     if( pIdx==pPrevIdx ){
 79774       idx++;
 79775     }else{
 79776       pPrevIdx = pIdx;
 79777       idx = 0;
 79779     assert( idx<pIdx->nSample );
 79780     pSample = &pIdx->aSample[idx];
 79781     pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
 79782     pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
 79783     pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
 79784     if( idx==pIdx->nSample-1 ){
 79785       if( pSample->nDLt>0 ){
 79786         for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
 79787         pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
 79789       if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
 79791     eType = sqlite3_column_type(pStmt, 4);
 79792     pSample->eType = (u8)eType;
 79793     switch( eType ){
 79794       case SQLITE_INTEGER: {
 79795         pSample->u.i = sqlite3_column_int64(pStmt, 4);
 79796         break;
 79798       case SQLITE_FLOAT: {
 79799         pSample->u.r = sqlite3_column_double(pStmt, 4);
 79800         break;
 79802       case SQLITE_NULL: {
 79803         break;
 79805       default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
 79806         const char *z = (const char *)(
 79807               (eType==SQLITE_BLOB) ?
 79808               sqlite3_column_blob(pStmt, 4):
 79809               sqlite3_column_text(pStmt, 4)
 79810            );
 79811         int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
 79812         pSample->nByte = n;
 79813         if( n < 1){
 79814           pSample->u.z = 0;
 79815         }else{
 79816           pSample->u.z = sqlite3DbMallocRaw(db, n);
 79817           if( pSample->u.z==0 ){
 79818             db->mallocFailed = 1;
 79819             sqlite3_finalize(pStmt);
 79820             return SQLITE_NOMEM;
 79822           memcpy(pSample->u.z, z, n);
 79827   return sqlite3_finalize(pStmt);
 79829 #endif /* SQLITE_ENABLE_STAT3 */
 79831 /*
 79832 ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
 79833 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
 79834 ** arrays. The contents of sqlite_stat3 are used to populate the
 79835 ** Index.aSample[] arrays.
 79836 **
 79837 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
 79838 ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined 
 79839 ** during compilation and the sqlite_stat3 table is present, no data is 
 79840 ** read from it.
 79841 **
 79842 ** If SQLITE_ENABLE_STAT3 was defined during compilation and the 
 79843 ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
 79844 ** returned. However, in this case, data is read from the sqlite_stat1
 79845 ** table (if it is present) before returning.
 79846 **
 79847 ** If an OOM error occurs, this function always sets db->mallocFailed.
 79848 ** This means if the caller does not care about other errors, the return
 79849 ** code may be ignored.
 79850 */
 79851 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
 79852   analysisInfo sInfo;
 79853   HashElem *i;
 79854   char *zSql;
 79855   int rc;
 79857   assert( iDb>=0 && iDb<db->nDb );
 79858   assert( db->aDb[iDb].pBt!=0 );
 79860   /* Clear any prior statistics */
 79861   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 79862   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
 79863     Index *pIdx = sqliteHashData(i);
 79864     sqlite3DefaultRowEst(pIdx);
 79865 #ifdef SQLITE_ENABLE_STAT3
 79866     sqlite3DeleteIndexSamples(db, pIdx);
 79867     pIdx->aSample = 0;
 79868 #endif
 79871   /* Check to make sure the sqlite_stat1 table exists */
 79872   sInfo.db = db;
 79873   sInfo.zDatabase = db->aDb[iDb].zName;
 79874   if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
 79875     return SQLITE_ERROR;
 79878   /* Load new statistics out of the sqlite_stat1 table */
 79879   zSql = sqlite3MPrintf(db, 
 79880       "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
 79881   if( zSql==0 ){
 79882     rc = SQLITE_NOMEM;
 79883   }else{
 79884     rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
 79885     sqlite3DbFree(db, zSql);
 79889   /* Load the statistics from the sqlite_stat3 table. */
 79890 #ifdef SQLITE_ENABLE_STAT3
 79891   if( rc==SQLITE_OK ){
 79892     int lookasideEnabled = db->lookaside.bEnabled;
 79893     db->lookaside.bEnabled = 0;
 79894     rc = loadStat3(db, sInfo.zDatabase);
 79895     db->lookaside.bEnabled = lookasideEnabled;
 79897 #endif
 79899   if( rc==SQLITE_NOMEM ){
 79900     db->mallocFailed = 1;
 79902   return rc;
 79906 #endif /* SQLITE_OMIT_ANALYZE */
 79908 /************** End of analyze.c *********************************************/
 79909 /************** Begin file attach.c ******************************************/
 79910 /*
 79911 ** 2003 April 6
 79912 **
 79913 ** The author disclaims copyright to this source code.  In place of
 79914 ** a legal notice, here is a blessing:
 79915 **
 79916 **    May you do good and not evil.
 79917 **    May you find forgiveness for yourself and forgive others.
 79918 **    May you share freely, never taking more than you give.
 79919 **
 79920 *************************************************************************
 79921 ** This file contains code used to implement the ATTACH and DETACH commands.
 79922 */
 79924 #ifndef SQLITE_OMIT_ATTACH
 79925 /*
 79926 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
 79927 ** is slightly different from resolving a normal SQL expression, because simple
 79928 ** identifiers are treated as strings, not possible column names or aliases.
 79929 **
 79930 ** i.e. if the parser sees:
 79931 **
 79932 **     ATTACH DATABASE abc AS def
 79933 **
 79934 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
 79935 ** looking for columns of the same name.
 79936 **
 79937 ** This only applies to the root node of pExpr, so the statement:
 79938 **
 79939 **     ATTACH DATABASE abc||def AS 'db2'
 79940 **
 79941 ** will fail because neither abc or def can be resolved.
 79942 */
 79943 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
 79945   int rc = SQLITE_OK;
 79946   if( pExpr ){
 79947     if( pExpr->op!=TK_ID ){
 79948       rc = sqlite3ResolveExprNames(pName, pExpr);
 79949       if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
 79950         sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
 79951         return SQLITE_ERROR;
 79953     }else{
 79954       pExpr->op = TK_STRING;
 79957   return rc;
 79960 /*
 79961 ** An SQL user-function registered to do the work of an ATTACH statement. The
 79962 ** three arguments to the function come directly from an attach statement:
 79963 **
 79964 **     ATTACH DATABASE x AS y KEY z
 79965 **
 79966 **     SELECT sqlite_attach(x, y, z)
 79967 **
 79968 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
 79969 ** third argument.
 79970 */
 79971 static void attachFunc(
 79972   sqlite3_context *context,
 79973   int NotUsed,
 79974   sqlite3_value **argv
 79975 ){
 79976   int i;
 79977   int rc = 0;
 79978   sqlite3 *db = sqlite3_context_db_handle(context);
 79979   const char *zName;
 79980   const char *zFile;
 79981   char *zPath = 0;
 79982   char *zErr = 0;
 79983   unsigned int flags;
 79984   Db *aNew;
 79985   char *zErrDyn = 0;
 79986   sqlite3_vfs *pVfs;
 79988   UNUSED_PARAMETER(NotUsed);
 79990   zFile = (const char *)sqlite3_value_text(argv[0]);
 79991   zName = (const char *)sqlite3_value_text(argv[1]);
 79992   if( zFile==0 ) zFile = "";
 79993   if( zName==0 ) zName = "";
 79995   /* Check for the following errors:
 79996   **
 79997   **     * Too many attached databases,
 79998   **     * Transaction currently open
 79999   **     * Specified database name already being used.
 80000   */
 80001   if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
 80002     zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
 80003       db->aLimit[SQLITE_LIMIT_ATTACHED]
 80004     );
 80005     goto attach_error;
 80007   if( !db->autoCommit ){
 80008     zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
 80009     goto attach_error;
 80011   for(i=0; i<db->nDb; i++){
 80012     char *z = db->aDb[i].zName;
 80013     assert( z && zName );
 80014     if( sqlite3StrICmp(z, zName)==0 ){
 80015       zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
 80016       goto attach_error;
 80020   /* Allocate the new entry in the db->aDb[] array and initialise the schema
 80021   ** hash tables.
 80022   */
 80023   if( db->aDb==db->aDbStatic ){
 80024     aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
 80025     if( aNew==0 ) return;
 80026     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
 80027   }else{
 80028     aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
 80029     if( aNew==0 ) return;
 80031   db->aDb = aNew;
 80032   aNew = &db->aDb[db->nDb];
 80033   memset(aNew, 0, sizeof(*aNew));
 80035   /* Open the database file. If the btree is successfully opened, use
 80036   ** it to obtain the database schema. At this point the schema may
 80037   ** or may not be initialised.
 80038   */
 80039   flags = db->openFlags;
 80040   rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
 80041   if( rc!=SQLITE_OK ){
 80042     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
 80043     sqlite3_result_error(context, zErr, -1);
 80044     sqlite3_free(zErr);
 80045     return;
 80047   assert( pVfs );
 80048   flags |= SQLITE_OPEN_MAIN_DB;
 80049   rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
 80050   sqlite3_free( zPath );
 80051   db->nDb++;
 80052   if( rc==SQLITE_CONSTRAINT ){
 80053     rc = SQLITE_ERROR;
 80054     zErrDyn = sqlite3MPrintf(db, "database is already attached");
 80055   }else if( rc==SQLITE_OK ){
 80056     Pager *pPager;
 80057     aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
 80058     if( !aNew->pSchema ){
 80059       rc = SQLITE_NOMEM;
 80060     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
 80061       zErrDyn = sqlite3MPrintf(db, 
 80062         "attached databases must use the same text encoding as main database");
 80063       rc = SQLITE_ERROR;
 80065     pPager = sqlite3BtreePager(aNew->pBt);
 80066     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
 80067     sqlite3BtreeSecureDelete(aNew->pBt,
 80068                              sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
 80070   aNew->safety_level = 3;
 80071   aNew->zName = sqlite3DbStrDup(db, zName);
 80072   if( rc==SQLITE_OK && aNew->zName==0 ){
 80073     rc = SQLITE_NOMEM;
 80077 #ifdef SQLITE_HAS_CODEC
 80078   if( rc==SQLITE_OK ){
 80079     extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
 80080     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
 80081     int nKey;
 80082     char *zKey;
 80083     int t = sqlite3_value_type(argv[2]);
 80084     switch( t ){
 80085       case SQLITE_INTEGER:
 80086       case SQLITE_FLOAT:
 80087         zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
 80088         rc = SQLITE_ERROR;
 80089         break;
 80091       case SQLITE_TEXT:
 80092       case SQLITE_BLOB:
 80093         nKey = sqlite3_value_bytes(argv[2]);
 80094         zKey = (char *)sqlite3_value_blob(argv[2]);
 80095         rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 80096         break;
 80098       case SQLITE_NULL:
 80099         /* No key specified.  Use the key from the main database */
 80100         sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 80101         if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
 80102           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 80104         break;
 80107 #endif
 80109   /* If the file was opened successfully, read the schema for the new database.
 80110   ** If this fails, or if opening the file failed, then close the file and 
 80111   ** remove the entry from the db->aDb[] array. i.e. put everything back the way
 80112   ** we found it.
 80113   */
 80114   if( rc==SQLITE_OK ){
 80115     sqlite3BtreeEnterAll(db);
 80116     rc = sqlite3Init(db, &zErrDyn);
 80117     sqlite3BtreeLeaveAll(db);
 80119   if( rc ){
 80120     int iDb = db->nDb - 1;
 80121     assert( iDb>=2 );
 80122     if( db->aDb[iDb].pBt ){
 80123       sqlite3BtreeClose(db->aDb[iDb].pBt);
 80124       db->aDb[iDb].pBt = 0;
 80125       db->aDb[iDb].pSchema = 0;
 80127     sqlite3ResetAllSchemasOfConnection(db);
 80128     db->nDb = iDb;
 80129     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 80130       db->mallocFailed = 1;
 80131       sqlite3DbFree(db, zErrDyn);
 80132       zErrDyn = sqlite3MPrintf(db, "out of memory");
 80133     }else if( zErrDyn==0 ){
 80134       zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
 80136     goto attach_error;
 80139   return;
 80141 attach_error:
 80142   /* Return an error if we get here */
 80143   if( zErrDyn ){
 80144     sqlite3_result_error(context, zErrDyn, -1);
 80145     sqlite3DbFree(db, zErrDyn);
 80147   if( rc ) sqlite3_result_error_code(context, rc);
 80150 /*
 80151 ** An SQL user-function registered to do the work of an DETACH statement. The
 80152 ** three arguments to the function come directly from a detach statement:
 80153 **
 80154 **     DETACH DATABASE x
 80155 **
 80156 **     SELECT sqlite_detach(x)
 80157 */
 80158 static void detachFunc(
 80159   sqlite3_context *context,
 80160   int NotUsed,
 80161   sqlite3_value **argv
 80162 ){
 80163   const char *zName = (const char *)sqlite3_value_text(argv[0]);
 80164   sqlite3 *db = sqlite3_context_db_handle(context);
 80165   int i;
 80166   Db *pDb = 0;
 80167   char zErr[128];
 80169   UNUSED_PARAMETER(NotUsed);
 80171   if( zName==0 ) zName = "";
 80172   for(i=0; i<db->nDb; i++){
 80173     pDb = &db->aDb[i];
 80174     if( pDb->pBt==0 ) continue;
 80175     if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
 80178   if( i>=db->nDb ){
 80179     sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
 80180     goto detach_error;
 80182   if( i<2 ){
 80183     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
 80184     goto detach_error;
 80186   if( !db->autoCommit ){
 80187     sqlite3_snprintf(sizeof(zErr), zErr,
 80188                      "cannot DETACH database within transaction");
 80189     goto detach_error;
 80191   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
 80192     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
 80193     goto detach_error;
 80196   sqlite3BtreeClose(pDb->pBt);
 80197   pDb->pBt = 0;
 80198   pDb->pSchema = 0;
 80199   sqlite3ResetAllSchemasOfConnection(db);
 80200   return;
 80202 detach_error:
 80203   sqlite3_result_error(context, zErr, -1);
 80206 /*
 80207 ** This procedure generates VDBE code for a single invocation of either the
 80208 ** sqlite_detach() or sqlite_attach() SQL user functions.
 80209 */
 80210 static void codeAttach(
 80211   Parse *pParse,       /* The parser context */
 80212   int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
 80213   FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
 80214   Expr *pAuthArg,      /* Expression to pass to authorization callback */
 80215   Expr *pFilename,     /* Name of database file */
 80216   Expr *pDbname,       /* Name of the database to use internally */
 80217   Expr *pKey           /* Database key for encryption extension */
 80218 ){
 80219   int rc;
 80220   NameContext sName;
 80221   Vdbe *v;
 80222   sqlite3* db = pParse->db;
 80223   int regArgs;
 80225   memset(&sName, 0, sizeof(NameContext));
 80226   sName.pParse = pParse;
 80228   if( 
 80229       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
 80230       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
 80231       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
 80232   ){
 80233     pParse->nErr++;
 80234     goto attach_end;
 80237 #ifndef SQLITE_OMIT_AUTHORIZATION
 80238   if( pAuthArg ){
 80239     char *zAuthArg;
 80240     if( pAuthArg->op==TK_STRING ){
 80241       zAuthArg = pAuthArg->u.zToken;
 80242     }else{
 80243       zAuthArg = 0;
 80245     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
 80246     if(rc!=SQLITE_OK ){
 80247       goto attach_end;
 80250 #endif /* SQLITE_OMIT_AUTHORIZATION */
 80253   v = sqlite3GetVdbe(pParse);
 80254   regArgs = sqlite3GetTempRange(pParse, 4);
 80255   sqlite3ExprCode(pParse, pFilename, regArgs);
 80256   sqlite3ExprCode(pParse, pDbname, regArgs+1);
 80257   sqlite3ExprCode(pParse, pKey, regArgs+2);
 80259   assert( v || db->mallocFailed );
 80260   if( v ){
 80261     sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
 80262     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
 80263     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
 80264     sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
 80266     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
 80267     ** statement only). For DETACH, set it to false (expire all existing
 80268     ** statements).
 80269     */
 80270     sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
 80273 attach_end:
 80274   sqlite3ExprDelete(db, pFilename);
 80275   sqlite3ExprDelete(db, pDbname);
 80276   sqlite3ExprDelete(db, pKey);
 80279 /*
 80280 ** Called by the parser to compile a DETACH statement.
 80281 **
 80282 **     DETACH pDbname
 80283 */
 80284 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
 80285   static const FuncDef detach_func = {
 80286     1,                /* nArg */
 80287     SQLITE_UTF8,      /* iPrefEnc */
 80288     0,                /* flags */
 80289     0,                /* pUserData */
 80290     0,                /* pNext */
 80291     detachFunc,       /* xFunc */
 80292     0,                /* xStep */
 80293     0,                /* xFinalize */
 80294     "sqlite_detach",  /* zName */
 80295     0,                /* pHash */
 80296     0                 /* pDestructor */
 80297   };
 80298   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
 80301 /*
 80302 ** Called by the parser to compile an ATTACH statement.
 80303 **
 80304 **     ATTACH p AS pDbname KEY pKey
 80305 */
 80306 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
 80307   static const FuncDef attach_func = {
 80308     3,                /* nArg */
 80309     SQLITE_UTF8,      /* iPrefEnc */
 80310     0,                /* flags */
 80311     0,                /* pUserData */
 80312     0,                /* pNext */
 80313     attachFunc,       /* xFunc */
 80314     0,                /* xStep */
 80315     0,                /* xFinalize */
 80316     "sqlite_attach",  /* zName */
 80317     0,                /* pHash */
 80318     0                 /* pDestructor */
 80319   };
 80320   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
 80322 #endif /* SQLITE_OMIT_ATTACH */
 80324 /*
 80325 ** Initialize a DbFixer structure.  This routine must be called prior
 80326 ** to passing the structure to one of the sqliteFixAAAA() routines below.
 80327 **
 80328 ** The return value indicates whether or not fixation is required.  TRUE
 80329 ** means we do need to fix the database references, FALSE means we do not.
 80330 */
 80331 SQLITE_PRIVATE int sqlite3FixInit(
 80332   DbFixer *pFix,      /* The fixer to be initialized */
 80333   Parse *pParse,      /* Error messages will be written here */
 80334   int iDb,            /* This is the database that must be used */
 80335   const char *zType,  /* "view", "trigger", or "index" */
 80336   const Token *pName  /* Name of the view, trigger, or index */
 80337 ){
 80338   sqlite3 *db;
 80340   if( NEVER(iDb<0) || iDb==1 ) return 0;
 80341   db = pParse->db;
 80342   assert( db->nDb>iDb );
 80343   pFix->pParse = pParse;
 80344   pFix->zDb = db->aDb[iDb].zName;
 80345   pFix->pSchema = db->aDb[iDb].pSchema;
 80346   pFix->zType = zType;
 80347   pFix->pName = pName;
 80348   return 1;
 80351 /*
 80352 ** The following set of routines walk through the parse tree and assign
 80353 ** a specific database to all table references where the database name
 80354 ** was left unspecified in the original SQL statement.  The pFix structure
 80355 ** must have been initialized by a prior call to sqlite3FixInit().
 80356 **
 80357 ** These routines are used to make sure that an index, trigger, or
 80358 ** view in one database does not refer to objects in a different database.
 80359 ** (Exception: indices, triggers, and views in the TEMP database are
 80360 ** allowed to refer to anything.)  If a reference is explicitly made
 80361 ** to an object in a different database, an error message is added to
 80362 ** pParse->zErrMsg and these routines return non-zero.  If everything
 80363 ** checks out, these routines return 0.
 80364 */
 80365 SQLITE_PRIVATE int sqlite3FixSrcList(
 80366   DbFixer *pFix,       /* Context of the fixation */
 80367   SrcList *pList       /* The Source list to check and modify */
 80368 ){
 80369   int i;
 80370   const char *zDb;
 80371   struct SrcList_item *pItem;
 80373   if( NEVER(pList==0) ) return 0;
 80374   zDb = pFix->zDb;
 80375   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
 80376     if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
 80377       sqlite3ErrorMsg(pFix->pParse,
 80378          "%s %T cannot reference objects in database %s",
 80379          pFix->zType, pFix->pName, pItem->zDatabase);
 80380       return 1;
 80382     sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
 80383     pItem->zDatabase = 0;
 80384     pItem->pSchema = pFix->pSchema;
 80385 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
 80386     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
 80387     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
 80388 #endif
 80390   return 0;
 80392 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
 80393 SQLITE_PRIVATE int sqlite3FixSelect(
 80394   DbFixer *pFix,       /* Context of the fixation */
 80395   Select *pSelect      /* The SELECT statement to be fixed to one database */
 80396 ){
 80397   while( pSelect ){
 80398     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
 80399       return 1;
 80401     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
 80402       return 1;
 80404     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
 80405       return 1;
 80407     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
 80408       return 1;
 80410     pSelect = pSelect->pPrior;
 80412   return 0;
 80414 SQLITE_PRIVATE int sqlite3FixExpr(
 80415   DbFixer *pFix,     /* Context of the fixation */
 80416   Expr *pExpr        /* The expression to be fixed to one database */
 80417 ){
 80418   while( pExpr ){
 80419     if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
 80420     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 80421       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
 80422     }else{
 80423       if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
 80425     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
 80426       return 1;
 80428     pExpr = pExpr->pLeft;
 80430   return 0;
 80432 SQLITE_PRIVATE int sqlite3FixExprList(
 80433   DbFixer *pFix,     /* Context of the fixation */
 80434   ExprList *pList    /* The expression to be fixed to one database */
 80435 ){
 80436   int i;
 80437   struct ExprList_item *pItem;
 80438   if( pList==0 ) return 0;
 80439   for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
 80440     if( sqlite3FixExpr(pFix, pItem->pExpr) ){
 80441       return 1;
 80444   return 0;
 80446 #endif
 80448 #ifndef SQLITE_OMIT_TRIGGER
 80449 SQLITE_PRIVATE int sqlite3FixTriggerStep(
 80450   DbFixer *pFix,     /* Context of the fixation */
 80451   TriggerStep *pStep /* The trigger step be fixed to one database */
 80452 ){
 80453   while( pStep ){
 80454     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
 80455       return 1;
 80457     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
 80458       return 1;
 80460     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
 80461       return 1;
 80463     pStep = pStep->pNext;
 80465   return 0;
 80467 #endif
 80469 /************** End of attach.c **********************************************/
 80470 /************** Begin file auth.c ********************************************/
 80471 /*
 80472 ** 2003 January 11
 80473 **
 80474 ** The author disclaims copyright to this source code.  In place of
 80475 ** a legal notice, here is a blessing:
 80476 **
 80477 **    May you do good and not evil.
 80478 **    May you find forgiveness for yourself and forgive others.
 80479 **    May you share freely, never taking more than you give.
 80480 **
 80481 *************************************************************************
 80482 ** This file contains code used to implement the sqlite3_set_authorizer()
 80483 ** API.  This facility is an optional feature of the library.  Embedded
 80484 ** systems that do not need this facility may omit it by recompiling
 80485 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
 80486 */
 80488 /*
 80489 ** All of the code in this file may be omitted by defining a single
 80490 ** macro.
 80491 */
 80492 #ifndef SQLITE_OMIT_AUTHORIZATION
 80494 /*
 80495 ** Set or clear the access authorization function.
 80496 **
 80497 ** The access authorization function is be called during the compilation
 80498 ** phase to verify that the user has read and/or write access permission on
 80499 ** various fields of the database.  The first argument to the auth function
 80500 ** is a copy of the 3rd argument to this routine.  The second argument
 80501 ** to the auth function is one of these constants:
 80502 **
 80503 **       SQLITE_CREATE_INDEX
 80504 **       SQLITE_CREATE_TABLE
 80505 **       SQLITE_CREATE_TEMP_INDEX
 80506 **       SQLITE_CREATE_TEMP_TABLE
 80507 **       SQLITE_CREATE_TEMP_TRIGGER
 80508 **       SQLITE_CREATE_TEMP_VIEW
 80509 **       SQLITE_CREATE_TRIGGER
 80510 **       SQLITE_CREATE_VIEW
 80511 **       SQLITE_DELETE
 80512 **       SQLITE_DROP_INDEX
 80513 **       SQLITE_DROP_TABLE
 80514 **       SQLITE_DROP_TEMP_INDEX
 80515 **       SQLITE_DROP_TEMP_TABLE
 80516 **       SQLITE_DROP_TEMP_TRIGGER
 80517 **       SQLITE_DROP_TEMP_VIEW
 80518 **       SQLITE_DROP_TRIGGER
 80519 **       SQLITE_DROP_VIEW
 80520 **       SQLITE_INSERT
 80521 **       SQLITE_PRAGMA
 80522 **       SQLITE_READ
 80523 **       SQLITE_SELECT
 80524 **       SQLITE_TRANSACTION
 80525 **       SQLITE_UPDATE
 80526 **
 80527 ** The third and fourth arguments to the auth function are the name of
 80528 ** the table and the column that are being accessed.  The auth function
 80529 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
 80530 ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
 80531 ** means that the SQL statement will never-run - the sqlite3_exec() call
 80532 ** will return with an error.  SQLITE_IGNORE means that the SQL statement
 80533 ** should run but attempts to read the specified column will return NULL
 80534 ** and attempts to write the column will be ignored.
 80535 **
 80536 ** Setting the auth function to NULL disables this hook.  The default
 80537 ** setting of the auth function is NULL.
 80538 */
 80539 SQLITE_API int sqlite3_set_authorizer(
 80540   sqlite3 *db,
 80541   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
 80542   void *pArg
 80543 ){
 80544   sqlite3_mutex_enter(db->mutex);
 80545   db->xAuth = xAuth;
 80546   db->pAuthArg = pArg;
 80547   sqlite3ExpirePreparedStatements(db);
 80548   sqlite3_mutex_leave(db->mutex);
 80549   return SQLITE_OK;
 80552 /*
 80553 ** Write an error message into pParse->zErrMsg that explains that the
 80554 ** user-supplied authorization function returned an illegal value.
 80555 */
 80556 static void sqliteAuthBadReturnCode(Parse *pParse){
 80557   sqlite3ErrorMsg(pParse, "authorizer malfunction");
 80558   pParse->rc = SQLITE_ERROR;
 80561 /*
 80562 ** Invoke the authorization callback for permission to read column zCol from
 80563 ** table zTab in database zDb. This function assumes that an authorization
 80564 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
 80565 **
 80566 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
 80567 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
 80568 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
 80569 */
 80570 SQLITE_PRIVATE int sqlite3AuthReadCol(
 80571   Parse *pParse,                  /* The parser context */
 80572   const char *zTab,               /* Table name */
 80573   const char *zCol,               /* Column name */
 80574   int iDb                         /* Index of containing database. */
 80575 ){
 80576   sqlite3 *db = pParse->db;       /* Database handle */
 80577   char *zDb = db->aDb[iDb].zName; /* Name of attached database */
 80578   int rc;                         /* Auth callback return code */
 80580   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
 80581   if( rc==SQLITE_DENY ){
 80582     if( db->nDb>2 || iDb!=0 ){
 80583       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
 80584     }else{
 80585       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
 80587     pParse->rc = SQLITE_AUTH;
 80588   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
 80589     sqliteAuthBadReturnCode(pParse);
 80591   return rc;
 80594 /*
 80595 ** The pExpr should be a TK_COLUMN expression.  The table referred to
 80596 ** is in pTabList or else it is the NEW or OLD table of a trigger.  
 80597 ** Check to see if it is OK to read this particular column.
 80598 **
 80599 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
 80600 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
 80601 ** then generate an error.
 80602 */
 80603 SQLITE_PRIVATE void sqlite3AuthRead(
 80604   Parse *pParse,        /* The parser context */
 80605   Expr *pExpr,          /* The expression to check authorization on */
 80606   Schema *pSchema,      /* The schema of the expression */
 80607   SrcList *pTabList     /* All table that pExpr might refer to */
 80608 ){
 80609   sqlite3 *db = pParse->db;
 80610   Table *pTab = 0;      /* The table being read */
 80611   const char *zCol;     /* Name of the column of the table */
 80612   int iSrc;             /* Index in pTabList->a[] of table being read */
 80613   int iDb;              /* The index of the database the expression refers to */
 80614   int iCol;             /* Index of column in table */
 80616   if( db->xAuth==0 ) return;
 80617   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
 80618   if( iDb<0 ){
 80619     /* An attempt to read a column out of a subquery or other
 80620     ** temporary table. */
 80621     return;
 80624   assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
 80625   if( pExpr->op==TK_TRIGGER ){
 80626     pTab = pParse->pTriggerTab;
 80627   }else{
 80628     assert( pTabList );
 80629     for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
 80630       if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
 80631         pTab = pTabList->a[iSrc].pTab;
 80632         break;
 80636   iCol = pExpr->iColumn;
 80637   if( NEVER(pTab==0) ) return;
 80639   if( iCol>=0 ){
 80640     assert( iCol<pTab->nCol );
 80641     zCol = pTab->aCol[iCol].zName;
 80642   }else if( pTab->iPKey>=0 ){
 80643     assert( pTab->iPKey<pTab->nCol );
 80644     zCol = pTab->aCol[pTab->iPKey].zName;
 80645   }else{
 80646     zCol = "ROWID";
 80648   assert( iDb>=0 && iDb<db->nDb );
 80649   if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
 80650     pExpr->op = TK_NULL;
 80654 /*
 80655 ** Do an authorization check using the code and arguments given.  Return
 80656 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
 80657 ** is returned, then the error count and error message in pParse are
 80658 ** modified appropriately.
 80659 */
 80660 SQLITE_PRIVATE int sqlite3AuthCheck(
 80661   Parse *pParse,
 80662   int code,
 80663   const char *zArg1,
 80664   const char *zArg2,
 80665   const char *zArg3
 80666 ){
 80667   sqlite3 *db = pParse->db;
 80668   int rc;
 80670   /* Don't do any authorization checks if the database is initialising
 80671   ** or if the parser is being invoked from within sqlite3_declare_vtab.
 80672   */
 80673   if( db->init.busy || IN_DECLARE_VTAB ){
 80674     return SQLITE_OK;
 80677   if( db->xAuth==0 ){
 80678     return SQLITE_OK;
 80680   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
 80681   if( rc==SQLITE_DENY ){
 80682     sqlite3ErrorMsg(pParse, "not authorized");
 80683     pParse->rc = SQLITE_AUTH;
 80684   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
 80685     rc = SQLITE_DENY;
 80686     sqliteAuthBadReturnCode(pParse);
 80688   return rc;
 80691 /*
 80692 ** Push an authorization context.  After this routine is called, the
 80693 ** zArg3 argument to authorization callbacks will be zContext until
 80694 ** popped.  Or if pParse==0, this routine is a no-op.
 80695 */
 80696 SQLITE_PRIVATE void sqlite3AuthContextPush(
 80697   Parse *pParse,
 80698   AuthContext *pContext, 
 80699   const char *zContext
 80700 ){
 80701   assert( pParse );
 80702   pContext->pParse = pParse;
 80703   pContext->zAuthContext = pParse->zAuthContext;
 80704   pParse->zAuthContext = zContext;
 80707 /*
 80708 ** Pop an authorization context that was previously pushed
 80709 ** by sqlite3AuthContextPush
 80710 */
 80711 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
 80712   if( pContext->pParse ){
 80713     pContext->pParse->zAuthContext = pContext->zAuthContext;
 80714     pContext->pParse = 0;
 80718 #endif /* SQLITE_OMIT_AUTHORIZATION */
 80720 /************** End of auth.c ************************************************/
 80721 /************** Begin file build.c *******************************************/
 80722 /*
 80723 ** 2001 September 15
 80724 **
 80725 ** The author disclaims copyright to this source code.  In place of
 80726 ** a legal notice, here is a blessing:
 80727 **
 80728 **    May you do good and not evil.
 80729 **    May you find forgiveness for yourself and forgive others.
 80730 **    May you share freely, never taking more than you give.
 80731 **
 80732 *************************************************************************
 80733 ** This file contains C code routines that are called by the SQLite parser
 80734 ** when syntax rules are reduced.  The routines in this file handle the
 80735 ** following kinds of SQL syntax:
 80736 **
 80737 **     CREATE TABLE
 80738 **     DROP TABLE
 80739 **     CREATE INDEX
 80740 **     DROP INDEX
 80741 **     creating ID lists
 80742 **     BEGIN TRANSACTION
 80743 **     COMMIT
 80744 **     ROLLBACK
 80745 */
 80747 /*
 80748 ** This routine is called when a new SQL statement is beginning to
 80749 ** be parsed.  Initialize the pParse structure as needed.
 80750 */
 80751 SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
 80752   pParse->explain = (u8)explainFlag;
 80753   pParse->nVar = 0;
 80756 #ifndef SQLITE_OMIT_SHARED_CACHE
 80757 /*
 80758 ** The TableLock structure is only used by the sqlite3TableLock() and
 80759 ** codeTableLocks() functions.
 80760 */
 80761 struct TableLock {
 80762   int iDb;             /* The database containing the table to be locked */
 80763   int iTab;            /* The root page of the table to be locked */
 80764   u8 isWriteLock;      /* True for write lock.  False for a read lock */
 80765   const char *zName;   /* Name of the table */
 80766 };
 80768 /*
 80769 ** Record the fact that we want to lock a table at run-time.  
 80770 **
 80771 ** The table to be locked has root page iTab and is found in database iDb.
 80772 ** A read or a write lock can be taken depending on isWritelock.
 80773 **
 80774 ** This routine just records the fact that the lock is desired.  The
 80775 ** code to make the lock occur is generated by a later call to
 80776 ** codeTableLocks() which occurs during sqlite3FinishCoding().
 80777 */
 80778 SQLITE_PRIVATE void sqlite3TableLock(
 80779   Parse *pParse,     /* Parsing context */
 80780   int iDb,           /* Index of the database containing the table to lock */
 80781   int iTab,          /* Root page number of the table to be locked */
 80782   u8 isWriteLock,    /* True for a write lock */
 80783   const char *zName  /* Name of the table to be locked */
 80784 ){
 80785   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 80786   int i;
 80787   int nBytes;
 80788   TableLock *p;
 80789   assert( iDb>=0 );
 80791   for(i=0; i<pToplevel->nTableLock; i++){
 80792     p = &pToplevel->aTableLock[i];
 80793     if( p->iDb==iDb && p->iTab==iTab ){
 80794       p->isWriteLock = (p->isWriteLock || isWriteLock);
 80795       return;
 80799   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
 80800   pToplevel->aTableLock =
 80801       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
 80802   if( pToplevel->aTableLock ){
 80803     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
 80804     p->iDb = iDb;
 80805     p->iTab = iTab;
 80806     p->isWriteLock = isWriteLock;
 80807     p->zName = zName;
 80808   }else{
 80809     pToplevel->nTableLock = 0;
 80810     pToplevel->db->mallocFailed = 1;
 80814 /*
 80815 ** Code an OP_TableLock instruction for each table locked by the
 80816 ** statement (configured by calls to sqlite3TableLock()).
 80817 */
 80818 static void codeTableLocks(Parse *pParse){
 80819   int i;
 80820   Vdbe *pVdbe; 
 80822   pVdbe = sqlite3GetVdbe(pParse);
 80823   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
 80825   for(i=0; i<pParse->nTableLock; i++){
 80826     TableLock *p = &pParse->aTableLock[i];
 80827     int p1 = p->iDb;
 80828     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
 80829                       p->zName, P4_STATIC);
 80832 #else
 80833   #define codeTableLocks(x)
 80834 #endif
 80836 /*
 80837 ** This routine is called after a single SQL statement has been
 80838 ** parsed and a VDBE program to execute that statement has been
 80839 ** prepared.  This routine puts the finishing touches on the
 80840 ** VDBE program and resets the pParse structure for the next
 80841 ** parse.
 80842 **
 80843 ** Note that if an error occurred, it might be the case that
 80844 ** no VDBE code was generated.
 80845 */
 80846 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
 80847   sqlite3 *db;
 80848   Vdbe *v;
 80850   assert( pParse->pToplevel==0 );
 80851   db = pParse->db;
 80852   if( db->mallocFailed ) return;
 80853   if( pParse->nested ) return;
 80854   if( pParse->nErr ) return;
 80856   /* Begin by generating some termination code at the end of the
 80857   ** vdbe program
 80858   */
 80859   v = sqlite3GetVdbe(pParse);
 80860   assert( !pParse->isMultiWrite 
 80861        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
 80862   if( v ){
 80863     sqlite3VdbeAddOp0(v, OP_Halt);
 80865     /* The cookie mask contains one bit for each database file open.
 80866     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
 80867     ** set for each database that is used.  Generate code to start a
 80868     ** transaction on each used database and to verify the schema cookie
 80869     ** on each used database.
 80870     */
 80871     if( pParse->cookieGoto>0 ){
 80872       yDbMask mask;
 80873       int iDb;
 80874       sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
 80875       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
 80876         if( (mask & pParse->cookieMask)==0 ) continue;
 80877         sqlite3VdbeUsesBtree(v, iDb);
 80878         sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
 80879         if( db->init.busy==0 ){
 80880           assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 80881           sqlite3VdbeAddOp3(v, OP_VerifyCookie,
 80882                             iDb, pParse->cookieValue[iDb],
 80883                             db->aDb[iDb].pSchema->iGeneration);
 80886 #ifndef SQLITE_OMIT_VIRTUALTABLE
 80888         int i;
 80889         for(i=0; i<pParse->nVtabLock; i++){
 80890           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
 80891           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
 80893         pParse->nVtabLock = 0;
 80895 #endif
 80897       /* Once all the cookies have been verified and transactions opened, 
 80898       ** obtain the required table-locks. This is a no-op unless the 
 80899       ** shared-cache feature is enabled.
 80900       */
 80901       codeTableLocks(pParse);
 80903       /* Initialize any AUTOINCREMENT data structures required.
 80904       */
 80905       sqlite3AutoincrementBegin(pParse);
 80907       /* Finally, jump back to the beginning of the executable code. */
 80908       sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
 80913   /* Get the VDBE program ready for execution
 80914   */
 80915   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
 80916 #ifdef SQLITE_DEBUG
 80917     FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
 80918     sqlite3VdbeTrace(v, trace);
 80919 #endif
 80920     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
 80921     /* A minimum of one cursor is required if autoincrement is used
 80922     *  See ticket [a696379c1f08866] */
 80923     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
 80924     sqlite3VdbeMakeReady(v, pParse);
 80925     pParse->rc = SQLITE_DONE;
 80926     pParse->colNamesSet = 0;
 80927   }else{
 80928     pParse->rc = SQLITE_ERROR;
 80930   pParse->nTab = 0;
 80931   pParse->nMem = 0;
 80932   pParse->nSet = 0;
 80933   pParse->nVar = 0;
 80934   pParse->cookieMask = 0;
 80935   pParse->cookieGoto = 0;
 80938 /*
 80939 ** Run the parser and code generator recursively in order to generate
 80940 ** code for the SQL statement given onto the end of the pParse context
 80941 ** currently under construction.  When the parser is run recursively
 80942 ** this way, the final OP_Halt is not appended and other initialization
 80943 ** and finalization steps are omitted because those are handling by the
 80944 ** outermost parser.
 80945 **
 80946 ** Not everything is nestable.  This facility is designed to permit
 80947 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
 80948 ** care if you decide to try to use this routine for some other purposes.
 80949 */
 80950 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
 80951   va_list ap;
 80952   char *zSql;
 80953   char *zErrMsg = 0;
 80954   sqlite3 *db = pParse->db;
 80955 # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
 80956   char saveBuf[SAVE_SZ];
 80958   if( pParse->nErr ) return;
 80959   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
 80960   va_start(ap, zFormat);
 80961   zSql = sqlite3VMPrintf(db, zFormat, ap);
 80962   va_end(ap);
 80963   if( zSql==0 ){
 80964     return;   /* A malloc must have failed */
 80966   pParse->nested++;
 80967   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
 80968   memset(&pParse->nVar, 0, SAVE_SZ);
 80969   sqlite3RunParser(pParse, zSql, &zErrMsg);
 80970   sqlite3DbFree(db, zErrMsg);
 80971   sqlite3DbFree(db, zSql);
 80972   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
 80973   pParse->nested--;
 80976 /*
 80977 ** Locate the in-memory structure that describes a particular database
 80978 ** table given the name of that table and (optionally) the name of the
 80979 ** database containing the table.  Return NULL if not found.
 80980 **
 80981 ** If zDatabase is 0, all databases are searched for the table and the
 80982 ** first matching table is returned.  (No checking for duplicate table
 80983 ** names is done.)  The search order is TEMP first, then MAIN, then any
 80984 ** auxiliary databases added using the ATTACH command.
 80985 **
 80986 ** See also sqlite3LocateTable().
 80987 */
 80988 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
 80989   Table *p = 0;
 80990   int i;
 80991   int nName;
 80992   assert( zName!=0 );
 80993   nName = sqlite3Strlen30(zName);
 80994   /* All mutexes are required for schema access.  Make sure we hold them. */
 80995   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 80996   for(i=OMIT_TEMPDB; i<db->nDb; i++){
 80997     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
 80998     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
 80999     assert( sqlite3SchemaMutexHeld(db, j, 0) );
 81000     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
 81001     if( p ) break;
 81003   return p;
 81006 /*
 81007 ** Locate the in-memory structure that describes a particular database
 81008 ** table given the name of that table and (optionally) the name of the
 81009 ** database containing the table.  Return NULL if not found.  Also leave an
 81010 ** error message in pParse->zErrMsg.
 81011 **
 81012 ** The difference between this routine and sqlite3FindTable() is that this
 81013 ** routine leaves an error message in pParse->zErrMsg where
 81014 ** sqlite3FindTable() does not.
 81015 */
 81016 SQLITE_PRIVATE Table *sqlite3LocateTable(
 81017   Parse *pParse,         /* context in which to report errors */
 81018   int isView,            /* True if looking for a VIEW rather than a TABLE */
 81019   const char *zName,     /* Name of the table we are looking for */
 81020   const char *zDbase     /* Name of the database.  Might be NULL */
 81021 ){
 81022   Table *p;
 81024   /* Read the database schema. If an error occurs, leave an error message
 81025   ** and code in pParse and return NULL. */
 81026   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 81027     return 0;
 81030   p = sqlite3FindTable(pParse->db, zName, zDbase);
 81031   if( p==0 ){
 81032     const char *zMsg = isView ? "no such view" : "no such table";
 81033     if( zDbase ){
 81034       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
 81035     }else{
 81036       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
 81038     pParse->checkSchema = 1;
 81040   return p;
 81043 /*
 81044 ** Locate the table identified by *p.
 81045 **
 81046 ** This is a wrapper around sqlite3LocateTable(). The difference between
 81047 ** sqlite3LocateTable() and this function is that this function restricts
 81048 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
 81049 ** non-NULL if it is part of a view or trigger program definition. See
 81050 ** sqlite3FixSrcList() for details.
 81051 */
 81052 SQLITE_PRIVATE Table *sqlite3LocateTableItem(
 81053   Parse *pParse, 
 81054   int isView, 
 81055   struct SrcList_item *p
 81056 ){
 81057   const char *zDb;
 81058   assert( p->pSchema==0 || p->zDatabase==0 );
 81059   if( p->pSchema ){
 81060     int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
 81061     zDb = pParse->db->aDb[iDb].zName;
 81062   }else{
 81063     zDb = p->zDatabase;
 81065   return sqlite3LocateTable(pParse, isView, p->zName, zDb);
 81068 /*
 81069 ** Locate the in-memory structure that describes 
 81070 ** a particular index given the name of that index
 81071 ** and the name of the database that contains the index.
 81072 ** Return NULL if not found.
 81073 **
 81074 ** If zDatabase is 0, all databases are searched for the
 81075 ** table and the first matching index is returned.  (No checking
 81076 ** for duplicate index names is done.)  The search order is
 81077 ** TEMP first, then MAIN, then any auxiliary databases added
 81078 ** using the ATTACH command.
 81079 */
 81080 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
 81081   Index *p = 0;
 81082   int i;
 81083   int nName = sqlite3Strlen30(zName);
 81084   /* All mutexes are required for schema access.  Make sure we hold them. */
 81085   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 81086   for(i=OMIT_TEMPDB; i<db->nDb; i++){
 81087     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 81088     Schema *pSchema = db->aDb[j].pSchema;
 81089     assert( pSchema );
 81090     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
 81091     assert( sqlite3SchemaMutexHeld(db, j, 0) );
 81092     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
 81093     if( p ) break;
 81095   return p;
 81098 /*
 81099 ** Reclaim the memory used by an index
 81100 */
 81101 static void freeIndex(sqlite3 *db, Index *p){
 81102 #ifndef SQLITE_OMIT_ANALYZE
 81103   sqlite3DeleteIndexSamples(db, p);
 81104 #endif
 81105   sqlite3DbFree(db, p->zColAff);
 81106   sqlite3DbFree(db, p);
 81109 /*
 81110 ** For the index called zIdxName which is found in the database iDb,
 81111 ** unlike that index from its Table then remove the index from
 81112 ** the index hash table and free all memory structures associated
 81113 ** with the index.
 81114 */
 81115 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
 81116   Index *pIndex;
 81117   int len;
 81118   Hash *pHash;
 81120   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 81121   pHash = &db->aDb[iDb].pSchema->idxHash;
 81122   len = sqlite3Strlen30(zIdxName);
 81123   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
 81124   if( ALWAYS(pIndex) ){
 81125     if( pIndex->pTable->pIndex==pIndex ){
 81126       pIndex->pTable->pIndex = pIndex->pNext;
 81127     }else{
 81128       Index *p;
 81129       /* Justification of ALWAYS();  The index must be on the list of
 81130       ** indices. */
 81131       p = pIndex->pTable->pIndex;
 81132       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
 81133       if( ALWAYS(p && p->pNext==pIndex) ){
 81134         p->pNext = pIndex->pNext;
 81137     freeIndex(db, pIndex);
 81139   db->flags |= SQLITE_InternChanges;
 81142 /*
 81143 ** Look through the list of open database files in db->aDb[] and if
 81144 ** any have been closed, remove them from the list.  Reallocate the
 81145 ** db->aDb[] structure to a smaller size, if possible.
 81146 **
 81147 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
 81148 ** are never candidates for being collapsed.
 81149 */
 81150 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
 81151   int i, j;
 81152   for(i=j=2; i<db->nDb; i++){
 81153     struct Db *pDb = &db->aDb[i];
 81154     if( pDb->pBt==0 ){
 81155       sqlite3DbFree(db, pDb->zName);
 81156       pDb->zName = 0;
 81157       continue;
 81159     if( j<i ){
 81160       db->aDb[j] = db->aDb[i];
 81162     j++;
 81164   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
 81165   db->nDb = j;
 81166   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
 81167     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
 81168     sqlite3DbFree(db, db->aDb);
 81169     db->aDb = db->aDbStatic;
 81173 /*
 81174 ** Reset the schema for the database at index iDb.  Also reset the
 81175 ** TEMP schema.
 81176 */
 81177 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
 81178   Db *pDb;
 81179   assert( iDb<db->nDb );
 81181   /* Case 1:  Reset the single schema identified by iDb */
 81182   pDb = &db->aDb[iDb];
 81183   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 81184   assert( pDb->pSchema!=0 );
 81185   sqlite3SchemaClear(pDb->pSchema);
 81187   /* If any database other than TEMP is reset, then also reset TEMP
 81188   ** since TEMP might be holding triggers that reference tables in the
 81189   ** other database.
 81190   */
 81191   if( iDb!=1 ){
 81192     pDb = &db->aDb[1];
 81193     assert( pDb->pSchema!=0 );
 81194     sqlite3SchemaClear(pDb->pSchema);
 81196   return;
 81199 /*
 81200 ** Erase all schema information from all attached databases (including
 81201 ** "main" and "temp") for a single database connection.
 81202 */
 81203 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
 81204   int i;
 81205   sqlite3BtreeEnterAll(db);
 81206   for(i=0; i<db->nDb; i++){
 81207     Db *pDb = &db->aDb[i];
 81208     if( pDb->pSchema ){
 81209       sqlite3SchemaClear(pDb->pSchema);
 81212   db->flags &= ~SQLITE_InternChanges;
 81213   sqlite3VtabUnlockList(db);
 81214   sqlite3BtreeLeaveAll(db);
 81215   sqlite3CollapseDatabaseArray(db);
 81218 /*
 81219 ** This routine is called when a commit occurs.
 81220 */
 81221 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
 81222   db->flags &= ~SQLITE_InternChanges;
 81225 /*
 81226 ** Delete memory allocated for the column names of a table or view (the
 81227 ** Table.aCol[] array).
 81228 */
 81229 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
 81230   int i;
 81231   Column *pCol;
 81232   assert( pTable!=0 );
 81233   if( (pCol = pTable->aCol)!=0 ){
 81234     for(i=0; i<pTable->nCol; i++, pCol++){
 81235       sqlite3DbFree(db, pCol->zName);
 81236       sqlite3ExprDelete(db, pCol->pDflt);
 81237       sqlite3DbFree(db, pCol->zDflt);
 81238       sqlite3DbFree(db, pCol->zType);
 81239       sqlite3DbFree(db, pCol->zColl);
 81241     sqlite3DbFree(db, pTable->aCol);
 81245 /*
 81246 ** Remove the memory data structures associated with the given
 81247 ** Table.  No changes are made to disk by this routine.
 81248 **
 81249 ** This routine just deletes the data structure.  It does not unlink
 81250 ** the table data structure from the hash table.  But it does destroy
 81251 ** memory structures of the indices and foreign keys associated with 
 81252 ** the table.
 81253 **
 81254 ** The db parameter is optional.  It is needed if the Table object 
 81255 ** contains lookaside memory.  (Table objects in the schema do not use
 81256 ** lookaside memory, but some ephemeral Table objects do.)  Or the
 81257 ** db parameter can be used with db->pnBytesFreed to measure the memory
 81258 ** used by the Table object.
 81259 */
 81260 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
 81261   Index *pIndex, *pNext;
 81262   TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
 81264   assert( !pTable || pTable->nRef>0 );
 81266   /* Do not delete the table until the reference count reaches zero. */
 81267   if( !pTable ) return;
 81268   if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
 81270   /* Record the number of outstanding lookaside allocations in schema Tables
 81271   ** prior to doing any free() operations.  Since schema Tables do not use
 81272   ** lookaside, this number should not change. */
 81273   TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
 81274                          db->lookaside.nOut : 0 );
 81276   /* Delete all indices associated with this table. */
 81277   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
 81278     pNext = pIndex->pNext;
 81279     assert( pIndex->pSchema==pTable->pSchema );
 81280     if( !db || db->pnBytesFreed==0 ){
 81281       char *zName = pIndex->zName; 
 81282       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
 81283          &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
 81284       );
 81285       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 81286       assert( pOld==pIndex || pOld==0 );
 81288     freeIndex(db, pIndex);
 81291   /* Delete any foreign keys attached to this table. */
 81292   sqlite3FkDelete(db, pTable);
 81294   /* Delete the Table structure itself.
 81295   */
 81296   sqliteDeleteColumnNames(db, pTable);
 81297   sqlite3DbFree(db, pTable->zName);
 81298   sqlite3DbFree(db, pTable->zColAff);
 81299   sqlite3SelectDelete(db, pTable->pSelect);
 81300 #ifndef SQLITE_OMIT_CHECK
 81301   sqlite3ExprListDelete(db, pTable->pCheck);
 81302 #endif
 81303 #ifndef SQLITE_OMIT_VIRTUALTABLE
 81304   sqlite3VtabClear(db, pTable);
 81305 #endif
 81306   sqlite3DbFree(db, pTable);
 81308   /* Verify that no lookaside memory was used by schema tables */
 81309   assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
 81312 /*
 81313 ** Unlink the given table from the hash tables and the delete the
 81314 ** table structure with all its indices and foreign keys.
 81315 */
 81316 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
 81317   Table *p;
 81318   Db *pDb;
 81320   assert( db!=0 );
 81321   assert( iDb>=0 && iDb<db->nDb );
 81322   assert( zTabName );
 81323   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 81324   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
 81325   pDb = &db->aDb[iDb];
 81326   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
 81327                         sqlite3Strlen30(zTabName),0);
 81328   sqlite3DeleteTable(db, p);
 81329   db->flags |= SQLITE_InternChanges;
 81332 /*
 81333 ** Given a token, return a string that consists of the text of that
 81334 ** token.  Space to hold the returned string
 81335 ** is obtained from sqliteMalloc() and must be freed by the calling
 81336 ** function.
 81337 **
 81338 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
 81339 ** surround the body of the token are removed.
 81340 **
 81341 ** Tokens are often just pointers into the original SQL text and so
 81342 ** are not \000 terminated and are not persistent.  The returned string
 81343 ** is \000 terminated and is persistent.
 81344 */
 81345 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
 81346   char *zName;
 81347   if( pName ){
 81348     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
 81349     sqlite3Dequote(zName);
 81350   }else{
 81351     zName = 0;
 81353   return zName;
 81356 /*
 81357 ** Open the sqlite_master table stored in database number iDb for
 81358 ** writing. The table is opened using cursor 0.
 81359 */
 81360 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
 81361   Vdbe *v = sqlite3GetVdbe(p);
 81362   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
 81363   sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
 81364   sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
 81365   if( p->nTab==0 ){
 81366     p->nTab = 1;
 81370 /*
 81371 ** Parameter zName points to a nul-terminated buffer containing the name
 81372 ** of a database ("main", "temp" or the name of an attached db). This
 81373 ** function returns the index of the named database in db->aDb[], or
 81374 ** -1 if the named db cannot be found.
 81375 */
 81376 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
 81377   int i = -1;         /* Database number */
 81378   if( zName ){
 81379     Db *pDb;
 81380     int n = sqlite3Strlen30(zName);
 81381     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
 81382       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && 
 81383           0==sqlite3StrICmp(pDb->zName, zName) ){
 81384         break;
 81388   return i;
 81391 /*
 81392 ** The token *pName contains the name of a database (either "main" or
 81393 ** "temp" or the name of an attached db). This routine returns the
 81394 ** index of the named database in db->aDb[], or -1 if the named db 
 81395 ** does not exist.
 81396 */
 81397 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
 81398   int i;                               /* Database number */
 81399   char *zName;                         /* Name we are searching for */
 81400   zName = sqlite3NameFromToken(db, pName);
 81401   i = sqlite3FindDbName(db, zName);
 81402   sqlite3DbFree(db, zName);
 81403   return i;
 81406 /* The table or view or trigger name is passed to this routine via tokens
 81407 ** pName1 and pName2. If the table name was fully qualified, for example:
 81408 **
 81409 ** CREATE TABLE xxx.yyy (...);
 81410 ** 
 81411 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
 81412 ** the table name is not fully qualified, i.e.:
 81413 **
 81414 ** CREATE TABLE yyy(...);
 81415 **
 81416 ** Then pName1 is set to "yyy" and pName2 is "".
 81417 **
 81418 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
 81419 ** pName2) that stores the unqualified table name.  The index of the
 81420 ** database "xxx" is returned.
 81421 */
 81422 SQLITE_PRIVATE int sqlite3TwoPartName(
 81423   Parse *pParse,      /* Parsing and code generating context */
 81424   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
 81425   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
 81426   Token **pUnqual     /* Write the unqualified object name here */
 81427 ){
 81428   int iDb;                    /* Database holding the object */
 81429   sqlite3 *db = pParse->db;
 81431   if( ALWAYS(pName2!=0) && pName2->n>0 ){
 81432     if( db->init.busy ) {
 81433       sqlite3ErrorMsg(pParse, "corrupt database");
 81434       pParse->nErr++;
 81435       return -1;
 81437     *pUnqual = pName2;
 81438     iDb = sqlite3FindDb(db, pName1);
 81439     if( iDb<0 ){
 81440       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
 81441       pParse->nErr++;
 81442       return -1;
 81444   }else{
 81445     assert( db->init.iDb==0 || db->init.busy );
 81446     iDb = db->init.iDb;
 81447     *pUnqual = pName1;
 81449   return iDb;
 81452 /*
 81453 ** This routine is used to check if the UTF-8 string zName is a legal
 81454 ** unqualified name for a new schema object (table, index, view or
 81455 ** trigger). All names are legal except those that begin with the string
 81456 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
 81457 ** is reserved for internal use.
 81458 */
 81459 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
 81460   if( !pParse->db->init.busy && pParse->nested==0 
 81461           && (pParse->db->flags & SQLITE_WriteSchema)==0
 81462           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
 81463     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
 81464     return SQLITE_ERROR;
 81466   return SQLITE_OK;
 81469 /*
 81470 ** Begin constructing a new table representation in memory.  This is
 81471 ** the first of several action routines that get called in response
 81472 ** to a CREATE TABLE statement.  In particular, this routine is called
 81473 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
 81474 ** flag is true if the table should be stored in the auxiliary database
 81475 ** file instead of in the main database file.  This is normally the case
 81476 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
 81477 ** CREATE and TABLE.
 81478 **
 81479 ** The new table record is initialized and put in pParse->pNewTable.
 81480 ** As more of the CREATE TABLE statement is parsed, additional action
 81481 ** routines will be called to add more information to this record.
 81482 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
 81483 ** is called to complete the construction of the new table record.
 81484 */
 81485 SQLITE_PRIVATE void sqlite3StartTable(
 81486   Parse *pParse,   /* Parser context */
 81487   Token *pName1,   /* First part of the name of the table or view */
 81488   Token *pName2,   /* Second part of the name of the table or view */
 81489   int isTemp,      /* True if this is a TEMP table */
 81490   int isView,      /* True if this is a VIEW */
 81491   int isVirtual,   /* True if this is a VIRTUAL table */
 81492   int noErr        /* Do nothing if table already exists */
 81493 ){
 81494   Table *pTable;
 81495   char *zName = 0; /* The name of the new table */
 81496   sqlite3 *db = pParse->db;
 81497   Vdbe *v;
 81498   int iDb;         /* Database number to create the table in */
 81499   Token *pName;    /* Unqualified name of the table to create */
 81501   /* The table or view name to create is passed to this routine via tokens
 81502   ** pName1 and pName2. If the table name was fully qualified, for example:
 81503   **
 81504   ** CREATE TABLE xxx.yyy (...);
 81505   ** 
 81506   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
 81507   ** the table name is not fully qualified, i.e.:
 81508   **
 81509   ** CREATE TABLE yyy(...);
 81510   **
 81511   ** Then pName1 is set to "yyy" and pName2 is "".
 81512   **
 81513   ** The call below sets the pName pointer to point at the token (pName1 or
 81514   ** pName2) that stores the unqualified table name. The variable iDb is
 81515   ** set to the index of the database that the table or view is to be
 81516   ** created in.
 81517   */
 81518   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 81519   if( iDb<0 ) return;
 81520   if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
 81521     /* If creating a temp table, the name may not be qualified. Unless 
 81522     ** the database name is "temp" anyway.  */
 81523     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
 81524     return;
 81526   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
 81528   pParse->sNameToken = *pName;
 81529   zName = sqlite3NameFromToken(db, pName);
 81530   if( zName==0 ) return;
 81531   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 81532     goto begin_table_error;
 81534   if( db->init.iDb==1 ) isTemp = 1;
 81535 #ifndef SQLITE_OMIT_AUTHORIZATION
 81536   assert( (isTemp & 1)==isTemp );
 81538     int code;
 81539     char *zDb = db->aDb[iDb].zName;
 81540     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
 81541       goto begin_table_error;
 81543     if( isView ){
 81544       if( !OMIT_TEMPDB && isTemp ){
 81545         code = SQLITE_CREATE_TEMP_VIEW;
 81546       }else{
 81547         code = SQLITE_CREATE_VIEW;
 81549     }else{
 81550       if( !OMIT_TEMPDB && isTemp ){
 81551         code = SQLITE_CREATE_TEMP_TABLE;
 81552       }else{
 81553         code = SQLITE_CREATE_TABLE;
 81556     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
 81557       goto begin_table_error;
 81560 #endif
 81562   /* Make sure the new table name does not collide with an existing
 81563   ** index or table name in the same database.  Issue an error message if
 81564   ** it does. The exception is if the statement being parsed was passed
 81565   ** to an sqlite3_declare_vtab() call. In that case only the column names
 81566   ** and types will be used, so there is no need to test for namespace
 81567   ** collisions.
 81568   */
 81569   if( !IN_DECLARE_VTAB ){
 81570     char *zDb = db->aDb[iDb].zName;
 81571     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 81572       goto begin_table_error;
 81574     pTable = sqlite3FindTable(db, zName, zDb);
 81575     if( pTable ){
 81576       if( !noErr ){
 81577         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
 81578       }else{
 81579         assert( !db->init.busy );
 81580         sqlite3CodeVerifySchema(pParse, iDb);
 81582       goto begin_table_error;
 81584     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
 81585       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
 81586       goto begin_table_error;
 81590   pTable = sqlite3DbMallocZero(db, sizeof(Table));
 81591   if( pTable==0 ){
 81592     db->mallocFailed = 1;
 81593     pParse->rc = SQLITE_NOMEM;
 81594     pParse->nErr++;
 81595     goto begin_table_error;
 81597   pTable->zName = zName;
 81598   pTable->iPKey = -1;
 81599   pTable->pSchema = db->aDb[iDb].pSchema;
 81600   pTable->nRef = 1;
 81601   pTable->nRowEst = 1000000;
 81602   assert( pParse->pNewTable==0 );
 81603   pParse->pNewTable = pTable;
 81605   /* If this is the magic sqlite_sequence table used by autoincrement,
 81606   ** then record a pointer to this table in the main database structure
 81607   ** so that INSERT can find the table easily.
 81608   */
 81609 #ifndef SQLITE_OMIT_AUTOINCREMENT
 81610   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
 81611     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 81612     pTable->pSchema->pSeqTab = pTable;
 81614 #endif
 81616   /* Begin generating the code that will insert the table record into
 81617   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
 81618   ** and allocate the record number for the table entry now.  Before any
 81619   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
 81620   ** indices to be created and the table record must come before the 
 81621   ** indices.  Hence, the record number for the table must be allocated
 81622   ** now.
 81623   */
 81624   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
 81625     int j1;
 81626     int fileFormat;
 81627     int reg1, reg2, reg3;
 81628     sqlite3BeginWriteOperation(pParse, 0, iDb);
 81630 #ifndef SQLITE_OMIT_VIRTUALTABLE
 81631     if( isVirtual ){
 81632       sqlite3VdbeAddOp0(v, OP_VBegin);
 81634 #endif
 81636     /* If the file format and encoding in the database have not been set, 
 81637     ** set them now.
 81638     */
 81639     reg1 = pParse->regRowid = ++pParse->nMem;
 81640     reg2 = pParse->regRoot = ++pParse->nMem;
 81641     reg3 = ++pParse->nMem;
 81642     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
 81643     sqlite3VdbeUsesBtree(v, iDb);
 81644     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
 81645     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
 81646                   1 : SQLITE_MAX_FILE_FORMAT;
 81647     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
 81648     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
 81649     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
 81650     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
 81651     sqlite3VdbeJumpHere(v, j1);
 81653     /* This just creates a place-holder record in the sqlite_master table.
 81654     ** The record created does not contain anything yet.  It will be replaced
 81655     ** by the real entry in code generated at sqlite3EndTable().
 81656     **
 81657     ** The rowid for the new entry is left in register pParse->regRowid.
 81658     ** The root page number of the new table is left in reg pParse->regRoot.
 81659     ** The rowid and root page number values are needed by the code that
 81660     ** sqlite3EndTable will generate.
 81661     */
 81662 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 81663     if( isView || isVirtual ){
 81664       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
 81665     }else
 81666 #endif
 81668       sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
 81670     sqlite3OpenMasterTable(pParse, iDb);
 81671     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
 81672     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
 81673     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
 81674     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 81675     sqlite3VdbeAddOp0(v, OP_Close);
 81678   /* Normal (non-error) return. */
 81679   return;
 81681   /* If an error occurs, we jump here */
 81682 begin_table_error:
 81683   sqlite3DbFree(db, zName);
 81684   return;
 81687 /*
 81688 ** This macro is used to compare two strings in a case-insensitive manner.
 81689 ** It is slightly faster than calling sqlite3StrICmp() directly, but
 81690 ** produces larger code.
 81691 **
 81692 ** WARNING: This macro is not compatible with the strcmp() family. It
 81693 ** returns true if the two strings are equal, otherwise false.
 81694 */
 81695 #define STRICMP(x, y) (\
 81696 sqlite3UpperToLower[*(unsigned char *)(x)]==   \
 81697 sqlite3UpperToLower[*(unsigned char *)(y)]     \
 81698 && sqlite3StrICmp((x)+1,(y)+1)==0 )
 81700 /*
 81701 ** Add a new column to the table currently being constructed.
 81702 **
 81703 ** The parser calls this routine once for each column declaration
 81704 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
 81705 ** first to get things going.  Then this routine is called for each
 81706 ** column.
 81707 */
 81708 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
 81709   Table *p;
 81710   int i;
 81711   char *z;
 81712   Column *pCol;
 81713   sqlite3 *db = pParse->db;
 81714   if( (p = pParse->pNewTable)==0 ) return;
 81715 #if SQLITE_MAX_COLUMN
 81716   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 81717     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
 81718     return;
 81720 #endif
 81721   z = sqlite3NameFromToken(db, pName);
 81722   if( z==0 ) return;
 81723   for(i=0; i<p->nCol; i++){
 81724     if( STRICMP(z, p->aCol[i].zName) ){
 81725       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
 81726       sqlite3DbFree(db, z);
 81727       return;
 81730   if( (p->nCol & 0x7)==0 ){
 81731     Column *aNew;
 81732     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
 81733     if( aNew==0 ){
 81734       sqlite3DbFree(db, z);
 81735       return;
 81737     p->aCol = aNew;
 81739   pCol = &p->aCol[p->nCol];
 81740   memset(pCol, 0, sizeof(p->aCol[0]));
 81741   pCol->zName = z;
 81743   /* If there is no type specified, columns have the default affinity
 81744   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
 81745   ** be called next to set pCol->affinity correctly.
 81746   */
 81747   pCol->affinity = SQLITE_AFF_NONE;
 81748   p->nCol++;
 81751 /*
 81752 ** This routine is called by the parser while in the middle of
 81753 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
 81754 ** been seen on a column.  This routine sets the notNull flag on
 81755 ** the column currently under construction.
 81756 */
 81757 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
 81758   Table *p;
 81759   p = pParse->pNewTable;
 81760   if( p==0 || NEVER(p->nCol<1) ) return;
 81761   p->aCol[p->nCol-1].notNull = (u8)onError;
 81764 /*
 81765 ** Scan the column type name zType (length nType) and return the
 81766 ** associated affinity type.
 81767 **
 81768 ** This routine does a case-independent search of zType for the 
 81769 ** substrings in the following table. If one of the substrings is
 81770 ** found, the corresponding affinity is returned. If zType contains
 81771 ** more than one of the substrings, entries toward the top of 
 81772 ** the table take priority. For example, if zType is 'BLOBINT', 
 81773 ** SQLITE_AFF_INTEGER is returned.
 81774 **
 81775 ** Substring     | Affinity
 81776 ** --------------------------------
 81777 ** 'INT'         | SQLITE_AFF_INTEGER
 81778 ** 'CHAR'        | SQLITE_AFF_TEXT
 81779 ** 'CLOB'        | SQLITE_AFF_TEXT
 81780 ** 'TEXT'        | SQLITE_AFF_TEXT
 81781 ** 'BLOB'        | SQLITE_AFF_NONE
 81782 ** 'REAL'        | SQLITE_AFF_REAL
 81783 ** 'FLOA'        | SQLITE_AFF_REAL
 81784 ** 'DOUB'        | SQLITE_AFF_REAL
 81785 **
 81786 ** If none of the substrings in the above table are found,
 81787 ** SQLITE_AFF_NUMERIC is returned.
 81788 */
 81789 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
 81790   u32 h = 0;
 81791   char aff = SQLITE_AFF_NUMERIC;
 81793   if( zIn ) while( zIn[0] ){
 81794     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
 81795     zIn++;
 81796     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
 81797       aff = SQLITE_AFF_TEXT; 
 81798     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
 81799       aff = SQLITE_AFF_TEXT;
 81800     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
 81801       aff = SQLITE_AFF_TEXT;
 81802     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
 81803         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
 81804       aff = SQLITE_AFF_NONE;
 81805 #ifndef SQLITE_OMIT_FLOATING_POINT
 81806     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
 81807         && aff==SQLITE_AFF_NUMERIC ){
 81808       aff = SQLITE_AFF_REAL;
 81809     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
 81810         && aff==SQLITE_AFF_NUMERIC ){
 81811       aff = SQLITE_AFF_REAL;
 81812     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
 81813         && aff==SQLITE_AFF_NUMERIC ){
 81814       aff = SQLITE_AFF_REAL;
 81815 #endif
 81816     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
 81817       aff = SQLITE_AFF_INTEGER;
 81818       break;
 81822   return aff;
 81825 /*
 81826 ** This routine is called by the parser while in the middle of
 81827 ** parsing a CREATE TABLE statement.  The pFirst token is the first
 81828 ** token in the sequence of tokens that describe the type of the
 81829 ** column currently under construction.   pLast is the last token
 81830 ** in the sequence.  Use this information to construct a string
 81831 ** that contains the typename of the column and store that string
 81832 ** in zType.
 81833 */ 
 81834 SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
 81835   Table *p;
 81836   Column *pCol;
 81838   p = pParse->pNewTable;
 81839   if( p==0 || NEVER(p->nCol<1) ) return;
 81840   pCol = &p->aCol[p->nCol-1];
 81841   assert( pCol->zType==0 );
 81842   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
 81843   pCol->affinity = sqlite3AffinityType(pCol->zType);
 81846 /*
 81847 ** The expression is the default value for the most recently added column
 81848 ** of the table currently under construction.
 81849 **
 81850 ** Default value expressions must be constant.  Raise an exception if this
 81851 ** is not the case.
 81852 **
 81853 ** This routine is called by the parser while in the middle of
 81854 ** parsing a CREATE TABLE statement.
 81855 */
 81856 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
 81857   Table *p;
 81858   Column *pCol;
 81859   sqlite3 *db = pParse->db;
 81860   p = pParse->pNewTable;
 81861   if( p!=0 ){
 81862     pCol = &(p->aCol[p->nCol-1]);
 81863     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
 81864       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
 81865           pCol->zName);
 81866     }else{
 81867       /* A copy of pExpr is used instead of the original, as pExpr contains
 81868       ** tokens that point to volatile memory. The 'span' of the expression
 81869       ** is required by pragma table_info.
 81870       */
 81871       sqlite3ExprDelete(db, pCol->pDflt);
 81872       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
 81873       sqlite3DbFree(db, pCol->zDflt);
 81874       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
 81875                                      (int)(pSpan->zEnd - pSpan->zStart));
 81878   sqlite3ExprDelete(db, pSpan->pExpr);
 81881 /*
 81882 ** Designate the PRIMARY KEY for the table.  pList is a list of names 
 81883 ** of columns that form the primary key.  If pList is NULL, then the
 81884 ** most recently added column of the table is the primary key.
 81885 **
 81886 ** A table can have at most one primary key.  If the table already has
 81887 ** a primary key (and this is the second primary key) then create an
 81888 ** error.
 81889 **
 81890 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
 81891 ** then we will try to use that column as the rowid.  Set the Table.iPKey
 81892 ** field of the table under construction to be the index of the
 81893 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
 81894 ** no INTEGER PRIMARY KEY.
 81895 **
 81896 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
 81897 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
 81898 */
 81899 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
 81900   Parse *pParse,    /* Parsing context */
 81901   ExprList *pList,  /* List of field names to be indexed */
 81902   int onError,      /* What to do with a uniqueness conflict */
 81903   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
 81904   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
 81905 ){
 81906   Table *pTab = pParse->pNewTable;
 81907   char *zType = 0;
 81908   int iCol = -1, i;
 81909   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
 81910   if( pTab->tabFlags & TF_HasPrimaryKey ){
 81911     sqlite3ErrorMsg(pParse, 
 81912       "table \"%s\" has more than one primary key", pTab->zName);
 81913     goto primary_key_exit;
 81915   pTab->tabFlags |= TF_HasPrimaryKey;
 81916   if( pList==0 ){
 81917     iCol = pTab->nCol - 1;
 81918     pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 81919   }else{
 81920     for(i=0; i<pList->nExpr; i++){
 81921       for(iCol=0; iCol<pTab->nCol; iCol++){
 81922         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
 81923           break;
 81926       if( iCol<pTab->nCol ){
 81927         pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 81930     if( pList->nExpr>1 ) iCol = -1;
 81932   if( iCol>=0 && iCol<pTab->nCol ){
 81933     zType = pTab->aCol[iCol].zType;
 81935   if( zType && sqlite3StrICmp(zType, "INTEGER")==0
 81936         && sortOrder==SQLITE_SO_ASC ){
 81937     pTab->iPKey = iCol;
 81938     pTab->keyConf = (u8)onError;
 81939     assert( autoInc==0 || autoInc==1 );
 81940     pTab->tabFlags |= autoInc*TF_Autoincrement;
 81941   }else if( autoInc ){
 81942 #ifndef SQLITE_OMIT_AUTOINCREMENT
 81943     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
 81944        "INTEGER PRIMARY KEY");
 81945 #endif
 81946   }else{
 81947     Index *p;
 81948     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
 81949     if( p ){
 81950       p->autoIndex = 2;
 81952     pList = 0;
 81955 primary_key_exit:
 81956   sqlite3ExprListDelete(pParse->db, pList);
 81957   return;
 81960 /*
 81961 ** Add a new CHECK constraint to the table currently under construction.
 81962 */
 81963 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
 81964   Parse *pParse,    /* Parsing context */
 81965   Expr *pCheckExpr  /* The check expression */
 81966 ){
 81967 #ifndef SQLITE_OMIT_CHECK
 81968   Table *pTab = pParse->pNewTable;
 81969   if( pTab && !IN_DECLARE_VTAB ){
 81970     pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
 81971     if( pParse->constraintName.n ){
 81972       sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
 81974   }else
 81975 #endif
 81977     sqlite3ExprDelete(pParse->db, pCheckExpr);
 81981 /*
 81982 ** Set the collation function of the most recently parsed table column
 81983 ** to the CollSeq given.
 81984 */
 81985 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
 81986   Table *p;
 81987   int i;
 81988   char *zColl;              /* Dequoted name of collation sequence */
 81989   sqlite3 *db;
 81991   if( (p = pParse->pNewTable)==0 ) return;
 81992   i = p->nCol-1;
 81993   db = pParse->db;
 81994   zColl = sqlite3NameFromToken(db, pToken);
 81995   if( !zColl ) return;
 81997   if( sqlite3LocateCollSeq(pParse, zColl) ){
 81998     Index *pIdx;
 81999     p->aCol[i].zColl = zColl;
 82001     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
 82002     ** then an index may have been created on this column before the
 82003     ** collation type was added. Correct this if it is the case.
 82004     */
 82005     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
 82006       assert( pIdx->nColumn==1 );
 82007       if( pIdx->aiColumn[0]==i ){
 82008         pIdx->azColl[0] = p->aCol[i].zColl;
 82011   }else{
 82012     sqlite3DbFree(db, zColl);
 82016 /*
 82017 ** This function returns the collation sequence for database native text
 82018 ** encoding identified by the string zName, length nName.
 82019 **
 82020 ** If the requested collation sequence is not available, or not available
 82021 ** in the database native encoding, the collation factory is invoked to
 82022 ** request it. If the collation factory does not supply such a sequence,
 82023 ** and the sequence is available in another text encoding, then that is
 82024 ** returned instead.
 82025 **
 82026 ** If no versions of the requested collations sequence are available, or
 82027 ** another error occurs, NULL is returned and an error message written into
 82028 ** pParse.
 82029 **
 82030 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
 82031 ** invokes the collation factory if the named collation cannot be found
 82032 ** and generates an error message.
 82033 **
 82034 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
 82035 */
 82036 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
 82037   sqlite3 *db = pParse->db;
 82038   u8 enc = ENC(db);
 82039   u8 initbusy = db->init.busy;
 82040   CollSeq *pColl;
 82042   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
 82043   if( !initbusy && (!pColl || !pColl->xCmp) ){
 82044     pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
 82047   return pColl;
 82051 /*
 82052 ** Generate code that will increment the schema cookie.
 82053 **
 82054 ** The schema cookie is used to determine when the schema for the
 82055 ** database changes.  After each schema change, the cookie value
 82056 ** changes.  When a process first reads the schema it records the
 82057 ** cookie.  Thereafter, whenever it goes to access the database,
 82058 ** it checks the cookie to make sure the schema has not changed
 82059 ** since it was last read.
 82060 **
 82061 ** This plan is not completely bullet-proof.  It is possible for
 82062 ** the schema to change multiple times and for the cookie to be
 82063 ** set back to prior value.  But schema changes are infrequent
 82064 ** and the probability of hitting the same cookie value is only
 82065 ** 1 chance in 2^32.  So we're safe enough.
 82066 */
 82067 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
 82068   int r1 = sqlite3GetTempReg(pParse);
 82069   sqlite3 *db = pParse->db;
 82070   Vdbe *v = pParse->pVdbe;
 82071   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 82072   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
 82073   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
 82074   sqlite3ReleaseTempReg(pParse, r1);
 82077 /*
 82078 ** Measure the number of characters needed to output the given
 82079 ** identifier.  The number returned includes any quotes used
 82080 ** but does not include the null terminator.
 82081 **
 82082 ** The estimate is conservative.  It might be larger that what is
 82083 ** really needed.
 82084 */
 82085 static int identLength(const char *z){
 82086   int n;
 82087   for(n=0; *z; n++, z++){
 82088     if( *z=='"' ){ n++; }
 82090   return n + 2;
 82093 /*
 82094 ** The first parameter is a pointer to an output buffer. The second 
 82095 ** parameter is a pointer to an integer that contains the offset at
 82096 ** which to write into the output buffer. This function copies the
 82097 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
 82098 ** to the specified offset in the buffer and updates *pIdx to refer
 82099 ** to the first byte after the last byte written before returning.
 82100 ** 
 82101 ** If the string zSignedIdent consists entirely of alpha-numeric
 82102 ** characters, does not begin with a digit and is not an SQL keyword,
 82103 ** then it is copied to the output buffer exactly as it is. Otherwise,
 82104 ** it is quoted using double-quotes.
 82105 */
 82106 static void identPut(char *z, int *pIdx, char *zSignedIdent){
 82107   unsigned char *zIdent = (unsigned char*)zSignedIdent;
 82108   int i, j, needQuote;
 82109   i = *pIdx;
 82111   for(j=0; zIdent[j]; j++){
 82112     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
 82114   needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
 82115   if( !needQuote ){
 82116     needQuote = zIdent[j];
 82119   if( needQuote ) z[i++] = '"';
 82120   for(j=0; zIdent[j]; j++){
 82121     z[i++] = zIdent[j];
 82122     if( zIdent[j]=='"' ) z[i++] = '"';
 82124   if( needQuote ) z[i++] = '"';
 82125   z[i] = 0;
 82126   *pIdx = i;
 82129 /*
 82130 ** Generate a CREATE TABLE statement appropriate for the given
 82131 ** table.  Memory to hold the text of the statement is obtained
 82132 ** from sqliteMalloc() and must be freed by the calling function.
 82133 */
 82134 static char *createTableStmt(sqlite3 *db, Table *p){
 82135   int i, k, n;
 82136   char *zStmt;
 82137   char *zSep, *zSep2, *zEnd;
 82138   Column *pCol;
 82139   n = 0;
 82140   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
 82141     n += identLength(pCol->zName) + 5;
 82143   n += identLength(p->zName);
 82144   if( n<50 ){ 
 82145     zSep = "";
 82146     zSep2 = ",";
 82147     zEnd = ")";
 82148   }else{
 82149     zSep = "\n  ";
 82150     zSep2 = ",\n  ";
 82151     zEnd = "\n)";
 82153   n += 35 + 6*p->nCol;
 82154   zStmt = sqlite3DbMallocRaw(0, n);
 82155   if( zStmt==0 ){
 82156     db->mallocFailed = 1;
 82157     return 0;
 82159   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
 82160   k = sqlite3Strlen30(zStmt);
 82161   identPut(zStmt, &k, p->zName);
 82162   zStmt[k++] = '(';
 82163   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
 82164     static const char * const azType[] = {
 82165         /* SQLITE_AFF_TEXT    */ " TEXT",
 82166         /* SQLITE_AFF_NONE    */ "",
 82167         /* SQLITE_AFF_NUMERIC */ " NUM",
 82168         /* SQLITE_AFF_INTEGER */ " INT",
 82169         /* SQLITE_AFF_REAL    */ " REAL"
 82170     };
 82171     int len;
 82172     const char *zType;
 82174     sqlite3_snprintf(n-k, &zStmt[k], zSep);
 82175     k += sqlite3Strlen30(&zStmt[k]);
 82176     zSep = zSep2;
 82177     identPut(zStmt, &k, pCol->zName);
 82178     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
 82179     assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
 82180     testcase( pCol->affinity==SQLITE_AFF_TEXT );
 82181     testcase( pCol->affinity==SQLITE_AFF_NONE );
 82182     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
 82183     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
 82184     testcase( pCol->affinity==SQLITE_AFF_REAL );
 82186     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
 82187     len = sqlite3Strlen30(zType);
 82188     assert( pCol->affinity==SQLITE_AFF_NONE 
 82189             || pCol->affinity==sqlite3AffinityType(zType) );
 82190     memcpy(&zStmt[k], zType, len);
 82191     k += len;
 82192     assert( k<=n );
 82194   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
 82195   return zStmt;
 82198 /*
 82199 ** This routine is called to report the final ")" that terminates
 82200 ** a CREATE TABLE statement.
 82201 **
 82202 ** The table structure that other action routines have been building
 82203 ** is added to the internal hash tables, assuming no errors have
 82204 ** occurred.
 82205 **
 82206 ** An entry for the table is made in the master table on disk, unless
 82207 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
 82208 ** it means we are reading the sqlite_master table because we just
 82209 ** connected to the database or because the sqlite_master table has
 82210 ** recently changed, so the entry for this table already exists in
 82211 ** the sqlite_master table.  We do not want to create it again.
 82212 **
 82213 ** If the pSelect argument is not NULL, it means that this routine
 82214 ** was called to create a table generated from a 
 82215 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
 82216 ** the new table will match the result set of the SELECT.
 82217 */
 82218 SQLITE_PRIVATE void sqlite3EndTable(
 82219   Parse *pParse,          /* Parse context */
 82220   Token *pCons,           /* The ',' token after the last column defn. */
 82221   Token *pEnd,            /* The final ')' token in the CREATE TABLE */
 82222   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
 82223 ){
 82224   Table *p;
 82225   sqlite3 *db = pParse->db;
 82226   int iDb;
 82228   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
 82229     return;
 82231   p = pParse->pNewTable;
 82232   if( p==0 ) return;
 82234   assert( !db->init.busy || !pSelect );
 82236   iDb = sqlite3SchemaToIndex(db, p->pSchema);
 82238 #ifndef SQLITE_OMIT_CHECK
 82239   /* Resolve names in all CHECK constraint expressions.
 82240   */
 82241   if( p->pCheck ){
 82242     SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
 82243     NameContext sNC;                /* Name context for pParse->pNewTable */
 82244     ExprList *pList;                /* List of all CHECK constraints */
 82245     int i;                          /* Loop counter */
 82247     memset(&sNC, 0, sizeof(sNC));
 82248     memset(&sSrc, 0, sizeof(sSrc));
 82249     sSrc.nSrc = 1;
 82250     sSrc.a[0].zName = p->zName;
 82251     sSrc.a[0].pTab = p;
 82252     sSrc.a[0].iCursor = -1;
 82253     sNC.pParse = pParse;
 82254     sNC.pSrcList = &sSrc;
 82255     sNC.ncFlags = NC_IsCheck;
 82256     pList = p->pCheck;
 82257     for(i=0; i<pList->nExpr; i++){
 82258       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
 82259         return;
 82263 #endif /* !defined(SQLITE_OMIT_CHECK) */
 82265   /* If the db->init.busy is 1 it means we are reading the SQL off the
 82266   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
 82267   ** So do not write to the disk again.  Extract the root page number
 82268   ** for the table from the db->init.newTnum field.  (The page number
 82269   ** should have been put there by the sqliteOpenCb routine.)
 82270   */
 82271   if( db->init.busy ){
 82272     p->tnum = db->init.newTnum;
 82275   /* If not initializing, then create a record for the new table
 82276   ** in the SQLITE_MASTER table of the database.
 82277   **
 82278   ** If this is a TEMPORARY table, write the entry into the auxiliary
 82279   ** file instead of into the main database file.
 82280   */
 82281   if( !db->init.busy ){
 82282     int n;
 82283     Vdbe *v;
 82284     char *zType;    /* "view" or "table" */
 82285     char *zType2;   /* "VIEW" or "TABLE" */
 82286     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
 82288     v = sqlite3GetVdbe(pParse);
 82289     if( NEVER(v==0) ) return;
 82291     sqlite3VdbeAddOp1(v, OP_Close, 0);
 82293     /* 
 82294     ** Initialize zType for the new view or table.
 82295     */
 82296     if( p->pSelect==0 ){
 82297       /* A regular table */
 82298       zType = "table";
 82299       zType2 = "TABLE";
 82300 #ifndef SQLITE_OMIT_VIEW
 82301     }else{
 82302       /* A view */
 82303       zType = "view";
 82304       zType2 = "VIEW";
 82305 #endif
 82308     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
 82309     ** statement to populate the new table. The root-page number for the
 82310     ** new table is in register pParse->regRoot.
 82311     **
 82312     ** Once the SELECT has been coded by sqlite3Select(), it is in a
 82313     ** suitable state to query for the column names and types to be used
 82314     ** by the new table.
 82315     **
 82316     ** A shared-cache write-lock is not required to write to the new table,
 82317     ** as a schema-lock must have already been obtained to create it. Since
 82318     ** a schema-lock excludes all other database users, the write-lock would
 82319     ** be redundant.
 82320     */
 82321     if( pSelect ){
 82322       SelectDest dest;
 82323       Table *pSelTab;
 82325       assert(pParse->nTab==1);
 82326       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
 82327       sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
 82328       pParse->nTab = 2;
 82329       sqlite3SelectDestInit(&dest, SRT_Table, 1);
 82330       sqlite3Select(pParse, pSelect, &dest);
 82331       sqlite3VdbeAddOp1(v, OP_Close, 1);
 82332       if( pParse->nErr==0 ){
 82333         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
 82334         if( pSelTab==0 ) return;
 82335         assert( p->aCol==0 );
 82336         p->nCol = pSelTab->nCol;
 82337         p->aCol = pSelTab->aCol;
 82338         pSelTab->nCol = 0;
 82339         pSelTab->aCol = 0;
 82340         sqlite3DeleteTable(db, pSelTab);
 82344     /* Compute the complete text of the CREATE statement */
 82345     if( pSelect ){
 82346       zStmt = createTableStmt(db, p);
 82347     }else{
 82348       n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
 82349       zStmt = sqlite3MPrintf(db, 
 82350           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
 82351       );
 82354     /* A slot for the record has already been allocated in the 
 82355     ** SQLITE_MASTER table.  We just need to update that slot with all
 82356     ** the information we've collected.
 82357     */
 82358     sqlite3NestedParse(pParse,
 82359       "UPDATE %Q.%s "
 82360          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
 82361        "WHERE rowid=#%d",
 82362       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 82363       zType,
 82364       p->zName,
 82365       p->zName,
 82366       pParse->regRoot,
 82367       zStmt,
 82368       pParse->regRowid
 82369     );
 82370     sqlite3DbFree(db, zStmt);
 82371     sqlite3ChangeCookie(pParse, iDb);
 82373 #ifndef SQLITE_OMIT_AUTOINCREMENT
 82374     /* Check to see if we need to create an sqlite_sequence table for
 82375     ** keeping track of autoincrement keys.
 82376     */
 82377     if( p->tabFlags & TF_Autoincrement ){
 82378       Db *pDb = &db->aDb[iDb];
 82379       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 82380       if( pDb->pSchema->pSeqTab==0 ){
 82381         sqlite3NestedParse(pParse,
 82382           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
 82383           pDb->zName
 82384         );
 82387 #endif
 82389     /* Reparse everything to update our internal data structures */
 82390     sqlite3VdbeAddParseSchemaOp(v, iDb,
 82391                sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
 82395   /* Add the table to the in-memory representation of the database.
 82396   */
 82397   if( db->init.busy ){
 82398     Table *pOld;
 82399     Schema *pSchema = p->pSchema;
 82400     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 82401     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
 82402                              sqlite3Strlen30(p->zName),p);
 82403     if( pOld ){
 82404       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
 82405       db->mallocFailed = 1;
 82406       return;
 82408     pParse->pNewTable = 0;
 82409     db->flags |= SQLITE_InternChanges;
 82411 #ifndef SQLITE_OMIT_ALTERTABLE
 82412     if( !p->pSelect ){
 82413       const char *zName = (const char *)pParse->sNameToken.z;
 82414       int nName;
 82415       assert( !pSelect && pCons && pEnd );
 82416       if( pCons->z==0 ){
 82417         pCons = pEnd;
 82419       nName = (int)((const char *)pCons->z - zName);
 82420       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
 82422 #endif
 82426 #ifndef SQLITE_OMIT_VIEW
 82427 /*
 82428 ** The parser calls this routine in order to create a new VIEW
 82429 */
 82430 SQLITE_PRIVATE void sqlite3CreateView(
 82431   Parse *pParse,     /* The parsing context */
 82432   Token *pBegin,     /* The CREATE token that begins the statement */
 82433   Token *pName1,     /* The token that holds the name of the view */
 82434   Token *pName2,     /* The token that holds the name of the view */
 82435   Select *pSelect,   /* A SELECT statement that will become the new view */
 82436   int isTemp,        /* TRUE for a TEMPORARY view */
 82437   int noErr          /* Suppress error messages if VIEW already exists */
 82438 ){
 82439   Table *p;
 82440   int n;
 82441   const char *z;
 82442   Token sEnd;
 82443   DbFixer sFix;
 82444   Token *pName = 0;
 82445   int iDb;
 82446   sqlite3 *db = pParse->db;
 82448   if( pParse->nVar>0 ){
 82449     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
 82450     sqlite3SelectDelete(db, pSelect);
 82451     return;
 82453   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
 82454   p = pParse->pNewTable;
 82455   if( p==0 || pParse->nErr ){
 82456     sqlite3SelectDelete(db, pSelect);
 82457     return;
 82459   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 82460   iDb = sqlite3SchemaToIndex(db, p->pSchema);
 82461   if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
 82462     && sqlite3FixSelect(&sFix, pSelect)
 82463   ){
 82464     sqlite3SelectDelete(db, pSelect);
 82465     return;
 82468   /* Make a copy of the entire SELECT statement that defines the view.
 82469   ** This will force all the Expr.token.z values to be dynamically
 82470   ** allocated rather than point to the input string - which means that
 82471   ** they will persist after the current sqlite3_exec() call returns.
 82472   */
 82473   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 82474   sqlite3SelectDelete(db, pSelect);
 82475   if( db->mallocFailed ){
 82476     return;
 82478   if( !db->init.busy ){
 82479     sqlite3ViewGetColumnNames(pParse, p);
 82482   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
 82483   ** the end.
 82484   */
 82485   sEnd = pParse->sLastToken;
 82486   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
 82487     sEnd.z += sEnd.n;
 82489   sEnd.n = 0;
 82490   n = (int)(sEnd.z - pBegin->z);
 82491   z = pBegin->z;
 82492   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
 82493   sEnd.z = &z[n-1];
 82494   sEnd.n = 1;
 82496   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
 82497   sqlite3EndTable(pParse, 0, &sEnd, 0);
 82498   return;
 82500 #endif /* SQLITE_OMIT_VIEW */
 82502 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 82503 /*
 82504 ** The Table structure pTable is really a VIEW.  Fill in the names of
 82505 ** the columns of the view in the pTable structure.  Return the number
 82506 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
 82507 */
 82508 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
 82509   Table *pSelTab;   /* A fake table from which we get the result set */
 82510   Select *pSel;     /* Copy of the SELECT that implements the view */
 82511   int nErr = 0;     /* Number of errors encountered */
 82512   int n;            /* Temporarily holds the number of cursors assigned */
 82513   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
 82514   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
 82516   assert( pTable );
 82518 #ifndef SQLITE_OMIT_VIRTUALTABLE
 82519   if( sqlite3VtabCallConnect(pParse, pTable) ){
 82520     return SQLITE_ERROR;
 82522   if( IsVirtual(pTable) ) return 0;
 82523 #endif
 82525 #ifndef SQLITE_OMIT_VIEW
 82526   /* A positive nCol means the columns names for this view are
 82527   ** already known.
 82528   */
 82529   if( pTable->nCol>0 ) return 0;
 82531   /* A negative nCol is a special marker meaning that we are currently
 82532   ** trying to compute the column names.  If we enter this routine with
 82533   ** a negative nCol, it means two or more views form a loop, like this:
 82534   **
 82535   **     CREATE VIEW one AS SELECT * FROM two;
 82536   **     CREATE VIEW two AS SELECT * FROM one;
 82537   **
 82538   ** Actually, the error above is now caught prior to reaching this point.
 82539   ** But the following test is still important as it does come up
 82540   ** in the following:
 82541   ** 
 82542   **     CREATE TABLE main.ex1(a);
 82543   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
 82544   **     SELECT * FROM temp.ex1;
 82545   */
 82546   if( pTable->nCol<0 ){
 82547     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
 82548     return 1;
 82550   assert( pTable->nCol>=0 );
 82552   /* If we get this far, it means we need to compute the table names.
 82553   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
 82554   ** "*" elements in the results set of the view and will assign cursors
 82555   ** to the elements of the FROM clause.  But we do not want these changes
 82556   ** to be permanent.  So the computation is done on a copy of the SELECT
 82557   ** statement that defines the view.
 82558   */
 82559   assert( pTable->pSelect );
 82560   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
 82561   if( pSel ){
 82562     u8 enableLookaside = db->lookaside.bEnabled;
 82563     n = pParse->nTab;
 82564     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
 82565     pTable->nCol = -1;
 82566     db->lookaside.bEnabled = 0;
 82567 #ifndef SQLITE_OMIT_AUTHORIZATION
 82568     xAuth = db->xAuth;
 82569     db->xAuth = 0;
 82570     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
 82571     db->xAuth = xAuth;
 82572 #else
 82573     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
 82574 #endif
 82575     db->lookaside.bEnabled = enableLookaside;
 82576     pParse->nTab = n;
 82577     if( pSelTab ){
 82578       assert( pTable->aCol==0 );
 82579       pTable->nCol = pSelTab->nCol;
 82580       pTable->aCol = pSelTab->aCol;
 82581       pSelTab->nCol = 0;
 82582       pSelTab->aCol = 0;
 82583       sqlite3DeleteTable(db, pSelTab);
 82584       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
 82585       pTable->pSchema->flags |= DB_UnresetViews;
 82586     }else{
 82587       pTable->nCol = 0;
 82588       nErr++;
 82590     sqlite3SelectDelete(db, pSel);
 82591   } else {
 82592     nErr++;
 82594 #endif /* SQLITE_OMIT_VIEW */
 82595   return nErr;  
 82597 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
 82599 #ifndef SQLITE_OMIT_VIEW
 82600 /*
 82601 ** Clear the column names from every VIEW in database idx.
 82602 */
 82603 static void sqliteViewResetAll(sqlite3 *db, int idx){
 82604   HashElem *i;
 82605   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
 82606   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
 82607   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
 82608     Table *pTab = sqliteHashData(i);
 82609     if( pTab->pSelect ){
 82610       sqliteDeleteColumnNames(db, pTab);
 82611       pTab->aCol = 0;
 82612       pTab->nCol = 0;
 82615   DbClearProperty(db, idx, DB_UnresetViews);
 82617 #else
 82618 # define sqliteViewResetAll(A,B)
 82619 #endif /* SQLITE_OMIT_VIEW */
 82621 /*
 82622 ** This function is called by the VDBE to adjust the internal schema
 82623 ** used by SQLite when the btree layer moves a table root page. The
 82624 ** root-page of a table or index in database iDb has changed from iFrom
 82625 ** to iTo.
 82626 **
 82627 ** Ticket #1728:  The symbol table might still contain information
 82628 ** on tables and/or indices that are the process of being deleted.
 82629 ** If you are unlucky, one of those deleted indices or tables might
 82630 ** have the same rootpage number as the real table or index that is
 82631 ** being moved.  So we cannot stop searching after the first match 
 82632 ** because the first match might be for one of the deleted indices
 82633 ** or tables and not the table/index that is actually being moved.
 82634 ** We must continue looping until all tables and indices with
 82635 ** rootpage==iFrom have been converted to have a rootpage of iTo
 82636 ** in order to be certain that we got the right one.
 82637 */
 82638 #ifndef SQLITE_OMIT_AUTOVACUUM
 82639 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
 82640   HashElem *pElem;
 82641   Hash *pHash;
 82642   Db *pDb;
 82644   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 82645   pDb = &db->aDb[iDb];
 82646   pHash = &pDb->pSchema->tblHash;
 82647   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
 82648     Table *pTab = sqliteHashData(pElem);
 82649     if( pTab->tnum==iFrom ){
 82650       pTab->tnum = iTo;
 82653   pHash = &pDb->pSchema->idxHash;
 82654   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
 82655     Index *pIdx = sqliteHashData(pElem);
 82656     if( pIdx->tnum==iFrom ){
 82657       pIdx->tnum = iTo;
 82661 #endif
 82663 /*
 82664 ** Write code to erase the table with root-page iTable from database iDb.
 82665 ** Also write code to modify the sqlite_master table and internal schema
 82666 ** if a root-page of another table is moved by the btree-layer whilst
 82667 ** erasing iTable (this can happen with an auto-vacuum database).
 82668 */ 
 82669 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
 82670   Vdbe *v = sqlite3GetVdbe(pParse);
 82671   int r1 = sqlite3GetTempReg(pParse);
 82672   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
 82673   sqlite3MayAbort(pParse);
 82674 #ifndef SQLITE_OMIT_AUTOVACUUM
 82675   /* OP_Destroy stores an in integer r1. If this integer
 82676   ** is non-zero, then it is the root page number of a table moved to
 82677   ** location iTable. The following code modifies the sqlite_master table to
 82678   ** reflect this.
 82679   **
 82680   ** The "#NNN" in the SQL is a special constant that means whatever value
 82681   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
 82682   ** token for additional information.
 82683   */
 82684   sqlite3NestedParse(pParse, 
 82685      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
 82686      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
 82687 #endif
 82688   sqlite3ReleaseTempReg(pParse, r1);
 82691 /*
 82692 ** Write VDBE code to erase table pTab and all associated indices on disk.
 82693 ** Code to update the sqlite_master tables and internal schema definitions
 82694 ** in case a root-page belonging to another table is moved by the btree layer
 82695 ** is also added (this can happen with an auto-vacuum database).
 82696 */
 82697 static void destroyTable(Parse *pParse, Table *pTab){
 82698 #ifdef SQLITE_OMIT_AUTOVACUUM
 82699   Index *pIdx;
 82700   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 82701   destroyRootPage(pParse, pTab->tnum, iDb);
 82702   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 82703     destroyRootPage(pParse, pIdx->tnum, iDb);
 82705 #else
 82706   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
 82707   ** is not defined), then it is important to call OP_Destroy on the
 82708   ** table and index root-pages in order, starting with the numerically 
 82709   ** largest root-page number. This guarantees that none of the root-pages
 82710   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
 82711   ** following were coded:
 82712   **
 82713   ** OP_Destroy 4 0
 82714   ** ...
 82715   ** OP_Destroy 5 0
 82716   **
 82717   ** and root page 5 happened to be the largest root-page number in the
 82718   ** database, then root page 5 would be moved to page 4 by the 
 82719   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
 82720   ** a free-list page.
 82721   */
 82722   int iTab = pTab->tnum;
 82723   int iDestroyed = 0;
 82725   while( 1 ){
 82726     Index *pIdx;
 82727     int iLargest = 0;
 82729     if( iDestroyed==0 || iTab<iDestroyed ){
 82730       iLargest = iTab;
 82732     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 82733       int iIdx = pIdx->tnum;
 82734       assert( pIdx->pSchema==pTab->pSchema );
 82735       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
 82736         iLargest = iIdx;
 82739     if( iLargest==0 ){
 82740       return;
 82741     }else{
 82742       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 82743       assert( iDb>=0 && iDb<pParse->db->nDb );
 82744       destroyRootPage(pParse, iLargest, iDb);
 82745       iDestroyed = iLargest;
 82748 #endif
 82751 /*
 82752 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
 82753 ** after a DROP INDEX or DROP TABLE command.
 82754 */
 82755 static void sqlite3ClearStatTables(
 82756   Parse *pParse,         /* The parsing context */
 82757   int iDb,               /* The database number */
 82758   const char *zType,     /* "idx" or "tbl" */
 82759   const char *zName      /* Name of index or table */
 82760 ){
 82761   int i;
 82762   const char *zDbName = pParse->db->aDb[iDb].zName;
 82763   for(i=1; i<=3; i++){
 82764     char zTab[24];
 82765     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
 82766     if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
 82767       sqlite3NestedParse(pParse,
 82768         "DELETE FROM %Q.%s WHERE %s=%Q",
 82769         zDbName, zTab, zType, zName
 82770       );
 82775 /*
 82776 ** Generate code to drop a table.
 82777 */
 82778 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
 82779   Vdbe *v;
 82780   sqlite3 *db = pParse->db;
 82781   Trigger *pTrigger;
 82782   Db *pDb = &db->aDb[iDb];
 82784   v = sqlite3GetVdbe(pParse);
 82785   assert( v!=0 );
 82786   sqlite3BeginWriteOperation(pParse, 1, iDb);
 82788 #ifndef SQLITE_OMIT_VIRTUALTABLE
 82789   if( IsVirtual(pTab) ){
 82790     sqlite3VdbeAddOp0(v, OP_VBegin);
 82792 #endif
 82794   /* Drop all triggers associated with the table being dropped. Code
 82795   ** is generated to remove entries from sqlite_master and/or
 82796   ** sqlite_temp_master if required.
 82797   */
 82798   pTrigger = sqlite3TriggerList(pParse, pTab);
 82799   while( pTrigger ){
 82800     assert( pTrigger->pSchema==pTab->pSchema || 
 82801         pTrigger->pSchema==db->aDb[1].pSchema );
 82802     sqlite3DropTriggerPtr(pParse, pTrigger);
 82803     pTrigger = pTrigger->pNext;
 82806 #ifndef SQLITE_OMIT_AUTOINCREMENT
 82807   /* Remove any entries of the sqlite_sequence table associated with
 82808   ** the table being dropped. This is done before the table is dropped
 82809   ** at the btree level, in case the sqlite_sequence table needs to
 82810   ** move as a result of the drop (can happen in auto-vacuum mode).
 82811   */
 82812   if( pTab->tabFlags & TF_Autoincrement ){
 82813     sqlite3NestedParse(pParse,
 82814       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
 82815       pDb->zName, pTab->zName
 82816     );
 82818 #endif
 82820   /* Drop all SQLITE_MASTER table and index entries that refer to the
 82821   ** table. The program name loops through the master table and deletes
 82822   ** every row that refers to a table of the same name as the one being
 82823   ** dropped. Triggers are handled seperately because a trigger can be
 82824   ** created in the temp database that refers to a table in another
 82825   ** database.
 82826   */
 82827   sqlite3NestedParse(pParse, 
 82828       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
 82829       pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
 82830   if( !isView && !IsVirtual(pTab) ){
 82831     destroyTable(pParse, pTab);
 82834   /* Remove the table entry from SQLite's internal schema and modify
 82835   ** the schema cookie.
 82836   */
 82837   if( IsVirtual(pTab) ){
 82838     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
 82840   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 82841   sqlite3ChangeCookie(pParse, iDb);
 82842   sqliteViewResetAll(db, iDb);
 82845 /*
 82846 ** This routine is called to do the work of a DROP TABLE statement.
 82847 ** pName is the name of the table to be dropped.
 82848 */
 82849 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
 82850   Table *pTab;
 82851   Vdbe *v;
 82852   sqlite3 *db = pParse->db;
 82853   int iDb;
 82855   if( db->mallocFailed ){
 82856     goto exit_drop_table;
 82858   assert( pParse->nErr==0 );
 82859   assert( pName->nSrc==1 );
 82860   if( noErr ) db->suppressErr++;
 82861   pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
 82862   if( noErr ) db->suppressErr--;
 82864   if( pTab==0 ){
 82865     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 82866     goto exit_drop_table;
 82868   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 82869   assert( iDb>=0 && iDb<db->nDb );
 82871   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
 82872   ** it is initialized.
 82873   */
 82874   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
 82875     goto exit_drop_table;
 82877 #ifndef SQLITE_OMIT_AUTHORIZATION
 82879     int code;
 82880     const char *zTab = SCHEMA_TABLE(iDb);
 82881     const char *zDb = db->aDb[iDb].zName;
 82882     const char *zArg2 = 0;
 82883     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
 82884       goto exit_drop_table;
 82886     if( isView ){
 82887       if( !OMIT_TEMPDB && iDb==1 ){
 82888         code = SQLITE_DROP_TEMP_VIEW;
 82889       }else{
 82890         code = SQLITE_DROP_VIEW;
 82892 #ifndef SQLITE_OMIT_VIRTUALTABLE
 82893     }else if( IsVirtual(pTab) ){
 82894       code = SQLITE_DROP_VTABLE;
 82895       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
 82896 #endif
 82897     }else{
 82898       if( !OMIT_TEMPDB && iDb==1 ){
 82899         code = SQLITE_DROP_TEMP_TABLE;
 82900       }else{
 82901         code = SQLITE_DROP_TABLE;
 82904     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
 82905       goto exit_drop_table;
 82907     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
 82908       goto exit_drop_table;
 82911 #endif
 82912   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
 82913     && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
 82914     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
 82915     goto exit_drop_table;
 82918 #ifndef SQLITE_OMIT_VIEW
 82919   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
 82920   ** on a table.
 82921   */
 82922   if( isView && pTab->pSelect==0 ){
 82923     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
 82924     goto exit_drop_table;
 82926   if( !isView && pTab->pSelect ){
 82927     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
 82928     goto exit_drop_table;
 82930 #endif
 82932   /* Generate code to remove the table from the master table
 82933   ** on disk.
 82934   */
 82935   v = sqlite3GetVdbe(pParse);
 82936   if( v ){
 82937     sqlite3BeginWriteOperation(pParse, 1, iDb);
 82938     sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 82939     sqlite3FkDropTable(pParse, pName, pTab);
 82940     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
 82943 exit_drop_table:
 82944   sqlite3SrcListDelete(db, pName);
 82947 /*
 82948 ** This routine is called to create a new foreign key on the table
 82949 ** currently under construction.  pFromCol determines which columns
 82950 ** in the current table point to the foreign key.  If pFromCol==0 then
 82951 ** connect the key to the last column inserted.  pTo is the name of
 82952 ** the table referred to.  pToCol is a list of tables in the other
 82953 ** pTo table that the foreign key points to.  flags contains all
 82954 ** information about the conflict resolution algorithms specified
 82955 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
 82956 **
 82957 ** An FKey structure is created and added to the table currently
 82958 ** under construction in the pParse->pNewTable field.
 82959 **
 82960 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
 82961 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
 82962 */
 82963 SQLITE_PRIVATE void sqlite3CreateForeignKey(
 82964   Parse *pParse,       /* Parsing context */
 82965   ExprList *pFromCol,  /* Columns in this table that point to other table */
 82966   Token *pTo,          /* Name of the other table */
 82967   ExprList *pToCol,    /* Columns in the other table */
 82968   int flags            /* Conflict resolution algorithms. */
 82969 ){
 82970   sqlite3 *db = pParse->db;
 82971 #ifndef SQLITE_OMIT_FOREIGN_KEY
 82972   FKey *pFKey = 0;
 82973   FKey *pNextTo;
 82974   Table *p = pParse->pNewTable;
 82975   int nByte;
 82976   int i;
 82977   int nCol;
 82978   char *z;
 82980   assert( pTo!=0 );
 82981   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
 82982   if( pFromCol==0 ){
 82983     int iCol = p->nCol-1;
 82984     if( NEVER(iCol<0) ) goto fk_end;
 82985     if( pToCol && pToCol->nExpr!=1 ){
 82986       sqlite3ErrorMsg(pParse, "foreign key on %s"
 82987          " should reference only one column of table %T",
 82988          p->aCol[iCol].zName, pTo);
 82989       goto fk_end;
 82991     nCol = 1;
 82992   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
 82993     sqlite3ErrorMsg(pParse,
 82994         "number of columns in foreign key does not match the number of "
 82995         "columns in the referenced table");
 82996     goto fk_end;
 82997   }else{
 82998     nCol = pFromCol->nExpr;
 83000   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
 83001   if( pToCol ){
 83002     for(i=0; i<pToCol->nExpr; i++){
 83003       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
 83006   pFKey = sqlite3DbMallocZero(db, nByte );
 83007   if( pFKey==0 ){
 83008     goto fk_end;
 83010   pFKey->pFrom = p;
 83011   pFKey->pNextFrom = p->pFKey;
 83012   z = (char*)&pFKey->aCol[nCol];
 83013   pFKey->zTo = z;
 83014   memcpy(z, pTo->z, pTo->n);
 83015   z[pTo->n] = 0;
 83016   sqlite3Dequote(z);
 83017   z += pTo->n+1;
 83018   pFKey->nCol = nCol;
 83019   if( pFromCol==0 ){
 83020     pFKey->aCol[0].iFrom = p->nCol-1;
 83021   }else{
 83022     for(i=0; i<nCol; i++){
 83023       int j;
 83024       for(j=0; j<p->nCol; j++){
 83025         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
 83026           pFKey->aCol[i].iFrom = j;
 83027           break;
 83030       if( j>=p->nCol ){
 83031         sqlite3ErrorMsg(pParse, 
 83032           "unknown column \"%s\" in foreign key definition", 
 83033           pFromCol->a[i].zName);
 83034         goto fk_end;
 83038   if( pToCol ){
 83039     for(i=0; i<nCol; i++){
 83040       int n = sqlite3Strlen30(pToCol->a[i].zName);
 83041       pFKey->aCol[i].zCol = z;
 83042       memcpy(z, pToCol->a[i].zName, n);
 83043       z[n] = 0;
 83044       z += n+1;
 83047   pFKey->isDeferred = 0;
 83048   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
 83049   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
 83051   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
 83052   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
 83053       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
 83054   );
 83055   if( pNextTo==pFKey ){
 83056     db->mallocFailed = 1;
 83057     goto fk_end;
 83059   if( pNextTo ){
 83060     assert( pNextTo->pPrevTo==0 );
 83061     pFKey->pNextTo = pNextTo;
 83062     pNextTo->pPrevTo = pFKey;
 83065   /* Link the foreign key to the table as the last step.
 83066   */
 83067   p->pFKey = pFKey;
 83068   pFKey = 0;
 83070 fk_end:
 83071   sqlite3DbFree(db, pFKey);
 83072 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 83073   sqlite3ExprListDelete(db, pFromCol);
 83074   sqlite3ExprListDelete(db, pToCol);
 83077 /*
 83078 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
 83079 ** clause is seen as part of a foreign key definition.  The isDeferred
 83080 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
 83081 ** The behavior of the most recently created foreign key is adjusted
 83082 ** accordingly.
 83083 */
 83084 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
 83085 #ifndef SQLITE_OMIT_FOREIGN_KEY
 83086   Table *pTab;
 83087   FKey *pFKey;
 83088   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
 83089   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
 83090   pFKey->isDeferred = (u8)isDeferred;
 83091 #endif
 83094 /*
 83095 ** Generate code that will erase and refill index *pIdx.  This is
 83096 ** used to initialize a newly created index or to recompute the
 83097 ** content of an index in response to a REINDEX command.
 83098 **
 83099 ** if memRootPage is not negative, it means that the index is newly
 83100 ** created.  The register specified by memRootPage contains the
 83101 ** root page number of the index.  If memRootPage is negative, then
 83102 ** the index already exists and must be cleared before being refilled and
 83103 ** the root page number of the index is taken from pIndex->tnum.
 83104 */
 83105 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
 83106   Table *pTab = pIndex->pTable;  /* The table that is indexed */
 83107   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
 83108   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
 83109   int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
 83110   int addr1;                     /* Address of top of loop */
 83111   int addr2;                     /* Address to jump to for next iteration */
 83112   int tnum;                      /* Root page of index */
 83113   Vdbe *v;                       /* Generate code into this virtual machine */
 83114   KeyInfo *pKey;                 /* KeyInfo for index */
 83115 #ifdef SQLITE_OMIT_MERGE_SORT
 83116   int regIdxKey;                 /* Registers containing the index key */
 83117 #endif
 83118   int regRecord;                 /* Register holding assemblied index record */
 83119   sqlite3 *db = pParse->db;      /* The database connection */
 83120   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 83122 #ifndef SQLITE_OMIT_AUTHORIZATION
 83123   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
 83124       db->aDb[iDb].zName ) ){
 83125     return;
 83127 #endif
 83129   /* Require a write-lock on the table to perform this operation */
 83130   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
 83132   v = sqlite3GetVdbe(pParse);
 83133   if( v==0 ) return;
 83134   if( memRootPage>=0 ){
 83135     tnum = memRootPage;
 83136   }else{
 83137     tnum = pIndex->tnum;
 83138     sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
 83140   pKey = sqlite3IndexKeyinfo(pParse, pIndex);
 83141   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 83142                     (char *)pKey, P4_KEYINFO_HANDOFF);
 83143   sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
 83145 #ifndef SQLITE_OMIT_MERGE_SORT
 83146   /* Open the sorter cursor if we are to use one. */
 83147   iSorter = pParse->nTab++;
 83148   sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
 83149 #else
 83150   iSorter = iTab;
 83151 #endif
 83153   /* Open the table. Loop through all rows of the table, inserting index
 83154   ** records into the sorter. */
 83155   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 83156   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
 83157   regRecord = sqlite3GetTempReg(pParse);
 83159 #ifndef SQLITE_OMIT_MERGE_SORT
 83160   sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 83161   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
 83162   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
 83163   sqlite3VdbeJumpHere(v, addr1);
 83164   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
 83165   if( pIndex->onError!=OE_None ){
 83166     int j2 = sqlite3VdbeCurrentAddr(v) + 3;
 83167     sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
 83168     addr2 = sqlite3VdbeCurrentAddr(v);
 83169     sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
 83170     sqlite3HaltConstraint(
 83171         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
 83172     );
 83173   }else{
 83174     addr2 = sqlite3VdbeCurrentAddr(v);
 83176   sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
 83177   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
 83178   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 83179 #else
 83180   regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 83181   addr2 = addr1 + 1;
 83182   if( pIndex->onError!=OE_None ){
 83183     const int regRowid = regIdxKey + pIndex->nColumn;
 83184     const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
 83185     void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
 83187     /* The registers accessed by the OP_IsUnique opcode were allocated
 83188     ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
 83189     ** call above. Just before that function was freed they were released
 83190     ** (made available to the compiler for reuse) using 
 83191     ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
 83192     ** opcode use the values stored within seems dangerous. However, since
 83193     ** we can be sure that no other temp registers have been allocated
 83194     ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
 83195     */
 83196     sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
 83197     sqlite3HaltConstraint(
 83198         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
 83200   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
 83201   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 83202 #endif
 83203   sqlite3ReleaseTempReg(pParse, regRecord);
 83204   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
 83205   sqlite3VdbeJumpHere(v, addr1);
 83207   sqlite3VdbeAddOp1(v, OP_Close, iTab);
 83208   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
 83209   sqlite3VdbeAddOp1(v, OP_Close, iSorter);
 83212 /*
 83213 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
 83214 ** and pTblList is the name of the table that is to be indexed.  Both will 
 83215 ** be NULL for a primary key or an index that is created to satisfy a
 83216 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
 83217 ** as the table to be indexed.  pParse->pNewTable is a table that is
 83218 ** currently being constructed by a CREATE TABLE statement.
 83219 **
 83220 ** pList is a list of columns to be indexed.  pList will be NULL if this
 83221 ** is a primary key or unique-constraint on the most recent column added
 83222 ** to the table currently under construction.  
 83223 **
 83224 ** If the index is created successfully, return a pointer to the new Index
 83225 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
 83226 ** as the tables primary key (Index.autoIndex==2).
 83227 */
 83228 SQLITE_PRIVATE Index *sqlite3CreateIndex(
 83229   Parse *pParse,     /* All information about this parse */
 83230   Token *pName1,     /* First part of index name. May be NULL */
 83231   Token *pName2,     /* Second part of index name. May be NULL */
 83232   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
 83233   ExprList *pList,   /* A list of columns to be indexed */
 83234   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 83235   Token *pStart,     /* The CREATE token that begins this statement */
 83236   Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
 83237   int sortOrder,     /* Sort order of primary key when pList==NULL */
 83238   int ifNotExist     /* Omit error if index already exists */
 83239 ){
 83240   Index *pRet = 0;     /* Pointer to return */
 83241   Table *pTab = 0;     /* Table to be indexed */
 83242   Index *pIndex = 0;   /* The index to be created */
 83243   char *zName = 0;     /* Name of the index */
 83244   int nName;           /* Number of characters in zName */
 83245   int i, j;
 83246   Token nullId;        /* Fake token for an empty ID list */
 83247   DbFixer sFix;        /* For assigning database names to pTable */
 83248   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
 83249   sqlite3 *db = pParse->db;
 83250   Db *pDb;             /* The specific table containing the indexed database */
 83251   int iDb;             /* Index of the database that is being written */
 83252   Token *pName = 0;    /* Unqualified name of the index to create */
 83253   struct ExprList_item *pListItem; /* For looping over pList */
 83254   int nCol;
 83255   int nExtra = 0;
 83256   char *zExtra;
 83258   assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
 83259   assert( pParse->nErr==0 );      /* Never called with prior errors */
 83260   if( db->mallocFailed || IN_DECLARE_VTAB ){
 83261     goto exit_create_index;
 83263   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 83264     goto exit_create_index;
 83267   /*
 83268   ** Find the table that is to be indexed.  Return early if not found.
 83269   */
 83270   if( pTblName!=0 ){
 83272     /* Use the two-part index name to determine the database 
 83273     ** to search for the table. 'Fix' the table name to this db
 83274     ** before looking up the table.
 83275     */
 83276     assert( pName1 && pName2 );
 83277     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 83278     if( iDb<0 ) goto exit_create_index;
 83279     assert( pName && pName->z );
 83281 #ifndef SQLITE_OMIT_TEMPDB
 83282     /* If the index name was unqualified, check if the table
 83283     ** is a temp table. If so, set the database to 1. Do not do this
 83284     ** if initialising a database schema.
 83285     */
 83286     if( !db->init.busy ){
 83287       pTab = sqlite3SrcListLookup(pParse, pTblName);
 83288       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
 83289         iDb = 1;
 83292 #endif
 83294     if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
 83295         sqlite3FixSrcList(&sFix, pTblName)
 83296     ){
 83297       /* Because the parser constructs pTblName from a single identifier,
 83298       ** sqlite3FixSrcList can never fail. */
 83299       assert(0);
 83301     pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
 83302     assert( db->mallocFailed==0 || pTab==0 );
 83303     if( pTab==0 ) goto exit_create_index;
 83304     assert( db->aDb[iDb].pSchema==pTab->pSchema );
 83305   }else{
 83306     assert( pName==0 );
 83307     assert( pStart==0 );
 83308     pTab = pParse->pNewTable;
 83309     if( !pTab ) goto exit_create_index;
 83310     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 83312   pDb = &db->aDb[iDb];
 83314   assert( pTab!=0 );
 83315   assert( pParse->nErr==0 );
 83316   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
 83317        && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
 83318     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
 83319     goto exit_create_index;
 83321 #ifndef SQLITE_OMIT_VIEW
 83322   if( pTab->pSelect ){
 83323     sqlite3ErrorMsg(pParse, "views may not be indexed");
 83324     goto exit_create_index;
 83326 #endif
 83327 #ifndef SQLITE_OMIT_VIRTUALTABLE
 83328   if( IsVirtual(pTab) ){
 83329     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
 83330     goto exit_create_index;
 83332 #endif
 83334   /*
 83335   ** Find the name of the index.  Make sure there is not already another
 83336   ** index or table with the same name.  
 83337   **
 83338   ** Exception:  If we are reading the names of permanent indices from the
 83339   ** sqlite_master table (because some other process changed the schema) and
 83340   ** one of the index names collides with the name of a temporary table or
 83341   ** index, then we will continue to process this index.
 83342   **
 83343   ** If pName==0 it means that we are
 83344   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
 83345   ** own name.
 83346   */
 83347   if( pName ){
 83348     zName = sqlite3NameFromToken(db, pName);
 83349     if( zName==0 ) goto exit_create_index;
 83350     assert( pName->z!=0 );
 83351     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 83352       goto exit_create_index;
 83354     if( !db->init.busy ){
 83355       if( sqlite3FindTable(db, zName, 0)!=0 ){
 83356         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
 83357         goto exit_create_index;
 83360     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
 83361       if( !ifNotExist ){
 83362         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
 83363       }else{
 83364         assert( !db->init.busy );
 83365         sqlite3CodeVerifySchema(pParse, iDb);
 83367       goto exit_create_index;
 83369   }else{
 83370     int n;
 83371     Index *pLoop;
 83372     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
 83373     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
 83374     if( zName==0 ){
 83375       goto exit_create_index;
 83379   /* Check for authorization to create an index.
 83380   */
 83381 #ifndef SQLITE_OMIT_AUTHORIZATION
 83383     const char *zDb = pDb->zName;
 83384     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
 83385       goto exit_create_index;
 83387     i = SQLITE_CREATE_INDEX;
 83388     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
 83389     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
 83390       goto exit_create_index;
 83393 #endif
 83395   /* If pList==0, it means this routine was called to make a primary
 83396   ** key out of the last column added to the table under construction.
 83397   ** So create a fake list to simulate this.
 83398   */
 83399   if( pList==0 ){
 83400     nullId.z = pTab->aCol[pTab->nCol-1].zName;
 83401     nullId.n = sqlite3Strlen30((char*)nullId.z);
 83402     pList = sqlite3ExprListAppend(pParse, 0, 0);
 83403     if( pList==0 ) goto exit_create_index;
 83404     sqlite3ExprListSetName(pParse, pList, &nullId, 0);
 83405     pList->a[0].sortOrder = (u8)sortOrder;
 83408   /* Figure out how many bytes of space are required to store explicitly
 83409   ** specified collation sequence names.
 83410   */
 83411   for(i=0; i<pList->nExpr; i++){
 83412     Expr *pExpr = pList->a[i].pExpr;
 83413     if( pExpr ){
 83414       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
 83415       if( pColl ){
 83416         nExtra += (1 + sqlite3Strlen30(pColl->zName));
 83421   /* 
 83422   ** Allocate the index structure. 
 83423   */
 83424   nName = sqlite3Strlen30(zName);
 83425   nCol = pList->nExpr;
 83426   pIndex = sqlite3DbMallocZero(db, 
 83427       ROUND8(sizeof(Index)) +              /* Index structure  */
 83428       ROUND8(sizeof(tRowcnt)*(nCol+1)) +   /* Index.aiRowEst   */
 83429       sizeof(char *)*nCol +                /* Index.azColl     */
 83430       sizeof(int)*nCol +                   /* Index.aiColumn   */
 83431       sizeof(u8)*nCol +                    /* Index.aSortOrder */
 83432       nName + 1 +                          /* Index.zName      */
 83433       nExtra                               /* Collation sequence names */
 83434   );
 83435   if( db->mallocFailed ){
 83436     goto exit_create_index;
 83438   zExtra = (char*)pIndex;
 83439   pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
 83440   pIndex->azColl = (char**)
 83441      ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
 83442   assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
 83443   assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
 83444   pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
 83445   pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
 83446   pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
 83447   zExtra = (char *)(&pIndex->zName[nName+1]);
 83448   memcpy(pIndex->zName, zName, nName+1);
 83449   pIndex->pTable = pTab;
 83450   pIndex->nColumn = pList->nExpr;
 83451   pIndex->onError = (u8)onError;
 83452   pIndex->autoIndex = (u8)(pName==0);
 83453   pIndex->pSchema = db->aDb[iDb].pSchema;
 83454   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 83456   /* Check to see if we should honor DESC requests on index columns
 83457   */
 83458   if( pDb->pSchema->file_format>=4 ){
 83459     sortOrderMask = -1;   /* Honor DESC */
 83460   }else{
 83461     sortOrderMask = 0;    /* Ignore DESC */
 83464   /* Scan the names of the columns of the table to be indexed and
 83465   ** load the column indices into the Index structure.  Report an error
 83466   ** if any column is not found.
 83467   **
 83468   ** TODO:  Add a test to make sure that the same column is not named
 83469   ** more than once within the same index.  Only the first instance of
 83470   ** the column will ever be used by the optimizer.  Note that using the
 83471   ** same column more than once cannot be an error because that would 
 83472   ** break backwards compatibility - it needs to be a warning.
 83473   */
 83474   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
 83475     const char *zColName = pListItem->zName;
 83476     Column *pTabCol;
 83477     int requestedSortOrder;
 83478     CollSeq *pColl;                /* Collating sequence */
 83479     char *zColl;                   /* Collation sequence name */
 83481     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
 83482       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
 83484     if( j>=pTab->nCol ){
 83485       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
 83486         pTab->zName, zColName);
 83487       pParse->checkSchema = 1;
 83488       goto exit_create_index;
 83490     pIndex->aiColumn[i] = j;
 83491     if( pListItem->pExpr
 83492      && (pColl = sqlite3ExprCollSeq(pParse, pListItem->pExpr))!=0
 83493     ){
 83494       int nColl;
 83495       zColl = pColl->zName;
 83496       nColl = sqlite3Strlen30(zColl) + 1;
 83497       assert( nExtra>=nColl );
 83498       memcpy(zExtra, zColl, nColl);
 83499       zColl = zExtra;
 83500       zExtra += nColl;
 83501       nExtra -= nColl;
 83502     }else{
 83503       zColl = pTab->aCol[j].zColl;
 83504       if( !zColl ){
 83505         zColl = "BINARY";
 83508     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
 83509       goto exit_create_index;
 83511     pIndex->azColl[i] = zColl;
 83512     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
 83513     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
 83515   sqlite3DefaultRowEst(pIndex);
 83517   if( pTab==pParse->pNewTable ){
 83518     /* This routine has been called to create an automatic index as a
 83519     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
 83520     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
 83521     ** i.e. one of:
 83522     **
 83523     ** CREATE TABLE t(x PRIMARY KEY, y);
 83524     ** CREATE TABLE t(x, y, UNIQUE(x, y));
 83525     **
 83526     ** Either way, check to see if the table already has such an index. If
 83527     ** so, don't bother creating this one. This only applies to
 83528     ** automatically created indices. Users can do as they wish with
 83529     ** explicit indices.
 83530     **
 83531     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
 83532     ** (and thus suppressing the second one) even if they have different
 83533     ** sort orders.
 83534     **
 83535     ** If there are different collating sequences or if the columns of
 83536     ** the constraint occur in different orders, then the constraints are
 83537     ** considered distinct and both result in separate indices.
 83538     */
 83539     Index *pIdx;
 83540     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 83541       int k;
 83542       assert( pIdx->onError!=OE_None );
 83543       assert( pIdx->autoIndex );
 83544       assert( pIndex->onError!=OE_None );
 83546       if( pIdx->nColumn!=pIndex->nColumn ) continue;
 83547       for(k=0; k<pIdx->nColumn; k++){
 83548         const char *z1;
 83549         const char *z2;
 83550         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
 83551         z1 = pIdx->azColl[k];
 83552         z2 = pIndex->azColl[k];
 83553         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
 83555       if( k==pIdx->nColumn ){
 83556         if( pIdx->onError!=pIndex->onError ){
 83557           /* This constraint creates the same index as a previous
 83558           ** constraint specified somewhere in the CREATE TABLE statement.
 83559           ** However the ON CONFLICT clauses are different. If both this 
 83560           ** constraint and the previous equivalent constraint have explicit
 83561           ** ON CONFLICT clauses this is an error. Otherwise, use the
 83562           ** explicitly specified behaviour for the index.
 83563           */
 83564           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
 83565             sqlite3ErrorMsg(pParse, 
 83566                 "conflicting ON CONFLICT clauses specified", 0);
 83568           if( pIdx->onError==OE_Default ){
 83569             pIdx->onError = pIndex->onError;
 83572         goto exit_create_index;
 83577   /* Link the new Index structure to its table and to the other
 83578   ** in-memory database structures. 
 83579   */
 83580   if( db->init.busy ){
 83581     Index *p;
 83582     assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 83583     p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
 83584                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
 83585                           pIndex);
 83586     if( p ){
 83587       assert( p==pIndex );  /* Malloc must have failed */
 83588       db->mallocFailed = 1;
 83589       goto exit_create_index;
 83591     db->flags |= SQLITE_InternChanges;
 83592     if( pTblName!=0 ){
 83593       pIndex->tnum = db->init.newTnum;
 83597   /* If the db->init.busy is 0 then create the index on disk.  This
 83598   ** involves writing the index into the master table and filling in the
 83599   ** index with the current table contents.
 83600   **
 83601   ** The db->init.busy is 0 when the user first enters a CREATE INDEX 
 83602   ** command.  db->init.busy is 1 when a database is opened and 
 83603   ** CREATE INDEX statements are read out of the master table.  In
 83604   ** the latter case the index already exists on disk, which is why
 83605   ** we don't want to recreate it.
 83606   **
 83607   ** If pTblName==0 it means this index is generated as a primary key
 83608   ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
 83609   ** has just been created, it contains no data and the index initialization
 83610   ** step can be skipped.
 83611   */
 83612   else{ /* if( db->init.busy==0 ) */
 83613     Vdbe *v;
 83614     char *zStmt;
 83615     int iMem = ++pParse->nMem;
 83617     v = sqlite3GetVdbe(pParse);
 83618     if( v==0 ) goto exit_create_index;
 83621     /* Create the rootpage for the index
 83622     */
 83623     sqlite3BeginWriteOperation(pParse, 1, iDb);
 83624     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
 83626     /* Gather the complete text of the CREATE INDEX statement into
 83627     ** the zStmt variable
 83628     */
 83629     if( pStart ){
 83630       assert( pEnd!=0 );
 83631       /* A named index with an explicit CREATE INDEX statement */
 83632       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
 83633         onError==OE_None ? "" : " UNIQUE",
 83634         (int)(pEnd->z - pName->z) + 1,
 83635         pName->z);
 83636     }else{
 83637       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
 83638       /* zStmt = sqlite3MPrintf(""); */
 83639       zStmt = 0;
 83642     /* Add an entry in sqlite_master for this index
 83643     */
 83644     sqlite3NestedParse(pParse, 
 83645         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
 83646         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 83647         pIndex->zName,
 83648         pTab->zName,
 83649         iMem,
 83650         zStmt
 83651     );
 83652     sqlite3DbFree(db, zStmt);
 83654     /* Fill the index with data and reparse the schema. Code an OP_Expire
 83655     ** to invalidate all pre-compiled statements.
 83656     */
 83657     if( pTblName ){
 83658       sqlite3RefillIndex(pParse, pIndex, iMem);
 83659       sqlite3ChangeCookie(pParse, iDb);
 83660       sqlite3VdbeAddParseSchemaOp(v, iDb,
 83661          sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
 83662       sqlite3VdbeAddOp1(v, OP_Expire, 0);
 83666   /* When adding an index to the list of indices for a table, make
 83667   ** sure all indices labeled OE_Replace come after all those labeled
 83668   ** OE_Ignore.  This is necessary for the correct constraint check
 83669   ** processing (in sqlite3GenerateConstraintChecks()) as part of
 83670   ** UPDATE and INSERT statements.  
 83671   */
 83672   if( db->init.busy || pTblName==0 ){
 83673     if( onError!=OE_Replace || pTab->pIndex==0
 83674          || pTab->pIndex->onError==OE_Replace){
 83675       pIndex->pNext = pTab->pIndex;
 83676       pTab->pIndex = pIndex;
 83677     }else{
 83678       Index *pOther = pTab->pIndex;
 83679       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
 83680         pOther = pOther->pNext;
 83682       pIndex->pNext = pOther->pNext;
 83683       pOther->pNext = pIndex;
 83685     pRet = pIndex;
 83686     pIndex = 0;
 83689   /* Clean up before exiting */
 83690 exit_create_index:
 83691   if( pIndex ){
 83692     sqlite3DbFree(db, pIndex->zColAff);
 83693     sqlite3DbFree(db, pIndex);
 83695   sqlite3ExprListDelete(db, pList);
 83696   sqlite3SrcListDelete(db, pTblName);
 83697   sqlite3DbFree(db, zName);
 83698   return pRet;
 83701 /*
 83702 ** Fill the Index.aiRowEst[] array with default information - information
 83703 ** to be used when we have not run the ANALYZE command.
 83704 **
 83705 ** aiRowEst[0] is suppose to contain the number of elements in the index.
 83706 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
 83707 ** number of rows in the table that match any particular value of the
 83708 ** first column of the index.  aiRowEst[2] is an estimate of the number
 83709 ** of rows that match any particular combiniation of the first 2 columns
 83710 ** of the index.  And so forth.  It must always be the case that
 83712 **           aiRowEst[N]<=aiRowEst[N-1]
 83713 **           aiRowEst[N]>=1
 83714 **
 83715 ** Apart from that, we have little to go on besides intuition as to
 83716 ** how aiRowEst[] should be initialized.  The numbers generated here
 83717 ** are based on typical values found in actual indices.
 83718 */
 83719 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
 83720   tRowcnt *a = pIdx->aiRowEst;
 83721   int i;
 83722   tRowcnt n;
 83723   assert( a!=0 );
 83724   a[0] = pIdx->pTable->nRowEst;
 83725   if( a[0]<10 ) a[0] = 10;
 83726   n = 10;
 83727   for(i=1; i<=pIdx->nColumn; i++){
 83728     a[i] = n;
 83729     if( n>5 ) n--;
 83731   if( pIdx->onError!=OE_None ){
 83732     a[pIdx->nColumn] = 1;
 83736 /*
 83737 ** This routine will drop an existing named index.  This routine
 83738 ** implements the DROP INDEX statement.
 83739 */
 83740 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
 83741   Index *pIndex;
 83742   Vdbe *v;
 83743   sqlite3 *db = pParse->db;
 83744   int iDb;
 83746   assert( pParse->nErr==0 );   /* Never called with prior errors */
 83747   if( db->mallocFailed ){
 83748     goto exit_drop_index;
 83750   assert( pName->nSrc==1 );
 83751   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 83752     goto exit_drop_index;
 83754   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
 83755   if( pIndex==0 ){
 83756     if( !ifExists ){
 83757       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
 83758     }else{
 83759       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 83761     pParse->checkSchema = 1;
 83762     goto exit_drop_index;
 83764   if( pIndex->autoIndex ){
 83765     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
 83766       "or PRIMARY KEY constraint cannot be dropped", 0);
 83767     goto exit_drop_index;
 83769   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 83770 #ifndef SQLITE_OMIT_AUTHORIZATION
 83772     int code = SQLITE_DROP_INDEX;
 83773     Table *pTab = pIndex->pTable;
 83774     const char *zDb = db->aDb[iDb].zName;
 83775     const char *zTab = SCHEMA_TABLE(iDb);
 83776     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
 83777       goto exit_drop_index;
 83779     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
 83780     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
 83781       goto exit_drop_index;
 83784 #endif
 83786   /* Generate code to remove the index and from the master table */
 83787   v = sqlite3GetVdbe(pParse);
 83788   if( v ){
 83789     sqlite3BeginWriteOperation(pParse, 1, iDb);
 83790     sqlite3NestedParse(pParse,
 83791        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
 83792        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
 83793     );
 83794     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
 83795     sqlite3ChangeCookie(pParse, iDb);
 83796     destroyRootPage(pParse, pIndex->tnum, iDb);
 83797     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
 83800 exit_drop_index:
 83801   sqlite3SrcListDelete(db, pName);
 83804 /*
 83805 ** pArray is a pointer to an array of objects. Each object in the
 83806 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
 83807 ** to extend the array so that there is space for a new object at the end.
 83808 **
 83809 ** When this function is called, *pnEntry contains the current size of
 83810 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
 83811 ** in total).
 83812 **
 83813 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
 83814 ** space allocated for the new object is zeroed, *pnEntry updated to
 83815 ** reflect the new size of the array and a pointer to the new allocation
 83816 ** returned. *pIdx is set to the index of the new array entry in this case.
 83817 **
 83818 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
 83819 ** unchanged and a copy of pArray returned.
 83820 */
 83821 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
 83822   sqlite3 *db,      /* Connection to notify of malloc failures */
 83823   void *pArray,     /* Array of objects.  Might be reallocated */
 83824   int szEntry,      /* Size of each object in the array */
 83825   int *pnEntry,     /* Number of objects currently in use */
 83826   int *pIdx         /* Write the index of a new slot here */
 83827 ){
 83828   char *z;
 83829   int n = *pnEntry;
 83830   if( (n & (n-1))==0 ){
 83831     int sz = (n==0) ? 1 : 2*n;
 83832     void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
 83833     if( pNew==0 ){
 83834       *pIdx = -1;
 83835       return pArray;
 83837     pArray = pNew;
 83839   z = (char*)pArray;
 83840   memset(&z[n * szEntry], 0, szEntry);
 83841   *pIdx = n;
 83842   ++*pnEntry;
 83843   return pArray;
 83846 /*
 83847 ** Append a new element to the given IdList.  Create a new IdList if
 83848 ** need be.
 83849 **
 83850 ** A new IdList is returned, or NULL if malloc() fails.
 83851 */
 83852 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
 83853   int i;
 83854   if( pList==0 ){
 83855     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
 83856     if( pList==0 ) return 0;
 83858   pList->a = sqlite3ArrayAllocate(
 83859       db,
 83860       pList->a,
 83861       sizeof(pList->a[0]),
 83862       &pList->nId,
 83863       &i
 83864   );
 83865   if( i<0 ){
 83866     sqlite3IdListDelete(db, pList);
 83867     return 0;
 83869   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
 83870   return pList;
 83873 /*
 83874 ** Delete an IdList.
 83875 */
 83876 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
 83877   int i;
 83878   if( pList==0 ) return;
 83879   for(i=0; i<pList->nId; i++){
 83880     sqlite3DbFree(db, pList->a[i].zName);
 83882   sqlite3DbFree(db, pList->a);
 83883   sqlite3DbFree(db, pList);
 83886 /*
 83887 ** Return the index in pList of the identifier named zId.  Return -1
 83888 ** if not found.
 83889 */
 83890 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
 83891   int i;
 83892   if( pList==0 ) return -1;
 83893   for(i=0; i<pList->nId; i++){
 83894     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
 83896   return -1;
 83899 /*
 83900 ** Expand the space allocated for the given SrcList object by
 83901 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
 83902 ** New slots are zeroed.
 83903 **
 83904 ** For example, suppose a SrcList initially contains two entries: A,B.
 83905 ** To append 3 new entries onto the end, do this:
 83906 **
 83907 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
 83908 **
 83909 ** After the call above it would contain:  A, B, nil, nil, nil.
 83910 ** If the iStart argument had been 1 instead of 2, then the result
 83911 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
 83912 ** the iStart value would be 0.  The result then would
 83913 ** be: nil, nil, nil, A, B.
 83914 **
 83915 ** If a memory allocation fails the SrcList is unchanged.  The
 83916 ** db->mallocFailed flag will be set to true.
 83917 */
 83918 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
 83919   sqlite3 *db,       /* Database connection to notify of OOM errors */
 83920   SrcList *pSrc,     /* The SrcList to be enlarged */
 83921   int nExtra,        /* Number of new slots to add to pSrc->a[] */
 83922   int iStart         /* Index in pSrc->a[] of first new slot */
 83923 ){
 83924   int i;
 83926   /* Sanity checking on calling parameters */
 83927   assert( iStart>=0 );
 83928   assert( nExtra>=1 );
 83929   assert( pSrc!=0 );
 83930   assert( iStart<=pSrc->nSrc );
 83932   /* Allocate additional space if needed */
 83933   if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
 83934     SrcList *pNew;
 83935     int nAlloc = pSrc->nSrc+nExtra;
 83936     int nGot;
 83937     pNew = sqlite3DbRealloc(db, pSrc,
 83938                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
 83939     if( pNew==0 ){
 83940       assert( db->mallocFailed );
 83941       return pSrc;
 83943     pSrc = pNew;
 83944     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
 83945     pSrc->nAlloc = (u16)nGot;
 83948   /* Move existing slots that come after the newly inserted slots
 83949   ** out of the way */
 83950   for(i=pSrc->nSrc-1; i>=iStart; i--){
 83951     pSrc->a[i+nExtra] = pSrc->a[i];
 83953   pSrc->nSrc += (i16)nExtra;
 83955   /* Zero the newly allocated slots */
 83956   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
 83957   for(i=iStart; i<iStart+nExtra; i++){
 83958     pSrc->a[i].iCursor = -1;
 83961   /* Return a pointer to the enlarged SrcList */
 83962   return pSrc;
 83966 /*
 83967 ** Append a new table name to the given SrcList.  Create a new SrcList if
 83968 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
 83969 **
 83970 ** A SrcList is returned, or NULL if there is an OOM error.  The returned
 83971 ** SrcList might be the same as the SrcList that was input or it might be
 83972 ** a new one.  If an OOM error does occurs, then the prior value of pList
 83973 ** that is input to this routine is automatically freed.
 83974 **
 83975 ** If pDatabase is not null, it means that the table has an optional
 83976 ** database name prefix.  Like this:  "database.table".  The pDatabase
 83977 ** points to the table name and the pTable points to the database name.
 83978 ** The SrcList.a[].zName field is filled with the table name which might
 83979 ** come from pTable (if pDatabase is NULL) or from pDatabase.  
 83980 ** SrcList.a[].zDatabase is filled with the database name from pTable,
 83981 ** or with NULL if no database is specified.
 83982 **
 83983 ** In other words, if call like this:
 83984 **
 83985 **         sqlite3SrcListAppend(D,A,B,0);
 83986 **
 83987 ** Then B is a table name and the database name is unspecified.  If called
 83988 ** like this:
 83989 **
 83990 **         sqlite3SrcListAppend(D,A,B,C);
 83991 **
 83992 ** Then C is the table name and B is the database name.  If C is defined
 83993 ** then so is B.  In other words, we never have a case where:
 83994 **
 83995 **         sqlite3SrcListAppend(D,A,0,C);
 83996 **
 83997 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
 83998 ** before being added to the SrcList.
 83999 */
 84000 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
 84001   sqlite3 *db,        /* Connection to notify of malloc failures */
 84002   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
 84003   Token *pTable,      /* Table to append */
 84004   Token *pDatabase    /* Database of the table */
 84005 ){
 84006   struct SrcList_item *pItem;
 84007   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
 84008   if( pList==0 ){
 84009     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
 84010     if( pList==0 ) return 0;
 84011     pList->nAlloc = 1;
 84013   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
 84014   if( db->mallocFailed ){
 84015     sqlite3SrcListDelete(db, pList);
 84016     return 0;
 84018   pItem = &pList->a[pList->nSrc-1];
 84019   if( pDatabase && pDatabase->z==0 ){
 84020     pDatabase = 0;
 84022   if( pDatabase ){
 84023     Token *pTemp = pDatabase;
 84024     pDatabase = pTable;
 84025     pTable = pTemp;
 84027   pItem->zName = sqlite3NameFromToken(db, pTable);
 84028   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
 84029   return pList;
 84032 /*
 84033 ** Assign VdbeCursor index numbers to all tables in a SrcList
 84034 */
 84035 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
 84036   int i;
 84037   struct SrcList_item *pItem;
 84038   assert(pList || pParse->db->mallocFailed );
 84039   if( pList ){
 84040     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
 84041       if( pItem->iCursor>=0 ) break;
 84042       pItem->iCursor = pParse->nTab++;
 84043       if( pItem->pSelect ){
 84044         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
 84050 /*
 84051 ** Delete an entire SrcList including all its substructure.
 84052 */
 84053 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
 84054   int i;
 84055   struct SrcList_item *pItem;
 84056   if( pList==0 ) return;
 84057   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
 84058     sqlite3DbFree(db, pItem->zDatabase);
 84059     sqlite3DbFree(db, pItem->zName);
 84060     sqlite3DbFree(db, pItem->zAlias);
 84061     sqlite3DbFree(db, pItem->zIndex);
 84062     sqlite3DeleteTable(db, pItem->pTab);
 84063     sqlite3SelectDelete(db, pItem->pSelect);
 84064     sqlite3ExprDelete(db, pItem->pOn);
 84065     sqlite3IdListDelete(db, pItem->pUsing);
 84067   sqlite3DbFree(db, pList);
 84070 /*
 84071 ** This routine is called by the parser to add a new term to the
 84072 ** end of a growing FROM clause.  The "p" parameter is the part of
 84073 ** the FROM clause that has already been constructed.  "p" is NULL
 84074 ** if this is the first term of the FROM clause.  pTable and pDatabase
 84075 ** are the name of the table and database named in the FROM clause term.
 84076 ** pDatabase is NULL if the database name qualifier is missing - the
 84077 ** usual case.  If the term has a alias, then pAlias points to the
 84078 ** alias token.  If the term is a subquery, then pSubquery is the
 84079 ** SELECT statement that the subquery encodes.  The pTable and
 84080 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
 84081 ** parameters are the content of the ON and USING clauses.
 84082 **
 84083 ** Return a new SrcList which encodes is the FROM with the new
 84084 ** term added.
 84085 */
 84086 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
 84087   Parse *pParse,          /* Parsing context */
 84088   SrcList *p,             /* The left part of the FROM clause already seen */
 84089   Token *pTable,          /* Name of the table to add to the FROM clause */
 84090   Token *pDatabase,       /* Name of the database containing pTable */
 84091   Token *pAlias,          /* The right-hand side of the AS subexpression */
 84092   Select *pSubquery,      /* A subquery used in place of a table name */
 84093   Expr *pOn,              /* The ON clause of a join */
 84094   IdList *pUsing          /* The USING clause of a join */
 84095 ){
 84096   struct SrcList_item *pItem;
 84097   sqlite3 *db = pParse->db;
 84098   if( !p && (pOn || pUsing) ){
 84099     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
 84100       (pOn ? "ON" : "USING")
 84101     );
 84102     goto append_from_error;
 84104   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
 84105   if( p==0 || NEVER(p->nSrc==0) ){
 84106     goto append_from_error;
 84108   pItem = &p->a[p->nSrc-1];
 84109   assert( pAlias!=0 );
 84110   if( pAlias->n ){
 84111     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
 84113   pItem->pSelect = pSubquery;
 84114   pItem->pOn = pOn;
 84115   pItem->pUsing = pUsing;
 84116   return p;
 84118  append_from_error:
 84119   assert( p==0 );
 84120   sqlite3ExprDelete(db, pOn);
 84121   sqlite3IdListDelete(db, pUsing);
 84122   sqlite3SelectDelete(db, pSubquery);
 84123   return 0;
 84126 /*
 84127 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
 84128 ** element of the source-list passed as the second argument.
 84129 */
 84130 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
 84131   assert( pIndexedBy!=0 );
 84132   if( p && ALWAYS(p->nSrc>0) ){
 84133     struct SrcList_item *pItem = &p->a[p->nSrc-1];
 84134     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
 84135     if( pIndexedBy->n==1 && !pIndexedBy->z ){
 84136       /* A "NOT INDEXED" clause was supplied. See parse.y 
 84137       ** construct "indexed_opt" for details. */
 84138       pItem->notIndexed = 1;
 84139     }else{
 84140       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
 84145 /*
 84146 ** When building up a FROM clause in the parser, the join operator
 84147 ** is initially attached to the left operand.  But the code generator
 84148 ** expects the join operator to be on the right operand.  This routine
 84149 ** Shifts all join operators from left to right for an entire FROM
 84150 ** clause.
 84151 **
 84152 ** Example: Suppose the join is like this:
 84153 **
 84154 **           A natural cross join B
 84155 **
 84156 ** The operator is "natural cross join".  The A and B operands are stored
 84157 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
 84158 ** operator with A.  This routine shifts that operator over to B.
 84159 */
 84160 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
 84161   if( p ){
 84162     int i;
 84163     assert( p->a || p->nSrc==0 );
 84164     for(i=p->nSrc-1; i>0; i--){
 84165       p->a[i].jointype = p->a[i-1].jointype;
 84167     p->a[0].jointype = 0;
 84171 /*
 84172 ** Begin a transaction
 84173 */
 84174 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
 84175   sqlite3 *db;
 84176   Vdbe *v;
 84177   int i;
 84179   assert( pParse!=0 );
 84180   db = pParse->db;
 84181   assert( db!=0 );
 84182 /*  if( db->aDb[0].pBt==0 ) return; */
 84183   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
 84184     return;
 84186   v = sqlite3GetVdbe(pParse);
 84187   if( !v ) return;
 84188   if( type!=TK_DEFERRED ){
 84189     for(i=0; i<db->nDb; i++){
 84190       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
 84191       sqlite3VdbeUsesBtree(v, i);
 84194   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
 84197 /*
 84198 ** Commit a transaction
 84199 */
 84200 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
 84201   Vdbe *v;
 84203   assert( pParse!=0 );
 84204   assert( pParse->db!=0 );
 84205   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
 84206     return;
 84208   v = sqlite3GetVdbe(pParse);
 84209   if( v ){
 84210     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
 84214 /*
 84215 ** Rollback a transaction
 84216 */
 84217 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
 84218   Vdbe *v;
 84220   assert( pParse!=0 );
 84221   assert( pParse->db!=0 );
 84222   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
 84223     return;
 84225   v = sqlite3GetVdbe(pParse);
 84226   if( v ){
 84227     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
 84231 /*
 84232 ** This function is called by the parser when it parses a command to create,
 84233 ** release or rollback an SQL savepoint. 
 84234 */
 84235 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
 84236   char *zName = sqlite3NameFromToken(pParse->db, pName);
 84237   if( zName ){
 84238     Vdbe *v = sqlite3GetVdbe(pParse);
 84239 #ifndef SQLITE_OMIT_AUTHORIZATION
 84240     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
 84241     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
 84242 #endif
 84243     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
 84244       sqlite3DbFree(pParse->db, zName);
 84245       return;
 84247     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
 84251 /*
 84252 ** Make sure the TEMP database is open and available for use.  Return
 84253 ** the number of errors.  Leave any error messages in the pParse structure.
 84254 */
 84255 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
 84256   sqlite3 *db = pParse->db;
 84257   if( db->aDb[1].pBt==0 && !pParse->explain ){
 84258     int rc;
 84259     Btree *pBt;
 84260     static const int flags = 
 84261           SQLITE_OPEN_READWRITE |
 84262           SQLITE_OPEN_CREATE |
 84263           SQLITE_OPEN_EXCLUSIVE |
 84264           SQLITE_OPEN_DELETEONCLOSE |
 84265           SQLITE_OPEN_TEMP_DB;
 84267     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
 84268     if( rc!=SQLITE_OK ){
 84269       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
 84270         "file for storing temporary tables");
 84271       pParse->rc = rc;
 84272       return 1;
 84274     db->aDb[1].pBt = pBt;
 84275     assert( db->aDb[1].pSchema );
 84276     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
 84277       db->mallocFailed = 1;
 84278       return 1;
 84281   return 0;
 84284 /*
 84285 ** Generate VDBE code that will verify the schema cookie and start
 84286 ** a read-transaction for all named database files.
 84287 **
 84288 ** It is important that all schema cookies be verified and all
 84289 ** read transactions be started before anything else happens in
 84290 ** the VDBE program.  But this routine can be called after much other
 84291 ** code has been generated.  So here is what we do:
 84292 **
 84293 ** The first time this routine is called, we code an OP_Goto that
 84294 ** will jump to a subroutine at the end of the program.  Then we
 84295 ** record every database that needs its schema verified in the
 84296 ** pParse->cookieMask field.  Later, after all other code has been
 84297 ** generated, the subroutine that does the cookie verifications and
 84298 ** starts the transactions will be coded and the OP_Goto P2 value
 84299 ** will be made to point to that subroutine.  The generation of the
 84300 ** cookie verification subroutine code happens in sqlite3FinishCoding().
 84301 **
 84302 ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
 84303 ** schema on any databases.  This can be used to position the OP_Goto
 84304 ** early in the code, before we know if any database tables will be used.
 84305 */
 84306 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
 84307   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 84309 #ifndef SQLITE_OMIT_TRIGGER
 84310   if( pToplevel!=pParse ){
 84311     /* This branch is taken if a trigger is currently being coded. In this
 84312     ** case, set cookieGoto to a non-zero value to show that this function
 84313     ** has been called. This is used by the sqlite3ExprCodeConstants()
 84314     ** function. */
 84315     pParse->cookieGoto = -1;
 84317 #endif
 84318   if( pToplevel->cookieGoto==0 ){
 84319     Vdbe *v = sqlite3GetVdbe(pToplevel);
 84320     if( v==0 ) return;  /* This only happens if there was a prior error */
 84321     pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
 84323   if( iDb>=0 ){
 84324     sqlite3 *db = pToplevel->db;
 84325     yDbMask mask;
 84327     assert( iDb<db->nDb );
 84328     assert( db->aDb[iDb].pBt!=0 || iDb==1 );
 84329     assert( iDb<SQLITE_MAX_ATTACHED+2 );
 84330     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 84331     mask = ((yDbMask)1)<<iDb;
 84332     if( (pToplevel->cookieMask & mask)==0 ){
 84333       pToplevel->cookieMask |= mask;
 84334       pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
 84335       if( !OMIT_TEMPDB && iDb==1 ){
 84336         sqlite3OpenTempDatabase(pToplevel);
 84342 /*
 84343 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
 84344 ** attached database. Otherwise, invoke it for the database named zDb only.
 84345 */
 84346 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
 84347   sqlite3 *db = pParse->db;
 84348   int i;
 84349   for(i=0; i<db->nDb; i++){
 84350     Db *pDb = &db->aDb[i];
 84351     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
 84352       sqlite3CodeVerifySchema(pParse, i);
 84357 /*
 84358 ** Generate VDBE code that prepares for doing an operation that
 84359 ** might change the database.
 84360 **
 84361 ** This routine starts a new transaction if we are not already within
 84362 ** a transaction.  If we are already within a transaction, then a checkpoint
 84363 ** is set if the setStatement parameter is true.  A checkpoint should
 84364 ** be set for operations that might fail (due to a constraint) part of
 84365 ** the way through and which will need to undo some writes without having to
 84366 ** rollback the whole transaction.  For operations where all constraints
 84367 ** can be checked before any changes are made to the database, it is never
 84368 ** necessary to undo a write and the checkpoint should not be set.
 84369 */
 84370 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
 84371   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 84372   sqlite3CodeVerifySchema(pParse, iDb);
 84373   pToplevel->writeMask |= ((yDbMask)1)<<iDb;
 84374   pToplevel->isMultiWrite |= setStatement;
 84377 /*
 84378 ** Indicate that the statement currently under construction might write
 84379 ** more than one entry (example: deleting one row then inserting another,
 84380 ** inserting multiple rows in a table, or inserting a row and index entries.)
 84381 ** If an abort occurs after some of these writes have completed, then it will
 84382 ** be necessary to undo the completed writes.
 84383 */
 84384 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
 84385   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 84386   pToplevel->isMultiWrite = 1;
 84389 /* 
 84390 ** The code generator calls this routine if is discovers that it is
 84391 ** possible to abort a statement prior to completion.  In order to 
 84392 ** perform this abort without corrupting the database, we need to make
 84393 ** sure that the statement is protected by a statement transaction.
 84394 **
 84395 ** Technically, we only need to set the mayAbort flag if the
 84396 ** isMultiWrite flag was previously set.  There is a time dependency
 84397 ** such that the abort must occur after the multiwrite.  This makes
 84398 ** some statements involving the REPLACE conflict resolution algorithm
 84399 ** go a little faster.  But taking advantage of this time dependency
 84400 ** makes it more difficult to prove that the code is correct (in 
 84401 ** particular, it prevents us from writing an effective
 84402 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
 84403 ** to take the safe route and skip the optimization.
 84404 */
 84405 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
 84406   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 84407   pToplevel->mayAbort = 1;
 84410 /*
 84411 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
 84412 ** error. The onError parameter determines which (if any) of the statement
 84413 ** and/or current transaction is rolled back.
 84414 */
 84415 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
 84416   Vdbe *v = sqlite3GetVdbe(pParse);
 84417   if( onError==OE_Abort ){
 84418     sqlite3MayAbort(pParse);
 84420   sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
 84423 /*
 84424 ** Check to see if pIndex uses the collating sequence pColl.  Return
 84425 ** true if it does and false if it does not.
 84426 */
 84427 #ifndef SQLITE_OMIT_REINDEX
 84428 static int collationMatch(const char *zColl, Index *pIndex){
 84429   int i;
 84430   assert( zColl!=0 );
 84431   for(i=0; i<pIndex->nColumn; i++){
 84432     const char *z = pIndex->azColl[i];
 84433     assert( z!=0 );
 84434     if( 0==sqlite3StrICmp(z, zColl) ){
 84435       return 1;
 84438   return 0;
 84440 #endif
 84442 /*
 84443 ** Recompute all indices of pTab that use the collating sequence pColl.
 84444 ** If pColl==0 then recompute all indices of pTab.
 84445 */
 84446 #ifndef SQLITE_OMIT_REINDEX
 84447 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
 84448   Index *pIndex;              /* An index associated with pTab */
 84450   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
 84451     if( zColl==0 || collationMatch(zColl, pIndex) ){
 84452       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 84453       sqlite3BeginWriteOperation(pParse, 0, iDb);
 84454       sqlite3RefillIndex(pParse, pIndex, -1);
 84458 #endif
 84460 /*
 84461 ** Recompute all indices of all tables in all databases where the
 84462 ** indices use the collating sequence pColl.  If pColl==0 then recompute
 84463 ** all indices everywhere.
 84464 */
 84465 #ifndef SQLITE_OMIT_REINDEX
 84466 static void reindexDatabases(Parse *pParse, char const *zColl){
 84467   Db *pDb;                    /* A single database */
 84468   int iDb;                    /* The database index number */
 84469   sqlite3 *db = pParse->db;   /* The database connection */
 84470   HashElem *k;                /* For looping over tables in pDb */
 84471   Table *pTab;                /* A table in the database */
 84473   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
 84474   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
 84475     assert( pDb!=0 );
 84476     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
 84477       pTab = (Table*)sqliteHashData(k);
 84478       reindexTable(pParse, pTab, zColl);
 84482 #endif
 84484 /*
 84485 ** Generate code for the REINDEX command.
 84486 **
 84487 **        REINDEX                            -- 1
 84488 **        REINDEX  <collation>               -- 2
 84489 **        REINDEX  ?<database>.?<tablename>  -- 3
 84490 **        REINDEX  ?<database>.?<indexname>  -- 4
 84491 **
 84492 ** Form 1 causes all indices in all attached databases to be rebuilt.
 84493 ** Form 2 rebuilds all indices in all databases that use the named
 84494 ** collating function.  Forms 3 and 4 rebuild the named index or all
 84495 ** indices associated with the named table.
 84496 */
 84497 #ifndef SQLITE_OMIT_REINDEX
 84498 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
 84499   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
 84500   char *z;                    /* Name of a table or index */
 84501   const char *zDb;            /* Name of the database */
 84502   Table *pTab;                /* A table in the database */
 84503   Index *pIndex;              /* An index associated with pTab */
 84504   int iDb;                    /* The database index number */
 84505   sqlite3 *db = pParse->db;   /* The database connection */
 84506   Token *pObjName;            /* Name of the table or index to be reindexed */
 84508   /* Read the database schema. If an error occurs, leave an error message
 84509   ** and code in pParse and return NULL. */
 84510   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 84511     return;
 84514   if( pName1==0 ){
 84515     reindexDatabases(pParse, 0);
 84516     return;
 84517   }else if( NEVER(pName2==0) || pName2->z==0 ){
 84518     char *zColl;
 84519     assert( pName1->z );
 84520     zColl = sqlite3NameFromToken(pParse->db, pName1);
 84521     if( !zColl ) return;
 84522     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 84523     if( pColl ){
 84524       reindexDatabases(pParse, zColl);
 84525       sqlite3DbFree(db, zColl);
 84526       return;
 84528     sqlite3DbFree(db, zColl);
 84530   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
 84531   if( iDb<0 ) return;
 84532   z = sqlite3NameFromToken(db, pObjName);
 84533   if( z==0 ) return;
 84534   zDb = db->aDb[iDb].zName;
 84535   pTab = sqlite3FindTable(db, z, zDb);
 84536   if( pTab ){
 84537     reindexTable(pParse, pTab, 0);
 84538     sqlite3DbFree(db, z);
 84539     return;
 84541   pIndex = sqlite3FindIndex(db, z, zDb);
 84542   sqlite3DbFree(db, z);
 84543   if( pIndex ){
 84544     sqlite3BeginWriteOperation(pParse, 0, iDb);
 84545     sqlite3RefillIndex(pParse, pIndex, -1);
 84546     return;
 84548   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
 84550 #endif
 84552 /*
 84553 ** Return a dynamicly allocated KeyInfo structure that can be used
 84554 ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
 84555 **
 84556 ** If successful, a pointer to the new structure is returned. In this case
 84557 ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned 
 84558 ** pointer. If an error occurs (out of memory or missing collation 
 84559 ** sequence), NULL is returned and the state of pParse updated to reflect
 84560 ** the error.
 84561 */
 84562 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
 84563   int i;
 84564   int nCol = pIdx->nColumn;
 84565   int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
 84566   sqlite3 *db = pParse->db;
 84567   KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
 84569   if( pKey ){
 84570     pKey->db = pParse->db;
 84571     pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
 84572     assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
 84573     for(i=0; i<nCol; i++){
 84574       char *zColl = pIdx->azColl[i];
 84575       assert( zColl );
 84576       pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
 84577       pKey->aSortOrder[i] = pIdx->aSortOrder[i];
 84579     pKey->nField = (u16)nCol;
 84582   if( pParse->nErr ){
 84583     sqlite3DbFree(db, pKey);
 84584     pKey = 0;
 84586   return pKey;
 84589 /************** End of build.c ***********************************************/
 84590 /************** Begin file callback.c ****************************************/
 84591 /*
 84592 ** 2005 May 23 
 84593 **
 84594 ** The author disclaims copyright to this source code.  In place of
 84595 ** a legal notice, here is a blessing:
 84596 **
 84597 **    May you do good and not evil.
 84598 **    May you find forgiveness for yourself and forgive others.
 84599 **    May you share freely, never taking more than you give.
 84600 **
 84601 *************************************************************************
 84602 **
 84603 ** This file contains functions used to access the internal hash tables
 84604 ** of user defined functions and collation sequences.
 84605 */
 84608 /*
 84609 ** Invoke the 'collation needed' callback to request a collation sequence
 84610 ** in the encoding enc of name zName, length nName.
 84611 */
 84612 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
 84613   assert( !db->xCollNeeded || !db->xCollNeeded16 );
 84614   if( db->xCollNeeded ){
 84615     char *zExternal = sqlite3DbStrDup(db, zName);
 84616     if( !zExternal ) return;
 84617     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
 84618     sqlite3DbFree(db, zExternal);
 84620 #ifndef SQLITE_OMIT_UTF16
 84621   if( db->xCollNeeded16 ){
 84622     char const *zExternal;
 84623     sqlite3_value *pTmp = sqlite3ValueNew(db);
 84624     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
 84625     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
 84626     if( zExternal ){
 84627       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
 84629     sqlite3ValueFree(pTmp);
 84631 #endif
 84634 /*
 84635 ** This routine is called if the collation factory fails to deliver a
 84636 ** collation function in the best encoding but there may be other versions
 84637 ** of this collation function (for other text encodings) available. Use one
 84638 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
 84639 ** possible.
 84640 */
 84641 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
 84642   CollSeq *pColl2;
 84643   char *z = pColl->zName;
 84644   int i;
 84645   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
 84646   for(i=0; i<3; i++){
 84647     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
 84648     if( pColl2->xCmp!=0 ){
 84649       memcpy(pColl, pColl2, sizeof(CollSeq));
 84650       pColl->xDel = 0;         /* Do not copy the destructor */
 84651       return SQLITE_OK;
 84654   return SQLITE_ERROR;
 84657 /*
 84658 ** This function is responsible for invoking the collation factory callback
 84659 ** or substituting a collation sequence of a different encoding when the
 84660 ** requested collation sequence is not available in the desired encoding.
 84661 ** 
 84662 ** If it is not NULL, then pColl must point to the database native encoding 
 84663 ** collation sequence with name zName, length nName.
 84664 **
 84665 ** The return value is either the collation sequence to be used in database
 84666 ** db for collation type name zName, length nName, or NULL, if no collation
 84667 ** sequence can be found.  If no collation is found, leave an error message.
 84668 **
 84669 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
 84670 */
 84671 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
 84672   Parse *pParse,        /* Parsing context */
 84673   u8 enc,               /* The desired encoding for the collating sequence */
 84674   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
 84675   const char *zName     /* Collating sequence name */
 84676 ){
 84677   CollSeq *p;
 84678   sqlite3 *db = pParse->db;
 84680   p = pColl;
 84681   if( !p ){
 84682     p = sqlite3FindCollSeq(db, enc, zName, 0);
 84684   if( !p || !p->xCmp ){
 84685     /* No collation sequence of this type for this encoding is registered.
 84686     ** Call the collation factory to see if it can supply us with one.
 84687     */
 84688     callCollNeeded(db, enc, zName);
 84689     p = sqlite3FindCollSeq(db, enc, zName, 0);
 84691   if( p && !p->xCmp && synthCollSeq(db, p) ){
 84692     p = 0;
 84694   assert( !p || p->xCmp );
 84695   if( p==0 ){
 84696     sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 84698   return p;
 84701 /*
 84702 ** This routine is called on a collation sequence before it is used to
 84703 ** check that it is defined. An undefined collation sequence exists when
 84704 ** a database is loaded that contains references to collation sequences
 84705 ** that have not been defined by sqlite3_create_collation() etc.
 84706 **
 84707 ** If required, this routine calls the 'collation needed' callback to
 84708 ** request a definition of the collating sequence. If this doesn't work, 
 84709 ** an equivalent collating sequence that uses a text encoding different
 84710 ** from the main database is substituted, if one is available.
 84711 */
 84712 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
 84713   if( pColl ){
 84714     const char *zName = pColl->zName;
 84715     sqlite3 *db = pParse->db;
 84716     CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
 84717     if( !p ){
 84718       return SQLITE_ERROR;
 84720     assert( p==pColl );
 84722   return SQLITE_OK;
 84727 /*
 84728 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
 84729 ** specified by zName and nName is not found and parameter 'create' is
 84730 ** true, then create a new entry. Otherwise return NULL.
 84731 **
 84732 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
 84733 ** array of three CollSeq structures. The first is the collation sequence
 84734 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
 84735 **
 84736 ** Stored immediately after the three collation sequences is a copy of
 84737 ** the collation sequence name. A pointer to this string is stored in
 84738 ** each collation sequence structure.
 84739 */
 84740 static CollSeq *findCollSeqEntry(
 84741   sqlite3 *db,          /* Database connection */
 84742   const char *zName,    /* Name of the collating sequence */
 84743   int create            /* Create a new entry if true */
 84744 ){
 84745   CollSeq *pColl;
 84746   int nName = sqlite3Strlen30(zName);
 84747   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
 84749   if( 0==pColl && create ){
 84750     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
 84751     if( pColl ){
 84752       CollSeq *pDel = 0;
 84753       pColl[0].zName = (char*)&pColl[3];
 84754       pColl[0].enc = SQLITE_UTF8;
 84755       pColl[1].zName = (char*)&pColl[3];
 84756       pColl[1].enc = SQLITE_UTF16LE;
 84757       pColl[2].zName = (char*)&pColl[3];
 84758       pColl[2].enc = SQLITE_UTF16BE;
 84759       memcpy(pColl[0].zName, zName, nName);
 84760       pColl[0].zName[nName] = 0;
 84761       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
 84763       /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
 84764       ** return the pColl pointer to be deleted (because it wasn't added
 84765       ** to the hash table).
 84766       */
 84767       assert( pDel==0 || pDel==pColl );
 84768       if( pDel!=0 ){
 84769         db->mallocFailed = 1;
 84770         sqlite3DbFree(db, pDel);
 84771         pColl = 0;
 84775   return pColl;
 84778 /*
 84779 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
 84780 ** Return the CollSeq* pointer for the collation sequence named zName
 84781 ** for the encoding 'enc' from the database 'db'.
 84782 **
 84783 ** If the entry specified is not found and 'create' is true, then create a
 84784 ** new entry.  Otherwise return NULL.
 84785 **
 84786 ** A separate function sqlite3LocateCollSeq() is a wrapper around
 84787 ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
 84788 ** if necessary and generates an error message if the collating sequence
 84789 ** cannot be found.
 84790 **
 84791 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
 84792 */
 84793 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
 84794   sqlite3 *db,
 84795   u8 enc,
 84796   const char *zName,
 84797   int create
 84798 ){
 84799   CollSeq *pColl;
 84800   if( zName ){
 84801     pColl = findCollSeqEntry(db, zName, create);
 84802   }else{
 84803     pColl = db->pDfltColl;
 84805   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
 84806   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
 84807   if( pColl ) pColl += enc-1;
 84808   return pColl;
 84811 /* During the search for the best function definition, this procedure
 84812 ** is called to test how well the function passed as the first argument
 84813 ** matches the request for a function with nArg arguments in a system
 84814 ** that uses encoding enc. The value returned indicates how well the
 84815 ** request is matched. A higher value indicates a better match.
 84816 **
 84817 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
 84818 ** is also -1.  In other words, we are searching for a function that
 84819 ** takes a variable number of arguments.
 84820 **
 84821 ** If nArg is -2 that means that we are searching for any function 
 84822 ** regardless of the number of arguments it uses, so return a positive
 84823 ** match score for any
 84824 **
 84825 ** The returned value is always between 0 and 6, as follows:
 84826 **
 84827 ** 0: Not a match.
 84828 ** 1: UTF8/16 conversion required and function takes any number of arguments.
 84829 ** 2: UTF16 byte order change required and function takes any number of args.
 84830 ** 3: encoding matches and function takes any number of arguments
 84831 ** 4: UTF8/16 conversion required - argument count matches exactly
 84832 ** 5: UTF16 byte order conversion required - argument count matches exactly
 84833 ** 6: Perfect match:  encoding and argument count match exactly.
 84834 **
 84835 ** If nArg==(-2) then any function with a non-null xStep or xFunc is
 84836 ** a perfect match and any function with both xStep and xFunc NULL is
 84837 ** a non-match.
 84838 */
 84839 #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
 84840 static int matchQuality(
 84841   FuncDef *p,     /* The function we are evaluating for match quality */
 84842   int nArg,       /* Desired number of arguments.  (-1)==any */
 84843   u8 enc          /* Desired text encoding */
 84844 ){
 84845   int match;
 84847   /* nArg of -2 is a special case */
 84848   if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
 84850   /* Wrong number of arguments means "no match" */
 84851   if( p->nArg!=nArg && p->nArg>=0 ) return 0;
 84853   /* Give a better score to a function with a specific number of arguments
 84854   ** than to function that accepts any number of arguments. */
 84855   if( p->nArg==nArg ){
 84856     match = 4;
 84857   }else{
 84858     match = 1;
 84861   /* Bonus points if the text encoding matches */
 84862   if( enc==p->iPrefEnc ){
 84863     match += 2;  /* Exact encoding match */
 84864   }else if( (enc & p->iPrefEnc & 2)!=0 ){
 84865     match += 1;  /* Both are UTF16, but with different byte orders */
 84868   return match;
 84871 /*
 84872 ** Search a FuncDefHash for a function with the given name.  Return
 84873 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
 84874 */
 84875 static FuncDef *functionSearch(
 84876   FuncDefHash *pHash,  /* Hash table to search */
 84877   int h,               /* Hash of the name */
 84878   const char *zFunc,   /* Name of function */
 84879   int nFunc            /* Number of bytes in zFunc */
 84880 ){
 84881   FuncDef *p;
 84882   for(p=pHash->a[h]; p; p=p->pHash){
 84883     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
 84884       return p;
 84887   return 0;
 84890 /*
 84891 ** Insert a new FuncDef into a FuncDefHash hash table.
 84892 */
 84893 SQLITE_PRIVATE void sqlite3FuncDefInsert(
 84894   FuncDefHash *pHash,  /* The hash table into which to insert */
 84895   FuncDef *pDef        /* The function definition to insert */
 84896 ){
 84897   FuncDef *pOther;
 84898   int nName = sqlite3Strlen30(pDef->zName);
 84899   u8 c1 = (u8)pDef->zName[0];
 84900   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
 84901   pOther = functionSearch(pHash, h, pDef->zName, nName);
 84902   if( pOther ){
 84903     assert( pOther!=pDef && pOther->pNext!=pDef );
 84904     pDef->pNext = pOther->pNext;
 84905     pOther->pNext = pDef;
 84906   }else{
 84907     pDef->pNext = 0;
 84908     pDef->pHash = pHash->a[h];
 84909     pHash->a[h] = pDef;
 84915 /*
 84916 ** Locate a user function given a name, a number of arguments and a flag
 84917 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
 84918 ** pointer to the FuncDef structure that defines that function, or return
 84919 ** NULL if the function does not exist.
 84920 **
 84921 ** If the createFlag argument is true, then a new (blank) FuncDef
 84922 ** structure is created and liked into the "db" structure if a
 84923 ** no matching function previously existed.
 84924 **
 84925 ** If nArg is -2, then the first valid function found is returned.  A
 84926 ** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
 84927 ** case is used to see if zName is a valid function name for some number
 84928 ** of arguments.  If nArg is -2, then createFlag must be 0.
 84929 **
 84930 ** If createFlag is false, then a function with the required name and
 84931 ** number of arguments may be returned even if the eTextRep flag does not
 84932 ** match that requested.
 84933 */
 84934 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
 84935   sqlite3 *db,       /* An open database */
 84936   const char *zName, /* Name of the function.  Not null-terminated */
 84937   int nName,         /* Number of characters in the name */
 84938   int nArg,          /* Number of arguments.  -1 means any number */
 84939   u8 enc,            /* Preferred text encoding */
 84940   u8 createFlag      /* Create new entry if true and does not otherwise exist */
 84941 ){
 84942   FuncDef *p;         /* Iterator variable */
 84943   FuncDef *pBest = 0; /* Best match found so far */
 84944   int bestScore = 0;  /* Score of best match */
 84945   int h;              /* Hash value */
 84947   assert( nArg>=(-2) );
 84948   assert( nArg>=(-1) || createFlag==0 );
 84949   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
 84950   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
 84952   /* First search for a match amongst the application-defined functions.
 84953   */
 84954   p = functionSearch(&db->aFunc, h, zName, nName);
 84955   while( p ){
 84956     int score = matchQuality(p, nArg, enc);
 84957     if( score>bestScore ){
 84958       pBest = p;
 84959       bestScore = score;
 84961     p = p->pNext;
 84964   /* If no match is found, search the built-in functions.
 84965   **
 84966   ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
 84967   ** functions even if a prior app-defined function was found.  And give
 84968   ** priority to built-in functions.
 84969   **
 84970   ** Except, if createFlag is true, that means that we are trying to
 84971   ** install a new function.  Whatever FuncDef structure is returned it will
 84972   ** have fields overwritten with new information appropriate for the
 84973   ** new function.  But the FuncDefs for built-in functions are read-only.
 84974   ** So we must not search for built-ins when creating a new function.
 84975   */ 
 84976   if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
 84977     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 84978     bestScore = 0;
 84979     p = functionSearch(pHash, h, zName, nName);
 84980     while( p ){
 84981       int score = matchQuality(p, nArg, enc);
 84982       if( score>bestScore ){
 84983         pBest = p;
 84984         bestScore = score;
 84986       p = p->pNext;
 84990   /* If the createFlag parameter is true and the search did not reveal an
 84991   ** exact match for the name, number of arguments and encoding, then add a
 84992   ** new entry to the hash table and return it.
 84993   */
 84994   if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
 84995       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
 84996     pBest->zName = (char *)&pBest[1];
 84997     pBest->nArg = (u16)nArg;
 84998     pBest->iPrefEnc = enc;
 84999     memcpy(pBest->zName, zName, nName);
 85000     pBest->zName[nName] = 0;
 85001     sqlite3FuncDefInsert(&db->aFunc, pBest);
 85004   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
 85005     return pBest;
 85007   return 0;
 85010 /*
 85011 ** Free all resources held by the schema structure. The void* argument points
 85012 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 
 85013 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
 85014 ** of the schema hash tables).
 85015 **
 85016 ** The Schema.cache_size variable is not cleared.
 85017 */
 85018 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
 85019   Hash temp1;
 85020   Hash temp2;
 85021   HashElem *pElem;
 85022   Schema *pSchema = (Schema *)p;
 85024   temp1 = pSchema->tblHash;
 85025   temp2 = pSchema->trigHash;
 85026   sqlite3HashInit(&pSchema->trigHash);
 85027   sqlite3HashClear(&pSchema->idxHash);
 85028   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
 85029     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
 85031   sqlite3HashClear(&temp2);
 85032   sqlite3HashInit(&pSchema->tblHash);
 85033   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
 85034     Table *pTab = sqliteHashData(pElem);
 85035     sqlite3DeleteTable(0, pTab);
 85037   sqlite3HashClear(&temp1);
 85038   sqlite3HashClear(&pSchema->fkeyHash);
 85039   pSchema->pSeqTab = 0;
 85040   if( pSchema->flags & DB_SchemaLoaded ){
 85041     pSchema->iGeneration++;
 85042     pSchema->flags &= ~DB_SchemaLoaded;
 85046 /*
 85047 ** Find and return the schema associated with a BTree.  Create
 85048 ** a new one if necessary.
 85049 */
 85050 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
 85051   Schema * p;
 85052   if( pBt ){
 85053     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
 85054   }else{
 85055     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
 85057   if( !p ){
 85058     db->mallocFailed = 1;
 85059   }else if ( 0==p->file_format ){
 85060     sqlite3HashInit(&p->tblHash);
 85061     sqlite3HashInit(&p->idxHash);
 85062     sqlite3HashInit(&p->trigHash);
 85063     sqlite3HashInit(&p->fkeyHash);
 85064     p->enc = SQLITE_UTF8;
 85066   return p;
 85069 /************** End of callback.c ********************************************/
 85070 /************** Begin file delete.c ******************************************/
 85071 /*
 85072 ** 2001 September 15
 85073 **
 85074 ** The author disclaims copyright to this source code.  In place of
 85075 ** a legal notice, here is a blessing:
 85076 **
 85077 **    May you do good and not evil.
 85078 **    May you find forgiveness for yourself and forgive others.
 85079 **    May you share freely, never taking more than you give.
 85080 **
 85081 *************************************************************************
 85082 ** This file contains C code routines that are called by the parser
 85083 ** in order to generate code for DELETE FROM statements.
 85084 */
 85086 /*
 85087 ** While a SrcList can in general represent multiple tables and subqueries
 85088 ** (as in the FROM clause of a SELECT statement) in this case it contains
 85089 ** the name of a single table, as one might find in an INSERT, DELETE,
 85090 ** or UPDATE statement.  Look up that table in the symbol table and
 85091 ** return a pointer.  Set an error message and return NULL if the table 
 85092 ** name is not found or if any other error occurs.
 85093 **
 85094 ** The following fields are initialized appropriate in pSrc:
 85095 **
 85096 **    pSrc->a[0].pTab       Pointer to the Table object
 85097 **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
 85098 **
 85099 */
 85100 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
 85101   struct SrcList_item *pItem = pSrc->a;
 85102   Table *pTab;
 85103   assert( pItem && pSrc->nSrc==1 );
 85104   pTab = sqlite3LocateTableItem(pParse, 0, pItem);
 85105   sqlite3DeleteTable(pParse->db, pItem->pTab);
 85106   pItem->pTab = pTab;
 85107   if( pTab ){
 85108     pTab->nRef++;
 85110   if( sqlite3IndexedByLookup(pParse, pItem) ){
 85111     pTab = 0;
 85113   return pTab;
 85116 /*
 85117 ** Check to make sure the given table is writable.  If it is not
 85118 ** writable, generate an error message and return 1.  If it is
 85119 ** writable return 0;
 85120 */
 85121 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
 85122   /* A table is not writable under the following circumstances:
 85123   **
 85124   **   1) It is a virtual table and no implementation of the xUpdate method
 85125   **      has been provided, or
 85126   **   2) It is a system table (i.e. sqlite_master), this call is not
 85127   **      part of a nested parse and writable_schema pragma has not 
 85128   **      been specified.
 85129   **
 85130   ** In either case leave an error message in pParse and return non-zero.
 85131   */
 85132   if( ( IsVirtual(pTab) 
 85133      && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
 85134    || ( (pTab->tabFlags & TF_Readonly)!=0
 85135      && (pParse->db->flags & SQLITE_WriteSchema)==0
 85136      && pParse->nested==0 )
 85137   ){
 85138     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
 85139     return 1;
 85142 #ifndef SQLITE_OMIT_VIEW
 85143   if( !viewOk && pTab->pSelect ){
 85144     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
 85145     return 1;
 85147 #endif
 85148   return 0;
 85152 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 85153 /*
 85154 ** Evaluate a view and store its result in an ephemeral table.  The
 85155 ** pWhere argument is an optional WHERE clause that restricts the
 85156 ** set of rows in the view that are to be added to the ephemeral table.
 85157 */
 85158 SQLITE_PRIVATE void sqlite3MaterializeView(
 85159   Parse *pParse,       /* Parsing context */
 85160   Table *pView,        /* View definition */
 85161   Expr *pWhere,        /* Optional WHERE clause to be added */
 85162   int iCur             /* Cursor number for ephemerial table */
 85163 ){
 85164   SelectDest dest;
 85165   Select *pDup;
 85166   sqlite3 *db = pParse->db;
 85168   pDup = sqlite3SelectDup(db, pView->pSelect, 0);
 85169   if( pWhere ){
 85170     SrcList *pFrom;
 85172     pWhere = sqlite3ExprDup(db, pWhere, 0);
 85173     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
 85174     if( pFrom ){
 85175       assert( pFrom->nSrc==1 );
 85176       pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
 85177       pFrom->a[0].pSelect = pDup;
 85178       assert( pFrom->a[0].pOn==0 );
 85179       assert( pFrom->a[0].pUsing==0 );
 85180     }else{
 85181       sqlite3SelectDelete(db, pDup);
 85183     pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
 85184     if( pDup ) pDup->selFlags |= SF_Materialize;
 85186   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
 85187   sqlite3Select(pParse, pDup, &dest);
 85188   sqlite3SelectDelete(db, pDup);
 85190 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
 85192 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 85193 /*
 85194 ** Generate an expression tree to implement the WHERE, ORDER BY,
 85195 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
 85196 **
 85197 **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
 85198 **                            \__________________________/
 85199 **                               pLimitWhere (pInClause)
 85200 */
 85201 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
 85202   Parse *pParse,               /* The parser context */
 85203   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
 85204   Expr *pWhere,                /* The WHERE clause.  May be null */
 85205   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
 85206   Expr *pLimit,                /* The LIMIT clause.  May be null */
 85207   Expr *pOffset,               /* The OFFSET clause.  May be null */
 85208   char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
 85209 ){
 85210   Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
 85211   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
 85212   Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
 85213   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
 85214   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
 85215   Select *pSelect = NULL;      /* Complete SELECT tree */
 85217   /* Check that there isn't an ORDER BY without a LIMIT clause.
 85218   */
 85219   if( pOrderBy && (pLimit == 0) ) {
 85220     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
 85221     goto limit_where_cleanup_2;
 85224   /* We only need to generate a select expression if there
 85225   ** is a limit/offset term to enforce.
 85226   */
 85227   if( pLimit == 0 ) {
 85228     /* if pLimit is null, pOffset will always be null as well. */
 85229     assert( pOffset == 0 );
 85230     return pWhere;
 85233   /* Generate a select expression tree to enforce the limit/offset 
 85234   ** term for the DELETE or UPDATE statement.  For example:
 85235   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
 85236   ** becomes:
 85237   **   DELETE FROM table_a WHERE rowid IN ( 
 85238   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
 85239   **   );
 85240   */
 85242   pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
 85243   if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
 85244   pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
 85245   if( pEList == 0 ) goto limit_where_cleanup_2;
 85247   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
 85248   ** and the SELECT subtree. */
 85249   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
 85250   if( pSelectSrc == 0 ) {
 85251     sqlite3ExprListDelete(pParse->db, pEList);
 85252     goto limit_where_cleanup_2;
 85255   /* generate the SELECT expression tree. */
 85256   pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
 85257                              pOrderBy,0,pLimit,pOffset);
 85258   if( pSelect == 0 ) return 0;
 85260   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
 85261   pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
 85262   if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
 85263   pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
 85264   if( pInClause == 0 ) goto limit_where_cleanup_1;
 85266   pInClause->x.pSelect = pSelect;
 85267   pInClause->flags |= EP_xIsSelect;
 85268   sqlite3ExprSetHeight(pParse, pInClause);
 85269   return pInClause;
 85271   /* something went wrong. clean up anything allocated. */
 85272 limit_where_cleanup_1:
 85273   sqlite3SelectDelete(pParse->db, pSelect);
 85274   return 0;
 85276 limit_where_cleanup_2:
 85277   sqlite3ExprDelete(pParse->db, pWhere);
 85278   sqlite3ExprListDelete(pParse->db, pOrderBy);
 85279   sqlite3ExprDelete(pParse->db, pLimit);
 85280   sqlite3ExprDelete(pParse->db, pOffset);
 85281   return 0;
 85283 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
 85285 /*
 85286 ** Generate code for a DELETE FROM statement.
 85287 **
 85288 **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
 85289 **                 \________/       \________________/
 85290 **                  pTabList              pWhere
 85291 */
 85292 SQLITE_PRIVATE void sqlite3DeleteFrom(
 85293   Parse *pParse,         /* The parser context */
 85294   SrcList *pTabList,     /* The table from which we should delete things */
 85295   Expr *pWhere           /* The WHERE clause.  May be null */
 85296 ){
 85297   Vdbe *v;               /* The virtual database engine */
 85298   Table *pTab;           /* The table from which records will be deleted */
 85299   const char *zDb;       /* Name of database holding pTab */
 85300   int end, addr = 0;     /* A couple addresses of generated code */
 85301   int i;                 /* Loop counter */
 85302   WhereInfo *pWInfo;     /* Information about the WHERE clause */
 85303   Index *pIdx;           /* For looping over indices of the table */
 85304   int iCur;              /* VDBE Cursor number for pTab */
 85305   sqlite3 *db;           /* Main database structure */
 85306   AuthContext sContext;  /* Authorization context */
 85307   NameContext sNC;       /* Name context to resolve expressions in */
 85308   int iDb;               /* Database number */
 85309   int memCnt = -1;       /* Memory cell used for change counting */
 85310   int rcauth;            /* Value returned by authorization callback */
 85312 #ifndef SQLITE_OMIT_TRIGGER
 85313   int isView;                  /* True if attempting to delete from a view */
 85314   Trigger *pTrigger;           /* List of table triggers, if required */
 85315 #endif
 85317   memset(&sContext, 0, sizeof(sContext));
 85318   db = pParse->db;
 85319   if( pParse->nErr || db->mallocFailed ){
 85320     goto delete_from_cleanup;
 85322   assert( pTabList->nSrc==1 );
 85324   /* Locate the table which we want to delete.  This table has to be
 85325   ** put in an SrcList structure because some of the subroutines we
 85326   ** will be calling are designed to work with multiple tables and expect
 85327   ** an SrcList* parameter instead of just a Table* parameter.
 85328   */
 85329   pTab = sqlite3SrcListLookup(pParse, pTabList);
 85330   if( pTab==0 )  goto delete_from_cleanup;
 85332   /* Figure out if we have any triggers and if the table being
 85333   ** deleted from is a view
 85334   */
 85335 #ifndef SQLITE_OMIT_TRIGGER
 85336   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 85337   isView = pTab->pSelect!=0;
 85338 #else
 85339 # define pTrigger 0
 85340 # define isView 0
 85341 #endif
 85342 #ifdef SQLITE_OMIT_VIEW
 85343 # undef isView
 85344 # define isView 0
 85345 #endif
 85347   /* If pTab is really a view, make sure it has been initialized.
 85348   */
 85349   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 85350     goto delete_from_cleanup;
 85353   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
 85354     goto delete_from_cleanup;
 85356   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 85357   assert( iDb<db->nDb );
 85358   zDb = db->aDb[iDb].zName;
 85359   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
 85360   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
 85361   if( rcauth==SQLITE_DENY ){
 85362     goto delete_from_cleanup;
 85364   assert(!isView || pTrigger);
 85366   /* Assign  cursor number to the table and all its indices.
 85367   */
 85368   assert( pTabList->nSrc==1 );
 85369   iCur = pTabList->a[0].iCursor = pParse->nTab++;
 85370   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 85371     pParse->nTab++;
 85374   /* Start the view context
 85375   */
 85376   if( isView ){
 85377     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
 85380   /* Begin generating code.
 85381   */
 85382   v = sqlite3GetVdbe(pParse);
 85383   if( v==0 ){
 85384     goto delete_from_cleanup;
 85386   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 85387   sqlite3BeginWriteOperation(pParse, 1, iDb);
 85389   /* If we are trying to delete from a view, realize that view into
 85390   ** a ephemeral table.
 85391   */
 85392 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 85393   if( isView ){
 85394     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
 85396 #endif
 85398   /* Resolve the column names in the WHERE clause.
 85399   */
 85400   memset(&sNC, 0, sizeof(sNC));
 85401   sNC.pParse = pParse;
 85402   sNC.pSrcList = pTabList;
 85403   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
 85404     goto delete_from_cleanup;
 85407   /* Initialize the counter of the number of rows deleted, if
 85408   ** we are counting rows.
 85409   */
 85410   if( db->flags & SQLITE_CountRows ){
 85411     memCnt = ++pParse->nMem;
 85412     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
 85415 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 85416   /* Special case: A DELETE without a WHERE clause deletes everything.
 85417   ** It is easier just to erase the whole table. Prior to version 3.6.5,
 85418   ** this optimization caused the row change count (the value returned by 
 85419   ** API function sqlite3_count_changes) to be set incorrectly.  */
 85420   if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
 85421    && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
 85422   ){
 85423     assert( !isView );
 85424     sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
 85425                       pTab->zName, P4_STATIC);
 85426     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 85427       assert( pIdx->pSchema==pTab->pSchema );
 85428       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
 85430   }else
 85431 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
 85432   /* The usual case: There is a WHERE clause so we have to scan through
 85433   ** the table and pick which records to delete.
 85434   */
 85436     int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
 85437     int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
 85438     int regRowid;                   /* Actual register containing rowids */
 85440     /* Collect rowids of every row to be deleted.
 85441     */
 85442     sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
 85443     pWInfo = sqlite3WhereBegin(
 85444         pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
 85445     );
 85446     if( pWInfo==0 ) goto delete_from_cleanup;
 85447     regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
 85448     sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
 85449     if( db->flags & SQLITE_CountRows ){
 85450       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
 85452     sqlite3WhereEnd(pWInfo);
 85454     /* Delete every item whose key was written to the list during the
 85455     ** database scan.  We have to delete items after the scan is complete
 85456     ** because deleting an item can change the scan order.  */
 85457     end = sqlite3VdbeMakeLabel(v);
 85459     /* Unless this is a view, open cursors for the table we are 
 85460     ** deleting from and all its indices. If this is a view, then the
 85461     ** only effect this statement has is to fire the INSTEAD OF 
 85462     ** triggers.  */
 85463     if( !isView ){
 85464       sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
 85467     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
 85469     /* Delete the row */
 85470 #ifndef SQLITE_OMIT_VIRTUALTABLE
 85471     if( IsVirtual(pTab) ){
 85472       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 85473       sqlite3VtabMakeWritable(pParse, pTab);
 85474       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
 85475       sqlite3VdbeChangeP5(v, OE_Abort);
 85476       sqlite3MayAbort(pParse);
 85477     }else
 85478 #endif
 85480       int count = (pParse->nested==0);    /* True to count changes */
 85481       sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
 85484     /* End of the delete loop */
 85485     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
 85486     sqlite3VdbeResolveLabel(v, end);
 85488     /* Close the cursors open on the table and its indexes. */
 85489     if( !isView && !IsVirtual(pTab) ){
 85490       for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
 85491         sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
 85493       sqlite3VdbeAddOp1(v, OP_Close, iCur);
 85497   /* Update the sqlite_sequence table by storing the content of the
 85498   ** maximum rowid counter values recorded while inserting into
 85499   ** autoincrement tables.
 85500   */
 85501   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 85502     sqlite3AutoincrementEnd(pParse);
 85505   /* Return the number of rows that were deleted. If this routine is 
 85506   ** generating code because of a call to sqlite3NestedParse(), do not
 85507   ** invoke the callback function.
 85508   */
 85509   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
 85510     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
 85511     sqlite3VdbeSetNumCols(v, 1);
 85512     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
 85515 delete_from_cleanup:
 85516   sqlite3AuthContextPop(&sContext);
 85517   sqlite3SrcListDelete(db, pTabList);
 85518   sqlite3ExprDelete(db, pWhere);
 85519   return;
 85521 /* Make sure "isView" and other macros defined above are undefined. Otherwise
 85522 ** thely may interfere with compilation of other functions in this file
 85523 ** (or in another file, if this file becomes part of the amalgamation).  */
 85524 #ifdef isView
 85525  #undef isView
 85526 #endif
 85527 #ifdef pTrigger
 85528  #undef pTrigger
 85529 #endif
 85531 /*
 85532 ** This routine generates VDBE code that causes a single row of a
 85533 ** single table to be deleted.
 85534 **
 85535 ** The VDBE must be in a particular state when this routine is called.
 85536 ** These are the requirements:
 85537 **
 85538 **   1.  A read/write cursor pointing to pTab, the table containing the row
 85539 **       to be deleted, must be opened as cursor number $iCur.
 85540 **
 85541 **   2.  Read/write cursors for all indices of pTab must be open as
 85542 **       cursor number base+i for the i-th index.
 85543 **
 85544 **   3.  The record number of the row to be deleted must be stored in
 85545 **       memory cell iRowid.
 85546 **
 85547 ** This routine generates code to remove both the table record and all 
 85548 ** index entries that point to that record.
 85549 */
 85550 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 85551   Parse *pParse,     /* Parsing context */
 85552   Table *pTab,       /* Table containing the row to be deleted */
 85553   int iCur,          /* Cursor number for the table */
 85554   int iRowid,        /* Memory cell that contains the rowid to delete */
 85555   int count,         /* If non-zero, increment the row change counter */
 85556   Trigger *pTrigger, /* List of triggers to (potentially) fire */
 85557   int onconf         /* Default ON CONFLICT policy for triggers */
 85558 ){
 85559   Vdbe *v = pParse->pVdbe;        /* Vdbe */
 85560   int iOld = 0;                   /* First register in OLD.* array */
 85561   int iLabel;                     /* Label resolved to end of generated code */
 85563   /* Vdbe is guaranteed to have been allocated by this stage. */
 85564   assert( v );
 85566   /* Seek cursor iCur to the row to delete. If this row no longer exists 
 85567   ** (this can happen if a trigger program has already deleted it), do
 85568   ** not attempt to delete it or fire any DELETE triggers.  */
 85569   iLabel = sqlite3VdbeMakeLabel(v);
 85570   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
 85572   /* If there are any triggers to fire, allocate a range of registers to
 85573   ** use for the old.* references in the triggers.  */
 85574   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
 85575     u32 mask;                     /* Mask of OLD.* columns in use */
 85576     int iCol;                     /* Iterator used while populating OLD.* */
 85578     /* TODO: Could use temporary registers here. Also could attempt to
 85579     ** avoid copying the contents of the rowid register.  */
 85580     mask = sqlite3TriggerColmask(
 85581         pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
 85582     );
 85583     mask |= sqlite3FkOldmask(pParse, pTab);
 85584     iOld = pParse->nMem+1;
 85585     pParse->nMem += (1 + pTab->nCol);
 85587     /* Populate the OLD.* pseudo-table register array. These values will be 
 85588     ** used by any BEFORE and AFTER triggers that exist.  */
 85589     sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
 85590     for(iCol=0; iCol<pTab->nCol; iCol++){
 85591       if( mask==0xffffffff || mask&(1<<iCol) ){
 85592         sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
 85596     /* Invoke BEFORE DELETE trigger programs. */
 85597     sqlite3CodeRowTrigger(pParse, pTrigger, 
 85598         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
 85599     );
 85601     /* Seek the cursor to the row to be deleted again. It may be that
 85602     ** the BEFORE triggers coded above have already removed the row
 85603     ** being deleted. Do not attempt to delete the row a second time, and 
 85604     ** do not fire AFTER triggers.  */
 85605     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
 85607     /* Do FK processing. This call checks that any FK constraints that
 85608     ** refer to this table (i.e. constraints attached to other tables) 
 85609     ** are not violated by deleting this row.  */
 85610     sqlite3FkCheck(pParse, pTab, iOld, 0);
 85613   /* Delete the index and table entries. Skip this step if pTab is really
 85614   ** a view (in which case the only effect of the DELETE statement is to
 85615   ** fire the INSTEAD OF triggers).  */ 
 85616   if( pTab->pSelect==0 ){
 85617     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
 85618     sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
 85619     if( count ){
 85620       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 85624   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
 85625   ** handle rows (possibly in other tables) that refer via a foreign key
 85626   ** to the row just deleted. */ 
 85627   sqlite3FkActions(pParse, pTab, 0, iOld);
 85629   /* Invoke AFTER DELETE trigger programs. */
 85630   sqlite3CodeRowTrigger(pParse, pTrigger, 
 85631       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
 85632   );
 85634   /* Jump here if the row had already been deleted before any BEFORE
 85635   ** trigger programs were invoked. Or if a trigger program throws a 
 85636   ** RAISE(IGNORE) exception.  */
 85637   sqlite3VdbeResolveLabel(v, iLabel);
 85640 /*
 85641 ** This routine generates VDBE code that causes the deletion of all
 85642 ** index entries associated with a single row of a single table.
 85643 **
 85644 ** The VDBE must be in a particular state when this routine is called.
 85645 ** These are the requirements:
 85646 **
 85647 **   1.  A read/write cursor pointing to pTab, the table containing the row
 85648 **       to be deleted, must be opened as cursor number "iCur".
 85649 **
 85650 **   2.  Read/write cursors for all indices of pTab must be open as
 85651 **       cursor number iCur+i for the i-th index.
 85652 **
 85653 **   3.  The "iCur" cursor must be pointing to the row that is to be
 85654 **       deleted.
 85655 */
 85656 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
 85657   Parse *pParse,     /* Parsing and code generating context */
 85658   Table *pTab,       /* Table containing the row to be deleted */
 85659   int iCur,          /* Cursor number for the table */
 85660   int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
 85661 ){
 85662   int i;
 85663   Index *pIdx;
 85664   int r1;
 85666   for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
 85667     if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
 85668     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
 85669     sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
 85673 /*
 85674 ** Generate code that will assemble an index key and put it in register
 85675 ** regOut.  The key with be for index pIdx which is an index on pTab.
 85676 ** iCur is the index of a cursor open on the pTab table and pointing to
 85677 ** the entry that needs indexing.
 85678 **
 85679 ** Return a register number which is the first in a block of
 85680 ** registers that holds the elements of the index key.  The
 85681 ** block of registers has already been deallocated by the time
 85682 ** this routine returns.
 85683 */
 85684 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
 85685   Parse *pParse,     /* Parsing context */
 85686   Index *pIdx,       /* The index for which to generate a key */
 85687   int iCur,          /* Cursor number for the pIdx->pTable table */
 85688   int regOut,        /* Write the new index key to this register */
 85689   int doMakeRec      /* Run the OP_MakeRecord instruction if true */
 85690 ){
 85691   Vdbe *v = pParse->pVdbe;
 85692   int j;
 85693   Table *pTab = pIdx->pTable;
 85694   int regBase;
 85695   int nCol;
 85697   nCol = pIdx->nColumn;
 85698   regBase = sqlite3GetTempRange(pParse, nCol+1);
 85699   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
 85700   for(j=0; j<nCol; j++){
 85701     int idx = pIdx->aiColumn[j];
 85702     if( idx==pTab->iPKey ){
 85703       sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
 85704     }else{
 85705       sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
 85706       sqlite3ColumnDefault(v, pTab, idx, -1);
 85709   if( doMakeRec ){
 85710     const char *zAff;
 85711     if( pTab->pSelect
 85712      || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
 85713     ){
 85714       zAff = 0;
 85715     }else{
 85716       zAff = sqlite3IndexAffinityStr(v, pIdx);
 85718     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
 85719     sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
 85721   sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
 85722   return regBase;
 85725 /************** End of delete.c **********************************************/
 85726 /************** Begin file func.c ********************************************/
 85727 /*
 85728 ** 2002 February 23
 85729 **
 85730 ** The author disclaims copyright to this source code.  In place of
 85731 ** a legal notice, here is a blessing:
 85732 **
 85733 **    May you do good and not evil.
 85734 **    May you find forgiveness for yourself and forgive others.
 85735 **    May you share freely, never taking more than you give.
 85736 **
 85737 *************************************************************************
 85738 ** This file contains the C functions that implement various SQL
 85739 ** functions of SQLite.  
 85740 **
 85741 ** There is only one exported symbol in this file - the function
 85742 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
 85743 ** All other code has file scope.
 85744 */
 85745 /* #include <stdlib.h> */
 85746 /* #include <assert.h> */
 85748 /*
 85749 ** Return the collating function associated with a function.
 85750 */
 85751 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
 85752   return context->pColl;
 85755 /*
 85756 ** Indicate that the accumulator load should be skipped on this
 85757 ** iteration of the aggregate loop.
 85758 */
 85759 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
 85760   context->skipFlag = 1;
 85763 /*
 85764 ** Implementation of the non-aggregate min() and max() functions
 85765 */
 85766 static void minmaxFunc(
 85767   sqlite3_context *context,
 85768   int argc,
 85769   sqlite3_value **argv
 85770 ){
 85771   int i;
 85772   int mask;    /* 0 for min() or 0xffffffff for max() */
 85773   int iBest;
 85774   CollSeq *pColl;
 85776   assert( argc>1 );
 85777   mask = sqlite3_user_data(context)==0 ? 0 : -1;
 85778   pColl = sqlite3GetFuncCollSeq(context);
 85779   assert( pColl );
 85780   assert( mask==-1 || mask==0 );
 85781   iBest = 0;
 85782   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 85783   for(i=1; i<argc; i++){
 85784     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
 85785     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
 85786       testcase( mask==0 );
 85787       iBest = i;
 85790   sqlite3_result_value(context, argv[iBest]);
 85793 /*
 85794 ** Return the type of the argument.
 85795 */
 85796 static void typeofFunc(
 85797   sqlite3_context *context,
 85798   int NotUsed,
 85799   sqlite3_value **argv
 85800 ){
 85801   const char *z = 0;
 85802   UNUSED_PARAMETER(NotUsed);
 85803   switch( sqlite3_value_type(argv[0]) ){
 85804     case SQLITE_INTEGER: z = "integer"; break;
 85805     case SQLITE_TEXT:    z = "text";    break;
 85806     case SQLITE_FLOAT:   z = "real";    break;
 85807     case SQLITE_BLOB:    z = "blob";    break;
 85808     default:             z = "null";    break;
 85810   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
 85814 /*
 85815 ** Implementation of the length() function
 85816 */
 85817 static void lengthFunc(
 85818   sqlite3_context *context,
 85819   int argc,
 85820   sqlite3_value **argv
 85821 ){
 85822   int len;
 85824   assert( argc==1 );
 85825   UNUSED_PARAMETER(argc);
 85826   switch( sqlite3_value_type(argv[0]) ){
 85827     case SQLITE_BLOB:
 85828     case SQLITE_INTEGER:
 85829     case SQLITE_FLOAT: {
 85830       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
 85831       break;
 85833     case SQLITE_TEXT: {
 85834       const unsigned char *z = sqlite3_value_text(argv[0]);
 85835       if( z==0 ) return;
 85836       len = 0;
 85837       while( *z ){
 85838         len++;
 85839         SQLITE_SKIP_UTF8(z);
 85841       sqlite3_result_int(context, len);
 85842       break;
 85844     default: {
 85845       sqlite3_result_null(context);
 85846       break;
 85851 /*
 85852 ** Implementation of the abs() function.
 85853 **
 85854 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
 85855 ** the numeric argument X. 
 85856 */
 85857 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 85858   assert( argc==1 );
 85859   UNUSED_PARAMETER(argc);
 85860   switch( sqlite3_value_type(argv[0]) ){
 85861     case SQLITE_INTEGER: {
 85862       i64 iVal = sqlite3_value_int64(argv[0]);
 85863       if( iVal<0 ){
 85864         if( (iVal<<1)==0 ){
 85865           /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
 85866           ** abs(X) throws an integer overflow error since there is no
 85867           ** equivalent positive 64-bit two complement value. */
 85868           sqlite3_result_error(context, "integer overflow", -1);
 85869           return;
 85871         iVal = -iVal;
 85873       sqlite3_result_int64(context, iVal);
 85874       break;
 85876     case SQLITE_NULL: {
 85877       /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
 85878       sqlite3_result_null(context);
 85879       break;
 85881     default: {
 85882       /* Because sqlite3_value_double() returns 0.0 if the argument is not
 85883       ** something that can be converted into a number, we have:
 85884       ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
 85885       ** cannot be converted to a numeric value. 
 85886       */
 85887       double rVal = sqlite3_value_double(argv[0]);
 85888       if( rVal<0 ) rVal = -rVal;
 85889       sqlite3_result_double(context, rVal);
 85890       break;
 85895 /*
 85896 ** Implementation of the instr() function.
 85897 **
 85898 ** instr(haystack,needle) finds the first occurrence of needle
 85899 ** in haystack and returns the number of previous characters plus 1,
 85900 ** or 0 if needle does not occur within haystack.
 85901 **
 85902 ** If both haystack and needle are BLOBs, then the result is one more than
 85903 ** the number of bytes in haystack prior to the first occurrence of needle,
 85904 ** or 0 if needle never occurs in haystack.
 85905 */
 85906 static void instrFunc(
 85907   sqlite3_context *context,
 85908   int argc,
 85909   sqlite3_value **argv
 85910 ){
 85911   const unsigned char *zHaystack;
 85912   const unsigned char *zNeedle;
 85913   int nHaystack;
 85914   int nNeedle;
 85915   int typeHaystack, typeNeedle;
 85916   int N = 1;
 85917   int isText;
 85919   UNUSED_PARAMETER(argc);
 85920   typeHaystack = sqlite3_value_type(argv[0]);
 85921   typeNeedle = sqlite3_value_type(argv[1]);
 85922   if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
 85923   nHaystack = sqlite3_value_bytes(argv[0]);
 85924   nNeedle = sqlite3_value_bytes(argv[1]);
 85925   if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
 85926     zHaystack = sqlite3_value_blob(argv[0]);
 85927     zNeedle = sqlite3_value_blob(argv[1]);
 85928     isText = 0;
 85929   }else{
 85930     zHaystack = sqlite3_value_text(argv[0]);
 85931     zNeedle = sqlite3_value_text(argv[1]);
 85932     isText = 1;
 85934   while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
 85935     N++;
 85936     do{
 85937       nHaystack--;
 85938       zHaystack++;
 85939     }while( isText && (zHaystack[0]&0xc0)==0x80 );
 85941   if( nNeedle>nHaystack ) N = 0;
 85942   sqlite3_result_int(context, N);
 85945 /*
 85946 ** Implementation of the substr() function.
 85947 **
 85948 ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
 85949 ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
 85950 ** of x.  If x is text, then we actually count UTF-8 characters.
 85951 ** If x is a blob, then we count bytes.
 85952 **
 85953 ** If p1 is negative, then we begin abs(p1) from the end of x[].
 85954 **
 85955 ** If p2 is negative, return the p2 characters preceeding p1.
 85956 */
 85957 static void substrFunc(
 85958   sqlite3_context *context,
 85959   int argc,
 85960   sqlite3_value **argv
 85961 ){
 85962   const unsigned char *z;
 85963   const unsigned char *z2;
 85964   int len;
 85965   int p0type;
 85966   i64 p1, p2;
 85967   int negP2 = 0;
 85969   assert( argc==3 || argc==2 );
 85970   if( sqlite3_value_type(argv[1])==SQLITE_NULL
 85971    || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
 85972   ){
 85973     return;
 85975   p0type = sqlite3_value_type(argv[0]);
 85976   p1 = sqlite3_value_int(argv[1]);
 85977   if( p0type==SQLITE_BLOB ){
 85978     len = sqlite3_value_bytes(argv[0]);
 85979     z = sqlite3_value_blob(argv[0]);
 85980     if( z==0 ) return;
 85981     assert( len==sqlite3_value_bytes(argv[0]) );
 85982   }else{
 85983     z = sqlite3_value_text(argv[0]);
 85984     if( z==0 ) return;
 85985     len = 0;
 85986     if( p1<0 ){
 85987       for(z2=z; *z2; len++){
 85988         SQLITE_SKIP_UTF8(z2);
 85992   if( argc==3 ){
 85993     p2 = sqlite3_value_int(argv[2]);
 85994     if( p2<0 ){
 85995       p2 = -p2;
 85996       negP2 = 1;
 85998   }else{
 85999     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
 86001   if( p1<0 ){
 86002     p1 += len;
 86003     if( p1<0 ){
 86004       p2 += p1;
 86005       if( p2<0 ) p2 = 0;
 86006       p1 = 0;
 86008   }else if( p1>0 ){
 86009     p1--;
 86010   }else if( p2>0 ){
 86011     p2--;
 86013   if( negP2 ){
 86014     p1 -= p2;
 86015     if( p1<0 ){
 86016       p2 += p1;
 86017       p1 = 0;
 86020   assert( p1>=0 && p2>=0 );
 86021   if( p0type!=SQLITE_BLOB ){
 86022     while( *z && p1 ){
 86023       SQLITE_SKIP_UTF8(z);
 86024       p1--;
 86026     for(z2=z; *z2 && p2; p2--){
 86027       SQLITE_SKIP_UTF8(z2);
 86029     sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
 86030   }else{
 86031     if( p1+p2>len ){
 86032       p2 = len-p1;
 86033       if( p2<0 ) p2 = 0;
 86035     sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
 86039 /*
 86040 ** Implementation of the round() function
 86041 */
 86042 #ifndef SQLITE_OMIT_FLOATING_POINT
 86043 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 86044   int n = 0;
 86045   double r;
 86046   char *zBuf;
 86047   assert( argc==1 || argc==2 );
 86048   if( argc==2 ){
 86049     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
 86050     n = sqlite3_value_int(argv[1]);
 86051     if( n>30 ) n = 30;
 86052     if( n<0 ) n = 0;
 86054   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 86055   r = sqlite3_value_double(argv[0]);
 86056   /* If Y==0 and X will fit in a 64-bit int,
 86057   ** handle the rounding directly,
 86058   ** otherwise use printf.
 86059   */
 86060   if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
 86061     r = (double)((sqlite_int64)(r+0.5));
 86062   }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
 86063     r = -(double)((sqlite_int64)((-r)+0.5));
 86064   }else{
 86065     zBuf = sqlite3_mprintf("%.*f",n,r);
 86066     if( zBuf==0 ){
 86067       sqlite3_result_error_nomem(context);
 86068       return;
 86070     sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
 86071     sqlite3_free(zBuf);
 86073   sqlite3_result_double(context, r);
 86075 #endif
 86077 /*
 86078 ** Allocate nByte bytes of space using sqlite3_malloc(). If the
 86079 ** allocation fails, call sqlite3_result_error_nomem() to notify
 86080 ** the database handle that malloc() has failed and return NULL.
 86081 ** If nByte is larger than the maximum string or blob length, then
 86082 ** raise an SQLITE_TOOBIG exception and return NULL.
 86083 */
 86084 static void *contextMalloc(sqlite3_context *context, i64 nByte){
 86085   char *z;
 86086   sqlite3 *db = sqlite3_context_db_handle(context);
 86087   assert( nByte>0 );
 86088   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
 86089   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 86090   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 86091     sqlite3_result_error_toobig(context);
 86092     z = 0;
 86093   }else{
 86094     z = sqlite3Malloc((int)nByte);
 86095     if( !z ){
 86096       sqlite3_result_error_nomem(context);
 86099   return z;
 86102 /*
 86103 ** Implementation of the upper() and lower() SQL functions.
 86104 */
 86105 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 86106   char *z1;
 86107   const char *z2;
 86108   int i, n;
 86109   UNUSED_PARAMETER(argc);
 86110   z2 = (char*)sqlite3_value_text(argv[0]);
 86111   n = sqlite3_value_bytes(argv[0]);
 86112   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 86113   assert( z2==(char*)sqlite3_value_text(argv[0]) );
 86114   if( z2 ){
 86115     z1 = contextMalloc(context, ((i64)n)+1);
 86116     if( z1 ){
 86117       for(i=0; i<n; i++){
 86118         z1[i] = (char)sqlite3Toupper(z2[i]);
 86120       sqlite3_result_text(context, z1, n, sqlite3_free);
 86124 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 86125   char *z1;
 86126   const char *z2;
 86127   int i, n;
 86128   UNUSED_PARAMETER(argc);
 86129   z2 = (char*)sqlite3_value_text(argv[0]);
 86130   n = sqlite3_value_bytes(argv[0]);
 86131   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 86132   assert( z2==(char*)sqlite3_value_text(argv[0]) );
 86133   if( z2 ){
 86134     z1 = contextMalloc(context, ((i64)n)+1);
 86135     if( z1 ){
 86136       for(i=0; i<n; i++){
 86137         z1[i] = sqlite3Tolower(z2[i]);
 86139       sqlite3_result_text(context, z1, n, sqlite3_free);
 86144 /*
 86145 ** The COALESCE() and IFNULL() functions are implemented as VDBE code so
 86146 ** that unused argument values do not have to be computed.  However, we
 86147 ** still need some kind of function implementation for this routines in
 86148 ** the function table.  That function implementation will never be called
 86149 ** so it doesn't matter what the implementation is.  We might as well use
 86150 ** the "version()" function as a substitute.
 86151 */
 86152 #define ifnullFunc versionFunc   /* Substitute function - never called */
 86154 /*
 86155 ** Implementation of random().  Return a random integer.  
 86156 */
 86157 static void randomFunc(
 86158   sqlite3_context *context,
 86159   int NotUsed,
 86160   sqlite3_value **NotUsed2
 86161 ){
 86162   sqlite_int64 r;
 86163   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 86164   sqlite3_randomness(sizeof(r), &r);
 86165   if( r<0 ){
 86166     /* We need to prevent a random number of 0x8000000000000000 
 86167     ** (or -9223372036854775808) since when you do abs() of that
 86168     ** number of you get the same value back again.  To do this
 86169     ** in a way that is testable, mask the sign bit off of negative
 86170     ** values, resulting in a positive value.  Then take the 
 86171     ** 2s complement of that positive value.  The end result can
 86172     ** therefore be no less than -9223372036854775807.
 86173     */
 86174     r = -(r & LARGEST_INT64);
 86176   sqlite3_result_int64(context, r);
 86179 /*
 86180 ** Implementation of randomblob(N).  Return a random blob
 86181 ** that is N bytes long.
 86182 */
 86183 static void randomBlob(
 86184   sqlite3_context *context,
 86185   int argc,
 86186   sqlite3_value **argv
 86187 ){
 86188   int n;
 86189   unsigned char *p;
 86190   assert( argc==1 );
 86191   UNUSED_PARAMETER(argc);
 86192   n = sqlite3_value_int(argv[0]);
 86193   if( n<1 ){
 86194     n = 1;
 86196   p = contextMalloc(context, n);
 86197   if( p ){
 86198     sqlite3_randomness(n, p);
 86199     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
 86203 /*
 86204 ** Implementation of the last_insert_rowid() SQL function.  The return
 86205 ** value is the same as the sqlite3_last_insert_rowid() API function.
 86206 */
 86207 static void last_insert_rowid(
 86208   sqlite3_context *context, 
 86209   int NotUsed, 
 86210   sqlite3_value **NotUsed2
 86211 ){
 86212   sqlite3 *db = sqlite3_context_db_handle(context);
 86213   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 86214   /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
 86215   ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
 86216   ** function. */
 86217   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
 86220 /*
 86221 ** Implementation of the changes() SQL function.
 86222 **
 86223 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
 86224 ** around the sqlite3_changes() C/C++ function and hence follows the same
 86225 ** rules for counting changes.
 86226 */
 86227 static void changes(
 86228   sqlite3_context *context,
 86229   int NotUsed,
 86230   sqlite3_value **NotUsed2
 86231 ){
 86232   sqlite3 *db = sqlite3_context_db_handle(context);
 86233   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 86234   sqlite3_result_int(context, sqlite3_changes(db));
 86237 /*
 86238 ** Implementation of the total_changes() SQL function.  The return value is
 86239 ** the same as the sqlite3_total_changes() API function.
 86240 */
 86241 static void total_changes(
 86242   sqlite3_context *context,
 86243   int NotUsed,
 86244   sqlite3_value **NotUsed2
 86245 ){
 86246   sqlite3 *db = sqlite3_context_db_handle(context);
 86247   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 86248   /* IMP: R-52756-41993 This function is a wrapper around the
 86249   ** sqlite3_total_changes() C/C++ interface. */
 86250   sqlite3_result_int(context, sqlite3_total_changes(db));
 86253 /*
 86254 ** A structure defining how to do GLOB-style comparisons.
 86255 */
 86256 struct compareInfo {
 86257   u8 matchAll;
 86258   u8 matchOne;
 86259   u8 matchSet;
 86260   u8 noCase;
 86261 };
 86263 /*
 86264 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
 86265 ** character is exactly one byte in size.  Also, all characters are
 86266 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
 86267 ** whereas only characters less than 0x80 do in ASCII.
 86268 */
 86269 #if defined(SQLITE_EBCDIC)
 86270 # define sqlite3Utf8Read(A)    (*((*A)++))
 86271 # define GlogUpperToLower(A)   A = sqlite3UpperToLower[A]
 86272 #else
 86273 # define GlogUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
 86274 #endif
 86276 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
 86277 /* The correct SQL-92 behavior is for the LIKE operator to ignore
 86278 ** case.  Thus  'a' LIKE 'A' would be true. */
 86279 static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
 86280 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
 86281 ** is case sensitive causing 'a' LIKE 'A' to be false */
 86282 static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
 86284 /*
 86285 ** Compare two UTF-8 strings for equality where the first string can
 86286 ** potentially be a "glob" expression.  Return true (1) if they
 86287 ** are the same and false (0) if they are different.
 86288 **
 86289 ** Globbing rules:
 86290 **
 86291 **      '*'       Matches any sequence of zero or more characters.
 86292 **
 86293 **      '?'       Matches exactly one character.
 86294 **
 86295 **     [...]      Matches one character from the enclosed list of
 86296 **                characters.
 86297 **
 86298 **     [^...]     Matches one character not in the enclosed list.
 86299 **
 86300 ** With the [...] and [^...] matching, a ']' character can be included
 86301 ** in the list by making it the first character after '[' or '^'.  A
 86302 ** range of characters can be specified using '-'.  Example:
 86303 ** "[a-z]" matches any single lower-case letter.  To match a '-', make
 86304 ** it the last character in the list.
 86305 **
 86306 ** This routine is usually quick, but can be N**2 in the worst case.
 86307 **
 86308 ** Hints: to match '*' or '?', put them in "[]".  Like this:
 86309 **
 86310 **         abc[*]xyz        Matches "abc*xyz" only
 86311 */
 86312 static int patternCompare(
 86313   const u8 *zPattern,              /* The glob pattern */
 86314   const u8 *zString,               /* The string to compare against the glob */
 86315   const struct compareInfo *pInfo, /* Information about how to do the compare */
 86316   u32 esc                          /* The escape character */
 86317 ){
 86318   u32 c, c2;
 86319   int invert;
 86320   int seen;
 86321   u8 matchOne = pInfo->matchOne;
 86322   u8 matchAll = pInfo->matchAll;
 86323   u8 matchSet = pInfo->matchSet;
 86324   u8 noCase = pInfo->noCase; 
 86325   int prevEscape = 0;     /* True if the previous character was 'escape' */
 86327   while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
 86328     if( c==matchAll && !prevEscape ){
 86329       while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
 86330                || c == matchOne ){
 86331         if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
 86332           return 0;
 86335       if( c==0 ){
 86336         return 1;
 86337       }else if( c==esc ){
 86338         c = sqlite3Utf8Read(&zPattern);
 86339         if( c==0 ){
 86340           return 0;
 86342       }else if( c==matchSet ){
 86343         assert( esc==0 );         /* This is GLOB, not LIKE */
 86344         assert( matchSet<0x80 );  /* '[' is a single-byte character */
 86345         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
 86346           SQLITE_SKIP_UTF8(zString);
 86348         return *zString!=0;
 86350       while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
 86351         if( noCase ){
 86352           GlogUpperToLower(c2);
 86353           GlogUpperToLower(c);
 86354           while( c2 != 0 && c2 != c ){
 86355             c2 = sqlite3Utf8Read(&zString);
 86356             GlogUpperToLower(c2);
 86358         }else{
 86359           while( c2 != 0 && c2 != c ){
 86360             c2 = sqlite3Utf8Read(&zString);
 86363         if( c2==0 ) return 0;
 86364         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
 86366       return 0;
 86367     }else if( c==matchOne && !prevEscape ){
 86368       if( sqlite3Utf8Read(&zString)==0 ){
 86369         return 0;
 86371     }else if( c==matchSet ){
 86372       u32 prior_c = 0;
 86373       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
 86374       seen = 0;
 86375       invert = 0;
 86376       c = sqlite3Utf8Read(&zString);
 86377       if( c==0 ) return 0;
 86378       c2 = sqlite3Utf8Read(&zPattern);
 86379       if( c2=='^' ){
 86380         invert = 1;
 86381         c2 = sqlite3Utf8Read(&zPattern);
 86383       if( c2==']' ){
 86384         if( c==']' ) seen = 1;
 86385         c2 = sqlite3Utf8Read(&zPattern);
 86387       while( c2 && c2!=']' ){
 86388         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
 86389           c2 = sqlite3Utf8Read(&zPattern);
 86390           if( c>=prior_c && c<=c2 ) seen = 1;
 86391           prior_c = 0;
 86392         }else{
 86393           if( c==c2 ){
 86394             seen = 1;
 86396           prior_c = c2;
 86398         c2 = sqlite3Utf8Read(&zPattern);
 86400       if( c2==0 || (seen ^ invert)==0 ){
 86401         return 0;
 86403     }else if( esc==c && !prevEscape ){
 86404       prevEscape = 1;
 86405     }else{
 86406       c2 = sqlite3Utf8Read(&zString);
 86407       if( noCase ){
 86408         GlogUpperToLower(c);
 86409         GlogUpperToLower(c2);
 86411       if( c!=c2 ){
 86412         return 0;
 86414       prevEscape = 0;
 86417   return *zString==0;
 86420 /*
 86421 ** Count the number of times that the LIKE operator (or GLOB which is
 86422 ** just a variation of LIKE) gets called.  This is used for testing
 86423 ** only.
 86424 */
 86425 #ifdef SQLITE_TEST
 86426 SQLITE_API int sqlite3_like_count = 0;
 86427 #endif
 86430 /*
 86431 ** Implementation of the like() SQL function.  This function implements
 86432 ** the build-in LIKE operator.  The first argument to the function is the
 86433 ** pattern and the second argument is the string.  So, the SQL statements:
 86434 **
 86435 **       A LIKE B
 86436 **
 86437 ** is implemented as like(B,A).
 86438 **
 86439 ** This same function (with a different compareInfo structure) computes
 86440 ** the GLOB operator.
 86441 */
 86442 static void likeFunc(
 86443   sqlite3_context *context, 
 86444   int argc, 
 86445   sqlite3_value **argv
 86446 ){
 86447   const unsigned char *zA, *zB;
 86448   u32 escape = 0;
 86449   int nPat;
 86450   sqlite3 *db = sqlite3_context_db_handle(context);
 86452   zB = sqlite3_value_text(argv[0]);
 86453   zA = sqlite3_value_text(argv[1]);
 86455   /* Limit the length of the LIKE or GLOB pattern to avoid problems
 86456   ** of deep recursion and N*N behavior in patternCompare().
 86457   */
 86458   nPat = sqlite3_value_bytes(argv[0]);
 86459   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
 86460   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
 86461   if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
 86462     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
 86463     return;
 86465   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
 86467   if( argc==3 ){
 86468     /* The escape character string must consist of a single UTF-8 character.
 86469     ** Otherwise, return an error.
 86470     */
 86471     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
 86472     if( zEsc==0 ) return;
 86473     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
 86474       sqlite3_result_error(context, 
 86475           "ESCAPE expression must be a single character", -1);
 86476       return;
 86478     escape = sqlite3Utf8Read(&zEsc);
 86480   if( zA && zB ){
 86481     struct compareInfo *pInfo = sqlite3_user_data(context);
 86482 #ifdef SQLITE_TEST
 86483     sqlite3_like_count++;
 86484 #endif
 86486     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
 86490 /*
 86491 ** Implementation of the NULLIF(x,y) function.  The result is the first
 86492 ** argument if the arguments are different.  The result is NULL if the
 86493 ** arguments are equal to each other.
 86494 */
 86495 static void nullifFunc(
 86496   sqlite3_context *context,
 86497   int NotUsed,
 86498   sqlite3_value **argv
 86499 ){
 86500   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
 86501   UNUSED_PARAMETER(NotUsed);
 86502   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
 86503     sqlite3_result_value(context, argv[0]);
 86507 /*
 86508 ** Implementation of the sqlite_version() function.  The result is the version
 86509 ** of the SQLite library that is running.
 86510 */
 86511 static void versionFunc(
 86512   sqlite3_context *context,
 86513   int NotUsed,
 86514   sqlite3_value **NotUsed2
 86515 ){
 86516   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 86517   /* IMP: R-48699-48617 This function is an SQL wrapper around the
 86518   ** sqlite3_libversion() C-interface. */
 86519   sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
 86522 /*
 86523 ** Implementation of the sqlite_source_id() function. The result is a string
 86524 ** that identifies the particular version of the source code used to build
 86525 ** SQLite.
 86526 */
 86527 static void sourceidFunc(
 86528   sqlite3_context *context,
 86529   int NotUsed,
 86530   sqlite3_value **NotUsed2
 86531 ){
 86532   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 86533   /* IMP: R-24470-31136 This function is an SQL wrapper around the
 86534   ** sqlite3_sourceid() C interface. */
 86535   sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
 86538 /*
 86539 ** Implementation of the sqlite_log() function.  This is a wrapper around
 86540 ** sqlite3_log().  The return value is NULL.  The function exists purely for
 86541 ** its side-effects.
 86542 */
 86543 static void errlogFunc(
 86544   sqlite3_context *context,
 86545   int argc,
 86546   sqlite3_value **argv
 86547 ){
 86548   UNUSED_PARAMETER(argc);
 86549   UNUSED_PARAMETER(context);
 86550   sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
 86553 /*
 86554 ** Implementation of the sqlite_compileoption_used() function.
 86555 ** The result is an integer that identifies if the compiler option
 86556 ** was used to build SQLite.
 86557 */
 86558 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 86559 static void compileoptionusedFunc(
 86560   sqlite3_context *context,
 86561   int argc,
 86562   sqlite3_value **argv
 86563 ){
 86564   const char *zOptName;
 86565   assert( argc==1 );
 86566   UNUSED_PARAMETER(argc);
 86567   /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
 86568   ** function is a wrapper around the sqlite3_compileoption_used() C/C++
 86569   ** function.
 86570   */
 86571   if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
 86572     sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
 86575 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 86577 /*
 86578 ** Implementation of the sqlite_compileoption_get() function. 
 86579 ** The result is a string that identifies the compiler options 
 86580 ** used to build SQLite.
 86581 */
 86582 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 86583 static void compileoptiongetFunc(
 86584   sqlite3_context *context,
 86585   int argc,
 86586   sqlite3_value **argv
 86587 ){
 86588   int n;
 86589   assert( argc==1 );
 86590   UNUSED_PARAMETER(argc);
 86591   /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
 86592   ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
 86593   */
 86594   n = sqlite3_value_int(argv[0]);
 86595   sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
 86597 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 86599 /* Array for converting from half-bytes (nybbles) into ASCII hex
 86600 ** digits. */
 86601 static const char hexdigits[] = {
 86602   '0', '1', '2', '3', '4', '5', '6', '7',
 86603   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
 86604 };
 86606 /*
 86607 ** EXPERIMENTAL - This is not an official function.  The interface may
 86608 ** change.  This function may disappear.  Do not write code that depends
 86609 ** on this function.
 86610 **
 86611 ** Implementation of the QUOTE() function.  This function takes a single
 86612 ** argument.  If the argument is numeric, the return value is the same as
 86613 ** the argument.  If the argument is NULL, the return value is the string
 86614 ** "NULL".  Otherwise, the argument is enclosed in single quotes with
 86615 ** single-quote escapes.
 86616 */
 86617 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 86618   assert( argc==1 );
 86619   UNUSED_PARAMETER(argc);
 86620   switch( sqlite3_value_type(argv[0]) ){
 86621     case SQLITE_FLOAT: {
 86622       double r1, r2;
 86623       char zBuf[50];
 86624       r1 = sqlite3_value_double(argv[0]);
 86625       sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
 86626       sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
 86627       if( r1!=r2 ){
 86628         sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
 86630       sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 86631       break;
 86633     case SQLITE_INTEGER: {
 86634       sqlite3_result_value(context, argv[0]);
 86635       break;
 86637     case SQLITE_BLOB: {
 86638       char *zText = 0;
 86639       char const *zBlob = sqlite3_value_blob(argv[0]);
 86640       int nBlob = sqlite3_value_bytes(argv[0]);
 86641       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
 86642       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
 86643       if( zText ){
 86644         int i;
 86645         for(i=0; i<nBlob; i++){
 86646           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
 86647           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
 86649         zText[(nBlob*2)+2] = '\'';
 86650         zText[(nBlob*2)+3] = '\0';
 86651         zText[0] = 'X';
 86652         zText[1] = '\'';
 86653         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
 86654         sqlite3_free(zText);
 86656       break;
 86658     case SQLITE_TEXT: {
 86659       int i,j;
 86660       u64 n;
 86661       const unsigned char *zArg = sqlite3_value_text(argv[0]);
 86662       char *z;
 86664       if( zArg==0 ) return;
 86665       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
 86666       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
 86667       if( z ){
 86668         z[0] = '\'';
 86669         for(i=0, j=1; zArg[i]; i++){
 86670           z[j++] = zArg[i];
 86671           if( zArg[i]=='\'' ){
 86672             z[j++] = '\'';
 86675         z[j++] = '\'';
 86676         z[j] = 0;
 86677         sqlite3_result_text(context, z, j, sqlite3_free);
 86679       break;
 86681     default: {
 86682       assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
 86683       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
 86684       break;
 86689 /*
 86690 ** The hex() function.  Interpret the argument as a blob.  Return
 86691 ** a hexadecimal rendering as text.
 86692 */
 86693 static void hexFunc(
 86694   sqlite3_context *context,
 86695   int argc,
 86696   sqlite3_value **argv
 86697 ){
 86698   int i, n;
 86699   const unsigned char *pBlob;
 86700   char *zHex, *z;
 86701   assert( argc==1 );
 86702   UNUSED_PARAMETER(argc);
 86703   pBlob = sqlite3_value_blob(argv[0]);
 86704   n = sqlite3_value_bytes(argv[0]);
 86705   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
 86706   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
 86707   if( zHex ){
 86708     for(i=0; i<n; i++, pBlob++){
 86709       unsigned char c = *pBlob;
 86710       *(z++) = hexdigits[(c>>4)&0xf];
 86711       *(z++) = hexdigits[c&0xf];
 86713     *z = 0;
 86714     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
 86718 /*
 86719 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
 86720 */
 86721 static void zeroblobFunc(
 86722   sqlite3_context *context,
 86723   int argc,
 86724   sqlite3_value **argv
 86725 ){
 86726   i64 n;
 86727   sqlite3 *db = sqlite3_context_db_handle(context);
 86728   assert( argc==1 );
 86729   UNUSED_PARAMETER(argc);
 86730   n = sqlite3_value_int64(argv[0]);
 86731   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
 86732   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 86733   if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 86734     sqlite3_result_error_toobig(context);
 86735   }else{
 86736     sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
 86740 /*
 86741 ** The replace() function.  Three arguments are all strings: call
 86742 ** them A, B, and C. The result is also a string which is derived
 86743 ** from A by replacing every occurance of B with C.  The match
 86744 ** must be exact.  Collating sequences are not used.
 86745 */
 86746 static void replaceFunc(
 86747   sqlite3_context *context,
 86748   int argc,
 86749   sqlite3_value **argv
 86750 ){
 86751   const unsigned char *zStr;        /* The input string A */
 86752   const unsigned char *zPattern;    /* The pattern string B */
 86753   const unsigned char *zRep;        /* The replacement string C */
 86754   unsigned char *zOut;              /* The output */
 86755   int nStr;                /* Size of zStr */
 86756   int nPattern;            /* Size of zPattern */
 86757   int nRep;                /* Size of zRep */
 86758   i64 nOut;                /* Maximum size of zOut */
 86759   int loopLimit;           /* Last zStr[] that might match zPattern[] */
 86760   int i, j;                /* Loop counters */
 86762   assert( argc==3 );
 86763   UNUSED_PARAMETER(argc);
 86764   zStr = sqlite3_value_text(argv[0]);
 86765   if( zStr==0 ) return;
 86766   nStr = sqlite3_value_bytes(argv[0]);
 86767   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
 86768   zPattern = sqlite3_value_text(argv[1]);
 86769   if( zPattern==0 ){
 86770     assert( sqlite3_value_type(argv[1])==SQLITE_NULL
 86771             || sqlite3_context_db_handle(context)->mallocFailed );
 86772     return;
 86774   if( zPattern[0]==0 ){
 86775     assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
 86776     sqlite3_result_value(context, argv[0]);
 86777     return;
 86779   nPattern = sqlite3_value_bytes(argv[1]);
 86780   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
 86781   zRep = sqlite3_value_text(argv[2]);
 86782   if( zRep==0 ) return;
 86783   nRep = sqlite3_value_bytes(argv[2]);
 86784   assert( zRep==sqlite3_value_text(argv[2]) );
 86785   nOut = nStr + 1;
 86786   assert( nOut<SQLITE_MAX_LENGTH );
 86787   zOut = contextMalloc(context, (i64)nOut);
 86788   if( zOut==0 ){
 86789     return;
 86791   loopLimit = nStr - nPattern;  
 86792   for(i=j=0; i<=loopLimit; i++){
 86793     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
 86794       zOut[j++] = zStr[i];
 86795     }else{
 86796       u8 *zOld;
 86797       sqlite3 *db = sqlite3_context_db_handle(context);
 86798       nOut += nRep - nPattern;
 86799       testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
 86800       testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
 86801       if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 86802         sqlite3_result_error_toobig(context);
 86803         sqlite3_free(zOut);
 86804         return;
 86806       zOld = zOut;
 86807       zOut = sqlite3_realloc(zOut, (int)nOut);
 86808       if( zOut==0 ){
 86809         sqlite3_result_error_nomem(context);
 86810         sqlite3_free(zOld);
 86811         return;
 86813       memcpy(&zOut[j], zRep, nRep);
 86814       j += nRep;
 86815       i += nPattern-1;
 86818   assert( j+nStr-i+1==nOut );
 86819   memcpy(&zOut[j], &zStr[i], nStr-i);
 86820   j += nStr - i;
 86821   assert( j<=nOut );
 86822   zOut[j] = 0;
 86823   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
 86826 /*
 86827 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
 86828 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
 86829 */
 86830 static void trimFunc(
 86831   sqlite3_context *context,
 86832   int argc,
 86833   sqlite3_value **argv
 86834 ){
 86835   const unsigned char *zIn;         /* Input string */
 86836   const unsigned char *zCharSet;    /* Set of characters to trim */
 86837   int nIn;                          /* Number of bytes in input */
 86838   int flags;                        /* 1: trimleft  2: trimright  3: trim */
 86839   int i;                            /* Loop counter */
 86840   unsigned char *aLen = 0;          /* Length of each character in zCharSet */
 86841   unsigned char **azChar = 0;       /* Individual characters in zCharSet */
 86842   int nChar;                        /* Number of characters in zCharSet */
 86844   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
 86845     return;
 86847   zIn = sqlite3_value_text(argv[0]);
 86848   if( zIn==0 ) return;
 86849   nIn = sqlite3_value_bytes(argv[0]);
 86850   assert( zIn==sqlite3_value_text(argv[0]) );
 86851   if( argc==1 ){
 86852     static const unsigned char lenOne[] = { 1 };
 86853     static unsigned char * const azOne[] = { (u8*)" " };
 86854     nChar = 1;
 86855     aLen = (u8*)lenOne;
 86856     azChar = (unsigned char **)azOne;
 86857     zCharSet = 0;
 86858   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
 86859     return;
 86860   }else{
 86861     const unsigned char *z;
 86862     for(z=zCharSet, nChar=0; *z; nChar++){
 86863       SQLITE_SKIP_UTF8(z);
 86865     if( nChar>0 ){
 86866       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
 86867       if( azChar==0 ){
 86868         return;
 86870       aLen = (unsigned char*)&azChar[nChar];
 86871       for(z=zCharSet, nChar=0; *z; nChar++){
 86872         azChar[nChar] = (unsigned char *)z;
 86873         SQLITE_SKIP_UTF8(z);
 86874         aLen[nChar] = (u8)(z - azChar[nChar]);
 86878   if( nChar>0 ){
 86879     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
 86880     if( flags & 1 ){
 86881       while( nIn>0 ){
 86882         int len = 0;
 86883         for(i=0; i<nChar; i++){
 86884           len = aLen[i];
 86885           if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
 86887         if( i>=nChar ) break;
 86888         zIn += len;
 86889         nIn -= len;
 86892     if( flags & 2 ){
 86893       while( nIn>0 ){
 86894         int len = 0;
 86895         for(i=0; i<nChar; i++){
 86896           len = aLen[i];
 86897           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
 86899         if( i>=nChar ) break;
 86900         nIn -= len;
 86903     if( zCharSet ){
 86904       sqlite3_free(azChar);
 86907   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
 86911 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
 86912 ** is only available if the SQLITE_SOUNDEX compile-time option is used
 86913 ** when SQLite is built.
 86914 */
 86915 #ifdef SQLITE_SOUNDEX
 86916 /*
 86917 ** Compute the soundex encoding of a word.
 86918 **
 86919 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
 86920 ** soundex encoding of the string X. 
 86921 */
 86922 static void soundexFunc(
 86923   sqlite3_context *context,
 86924   int argc,
 86925   sqlite3_value **argv
 86926 ){
 86927   char zResult[8];
 86928   const u8 *zIn;
 86929   int i, j;
 86930   static const unsigned char iCode[] = {
 86931     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 86932     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 86933     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 86934     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 86935     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
 86936     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
 86937     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
 86938     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
 86939   };
 86940   assert( argc==1 );
 86941   zIn = (u8*)sqlite3_value_text(argv[0]);
 86942   if( zIn==0 ) zIn = (u8*)"";
 86943   for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
 86944   if( zIn[i] ){
 86945     u8 prevcode = iCode[zIn[i]&0x7f];
 86946     zResult[0] = sqlite3Toupper(zIn[i]);
 86947     for(j=1; j<4 && zIn[i]; i++){
 86948       int code = iCode[zIn[i]&0x7f];
 86949       if( code>0 ){
 86950         if( code!=prevcode ){
 86951           prevcode = code;
 86952           zResult[j++] = code + '0';
 86954       }else{
 86955         prevcode = 0;
 86958     while( j<4 ){
 86959       zResult[j++] = '0';
 86961     zResult[j] = 0;
 86962     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
 86963   }else{
 86964     /* IMP: R-64894-50321 The string "?000" is returned if the argument
 86965     ** is NULL or contains no ASCII alphabetic characters. */
 86966     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
 86969 #endif /* SQLITE_SOUNDEX */
 86971 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 86972 /*
 86973 ** A function that loads a shared-library extension then returns NULL.
 86974 */
 86975 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
 86976   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
 86977   const char *zProc;
 86978   sqlite3 *db = sqlite3_context_db_handle(context);
 86979   char *zErrMsg = 0;
 86981   if( argc==2 ){
 86982     zProc = (const char *)sqlite3_value_text(argv[1]);
 86983   }else{
 86984     zProc = 0;
 86986   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
 86987     sqlite3_result_error(context, zErrMsg, -1);
 86988     sqlite3_free(zErrMsg);
 86991 #endif
 86994 /*
 86995 ** An instance of the following structure holds the context of a
 86996 ** sum() or avg() aggregate computation.
 86997 */
 86998 typedef struct SumCtx SumCtx;
 86999 struct SumCtx {
 87000   double rSum;      /* Floating point sum */
 87001   i64 iSum;         /* Integer sum */   
 87002   i64 cnt;          /* Number of elements summed */
 87003   u8 overflow;      /* True if integer overflow seen */
 87004   u8 approx;        /* True if non-integer value was input to the sum */
 87005 };
 87007 /*
 87008 ** Routines used to compute the sum, average, and total.
 87009 **
 87010 ** The SUM() function follows the (broken) SQL standard which means
 87011 ** that it returns NULL if it sums over no inputs.  TOTAL returns
 87012 ** 0.0 in that case.  In addition, TOTAL always returns a float where
 87013 ** SUM might return an integer if it never encounters a floating point
 87014 ** value.  TOTAL never fails, but SUM might through an exception if
 87015 ** it overflows an integer.
 87016 */
 87017 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
 87018   SumCtx *p;
 87019   int type;
 87020   assert( argc==1 );
 87021   UNUSED_PARAMETER(argc);
 87022   p = sqlite3_aggregate_context(context, sizeof(*p));
 87023   type = sqlite3_value_numeric_type(argv[0]);
 87024   if( p && type!=SQLITE_NULL ){
 87025     p->cnt++;
 87026     if( type==SQLITE_INTEGER ){
 87027       i64 v = sqlite3_value_int64(argv[0]);
 87028       p->rSum += v;
 87029       if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
 87030         p->overflow = 1;
 87032     }else{
 87033       p->rSum += sqlite3_value_double(argv[0]);
 87034       p->approx = 1;
 87038 static void sumFinalize(sqlite3_context *context){
 87039   SumCtx *p;
 87040   p = sqlite3_aggregate_context(context, 0);
 87041   if( p && p->cnt>0 ){
 87042     if( p->overflow ){
 87043       sqlite3_result_error(context,"integer overflow",-1);
 87044     }else if( p->approx ){
 87045       sqlite3_result_double(context, p->rSum);
 87046     }else{
 87047       sqlite3_result_int64(context, p->iSum);
 87051 static void avgFinalize(sqlite3_context *context){
 87052   SumCtx *p;
 87053   p = sqlite3_aggregate_context(context, 0);
 87054   if( p && p->cnt>0 ){
 87055     sqlite3_result_double(context, p->rSum/(double)p->cnt);
 87058 static void totalFinalize(sqlite3_context *context){
 87059   SumCtx *p;
 87060   p = sqlite3_aggregate_context(context, 0);
 87061   /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 87062   sqlite3_result_double(context, p ? p->rSum : (double)0);
 87065 /*
 87066 ** The following structure keeps track of state information for the
 87067 ** count() aggregate function.
 87068 */
 87069 typedef struct CountCtx CountCtx;
 87070 struct CountCtx {
 87071   i64 n;
 87072 };
 87074 /*
 87075 ** Routines to implement the count() aggregate function.
 87076 */
 87077 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
 87078   CountCtx *p;
 87079   p = sqlite3_aggregate_context(context, sizeof(*p));
 87080   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
 87081     p->n++;
 87084 #ifndef SQLITE_OMIT_DEPRECATED
 87085   /* The sqlite3_aggregate_count() function is deprecated.  But just to make
 87086   ** sure it still operates correctly, verify that its count agrees with our 
 87087   ** internal count when using count(*) and when the total count can be
 87088   ** expressed as a 32-bit integer. */
 87089   assert( argc==1 || p==0 || p->n>0x7fffffff
 87090           || p->n==sqlite3_aggregate_count(context) );
 87091 #endif
 87093 static void countFinalize(sqlite3_context *context){
 87094   CountCtx *p;
 87095   p = sqlite3_aggregate_context(context, 0);
 87096   sqlite3_result_int64(context, p ? p->n : 0);
 87099 /*
 87100 ** Routines to implement min() and max() aggregate functions.
 87101 */
 87102 static void minmaxStep(
 87103   sqlite3_context *context, 
 87104   int NotUsed, 
 87105   sqlite3_value **argv
 87106 ){
 87107   Mem *pArg  = (Mem *)argv[0];
 87108   Mem *pBest;
 87109   UNUSED_PARAMETER(NotUsed);
 87111   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
 87112   if( !pBest ) return;
 87114   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
 87115     if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
 87116   }else if( pBest->flags ){
 87117     int max;
 87118     int cmp;
 87119     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
 87120     /* This step function is used for both the min() and max() aggregates,
 87121     ** the only difference between the two being that the sense of the
 87122     ** comparison is inverted. For the max() aggregate, the
 87123     ** sqlite3_user_data() function returns (void *)-1. For min() it
 87124     ** returns (void *)db, where db is the sqlite3* database pointer.
 87125     ** Therefore the next statement sets variable 'max' to 1 for the max()
 87126     ** aggregate, or 0 for min().
 87127     */
 87128     max = sqlite3_user_data(context)!=0;
 87129     cmp = sqlite3MemCompare(pBest, pArg, pColl);
 87130     if( (max && cmp<0) || (!max && cmp>0) ){
 87131       sqlite3VdbeMemCopy(pBest, pArg);
 87132     }else{
 87133       sqlite3SkipAccumulatorLoad(context);
 87135   }else{
 87136     sqlite3VdbeMemCopy(pBest, pArg);
 87139 static void minMaxFinalize(sqlite3_context *context){
 87140   sqlite3_value *pRes;
 87141   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
 87142   if( pRes ){
 87143     if( pRes->flags ){
 87144       sqlite3_result_value(context, pRes);
 87146     sqlite3VdbeMemRelease(pRes);
 87150 /*
 87151 ** group_concat(EXPR, ?SEPARATOR?)
 87152 */
 87153 static void groupConcatStep(
 87154   sqlite3_context *context,
 87155   int argc,
 87156   sqlite3_value **argv
 87157 ){
 87158   const char *zVal;
 87159   StrAccum *pAccum;
 87160   const char *zSep;
 87161   int nVal, nSep;
 87162   assert( argc==1 || argc==2 );
 87163   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 87164   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
 87166   if( pAccum ){
 87167     sqlite3 *db = sqlite3_context_db_handle(context);
 87168     int firstTerm = pAccum->useMalloc==0;
 87169     pAccum->useMalloc = 2;
 87170     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
 87171     if( !firstTerm ){
 87172       if( argc==2 ){
 87173         zSep = (char*)sqlite3_value_text(argv[1]);
 87174         nSep = sqlite3_value_bytes(argv[1]);
 87175       }else{
 87176         zSep = ",";
 87177         nSep = 1;
 87179       sqlite3StrAccumAppend(pAccum, zSep, nSep);
 87181     zVal = (char*)sqlite3_value_text(argv[0]);
 87182     nVal = sqlite3_value_bytes(argv[0]);
 87183     sqlite3StrAccumAppend(pAccum, zVal, nVal);
 87186 static void groupConcatFinalize(sqlite3_context *context){
 87187   StrAccum *pAccum;
 87188   pAccum = sqlite3_aggregate_context(context, 0);
 87189   if( pAccum ){
 87190     if( pAccum->tooBig ){
 87191       sqlite3_result_error_toobig(context);
 87192     }else if( pAccum->mallocFailed ){
 87193       sqlite3_result_error_nomem(context);
 87194     }else{    
 87195       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
 87196                           sqlite3_free);
 87201 /*
 87202 ** This routine does per-connection function registration.  Most
 87203 ** of the built-in functions above are part of the global function set.
 87204 ** This routine only deals with those that are not global.
 87205 */
 87206 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
 87207   int rc = sqlite3_overload_function(db, "MATCH", 2);
 87208   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
 87209   if( rc==SQLITE_NOMEM ){
 87210     db->mallocFailed = 1;
 87214 /*
 87215 ** Set the LIKEOPT flag on the 2-argument function with the given name.
 87216 */
 87217 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
 87218   FuncDef *pDef;
 87219   pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
 87220                              2, SQLITE_UTF8, 0);
 87221   if( ALWAYS(pDef) ){
 87222     pDef->flags = flagVal;
 87226 /*
 87227 ** Register the built-in LIKE and GLOB functions.  The caseSensitive
 87228 ** parameter determines whether or not the LIKE operator is case
 87229 ** sensitive.  GLOB is always case sensitive.
 87230 */
 87231 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
 87232   struct compareInfo *pInfo;
 87233   if( caseSensitive ){
 87234     pInfo = (struct compareInfo*)&likeInfoAlt;
 87235   }else{
 87236     pInfo = (struct compareInfo*)&likeInfoNorm;
 87238   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
 87239   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
 87240   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
 87241       (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
 87242   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
 87243   setLikeOptFlag(db, "like", 
 87244       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
 87247 /*
 87248 ** pExpr points to an expression which implements a function.  If
 87249 ** it is appropriate to apply the LIKE optimization to that function
 87250 ** then set aWc[0] through aWc[2] to the wildcard characters and
 87251 ** return TRUE.  If the function is not a LIKE-style function then
 87252 ** return FALSE.
 87253 */
 87254 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
 87255   FuncDef *pDef;
 87256   if( pExpr->op!=TK_FUNCTION 
 87257    || !pExpr->x.pList 
 87258    || pExpr->x.pList->nExpr!=2
 87259   ){
 87260     return 0;
 87262   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 87263   pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
 87264                              sqlite3Strlen30(pExpr->u.zToken),
 87265                              2, SQLITE_UTF8, 0);
 87266   if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
 87267     return 0;
 87270   /* The memcpy() statement assumes that the wildcard characters are
 87271   ** the first three statements in the compareInfo structure.  The
 87272   ** asserts() that follow verify that assumption
 87273   */
 87274   memcpy(aWc, pDef->pUserData, 3);
 87275   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
 87276   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
 87277   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
 87278   *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
 87279   return 1;
 87282 /*
 87283 ** All all of the FuncDef structures in the aBuiltinFunc[] array above
 87284 ** to the global function hash table.  This occurs at start-time (as
 87285 ** a consequence of calling sqlite3_initialize()).
 87286 **
 87287 ** After this routine runs
 87288 */
 87289 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
 87290   /*
 87291   ** The following array holds FuncDef structures for all of the functions
 87292   ** defined in this file.
 87293   **
 87294   ** The array cannot be constant since changes are made to the
 87295   ** FuncDef.pHash elements at start-time.  The elements of this array
 87296   ** are read-only after initialization is complete.
 87297   */
 87298   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
 87299     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
 87300     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
 87301     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
 87302     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
 87303     FUNCTION(trim,               1, 3, 0, trimFunc         ),
 87304     FUNCTION(trim,               2, 3, 0, trimFunc         ),
 87305     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
 87306     FUNCTION(min,                0, 0, 1, 0                ),
 87307     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
 87308     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
 87309     FUNCTION(max,                0, 1, 1, 0                ),
 87310     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
 87311     FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
 87312     FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
 87313     FUNCTION(instr,              2, 0, 0, instrFunc        ),
 87314     FUNCTION(substr,             2, 0, 0, substrFunc       ),
 87315     FUNCTION(substr,             3, 0, 0, substrFunc       ),
 87316     FUNCTION(abs,                1, 0, 0, absFunc          ),
 87317 #ifndef SQLITE_OMIT_FLOATING_POINT
 87318     FUNCTION(round,              1, 0, 0, roundFunc        ),
 87319     FUNCTION(round,              2, 0, 0, roundFunc        ),
 87320 #endif
 87321     FUNCTION(upper,              1, 0, 0, upperFunc        ),
 87322     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
 87323     FUNCTION(coalesce,           1, 0, 0, 0                ),
 87324     FUNCTION(coalesce,           0, 0, 0, 0                ),
 87325     FUNCTION2(coalesce,         -1, 0, 0, ifnullFunc,  SQLITE_FUNC_COALESCE),
 87326     FUNCTION(hex,                1, 0, 0, hexFunc          ),
 87327     FUNCTION2(ifnull,            2, 0, 0, ifnullFunc,  SQLITE_FUNC_COALESCE),
 87328     FUNCTION(random,             0, 0, 0, randomFunc       ),
 87329     FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
 87330     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
 87331     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
 87332     FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
 87333     FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
 87334 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 87335     FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
 87336     FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
 87337 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 87338     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
 87339     FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
 87340     FUNCTION(changes,            0, 0, 0, changes          ),
 87341     FUNCTION(total_changes,      0, 0, 0, total_changes    ),
 87342     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
 87343     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
 87344   #ifdef SQLITE_SOUNDEX
 87345     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
 87346   #endif
 87347   #ifndef SQLITE_OMIT_LOAD_EXTENSION
 87348     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
 87349     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
 87350   #endif
 87351     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
 87352     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
 87353     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
 87354  /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
 87355     {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
 87356     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
 87357     AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
 87358     AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
 87360     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 87361   #ifdef SQLITE_CASE_SENSITIVE_LIKE
 87362     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 87363     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 87364   #else
 87365     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
 87366     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
 87367   #endif
 87368   };
 87370   int i;
 87371   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 87372   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
 87374   for(i=0; i<ArraySize(aBuiltinFunc); i++){
 87375     sqlite3FuncDefInsert(pHash, &aFunc[i]);
 87377   sqlite3RegisterDateTimeFunctions();
 87378 #ifndef SQLITE_OMIT_ALTERTABLE
 87379   sqlite3AlterFunctions();
 87380 #endif
 87383 /************** End of func.c ************************************************/
 87384 /************** Begin file fkey.c ********************************************/
 87385 /*
 87386 **
 87387 ** The author disclaims copyright to this source code.  In place of
 87388 ** a legal notice, here is a blessing:
 87389 **
 87390 **    May you do good and not evil.
 87391 **    May you find forgiveness for yourself and forgive others.
 87392 **    May you share freely, never taking more than you give.
 87393 **
 87394 *************************************************************************
 87395 ** This file contains code used by the compiler to add foreign key
 87396 ** support to compiled SQL statements.
 87397 */
 87399 #ifndef SQLITE_OMIT_FOREIGN_KEY
 87400 #ifndef SQLITE_OMIT_TRIGGER
 87402 /*
 87403 ** Deferred and Immediate FKs
 87404 ** --------------------------
 87405 **
 87406 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
 87407 ** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
 87408 ** is returned and the current statement transaction rolled back. If a 
 87409 ** deferred foreign key constraint is violated, no action is taken 
 87410 ** immediately. However if the application attempts to commit the 
 87411 ** transaction before fixing the constraint violation, the attempt fails.
 87412 **
 87413 ** Deferred constraints are implemented using a simple counter associated
 87414 ** with the database handle. The counter is set to zero each time a 
 87415 ** database transaction is opened. Each time a statement is executed 
 87416 ** that causes a foreign key violation, the counter is incremented. Each
 87417 ** time a statement is executed that removes an existing violation from
 87418 ** the database, the counter is decremented. When the transaction is
 87419 ** committed, the commit fails if the current value of the counter is
 87420 ** greater than zero. This scheme has two big drawbacks:
 87421 **
 87422 **   * When a commit fails due to a deferred foreign key constraint, 
 87423 **     there is no way to tell which foreign constraint is not satisfied,
 87424 **     or which row it is not satisfied for.
 87425 **
 87426 **   * If the database contains foreign key violations when the 
 87427 **     transaction is opened, this may cause the mechanism to malfunction.
 87428 **
 87429 ** Despite these problems, this approach is adopted as it seems simpler
 87430 ** than the alternatives.
 87431 **
 87432 ** INSERT operations:
 87433 **
 87434 **   I.1) For each FK for which the table is the child table, search
 87435 **        the parent table for a match. If none is found increment the
 87436 **        constraint counter.
 87437 **
 87438 **   I.2) For each FK for which the table is the parent table, 
 87439 **        search the child table for rows that correspond to the new
 87440 **        row in the parent table. Decrement the counter for each row
 87441 **        found (as the constraint is now satisfied).
 87442 **
 87443 ** DELETE operations:
 87444 **
 87445 **   D.1) For each FK for which the table is the child table, 
 87446 **        search the parent table for a row that corresponds to the 
 87447 **        deleted row in the child table. If such a row is not found, 
 87448 **        decrement the counter.
 87449 **
 87450 **   D.2) For each FK for which the table is the parent table, search 
 87451 **        the child table for rows that correspond to the deleted row 
 87452 **        in the parent table. For each found increment the counter.
 87453 **
 87454 ** UPDATE operations:
 87455 **
 87456 **   An UPDATE command requires that all 4 steps above are taken, but only
 87457 **   for FK constraints for which the affected columns are actually 
 87458 **   modified (values must be compared at runtime).
 87459 **
 87460 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
 87461 ** This simplifies the implementation a bit.
 87462 **
 87463 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
 87464 ** resolution is considered to delete rows before the new row is inserted.
 87465 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
 87466 ** is thrown, even if the FK constraint would be satisfied after the new 
 87467 ** row is inserted.
 87468 **
 87469 ** Immediate constraints are usually handled similarly. The only difference 
 87470 ** is that the counter used is stored as part of each individual statement
 87471 ** object (struct Vdbe). If, after the statement has run, its immediate
 87472 ** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
 87473 ** and the statement transaction is rolled back. An exception is an INSERT
 87474 ** statement that inserts a single row only (no triggers). In this case,
 87475 ** instead of using a counter, an exception is thrown immediately if the
 87476 ** INSERT violates a foreign key constraint. This is necessary as such
 87477 ** an INSERT does not open a statement transaction.
 87478 **
 87479 ** TODO: How should dropping a table be handled? How should renaming a 
 87480 ** table be handled?
 87481 **
 87482 **
 87483 ** Query API Notes
 87484 ** ---------------
 87485 **
 87486 ** Before coding an UPDATE or DELETE row operation, the code-generator
 87487 ** for those two operations needs to know whether or not the operation
 87488 ** requires any FK processing and, if so, which columns of the original
 87489 ** row are required by the FK processing VDBE code (i.e. if FKs were
 87490 ** implemented using triggers, which of the old.* columns would be 
 87491 ** accessed). No information is required by the code-generator before
 87492 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
 87493 ** generation code to query for this information are:
 87494 **
 87495 **   sqlite3FkRequired() - Test to see if FK processing is required.
 87496 **   sqlite3FkOldmask()  - Query for the set of required old.* columns.
 87497 **
 87498 **
 87499 ** Externally accessible module functions
 87500 ** --------------------------------------
 87501 **
 87502 **   sqlite3FkCheck()    - Check for foreign key violations.
 87503 **   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
 87504 **   sqlite3FkDelete()   - Delete an FKey structure.
 87505 */
 87507 /*
 87508 ** VDBE Calling Convention
 87509 ** -----------------------
 87510 **
 87511 ** Example:
 87512 **
 87513 **   For the following INSERT statement:
 87514 **
 87515 **     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
 87516 **     INSERT INTO t1 VALUES(1, 2, 3.1);
 87517 **
 87518 **   Register (x):        2    (type integer)
 87519 **   Register (x+1):      1    (type integer)
 87520 **   Register (x+2):      NULL (type NULL)
 87521 **   Register (x+3):      3.1  (type real)
 87522 */
 87524 /*
 87525 ** A foreign key constraint requires that the key columns in the parent
 87526 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
 87527 ** Given that pParent is the parent table for foreign key constraint pFKey, 
 87528 ** search the schema a unique index on the parent key columns. 
 87529 **
 87530 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
 87531 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
 87532 ** is set to point to the unique index. 
 87533 ** 
 87534 ** If the parent key consists of a single column (the foreign key constraint
 87535 ** is not a composite foreign key), output variable *paiCol is set to NULL.
 87536 ** Otherwise, it is set to point to an allocated array of size N, where
 87537 ** N is the number of columns in the parent key. The first element of the
 87538 ** array is the index of the child table column that is mapped by the FK
 87539 ** constraint to the parent table column stored in the left-most column
 87540 ** of index *ppIdx. The second element of the array is the index of the
 87541 ** child table column that corresponds to the second left-most column of
 87542 ** *ppIdx, and so on.
 87543 **
 87544 ** If the required index cannot be found, either because:
 87545 **
 87546 **   1) The named parent key columns do not exist, or
 87547 **
 87548 **   2) The named parent key columns do exist, but are not subject to a
 87549 **      UNIQUE or PRIMARY KEY constraint, or
 87550 **
 87551 **   3) No parent key columns were provided explicitly as part of the
 87552 **      foreign key definition, and the parent table does not have a
 87553 **      PRIMARY KEY, or
 87554 **
 87555 **   4) No parent key columns were provided explicitly as part of the
 87556 **      foreign key definition, and the PRIMARY KEY of the parent table 
 87557 **      consists of a a different number of columns to the child key in 
 87558 **      the child table.
 87559 **
 87560 ** then non-zero is returned, and a "foreign key mismatch" error loaded
 87561 ** into pParse. If an OOM error occurs, non-zero is returned and the
 87562 ** pParse->db->mallocFailed flag is set.
 87563 */
 87564 static int locateFkeyIndex(
 87565   Parse *pParse,                  /* Parse context to store any error in */
 87566   Table *pParent,                 /* Parent table of FK constraint pFKey */
 87567   FKey *pFKey,                    /* Foreign key to find index for */
 87568   Index **ppIdx,                  /* OUT: Unique index on parent table */
 87569   int **paiCol                    /* OUT: Map of index columns in pFKey */
 87570 ){
 87571   Index *pIdx = 0;                    /* Value to return via *ppIdx */
 87572   int *aiCol = 0;                     /* Value to return via *paiCol */
 87573   int nCol = pFKey->nCol;             /* Number of columns in parent key */
 87574   char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
 87576   /* The caller is responsible for zeroing output parameters. */
 87577   assert( ppIdx && *ppIdx==0 );
 87578   assert( !paiCol || *paiCol==0 );
 87579   assert( pParse );
 87581   /* If this is a non-composite (single column) foreign key, check if it 
 87582   ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx 
 87583   ** and *paiCol set to zero and return early. 
 87584   **
 87585   ** Otherwise, for a composite foreign key (more than one column), allocate
 87586   ** space for the aiCol array (returned via output parameter *paiCol).
 87587   ** Non-composite foreign keys do not require the aiCol array.
 87588   */
 87589   if( nCol==1 ){
 87590     /* The FK maps to the IPK if any of the following are true:
 87591     **
 87592     **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly 
 87593     **      mapped to the primary key of table pParent, or
 87594     **   2) The FK is explicitly mapped to a column declared as INTEGER
 87595     **      PRIMARY KEY.
 87596     */
 87597     if( pParent->iPKey>=0 ){
 87598       if( !zKey ) return 0;
 87599       if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
 87601   }else if( paiCol ){
 87602     assert( nCol>1 );
 87603     aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
 87604     if( !aiCol ) return 1;
 87605     *paiCol = aiCol;
 87608   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
 87609     if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
 87610       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
 87611       ** of columns. If each indexed column corresponds to a foreign key
 87612       ** column of pFKey, then this index is a winner.  */
 87614       if( zKey==0 ){
 87615         /* If zKey is NULL, then this foreign key is implicitly mapped to 
 87616         ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
 87617         ** identified by the test (Index.autoIndex==2).  */
 87618         if( pIdx->autoIndex==2 ){
 87619           if( aiCol ){
 87620             int i;
 87621             for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
 87623           break;
 87625       }else{
 87626         /* If zKey is non-NULL, then this foreign key was declared to
 87627         ** map to an explicit list of columns in table pParent. Check if this
 87628         ** index matches those columns. Also, check that the index uses
 87629         ** the default collation sequences for each column. */
 87630         int i, j;
 87631         for(i=0; i<nCol; i++){
 87632           int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
 87633           char *zDfltColl;                  /* Def. collation for column */
 87634           char *zIdxCol;                    /* Name of indexed column */
 87636           /* If the index uses a collation sequence that is different from
 87637           ** the default collation sequence for the column, this index is
 87638           ** unusable. Bail out early in this case.  */
 87639           zDfltColl = pParent->aCol[iCol].zColl;
 87640           if( !zDfltColl ){
 87641             zDfltColl = "BINARY";
 87643           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
 87645           zIdxCol = pParent->aCol[iCol].zName;
 87646           for(j=0; j<nCol; j++){
 87647             if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
 87648               if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
 87649               break;
 87652           if( j==nCol ) break;
 87654         if( i==nCol ) break;      /* pIdx is usable */
 87659   if( !pIdx ){
 87660     if( !pParse->disableTriggers ){
 87661       sqlite3ErrorMsg(pParse, "foreign key mismatch");
 87663     sqlite3DbFree(pParse->db, aiCol);
 87664     return 1;
 87667   *ppIdx = pIdx;
 87668   return 0;
 87671 /*
 87672 ** This function is called when a row is inserted into or deleted from the 
 87673 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed 
 87674 ** on the child table of pFKey, this function is invoked twice for each row
 87675 ** affected - once to "delete" the old row, and then again to "insert" the
 87676 ** new row.
 87677 **
 87678 ** Each time it is called, this function generates VDBE code to locate the
 87679 ** row in the parent table that corresponds to the row being inserted into 
 87680 ** or deleted from the child table. If the parent row can be found, no 
 87681 ** special action is taken. Otherwise, if the parent row can *not* be
 87682 ** found in the parent table:
 87683 **
 87684 **   Operation | FK type   | Action taken
 87685 **   --------------------------------------------------------------------------
 87686 **   INSERT      immediate   Increment the "immediate constraint counter".
 87687 **
 87688 **   DELETE      immediate   Decrement the "immediate constraint counter".
 87689 **
 87690 **   INSERT      deferred    Increment the "deferred constraint counter".
 87691 **
 87692 **   DELETE      deferred    Decrement the "deferred constraint counter".
 87693 **
 87694 ** These operations are identified in the comment at the top of this file 
 87695 ** (fkey.c) as "I.1" and "D.1".
 87696 */
 87697 static void fkLookupParent(
 87698   Parse *pParse,        /* Parse context */
 87699   int iDb,              /* Index of database housing pTab */
 87700   Table *pTab,          /* Parent table of FK pFKey */
 87701   Index *pIdx,          /* Unique index on parent key columns in pTab */
 87702   FKey *pFKey,          /* Foreign key constraint */
 87703   int *aiCol,           /* Map from parent key columns to child table columns */
 87704   int regData,          /* Address of array containing child table row */
 87705   int nIncr,            /* Increment constraint counter by this */
 87706   int isIgnore          /* If true, pretend pTab contains all NULL values */
 87707 ){
 87708   int i;                                    /* Iterator variable */
 87709   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
 87710   int iCur = pParse->nTab - 1;              /* Cursor number to use */
 87711   int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
 87713   /* If nIncr is less than zero, then check at runtime if there are any
 87714   ** outstanding constraints to resolve. If there are not, there is no need
 87715   ** to check if deleting this row resolves any outstanding violations.
 87716   **
 87717   ** Check if any of the key columns in the child table row are NULL. If 
 87718   ** any are, then the constraint is considered satisfied. No need to 
 87719   ** search for a matching row in the parent table.  */
 87720   if( nIncr<0 ){
 87721     sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
 87723   for(i=0; i<pFKey->nCol; i++){
 87724     int iReg = aiCol[i] + regData + 1;
 87725     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
 87728   if( isIgnore==0 ){
 87729     if( pIdx==0 ){
 87730       /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
 87731       ** column of the parent table (table pTab).  */
 87732       int iMustBeInt;               /* Address of MustBeInt instruction */
 87733       int regTemp = sqlite3GetTempReg(pParse);
 87735       /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
 87736       ** apply the affinity of the parent key). If this fails, then there
 87737       ** is no matching parent key. Before using MustBeInt, make a copy of
 87738       ** the value. Otherwise, the value inserted into the child key column
 87739       ** will have INTEGER affinity applied to it, which may not be correct.  */
 87740       sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
 87741       iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
 87743       /* If the parent table is the same as the child table, and we are about
 87744       ** to increment the constraint-counter (i.e. this is an INSERT operation),
 87745       ** then check if the row being inserted matches itself. If so, do not
 87746       ** increment the constraint-counter.  */
 87747       if( pTab==pFKey->pFrom && nIncr==1 ){
 87748         sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
 87751       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
 87752       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
 87753       sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 87754       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
 87755       sqlite3VdbeJumpHere(v, iMustBeInt);
 87756       sqlite3ReleaseTempReg(pParse, regTemp);
 87757     }else{
 87758       int nCol = pFKey->nCol;
 87759       int regTemp = sqlite3GetTempRange(pParse, nCol);
 87760       int regRec = sqlite3GetTempReg(pParse);
 87761       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 87763       sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
 87764       sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
 87765       for(i=0; i<nCol; i++){
 87766         sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
 87769       /* If the parent table is the same as the child table, and we are about
 87770       ** to increment the constraint-counter (i.e. this is an INSERT operation),
 87771       ** then check if the row being inserted matches itself. If so, do not
 87772       ** increment the constraint-counter. 
 87773       **
 87774       ** If any of the parent-key values are NULL, then the row cannot match 
 87775       ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
 87776       ** of the parent-key values are NULL (at this point it is known that
 87777       ** none of the child key values are).
 87778       */
 87779       if( pTab==pFKey->pFrom && nIncr==1 ){
 87780         int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
 87781         for(i=0; i<nCol; i++){
 87782           int iChild = aiCol[i]+1+regData;
 87783           int iParent = pIdx->aiColumn[i]+1+regData;
 87784           assert( aiCol[i]!=pTab->iPKey );
 87785           if( pIdx->aiColumn[i]==pTab->iPKey ){
 87786             /* The parent key is a composite key that includes the IPK column */
 87787             iParent = regData;
 87789           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
 87790           sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 87792         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 87795       sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
 87796       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
 87797       sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
 87799       sqlite3ReleaseTempReg(pParse, regRec);
 87800       sqlite3ReleaseTempRange(pParse, regTemp, nCol);
 87804   if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
 87805     /* Special case: If this is an INSERT statement that will insert exactly
 87806     ** one row into the table, raise a constraint immediately instead of
 87807     ** incrementing a counter. This is necessary as the VM code is being
 87808     ** generated for will not open a statement transaction.  */
 87809     assert( nIncr==1 );
 87810     sqlite3HaltConstraint(
 87811         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
 87812     );
 87813   }else{
 87814     if( nIncr>0 && pFKey->isDeferred==0 ){
 87815       sqlite3ParseToplevel(pParse)->mayAbort = 1;
 87817     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
 87820   sqlite3VdbeResolveLabel(v, iOk);
 87821   sqlite3VdbeAddOp1(v, OP_Close, iCur);
 87824 /*
 87825 ** This function is called to generate code executed when a row is deleted
 87826 ** from the parent table of foreign key constraint pFKey and, if pFKey is 
 87827 ** deferred, when a row is inserted into the same table. When generating
 87828 ** code for an SQL UPDATE operation, this function may be called twice -
 87829 ** once to "delete" the old row and once to "insert" the new row.
 87830 **
 87831 ** The code generated by this function scans through the rows in the child
 87832 ** table that correspond to the parent table row being deleted or inserted.
 87833 ** For each child row found, one of the following actions is taken:
 87834 **
 87835 **   Operation | FK type   | Action taken
 87836 **   --------------------------------------------------------------------------
 87837 **   DELETE      immediate   Increment the "immediate constraint counter".
 87838 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
 87839 **                           throw a "foreign key constraint failed" exception.
 87840 **
 87841 **   INSERT      immediate   Decrement the "immediate constraint counter".
 87842 **
 87843 **   DELETE      deferred    Increment the "deferred constraint counter".
 87844 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
 87845 **                           throw a "foreign key constraint failed" exception.
 87846 **
 87847 **   INSERT      deferred    Decrement the "deferred constraint counter".
 87848 **
 87849 ** These operations are identified in the comment at the top of this file 
 87850 ** (fkey.c) as "I.2" and "D.2".
 87851 */
 87852 static void fkScanChildren(
 87853   Parse *pParse,                  /* Parse context */
 87854   SrcList *pSrc,                  /* SrcList containing the table to scan */
 87855   Table *pTab,
 87856   Index *pIdx,                    /* Foreign key index */
 87857   FKey *pFKey,                    /* Foreign key relationship */
 87858   int *aiCol,                     /* Map from pIdx cols to child table cols */
 87859   int regData,                    /* Referenced table data starts here */
 87860   int nIncr                       /* Amount to increment deferred counter by */
 87861 ){
 87862   sqlite3 *db = pParse->db;       /* Database handle */
 87863   int i;                          /* Iterator variable */
 87864   Expr *pWhere = 0;               /* WHERE clause to scan with */
 87865   NameContext sNameContext;       /* Context used to resolve WHERE clause */
 87866   WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
 87867   int iFkIfZero = 0;              /* Address of OP_FkIfZero */
 87868   Vdbe *v = sqlite3GetVdbe(pParse);
 87870   assert( !pIdx || pIdx->pTable==pTab );
 87872   if( nIncr<0 ){
 87873     iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
 87876   /* Create an Expr object representing an SQL expression like:
 87877   **
 87878   **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
 87879   **
 87880   ** The collation sequence used for the comparison should be that of
 87881   ** the parent key columns. The affinity of the parent key column should
 87882   ** be applied to each child key value before the comparison takes place.
 87883   */
 87884   for(i=0; i<pFKey->nCol; i++){
 87885     Expr *pLeft;                  /* Value from parent table row */
 87886     Expr *pRight;                 /* Column ref to child table */
 87887     Expr *pEq;                    /* Expression (pLeft = pRight) */
 87888     int iCol;                     /* Index of column in child table */ 
 87889     const char *zCol;             /* Name of column in child table */
 87891     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
 87892     if( pLeft ){
 87893       /* Set the collation sequence and affinity of the LHS of each TK_EQ
 87894       ** expression to the parent key column defaults.  */
 87895       if( pIdx ){
 87896         Column *pCol;
 87897         const char *zColl;
 87898         iCol = pIdx->aiColumn[i];
 87899         pCol = &pTab->aCol[iCol];
 87900         if( pTab->iPKey==iCol ) iCol = -1;
 87901         pLeft->iTable = regData+iCol+1;
 87902         pLeft->affinity = pCol->affinity;
 87903         zColl = pCol->zColl;
 87904         if( zColl==0 ) zColl = db->pDfltColl->zName;
 87905         pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
 87906       }else{
 87907         pLeft->iTable = regData;
 87908         pLeft->affinity = SQLITE_AFF_INTEGER;
 87911     iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 87912     assert( iCol>=0 );
 87913     zCol = pFKey->pFrom->aCol[iCol].zName;
 87914     pRight = sqlite3Expr(db, TK_ID, zCol);
 87915     pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
 87916     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 87919   /* If the child table is the same as the parent table, and this scan
 87920   ** is taking place as part of a DELETE operation (operation D.2), omit the
 87921   ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
 87922   ** clause, where $rowid is the rowid of the row being deleted.  */
 87923   if( pTab==pFKey->pFrom && nIncr>0 ){
 87924     Expr *pEq;                    /* Expression (pLeft = pRight) */
 87925     Expr *pLeft;                  /* Value from parent table row */
 87926     Expr *pRight;                 /* Column ref to child table */
 87927     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
 87928     pRight = sqlite3Expr(db, TK_COLUMN, 0);
 87929     if( pLeft && pRight ){
 87930       pLeft->iTable = regData;
 87931       pLeft->affinity = SQLITE_AFF_INTEGER;
 87932       pRight->iTable = pSrc->a[0].iCursor;
 87933       pRight->iColumn = -1;
 87935     pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
 87936     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 87939   /* Resolve the references in the WHERE clause. */
 87940   memset(&sNameContext, 0, sizeof(NameContext));
 87941   sNameContext.pSrcList = pSrc;
 87942   sNameContext.pParse = pParse;
 87943   sqlite3ResolveExprNames(&sNameContext, pWhere);
 87945   /* Create VDBE to loop through the entries in pSrc that match the WHERE
 87946   ** clause. If the constraint is not deferred, throw an exception for
 87947   ** each row found. Otherwise, for deferred constraints, increment the
 87948   ** deferred constraint counter by nIncr for each row selected.  */
 87949   pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
 87950   if( nIncr>0 && pFKey->isDeferred==0 ){
 87951     sqlite3ParseToplevel(pParse)->mayAbort = 1;
 87953   sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
 87954   if( pWInfo ){
 87955     sqlite3WhereEnd(pWInfo);
 87958   /* Clean up the WHERE clause constructed above. */
 87959   sqlite3ExprDelete(db, pWhere);
 87960   if( iFkIfZero ){
 87961     sqlite3VdbeJumpHere(v, iFkIfZero);
 87965 /*
 87966 ** This function returns a pointer to the head of a linked list of FK
 87967 ** constraints for which table pTab is the parent table. For example,
 87968 ** given the following schema:
 87969 **
 87970 **   CREATE TABLE t1(a PRIMARY KEY);
 87971 **   CREATE TABLE t2(b REFERENCES t1(a);
 87972 **
 87973 ** Calling this function with table "t1" as an argument returns a pointer
 87974 ** to the FKey structure representing the foreign key constraint on table
 87975 ** "t2". Calling this function with "t2" as the argument would return a
 87976 ** NULL pointer (as there are no FK constraints for which t2 is the parent
 87977 ** table).
 87978 */
 87979 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
 87980   int nName = sqlite3Strlen30(pTab->zName);
 87981   return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
 87984 /*
 87985 ** The second argument is a Trigger structure allocated by the 
 87986 ** fkActionTrigger() routine. This function deletes the Trigger structure
 87987 ** and all of its sub-components.
 87988 **
 87989 ** The Trigger structure or any of its sub-components may be allocated from
 87990 ** the lookaside buffer belonging to database handle dbMem.
 87991 */
 87992 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
 87993   if( p ){
 87994     TriggerStep *pStep = p->step_list;
 87995     sqlite3ExprDelete(dbMem, pStep->pWhere);
 87996     sqlite3ExprListDelete(dbMem, pStep->pExprList);
 87997     sqlite3SelectDelete(dbMem, pStep->pSelect);
 87998     sqlite3ExprDelete(dbMem, p->pWhen);
 87999     sqlite3DbFree(dbMem, p);
 88003 /*
 88004 ** This function is called to generate code that runs when table pTab is
 88005 ** being dropped from the database. The SrcList passed as the second argument
 88006 ** to this function contains a single entry guaranteed to resolve to
 88007 ** table pTab.
 88008 **
 88009 ** Normally, no code is required. However, if either
 88010 **
 88011 **   (a) The table is the parent table of a FK constraint, or
 88012 **   (b) The table is the child table of a deferred FK constraint and it is
 88013 **       determined at runtime that there are outstanding deferred FK 
 88014 **       constraint violations in the database,
 88015 **
 88016 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
 88017 ** the table from the database. Triggers are disabled while running this
 88018 ** DELETE, but foreign key actions are not.
 88019 */
 88020 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
 88021   sqlite3 *db = pParse->db;
 88022   if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
 88023     int iSkip = 0;
 88024     Vdbe *v = sqlite3GetVdbe(pParse);
 88026     assert( v );                  /* VDBE has already been allocated */
 88027     if( sqlite3FkReferences(pTab)==0 ){
 88028       /* Search for a deferred foreign key constraint for which this table
 88029       ** is the child table. If one cannot be found, return without 
 88030       ** generating any VDBE code. If one can be found, then jump over
 88031       ** the entire DELETE if there are no outstanding deferred constraints
 88032       ** when this statement is run.  */
 88033       FKey *p;
 88034       for(p=pTab->pFKey; p; p=p->pNextFrom){
 88035         if( p->isDeferred ) break;
 88037       if( !p ) return;
 88038       iSkip = sqlite3VdbeMakeLabel(v);
 88039       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
 88042     pParse->disableTriggers = 1;
 88043     sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
 88044     pParse->disableTriggers = 0;
 88046     /* If the DELETE has generated immediate foreign key constraint 
 88047     ** violations, halt the VDBE and return an error at this point, before
 88048     ** any modifications to the schema are made. This is because statement
 88049     ** transactions are not able to rollback schema changes.  */
 88050     sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
 88051     sqlite3HaltConstraint(
 88052         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
 88053     );
 88055     if( iSkip ){
 88056       sqlite3VdbeResolveLabel(v, iSkip);
 88061 /*
 88062 ** This function is called when inserting, deleting or updating a row of
 88063 ** table pTab to generate VDBE code to perform foreign key constraint 
 88064 ** processing for the operation.
 88065 **
 88066 ** For a DELETE operation, parameter regOld is passed the index of the
 88067 ** first register in an array of (pTab->nCol+1) registers containing the
 88068 ** rowid of the row being deleted, followed by each of the column values
 88069 ** of the row being deleted, from left to right. Parameter regNew is passed
 88070 ** zero in this case.
 88071 **
 88072 ** For an INSERT operation, regOld is passed zero and regNew is passed the
 88073 ** first register of an array of (pTab->nCol+1) registers containing the new
 88074 ** row data.
 88075 **
 88076 ** For an UPDATE operation, this function is called twice. Once before
 88077 ** the original record is deleted from the table using the calling convention
 88078 ** described for DELETE. Then again after the original record is deleted
 88079 ** but before the new record is inserted using the INSERT convention. 
 88080 */
 88081 SQLITE_PRIVATE void sqlite3FkCheck(
 88082   Parse *pParse,                  /* Parse context */
 88083   Table *pTab,                    /* Row is being deleted from this table */ 
 88084   int regOld,                     /* Previous row data is stored here */
 88085   int regNew                      /* New row data is stored here */
 88086 ){
 88087   sqlite3 *db = pParse->db;       /* Database handle */
 88088   FKey *pFKey;                    /* Used to iterate through FKs */
 88089   int iDb;                        /* Index of database containing pTab */
 88090   const char *zDb;                /* Name of database containing pTab */
 88091   int isIgnoreErrors = pParse->disableTriggers;
 88093   /* Exactly one of regOld and regNew should be non-zero. */
 88094   assert( (regOld==0)!=(regNew==0) );
 88096   /* If foreign-keys are disabled, this function is a no-op. */
 88097   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
 88099   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 88100   zDb = db->aDb[iDb].zName;
 88102   /* Loop through all the foreign key constraints for which pTab is the
 88103   ** child table (the table that the foreign key definition is part of).  */
 88104   for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
 88105     Table *pTo;                   /* Parent table of foreign key pFKey */
 88106     Index *pIdx = 0;              /* Index on key columns in pTo */
 88107     int *aiFree = 0;
 88108     int *aiCol;
 88109     int iCol;
 88110     int i;
 88111     int isIgnore = 0;
 88113     /* Find the parent table of this foreign key. Also find a unique index 
 88114     ** on the parent key columns in the parent table. If either of these 
 88115     ** schema items cannot be located, set an error in pParse and return 
 88116     ** early.  */
 88117     if( pParse->disableTriggers ){
 88118       pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
 88119     }else{
 88120       pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
 88122     if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
 88123       assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
 88124       if( !isIgnoreErrors || db->mallocFailed ) return;
 88125       if( pTo==0 ){
 88126         /* If isIgnoreErrors is true, then a table is being dropped. In this
 88127         ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
 88128         ** before actually dropping it in order to check FK constraints.
 88129         ** If the parent table of an FK constraint on the current table is
 88130         ** missing, behave as if it is empty. i.e. decrement the relevant
 88131         ** FK counter for each row of the current table with non-NULL keys.
 88132         */
 88133         Vdbe *v = sqlite3GetVdbe(pParse);
 88134         int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
 88135         for(i=0; i<pFKey->nCol; i++){
 88136           int iReg = pFKey->aCol[i].iFrom + regOld + 1;
 88137           sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
 88139         sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
 88141       continue;
 88143     assert( pFKey->nCol==1 || (aiFree && pIdx) );
 88145     if( aiFree ){
 88146       aiCol = aiFree;
 88147     }else{
 88148       iCol = pFKey->aCol[0].iFrom;
 88149       aiCol = &iCol;
 88151     for(i=0; i<pFKey->nCol; i++){
 88152       if( aiCol[i]==pTab->iPKey ){
 88153         aiCol[i] = -1;
 88155 #ifndef SQLITE_OMIT_AUTHORIZATION
 88156       /* Request permission to read the parent key columns. If the 
 88157       ** authorization callback returns SQLITE_IGNORE, behave as if any
 88158       ** values read from the parent table are NULL. */
 88159       if( db->xAuth ){
 88160         int rcauth;
 88161         char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
 88162         rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
 88163         isIgnore = (rcauth==SQLITE_IGNORE);
 88165 #endif
 88168     /* Take a shared-cache advisory read-lock on the parent table. Allocate 
 88169     ** a cursor to use to search the unique index on the parent key columns 
 88170     ** in the parent table.  */
 88171     sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
 88172     pParse->nTab++;
 88174     if( regOld!=0 ){
 88175       /* A row is being removed from the child table. Search for the parent.
 88176       ** If the parent does not exist, removing the child row resolves an 
 88177       ** outstanding foreign key constraint violation. */
 88178       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
 88180     if( regNew!=0 ){
 88181       /* A row is being added to the child table. If a parent row cannot
 88182       ** be found, adding the child row has violated the FK constraint. */ 
 88183       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
 88186     sqlite3DbFree(db, aiFree);
 88189   /* Loop through all the foreign key constraints that refer to this table */
 88190   for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
 88191     Index *pIdx = 0;              /* Foreign key index for pFKey */
 88192     SrcList *pSrc;
 88193     int *aiCol = 0;
 88195     if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
 88196       assert( regOld==0 && regNew!=0 );
 88197       /* Inserting a single row into a parent table cannot cause an immediate
 88198       ** foreign key violation. So do nothing in this case.  */
 88199       continue;
 88202     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
 88203       if( !isIgnoreErrors || db->mallocFailed ) return;
 88204       continue;
 88206     assert( aiCol || pFKey->nCol==1 );
 88208     /* Create a SrcList structure containing a single table (the table 
 88209     ** the foreign key that refers to this table is attached to). This
 88210     ** is required for the sqlite3WhereXXX() interface.  */
 88211     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
 88212     if( pSrc ){
 88213       struct SrcList_item *pItem = pSrc->a;
 88214       pItem->pTab = pFKey->pFrom;
 88215       pItem->zName = pFKey->pFrom->zName;
 88216       pItem->pTab->nRef++;
 88217       pItem->iCursor = pParse->nTab++;
 88219       if( regNew!=0 ){
 88220         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
 88222       if( regOld!=0 ){
 88223         /* If there is a RESTRICT action configured for the current operation
 88224         ** on the parent table of this FK, then throw an exception 
 88225         ** immediately if the FK constraint is violated, even if this is a
 88226         ** deferred trigger. That's what RESTRICT means. To defer checking
 88227         ** the constraint, the FK should specify NO ACTION (represented
 88228         ** using OE_None). NO ACTION is the default.  */
 88229         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
 88231       pItem->zName = 0;
 88232       sqlite3SrcListDelete(db, pSrc);
 88234     sqlite3DbFree(db, aiCol);
 88238 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
 88240 /*
 88241 ** This function is called before generating code to update or delete a 
 88242 ** row contained in table pTab.
 88243 */
 88244 SQLITE_PRIVATE u32 sqlite3FkOldmask(
 88245   Parse *pParse,                  /* Parse context */
 88246   Table *pTab                     /* Table being modified */
 88247 ){
 88248   u32 mask = 0;
 88249   if( pParse->db->flags&SQLITE_ForeignKeys ){
 88250     FKey *p;
 88251     int i;
 88252     for(p=pTab->pFKey; p; p=p->pNextFrom){
 88253       for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
 88255     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 88256       Index *pIdx = 0;
 88257       locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
 88258       if( pIdx ){
 88259         for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
 88263   return mask;
 88266 /*
 88267 ** This function is called before generating code to update or delete a 
 88268 ** row contained in table pTab. If the operation is a DELETE, then
 88269 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
 88270 ** to an array of size N, where N is the number of columns in table pTab.
 88271 ** If the i'th column is not modified by the UPDATE, then the corresponding 
 88272 ** entry in the aChange[] array is set to -1. If the column is modified,
 88273 ** the value is 0 or greater. Parameter chngRowid is set to true if the
 88274 ** UPDATE statement modifies the rowid fields of the table.
 88275 **
 88276 ** If any foreign key processing will be required, this function returns
 88277 ** true. If there is no foreign key related processing, this function 
 88278 ** returns false.
 88279 */
 88280 SQLITE_PRIVATE int sqlite3FkRequired(
 88281   Parse *pParse,                  /* Parse context */
 88282   Table *pTab,                    /* Table being modified */
 88283   int *aChange,                   /* Non-NULL for UPDATE operations */
 88284   int chngRowid                   /* True for UPDATE that affects rowid */
 88285 ){
 88286   if( pParse->db->flags&SQLITE_ForeignKeys ){
 88287     if( !aChange ){
 88288       /* A DELETE operation. Foreign key processing is required if the 
 88289       ** table in question is either the child or parent table for any 
 88290       ** foreign key constraint.  */
 88291       return (sqlite3FkReferences(pTab) || pTab->pFKey);
 88292     }else{
 88293       /* This is an UPDATE. Foreign key processing is only required if the
 88294       ** operation modifies one or more child or parent key columns. */
 88295       int i;
 88296       FKey *p;
 88298       /* Check if any child key columns are being modified. */
 88299       for(p=pTab->pFKey; p; p=p->pNextFrom){
 88300         for(i=0; i<p->nCol; i++){
 88301           int iChildKey = p->aCol[i].iFrom;
 88302           if( aChange[iChildKey]>=0 ) return 1;
 88303           if( iChildKey==pTab->iPKey && chngRowid ) return 1;
 88307       /* Check if any parent key columns are being modified. */
 88308       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 88309         for(i=0; i<p->nCol; i++){
 88310           char *zKey = p->aCol[i].zCol;
 88311           int iKey;
 88312           for(iKey=0; iKey<pTab->nCol; iKey++){
 88313             Column *pCol = &pTab->aCol[iKey];
 88314             if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey)
 88315                       : (pCol->colFlags & COLFLAG_PRIMKEY)!=0) ){
 88316               if( aChange[iKey]>=0 ) return 1;
 88317               if( iKey==pTab->iPKey && chngRowid ) return 1;
 88324   return 0;
 88327 /*
 88328 ** This function is called when an UPDATE or DELETE operation is being 
 88329 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
 88330 ** If the current operation is an UPDATE, then the pChanges parameter is
 88331 ** passed a pointer to the list of columns being modified. If it is a
 88332 ** DELETE, pChanges is passed a NULL pointer.
 88333 **
 88334 ** It returns a pointer to a Trigger structure containing a trigger
 88335 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
 88336 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
 88337 ** returned (these actions require no special handling by the triggers
 88338 ** sub-system, code for them is created by fkScanChildren()).
 88339 **
 88340 ** For example, if pFKey is the foreign key and pTab is table "p" in 
 88341 ** the following schema:
 88342 **
 88343 **   CREATE TABLE p(pk PRIMARY KEY);
 88344 **   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
 88345 **
 88346 ** then the returned trigger structure is equivalent to:
 88347 **
 88348 **   CREATE TRIGGER ... DELETE ON p BEGIN
 88349 **     DELETE FROM c WHERE ck = old.pk;
 88350 **   END;
 88351 **
 88352 ** The returned pointer is cached as part of the foreign key object. It
 88353 ** is eventually freed along with the rest of the foreign key object by 
 88354 ** sqlite3FkDelete().
 88355 */
 88356 static Trigger *fkActionTrigger(
 88357   Parse *pParse,                  /* Parse context */
 88358   Table *pTab,                    /* Table being updated or deleted from */
 88359   FKey *pFKey,                    /* Foreign key to get action for */
 88360   ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
 88361 ){
 88362   sqlite3 *db = pParse->db;       /* Database handle */
 88363   int action;                     /* One of OE_None, OE_Cascade etc. */
 88364   Trigger *pTrigger;              /* Trigger definition to return */
 88365   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
 88367   action = pFKey->aAction[iAction];
 88368   pTrigger = pFKey->apTrigger[iAction];
 88370   if( action!=OE_None && !pTrigger ){
 88371     u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
 88372     char const *zFrom;            /* Name of child table */
 88373     int nFrom;                    /* Length in bytes of zFrom */
 88374     Index *pIdx = 0;              /* Parent key index for this FK */
 88375     int *aiCol = 0;               /* child table cols -> parent key cols */
 88376     TriggerStep *pStep = 0;        /* First (only) step of trigger program */
 88377     Expr *pWhere = 0;             /* WHERE clause of trigger step */
 88378     ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
 88379     Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
 88380     int i;                        /* Iterator variable */
 88381     Expr *pWhen = 0;              /* WHEN clause for the trigger */
 88383     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
 88384     assert( aiCol || pFKey->nCol==1 );
 88386     for(i=0; i<pFKey->nCol; i++){
 88387       Token tOld = { "old", 3 };  /* Literal "old" token */
 88388       Token tNew = { "new", 3 };  /* Literal "new" token */
 88389       Token tFromCol;             /* Name of column in child table */
 88390       Token tToCol;               /* Name of column in parent table */
 88391       int iFromCol;               /* Idx of column in child table */
 88392       Expr *pEq;                  /* tFromCol = OLD.tToCol */
 88394       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 88395       assert( iFromCol>=0 );
 88396       tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
 88397       tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
 88399       tToCol.n = sqlite3Strlen30(tToCol.z);
 88400       tFromCol.n = sqlite3Strlen30(tFromCol.z);
 88402       /* Create the expression "OLD.zToCol = zFromCol". It is important
 88403       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
 88404       ** that the affinity and collation sequence associated with the
 88405       ** parent table are used for the comparison. */
 88406       pEq = sqlite3PExpr(pParse, TK_EQ,
 88407           sqlite3PExpr(pParse, TK_DOT, 
 88408             sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 88409             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
 88410           , 0),
 88411           sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
 88412       , 0);
 88413       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 88415       /* For ON UPDATE, construct the next term of the WHEN clause.
 88416       ** The final WHEN clause will be like this:
 88417       **
 88418       **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
 88419       */
 88420       if( pChanges ){
 88421         pEq = sqlite3PExpr(pParse, TK_IS,
 88422             sqlite3PExpr(pParse, TK_DOT, 
 88423               sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 88424               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
 88425               0),
 88426             sqlite3PExpr(pParse, TK_DOT, 
 88427               sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 88428               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
 88429               0),
 88430             0);
 88431         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
 88434       if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
 88435         Expr *pNew;
 88436         if( action==OE_Cascade ){
 88437           pNew = sqlite3PExpr(pParse, TK_DOT, 
 88438             sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 88439             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
 88440           , 0);
 88441         }else if( action==OE_SetDflt ){
 88442           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
 88443           if( pDflt ){
 88444             pNew = sqlite3ExprDup(db, pDflt, 0);
 88445           }else{
 88446             pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
 88448         }else{
 88449           pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
 88451         pList = sqlite3ExprListAppend(pParse, pList, pNew);
 88452         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
 88455     sqlite3DbFree(db, aiCol);
 88457     zFrom = pFKey->pFrom->zName;
 88458     nFrom = sqlite3Strlen30(zFrom);
 88460     if( action==OE_Restrict ){
 88461       Token tFrom;
 88462       Expr *pRaise; 
 88464       tFrom.z = zFrom;
 88465       tFrom.n = nFrom;
 88466       pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
 88467       if( pRaise ){
 88468         pRaise->affinity = OE_Abort;
 88470       pSelect = sqlite3SelectNew(pParse, 
 88471           sqlite3ExprListAppend(pParse, 0, pRaise),
 88472           sqlite3SrcListAppend(db, 0, &tFrom, 0),
 88473           pWhere,
 88474           0, 0, 0, 0, 0, 0
 88475       );
 88476       pWhere = 0;
 88479     /* Disable lookaside memory allocation */
 88480     enableLookaside = db->lookaside.bEnabled;
 88481     db->lookaside.bEnabled = 0;
 88483     pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
 88484         sizeof(Trigger) +         /* struct Trigger */
 88485         sizeof(TriggerStep) +     /* Single step in trigger program */
 88486         nFrom + 1                 /* Space for pStep->target.z */
 88487     );
 88488     if( pTrigger ){
 88489       pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
 88490       pStep->target.z = (char *)&pStep[1];
 88491       pStep->target.n = nFrom;
 88492       memcpy((char *)pStep->target.z, zFrom, nFrom);
 88494       pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 88495       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
 88496       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 88497       if( pWhen ){
 88498         pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
 88499         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
 88503     /* Re-enable the lookaside buffer, if it was disabled earlier. */
 88504     db->lookaside.bEnabled = enableLookaside;
 88506     sqlite3ExprDelete(db, pWhere);
 88507     sqlite3ExprDelete(db, pWhen);
 88508     sqlite3ExprListDelete(db, pList);
 88509     sqlite3SelectDelete(db, pSelect);
 88510     if( db->mallocFailed==1 ){
 88511       fkTriggerDelete(db, pTrigger);
 88512       return 0;
 88514     assert( pStep!=0 );
 88516     switch( action ){
 88517       case OE_Restrict:
 88518         pStep->op = TK_SELECT; 
 88519         break;
 88520       case OE_Cascade: 
 88521         if( !pChanges ){ 
 88522           pStep->op = TK_DELETE; 
 88523           break; 
 88525       default:
 88526         pStep->op = TK_UPDATE;
 88528     pStep->pTrig = pTrigger;
 88529     pTrigger->pSchema = pTab->pSchema;
 88530     pTrigger->pTabSchema = pTab->pSchema;
 88531     pFKey->apTrigger[iAction] = pTrigger;
 88532     pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
 88535   return pTrigger;
 88538 /*
 88539 ** This function is called when deleting or updating a row to implement
 88540 ** any required CASCADE, SET NULL or SET DEFAULT actions.
 88541 */
 88542 SQLITE_PRIVATE void sqlite3FkActions(
 88543   Parse *pParse,                  /* Parse context */
 88544   Table *pTab,                    /* Table being updated or deleted from */
 88545   ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
 88546   int regOld                      /* Address of array containing old row */
 88547 ){
 88548   /* If foreign-key support is enabled, iterate through all FKs that 
 88549   ** refer to table pTab. If there is an action associated with the FK 
 88550   ** for this operation (either update or delete), invoke the associated 
 88551   ** trigger sub-program.  */
 88552   if( pParse->db->flags&SQLITE_ForeignKeys ){
 88553     FKey *pFKey;                  /* Iterator variable */
 88554     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
 88555       Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
 88556       if( pAction ){
 88557         sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
 88563 #endif /* ifndef SQLITE_OMIT_TRIGGER */
 88565 /*
 88566 ** Free all memory associated with foreign key definitions attached to
 88567 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
 88568 ** hash table.
 88569 */
 88570 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
 88571   FKey *pFKey;                    /* Iterator variable */
 88572   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
 88574   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
 88575   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
 88577     /* Remove the FK from the fkeyHash hash table. */
 88578     if( !db || db->pnBytesFreed==0 ){
 88579       if( pFKey->pPrevTo ){
 88580         pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
 88581       }else{
 88582         void *p = (void *)pFKey->pNextTo;
 88583         const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
 88584         sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
 88586       if( pFKey->pNextTo ){
 88587         pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
 88591     /* EV: R-30323-21917 Each foreign key constraint in SQLite is
 88592     ** classified as either immediate or deferred.
 88593     */
 88594     assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
 88596     /* Delete any triggers created to implement actions for this FK. */
 88597 #ifndef SQLITE_OMIT_TRIGGER
 88598     fkTriggerDelete(db, pFKey->apTrigger[0]);
 88599     fkTriggerDelete(db, pFKey->apTrigger[1]);
 88600 #endif
 88602     pNext = pFKey->pNextFrom;
 88603     sqlite3DbFree(db, pFKey);
 88606 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
 88608 /************** End of fkey.c ************************************************/
 88609 /************** Begin file insert.c ******************************************/
 88610 /*
 88611 ** 2001 September 15
 88612 **
 88613 ** The author disclaims copyright to this source code.  In place of
 88614 ** a legal notice, here is a blessing:
 88615 **
 88616 **    May you do good and not evil.
 88617 **    May you find forgiveness for yourself and forgive others.
 88618 **    May you share freely, never taking more than you give.
 88619 **
 88620 *************************************************************************
 88621 ** This file contains C code routines that are called by the parser
 88622 ** to handle INSERT statements in SQLite.
 88623 */
 88625 /*
 88626 ** Generate code that will open a table for reading.
 88627 */
 88628 SQLITE_PRIVATE void sqlite3OpenTable(
 88629   Parse *p,       /* Generate code into this VDBE */
 88630   int iCur,       /* The cursor number of the table */
 88631   int iDb,        /* The database index in sqlite3.aDb[] */
 88632   Table *pTab,    /* The table to be opened */
 88633   int opcode      /* OP_OpenRead or OP_OpenWrite */
 88634 ){
 88635   Vdbe *v;
 88636   assert( !IsVirtual(pTab) );
 88637   v = sqlite3GetVdbe(p);
 88638   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
 88639   sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
 88640   sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
 88641   sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
 88642   VdbeComment((v, "%s", pTab->zName));
 88645 /*
 88646 ** Return a pointer to the column affinity string associated with index
 88647 ** pIdx. A column affinity string has one character for each column in 
 88648 ** the table, according to the affinity of the column:
 88649 **
 88650 **  Character      Column affinity
 88651 **  ------------------------------
 88652 **  'a'            TEXT
 88653 **  'b'            NONE
 88654 **  'c'            NUMERIC
 88655 **  'd'            INTEGER
 88656 **  'e'            REAL
 88657 **
 88658 ** An extra 'd' is appended to the end of the string to cover the
 88659 ** rowid that appears as the last column in every index.
 88660 **
 88661 ** Memory for the buffer containing the column index affinity string
 88662 ** is managed along with the rest of the Index structure. It will be
 88663 ** released when sqlite3DeleteIndex() is called.
 88664 */
 88665 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
 88666   if( !pIdx->zColAff ){
 88667     /* The first time a column affinity string for a particular index is
 88668     ** required, it is allocated and populated here. It is then stored as
 88669     ** a member of the Index structure for subsequent use.
 88670     **
 88671     ** The column affinity string will eventually be deleted by
 88672     ** sqliteDeleteIndex() when the Index structure itself is cleaned
 88673     ** up.
 88674     */
 88675     int n;
 88676     Table *pTab = pIdx->pTable;
 88677     sqlite3 *db = sqlite3VdbeDb(v);
 88678     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
 88679     if( !pIdx->zColAff ){
 88680       db->mallocFailed = 1;
 88681       return 0;
 88683     for(n=0; n<pIdx->nColumn; n++){
 88684       pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
 88686     pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
 88687     pIdx->zColAff[n] = 0;
 88690   return pIdx->zColAff;
 88693 /*
 88694 ** Set P4 of the most recently inserted opcode to a column affinity
 88695 ** string for table pTab. A column affinity string has one character
 88696 ** for each column indexed by the index, according to the affinity of the
 88697 ** column:
 88698 **
 88699 **  Character      Column affinity
 88700 **  ------------------------------
 88701 **  'a'            TEXT
 88702 **  'b'            NONE
 88703 **  'c'            NUMERIC
 88704 **  'd'            INTEGER
 88705 **  'e'            REAL
 88706 */
 88707 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
 88708   /* The first time a column affinity string for a particular table
 88709   ** is required, it is allocated and populated here. It is then 
 88710   ** stored as a member of the Table structure for subsequent use.
 88711   **
 88712   ** The column affinity string will eventually be deleted by
 88713   ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
 88714   */
 88715   if( !pTab->zColAff ){
 88716     char *zColAff;
 88717     int i;
 88718     sqlite3 *db = sqlite3VdbeDb(v);
 88720     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
 88721     if( !zColAff ){
 88722       db->mallocFailed = 1;
 88723       return;
 88726     for(i=0; i<pTab->nCol; i++){
 88727       zColAff[i] = pTab->aCol[i].affinity;
 88729     zColAff[pTab->nCol] = '\0';
 88731     pTab->zColAff = zColAff;
 88734   sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
 88737 /*
 88738 ** Return non-zero if the table pTab in database iDb or any of its indices
 88739 ** have been opened at any point in the VDBE program beginning at location
 88740 ** iStartAddr throught the end of the program.  This is used to see if 
 88741 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
 88742 ** run without using temporary table for the results of the SELECT. 
 88743 */
 88744 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
 88745   Vdbe *v = sqlite3GetVdbe(p);
 88746   int i;
 88747   int iEnd = sqlite3VdbeCurrentAddr(v);
 88748 #ifndef SQLITE_OMIT_VIRTUALTABLE
 88749   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
 88750 #endif
 88752   for(i=iStartAddr; i<iEnd; i++){
 88753     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
 88754     assert( pOp!=0 );
 88755     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
 88756       Index *pIndex;
 88757       int tnum = pOp->p2;
 88758       if( tnum==pTab->tnum ){
 88759         return 1;
 88761       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
 88762         if( tnum==pIndex->tnum ){
 88763           return 1;
 88767 #ifndef SQLITE_OMIT_VIRTUALTABLE
 88768     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
 88769       assert( pOp->p4.pVtab!=0 );
 88770       assert( pOp->p4type==P4_VTAB );
 88771       return 1;
 88773 #endif
 88775   return 0;
 88778 #ifndef SQLITE_OMIT_AUTOINCREMENT
 88779 /*
 88780 ** Locate or create an AutoincInfo structure associated with table pTab
 88781 ** which is in database iDb.  Return the register number for the register
 88782 ** that holds the maximum rowid.
 88783 **
 88784 ** There is at most one AutoincInfo structure per table even if the
 88785 ** same table is autoincremented multiple times due to inserts within
 88786 ** triggers.  A new AutoincInfo structure is created if this is the
 88787 ** first use of table pTab.  On 2nd and subsequent uses, the original
 88788 ** AutoincInfo structure is used.
 88789 **
 88790 ** Three memory locations are allocated:
 88791 **
 88792 **   (1)  Register to hold the name of the pTab table.
 88793 **   (2)  Register to hold the maximum ROWID of pTab.
 88794 **   (3)  Register to hold the rowid in sqlite_sequence of pTab
 88795 **
 88796 ** The 2nd register is the one that is returned.  That is all the
 88797 ** insert routine needs to know about.
 88798 */
 88799 static int autoIncBegin(
 88800   Parse *pParse,      /* Parsing context */
 88801   int iDb,            /* Index of the database holding pTab */
 88802   Table *pTab         /* The table we are writing to */
 88803 ){
 88804   int memId = 0;      /* Register holding maximum rowid */
 88805   if( pTab->tabFlags & TF_Autoincrement ){
 88806     Parse *pToplevel = sqlite3ParseToplevel(pParse);
 88807     AutoincInfo *pInfo;
 88809     pInfo = pToplevel->pAinc;
 88810     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
 88811     if( pInfo==0 ){
 88812       pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
 88813       if( pInfo==0 ) return 0;
 88814       pInfo->pNext = pToplevel->pAinc;
 88815       pToplevel->pAinc = pInfo;
 88816       pInfo->pTab = pTab;
 88817       pInfo->iDb = iDb;
 88818       pToplevel->nMem++;                  /* Register to hold name of table */
 88819       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
 88820       pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
 88822     memId = pInfo->regCtr;
 88824   return memId;
 88827 /*
 88828 ** This routine generates code that will initialize all of the
 88829 ** register used by the autoincrement tracker.  
 88830 */
 88831 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
 88832   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
 88833   sqlite3 *db = pParse->db;  /* The database connection */
 88834   Db *pDb;                   /* Database only autoinc table */
 88835   int memId;                 /* Register holding max rowid */
 88836   int addr;                  /* A VDBE address */
 88837   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
 88839   /* This routine is never called during trigger-generation.  It is
 88840   ** only called from the top-level */
 88841   assert( pParse->pTriggerTab==0 );
 88842   assert( pParse==sqlite3ParseToplevel(pParse) );
 88844   assert( v );   /* We failed long ago if this is not so */
 88845   for(p = pParse->pAinc; p; p = p->pNext){
 88846     pDb = &db->aDb[p->iDb];
 88847     memId = p->regCtr;
 88848     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 88849     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
 88850     sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
 88851     addr = sqlite3VdbeCurrentAddr(v);
 88852     sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
 88853     sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
 88854     sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
 88855     sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
 88856     sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 88857     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
 88858     sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
 88859     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
 88860     sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
 88861     sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
 88862     sqlite3VdbeAddOp0(v, OP_Close);
 88866 /*
 88867 ** Update the maximum rowid for an autoincrement calculation.
 88868 **
 88869 ** This routine should be called when the top of the stack holds a
 88870 ** new rowid that is about to be inserted.  If that new rowid is
 88871 ** larger than the maximum rowid in the memId memory cell, then the
 88872 ** memory cell is updated.  The stack is unchanged.
 88873 */
 88874 static void autoIncStep(Parse *pParse, int memId, int regRowid){
 88875   if( memId>0 ){
 88876     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
 88880 /*
 88881 ** This routine generates the code needed to write autoincrement
 88882 ** maximum rowid values back into the sqlite_sequence register.
 88883 ** Every statement that might do an INSERT into an autoincrement
 88884 ** table (either directly or through triggers) needs to call this
 88885 ** routine just before the "exit" code.
 88886 */
 88887 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
 88888   AutoincInfo *p;
 88889   Vdbe *v = pParse->pVdbe;
 88890   sqlite3 *db = pParse->db;
 88892   assert( v );
 88893   for(p = pParse->pAinc; p; p = p->pNext){
 88894     Db *pDb = &db->aDb[p->iDb];
 88895     int j1, j2, j3, j4, j5;
 88896     int iRec;
 88897     int memId = p->regCtr;
 88899     iRec = sqlite3GetTempReg(pParse);
 88900     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 88901     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
 88902     j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
 88903     j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
 88904     j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
 88905     j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
 88906     sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
 88907     sqlite3VdbeJumpHere(v, j2);
 88908     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
 88909     j5 = sqlite3VdbeAddOp0(v, OP_Goto);
 88910     sqlite3VdbeJumpHere(v, j4);
 88911     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
 88912     sqlite3VdbeJumpHere(v, j1);
 88913     sqlite3VdbeJumpHere(v, j5);
 88914     sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
 88915     sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
 88916     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 88917     sqlite3VdbeAddOp0(v, OP_Close);
 88918     sqlite3ReleaseTempReg(pParse, iRec);
 88921 #else
 88922 /*
 88923 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
 88924 ** above are all no-ops
 88925 */
 88926 # define autoIncBegin(A,B,C) (0)
 88927 # define autoIncStep(A,B,C)
 88928 #endif /* SQLITE_OMIT_AUTOINCREMENT */
 88931 /*
 88932 ** Generate code for a co-routine that will evaluate a subquery one
 88933 ** row at a time.
 88934 **
 88935 ** The pSelect parameter is the subquery that the co-routine will evaluation.
 88936 ** Information about the location of co-routine and the registers it will use
 88937 ** is returned by filling in the pDest object.
 88938 **
 88939 ** Registers are allocated as follows:
 88940 **
 88941 **   pDest->iSDParm      The register holding the next entry-point of the
 88942 **                       co-routine.  Run the co-routine to its next breakpoint
 88943 **                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
 88944 **
 88945 **   pDest->iSDParm+1    The register holding the "completed" flag for the
 88946 **                       co-routine. This register is 0 if the previous Yield
 88947 **                       generated a new result row, or 1 if the subquery
 88948 **                       has completed.  If the Yield is called again
 88949 **                       after this register becomes 1, then the VDBE will
 88950 **                       halt with an SQLITE_INTERNAL error.
 88951 **
 88952 **   pDest->iSdst        First result register.
 88953 **
 88954 **   pDest->nSdst        Number of result registers.
 88955 **
 88956 ** This routine handles all of the register allocation and fills in the
 88957 ** pDest structure appropriately.
 88958 **
 88959 ** Here is a schematic of the generated code assuming that X is the 
 88960 ** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
 88961 ** completed flag reg[pDest->iSDParm+1], and R and S are the range of
 88962 ** registers that hold the result set, reg[pDest->iSdst] through
 88963 ** reg[pDest->iSdst+pDest->nSdst-1]:
 88964 **
 88965 **         X <- A
 88966 **         EOF <- 0
 88967 **         goto B
 88968 **      A: setup for the SELECT
 88969 **         loop rows in the SELECT
 88970 **           load results into registers R..S
 88971 **           yield X
 88972 **         end loop
 88973 **         cleanup after the SELECT
 88974 **         EOF <- 1
 88975 **         yield X
 88976 **         halt-error
 88977 **      B:
 88978 **
 88979 ** To use this subroutine, the caller generates code as follows:
 88980 **
 88981 **         [ Co-routine generated by this subroutine, shown above ]
 88982 **      S: yield X
 88983 **         if EOF goto E
 88984 **         if skip this row, goto C
 88985 **         if terminate loop, goto E
 88986 **         deal with this row
 88987 **      C: goto S
 88988 **      E:
 88989 */
 88990 SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
 88991   int regYield;       /* Register holding co-routine entry-point */
 88992   int regEof;         /* Register holding co-routine completion flag */
 88993   int addrTop;        /* Top of the co-routine */
 88994   int j1;             /* Jump instruction */
 88995   int rc;             /* Result code */
 88996   Vdbe *v;            /* VDBE under construction */
 88998   regYield = ++pParse->nMem;
 88999   regEof = ++pParse->nMem;
 89000   v = sqlite3GetVdbe(pParse);
 89001   addrTop = sqlite3VdbeCurrentAddr(v);
 89002   sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
 89003   VdbeComment((v, "Co-routine entry point"));
 89004   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
 89005   VdbeComment((v, "Co-routine completion flag"));
 89006   sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
 89007   j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 89008   rc = sqlite3Select(pParse, pSelect, pDest);
 89009   assert( pParse->nErr==0 || rc );
 89010   if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
 89011   if( rc ) return rc;
 89012   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
 89013   sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
 89014   sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
 89015   VdbeComment((v, "End of coroutine"));
 89016   sqlite3VdbeJumpHere(v, j1);                             /* label B: */
 89017   return rc;
 89022 /* Forward declaration */
 89023 static int xferOptimization(
 89024   Parse *pParse,        /* Parser context */
 89025   Table *pDest,         /* The table we are inserting into */
 89026   Select *pSelect,      /* A SELECT statement to use as the data source */
 89027   int onError,          /* How to handle constraint errors */
 89028   int iDbDest           /* The database of pDest */
 89029 );
 89031 /*
 89032 ** This routine is call to handle SQL of the following forms:
 89033 **
 89034 **    insert into TABLE (IDLIST) values(EXPRLIST)
 89035 **    insert into TABLE (IDLIST) select
 89036 **
 89037 ** The IDLIST following the table name is always optional.  If omitted,
 89038 ** then a list of all columns for the table is substituted.  The IDLIST
 89039 ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
 89040 **
 89041 ** The pList parameter holds EXPRLIST in the first form of the INSERT
 89042 ** statement above, and pSelect is NULL.  For the second form, pList is
 89043 ** NULL and pSelect is a pointer to the select statement used to generate
 89044 ** data for the insert.
 89045 **
 89046 ** The code generated follows one of four templates.  For a simple
 89047 ** select with data coming from a VALUES clause, the code executes
 89048 ** once straight down through.  Pseudo-code follows (we call this
 89049 ** the "1st template"):
 89050 **
 89051 **         open write cursor to <table> and its indices
 89052 **         puts VALUES clause expressions onto the stack
 89053 **         write the resulting record into <table>
 89054 **         cleanup
 89055 **
 89056 ** The three remaining templates assume the statement is of the form
 89057 **
 89058 **   INSERT INTO <table> SELECT ...
 89059 **
 89060 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
 89061 ** in other words if the SELECT pulls all columns from a single table
 89062 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
 89063 ** if <table2> and <table1> are distinct tables but have identical
 89064 ** schemas, including all the same indices, then a special optimization
 89065 ** is invoked that copies raw records from <table2> over to <table1>.
 89066 ** See the xferOptimization() function for the implementation of this
 89067 ** template.  This is the 2nd template.
 89068 **
 89069 **         open a write cursor to <table>
 89070 **         open read cursor on <table2>
 89071 **         transfer all records in <table2> over to <table>
 89072 **         close cursors
 89073 **         foreach index on <table>
 89074 **           open a write cursor on the <table> index
 89075 **           open a read cursor on the corresponding <table2> index
 89076 **           transfer all records from the read to the write cursors
 89077 **           close cursors
 89078 **         end foreach
 89079 **
 89080 ** The 3rd template is for when the second template does not apply
 89081 ** and the SELECT clause does not read from <table> at any time.
 89082 ** The generated code follows this template:
 89083 **
 89084 **         EOF <- 0
 89085 **         X <- A
 89086 **         goto B
 89087 **      A: setup for the SELECT
 89088 **         loop over the rows in the SELECT
 89089 **           load values into registers R..R+n
 89090 **           yield X
 89091 **         end loop
 89092 **         cleanup after the SELECT
 89093 **         EOF <- 1
 89094 **         yield X
 89095 **         goto A
 89096 **      B: open write cursor to <table> and its indices
 89097 **      C: yield X
 89098 **         if EOF goto D
 89099 **         insert the select result into <table> from R..R+n
 89100 **         goto C
 89101 **      D: cleanup
 89102 **
 89103 ** The 4th template is used if the insert statement takes its
 89104 ** values from a SELECT but the data is being inserted into a table
 89105 ** that is also read as part of the SELECT.  In the third form,
 89106 ** we have to use a intermediate table to store the results of
 89107 ** the select.  The template is like this:
 89108 **
 89109 **         EOF <- 0
 89110 **         X <- A
 89111 **         goto B
 89112 **      A: setup for the SELECT
 89113 **         loop over the tables in the SELECT
 89114 **           load value into register R..R+n
 89115 **           yield X
 89116 **         end loop
 89117 **         cleanup after the SELECT
 89118 **         EOF <- 1
 89119 **         yield X
 89120 **         halt-error
 89121 **      B: open temp table
 89122 **      L: yield X
 89123 **         if EOF goto M
 89124 **         insert row from R..R+n into temp table
 89125 **         goto L
 89126 **      M: open write cursor to <table> and its indices
 89127 **         rewind temp table
 89128 **      C: loop over rows of intermediate table
 89129 **           transfer values form intermediate table into <table>
 89130 **         end loop
 89131 **      D: cleanup
 89132 */
 89133 SQLITE_PRIVATE void sqlite3Insert(
 89134   Parse *pParse,        /* Parser context */
 89135   SrcList *pTabList,    /* Name of table into which we are inserting */
 89136   ExprList *pList,      /* List of values to be inserted */
 89137   Select *pSelect,      /* A SELECT statement to use as the data source */
 89138   IdList *pColumn,      /* Column names corresponding to IDLIST. */
 89139   int onError           /* How to handle constraint errors */
 89140 ){
 89141   sqlite3 *db;          /* The main database structure */
 89142   Table *pTab;          /* The table to insert into.  aka TABLE */
 89143   char *zTab;           /* Name of the table into which we are inserting */
 89144   const char *zDb;      /* Name of the database holding this table */
 89145   int i, j, idx;        /* Loop counters */
 89146   Vdbe *v;              /* Generate code into this virtual machine */
 89147   Index *pIdx;          /* For looping over indices of the table */
 89148   int nColumn;          /* Number of columns in the data */
 89149   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
 89150   int baseCur = 0;      /* VDBE Cursor number for pTab */
 89151   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
 89152   int endOfLoop;        /* Label for the end of the insertion loop */
 89153   int useTempTable = 0; /* Store SELECT results in intermediate table */
 89154   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
 89155   int addrInsTop = 0;   /* Jump to label "D" */
 89156   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
 89157   int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
 89158   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
 89159   int iDb;              /* Index of database holding TABLE */
 89160   Db *pDb;              /* The database containing table being inserted into */
 89161   int appendFlag = 0;   /* True if the insert is likely to be an append */
 89163   /* Register allocations */
 89164   int regFromSelect = 0;/* Base register for data coming from SELECT */
 89165   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
 89166   int regRowCount = 0;  /* Memory cell used for the row counter */
 89167   int regIns;           /* Block of regs holding rowid+data being inserted */
 89168   int regRowid;         /* registers holding insert rowid */
 89169   int regData;          /* register holding first column to insert */
 89170   int regEof = 0;       /* Register recording end of SELECT data */
 89171   int *aRegIdx = 0;     /* One register allocated to each index */
 89173 #ifndef SQLITE_OMIT_TRIGGER
 89174   int isView;                 /* True if attempting to insert into a view */
 89175   Trigger *pTrigger;          /* List of triggers on pTab, if required */
 89176   int tmask;                  /* Mask of trigger times */
 89177 #endif
 89179   db = pParse->db;
 89180   memset(&dest, 0, sizeof(dest));
 89181   if( pParse->nErr || db->mallocFailed ){
 89182     goto insert_cleanup;
 89185   /* Locate the table into which we will be inserting new information.
 89186   */
 89187   assert( pTabList->nSrc==1 );
 89188   zTab = pTabList->a[0].zName;
 89189   if( NEVER(zTab==0) ) goto insert_cleanup;
 89190   pTab = sqlite3SrcListLookup(pParse, pTabList);
 89191   if( pTab==0 ){
 89192     goto insert_cleanup;
 89194   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 89195   assert( iDb<db->nDb );
 89196   pDb = &db->aDb[iDb];
 89197   zDb = pDb->zName;
 89198   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
 89199     goto insert_cleanup;
 89202   /* Figure out if we have any triggers and if the table being
 89203   ** inserted into is a view
 89204   */
 89205 #ifndef SQLITE_OMIT_TRIGGER
 89206   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
 89207   isView = pTab->pSelect!=0;
 89208 #else
 89209 # define pTrigger 0
 89210 # define tmask 0
 89211 # define isView 0
 89212 #endif
 89213 #ifdef SQLITE_OMIT_VIEW
 89214 # undef isView
 89215 # define isView 0
 89216 #endif
 89217   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
 89219   /* If pTab is really a view, make sure it has been initialized.
 89220   ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 
 89221   ** module table).
 89222   */
 89223   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 89224     goto insert_cleanup;
 89227   /* Ensure that:
 89228   *  (a) the table is not read-only, 
 89229   *  (b) that if it is a view then ON INSERT triggers exist
 89230   */
 89231   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
 89232     goto insert_cleanup;
 89235   /* Allocate a VDBE
 89236   */
 89237   v = sqlite3GetVdbe(pParse);
 89238   if( v==0 ) goto insert_cleanup;
 89239   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 89240   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
 89242 #ifndef SQLITE_OMIT_XFER_OPT
 89243   /* If the statement is of the form
 89244   **
 89245   **       INSERT INTO <table1> SELECT * FROM <table2>;
 89246   **
 89247   ** Then special optimizations can be applied that make the transfer
 89248   ** very fast and which reduce fragmentation of indices.
 89249   **
 89250   ** This is the 2nd template.
 89251   */
 89252   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
 89253     assert( !pTrigger );
 89254     assert( pList==0 );
 89255     goto insert_end;
 89257 #endif /* SQLITE_OMIT_XFER_OPT */
 89259   /* If this is an AUTOINCREMENT table, look up the sequence number in the
 89260   ** sqlite_sequence table and store it in memory cell regAutoinc.
 89261   */
 89262   regAutoinc = autoIncBegin(pParse, iDb, pTab);
 89264   /* Figure out how many columns of data are supplied.  If the data
 89265   ** is coming from a SELECT statement, then generate a co-routine that
 89266   ** produces a single row of the SELECT on each invocation.  The
 89267   ** co-routine is the common header to the 3rd and 4th templates.
 89268   */
 89269   if( pSelect ){
 89270     /* Data is coming from a SELECT.  Generate a co-routine to run that
 89271     ** SELECT. */
 89272     int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
 89273     if( rc ) goto insert_cleanup;
 89275     regEof = dest.iSDParm + 1;
 89276     regFromSelect = dest.iSdst;
 89277     assert( pSelect->pEList );
 89278     nColumn = pSelect->pEList->nExpr;
 89279     assert( dest.nSdst==nColumn );
 89281     /* Set useTempTable to TRUE if the result of the SELECT statement
 89282     ** should be written into a temporary table (template 4).  Set to
 89283     ** FALSE if each* row of the SELECT can be written directly into
 89284     ** the destination table (template 3).
 89285     **
 89286     ** A temp table must be used if the table being updated is also one
 89287     ** of the tables being read by the SELECT statement.  Also use a 
 89288     ** temp table in the case of row triggers.
 89289     */
 89290     if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
 89291       useTempTable = 1;
 89294     if( useTempTable ){
 89295       /* Invoke the coroutine to extract information from the SELECT
 89296       ** and add it to a transient table srcTab.  The code generated
 89297       ** here is from the 4th template:
 89298       **
 89299       **      B: open temp table
 89300       **      L: yield X
 89301       **         if EOF goto M
 89302       **         insert row from R..R+n into temp table
 89303       **         goto L
 89304       **      M: ...
 89305       */
 89306       int regRec;          /* Register to hold packed record */
 89307       int regTempRowid;    /* Register to hold temp table ROWID */
 89308       int addrTop;         /* Label "L" */
 89309       int addrIf;          /* Address of jump to M */
 89311       srcTab = pParse->nTab++;
 89312       regRec = sqlite3GetTempReg(pParse);
 89313       regTempRowid = sqlite3GetTempReg(pParse);
 89314       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
 89315       addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 89316       addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
 89317       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
 89318       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
 89319       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
 89320       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
 89321       sqlite3VdbeJumpHere(v, addrIf);
 89322       sqlite3ReleaseTempReg(pParse, regRec);
 89323       sqlite3ReleaseTempReg(pParse, regTempRowid);
 89325   }else{
 89326     /* This is the case if the data for the INSERT is coming from a VALUES
 89327     ** clause
 89328     */
 89329     NameContext sNC;
 89330     memset(&sNC, 0, sizeof(sNC));
 89331     sNC.pParse = pParse;
 89332     srcTab = -1;
 89333     assert( useTempTable==0 );
 89334     nColumn = pList ? pList->nExpr : 0;
 89335     for(i=0; i<nColumn; i++){
 89336       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
 89337         goto insert_cleanup;
 89342   /* Make sure the number of columns in the source data matches the number
 89343   ** of columns to be inserted into the table.
 89344   */
 89345   if( IsVirtual(pTab) ){
 89346     for(i=0; i<pTab->nCol; i++){
 89347       nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
 89350   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
 89351     sqlite3ErrorMsg(pParse, 
 89352        "table %S has %d columns but %d values were supplied",
 89353        pTabList, 0, pTab->nCol-nHidden, nColumn);
 89354     goto insert_cleanup;
 89356   if( pColumn!=0 && nColumn!=pColumn->nId ){
 89357     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
 89358     goto insert_cleanup;
 89361   /* If the INSERT statement included an IDLIST term, then make sure
 89362   ** all elements of the IDLIST really are columns of the table and 
 89363   ** remember the column indices.
 89364   **
 89365   ** If the table has an INTEGER PRIMARY KEY column and that column
 89366   ** is named in the IDLIST, then record in the keyColumn variable
 89367   ** the index into IDLIST of the primary key column.  keyColumn is
 89368   ** the index of the primary key as it appears in IDLIST, not as
 89369   ** is appears in the original table.  (The index of the primary
 89370   ** key in the original table is pTab->iPKey.)
 89371   */
 89372   if( pColumn ){
 89373     for(i=0; i<pColumn->nId; i++){
 89374       pColumn->a[i].idx = -1;
 89376     for(i=0; i<pColumn->nId; i++){
 89377       for(j=0; j<pTab->nCol; j++){
 89378         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
 89379           pColumn->a[i].idx = j;
 89380           if( j==pTab->iPKey ){
 89381             keyColumn = i;
 89383           break;
 89386       if( j>=pTab->nCol ){
 89387         if( sqlite3IsRowid(pColumn->a[i].zName) ){
 89388           keyColumn = i;
 89389         }else{
 89390           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
 89391               pTabList, 0, pColumn->a[i].zName);
 89392           pParse->checkSchema = 1;
 89393           goto insert_cleanup;
 89399   /* If there is no IDLIST term but the table has an integer primary
 89400   ** key, the set the keyColumn variable to the primary key column index
 89401   ** in the original table definition.
 89402   */
 89403   if( pColumn==0 && nColumn>0 ){
 89404     keyColumn = pTab->iPKey;
 89407   /* Initialize the count of rows to be inserted
 89408   */
 89409   if( db->flags & SQLITE_CountRows ){
 89410     regRowCount = ++pParse->nMem;
 89411     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
 89414   /* If this is not a view, open the table and and all indices */
 89415   if( !isView ){
 89416     int nIdx;
 89418     baseCur = pParse->nTab;
 89419     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
 89420     aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
 89421     if( aRegIdx==0 ){
 89422       goto insert_cleanup;
 89424     for(i=0; i<nIdx; i++){
 89425       aRegIdx[i] = ++pParse->nMem;
 89429   /* This is the top of the main insertion loop */
 89430   if( useTempTable ){
 89431     /* This block codes the top of loop only.  The complete loop is the
 89432     ** following pseudocode (template 4):
 89433     **
 89434     **         rewind temp table
 89435     **      C: loop over rows of intermediate table
 89436     **           transfer values form intermediate table into <table>
 89437     **         end loop
 89438     **      D: ...
 89439     */
 89440     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
 89441     addrCont = sqlite3VdbeCurrentAddr(v);
 89442   }else if( pSelect ){
 89443     /* This block codes the top of loop only.  The complete loop is the
 89444     ** following pseudocode (template 3):
 89445     **
 89446     **      C: yield X
 89447     **         if EOF goto D
 89448     **         insert the select result into <table> from R..R+n
 89449     **         goto C
 89450     **      D: ...
 89451     */
 89452     addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 89453     addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
 89456   /* Allocate registers for holding the rowid of the new row,
 89457   ** the content of the new row, and the assemblied row record.
 89458   */
 89459   regRowid = regIns = pParse->nMem+1;
 89460   pParse->nMem += pTab->nCol + 1;
 89461   if( IsVirtual(pTab) ){
 89462     regRowid++;
 89463     pParse->nMem++;
 89465   regData = regRowid+1;
 89467   /* Run the BEFORE and INSTEAD OF triggers, if there are any
 89468   */
 89469   endOfLoop = sqlite3VdbeMakeLabel(v);
 89470   if( tmask & TRIGGER_BEFORE ){
 89471     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
 89473     /* build the NEW.* reference row.  Note that if there is an INTEGER
 89474     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
 89475     ** translated into a unique ID for the row.  But on a BEFORE trigger,
 89476     ** we do not know what the unique ID will be (because the insert has
 89477     ** not happened yet) so we substitute a rowid of -1
 89478     */
 89479     if( keyColumn<0 ){
 89480       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
 89481     }else{
 89482       int j1;
 89483       if( useTempTable ){
 89484         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
 89485       }else{
 89486         assert( pSelect==0 );  /* Otherwise useTempTable is true */
 89487         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
 89489       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
 89490       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
 89491       sqlite3VdbeJumpHere(v, j1);
 89492       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
 89495     /* Cannot have triggers on a virtual table. If it were possible,
 89496     ** this block would have to account for hidden column.
 89497     */
 89498     assert( !IsVirtual(pTab) );
 89500     /* Create the new column data
 89501     */
 89502     for(i=0; i<pTab->nCol; i++){
 89503       if( pColumn==0 ){
 89504         j = i;
 89505       }else{
 89506         for(j=0; j<pColumn->nId; j++){
 89507           if( pColumn->a[j].idx==i ) break;
 89510       if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
 89511         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
 89512       }else if( useTempTable ){
 89513         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
 89514       }else{
 89515         assert( pSelect==0 ); /* Otherwise useTempTable is true */
 89516         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
 89520     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
 89521     ** do not attempt any conversions before assembling the record.
 89522     ** If this is a real table, attempt conversions as required by the
 89523     ** table column affinities.
 89524     */
 89525     if( !isView ){
 89526       sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
 89527       sqlite3TableAffinityStr(v, pTab);
 89530     /* Fire BEFORE or INSTEAD OF triggers */
 89531     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
 89532         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
 89534     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
 89537   /* Push the record number for the new entry onto the stack.  The
 89538   ** record number is a randomly generate integer created by NewRowid
 89539   ** except when the table has an INTEGER PRIMARY KEY column, in which
 89540   ** case the record number is the same as that column. 
 89541   */
 89542   if( !isView ){
 89543     if( IsVirtual(pTab) ){
 89544       /* The row that the VUpdate opcode will delete: none */
 89545       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
 89547     if( keyColumn>=0 ){
 89548       if( useTempTable ){
 89549         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
 89550       }else if( pSelect ){
 89551         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
 89552       }else{
 89553         VdbeOp *pOp;
 89554         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
 89555         pOp = sqlite3VdbeGetOp(v, -1);
 89556         if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
 89557           appendFlag = 1;
 89558           pOp->opcode = OP_NewRowid;
 89559           pOp->p1 = baseCur;
 89560           pOp->p2 = regRowid;
 89561           pOp->p3 = regAutoinc;
 89564       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
 89565       ** to generate a unique primary key value.
 89566       */
 89567       if( !appendFlag ){
 89568         int j1;
 89569         if( !IsVirtual(pTab) ){
 89570           j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
 89571           sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
 89572           sqlite3VdbeJumpHere(v, j1);
 89573         }else{
 89574           j1 = sqlite3VdbeCurrentAddr(v);
 89575           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
 89577         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
 89579     }else if( IsVirtual(pTab) ){
 89580       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
 89581     }else{
 89582       sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
 89583       appendFlag = 1;
 89585     autoIncStep(pParse, regAutoinc, regRowid);
 89587     /* Push onto the stack, data for all columns of the new entry, beginning
 89588     ** with the first column.
 89589     */
 89590     nHidden = 0;
 89591     for(i=0; i<pTab->nCol; i++){
 89592       int iRegStore = regRowid+1+i;
 89593       if( i==pTab->iPKey ){
 89594         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
 89595         ** Whenever this column is read, the record number will be substituted
 89596         ** in its place.  So will fill this column with a NULL to avoid
 89597         ** taking up data space with information that will never be used. */
 89598         sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
 89599         continue;
 89601       if( pColumn==0 ){
 89602         if( IsHiddenColumn(&pTab->aCol[i]) ){
 89603           assert( IsVirtual(pTab) );
 89604           j = -1;
 89605           nHidden++;
 89606         }else{
 89607           j = i - nHidden;
 89609       }else{
 89610         for(j=0; j<pColumn->nId; j++){
 89611           if( pColumn->a[j].idx==i ) break;
 89614       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
 89615         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
 89616       }else if( useTempTable ){
 89617         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
 89618       }else if( pSelect ){
 89619         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
 89620       }else{
 89621         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
 89625     /* Generate code to check constraints and generate index keys and
 89626     ** do the insertion.
 89627     */
 89628 #ifndef SQLITE_OMIT_VIRTUALTABLE
 89629     if( IsVirtual(pTab) ){
 89630       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 89631       sqlite3VtabMakeWritable(pParse, pTab);
 89632       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
 89633       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
 89634       sqlite3MayAbort(pParse);
 89635     }else
 89636 #endif
 89638       int isReplace;    /* Set to true if constraints may cause a replace */
 89639       sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
 89640           keyColumn>=0, 0, onError, endOfLoop, &isReplace
 89641       );
 89642       sqlite3FkCheck(pParse, pTab, 0, regIns);
 89643       sqlite3CompleteInsertion(
 89644           pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
 89645       );
 89649   /* Update the count of rows that are inserted
 89650   */
 89651   if( (db->flags & SQLITE_CountRows)!=0 ){
 89652     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
 89655   if( pTrigger ){
 89656     /* Code AFTER triggers */
 89657     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
 89658         pTab, regData-2-pTab->nCol, onError, endOfLoop);
 89661   /* The bottom of the main insertion loop, if the data source
 89662   ** is a SELECT statement.
 89663   */
 89664   sqlite3VdbeResolveLabel(v, endOfLoop);
 89665   if( useTempTable ){
 89666     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
 89667     sqlite3VdbeJumpHere(v, addrInsTop);
 89668     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
 89669   }else if( pSelect ){
 89670     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
 89671     sqlite3VdbeJumpHere(v, addrInsTop);
 89674   if( !IsVirtual(pTab) && !isView ){
 89675     /* Close all tables opened */
 89676     sqlite3VdbeAddOp1(v, OP_Close, baseCur);
 89677     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
 89678       sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
 89682 insert_end:
 89683   /* Update the sqlite_sequence table by storing the content of the
 89684   ** maximum rowid counter values recorded while inserting into
 89685   ** autoincrement tables.
 89686   */
 89687   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 89688     sqlite3AutoincrementEnd(pParse);
 89691   /*
 89692   ** Return the number of rows inserted. If this routine is 
 89693   ** generating code because of a call to sqlite3NestedParse(), do not
 89694   ** invoke the callback function.
 89695   */
 89696   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
 89697     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
 89698     sqlite3VdbeSetNumCols(v, 1);
 89699     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
 89702 insert_cleanup:
 89703   sqlite3SrcListDelete(db, pTabList);
 89704   sqlite3ExprListDelete(db, pList);
 89705   sqlite3SelectDelete(db, pSelect);
 89706   sqlite3IdListDelete(db, pColumn);
 89707   sqlite3DbFree(db, aRegIdx);
 89710 /* Make sure "isView" and other macros defined above are undefined. Otherwise
 89711 ** thely may interfere with compilation of other functions in this file
 89712 ** (or in another file, if this file becomes part of the amalgamation).  */
 89713 #ifdef isView
 89714  #undef isView
 89715 #endif
 89716 #ifdef pTrigger
 89717  #undef pTrigger
 89718 #endif
 89719 #ifdef tmask
 89720  #undef tmask
 89721 #endif
 89724 /*
 89725 ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
 89726 **
 89727 ** The input is a range of consecutive registers as follows:
 89728 **
 89729 **    1.  The rowid of the row after the update.
 89730 **
 89731 **    2.  The data in the first column of the entry after the update.
 89732 **
 89733 **    i.  Data from middle columns...
 89734 **
 89735 **    N.  The data in the last column of the entry after the update.
 89736 **
 89737 ** The regRowid parameter is the index of the register containing (1).
 89738 **
 89739 ** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
 89740 ** the address of a register containing the rowid before the update takes
 89741 ** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
 89742 ** is false, indicating an INSERT statement, then a non-zero rowidChng 
 89743 ** indicates that the rowid was explicitly specified as part of the
 89744 ** INSERT statement. If rowidChng is false, it means that  the rowid is
 89745 ** computed automatically in an insert or that the rowid value is not 
 89746 ** modified by an update.
 89747 **
 89748 ** The code generated by this routine store new index entries into
 89749 ** registers identified by aRegIdx[].  No index entry is created for
 89750 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
 89751 ** the same as the order of indices on the linked list of indices
 89752 ** attached to the table.
 89753 **
 89754 ** This routine also generates code to check constraints.  NOT NULL,
 89755 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
 89756 ** then the appropriate action is performed.  There are five possible
 89757 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
 89758 **
 89759 **  Constraint type  Action       What Happens
 89760 **  ---------------  ----------   ----------------------------------------
 89761 **  any              ROLLBACK     The current transaction is rolled back and
 89762 **                                sqlite3_exec() returns immediately with a
 89763 **                                return code of SQLITE_CONSTRAINT.
 89764 **
 89765 **  any              ABORT        Back out changes from the current command
 89766 **                                only (do not do a complete rollback) then
 89767 **                                cause sqlite3_exec() to return immediately
 89768 **                                with SQLITE_CONSTRAINT.
 89769 **
 89770 **  any              FAIL         Sqlite3_exec() returns immediately with a
 89771 **                                return code of SQLITE_CONSTRAINT.  The
 89772 **                                transaction is not rolled back and any
 89773 **                                prior changes are retained.
 89774 **
 89775 **  any              IGNORE       The record number and data is popped from
 89776 **                                the stack and there is an immediate jump
 89777 **                                to label ignoreDest.
 89778 **
 89779 **  NOT NULL         REPLACE      The NULL value is replace by the default
 89780 **                                value for that column.  If the default value
 89781 **                                is NULL, the action is the same as ABORT.
 89782 **
 89783 **  UNIQUE           REPLACE      The other row that conflicts with the row
 89784 **                                being inserted is removed.
 89785 **
 89786 **  CHECK            REPLACE      Illegal.  The results in an exception.
 89787 **
 89788 ** Which action to take is determined by the overrideError parameter.
 89789 ** Or if overrideError==OE_Default, then the pParse->onError parameter
 89790 ** is used.  Or if pParse->onError==OE_Default then the onError value
 89791 ** for the constraint is used.
 89792 **
 89793 ** The calling routine must open a read/write cursor for pTab with
 89794 ** cursor number "baseCur".  All indices of pTab must also have open
 89795 ** read/write cursors with cursor number baseCur+i for the i-th cursor.
 89796 ** Except, if there is no possibility of a REPLACE action then
 89797 ** cursors do not need to be open for indices where aRegIdx[i]==0.
 89798 */
 89799 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
 89800   Parse *pParse,      /* The parser context */
 89801   Table *pTab,        /* the table into which we are inserting */
 89802   int baseCur,        /* Index of a read/write cursor pointing at pTab */
 89803   int regRowid,       /* Index of the range of input registers */
 89804   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
 89805   int rowidChng,      /* True if the rowid might collide with existing entry */
 89806   int isUpdate,       /* True for UPDATE, False for INSERT */
 89807   int overrideError,  /* Override onError to this if not OE_Default */
 89808   int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
 89809   int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
 89810 ){
 89811   int i;              /* loop counter */
 89812   Vdbe *v;            /* VDBE under constrution */
 89813   int nCol;           /* Number of columns */
 89814   int onError;        /* Conflict resolution strategy */
 89815   int j1;             /* Addresss of jump instruction */
 89816   int j2 = 0, j3;     /* Addresses of jump instructions */
 89817   int regData;        /* Register containing first data column */
 89818   int iCur;           /* Table cursor number */
 89819   Index *pIdx;         /* Pointer to one of the indices */
 89820   sqlite3 *db;         /* Database connection */
 89821   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
 89822   int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
 89824   db = pParse->db;
 89825   v = sqlite3GetVdbe(pParse);
 89826   assert( v!=0 );
 89827   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 89828   nCol = pTab->nCol;
 89829   regData = regRowid + 1;
 89831   /* Test all NOT NULL constraints.
 89832   */
 89833   for(i=0; i<nCol; i++){
 89834     if( i==pTab->iPKey ){
 89835       continue;
 89837     onError = pTab->aCol[i].notNull;
 89838     if( onError==OE_None ) continue;
 89839     if( overrideError!=OE_Default ){
 89840       onError = overrideError;
 89841     }else if( onError==OE_Default ){
 89842       onError = OE_Abort;
 89844     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
 89845       onError = OE_Abort;
 89847     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
 89848         || onError==OE_Ignore || onError==OE_Replace );
 89849     switch( onError ){
 89850       case OE_Abort:
 89851         sqlite3MayAbort(pParse);
 89852       case OE_Rollback:
 89853       case OE_Fail: {
 89854         char *zMsg;
 89855         sqlite3VdbeAddOp3(v, OP_HaltIfNull,
 89856                                   SQLITE_CONSTRAINT, onError, regData+i);
 89857         zMsg = sqlite3MPrintf(db, "%s.%s may not be NULL",
 89858                               pTab->zName, pTab->aCol[i].zName);
 89859         sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
 89860         break;
 89862       case OE_Ignore: {
 89863         sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
 89864         break;
 89866       default: {
 89867         assert( onError==OE_Replace );
 89868         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
 89869         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
 89870         sqlite3VdbeJumpHere(v, j1);
 89871         break;
 89876   /* Test all CHECK constraints
 89877   */
 89878 #ifndef SQLITE_OMIT_CHECK
 89879   if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
 89880     ExprList *pCheck = pTab->pCheck;
 89881     pParse->ckBase = regData;
 89882     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
 89883     for(i=0; i<pCheck->nExpr; i++){
 89884       int allOk = sqlite3VdbeMakeLabel(v);
 89885       sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
 89886       if( onError==OE_Ignore ){
 89887         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 89888       }else{
 89889         char *zConsName = pCheck->a[i].zName;
 89890         if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
 89891         if( zConsName ){
 89892           zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
 89893         }else{
 89894           zConsName = 0;
 89896         sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
 89898       sqlite3VdbeResolveLabel(v, allOk);
 89901 #endif /* !defined(SQLITE_OMIT_CHECK) */
 89903   /* If we have an INTEGER PRIMARY KEY, make sure the primary key
 89904   ** of the new record does not previously exist.  Except, if this
 89905   ** is an UPDATE and the primary key is not changing, that is OK.
 89906   */
 89907   if( rowidChng ){
 89908     onError = pTab->keyConf;
 89909     if( overrideError!=OE_Default ){
 89910       onError = overrideError;
 89911     }else if( onError==OE_Default ){
 89912       onError = OE_Abort;
 89915     if( isUpdate ){
 89916       j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
 89918     j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
 89919     switch( onError ){
 89920       default: {
 89921         onError = OE_Abort;
 89922         /* Fall thru into the next case */
 89924       case OE_Rollback:
 89925       case OE_Abort:
 89926       case OE_Fail: {
 89927         sqlite3HaltConstraint(
 89928           pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
 89929         break;
 89931       case OE_Replace: {
 89932         /* If there are DELETE triggers on this table and the
 89933         ** recursive-triggers flag is set, call GenerateRowDelete() to
 89934         ** remove the conflicting row from the table. This will fire
 89935         ** the triggers and remove both the table and index b-tree entries.
 89936         **
 89937         ** Otherwise, if there are no triggers or the recursive-triggers
 89938         ** flag is not set, but the table has one or more indexes, call 
 89939         ** GenerateRowIndexDelete(). This removes the index b-tree entries 
 89940         ** only. The table b-tree entry will be replaced by the new entry 
 89941         ** when it is inserted.  
 89942         **
 89943         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
 89944         ** also invoke MultiWrite() to indicate that this VDBE may require
 89945         ** statement rollback (if the statement is aborted after the delete
 89946         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
 89947         ** but being more selective here allows statements like:
 89948         **
 89949         **   REPLACE INTO t(rowid) VALUES($newrowid)
 89950         **
 89951         ** to run without a statement journal if there are no indexes on the
 89952         ** table.
 89953         */
 89954         Trigger *pTrigger = 0;
 89955         if( db->flags&SQLITE_RecTriggers ){
 89956           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 89958         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
 89959           sqlite3MultiWrite(pParse);
 89960           sqlite3GenerateRowDelete(
 89961               pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
 89962           );
 89963         }else if( pTab->pIndex ){
 89964           sqlite3MultiWrite(pParse);
 89965           sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
 89967         seenReplace = 1;
 89968         break;
 89970       case OE_Ignore: {
 89971         assert( seenReplace==0 );
 89972         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 89973         break;
 89976     sqlite3VdbeJumpHere(v, j3);
 89977     if( isUpdate ){
 89978       sqlite3VdbeJumpHere(v, j2);
 89982   /* Test all UNIQUE constraints by creating entries for each UNIQUE
 89983   ** index and making sure that duplicate entries do not already exist.
 89984   ** Add the new records to the indices as we go.
 89985   */
 89986   for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
 89987     int regIdx;
 89988     int regR;
 89990     if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
 89992     /* Create a key for accessing the index entry */
 89993     regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
 89994     for(i=0; i<pIdx->nColumn; i++){
 89995       int idx = pIdx->aiColumn[i];
 89996       if( idx==pTab->iPKey ){
 89997         sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
 89998       }else{
 89999         sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
 90002     sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
 90003     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
 90004     sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
 90005     sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
 90007     /* Find out what action to take in case there is an indexing conflict */
 90008     onError = pIdx->onError;
 90009     if( onError==OE_None ){ 
 90010       sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
 90011       continue;  /* pIdx is not a UNIQUE index */
 90013     if( overrideError!=OE_Default ){
 90014       onError = overrideError;
 90015     }else if( onError==OE_Default ){
 90016       onError = OE_Abort;
 90018     if( seenReplace ){
 90019       if( onError==OE_Ignore ) onError = OE_Replace;
 90020       else if( onError==OE_Fail ) onError = OE_Abort;
 90023     /* Check to see if the new index entry will be unique */
 90024     regR = sqlite3GetTempReg(pParse);
 90025     sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
 90026     j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
 90027                            regR, SQLITE_INT_TO_PTR(regIdx),
 90028                            P4_INT32);
 90029     sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
 90031     /* Generate code that executes if the new index entry is not unique */
 90032     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
 90033         || onError==OE_Ignore || onError==OE_Replace );
 90034     switch( onError ){
 90035       case OE_Rollback:
 90036       case OE_Abort:
 90037       case OE_Fail: {
 90038         int j;
 90039         StrAccum errMsg;
 90040         const char *zSep;
 90041         char *zErr;
 90043         sqlite3StrAccumInit(&errMsg, 0, 0, 200);
 90044         errMsg.db = db;
 90045         zSep = pIdx->nColumn>1 ? "columns " : "column ";
 90046         for(j=0; j<pIdx->nColumn; j++){
 90047           char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
 90048           sqlite3StrAccumAppend(&errMsg, zSep, -1);
 90049           zSep = ", ";
 90050           sqlite3StrAccumAppend(&errMsg, zCol, -1);
 90052         sqlite3StrAccumAppend(&errMsg,
 90053             pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
 90054         zErr = sqlite3StrAccumFinish(&errMsg);
 90055         sqlite3HaltConstraint(pParse, onError, zErr, 0);
 90056         sqlite3DbFree(errMsg.db, zErr);
 90057         break;
 90059       case OE_Ignore: {
 90060         assert( seenReplace==0 );
 90061         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 90062         break;
 90064       default: {
 90065         Trigger *pTrigger = 0;
 90066         assert( onError==OE_Replace );
 90067         sqlite3MultiWrite(pParse);
 90068         if( db->flags&SQLITE_RecTriggers ){
 90069           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 90071         sqlite3GenerateRowDelete(
 90072             pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
 90073         );
 90074         seenReplace = 1;
 90075         break;
 90078     sqlite3VdbeJumpHere(v, j3);
 90079     sqlite3ReleaseTempReg(pParse, regR);
 90082   if( pbMayReplace ){
 90083     *pbMayReplace = seenReplace;
 90087 /*
 90088 ** This routine generates code to finish the INSERT or UPDATE operation
 90089 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
 90090 ** A consecutive range of registers starting at regRowid contains the
 90091 ** rowid and the content to be inserted.
 90092 **
 90093 ** The arguments to this routine should be the same as the first six
 90094 ** arguments to sqlite3GenerateConstraintChecks.
 90095 */
 90096 SQLITE_PRIVATE void sqlite3CompleteInsertion(
 90097   Parse *pParse,      /* The parser context */
 90098   Table *pTab,        /* the table into which we are inserting */
 90099   int baseCur,        /* Index of a read/write cursor pointing at pTab */
 90100   int regRowid,       /* Range of content */
 90101   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
 90102   int isUpdate,       /* True for UPDATE, False for INSERT */
 90103   int appendBias,     /* True if this is likely to be an append */
 90104   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
 90105 ){
 90106   int i;
 90107   Vdbe *v;
 90108   int nIdx;
 90109   Index *pIdx;
 90110   u8 pik_flags;
 90111   int regData;
 90112   int regRec;
 90114   v = sqlite3GetVdbe(pParse);
 90115   assert( v!=0 );
 90116   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 90117   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
 90118   for(i=nIdx-1; i>=0; i--){
 90119     if( aRegIdx[i]==0 ) continue;
 90120     sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
 90121     if( useSeekResult ){
 90122       sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 90125   regData = regRowid + 1;
 90126   regRec = sqlite3GetTempReg(pParse);
 90127   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
 90128   sqlite3TableAffinityStr(v, pTab);
 90129   sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
 90130   if( pParse->nested ){
 90131     pik_flags = 0;
 90132   }else{
 90133     pik_flags = OPFLAG_NCHANGE;
 90134     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
 90136   if( appendBias ){
 90137     pik_flags |= OPFLAG_APPEND;
 90139   if( useSeekResult ){
 90140     pik_flags |= OPFLAG_USESEEKRESULT;
 90142   sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
 90143   if( !pParse->nested ){
 90144     sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 90146   sqlite3VdbeChangeP5(v, pik_flags);
 90149 /*
 90150 ** Generate code that will open cursors for a table and for all
 90151 ** indices of that table.  The "baseCur" parameter is the cursor number used
 90152 ** for the table.  Indices are opened on subsequent cursors.
 90153 **
 90154 ** Return the number of indices on the table.
 90155 */
 90156 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
 90157   Parse *pParse,   /* Parsing context */
 90158   Table *pTab,     /* Table to be opened */
 90159   int baseCur,     /* Cursor number assigned to the table */
 90160   int op           /* OP_OpenRead or OP_OpenWrite */
 90161 ){
 90162   int i;
 90163   int iDb;
 90164   Index *pIdx;
 90165   Vdbe *v;
 90167   if( IsVirtual(pTab) ) return 0;
 90168   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 90169   v = sqlite3GetVdbe(pParse);
 90170   assert( v!=0 );
 90171   sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
 90172   for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 90173     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 90174     assert( pIdx->pSchema==pTab->pSchema );
 90175     sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
 90176                       (char*)pKey, P4_KEYINFO_HANDOFF);
 90177     VdbeComment((v, "%s", pIdx->zName));
 90179   if( pParse->nTab<baseCur+i ){
 90180     pParse->nTab = baseCur+i;
 90182   return i-1;
 90186 #ifdef SQLITE_TEST
 90187 /*
 90188 ** The following global variable is incremented whenever the
 90189 ** transfer optimization is used.  This is used for testing
 90190 ** purposes only - to make sure the transfer optimization really
 90191 ** is happening when it is suppose to.
 90192 */
 90193 SQLITE_API int sqlite3_xferopt_count;
 90194 #endif /* SQLITE_TEST */
 90197 #ifndef SQLITE_OMIT_XFER_OPT
 90198 /*
 90199 ** Check to collation names to see if they are compatible.
 90200 */
 90201 static int xferCompatibleCollation(const char *z1, const char *z2){
 90202   if( z1==0 ){
 90203     return z2==0;
 90205   if( z2==0 ){
 90206     return 0;
 90208   return sqlite3StrICmp(z1, z2)==0;
 90212 /*
 90213 ** Check to see if index pSrc is compatible as a source of data
 90214 ** for index pDest in an insert transfer optimization.  The rules
 90215 ** for a compatible index:
 90216 **
 90217 **    *   The index is over the same set of columns
 90218 **    *   The same DESC and ASC markings occurs on all columns
 90219 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
 90220 **    *   The same collating sequence on each column
 90221 */
 90222 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
 90223   int i;
 90224   assert( pDest && pSrc );
 90225   assert( pDest->pTable!=pSrc->pTable );
 90226   if( pDest->nColumn!=pSrc->nColumn ){
 90227     return 0;   /* Different number of columns */
 90229   if( pDest->onError!=pSrc->onError ){
 90230     return 0;   /* Different conflict resolution strategies */
 90232   for(i=0; i<pSrc->nColumn; i++){
 90233     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
 90234       return 0;   /* Different columns indexed */
 90236     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
 90237       return 0;   /* Different sort orders */
 90239     if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
 90240       return 0;   /* Different collating sequences */
 90244   /* If no test above fails then the indices must be compatible */
 90245   return 1;
 90248 /*
 90249 ** Attempt the transfer optimization on INSERTs of the form
 90250 **
 90251 **     INSERT INTO tab1 SELECT * FROM tab2;
 90252 **
 90253 ** The xfer optimization transfers raw records from tab2 over to tab1.  
 90254 ** Columns are not decoded and reassemblied, which greatly improves
 90255 ** performance.  Raw index records are transferred in the same way.
 90256 **
 90257 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
 90258 ** There are lots of rules for determining compatibility - see comments
 90259 ** embedded in the code for details.
 90260 **
 90261 ** This routine returns TRUE if the optimization is guaranteed to be used.
 90262 ** Sometimes the xfer optimization will only work if the destination table
 90263 ** is empty - a factor that can only be determined at run-time.  In that
 90264 ** case, this routine generates code for the xfer optimization but also
 90265 ** does a test to see if the destination table is empty and jumps over the
 90266 ** xfer optimization code if the test fails.  In that case, this routine
 90267 ** returns FALSE so that the caller will know to go ahead and generate
 90268 ** an unoptimized transfer.  This routine also returns FALSE if there
 90269 ** is no chance that the xfer optimization can be applied.
 90270 **
 90271 ** This optimization is particularly useful at making VACUUM run faster.
 90272 */
 90273 static int xferOptimization(
 90274   Parse *pParse,        /* Parser context */
 90275   Table *pDest,         /* The table we are inserting into */
 90276   Select *pSelect,      /* A SELECT statement to use as the data source */
 90277   int onError,          /* How to handle constraint errors */
 90278   int iDbDest           /* The database of pDest */
 90279 ){
 90280   ExprList *pEList;                /* The result set of the SELECT */
 90281   Table *pSrc;                     /* The table in the FROM clause of SELECT */
 90282   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
 90283   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
 90284   int i;                           /* Loop counter */
 90285   int iDbSrc;                      /* The database of pSrc */
 90286   int iSrc, iDest;                 /* Cursors from source and destination */
 90287   int addr1, addr2;                /* Loop addresses */
 90288   int emptyDestTest;               /* Address of test for empty pDest */
 90289   int emptySrcTest;                /* Address of test for empty pSrc */
 90290   Vdbe *v;                         /* The VDBE we are building */
 90291   KeyInfo *pKey;                   /* Key information for an index */
 90292   int regAutoinc;                  /* Memory register used by AUTOINC */
 90293   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
 90294   int regData, regRowid;           /* Registers holding data and rowid */
 90296   if( pSelect==0 ){
 90297     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
 90299   if( sqlite3TriggerList(pParse, pDest) ){
 90300     return 0;   /* tab1 must not have triggers */
 90302 #ifndef SQLITE_OMIT_VIRTUALTABLE
 90303   if( pDest->tabFlags & TF_Virtual ){
 90304     return 0;   /* tab1 must not be a virtual table */
 90306 #endif
 90307   if( onError==OE_Default ){
 90308     if( pDest->iPKey>=0 ) onError = pDest->keyConf;
 90309     if( onError==OE_Default ) onError = OE_Abort;
 90311   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
 90312   if( pSelect->pSrc->nSrc!=1 ){
 90313     return 0;   /* FROM clause must have exactly one term */
 90315   if( pSelect->pSrc->a[0].pSelect ){
 90316     return 0;   /* FROM clause cannot contain a subquery */
 90318   if( pSelect->pWhere ){
 90319     return 0;   /* SELECT may not have a WHERE clause */
 90321   if( pSelect->pOrderBy ){
 90322     return 0;   /* SELECT may not have an ORDER BY clause */
 90324   /* Do not need to test for a HAVING clause.  If HAVING is present but
 90325   ** there is no ORDER BY, we will get an error. */
 90326   if( pSelect->pGroupBy ){
 90327     return 0;   /* SELECT may not have a GROUP BY clause */
 90329   if( pSelect->pLimit ){
 90330     return 0;   /* SELECT may not have a LIMIT clause */
 90332   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
 90333   if( pSelect->pPrior ){
 90334     return 0;   /* SELECT may not be a compound query */
 90336   if( pSelect->selFlags & SF_Distinct ){
 90337     return 0;   /* SELECT may not be DISTINCT */
 90339   pEList = pSelect->pEList;
 90340   assert( pEList!=0 );
 90341   if( pEList->nExpr!=1 ){
 90342     return 0;   /* The result set must have exactly one column */
 90344   assert( pEList->a[0].pExpr );
 90345   if( pEList->a[0].pExpr->op!=TK_ALL ){
 90346     return 0;   /* The result set must be the special operator "*" */
 90349   /* At this point we have established that the statement is of the
 90350   ** correct syntactic form to participate in this optimization.  Now
 90351   ** we have to check the semantics.
 90352   */
 90353   pItem = pSelect->pSrc->a;
 90354   pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
 90355   if( pSrc==0 ){
 90356     return 0;   /* FROM clause does not contain a real table */
 90358   if( pSrc==pDest ){
 90359     return 0;   /* tab1 and tab2 may not be the same table */
 90361 #ifndef SQLITE_OMIT_VIRTUALTABLE
 90362   if( pSrc->tabFlags & TF_Virtual ){
 90363     return 0;   /* tab2 must not be a virtual table */
 90365 #endif
 90366   if( pSrc->pSelect ){
 90367     return 0;   /* tab2 may not be a view */
 90369   if( pDest->nCol!=pSrc->nCol ){
 90370     return 0;   /* Number of columns must be the same in tab1 and tab2 */
 90372   if( pDest->iPKey!=pSrc->iPKey ){
 90373     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
 90375   for(i=0; i<pDest->nCol; i++){
 90376     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
 90377       return 0;    /* Affinity must be the same on all columns */
 90379     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
 90380       return 0;    /* Collating sequence must be the same on all columns */
 90382     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
 90383       return 0;    /* tab2 must be NOT NULL if tab1 is */
 90386   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
 90387     if( pDestIdx->onError!=OE_None ){
 90388       destHasUniqueIdx = 1;
 90390     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
 90391       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
 90393     if( pSrcIdx==0 ){
 90394       return 0;    /* pDestIdx has no corresponding index in pSrc */
 90397 #ifndef SQLITE_OMIT_CHECK
 90398   if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck, pDest->pCheck) ){
 90399     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
 90401 #endif
 90402 #ifndef SQLITE_OMIT_FOREIGN_KEY
 90403   /* Disallow the transfer optimization if the destination table constains
 90404   ** any foreign key constraints.  This is more restrictive than necessary.
 90405   ** But the main beneficiary of the transfer optimization is the VACUUM 
 90406   ** command, and the VACUUM command disables foreign key constraints.  So
 90407   ** the extra complication to make this rule less restrictive is probably
 90408   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
 90409   */
 90410   if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
 90411     return 0;
 90413 #endif
 90414   if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
 90415     return 0;  /* xfer opt does not play well with PRAGMA count_changes */
 90418   /* If we get this far, it means that the xfer optimization is at
 90419   ** least a possibility, though it might only work if the destination
 90420   ** table (tab1) is initially empty.
 90421   */
 90422 #ifdef SQLITE_TEST
 90423   sqlite3_xferopt_count++;
 90424 #endif
 90425   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
 90426   v = sqlite3GetVdbe(pParse);
 90427   sqlite3CodeVerifySchema(pParse, iDbSrc);
 90428   iSrc = pParse->nTab++;
 90429   iDest = pParse->nTab++;
 90430   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
 90431   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
 90432   if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
 90433    || destHasUniqueIdx                              /* (2) */
 90434    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
 90435   ){
 90436     /* In some circumstances, we are able to run the xfer optimization
 90437     ** only if the destination table is initially empty.  This code makes
 90438     ** that determination.  Conditions under which the destination must
 90439     ** be empty:
 90440     **
 90441     ** (1) There is no INTEGER PRIMARY KEY but there are indices.
 90442     **     (If the destination is not initially empty, the rowid fields
 90443     **     of index entries might need to change.)
 90444     **
 90445     ** (2) The destination has a unique index.  (The xfer optimization 
 90446     **     is unable to test uniqueness.)
 90447     **
 90448     ** (3) onError is something other than OE_Abort and OE_Rollback.
 90449     */
 90450     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
 90451     emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 90452     sqlite3VdbeJumpHere(v, addr1);
 90453   }else{
 90454     emptyDestTest = 0;
 90456   sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
 90457   emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
 90458   regData = sqlite3GetTempReg(pParse);
 90459   regRowid = sqlite3GetTempReg(pParse);
 90460   if( pDest->iPKey>=0 ){
 90461     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 90462     addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
 90463     sqlite3HaltConstraint(
 90464         pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
 90465     sqlite3VdbeJumpHere(v, addr2);
 90466     autoIncStep(pParse, regAutoinc, regRowid);
 90467   }else if( pDest->pIndex==0 ){
 90468     addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
 90469   }else{
 90470     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 90471     assert( (pDest->tabFlags & TF_Autoincrement)==0 );
 90473   sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
 90474   sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
 90475   sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
 90476   sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
 90477   sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
 90478   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
 90479     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
 90480       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
 90482     assert( pSrcIdx );
 90483     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 90484     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 90485     pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
 90486     sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
 90487                       (char*)pKey, P4_KEYINFO_HANDOFF);
 90488     VdbeComment((v, "%s", pSrcIdx->zName));
 90489     pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
 90490     sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
 90491                       (char*)pKey, P4_KEYINFO_HANDOFF);
 90492     VdbeComment((v, "%s", pDestIdx->zName));
 90493     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
 90494     sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
 90495     sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
 90496     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
 90497     sqlite3VdbeJumpHere(v, addr1);
 90499   sqlite3VdbeJumpHere(v, emptySrcTest);
 90500   sqlite3ReleaseTempReg(pParse, regRowid);
 90501   sqlite3ReleaseTempReg(pParse, regData);
 90502   sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 90503   sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 90504   if( emptyDestTest ){
 90505     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
 90506     sqlite3VdbeJumpHere(v, emptyDestTest);
 90507     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 90508     return 0;
 90509   }else{
 90510     return 1;
 90513 #endif /* SQLITE_OMIT_XFER_OPT */
 90515 /************** End of insert.c **********************************************/
 90516 /************** Begin file legacy.c ******************************************/
 90517 /*
 90518 ** 2001 September 15
 90519 **
 90520 ** The author disclaims copyright to this source code.  In place of
 90521 ** a legal notice, here is a blessing:
 90522 **
 90523 **    May you do good and not evil.
 90524 **    May you find forgiveness for yourself and forgive others.
 90525 **    May you share freely, never taking more than you give.
 90526 **
 90527 *************************************************************************
 90528 ** Main file for the SQLite library.  The routines in this file
 90529 ** implement the programmer interface to the library.  Routines in
 90530 ** other files are for internal use by SQLite and should not be
 90531 ** accessed by users of the library.
 90532 */
 90535 /*
 90536 ** Execute SQL code.  Return one of the SQLITE_ success/failure
 90537 ** codes.  Also write an error message into memory obtained from
 90538 ** malloc() and make *pzErrMsg point to that message.
 90539 **
 90540 ** If the SQL is a query, then for each row in the query result
 90541 ** the xCallback() function is called.  pArg becomes the first
 90542 ** argument to xCallback().  If xCallback=NULL then no callback
 90543 ** is invoked, even for queries.
 90544 */
 90545 SQLITE_API int sqlite3_exec(
 90546   sqlite3 *db,                /* The database on which the SQL executes */
 90547   const char *zSql,           /* The SQL to be executed */
 90548   sqlite3_callback xCallback, /* Invoke this callback routine */
 90549   void *pArg,                 /* First argument to xCallback() */
 90550   char **pzErrMsg             /* Write error messages here */
 90551 ){
 90552   int rc = SQLITE_OK;         /* Return code */
 90553   const char *zLeftover;      /* Tail of unprocessed SQL */
 90554   sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
 90555   char **azCols = 0;          /* Names of result columns */
 90556   int nRetry = 0;             /* Number of retry attempts */
 90557   int callbackIsInit;         /* True if callback data is initialized */
 90559   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
 90560   if( zSql==0 ) zSql = "";
 90562   sqlite3_mutex_enter(db->mutex);
 90563   sqlite3Error(db, SQLITE_OK, 0);
 90564   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
 90565     int nCol;
 90566     char **azVals = 0;
 90568     pStmt = 0;
 90569     rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
 90570     assert( rc==SQLITE_OK || pStmt==0 );
 90571     if( rc!=SQLITE_OK ){
 90572       continue;
 90574     if( !pStmt ){
 90575       /* this happens for a comment or white-space */
 90576       zSql = zLeftover;
 90577       continue;
 90580     callbackIsInit = 0;
 90581     nCol = sqlite3_column_count(pStmt);
 90583     while( 1 ){
 90584       int i;
 90585       rc = sqlite3_step(pStmt);
 90587       /* Invoke the callback function if required */
 90588       if( xCallback && (SQLITE_ROW==rc || 
 90589           (SQLITE_DONE==rc && !callbackIsInit
 90590                            && db->flags&SQLITE_NullCallback)) ){
 90591         if( !callbackIsInit ){
 90592           azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
 90593           if( azCols==0 ){
 90594             goto exec_out;
 90596           for(i=0; i<nCol; i++){
 90597             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
 90598             /* sqlite3VdbeSetColName() installs column names as UTF8
 90599             ** strings so there is no way for sqlite3_column_name() to fail. */
 90600             assert( azCols[i]!=0 );
 90602           callbackIsInit = 1;
 90604         if( rc==SQLITE_ROW ){
 90605           azVals = &azCols[nCol];
 90606           for(i=0; i<nCol; i++){
 90607             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
 90608             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
 90609               db->mallocFailed = 1;
 90610               goto exec_out;
 90614         if( xCallback(pArg, nCol, azVals, azCols) ){
 90615           rc = SQLITE_ABORT;
 90616           sqlite3VdbeFinalize((Vdbe *)pStmt);
 90617           pStmt = 0;
 90618           sqlite3Error(db, SQLITE_ABORT, 0);
 90619           goto exec_out;
 90623       if( rc!=SQLITE_ROW ){
 90624         rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
 90625         pStmt = 0;
 90626         if( rc!=SQLITE_SCHEMA ){
 90627           nRetry = 0;
 90628           zSql = zLeftover;
 90629           while( sqlite3Isspace(zSql[0]) ) zSql++;
 90631         break;
 90635     sqlite3DbFree(db, azCols);
 90636     azCols = 0;
 90639 exec_out:
 90640   if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
 90641   sqlite3DbFree(db, azCols);
 90643   rc = sqlite3ApiExit(db, rc);
 90644   if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
 90645     int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
 90646     *pzErrMsg = sqlite3Malloc(nErrMsg);
 90647     if( *pzErrMsg ){
 90648       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
 90649     }else{
 90650       rc = SQLITE_NOMEM;
 90651       sqlite3Error(db, SQLITE_NOMEM, 0);
 90653   }else if( pzErrMsg ){
 90654     *pzErrMsg = 0;
 90657   assert( (rc&db->errMask)==rc );
 90658   sqlite3_mutex_leave(db->mutex);
 90659   return rc;
 90662 /************** End of legacy.c **********************************************/
 90663 /************** Begin file loadext.c *****************************************/
 90664 /*
 90665 ** 2006 June 7
 90666 **
 90667 ** The author disclaims copyright to this source code.  In place of
 90668 ** a legal notice, here is a blessing:
 90669 **
 90670 **    May you do good and not evil.
 90671 **    May you find forgiveness for yourself and forgive others.
 90672 **    May you share freely, never taking more than you give.
 90673 **
 90674 *************************************************************************
 90675 ** This file contains code used to dynamically load extensions into
 90676 ** the SQLite library.
 90677 */
 90679 #ifndef SQLITE_CORE
 90680   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
 90681 #endif
 90682 /************** Include sqlite3ext.h in the middle of loadext.c **************/
 90683 /************** Begin file sqlite3ext.h **************************************/
 90684 /*
 90685 ** 2006 June 7
 90686 **
 90687 ** The author disclaims copyright to this source code.  In place of
 90688 ** a legal notice, here is a blessing:
 90689 **
 90690 **    May you do good and not evil.
 90691 **    May you find forgiveness for yourself and forgive others.
 90692 **    May you share freely, never taking more than you give.
 90693 **
 90694 *************************************************************************
 90695 ** This header file defines the SQLite interface for use by
 90696 ** shared libraries that want to be imported as extensions into
 90697 ** an SQLite instance.  Shared libraries that intend to be loaded
 90698 ** as extensions by SQLite should #include this file instead of 
 90699 ** sqlite3.h.
 90700 */
 90701 #ifndef _SQLITE3EXT_H_
 90702 #define _SQLITE3EXT_H_
 90704 typedef struct sqlite3_api_routines sqlite3_api_routines;
 90706 /*
 90707 ** The following structure holds pointers to all of the SQLite API
 90708 ** routines.
 90709 **
 90710 ** WARNING:  In order to maintain backwards compatibility, add new
 90711 ** interfaces to the end of this structure only.  If you insert new
 90712 ** interfaces in the middle of this structure, then older different
 90713 ** versions of SQLite will not be able to load each others' shared
 90714 ** libraries!
 90715 */
 90716 struct sqlite3_api_routines {
 90717   void * (*aggregate_context)(sqlite3_context*,int nBytes);
 90718   int  (*aggregate_count)(sqlite3_context*);
 90719   int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
 90720   int  (*bind_double)(sqlite3_stmt*,int,double);
 90721   int  (*bind_int)(sqlite3_stmt*,int,int);
 90722   int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
 90723   int  (*bind_null)(sqlite3_stmt*,int);
 90724   int  (*bind_parameter_count)(sqlite3_stmt*);
 90725   int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
 90726   const char * (*bind_parameter_name)(sqlite3_stmt*,int);
 90727   int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
 90728   int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
 90729   int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
 90730   int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
 90731   int  (*busy_timeout)(sqlite3*,int ms);
 90732   int  (*changes)(sqlite3*);
 90733   int  (*close)(sqlite3*);
 90734   int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
 90735                            int eTextRep,const char*));
 90736   int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
 90737                              int eTextRep,const void*));
 90738   const void * (*column_blob)(sqlite3_stmt*,int iCol);
 90739   int  (*column_bytes)(sqlite3_stmt*,int iCol);
 90740   int  (*column_bytes16)(sqlite3_stmt*,int iCol);
 90741   int  (*column_count)(sqlite3_stmt*pStmt);
 90742   const char * (*column_database_name)(sqlite3_stmt*,int);
 90743   const void * (*column_database_name16)(sqlite3_stmt*,int);
 90744   const char * (*column_decltype)(sqlite3_stmt*,int i);
 90745   const void * (*column_decltype16)(sqlite3_stmt*,int);
 90746   double  (*column_double)(sqlite3_stmt*,int iCol);
 90747   int  (*column_int)(sqlite3_stmt*,int iCol);
 90748   sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
 90749   const char * (*column_name)(sqlite3_stmt*,int);
 90750   const void * (*column_name16)(sqlite3_stmt*,int);
 90751   const char * (*column_origin_name)(sqlite3_stmt*,int);
 90752   const void * (*column_origin_name16)(sqlite3_stmt*,int);
 90753   const char * (*column_table_name)(sqlite3_stmt*,int);
 90754   const void * (*column_table_name16)(sqlite3_stmt*,int);
 90755   const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
 90756   const void * (*column_text16)(sqlite3_stmt*,int iCol);
 90757   int  (*column_type)(sqlite3_stmt*,int iCol);
 90758   sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
 90759   void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
 90760   int  (*complete)(const char*sql);
 90761   int  (*complete16)(const void*sql);
 90762   int  (*create_collation)(sqlite3*,const char*,int,void*,
 90763                            int(*)(void*,int,const void*,int,const void*));
 90764   int  (*create_collation16)(sqlite3*,const void*,int,void*,
 90765                              int(*)(void*,int,const void*,int,const void*));
 90766   int  (*create_function)(sqlite3*,const char*,int,int,void*,
 90767                           void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 90768                           void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 90769                           void (*xFinal)(sqlite3_context*));
 90770   int  (*create_function16)(sqlite3*,const void*,int,int,void*,
 90771                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 90772                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 90773                             void (*xFinal)(sqlite3_context*));
 90774   int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
 90775   int  (*data_count)(sqlite3_stmt*pStmt);
 90776   sqlite3 * (*db_handle)(sqlite3_stmt*);
 90777   int (*declare_vtab)(sqlite3*,const char*);
 90778   int  (*enable_shared_cache)(int);
 90779   int  (*errcode)(sqlite3*db);
 90780   const char * (*errmsg)(sqlite3*);
 90781   const void * (*errmsg16)(sqlite3*);
 90782   int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
 90783   int  (*expired)(sqlite3_stmt*);
 90784   int  (*finalize)(sqlite3_stmt*pStmt);
 90785   void  (*free)(void*);
 90786   void  (*free_table)(char**result);
 90787   int  (*get_autocommit)(sqlite3*);
 90788   void * (*get_auxdata)(sqlite3_context*,int);
 90789   int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
 90790   int  (*global_recover)(void);
 90791   void  (*interruptx)(sqlite3*);
 90792   sqlite_int64  (*last_insert_rowid)(sqlite3*);
 90793   const char * (*libversion)(void);
 90794   int  (*libversion_number)(void);
 90795   void *(*malloc)(int);
 90796   char * (*mprintf)(const char*,...);
 90797   int  (*open)(const char*,sqlite3**);
 90798   int  (*open16)(const void*,sqlite3**);
 90799   int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
 90800   int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
 90801   void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
 90802   void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
 90803   void *(*realloc)(void*,int);
 90804   int  (*reset)(sqlite3_stmt*pStmt);
 90805   void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
 90806   void  (*result_double)(sqlite3_context*,double);
 90807   void  (*result_error)(sqlite3_context*,const char*,int);
 90808   void  (*result_error16)(sqlite3_context*,const void*,int);
 90809   void  (*result_int)(sqlite3_context*,int);
 90810   void  (*result_int64)(sqlite3_context*,sqlite_int64);
 90811   void  (*result_null)(sqlite3_context*);
 90812   void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
 90813   void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
 90814   void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
 90815   void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
 90816   void  (*result_value)(sqlite3_context*,sqlite3_value*);
 90817   void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
 90818   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
 90819                          const char*,const char*),void*);
 90820   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
 90821   char * (*snprintf)(int,char*,const char*,...);
 90822   int  (*step)(sqlite3_stmt*);
 90823   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
 90824                                 char const**,char const**,int*,int*,int*);
 90825   void  (*thread_cleanup)(void);
 90826   int  (*total_changes)(sqlite3*);
 90827   void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
 90828   int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
 90829   void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
 90830                                          sqlite_int64),void*);
 90831   void * (*user_data)(sqlite3_context*);
 90832   const void * (*value_blob)(sqlite3_value*);
 90833   int  (*value_bytes)(sqlite3_value*);
 90834   int  (*value_bytes16)(sqlite3_value*);
 90835   double  (*value_double)(sqlite3_value*);
 90836   int  (*value_int)(sqlite3_value*);
 90837   sqlite_int64  (*value_int64)(sqlite3_value*);
 90838   int  (*value_numeric_type)(sqlite3_value*);
 90839   const unsigned char * (*value_text)(sqlite3_value*);
 90840   const void * (*value_text16)(sqlite3_value*);
 90841   const void * (*value_text16be)(sqlite3_value*);
 90842   const void * (*value_text16le)(sqlite3_value*);
 90843   int  (*value_type)(sqlite3_value*);
 90844   char *(*vmprintf)(const char*,va_list);
 90845   /* Added ??? */
 90846   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
 90847   /* Added by 3.3.13 */
 90848   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
 90849   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
 90850   int (*clear_bindings)(sqlite3_stmt*);
 90851   /* Added by 3.4.1 */
 90852   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
 90853                           void (*xDestroy)(void *));
 90854   /* Added by 3.5.0 */
 90855   int (*bind_zeroblob)(sqlite3_stmt*,int,int);
 90856   int (*blob_bytes)(sqlite3_blob*);
 90857   int (*blob_close)(sqlite3_blob*);
 90858   int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
 90859                    int,sqlite3_blob**);
 90860   int (*blob_read)(sqlite3_blob*,void*,int,int);
 90861   int (*blob_write)(sqlite3_blob*,const void*,int,int);
 90862   int (*create_collation_v2)(sqlite3*,const char*,int,void*,
 90863                              int(*)(void*,int,const void*,int,const void*),
 90864                              void(*)(void*));
 90865   int (*file_control)(sqlite3*,const char*,int,void*);
 90866   sqlite3_int64 (*memory_highwater)(int);
 90867   sqlite3_int64 (*memory_used)(void);
 90868   sqlite3_mutex *(*mutex_alloc)(int);
 90869   void (*mutex_enter)(sqlite3_mutex*);
 90870   void (*mutex_free)(sqlite3_mutex*);
 90871   void (*mutex_leave)(sqlite3_mutex*);
 90872   int (*mutex_try)(sqlite3_mutex*);
 90873   int (*open_v2)(const char*,sqlite3**,int,const char*);
 90874   int (*release_memory)(int);
 90875   void (*result_error_nomem)(sqlite3_context*);
 90876   void (*result_error_toobig)(sqlite3_context*);
 90877   int (*sleep)(int);
 90878   void (*soft_heap_limit)(int);
 90879   sqlite3_vfs *(*vfs_find)(const char*);
 90880   int (*vfs_register)(sqlite3_vfs*,int);
 90881   int (*vfs_unregister)(sqlite3_vfs*);
 90882   int (*xthreadsafe)(void);
 90883   void (*result_zeroblob)(sqlite3_context*,int);
 90884   void (*result_error_code)(sqlite3_context*,int);
 90885   int (*test_control)(int, ...);
 90886   void (*randomness)(int,void*);
 90887   sqlite3 *(*context_db_handle)(sqlite3_context*);
 90888   int (*extended_result_codes)(sqlite3*,int);
 90889   int (*limit)(sqlite3*,int,int);
 90890   sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
 90891   const char *(*sql)(sqlite3_stmt*);
 90892   int (*status)(int,int*,int*,int);
 90893   int (*backup_finish)(sqlite3_backup*);
 90894   sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
 90895   int (*backup_pagecount)(sqlite3_backup*);
 90896   int (*backup_remaining)(sqlite3_backup*);
 90897   int (*backup_step)(sqlite3_backup*,int);
 90898   const char *(*compileoption_get)(int);
 90899   int (*compileoption_used)(const char*);
 90900   int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
 90901                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 90902                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 90903                             void (*xFinal)(sqlite3_context*),
 90904                             void(*xDestroy)(void*));
 90905   int (*db_config)(sqlite3*,int,...);
 90906   sqlite3_mutex *(*db_mutex)(sqlite3*);
 90907   int (*db_status)(sqlite3*,int,int*,int*,int);
 90908   int (*extended_errcode)(sqlite3*);
 90909   void (*log)(int,const char*,...);
 90910   sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
 90911   const char *(*sourceid)(void);
 90912   int (*stmt_status)(sqlite3_stmt*,int,int);
 90913   int (*strnicmp)(const char*,const char*,int);
 90914   int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
 90915   int (*wal_autocheckpoint)(sqlite3*,int);
 90916   int (*wal_checkpoint)(sqlite3*,const char*);
 90917   void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
 90918   int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
 90919   int (*vtab_config)(sqlite3*,int op,...);
 90920   int (*vtab_on_conflict)(sqlite3*);
 90921 };
 90923 /*
 90924 ** The following macros redefine the API routines so that they are
 90925 ** redirected throught the global sqlite3_api structure.
 90926 **
 90927 ** This header file is also used by the loadext.c source file
 90928 ** (part of the main SQLite library - not an extension) so that
 90929 ** it can get access to the sqlite3_api_routines structure
 90930 ** definition.  But the main library does not want to redefine
 90931 ** the API.  So the redefinition macros are only valid if the
 90932 ** SQLITE_CORE macros is undefined.
 90933 */
 90934 #ifndef SQLITE_CORE
 90935 #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
 90936 #ifndef SQLITE_OMIT_DEPRECATED
 90937 #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
 90938 #endif
 90939 #define sqlite3_bind_blob              sqlite3_api->bind_blob
 90940 #define sqlite3_bind_double            sqlite3_api->bind_double
 90941 #define sqlite3_bind_int               sqlite3_api->bind_int
 90942 #define sqlite3_bind_int64             sqlite3_api->bind_int64
 90943 #define sqlite3_bind_null              sqlite3_api->bind_null
 90944 #define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
 90945 #define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
 90946 #define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
 90947 #define sqlite3_bind_text              sqlite3_api->bind_text
 90948 #define sqlite3_bind_text16            sqlite3_api->bind_text16
 90949 #define sqlite3_bind_value             sqlite3_api->bind_value
 90950 #define sqlite3_busy_handler           sqlite3_api->busy_handler
 90951 #define sqlite3_busy_timeout           sqlite3_api->busy_timeout
 90952 #define sqlite3_changes                sqlite3_api->changes
 90953 #define sqlite3_close                  sqlite3_api->close
 90954 #define sqlite3_collation_needed       sqlite3_api->collation_needed
 90955 #define sqlite3_collation_needed16     sqlite3_api->collation_needed16
 90956 #define sqlite3_column_blob            sqlite3_api->column_blob
 90957 #define sqlite3_column_bytes           sqlite3_api->column_bytes
 90958 #define sqlite3_column_bytes16         sqlite3_api->column_bytes16
 90959 #define sqlite3_column_count           sqlite3_api->column_count
 90960 #define sqlite3_column_database_name   sqlite3_api->column_database_name
 90961 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
 90962 #define sqlite3_column_decltype        sqlite3_api->column_decltype
 90963 #define sqlite3_column_decltype16      sqlite3_api->column_decltype16
 90964 #define sqlite3_column_double          sqlite3_api->column_double
 90965 #define sqlite3_column_int             sqlite3_api->column_int
 90966 #define sqlite3_column_int64           sqlite3_api->column_int64
 90967 #define sqlite3_column_name            sqlite3_api->column_name
 90968 #define sqlite3_column_name16          sqlite3_api->column_name16
 90969 #define sqlite3_column_origin_name     sqlite3_api->column_origin_name
 90970 #define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
 90971 #define sqlite3_column_table_name      sqlite3_api->column_table_name
 90972 #define sqlite3_column_table_name16    sqlite3_api->column_table_name16
 90973 #define sqlite3_column_text            sqlite3_api->column_text
 90974 #define sqlite3_column_text16          sqlite3_api->column_text16
 90975 #define sqlite3_column_type            sqlite3_api->column_type
 90976 #define sqlite3_column_value           sqlite3_api->column_value
 90977 #define sqlite3_commit_hook            sqlite3_api->commit_hook
 90978 #define sqlite3_complete               sqlite3_api->complete
 90979 #define sqlite3_complete16             sqlite3_api->complete16
 90980 #define sqlite3_create_collation       sqlite3_api->create_collation
 90981 #define sqlite3_create_collation16     sqlite3_api->create_collation16
 90982 #define sqlite3_create_function        sqlite3_api->create_function
 90983 #define sqlite3_create_function16      sqlite3_api->create_function16
 90984 #define sqlite3_create_module          sqlite3_api->create_module
 90985 #define sqlite3_create_module_v2       sqlite3_api->create_module_v2
 90986 #define sqlite3_data_count             sqlite3_api->data_count
 90987 #define sqlite3_db_handle              sqlite3_api->db_handle
 90988 #define sqlite3_declare_vtab           sqlite3_api->declare_vtab
 90989 #define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
 90990 #define sqlite3_errcode                sqlite3_api->errcode
 90991 #define sqlite3_errmsg                 sqlite3_api->errmsg
 90992 #define sqlite3_errmsg16               sqlite3_api->errmsg16
 90993 #define sqlite3_exec                   sqlite3_api->exec
 90994 #ifndef SQLITE_OMIT_DEPRECATED
 90995 #define sqlite3_expired                sqlite3_api->expired
 90996 #endif
 90997 #define sqlite3_finalize               sqlite3_api->finalize
 90998 #define sqlite3_free                   sqlite3_api->free
 90999 #define sqlite3_free_table             sqlite3_api->free_table
 91000 #define sqlite3_get_autocommit         sqlite3_api->get_autocommit
 91001 #define sqlite3_get_auxdata            sqlite3_api->get_auxdata
 91002 #define sqlite3_get_table              sqlite3_api->get_table
 91003 #ifndef SQLITE_OMIT_DEPRECATED
 91004 #define sqlite3_global_recover         sqlite3_api->global_recover
 91005 #endif
 91006 #define sqlite3_interrupt              sqlite3_api->interruptx
 91007 #define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
 91008 #define sqlite3_libversion             sqlite3_api->libversion
 91009 #define sqlite3_libversion_number      sqlite3_api->libversion_number
 91010 #define sqlite3_malloc                 sqlite3_api->malloc
 91011 #define sqlite3_mprintf                sqlite3_api->mprintf
 91012 #define sqlite3_open                   sqlite3_api->open
 91013 #define sqlite3_open16                 sqlite3_api->open16
 91014 #define sqlite3_prepare                sqlite3_api->prepare
 91015 #define sqlite3_prepare16              sqlite3_api->prepare16
 91016 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
 91017 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
 91018 #define sqlite3_profile                sqlite3_api->profile
 91019 #define sqlite3_progress_handler       sqlite3_api->progress_handler
 91020 #define sqlite3_realloc                sqlite3_api->realloc
 91021 #define sqlite3_reset                  sqlite3_api->reset
 91022 #define sqlite3_result_blob            sqlite3_api->result_blob
 91023 #define sqlite3_result_double          sqlite3_api->result_double
 91024 #define sqlite3_result_error           sqlite3_api->result_error
 91025 #define sqlite3_result_error16         sqlite3_api->result_error16
 91026 #define sqlite3_result_int             sqlite3_api->result_int
 91027 #define sqlite3_result_int64           sqlite3_api->result_int64
 91028 #define sqlite3_result_null            sqlite3_api->result_null
 91029 #define sqlite3_result_text            sqlite3_api->result_text
 91030 #define sqlite3_result_text16          sqlite3_api->result_text16
 91031 #define sqlite3_result_text16be        sqlite3_api->result_text16be
 91032 #define sqlite3_result_text16le        sqlite3_api->result_text16le
 91033 #define sqlite3_result_value           sqlite3_api->result_value
 91034 #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
 91035 #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
 91036 #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
 91037 #define sqlite3_snprintf               sqlite3_api->snprintf
 91038 #define sqlite3_step                   sqlite3_api->step
 91039 #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
 91040 #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
 91041 #define sqlite3_total_changes          sqlite3_api->total_changes
 91042 #define sqlite3_trace                  sqlite3_api->trace
 91043 #ifndef SQLITE_OMIT_DEPRECATED
 91044 #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
 91045 #endif
 91046 #define sqlite3_update_hook            sqlite3_api->update_hook
 91047 #define sqlite3_user_data              sqlite3_api->user_data
 91048 #define sqlite3_value_blob             sqlite3_api->value_blob
 91049 #define sqlite3_value_bytes            sqlite3_api->value_bytes
 91050 #define sqlite3_value_bytes16          sqlite3_api->value_bytes16
 91051 #define sqlite3_value_double           sqlite3_api->value_double
 91052 #define sqlite3_value_int              sqlite3_api->value_int
 91053 #define sqlite3_value_int64            sqlite3_api->value_int64
 91054 #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
 91055 #define sqlite3_value_text             sqlite3_api->value_text
 91056 #define sqlite3_value_text16           sqlite3_api->value_text16
 91057 #define sqlite3_value_text16be         sqlite3_api->value_text16be
 91058 #define sqlite3_value_text16le         sqlite3_api->value_text16le
 91059 #define sqlite3_value_type             sqlite3_api->value_type
 91060 #define sqlite3_vmprintf               sqlite3_api->vmprintf
 91061 #define sqlite3_overload_function      sqlite3_api->overload_function
 91062 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
 91063 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
 91064 #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
 91065 #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
 91066 #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
 91067 #define sqlite3_blob_close             sqlite3_api->blob_close
 91068 #define sqlite3_blob_open              sqlite3_api->blob_open
 91069 #define sqlite3_blob_read              sqlite3_api->blob_read
 91070 #define sqlite3_blob_write             sqlite3_api->blob_write
 91071 #define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
 91072 #define sqlite3_file_control           sqlite3_api->file_control
 91073 #define sqlite3_memory_highwater       sqlite3_api->memory_highwater
 91074 #define sqlite3_memory_used            sqlite3_api->memory_used
 91075 #define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
 91076 #define sqlite3_mutex_enter            sqlite3_api->mutex_enter
 91077 #define sqlite3_mutex_free             sqlite3_api->mutex_free
 91078 #define sqlite3_mutex_leave            sqlite3_api->mutex_leave
 91079 #define sqlite3_mutex_try              sqlite3_api->mutex_try
 91080 #define sqlite3_open_v2                sqlite3_api->open_v2
 91081 #define sqlite3_release_memory         sqlite3_api->release_memory
 91082 #define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
 91083 #define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
 91084 #define sqlite3_sleep                  sqlite3_api->sleep
 91085 #define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
 91086 #define sqlite3_vfs_find               sqlite3_api->vfs_find
 91087 #define sqlite3_vfs_register           sqlite3_api->vfs_register
 91088 #define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
 91089 #define sqlite3_threadsafe             sqlite3_api->xthreadsafe
 91090 #define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
 91091 #define sqlite3_result_error_code      sqlite3_api->result_error_code
 91092 #define sqlite3_test_control           sqlite3_api->test_control
 91093 #define sqlite3_randomness             sqlite3_api->randomness
 91094 #define sqlite3_context_db_handle      sqlite3_api->context_db_handle
 91095 #define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
 91096 #define sqlite3_limit                  sqlite3_api->limit
 91097 #define sqlite3_next_stmt              sqlite3_api->next_stmt
 91098 #define sqlite3_sql                    sqlite3_api->sql
 91099 #define sqlite3_status                 sqlite3_api->status
 91100 #define sqlite3_backup_finish          sqlite3_api->backup_finish
 91101 #define sqlite3_backup_init            sqlite3_api->backup_init
 91102 #define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
 91103 #define sqlite3_backup_remaining       sqlite3_api->backup_remaining
 91104 #define sqlite3_backup_step            sqlite3_api->backup_step
 91105 #define sqlite3_compileoption_get      sqlite3_api->compileoption_get
 91106 #define sqlite3_compileoption_used     sqlite3_api->compileoption_used
 91107 #define sqlite3_create_function_v2     sqlite3_api->create_function_v2
 91108 #define sqlite3_db_config              sqlite3_api->db_config
 91109 #define sqlite3_db_mutex               sqlite3_api->db_mutex
 91110 #define sqlite3_db_status              sqlite3_api->db_status
 91111 #define sqlite3_extended_errcode       sqlite3_api->extended_errcode
 91112 #define sqlite3_log                    sqlite3_api->log
 91113 #define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
 91114 #define sqlite3_sourceid               sqlite3_api->sourceid
 91115 #define sqlite3_stmt_status            sqlite3_api->stmt_status
 91116 #define sqlite3_strnicmp               sqlite3_api->strnicmp
 91117 #define sqlite3_unlock_notify          sqlite3_api->unlock_notify
 91118 #define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
 91119 #define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
 91120 #define sqlite3_wal_hook               sqlite3_api->wal_hook
 91121 #define sqlite3_blob_reopen            sqlite3_api->blob_reopen
 91122 #define sqlite3_vtab_config            sqlite3_api->vtab_config
 91123 #define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
 91124 #endif /* SQLITE_CORE */
 91126 #define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api = 0;
 91127 #define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
 91129 #endif /* _SQLITE3EXT_H_ */
 91131 /************** End of sqlite3ext.h ******************************************/
 91132 /************** Continuing where we left off in loadext.c ********************/
 91133 /* #include <string.h> */
 91135 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 91137 /*
 91138 ** Some API routines are omitted when various features are
 91139 ** excluded from a build of SQLite.  Substitute a NULL pointer
 91140 ** for any missing APIs.
 91141 */
 91142 #ifndef SQLITE_ENABLE_COLUMN_METADATA
 91143 # define sqlite3_column_database_name   0
 91144 # define sqlite3_column_database_name16 0
 91145 # define sqlite3_column_table_name      0
 91146 # define sqlite3_column_table_name16    0
 91147 # define sqlite3_column_origin_name     0
 91148 # define sqlite3_column_origin_name16   0
 91149 # define sqlite3_table_column_metadata  0
 91150 #endif
 91152 #ifdef SQLITE_OMIT_AUTHORIZATION
 91153 # define sqlite3_set_authorizer         0
 91154 #endif
 91156 #ifdef SQLITE_OMIT_UTF16
 91157 # define sqlite3_bind_text16            0
 91158 # define sqlite3_collation_needed16     0
 91159 # define sqlite3_column_decltype16      0
 91160 # define sqlite3_column_name16          0
 91161 # define sqlite3_column_text16          0
 91162 # define sqlite3_complete16             0
 91163 # define sqlite3_create_collation16     0
 91164 # define sqlite3_create_function16      0
 91165 # define sqlite3_errmsg16               0
 91166 # define sqlite3_open16                 0
 91167 # define sqlite3_prepare16              0
 91168 # define sqlite3_prepare16_v2           0
 91169 # define sqlite3_result_error16         0
 91170 # define sqlite3_result_text16          0
 91171 # define sqlite3_result_text16be        0
 91172 # define sqlite3_result_text16le        0
 91173 # define sqlite3_value_text16           0
 91174 # define sqlite3_value_text16be         0
 91175 # define sqlite3_value_text16le         0
 91176 # define sqlite3_column_database_name16 0
 91177 # define sqlite3_column_table_name16    0
 91178 # define sqlite3_column_origin_name16   0
 91179 #endif
 91181 #ifdef SQLITE_OMIT_COMPLETE
 91182 # define sqlite3_complete 0
 91183 # define sqlite3_complete16 0
 91184 #endif
 91186 #ifdef SQLITE_OMIT_DECLTYPE
 91187 # define sqlite3_column_decltype16      0
 91188 # define sqlite3_column_decltype        0
 91189 #endif
 91191 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
 91192 # define sqlite3_progress_handler 0
 91193 #endif
 91195 #ifdef SQLITE_OMIT_VIRTUALTABLE
 91196 # define sqlite3_create_module 0
 91197 # define sqlite3_create_module_v2 0
 91198 # define sqlite3_declare_vtab 0
 91199 # define sqlite3_vtab_config 0
 91200 # define sqlite3_vtab_on_conflict 0
 91201 #endif
 91203 #ifdef SQLITE_OMIT_SHARED_CACHE
 91204 # define sqlite3_enable_shared_cache 0
 91205 #endif
 91207 #ifdef SQLITE_OMIT_TRACE
 91208 # define sqlite3_profile       0
 91209 # define sqlite3_trace         0
 91210 #endif
 91212 #ifdef SQLITE_OMIT_GET_TABLE
 91213 # define sqlite3_free_table    0
 91214 # define sqlite3_get_table     0
 91215 #endif
 91217 #ifdef SQLITE_OMIT_INCRBLOB
 91218 #define sqlite3_bind_zeroblob  0
 91219 #define sqlite3_blob_bytes     0
 91220 #define sqlite3_blob_close     0
 91221 #define sqlite3_blob_open      0
 91222 #define sqlite3_blob_read      0
 91223 #define sqlite3_blob_write     0
 91224 #define sqlite3_blob_reopen    0
 91225 #endif
 91227 /*
 91228 ** The following structure contains pointers to all SQLite API routines.
 91229 ** A pointer to this structure is passed into extensions when they are
 91230 ** loaded so that the extension can make calls back into the SQLite
 91231 ** library.
 91232 **
 91233 ** When adding new APIs, add them to the bottom of this structure
 91234 ** in order to preserve backwards compatibility.
 91235 **
 91236 ** Extensions that use newer APIs should first call the
 91237 ** sqlite3_libversion_number() to make sure that the API they
 91238 ** intend to use is supported by the library.  Extensions should
 91239 ** also check to make sure that the pointer to the function is
 91240 ** not NULL before calling it.
 91241 */
 91242 static const sqlite3_api_routines sqlite3Apis = {
 91243   sqlite3_aggregate_context,
 91244 #ifndef SQLITE_OMIT_DEPRECATED
 91245   sqlite3_aggregate_count,
 91246 #else
 91247   0,
 91248 #endif
 91249   sqlite3_bind_blob,
 91250   sqlite3_bind_double,
 91251   sqlite3_bind_int,
 91252   sqlite3_bind_int64,
 91253   sqlite3_bind_null,
 91254   sqlite3_bind_parameter_count,
 91255   sqlite3_bind_parameter_index,
 91256   sqlite3_bind_parameter_name,
 91257   sqlite3_bind_text,
 91258   sqlite3_bind_text16,
 91259   sqlite3_bind_value,
 91260   sqlite3_busy_handler,
 91261   sqlite3_busy_timeout,
 91262   sqlite3_changes,
 91263   sqlite3_close,
 91264   sqlite3_collation_needed,
 91265   sqlite3_collation_needed16,
 91266   sqlite3_column_blob,
 91267   sqlite3_column_bytes,
 91268   sqlite3_column_bytes16,
 91269   sqlite3_column_count,
 91270   sqlite3_column_database_name,
 91271   sqlite3_column_database_name16,
 91272   sqlite3_column_decltype,
 91273   sqlite3_column_decltype16,
 91274   sqlite3_column_double,
 91275   sqlite3_column_int,
 91276   sqlite3_column_int64,
 91277   sqlite3_column_name,
 91278   sqlite3_column_name16,
 91279   sqlite3_column_origin_name,
 91280   sqlite3_column_origin_name16,
 91281   sqlite3_column_table_name,
 91282   sqlite3_column_table_name16,
 91283   sqlite3_column_text,
 91284   sqlite3_column_text16,
 91285   sqlite3_column_type,
 91286   sqlite3_column_value,
 91287   sqlite3_commit_hook,
 91288   sqlite3_complete,
 91289   sqlite3_complete16,
 91290   sqlite3_create_collation,
 91291   sqlite3_create_collation16,
 91292   sqlite3_create_function,
 91293   sqlite3_create_function16,
 91294   sqlite3_create_module,
 91295   sqlite3_data_count,
 91296   sqlite3_db_handle,
 91297   sqlite3_declare_vtab,
 91298   sqlite3_enable_shared_cache,
 91299   sqlite3_errcode,
 91300   sqlite3_errmsg,
 91301   sqlite3_errmsg16,
 91302   sqlite3_exec,
 91303 #ifndef SQLITE_OMIT_DEPRECATED
 91304   sqlite3_expired,
 91305 #else
 91306   0,
 91307 #endif
 91308   sqlite3_finalize,
 91309   sqlite3_free,
 91310   sqlite3_free_table,
 91311   sqlite3_get_autocommit,
 91312   sqlite3_get_auxdata,
 91313   sqlite3_get_table,
 91314   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
 91315   sqlite3_interrupt,
 91316   sqlite3_last_insert_rowid,
 91317   sqlite3_libversion,
 91318   sqlite3_libversion_number,
 91319   sqlite3_malloc,
 91320   sqlite3_mprintf,
 91321   sqlite3_open,
 91322   sqlite3_open16,
 91323   sqlite3_prepare,
 91324   sqlite3_prepare16,
 91325   sqlite3_profile,
 91326   sqlite3_progress_handler,
 91327   sqlite3_realloc,
 91328   sqlite3_reset,
 91329   sqlite3_result_blob,
 91330   sqlite3_result_double,
 91331   sqlite3_result_error,
 91332   sqlite3_result_error16,
 91333   sqlite3_result_int,
 91334   sqlite3_result_int64,
 91335   sqlite3_result_null,
 91336   sqlite3_result_text,
 91337   sqlite3_result_text16,
 91338   sqlite3_result_text16be,
 91339   sqlite3_result_text16le,
 91340   sqlite3_result_value,
 91341   sqlite3_rollback_hook,
 91342   sqlite3_set_authorizer,
 91343   sqlite3_set_auxdata,
 91344   sqlite3_snprintf,
 91345   sqlite3_step,
 91346   sqlite3_table_column_metadata,
 91347 #ifndef SQLITE_OMIT_DEPRECATED
 91348   sqlite3_thread_cleanup,
 91349 #else
 91350   0,
 91351 #endif
 91352   sqlite3_total_changes,
 91353   sqlite3_trace,
 91354 #ifndef SQLITE_OMIT_DEPRECATED
 91355   sqlite3_transfer_bindings,
 91356 #else
 91357   0,
 91358 #endif
 91359   sqlite3_update_hook,
 91360   sqlite3_user_data,
 91361   sqlite3_value_blob,
 91362   sqlite3_value_bytes,
 91363   sqlite3_value_bytes16,
 91364   sqlite3_value_double,
 91365   sqlite3_value_int,
 91366   sqlite3_value_int64,
 91367   sqlite3_value_numeric_type,
 91368   sqlite3_value_text,
 91369   sqlite3_value_text16,
 91370   sqlite3_value_text16be,
 91371   sqlite3_value_text16le,
 91372   sqlite3_value_type,
 91373   sqlite3_vmprintf,
 91374   /*
 91375   ** The original API set ends here.  All extensions can call any
 91376   ** of the APIs above provided that the pointer is not NULL.  But
 91377   ** before calling APIs that follow, extension should check the
 91378   ** sqlite3_libversion_number() to make sure they are dealing with
 91379   ** a library that is new enough to support that API.
 91380   *************************************************************************
 91381   */
 91382   sqlite3_overload_function,
 91384   /*
 91385   ** Added after 3.3.13
 91386   */
 91387   sqlite3_prepare_v2,
 91388   sqlite3_prepare16_v2,
 91389   sqlite3_clear_bindings,
 91391   /*
 91392   ** Added for 3.4.1
 91393   */
 91394   sqlite3_create_module_v2,
 91396   /*
 91397   ** Added for 3.5.0
 91398   */
 91399   sqlite3_bind_zeroblob,
 91400   sqlite3_blob_bytes,
 91401   sqlite3_blob_close,
 91402   sqlite3_blob_open,
 91403   sqlite3_blob_read,
 91404   sqlite3_blob_write,
 91405   sqlite3_create_collation_v2,
 91406   sqlite3_file_control,
 91407   sqlite3_memory_highwater,
 91408   sqlite3_memory_used,
 91409 #ifdef SQLITE_MUTEX_OMIT
 91410   0, 
 91411   0, 
 91412   0,
 91413   0,
 91414   0,
 91415 #else
 91416   sqlite3_mutex_alloc,
 91417   sqlite3_mutex_enter,
 91418   sqlite3_mutex_free,
 91419   sqlite3_mutex_leave,
 91420   sqlite3_mutex_try,
 91421 #endif
 91422   sqlite3_open_v2,
 91423   sqlite3_release_memory,
 91424   sqlite3_result_error_nomem,
 91425   sqlite3_result_error_toobig,
 91426   sqlite3_sleep,
 91427   sqlite3_soft_heap_limit,
 91428   sqlite3_vfs_find,
 91429   sqlite3_vfs_register,
 91430   sqlite3_vfs_unregister,
 91432   /*
 91433   ** Added for 3.5.8
 91434   */
 91435   sqlite3_threadsafe,
 91436   sqlite3_result_zeroblob,
 91437   sqlite3_result_error_code,
 91438   sqlite3_test_control,
 91439   sqlite3_randomness,
 91440   sqlite3_context_db_handle,
 91442   /*
 91443   ** Added for 3.6.0
 91444   */
 91445   sqlite3_extended_result_codes,
 91446   sqlite3_limit,
 91447   sqlite3_next_stmt,
 91448   sqlite3_sql,
 91449   sqlite3_status,
 91451   /*
 91452   ** Added for 3.7.4
 91453   */
 91454   sqlite3_backup_finish,
 91455   sqlite3_backup_init,
 91456   sqlite3_backup_pagecount,
 91457   sqlite3_backup_remaining,
 91458   sqlite3_backup_step,
 91459 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 91460   sqlite3_compileoption_get,
 91461   sqlite3_compileoption_used,
 91462 #else
 91463   0,
 91464   0,
 91465 #endif
 91466   sqlite3_create_function_v2,
 91467   sqlite3_db_config,
 91468   sqlite3_db_mutex,
 91469   sqlite3_db_status,
 91470   sqlite3_extended_errcode,
 91471   sqlite3_log,
 91472   sqlite3_soft_heap_limit64,
 91473   sqlite3_sourceid,
 91474   sqlite3_stmt_status,
 91475   sqlite3_strnicmp,
 91476 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 91477   sqlite3_unlock_notify,
 91478 #else
 91479   0,
 91480 #endif
 91481 #ifndef SQLITE_OMIT_WAL
 91482   sqlite3_wal_autocheckpoint,
 91483   sqlite3_wal_checkpoint,
 91484   sqlite3_wal_hook,
 91485 #else
 91486   0,
 91487   0,
 91488   0,
 91489 #endif
 91490   sqlite3_blob_reopen,
 91491   sqlite3_vtab_config,
 91492   sqlite3_vtab_on_conflict,
 91493 };
 91495 /*
 91496 ** Attempt to load an SQLite extension library contained in the file
 91497 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
 91498 ** default entry point name (sqlite3_extension_init) is used.  Use
 91499 ** of the default name is recommended.
 91500 **
 91501 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
 91502 **
 91503 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
 91504 ** error message text.  The calling function should free this memory
 91505 ** by calling sqlite3DbFree(db, ).
 91506 */
 91507 static int sqlite3LoadExtension(
 91508   sqlite3 *db,          /* Load the extension into this database connection */
 91509   const char *zFile,    /* Name of the shared library containing extension */
 91510   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
 91511   char **pzErrMsg       /* Put error message here if not 0 */
 91512 ){
 91513   sqlite3_vfs *pVfs = db->pVfs;
 91514   void *handle;
 91515   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 91516   char *zErrmsg = 0;
 91517   void **aHandle;
 91518   int nMsg = 300 + sqlite3Strlen30(zFile);
 91520   if( pzErrMsg ) *pzErrMsg = 0;
 91522   /* Ticket #1863.  To avoid a creating security problems for older
 91523   ** applications that relink against newer versions of SQLite, the
 91524   ** ability to run load_extension is turned off by default.  One
 91525   ** must call sqlite3_enable_load_extension() to turn on extension
 91526   ** loading.  Otherwise you get the following error.
 91527   */
 91528   if( (db->flags & SQLITE_LoadExtension)==0 ){
 91529     if( pzErrMsg ){
 91530       *pzErrMsg = sqlite3_mprintf("not authorized");
 91532     return SQLITE_ERROR;
 91535   if( zProc==0 ){
 91536     zProc = "sqlite3_extension_init";
 91539   handle = sqlite3OsDlOpen(pVfs, zFile);
 91540   if( handle==0 ){
 91541     if( pzErrMsg ){
 91542       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
 91543       if( zErrmsg ){
 91544         sqlite3_snprintf(nMsg, zErrmsg, 
 91545             "unable to open shared library [%s]", zFile);
 91546         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
 91549     return SQLITE_ERROR;
 91551   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 91552                    sqlite3OsDlSym(pVfs, handle, zProc);
 91553   if( xInit==0 ){
 91554     if( pzErrMsg ){
 91555       nMsg += sqlite3Strlen30(zProc);
 91556       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
 91557       if( zErrmsg ){
 91558         sqlite3_snprintf(nMsg, zErrmsg,
 91559             "no entry point [%s] in shared library [%s]", zProc,zFile);
 91560         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
 91562       sqlite3OsDlClose(pVfs, handle);
 91564     return SQLITE_ERROR;
 91565   }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
 91566     if( pzErrMsg ){
 91567       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
 91569     sqlite3_free(zErrmsg);
 91570     sqlite3OsDlClose(pVfs, handle);
 91571     return SQLITE_ERROR;
 91574   /* Append the new shared library handle to the db->aExtension array. */
 91575   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
 91576   if( aHandle==0 ){
 91577     return SQLITE_NOMEM;
 91579   if( db->nExtension>0 ){
 91580     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
 91582   sqlite3DbFree(db, db->aExtension);
 91583   db->aExtension = aHandle;
 91585   db->aExtension[db->nExtension++] = handle;
 91586   return SQLITE_OK;
 91588 SQLITE_API int sqlite3_load_extension(
 91589   sqlite3 *db,          /* Load the extension into this database connection */
 91590   const char *zFile,    /* Name of the shared library containing extension */
 91591   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
 91592   char **pzErrMsg       /* Put error message here if not 0 */
 91593 ){
 91594   int rc;
 91595   sqlite3_mutex_enter(db->mutex);
 91596   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
 91597   rc = sqlite3ApiExit(db, rc);
 91598   sqlite3_mutex_leave(db->mutex);
 91599   return rc;
 91602 /*
 91603 ** Call this routine when the database connection is closing in order
 91604 ** to clean up loaded extensions
 91605 */
 91606 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
 91607   int i;
 91608   assert( sqlite3_mutex_held(db->mutex) );
 91609   for(i=0; i<db->nExtension; i++){
 91610     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
 91612   sqlite3DbFree(db, db->aExtension);
 91615 /*
 91616 ** Enable or disable extension loading.  Extension loading is disabled by
 91617 ** default so as not to open security holes in older applications.
 91618 */
 91619 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
 91620   sqlite3_mutex_enter(db->mutex);
 91621   if( onoff ){
 91622     db->flags |= SQLITE_LoadExtension;
 91623   }else{
 91624     db->flags &= ~SQLITE_LoadExtension;
 91626   sqlite3_mutex_leave(db->mutex);
 91627   return SQLITE_OK;
 91630 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
 91632 /*
 91633 ** The auto-extension code added regardless of whether or not extension
 91634 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
 91635 ** code if regular extension loading is not available.  This is that
 91636 ** dummy pointer.
 91637 */
 91638 #ifdef SQLITE_OMIT_LOAD_EXTENSION
 91639 static const sqlite3_api_routines sqlite3Apis = { 0 };
 91640 #endif
 91643 /*
 91644 ** The following object holds the list of automatically loaded
 91645 ** extensions.
 91646 **
 91647 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
 91648 ** mutex must be held while accessing this list.
 91649 */
 91650 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
 91651 static SQLITE_WSD struct sqlite3AutoExtList {
 91652   int nExt;              /* Number of entries in aExt[] */          
 91653   void (**aExt)(void);   /* Pointers to the extension init functions */
 91654 } sqlite3Autoext = { 0, 0 };
 91656 /* The "wsdAutoext" macro will resolve to the autoextension
 91657 ** state vector.  If writable static data is unsupported on the target,
 91658 ** we have to locate the state vector at run-time.  In the more common
 91659 ** case where writable static data is supported, wsdStat can refer directly
 91660 ** to the "sqlite3Autoext" state vector declared above.
 91661 */
 91662 #ifdef SQLITE_OMIT_WSD
 91663 # define wsdAutoextInit \
 91664   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
 91665 # define wsdAutoext x[0]
 91666 #else
 91667 # define wsdAutoextInit
 91668 # define wsdAutoext sqlite3Autoext
 91669 #endif
 91672 /*
 91673 ** Register a statically linked extension that is automatically
 91674 ** loaded by every new database connection.
 91675 */
 91676 SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
 91677   int rc = SQLITE_OK;
 91678 #ifndef SQLITE_OMIT_AUTOINIT
 91679   rc = sqlite3_initialize();
 91680   if( rc ){
 91681     return rc;
 91682   }else
 91683 #endif
 91685     int i;
 91686 #if SQLITE_THREADSAFE
 91687     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 91688 #endif
 91689     wsdAutoextInit;
 91690     sqlite3_mutex_enter(mutex);
 91691     for(i=0; i<wsdAutoext.nExt; i++){
 91692       if( wsdAutoext.aExt[i]==xInit ) break;
 91694     if( i==wsdAutoext.nExt ){
 91695       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
 91696       void (**aNew)(void);
 91697       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
 91698       if( aNew==0 ){
 91699         rc = SQLITE_NOMEM;
 91700       }else{
 91701         wsdAutoext.aExt = aNew;
 91702         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
 91703         wsdAutoext.nExt++;
 91706     sqlite3_mutex_leave(mutex);
 91707     assert( (rc&0xff)==rc );
 91708     return rc;
 91712 /*
 91713 ** Reset the automatic extension loading mechanism.
 91714 */
 91715 SQLITE_API void sqlite3_reset_auto_extension(void){
 91716 #ifndef SQLITE_OMIT_AUTOINIT
 91717   if( sqlite3_initialize()==SQLITE_OK )
 91718 #endif
 91720 #if SQLITE_THREADSAFE
 91721     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 91722 #endif
 91723     wsdAutoextInit;
 91724     sqlite3_mutex_enter(mutex);
 91725     sqlite3_free(wsdAutoext.aExt);
 91726     wsdAutoext.aExt = 0;
 91727     wsdAutoext.nExt = 0;
 91728     sqlite3_mutex_leave(mutex);
 91732 /*
 91733 ** Load all automatic extensions.
 91734 **
 91735 ** If anything goes wrong, set an error in the database connection.
 91736 */
 91737 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
 91738   int i;
 91739   int go = 1;
 91740   int rc;
 91741   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 91743   wsdAutoextInit;
 91744   if( wsdAutoext.nExt==0 ){
 91745     /* Common case: early out without every having to acquire a mutex */
 91746     return;
 91748   for(i=0; go; i++){
 91749     char *zErrmsg;
 91750 #if SQLITE_THREADSAFE
 91751     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 91752 #endif
 91753     sqlite3_mutex_enter(mutex);
 91754     if( i>=wsdAutoext.nExt ){
 91755       xInit = 0;
 91756       go = 0;
 91757     }else{
 91758       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 91759               wsdAutoext.aExt[i];
 91761     sqlite3_mutex_leave(mutex);
 91762     zErrmsg = 0;
 91763     if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
 91764       sqlite3Error(db, rc,
 91765             "automatic extension loading failed: %s", zErrmsg);
 91766       go = 0;
 91768     sqlite3_free(zErrmsg);
 91772 /************** End of loadext.c *********************************************/
 91773 /************** Begin file pragma.c ******************************************/
 91774 /*
 91775 ** 2003 April 6
 91776 **
 91777 ** The author disclaims copyright to this source code.  In place of
 91778 ** a legal notice, here is a blessing:
 91779 **
 91780 **    May you do good and not evil.
 91781 **    May you find forgiveness for yourself and forgive others.
 91782 **    May you share freely, never taking more than you give.
 91783 **
 91784 *************************************************************************
 91785 ** This file contains code used to implement the PRAGMA command.
 91786 */
 91788 /*
 91789 ** Interpret the given string as a safety level.  Return 0 for OFF,
 91790 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
 91791 ** unrecognized string argument.  The FULL option is disallowed
 91792 ** if the omitFull parameter it 1.
 91793 **
 91794 ** Note that the values returned are one less that the values that
 91795 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
 91796 ** to support legacy SQL code.  The safety level used to be boolean
 91797 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
 91798 */
 91799 static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
 91800                              /* 123456789 123456789 */
 91801   static const char zText[] = "onoffalseyestruefull";
 91802   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
 91803   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
 91804   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
 91805   int i, n;
 91806   if( sqlite3Isdigit(*z) ){
 91807     return (u8)sqlite3Atoi(z);
 91809   n = sqlite3Strlen30(z);
 91810   for(i=0; i<ArraySize(iLength)-omitFull; i++){
 91811     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
 91812       return iValue[i];
 91815   return dflt;
 91818 /*
 91819 ** Interpret the given string as a boolean value.
 91820 */
 91821 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, int dflt){
 91822   return getSafetyLevel(z,1,dflt)!=0;
 91825 /* The sqlite3GetBoolean() function is used by other modules but the
 91826 ** remainder of this file is specific to PRAGMA processing.  So omit
 91827 ** the rest of the file if PRAGMAs are omitted from the build.
 91828 */
 91829 #if !defined(SQLITE_OMIT_PRAGMA)
 91831 /*
 91832 ** Interpret the given string as a locking mode value.
 91833 */
 91834 static int getLockingMode(const char *z){
 91835   if( z ){
 91836     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
 91837     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
 91839   return PAGER_LOCKINGMODE_QUERY;
 91842 #ifndef SQLITE_OMIT_AUTOVACUUM
 91843 /*
 91844 ** Interpret the given string as an auto-vacuum mode value.
 91845 **
 91846 ** The following strings, "none", "full" and "incremental" are 
 91847 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
 91848 */
 91849 static int getAutoVacuum(const char *z){
 91850   int i;
 91851   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
 91852   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
 91853   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
 91854   i = sqlite3Atoi(z);
 91855   return (u8)((i>=0&&i<=2)?i:0);
 91857 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
 91859 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 91860 /*
 91861 ** Interpret the given string as a temp db location. Return 1 for file
 91862 ** backed temporary databases, 2 for the Red-Black tree in memory database
 91863 ** and 0 to use the compile-time default.
 91864 */
 91865 static int getTempStore(const char *z){
 91866   if( z[0]>='0' && z[0]<='2' ){
 91867     return z[0] - '0';
 91868   }else if( sqlite3StrICmp(z, "file")==0 ){
 91869     return 1;
 91870   }else if( sqlite3StrICmp(z, "memory")==0 ){
 91871     return 2;
 91872   }else{
 91873     return 0;
 91876 #endif /* SQLITE_PAGER_PRAGMAS */
 91878 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 91879 /*
 91880 ** Invalidate temp storage, either when the temp storage is changed
 91881 ** from default, or when 'file' and the temp_store_directory has changed
 91882 */
 91883 static int invalidateTempStorage(Parse *pParse){
 91884   sqlite3 *db = pParse->db;
 91885   if( db->aDb[1].pBt!=0 ){
 91886     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
 91887       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
 91888         "from within a transaction");
 91889       return SQLITE_ERROR;
 91891     sqlite3BtreeClose(db->aDb[1].pBt);
 91892     db->aDb[1].pBt = 0;
 91893     sqlite3ResetAllSchemasOfConnection(db);
 91895   return SQLITE_OK;
 91897 #endif /* SQLITE_PAGER_PRAGMAS */
 91899 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 91900 /*
 91901 ** If the TEMP database is open, close it and mark the database schema
 91902 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
 91903 ** or DEFAULT_TEMP_STORE pragmas.
 91904 */
 91905 static int changeTempStorage(Parse *pParse, const char *zStorageType){
 91906   int ts = getTempStore(zStorageType);
 91907   sqlite3 *db = pParse->db;
 91908   if( db->temp_store==ts ) return SQLITE_OK;
 91909   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
 91910     return SQLITE_ERROR;
 91912   db->temp_store = (u8)ts;
 91913   return SQLITE_OK;
 91915 #endif /* SQLITE_PAGER_PRAGMAS */
 91917 /*
 91918 ** Generate code to return a single integer value.
 91919 */
 91920 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
 91921   Vdbe *v = sqlite3GetVdbe(pParse);
 91922   int mem = ++pParse->nMem;
 91923   i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
 91924   if( pI64 ){
 91925     memcpy(pI64, &value, sizeof(value));
 91927   sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
 91928   sqlite3VdbeSetNumCols(v, 1);
 91929   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
 91930   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
 91933 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
 91934 /*
 91935 ** Check to see if zRight and zLeft refer to a pragma that queries
 91936 ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
 91937 ** Also, implement the pragma.
 91938 */
 91939 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
 91940   static const struct sPragmaType {
 91941     const char *zName;  /* Name of the pragma */
 91942     int mask;           /* Mask for the db->flags value */
 91943   } aPragma[] = {
 91944     { "full_column_names",        SQLITE_FullColNames  },
 91945     { "short_column_names",       SQLITE_ShortColNames },
 91946     { "count_changes",            SQLITE_CountRows     },
 91947     { "empty_result_callbacks",   SQLITE_NullCallback  },
 91948     { "legacy_file_format",       SQLITE_LegacyFileFmt },
 91949     { "fullfsync",                SQLITE_FullFSync     },
 91950     { "checkpoint_fullfsync",     SQLITE_CkptFullFSync },
 91951     { "reverse_unordered_selects", SQLITE_ReverseOrder  },
 91952 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 91953     { "automatic_index",          SQLITE_AutoIndex     },
 91954 #endif
 91955 #ifdef SQLITE_DEBUG
 91956     { "sql_trace",                SQLITE_SqlTrace      },
 91957     { "vdbe_listing",             SQLITE_VdbeListing   },
 91958     { "vdbe_trace",               SQLITE_VdbeTrace     },
 91959 #endif
 91960 #ifndef SQLITE_OMIT_CHECK
 91961     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
 91962 #endif
 91963     /* The following is VERY experimental */
 91964     { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
 91966     /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
 91967     ** flag if there are any active statements. */
 91968     { "read_uncommitted",         SQLITE_ReadUncommitted },
 91969     { "recursive_triggers",       SQLITE_RecTriggers },
 91971     /* This flag may only be set if both foreign-key and trigger support
 91972     ** are present in the build.  */
 91973 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 91974     { "foreign_keys",             SQLITE_ForeignKeys },
 91975 #endif
 91976   };
 91977   int i;
 91978   const struct sPragmaType *p;
 91979   for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
 91980     if( sqlite3StrICmp(zLeft, p->zName)==0 ){
 91981       sqlite3 *db = pParse->db;
 91982       Vdbe *v;
 91983       v = sqlite3GetVdbe(pParse);
 91984       assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
 91985       if( ALWAYS(v) ){
 91986         if( zRight==0 ){
 91987           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
 91988         }else{
 91989           int mask = p->mask;          /* Mask of bits to set or clear. */
 91990           if( db->autoCommit==0 ){
 91991             /* Foreign key support may not be enabled or disabled while not
 91992             ** in auto-commit mode.  */
 91993             mask &= ~(SQLITE_ForeignKeys);
 91996           if( sqlite3GetBoolean(zRight, 0) ){
 91997             db->flags |= mask;
 91998           }else{
 91999             db->flags &= ~mask;
 92002           /* Many of the flag-pragmas modify the code generated by the SQL 
 92003           ** compiler (eg. count_changes). So add an opcode to expire all
 92004           ** compiled SQL statements after modifying a pragma value.
 92005           */
 92006           sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
 92010       return 1;
 92013   return 0;
 92015 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
 92017 /*
 92018 ** Return a human-readable name for a constraint resolution action.
 92019 */
 92020 #ifndef SQLITE_OMIT_FOREIGN_KEY
 92021 static const char *actionName(u8 action){
 92022   const char *zName;
 92023   switch( action ){
 92024     case OE_SetNull:  zName = "SET NULL";        break;
 92025     case OE_SetDflt:  zName = "SET DEFAULT";     break;
 92026     case OE_Cascade:  zName = "CASCADE";         break;
 92027     case OE_Restrict: zName = "RESTRICT";        break;
 92028     default:          zName = "NO ACTION";  
 92029                       assert( action==OE_None ); break;
 92031   return zName;
 92033 #endif
 92036 /*
 92037 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
 92038 ** defined in pager.h. This function returns the associated lowercase
 92039 ** journal-mode name.
 92040 */
 92041 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
 92042   static char * const azModeName[] = {
 92043     "delete", "persist", "off", "truncate", "memory"
 92044 #ifndef SQLITE_OMIT_WAL
 92045      , "wal"
 92046 #endif
 92047   };
 92048   assert( PAGER_JOURNALMODE_DELETE==0 );
 92049   assert( PAGER_JOURNALMODE_PERSIST==1 );
 92050   assert( PAGER_JOURNALMODE_OFF==2 );
 92051   assert( PAGER_JOURNALMODE_TRUNCATE==3 );
 92052   assert( PAGER_JOURNALMODE_MEMORY==4 );
 92053   assert( PAGER_JOURNALMODE_WAL==5 );
 92054   assert( eMode>=0 && eMode<=ArraySize(azModeName) );
 92056   if( eMode==ArraySize(azModeName) ) return 0;
 92057   return azModeName[eMode];
 92060 /*
 92061 ** Process a pragma statement.  
 92062 **
 92063 ** Pragmas are of this form:
 92064 **
 92065 **      PRAGMA [database.]id [= value]
 92066 **
 92067 ** The identifier might also be a string.  The value is a string, and
 92068 ** identifier, or a number.  If minusFlag is true, then the value is
 92069 ** a number that was preceded by a minus sign.
 92070 **
 92071 ** If the left side is "database.id" then pId1 is the database name
 92072 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
 92073 ** id and pId2 is any empty string.
 92074 */
 92075 SQLITE_PRIVATE void sqlite3Pragma(
 92076   Parse *pParse, 
 92077   Token *pId1,        /* First part of [database.]id field */
 92078   Token *pId2,        /* Second part of [database.]id field, or NULL */
 92079   Token *pValue,      /* Token for <value>, or NULL */
 92080   int minusFlag       /* True if a '-' sign preceded <value> */
 92081 ){
 92082   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
 92083   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
 92084   const char *zDb = 0;   /* The database name */
 92085   Token *pId;            /* Pointer to <id> token */
 92086   int iDb;               /* Database index for <database> */
 92087   char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
 92088   int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
 92089   sqlite3 *db = pParse->db;    /* The database connection */
 92090   Db *pDb;                     /* The specific database being pragmaed */
 92091   Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);  /* Prepared statement */
 92093   if( v==0 ) return;
 92094   sqlite3VdbeRunOnlyOnce(v);
 92095   pParse->nMem = 2;
 92097   /* Interpret the [database.] part of the pragma statement. iDb is the
 92098   ** index of the database this pragma is being applied to in db.aDb[]. */
 92099   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
 92100   if( iDb<0 ) return;
 92101   pDb = &db->aDb[iDb];
 92103   /* If the temp database has been explicitly named as part of the 
 92104   ** pragma, make sure it is open. 
 92105   */
 92106   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
 92107     return;
 92110   zLeft = sqlite3NameFromToken(db, pId);
 92111   if( !zLeft ) return;
 92112   if( minusFlag ){
 92113     zRight = sqlite3MPrintf(db, "-%T", pValue);
 92114   }else{
 92115     zRight = sqlite3NameFromToken(db, pValue);
 92118   assert( pId2 );
 92119   zDb = pId2->n>0 ? pDb->zName : 0;
 92120   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
 92121     goto pragma_out;
 92124   /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
 92125   ** connection.  If it returns SQLITE_OK, then assume that the VFS
 92126   ** handled the pragma and generate a no-op prepared statement.
 92127   */
 92128   aFcntl[0] = 0;
 92129   aFcntl[1] = zLeft;
 92130   aFcntl[2] = zRight;
 92131   aFcntl[3] = 0;
 92132   db->busyHandler.nBusy = 0;
 92133   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
 92134   if( rc==SQLITE_OK ){
 92135     if( aFcntl[0] ){
 92136       int mem = ++pParse->nMem;
 92137       sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
 92138       sqlite3VdbeSetNumCols(v, 1);
 92139       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
 92140       sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
 92141       sqlite3_free(aFcntl[0]);
 92143   }else if( rc!=SQLITE_NOTFOUND ){
 92144     if( aFcntl[0] ){
 92145       sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
 92146       sqlite3_free(aFcntl[0]);
 92148     pParse->nErr++;
 92149     pParse->rc = rc;
 92150   }else
 92153 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
 92154   /*
 92155   **  PRAGMA [database.]default_cache_size
 92156   **  PRAGMA [database.]default_cache_size=N
 92157   **
 92158   ** The first form reports the current persistent setting for the
 92159   ** page cache size.  The value returned is the maximum number of
 92160   ** pages in the page cache.  The second form sets both the current
 92161   ** page cache size value and the persistent page cache size value
 92162   ** stored in the database file.
 92163   **
 92164   ** Older versions of SQLite would set the default cache size to a
 92165   ** negative number to indicate synchronous=OFF.  These days, synchronous
 92166   ** is always on by default regardless of the sign of the default cache
 92167   ** size.  But continue to take the absolute value of the default cache
 92168   ** size of historical compatibility.
 92169   */
 92170   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
 92171     static const VdbeOpList getCacheSize[] = {
 92172       { OP_Transaction, 0, 0,        0},                         /* 0 */
 92173       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
 92174       { OP_IfPos,       1, 7,        0},
 92175       { OP_Integer,     0, 2,        0},
 92176       { OP_Subtract,    1, 2,        1},
 92177       { OP_IfPos,       1, 7,        0},
 92178       { OP_Integer,     0, 1,        0},                         /* 6 */
 92179       { OP_ResultRow,   1, 1,        0},
 92180     };
 92181     int addr;
 92182     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92183     sqlite3VdbeUsesBtree(v, iDb);
 92184     if( !zRight ){
 92185       sqlite3VdbeSetNumCols(v, 1);
 92186       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
 92187       pParse->nMem += 2;
 92188       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
 92189       sqlite3VdbeChangeP1(v, addr, iDb);
 92190       sqlite3VdbeChangeP1(v, addr+1, iDb);
 92191       sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
 92192     }else{
 92193       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
 92194       sqlite3BeginWriteOperation(pParse, 0, iDb);
 92195       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
 92196       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
 92197       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 92198       pDb->pSchema->cache_size = size;
 92199       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 92201   }else
 92202 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
 92204 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 92205   /*
 92206   **  PRAGMA [database.]page_size
 92207   **  PRAGMA [database.]page_size=N
 92208   **
 92209   ** The first form reports the current setting for the
 92210   ** database page size in bytes.  The second form sets the
 92211   ** database page size value.  The value can only be set if
 92212   ** the database has not yet been created.
 92213   */
 92214   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
 92215     Btree *pBt = pDb->pBt;
 92216     assert( pBt!=0 );
 92217     if( !zRight ){
 92218       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
 92219       returnSingleInt(pParse, "page_size", size);
 92220     }else{
 92221       /* Malloc may fail when setting the page-size, as there is an internal
 92222       ** buffer that the pager module resizes using sqlite3_realloc().
 92223       */
 92224       db->nextPagesize = sqlite3Atoi(zRight);
 92225       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
 92226         db->mallocFailed = 1;
 92229   }else
 92231   /*
 92232   **  PRAGMA [database.]secure_delete
 92233   **  PRAGMA [database.]secure_delete=ON/OFF
 92234   **
 92235   ** The first form reports the current setting for the
 92236   ** secure_delete flag.  The second form changes the secure_delete
 92237   ** flag setting and reports thenew value.
 92238   */
 92239   if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
 92240     Btree *pBt = pDb->pBt;
 92241     int b = -1;
 92242     assert( pBt!=0 );
 92243     if( zRight ){
 92244       b = sqlite3GetBoolean(zRight, 0);
 92246     if( pId2->n==0 && b>=0 ){
 92247       int ii;
 92248       for(ii=0; ii<db->nDb; ii++){
 92249         sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
 92252     b = sqlite3BtreeSecureDelete(pBt, b);
 92253     returnSingleInt(pParse, "secure_delete", b);
 92254   }else
 92256   /*
 92257   **  PRAGMA [database.]max_page_count
 92258   **  PRAGMA [database.]max_page_count=N
 92259   **
 92260   ** The first form reports the current setting for the
 92261   ** maximum number of pages in the database file.  The 
 92262   ** second form attempts to change this setting.  Both
 92263   ** forms return the current setting.
 92264   **
 92265   ** The absolute value of N is used.  This is undocumented and might
 92266   ** change.  The only purpose is to provide an easy way to test
 92267   ** the sqlite3AbsInt32() function.
 92268   **
 92269   **  PRAGMA [database.]page_count
 92270   **
 92271   ** Return the number of pages in the specified database.
 92272   */
 92273   if( sqlite3StrICmp(zLeft,"page_count")==0
 92274    || sqlite3StrICmp(zLeft,"max_page_count")==0
 92275   ){
 92276     int iReg;
 92277     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92278     sqlite3CodeVerifySchema(pParse, iDb);
 92279     iReg = ++pParse->nMem;
 92280     if( sqlite3Tolower(zLeft[0])=='p' ){
 92281       sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
 92282     }else{
 92283       sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, 
 92284                         sqlite3AbsInt32(sqlite3Atoi(zRight)));
 92286     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
 92287     sqlite3VdbeSetNumCols(v, 1);
 92288     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
 92289   }else
 92291   /*
 92292   **  PRAGMA [database.]locking_mode
 92293   **  PRAGMA [database.]locking_mode = (normal|exclusive)
 92294   */
 92295   if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
 92296     const char *zRet = "normal";
 92297     int eMode = getLockingMode(zRight);
 92299     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
 92300       /* Simple "PRAGMA locking_mode;" statement. This is a query for
 92301       ** the current default locking mode (which may be different to
 92302       ** the locking-mode of the main database).
 92303       */
 92304       eMode = db->dfltLockMode;
 92305     }else{
 92306       Pager *pPager;
 92307       if( pId2->n==0 ){
 92308         /* This indicates that no database name was specified as part
 92309         ** of the PRAGMA command. In this case the locking-mode must be
 92310         ** set on all attached databases, as well as the main db file.
 92311         **
 92312         ** Also, the sqlite3.dfltLockMode variable is set so that
 92313         ** any subsequently attached databases also use the specified
 92314         ** locking mode.
 92315         */
 92316         int ii;
 92317         assert(pDb==&db->aDb[0]);
 92318         for(ii=2; ii<db->nDb; ii++){
 92319           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
 92320           sqlite3PagerLockingMode(pPager, eMode);
 92322         db->dfltLockMode = (u8)eMode;
 92324       pPager = sqlite3BtreePager(pDb->pBt);
 92325       eMode = sqlite3PagerLockingMode(pPager, eMode);
 92328     assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
 92329     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
 92330       zRet = "exclusive";
 92332     sqlite3VdbeSetNumCols(v, 1);
 92333     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
 92334     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
 92335     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 92336   }else
 92338   /*
 92339   **  PRAGMA [database.]journal_mode
 92340   **  PRAGMA [database.]journal_mode =
 92341   **                      (delete|persist|off|truncate|memory|wal|off)
 92342   */
 92343   if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
 92344     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
 92345     int ii;           /* Loop counter */
 92347     /* Force the schema to be loaded on all databases.  This causes all
 92348     ** database files to be opened and the journal_modes set.  This is
 92349     ** necessary because subsequent processing must know if the databases
 92350     ** are in WAL mode. */
 92351     if( sqlite3ReadSchema(pParse) ){
 92352       goto pragma_out;
 92355     sqlite3VdbeSetNumCols(v, 1);
 92356     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
 92358     if( zRight==0 ){
 92359       /* If there is no "=MODE" part of the pragma, do a query for the
 92360       ** current mode */
 92361       eMode = PAGER_JOURNALMODE_QUERY;
 92362     }else{
 92363       const char *zMode;
 92364       int n = sqlite3Strlen30(zRight);
 92365       for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
 92366         if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
 92368       if( !zMode ){
 92369         /* If the "=MODE" part does not match any known journal mode,
 92370         ** then do a query */
 92371         eMode = PAGER_JOURNALMODE_QUERY;
 92374     if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
 92375       /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
 92376       iDb = 0;
 92377       pId2->n = 1;
 92379     for(ii=db->nDb-1; ii>=0; ii--){
 92380       if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
 92381         sqlite3VdbeUsesBtree(v, ii);
 92382         sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
 92385     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 92386   }else
 92388   /*
 92389   **  PRAGMA [database.]journal_size_limit
 92390   **  PRAGMA [database.]journal_size_limit=N
 92391   **
 92392   ** Get or set the size limit on rollback journal files.
 92393   */
 92394   if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
 92395     Pager *pPager = sqlite3BtreePager(pDb->pBt);
 92396     i64 iLimit = -2;
 92397     if( zRight ){
 92398       sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
 92399       if( iLimit<-1 ) iLimit = -1;
 92401     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
 92402     returnSingleInt(pParse, "journal_size_limit", iLimit);
 92403   }else
 92405 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
 92407   /*
 92408   **  PRAGMA [database.]auto_vacuum
 92409   **  PRAGMA [database.]auto_vacuum=N
 92410   **
 92411   ** Get or set the value of the database 'auto-vacuum' parameter.
 92412   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
 92413   */
 92414 #ifndef SQLITE_OMIT_AUTOVACUUM
 92415   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
 92416     Btree *pBt = pDb->pBt;
 92417     assert( pBt!=0 );
 92418     if( sqlite3ReadSchema(pParse) ){
 92419       goto pragma_out;
 92421     if( !zRight ){
 92422       int auto_vacuum;
 92423       if( ALWAYS(pBt) ){
 92424          auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
 92425       }else{
 92426          auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
 92428       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
 92429     }else{
 92430       int eAuto = getAutoVacuum(zRight);
 92431       assert( eAuto>=0 && eAuto<=2 );
 92432       db->nextAutovac = (u8)eAuto;
 92433       if( ALWAYS(eAuto>=0) ){
 92434         /* Call SetAutoVacuum() to set initialize the internal auto and
 92435         ** incr-vacuum flags. This is required in case this connection
 92436         ** creates the database file. It is important that it is created
 92437         ** as an auto-vacuum capable db.
 92438         */
 92439         rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
 92440         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
 92441           /* When setting the auto_vacuum mode to either "full" or 
 92442           ** "incremental", write the value of meta[6] in the database
 92443           ** file. Before writing to meta[6], check that meta[3] indicates
 92444           ** that this really is an auto-vacuum capable database.
 92445           */
 92446           static const VdbeOpList setMeta6[] = {
 92447             { OP_Transaction,    0,         1,                 0},    /* 0 */
 92448             { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
 92449             { OP_If,             1,         0,                 0},    /* 2 */
 92450             { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
 92451             { OP_Integer,        0,         1,                 0},    /* 4 */
 92452             { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
 92453           };
 92454           int iAddr;
 92455           iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
 92456           sqlite3VdbeChangeP1(v, iAddr, iDb);
 92457           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
 92458           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
 92459           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
 92460           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
 92461           sqlite3VdbeUsesBtree(v, iDb);
 92465   }else
 92466 #endif
 92468   /*
 92469   **  PRAGMA [database.]incremental_vacuum(N)
 92470   **
 92471   ** Do N steps of incremental vacuuming on a database.
 92472   */
 92473 #ifndef SQLITE_OMIT_AUTOVACUUM
 92474   if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
 92475     int iLimit, addr;
 92476     if( sqlite3ReadSchema(pParse) ){
 92477       goto pragma_out;
 92479     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
 92480       iLimit = 0x7fffffff;
 92482     sqlite3BeginWriteOperation(pParse, 0, iDb);
 92483     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
 92484     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
 92485     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
 92486     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
 92487     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
 92488     sqlite3VdbeJumpHere(v, addr);
 92489   }else
 92490 #endif
 92492 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 92493   /*
 92494   **  PRAGMA [database.]cache_size
 92495   **  PRAGMA [database.]cache_size=N
 92496   **
 92497   ** The first form reports the current local setting for the
 92498   ** page cache size. The second form sets the local
 92499   ** page cache size value.  If N is positive then that is the
 92500   ** number of pages in the cache.  If N is negative, then the
 92501   ** number of pages is adjusted so that the cache uses -N kibibytes
 92502   ** of memory.
 92503   */
 92504   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
 92505     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92506     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 92507     if( !zRight ){
 92508       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
 92509     }else{
 92510       int size = sqlite3Atoi(zRight);
 92511       pDb->pSchema->cache_size = size;
 92512       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 92514   }else
 92516   /*
 92517   **   PRAGMA temp_store
 92518   **   PRAGMA temp_store = "default"|"memory"|"file"
 92519   **
 92520   ** Return or set the local value of the temp_store flag.  Changing
 92521   ** the local value does not make changes to the disk file and the default
 92522   ** value will be restored the next time the database is opened.
 92523   **
 92524   ** Note that it is possible for the library compile-time options to
 92525   ** override this setting
 92526   */
 92527   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
 92528     if( !zRight ){
 92529       returnSingleInt(pParse, "temp_store", db->temp_store);
 92530     }else{
 92531       changeTempStorage(pParse, zRight);
 92533   }else
 92535   /*
 92536   **   PRAGMA temp_store_directory
 92537   **   PRAGMA temp_store_directory = ""|"directory_name"
 92538   **
 92539   ** Return or set the local value of the temp_store_directory flag.  Changing
 92540   ** the value sets a specific directory to be used for temporary files.
 92541   ** Setting to a null string reverts to the default temporary directory search.
 92542   ** If temporary directory is changed, then invalidateTempStorage.
 92543   **
 92544   */
 92545   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
 92546     if( !zRight ){
 92547       if( sqlite3_temp_directory ){
 92548         sqlite3VdbeSetNumCols(v, 1);
 92549         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 92550             "temp_store_directory", SQLITE_STATIC);
 92551         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
 92552         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 92554     }else{
 92555 #ifndef SQLITE_OMIT_WSD
 92556       if( zRight[0] ){
 92557         int res;
 92558         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
 92559         if( rc!=SQLITE_OK || res==0 ){
 92560           sqlite3ErrorMsg(pParse, "not a writable directory");
 92561           goto pragma_out;
 92564       if( SQLITE_TEMP_STORE==0
 92565        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
 92566        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
 92567       ){
 92568         invalidateTempStorage(pParse);
 92570       sqlite3_free(sqlite3_temp_directory);
 92571       if( zRight[0] ){
 92572         sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
 92573       }else{
 92574         sqlite3_temp_directory = 0;
 92576 #endif /* SQLITE_OMIT_WSD */
 92578   }else
 92580 #if SQLITE_OS_WIN
 92581   /*
 92582   **   PRAGMA data_store_directory
 92583   **   PRAGMA data_store_directory = ""|"directory_name"
 92584   **
 92585   ** Return or set the local value of the data_store_directory flag.  Changing
 92586   ** the value sets a specific directory to be used for database files that
 92587   ** were specified with a relative pathname.  Setting to a null string reverts
 92588   ** to the default database directory, which for database files specified with
 92589   ** a relative path will probably be based on the current directory for the
 92590   ** process.  Database file specified with an absolute path are not impacted
 92591   ** by this setting, regardless of its value.
 92592   **
 92593   */
 92594   if( sqlite3StrICmp(zLeft, "data_store_directory")==0 ){
 92595     if( !zRight ){
 92596       if( sqlite3_data_directory ){
 92597         sqlite3VdbeSetNumCols(v, 1);
 92598         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 92599             "data_store_directory", SQLITE_STATIC);
 92600         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
 92601         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 92603     }else{
 92604 #ifndef SQLITE_OMIT_WSD
 92605       if( zRight[0] ){
 92606         int res;
 92607         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
 92608         if( rc!=SQLITE_OK || res==0 ){
 92609           sqlite3ErrorMsg(pParse, "not a writable directory");
 92610           goto pragma_out;
 92613       sqlite3_free(sqlite3_data_directory);
 92614       if( zRight[0] ){
 92615         sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
 92616       }else{
 92617         sqlite3_data_directory = 0;
 92619 #endif /* SQLITE_OMIT_WSD */
 92621   }else
 92622 #endif
 92624 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
 92625 #  if defined(__APPLE__)
 92626 #    define SQLITE_ENABLE_LOCKING_STYLE 1
 92627 #  else
 92628 #    define SQLITE_ENABLE_LOCKING_STYLE 0
 92629 #  endif
 92630 #endif
 92631 #if SQLITE_ENABLE_LOCKING_STYLE
 92632   /*
 92633    **   PRAGMA [database.]lock_proxy_file
 92634    **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
 92635    **
 92636    ** Return or set the value of the lock_proxy_file flag.  Changing
 92637    ** the value sets a specific file to be used for database access locks.
 92638    **
 92639    */
 92640   if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
 92641     if( !zRight ){
 92642       Pager *pPager = sqlite3BtreePager(pDb->pBt);
 92643       char *proxy_file_path = NULL;
 92644       sqlite3_file *pFile = sqlite3PagerFile(pPager);
 92645       sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 
 92646                            &proxy_file_path);
 92648       if( proxy_file_path ){
 92649         sqlite3VdbeSetNumCols(v, 1);
 92650         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 92651                               "lock_proxy_file", SQLITE_STATIC);
 92652         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
 92653         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 92655     }else{
 92656       Pager *pPager = sqlite3BtreePager(pDb->pBt);
 92657       sqlite3_file *pFile = sqlite3PagerFile(pPager);
 92658       int res;
 92659       if( zRight[0] ){
 92660         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
 92661                                      zRight);
 92662       } else {
 92663         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
 92664                                      NULL);
 92666       if( res!=SQLITE_OK ){
 92667         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
 92668         goto pragma_out;
 92671   }else
 92672 #endif /* SQLITE_ENABLE_LOCKING_STYLE */      
 92674   /*
 92675   **   PRAGMA [database.]synchronous
 92676   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
 92677   **
 92678   ** Return or set the local value of the synchronous flag.  Changing
 92679   ** the local value does not make changes to the disk file and the
 92680   ** default value will be restored the next time the database is
 92681   ** opened.
 92682   */
 92683   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
 92684     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92685     if( !zRight ){
 92686       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
 92687     }else{
 92688       if( !db->autoCommit ){
 92689         sqlite3ErrorMsg(pParse, 
 92690             "Safety level may not be changed inside a transaction");
 92691       }else{
 92692         pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
 92695   }else
 92696 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
 92698 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
 92699   if( flagPragma(pParse, zLeft, zRight) ){
 92700     /* The flagPragma() subroutine also generates any necessary code
 92701     ** there is nothing more to do here */
 92702   }else
 92703 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
 92705 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
 92706   /*
 92707   **   PRAGMA table_info(<table>)
 92708   **
 92709   ** Return a single row for each column of the named table. The columns of
 92710   ** the returned data set are:
 92711   **
 92712   ** cid:        Column id (numbered from left to right, starting at 0)
 92713   ** name:       Column name
 92714   ** type:       Column declaration type.
 92715   ** notnull:    True if 'NOT NULL' is part of column declaration
 92716   ** dflt_value: The default value for the column, if any.
 92717   */
 92718   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
 92719     Table *pTab;
 92720     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92721     pTab = sqlite3FindTable(db, zRight, zDb);
 92722     if( pTab ){
 92723       int i;
 92724       int nHidden = 0;
 92725       Column *pCol;
 92726       sqlite3VdbeSetNumCols(v, 6);
 92727       pParse->nMem = 6;
 92728       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
 92729       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 92730       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
 92731       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
 92732       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
 92733       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
 92734       sqlite3ViewGetColumnNames(pParse, pTab);
 92735       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
 92736         if( IsHiddenColumn(pCol) ){
 92737           nHidden++;
 92738           continue;
 92740         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
 92741         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
 92742         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 92743            pCol->zType ? pCol->zType : "", 0);
 92744         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
 92745         if( pCol->zDflt ){
 92746           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
 92747         }else{
 92748           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
 92750         sqlite3VdbeAddOp2(v, OP_Integer,
 92751                             (pCol->colFlags&COLFLAG_PRIMKEY)!=0, 6);
 92752         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
 92755   }else
 92757   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
 92758     Index *pIdx;
 92759     Table *pTab;
 92760     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92761     pIdx = sqlite3FindIndex(db, zRight, zDb);
 92762     if( pIdx ){
 92763       int i;
 92764       pTab = pIdx->pTable;
 92765       sqlite3VdbeSetNumCols(v, 3);
 92766       pParse->nMem = 3;
 92767       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
 92768       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
 92769       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
 92770       for(i=0; i<pIdx->nColumn; i++){
 92771         int cnum = pIdx->aiColumn[i];
 92772         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 92773         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
 92774         assert( pTab->nCol>cnum );
 92775         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
 92776         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 92779   }else
 92781   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
 92782     Index *pIdx;
 92783     Table *pTab;
 92784     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92785     pTab = sqlite3FindTable(db, zRight, zDb);
 92786     if( pTab ){
 92787       v = sqlite3GetVdbe(pParse);
 92788       pIdx = pTab->pIndex;
 92789       if( pIdx ){
 92790         int i = 0; 
 92791         sqlite3VdbeSetNumCols(v, 3);
 92792         pParse->nMem = 3;
 92793         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 92794         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 92795         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
 92796         while(pIdx){
 92797           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 92798           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
 92799           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
 92800           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 92801           ++i;
 92802           pIdx = pIdx->pNext;
 92806   }else
 92808   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
 92809     int i;
 92810     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92811     sqlite3VdbeSetNumCols(v, 3);
 92812     pParse->nMem = 3;
 92813     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 92814     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 92815     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
 92816     for(i=0; i<db->nDb; i++){
 92817       if( db->aDb[i].pBt==0 ) continue;
 92818       assert( db->aDb[i].zName!=0 );
 92819       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 92820       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
 92821       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 92822            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
 92823       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 92825   }else
 92827   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
 92828     int i = 0;
 92829     HashElem *p;
 92830     sqlite3VdbeSetNumCols(v, 2);
 92831     pParse->nMem = 2;
 92832     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 92833     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 92834     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
 92835       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
 92836       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
 92837       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
 92838       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
 92840   }else
 92841 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
 92843 #ifndef SQLITE_OMIT_FOREIGN_KEY
 92844   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
 92845     FKey *pFK;
 92846     Table *pTab;
 92847     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92848     pTab = sqlite3FindTable(db, zRight, zDb);
 92849     if( pTab ){
 92850       v = sqlite3GetVdbe(pParse);
 92851       pFK = pTab->pFKey;
 92852       if( pFK ){
 92853         int i = 0; 
 92854         sqlite3VdbeSetNumCols(v, 8);
 92855         pParse->nMem = 8;
 92856         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
 92857         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
 92858         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
 92859         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
 92860         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
 92861         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
 92862         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
 92863         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
 92864         while(pFK){
 92865           int j;
 92866           for(j=0; j<pFK->nCol; j++){
 92867             char *zCol = pFK->aCol[j].zCol;
 92868             char *zOnDelete = (char *)actionName(pFK->aAction[0]);
 92869             char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
 92870             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 92871             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
 92872             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
 92873             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
 92874                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
 92875             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
 92876             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
 92877             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
 92878             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
 92879             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
 92881           ++i;
 92882           pFK = pFK->pNextFrom;
 92886   }else
 92887 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 92889 #ifndef NDEBUG
 92890   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
 92891     if( zRight ){
 92892       if( sqlite3GetBoolean(zRight, 0) ){
 92893         sqlite3ParserTrace(stderr, "parser: ");
 92894       }else{
 92895         sqlite3ParserTrace(0, 0);
 92898   }else
 92899 #endif
 92901   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
 92902   ** used will be case sensitive or not depending on the RHS.
 92903   */
 92904   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
 92905     if( zRight ){
 92906       sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
 92908   }else
 92910 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
 92911 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
 92912 #endif
 92914 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 92915   /* Pragma "quick_check" is an experimental reduced version of 
 92916   ** integrity_check designed to detect most database corruption
 92917   ** without most of the overhead of a full integrity-check.
 92918   */
 92919   if( sqlite3StrICmp(zLeft, "integrity_check")==0
 92920    || sqlite3StrICmp(zLeft, "quick_check")==0 
 92921   ){
 92922     int i, j, addr, mxErr;
 92924     /* Code that appears at the end of the integrity check.  If no error
 92925     ** messages have been generated, output OK.  Otherwise output the
 92926     ** error message
 92927     */
 92928     static const VdbeOpList endCode[] = {
 92929       { OP_AddImm,      1, 0,        0},    /* 0 */
 92930       { OP_IfNeg,       1, 0,        0},    /* 1 */
 92931       { OP_String8,     0, 3,        0},    /* 2 */
 92932       { OP_ResultRow,   3, 1,        0},
 92933     };
 92935     int isQuick = (sqlite3Tolower(zLeft[0])=='q');
 92937     /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
 92938     ** then iDb is set to the index of the database identified by <db>.
 92939     ** In this case, the integrity of database iDb only is verified by
 92940     ** the VDBE created below.
 92941     **
 92942     ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
 92943     ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
 92944     ** to -1 here, to indicate that the VDBE should verify the integrity
 92945     ** of all attached databases.  */
 92946     assert( iDb>=0 );
 92947     assert( iDb==0 || pId2->z );
 92948     if( pId2->z==0 ) iDb = -1;
 92950     /* Initialize the VDBE program */
 92951     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92952     pParse->nMem = 6;
 92953     sqlite3VdbeSetNumCols(v, 1);
 92954     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
 92956     /* Set the maximum error count */
 92957     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
 92958     if( zRight ){
 92959       sqlite3GetInt32(zRight, &mxErr);
 92960       if( mxErr<=0 ){
 92961         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
 92964     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
 92966     /* Do an integrity check on each database file */
 92967     for(i=0; i<db->nDb; i++){
 92968       HashElem *x;
 92969       Hash *pTbls;
 92970       int cnt = 0;
 92972       if( OMIT_TEMPDB && i==1 ) continue;
 92973       if( iDb>=0 && i!=iDb ) continue;
 92975       sqlite3CodeVerifySchema(pParse, i);
 92976       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
 92977       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 92978       sqlite3VdbeJumpHere(v, addr);
 92980       /* Do an integrity check of the B-Tree
 92981       **
 92982       ** Begin by filling registers 2, 3, ... with the root pages numbers
 92983       ** for all tables and indices in the database.
 92984       */
 92985       assert( sqlite3SchemaMutexHeld(db, i, 0) );
 92986       pTbls = &db->aDb[i].pSchema->tblHash;
 92987       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
 92988         Table *pTab = sqliteHashData(x);
 92989         Index *pIdx;
 92990         sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
 92991         cnt++;
 92992         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 92993           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
 92994           cnt++;
 92998       /* Make sure sufficient number of registers have been allocated */
 92999       if( pParse->nMem < cnt+4 ){
 93000         pParse->nMem = cnt+4;
 93003       /* Do the b-tree integrity checks */
 93004       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
 93005       sqlite3VdbeChangeP5(v, (u8)i);
 93006       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
 93007       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 93008          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
 93009          P4_DYNAMIC);
 93010       sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
 93011       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
 93012       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
 93013       sqlite3VdbeJumpHere(v, addr);
 93015       /* Make sure all the indices are constructed correctly.
 93016       */
 93017       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
 93018         Table *pTab = sqliteHashData(x);
 93019         Index *pIdx;
 93020         int loopTop;
 93022         if( pTab->pIndex==0 ) continue;
 93023         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
 93024         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 93025         sqlite3VdbeJumpHere(v, addr);
 93026         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
 93027         sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
 93028         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
 93029         sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
 93030         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 93031           int jmp2;
 93032           int r1;
 93033           static const VdbeOpList idxErr[] = {
 93034             { OP_AddImm,      1, -1,  0},
 93035             { OP_String8,     0,  3,  0},    /* 1 */
 93036             { OP_Rowid,       1,  4,  0},
 93037             { OP_String8,     0,  5,  0},    /* 3 */
 93038             { OP_String8,     0,  6,  0},    /* 4 */
 93039             { OP_Concat,      4,  3,  3},
 93040             { OP_Concat,      5,  3,  3},
 93041             { OP_Concat,      6,  3,  3},
 93042             { OP_ResultRow,   3,  1,  0},
 93043             { OP_IfPos,       1,  0,  0},    /* 9 */
 93044             { OP_Halt,        0,  0,  0},
 93045           };
 93046           r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
 93047           jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
 93048           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
 93049           sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
 93050           sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
 93051           sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
 93052           sqlite3VdbeJumpHere(v, addr+9);
 93053           sqlite3VdbeJumpHere(v, jmp2);
 93055         sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
 93056         sqlite3VdbeJumpHere(v, loopTop);
 93057         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 93058           static const VdbeOpList cntIdx[] = {
 93059              { OP_Integer,      0,  3,  0},
 93060              { OP_Rewind,       0,  0,  0},  /* 1 */
 93061              { OP_AddImm,       3,  1,  0},
 93062              { OP_Next,         0,  0,  0},  /* 3 */
 93063              { OP_Eq,           2,  0,  3},  /* 4 */
 93064              { OP_AddImm,       1, -1,  0},
 93065              { OP_String8,      0,  2,  0},  /* 6 */
 93066              { OP_String8,      0,  3,  0},  /* 7 */
 93067              { OP_Concat,       3,  2,  2},
 93068              { OP_ResultRow,    2,  1,  0},
 93069           };
 93070           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
 93071           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 93072           sqlite3VdbeJumpHere(v, addr);
 93073           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
 93074           sqlite3VdbeChangeP1(v, addr+1, j+2);
 93075           sqlite3VdbeChangeP2(v, addr+1, addr+4);
 93076           sqlite3VdbeChangeP1(v, addr+3, j+2);
 93077           sqlite3VdbeChangeP2(v, addr+3, addr+2);
 93078           sqlite3VdbeJumpHere(v, addr+4);
 93079           sqlite3VdbeChangeP4(v, addr+6, 
 93080                      "wrong # of entries in index ", P4_STATIC);
 93081           sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
 93085     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
 93086     sqlite3VdbeChangeP2(v, addr, -mxErr);
 93087     sqlite3VdbeJumpHere(v, addr+1);
 93088     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
 93089   }else
 93090 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 93092 #ifndef SQLITE_OMIT_UTF16
 93093   /*
 93094   **   PRAGMA encoding
 93095   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
 93096   **
 93097   ** In its first form, this pragma returns the encoding of the main
 93098   ** database. If the database is not initialized, it is initialized now.
 93099   **
 93100   ** The second form of this pragma is a no-op if the main database file
 93101   ** has not already been initialized. In this case it sets the default
 93102   ** encoding that will be used for the main database file if a new file
 93103   ** is created. If an existing main database file is opened, then the
 93104   ** default text encoding for the existing database is used.
 93105   ** 
 93106   ** In all cases new databases created using the ATTACH command are
 93107   ** created to use the same default text encoding as the main database. If
 93108   ** the main database has not been initialized and/or created when ATTACH
 93109   ** is executed, this is done before the ATTACH operation.
 93110   **
 93111   ** In the second form this pragma sets the text encoding to be used in
 93112   ** new database files created using this database handle. It is only
 93113   ** useful if invoked immediately after the main database i
 93114   */
 93115   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
 93116     static const struct EncName {
 93117       char *zName;
 93118       u8 enc;
 93119     } encnames[] = {
 93120       { "UTF8",     SQLITE_UTF8        },
 93121       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
 93122       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
 93123       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
 93124       { "UTF16le",  SQLITE_UTF16LE     },
 93125       { "UTF16be",  SQLITE_UTF16BE     },
 93126       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
 93127       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
 93128       { 0, 0 }
 93129     };
 93130     const struct EncName *pEnc;
 93131     if( !zRight ){    /* "PRAGMA encoding" */
 93132       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 93133       sqlite3VdbeSetNumCols(v, 1);
 93134       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
 93135       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
 93136       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
 93137       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
 93138       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
 93139       sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
 93140       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 93141     }else{                        /* "PRAGMA encoding = XXX" */
 93142       /* Only change the value of sqlite.enc if the database handle is not
 93143       ** initialized. If the main database exists, the new sqlite.enc value
 93144       ** will be overwritten when the schema is next loaded. If it does not
 93145       ** already exists, it will be created to use the new encoding value.
 93146       */
 93147       if( 
 93148         !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
 93149         DbHasProperty(db, 0, DB_Empty) 
 93150       ){
 93151         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
 93152           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
 93153             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
 93154             break;
 93157         if( !pEnc->zName ){
 93158           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
 93162   }else
 93163 #endif /* SQLITE_OMIT_UTF16 */
 93165 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
 93166   /*
 93167   **   PRAGMA [database.]schema_version
 93168   **   PRAGMA [database.]schema_version = <integer>
 93169   **
 93170   **   PRAGMA [database.]user_version
 93171   **   PRAGMA [database.]user_version = <integer>
 93172   **
 93173   ** The pragma's schema_version and user_version are used to set or get
 93174   ** the value of the schema-version and user-version, respectively. Both
 93175   ** the schema-version and the user-version are 32-bit signed integers
 93176   ** stored in the database header.
 93177   **
 93178   ** The schema-cookie is usually only manipulated internally by SQLite. It
 93179   ** is incremented by SQLite whenever the database schema is modified (by
 93180   ** creating or dropping a table or index). The schema version is used by
 93181   ** SQLite each time a query is executed to ensure that the internal cache
 93182   ** of the schema used when compiling the SQL query matches the schema of
 93183   ** the database against which the compiled query is actually executed.
 93184   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
 93185   ** the schema-version is potentially dangerous and may lead to program
 93186   ** crashes or database corruption. Use with caution!
 93187   **
 93188   ** The user-version is not used internally by SQLite. It may be used by
 93189   ** applications for any purpose.
 93190   */
 93191   if( sqlite3StrICmp(zLeft, "schema_version")==0 
 93192    || sqlite3StrICmp(zLeft, "user_version")==0 
 93193    || sqlite3StrICmp(zLeft, "freelist_count")==0 
 93194   ){
 93195     int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
 93196     sqlite3VdbeUsesBtree(v, iDb);
 93197     switch( zLeft[0] ){
 93198       case 'f': case 'F':
 93199         iCookie = BTREE_FREE_PAGE_COUNT;
 93200         break;
 93201       case 's': case 'S':
 93202         iCookie = BTREE_SCHEMA_VERSION;
 93203         break;
 93204       default:
 93205         iCookie = BTREE_USER_VERSION;
 93206         break;
 93209     if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
 93210       /* Write the specified cookie value */
 93211       static const VdbeOpList setCookie[] = {
 93212         { OP_Transaction,    0,  1,  0},    /* 0 */
 93213         { OP_Integer,        0,  1,  0},    /* 1 */
 93214         { OP_SetCookie,      0,  0,  1},    /* 2 */
 93215       };
 93216       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
 93217       sqlite3VdbeChangeP1(v, addr, iDb);
 93218       sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
 93219       sqlite3VdbeChangeP1(v, addr+2, iDb);
 93220       sqlite3VdbeChangeP2(v, addr+2, iCookie);
 93221     }else{
 93222       /* Read the specified cookie value */
 93223       static const VdbeOpList readCookie[] = {
 93224         { OP_Transaction,     0,  0,  0},    /* 0 */
 93225         { OP_ReadCookie,      0,  1,  0},    /* 1 */
 93226         { OP_ResultRow,       1,  1,  0}
 93227       };
 93228       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
 93229       sqlite3VdbeChangeP1(v, addr, iDb);
 93230       sqlite3VdbeChangeP1(v, addr+1, iDb);
 93231       sqlite3VdbeChangeP3(v, addr+1, iCookie);
 93232       sqlite3VdbeSetNumCols(v, 1);
 93233       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
 93235   }else
 93236 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
 93238 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 93239   /*
 93240   **   PRAGMA compile_options
 93241   **
 93242   ** Return the names of all compile-time options used in this build,
 93243   ** one option per row.
 93244   */
 93245   if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
 93246     int i = 0;
 93247     const char *zOpt;
 93248     sqlite3VdbeSetNumCols(v, 1);
 93249     pParse->nMem = 1;
 93250     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
 93251     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
 93252       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
 93253       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 93255   }else
 93256 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 93258 #ifndef SQLITE_OMIT_WAL
 93259   /*
 93260   **   PRAGMA [database.]wal_checkpoint = passive|full|restart
 93261   **
 93262   ** Checkpoint the database.
 93263   */
 93264   if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
 93265     int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
 93266     int eMode = SQLITE_CHECKPOINT_PASSIVE;
 93267     if( zRight ){
 93268       if( sqlite3StrICmp(zRight, "full")==0 ){
 93269         eMode = SQLITE_CHECKPOINT_FULL;
 93270       }else if( sqlite3StrICmp(zRight, "restart")==0 ){
 93271         eMode = SQLITE_CHECKPOINT_RESTART;
 93274     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 93275     sqlite3VdbeSetNumCols(v, 3);
 93276     pParse->nMem = 3;
 93277     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
 93278     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
 93279     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
 93281     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
 93282     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 93283   }else
 93285   /*
 93286   **   PRAGMA wal_autocheckpoint
 93287   **   PRAGMA wal_autocheckpoint = N
 93288   **
 93289   ** Configure a database connection to automatically checkpoint a database
 93290   ** after accumulating N frames in the log. Or query for the current value
 93291   ** of N.
 93292   */
 93293   if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
 93294     if( zRight ){
 93295       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
 93297     returnSingleInt(pParse, "wal_autocheckpoint", 
 93298        db->xWalCallback==sqlite3WalDefaultHook ? 
 93299            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
 93300   }else
 93301 #endif
 93303   /*
 93304   **  PRAGMA shrink_memory
 93305   **
 93306   ** This pragma attempts to free as much memory as possible from the
 93307   ** current database connection.
 93308   */
 93309   if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
 93310     sqlite3_db_release_memory(db);
 93311   }else
 93313   /*
 93314   **   PRAGMA busy_timeout
 93315   **   PRAGMA busy_timeout = N
 93316   **
 93317   ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
 93318   ** if one is set.  If no busy handler or a different busy handler is set
 93319   ** then 0 is returned.  Setting the busy_timeout to 0 or negative
 93320   ** disables the timeout.
 93321   */
 93322   if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
 93323     if( zRight ){
 93324       sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
 93326     returnSingleInt(pParse, "timeout",  db->busyTimeout);
 93327   }else
 93329 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
 93330   /*
 93331   ** Report the current state of file logs for all databases
 93332   */
 93333   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
 93334     static const char *const azLockName[] = {
 93335       "unlocked", "shared", "reserved", "pending", "exclusive"
 93336     };
 93337     int i;
 93338     sqlite3VdbeSetNumCols(v, 2);
 93339     pParse->nMem = 2;
 93340     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
 93341     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
 93342     for(i=0; i<db->nDb; i++){
 93343       Btree *pBt;
 93344       const char *zState = "unknown";
 93345       int j;
 93346       if( db->aDb[i].zName==0 ) continue;
 93347       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
 93348       pBt = db->aDb[i].pBt;
 93349       if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
 93350         zState = "closed";
 93351       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
 93352                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
 93353          zState = azLockName[j];
 93355       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
 93356       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
 93359   }else
 93360 #endif
 93362 #ifdef SQLITE_HAS_CODEC
 93363   if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
 93364     sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
 93365   }else
 93366   if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
 93367     sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
 93368   }else
 93369   if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
 93370                  sqlite3StrICmp(zLeft, "hexrekey")==0) ){
 93371     int i, h1, h2;
 93372     char zKey[40];
 93373     for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
 93374       h1 += 9*(1&(h1>>6));
 93375       h2 += 9*(1&(h2>>6));
 93376       zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
 93378     if( (zLeft[3] & 0xf)==0xb ){
 93379       sqlite3_key(db, zKey, i/2);
 93380     }else{
 93381       sqlite3_rekey(db, zKey, i/2);
 93383   }else
 93384 #endif
 93385 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
 93386   if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
 93387 #ifdef SQLITE_HAS_CODEC
 93388     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
 93389       sqlite3_activate_see(&zRight[4]);
 93391 #endif
 93392 #ifdef SQLITE_ENABLE_CEROD
 93393     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
 93394       sqlite3_activate_cerod(&zRight[6]);
 93396 #endif
 93397   }else
 93398 #endif
 93401   {/* Empty ELSE clause */}
 93403   /*
 93404   ** Reset the safety level, in case the fullfsync flag or synchronous
 93405   ** setting changed.
 93406   */
 93407 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 93408   if( db->autoCommit ){
 93409     sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
 93410                (db->flags&SQLITE_FullFSync)!=0,
 93411                (db->flags&SQLITE_CkptFullFSync)!=0);
 93413 #endif
 93414 pragma_out:
 93415   sqlite3DbFree(db, zLeft);
 93416   sqlite3DbFree(db, zRight);
 93419 #endif /* SQLITE_OMIT_PRAGMA */
 93421 /************** End of pragma.c **********************************************/
 93422 /************** Begin file prepare.c *****************************************/
 93423 /*
 93424 ** 2005 May 25
 93425 **
 93426 ** The author disclaims copyright to this source code.  In place of
 93427 ** a legal notice, here is a blessing:
 93428 **
 93429 **    May you do good and not evil.
 93430 **    May you find forgiveness for yourself and forgive others.
 93431 **    May you share freely, never taking more than you give.
 93432 **
 93433 *************************************************************************
 93434 ** This file contains the implementation of the sqlite3_prepare()
 93435 ** interface, and routines that contribute to loading the database schema
 93436 ** from disk.
 93437 */
 93439 /*
 93440 ** Fill the InitData structure with an error message that indicates
 93441 ** that the database is corrupt.
 93442 */
 93443 static void corruptSchema(
 93444   InitData *pData,     /* Initialization context */
 93445   const char *zObj,    /* Object being parsed at the point of error */
 93446   const char *zExtra   /* Error information */
 93447 ){
 93448   sqlite3 *db = pData->db;
 93449   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
 93450     if( zObj==0 ) zObj = "?";
 93451     sqlite3SetString(pData->pzErrMsg, db,
 93452       "malformed database schema (%s)", zObj);
 93453     if( zExtra ){
 93454       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
 93455                                  "%s - %s", *pData->pzErrMsg, zExtra);
 93458   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
 93461 /*
 93462 ** This is the callback routine for the code that initializes the
 93463 ** database.  See sqlite3Init() below for additional information.
 93464 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
 93465 **
 93466 ** Each callback contains the following information:
 93467 **
 93468 **     argv[0] = name of thing being created
 93469 **     argv[1] = root page number for table or index. 0 for trigger or view.
 93470 **     argv[2] = SQL text for the CREATE statement.
 93471 **
 93472 */
 93473 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
 93474   InitData *pData = (InitData*)pInit;
 93475   sqlite3 *db = pData->db;
 93476   int iDb = pData->iDb;
 93478   assert( argc==3 );
 93479   UNUSED_PARAMETER2(NotUsed, argc);
 93480   assert( sqlite3_mutex_held(db->mutex) );
 93481   DbClearProperty(db, iDb, DB_Empty);
 93482   if( db->mallocFailed ){
 93483     corruptSchema(pData, argv[0], 0);
 93484     return 1;
 93487   assert( iDb>=0 && iDb<db->nDb );
 93488   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
 93489   if( argv[1]==0 ){
 93490     corruptSchema(pData, argv[0], 0);
 93491   }else if( argv[2] && argv[2][0] ){
 93492     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
 93493     ** But because db->init.busy is set to 1, no VDBE code is generated
 93494     ** or executed.  All the parser does is build the internal data
 93495     ** structures that describe the table, index, or view.
 93496     */
 93497     int rc;
 93498     sqlite3_stmt *pStmt;
 93499     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
 93501     assert( db->init.busy );
 93502     db->init.iDb = iDb;
 93503     db->init.newTnum = sqlite3Atoi(argv[1]);
 93504     db->init.orphanTrigger = 0;
 93505     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
 93506     rc = db->errCode;
 93507     assert( (rc&0xFF)==(rcp&0xFF) );
 93508     db->init.iDb = 0;
 93509     if( SQLITE_OK!=rc ){
 93510       if( db->init.orphanTrigger ){
 93511         assert( iDb==1 );
 93512       }else{
 93513         pData->rc = rc;
 93514         if( rc==SQLITE_NOMEM ){
 93515           db->mallocFailed = 1;
 93516         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
 93517           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
 93521     sqlite3_finalize(pStmt);
 93522   }else if( argv[0]==0 ){
 93523     corruptSchema(pData, 0, 0);
 93524   }else{
 93525     /* If the SQL column is blank it means this is an index that
 93526     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
 93527     ** constraint for a CREATE TABLE.  The index should have already
 93528     ** been created when we processed the CREATE TABLE.  All we have
 93529     ** to do here is record the root page number for that index.
 93530     */
 93531     Index *pIndex;
 93532     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
 93533     if( pIndex==0 ){
 93534       /* This can occur if there exists an index on a TEMP table which
 93535       ** has the same name as another index on a permanent index.  Since
 93536       ** the permanent table is hidden by the TEMP table, we can also
 93537       ** safely ignore the index on the permanent table.
 93538       */
 93539       /* Do Nothing */;
 93540     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
 93541       corruptSchema(pData, argv[0], "invalid rootpage");
 93544   return 0;
 93547 /*
 93548 ** Attempt to read the database schema and initialize internal
 93549 ** data structures for a single database file.  The index of the
 93550 ** database file is given by iDb.  iDb==0 is used for the main
 93551 ** database.  iDb==1 should never be used.  iDb>=2 is used for
 93552 ** auxiliary databases.  Return one of the SQLITE_ error codes to
 93553 ** indicate success or failure.
 93554 */
 93555 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
 93556   int rc;
 93557   int i;
 93558 #ifndef SQLITE_OMIT_DEPRECATED
 93559   int size;
 93560 #endif
 93561   Table *pTab;
 93562   Db *pDb;
 93563   char const *azArg[4];
 93564   int meta[5];
 93565   InitData initData;
 93566   char const *zMasterSchema;
 93567   char const *zMasterName;
 93568   int openedTransaction = 0;
 93570   /*
 93571   ** The master database table has a structure like this
 93572   */
 93573   static const char master_schema[] = 
 93574      "CREATE TABLE sqlite_master(\n"
 93575      "  type text,\n"
 93576      "  name text,\n"
 93577      "  tbl_name text,\n"
 93578      "  rootpage integer,\n"
 93579      "  sql text\n"
 93580      ")"
 93582 #ifndef SQLITE_OMIT_TEMPDB
 93583   static const char temp_master_schema[] = 
 93584      "CREATE TEMP TABLE sqlite_temp_master(\n"
 93585      "  type text,\n"
 93586      "  name text,\n"
 93587      "  tbl_name text,\n"
 93588      "  rootpage integer,\n"
 93589      "  sql text\n"
 93590      ")"
 93592 #else
 93593   #define temp_master_schema 0
 93594 #endif
 93596   assert( iDb>=0 && iDb<db->nDb );
 93597   assert( db->aDb[iDb].pSchema );
 93598   assert( sqlite3_mutex_held(db->mutex) );
 93599   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
 93601   /* zMasterSchema and zInitScript are set to point at the master schema
 93602   ** and initialisation script appropriate for the database being
 93603   ** initialised. zMasterName is the name of the master table.
 93604   */
 93605   if( !OMIT_TEMPDB && iDb==1 ){
 93606     zMasterSchema = temp_master_schema;
 93607   }else{
 93608     zMasterSchema = master_schema;
 93610   zMasterName = SCHEMA_TABLE(iDb);
 93612   /* Construct the schema tables.  */
 93613   azArg[0] = zMasterName;
 93614   azArg[1] = "1";
 93615   azArg[2] = zMasterSchema;
 93616   azArg[3] = 0;
 93617   initData.db = db;
 93618   initData.iDb = iDb;
 93619   initData.rc = SQLITE_OK;
 93620   initData.pzErrMsg = pzErrMsg;
 93621   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
 93622   if( initData.rc ){
 93623     rc = initData.rc;
 93624     goto error_out;
 93626   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
 93627   if( ALWAYS(pTab) ){
 93628     pTab->tabFlags |= TF_Readonly;
 93631   /* Create a cursor to hold the database open
 93632   */
 93633   pDb = &db->aDb[iDb];
 93634   if( pDb->pBt==0 ){
 93635     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
 93636       DbSetProperty(db, 1, DB_SchemaLoaded);
 93638     return SQLITE_OK;
 93641   /* If there is not already a read-only (or read-write) transaction opened
 93642   ** on the b-tree database, open one now. If a transaction is opened, it 
 93643   ** will be closed before this function returns.  */
 93644   sqlite3BtreeEnter(pDb->pBt);
 93645   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
 93646     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
 93647     if( rc!=SQLITE_OK ){
 93648       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
 93649       goto initone_error_out;
 93651     openedTransaction = 1;
 93654   /* Get the database meta information.
 93655   **
 93656   ** Meta values are as follows:
 93657   **    meta[0]   Schema cookie.  Changes with each schema change.
 93658   **    meta[1]   File format of schema layer.
 93659   **    meta[2]   Size of the page cache.
 93660   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
 93661   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
 93662   **    meta[5]   User version
 93663   **    meta[6]   Incremental vacuum mode
 93664   **    meta[7]   unused
 93665   **    meta[8]   unused
 93666   **    meta[9]   unused
 93667   **
 93668   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
 93669   ** the possible values of meta[4].
 93670   */
 93671   for(i=0; i<ArraySize(meta); i++){
 93672     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
 93674   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
 93676   /* If opening a non-empty database, check the text encoding. For the
 93677   ** main database, set sqlite3.enc to the encoding of the main database.
 93678   ** For an attached db, it is an error if the encoding is not the same
 93679   ** as sqlite3.enc.
 93680   */
 93681   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
 93682     if( iDb==0 ){
 93683       u8 encoding;
 93684       /* If opening the main database, set ENC(db). */
 93685       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
 93686       if( encoding==0 ) encoding = SQLITE_UTF8;
 93687       ENC(db) = encoding;
 93688     }else{
 93689       /* If opening an attached database, the encoding much match ENC(db) */
 93690       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
 93691         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
 93692             " text encoding as main database");
 93693         rc = SQLITE_ERROR;
 93694         goto initone_error_out;
 93697   }else{
 93698     DbSetProperty(db, iDb, DB_Empty);
 93700   pDb->pSchema->enc = ENC(db);
 93702   if( pDb->pSchema->cache_size==0 ){
 93703 #ifndef SQLITE_OMIT_DEPRECATED
 93704     size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
 93705     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
 93706     pDb->pSchema->cache_size = size;
 93707 #else
 93708     pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
 93709 #endif
 93710     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 93713   /*
 93714   ** file_format==1    Version 3.0.0.
 93715   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
 93716   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
 93717   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
 93718   */
 93719   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
 93720   if( pDb->pSchema->file_format==0 ){
 93721     pDb->pSchema->file_format = 1;
 93723   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
 93724     sqlite3SetString(pzErrMsg, db, "unsupported file format");
 93725     rc = SQLITE_ERROR;
 93726     goto initone_error_out;
 93729   /* Ticket #2804:  When we open a database in the newer file format,
 93730   ** clear the legacy_file_format pragma flag so that a VACUUM will
 93731   ** not downgrade the database and thus invalidate any descending
 93732   ** indices that the user might have created.
 93733   */
 93734   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
 93735     db->flags &= ~SQLITE_LegacyFileFmt;
 93738   /* Read the schema information out of the schema tables
 93739   */
 93740   assert( db->init.busy );
 93742     char *zSql;
 93743     zSql = sqlite3MPrintf(db, 
 93744         "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
 93745         db->aDb[iDb].zName, zMasterName);
 93746 #ifndef SQLITE_OMIT_AUTHORIZATION
 93748       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
 93749       xAuth = db->xAuth;
 93750       db->xAuth = 0;
 93751 #endif
 93752       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
 93753 #ifndef SQLITE_OMIT_AUTHORIZATION
 93754       db->xAuth = xAuth;
 93756 #endif
 93757     if( rc==SQLITE_OK ) rc = initData.rc;
 93758     sqlite3DbFree(db, zSql);
 93759 #ifndef SQLITE_OMIT_ANALYZE
 93760     if( rc==SQLITE_OK ){
 93761       sqlite3AnalysisLoad(db, iDb);
 93763 #endif
 93765   if( db->mallocFailed ){
 93766     rc = SQLITE_NOMEM;
 93767     sqlite3ResetAllSchemasOfConnection(db);
 93769   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
 93770     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
 93771     ** the schema loaded, even if errors occurred. In this situation the 
 93772     ** current sqlite3_prepare() operation will fail, but the following one
 93773     ** will attempt to compile the supplied statement against whatever subset
 93774     ** of the schema was loaded before the error occurred. The primary
 93775     ** purpose of this is to allow access to the sqlite_master table
 93776     ** even when its contents have been corrupted.
 93777     */
 93778     DbSetProperty(db, iDb, DB_SchemaLoaded);
 93779     rc = SQLITE_OK;
 93782   /* Jump here for an error that occurs after successfully allocating
 93783   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
 93784   ** before that point, jump to error_out.
 93785   */
 93786 initone_error_out:
 93787   if( openedTransaction ){
 93788     sqlite3BtreeCommit(pDb->pBt);
 93790   sqlite3BtreeLeave(pDb->pBt);
 93792 error_out:
 93793   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 93794     db->mallocFailed = 1;
 93796   return rc;
 93799 /*
 93800 ** Initialize all database files - the main database file, the file
 93801 ** used to store temporary tables, and any additional database files
 93802 ** created using ATTACH statements.  Return a success code.  If an
 93803 ** error occurs, write an error message into *pzErrMsg.
 93804 **
 93805 ** After a database is initialized, the DB_SchemaLoaded bit is set
 93806 ** bit is set in the flags field of the Db structure. If the database
 93807 ** file was of zero-length, then the DB_Empty flag is also set.
 93808 */
 93809 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
 93810   int i, rc;
 93811   int commit_internal = !(db->flags&SQLITE_InternChanges);
 93813   assert( sqlite3_mutex_held(db->mutex) );
 93814   rc = SQLITE_OK;
 93815   db->init.busy = 1;
 93816   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 93817     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
 93818     rc = sqlite3InitOne(db, i, pzErrMsg);
 93819     if( rc ){
 93820       sqlite3ResetOneSchema(db, i);
 93824   /* Once all the other databases have been initialised, load the schema
 93825   ** for the TEMP database. This is loaded last, as the TEMP database
 93826   ** schema may contain references to objects in other databases.
 93827   */
 93828 #ifndef SQLITE_OMIT_TEMPDB
 93829   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
 93830                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
 93831     rc = sqlite3InitOne(db, 1, pzErrMsg);
 93832     if( rc ){
 93833       sqlite3ResetOneSchema(db, 1);
 93836 #endif
 93838   db->init.busy = 0;
 93839   if( rc==SQLITE_OK && commit_internal ){
 93840     sqlite3CommitInternalChanges(db);
 93843   return rc; 
 93846 /*
 93847 ** This routine is a no-op if the database schema is already initialised.
 93848 ** Otherwise, the schema is loaded. An error code is returned.
 93849 */
 93850 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
 93851   int rc = SQLITE_OK;
 93852   sqlite3 *db = pParse->db;
 93853   assert( sqlite3_mutex_held(db->mutex) );
 93854   if( !db->init.busy ){
 93855     rc = sqlite3Init(db, &pParse->zErrMsg);
 93857   if( rc!=SQLITE_OK ){
 93858     pParse->rc = rc;
 93859     pParse->nErr++;
 93861   return rc;
 93865 /*
 93866 ** Check schema cookies in all databases.  If any cookie is out
 93867 ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
 93868 ** make no changes to pParse->rc.
 93869 */
 93870 static void schemaIsValid(Parse *pParse){
 93871   sqlite3 *db = pParse->db;
 93872   int iDb;
 93873   int rc;
 93874   int cookie;
 93876   assert( pParse->checkSchema );
 93877   assert( sqlite3_mutex_held(db->mutex) );
 93878   for(iDb=0; iDb<db->nDb; iDb++){
 93879     int openedTransaction = 0;         /* True if a transaction is opened */
 93880     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
 93881     if( pBt==0 ) continue;
 93883     /* If there is not already a read-only (or read-write) transaction opened
 93884     ** on the b-tree database, open one now. If a transaction is opened, it 
 93885     ** will be closed immediately after reading the meta-value. */
 93886     if( !sqlite3BtreeIsInReadTrans(pBt) ){
 93887       rc = sqlite3BtreeBeginTrans(pBt, 0);
 93888       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 93889         db->mallocFailed = 1;
 93891       if( rc!=SQLITE_OK ) return;
 93892       openedTransaction = 1;
 93895     /* Read the schema cookie from the database. If it does not match the 
 93896     ** value stored as part of the in-memory schema representation,
 93897     ** set Parse.rc to SQLITE_SCHEMA. */
 93898     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
 93899     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 93900     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
 93901       sqlite3ResetOneSchema(db, iDb);
 93902       pParse->rc = SQLITE_SCHEMA;
 93905     /* Close the transaction, if one was opened. */
 93906     if( openedTransaction ){
 93907       sqlite3BtreeCommit(pBt);
 93912 /*
 93913 ** Convert a schema pointer into the iDb index that indicates
 93914 ** which database file in db->aDb[] the schema refers to.
 93915 **
 93916 ** If the same database is attached more than once, the first
 93917 ** attached database is returned.
 93918 */
 93919 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
 93920   int i = -1000000;
 93922   /* If pSchema is NULL, then return -1000000. This happens when code in 
 93923   ** expr.c is trying to resolve a reference to a transient table (i.e. one
 93924   ** created by a sub-select). In this case the return value of this 
 93925   ** function should never be used.
 93926   **
 93927   ** We return -1000000 instead of the more usual -1 simply because using
 93928   ** -1000000 as the incorrect index into db->aDb[] is much 
 93929   ** more likely to cause a segfault than -1 (of course there are assert()
 93930   ** statements too, but it never hurts to play the odds).
 93931   */
 93932   assert( sqlite3_mutex_held(db->mutex) );
 93933   if( pSchema ){
 93934     for(i=0; ALWAYS(i<db->nDb); i++){
 93935       if( db->aDb[i].pSchema==pSchema ){
 93936         break;
 93939     assert( i>=0 && i<db->nDb );
 93941   return i;
 93944 /*
 93945 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
 93946 */
 93947 static int sqlite3Prepare(
 93948   sqlite3 *db,              /* Database handle. */
 93949   const char *zSql,         /* UTF-8 encoded SQL statement. */
 93950   int nBytes,               /* Length of zSql in bytes. */
 93951   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
 93952   Vdbe *pReprepare,         /* VM being reprepared */
 93953   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 93954   const char **pzTail       /* OUT: End of parsed string */
 93955 ){
 93956   Parse *pParse;            /* Parsing context */
 93957   char *zErrMsg = 0;        /* Error message */
 93958   int rc = SQLITE_OK;       /* Result code */
 93959   int i;                    /* Loop counter */
 93961   /* Allocate the parsing context */
 93962   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
 93963   if( pParse==0 ){
 93964     rc = SQLITE_NOMEM;
 93965     goto end_prepare;
 93967   pParse->pReprepare = pReprepare;
 93968   assert( ppStmt && *ppStmt==0 );
 93969   assert( !db->mallocFailed );
 93970   assert( sqlite3_mutex_held(db->mutex) );
 93972   /* Check to verify that it is possible to get a read lock on all
 93973   ** database schemas.  The inability to get a read lock indicates that
 93974   ** some other database connection is holding a write-lock, which in
 93975   ** turn means that the other connection has made uncommitted changes
 93976   ** to the schema.
 93977   **
 93978   ** Were we to proceed and prepare the statement against the uncommitted
 93979   ** schema changes and if those schema changes are subsequently rolled
 93980   ** back and different changes are made in their place, then when this
 93981   ** prepared statement goes to run the schema cookie would fail to detect
 93982   ** the schema change.  Disaster would follow.
 93983   **
 93984   ** This thread is currently holding mutexes on all Btrees (because
 93985   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
 93986   ** is not possible for another thread to start a new schema change
 93987   ** while this routine is running.  Hence, we do not need to hold 
 93988   ** locks on the schema, we just need to make sure nobody else is 
 93989   ** holding them.
 93990   **
 93991   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
 93992   ** but it does *not* override schema lock detection, so this all still
 93993   ** works even if READ_UNCOMMITTED is set.
 93994   */
 93995   for(i=0; i<db->nDb; i++) {
 93996     Btree *pBt = db->aDb[i].pBt;
 93997     if( pBt ){
 93998       assert( sqlite3BtreeHoldsMutex(pBt) );
 93999       rc = sqlite3BtreeSchemaLocked(pBt);
 94000       if( rc ){
 94001         const char *zDb = db->aDb[i].zName;
 94002         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
 94003         testcase( db->flags & SQLITE_ReadUncommitted );
 94004         goto end_prepare;
 94009   sqlite3VtabUnlockList(db);
 94011   pParse->db = db;
 94012   pParse->nQueryLoop = (double)1;
 94013   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
 94014     char *zSqlCopy;
 94015     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
 94016     testcase( nBytes==mxLen );
 94017     testcase( nBytes==mxLen+1 );
 94018     if( nBytes>mxLen ){
 94019       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
 94020       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
 94021       goto end_prepare;
 94023     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
 94024     if( zSqlCopy ){
 94025       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
 94026       sqlite3DbFree(db, zSqlCopy);
 94027       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
 94028     }else{
 94029       pParse->zTail = &zSql[nBytes];
 94031   }else{
 94032     sqlite3RunParser(pParse, zSql, &zErrMsg);
 94034   assert( 1==(int)pParse->nQueryLoop );
 94036   if( db->mallocFailed ){
 94037     pParse->rc = SQLITE_NOMEM;
 94039   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
 94040   if( pParse->checkSchema ){
 94041     schemaIsValid(pParse);
 94043   if( db->mallocFailed ){
 94044     pParse->rc = SQLITE_NOMEM;
 94046   if( pzTail ){
 94047     *pzTail = pParse->zTail;
 94049   rc = pParse->rc;
 94051 #ifndef SQLITE_OMIT_EXPLAIN
 94052   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
 94053     static const char * const azColName[] = {
 94054        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
 94055        "selectid", "order", "from", "detail"
 94056     };
 94057     int iFirst, mx;
 94058     if( pParse->explain==2 ){
 94059       sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
 94060       iFirst = 8;
 94061       mx = 12;
 94062     }else{
 94063       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
 94064       iFirst = 0;
 94065       mx = 8;
 94067     for(i=iFirst; i<mx; i++){
 94068       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
 94069                             azColName[i], SQLITE_STATIC);
 94072 #endif
 94074   assert( db->init.busy==0 || saveSqlFlag==0 );
 94075   if( db->init.busy==0 ){
 94076     Vdbe *pVdbe = pParse->pVdbe;
 94077     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
 94079   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
 94080     sqlite3VdbeFinalize(pParse->pVdbe);
 94081     assert(!(*ppStmt));
 94082   }else{
 94083     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
 94086   if( zErrMsg ){
 94087     sqlite3Error(db, rc, "%s", zErrMsg);
 94088     sqlite3DbFree(db, zErrMsg);
 94089   }else{
 94090     sqlite3Error(db, rc, 0);
 94093   /* Delete any TriggerPrg structures allocated while parsing this statement. */
 94094   while( pParse->pTriggerPrg ){
 94095     TriggerPrg *pT = pParse->pTriggerPrg;
 94096     pParse->pTriggerPrg = pT->pNext;
 94097     sqlite3DbFree(db, pT);
 94100 end_prepare:
 94102   sqlite3StackFree(db, pParse);
 94103   rc = sqlite3ApiExit(db, rc);
 94104   assert( (rc&db->errMask)==rc );
 94105   return rc;
 94107 static int sqlite3LockAndPrepare(
 94108   sqlite3 *db,              /* Database handle. */
 94109   const char *zSql,         /* UTF-8 encoded SQL statement. */
 94110   int nBytes,               /* Length of zSql in bytes. */
 94111   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
 94112   Vdbe *pOld,               /* VM being reprepared */
 94113   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 94114   const char **pzTail       /* OUT: End of parsed string */
 94115 ){
 94116   int rc;
 94117   assert( ppStmt!=0 );
 94118   *ppStmt = 0;
 94119   if( !sqlite3SafetyCheckOk(db) ){
 94120     return SQLITE_MISUSE_BKPT;
 94122   sqlite3_mutex_enter(db->mutex);
 94123   sqlite3BtreeEnterAll(db);
 94124   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
 94125   if( rc==SQLITE_SCHEMA ){
 94126     sqlite3_finalize(*ppStmt);
 94127     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
 94129   sqlite3BtreeLeaveAll(db);
 94130   sqlite3_mutex_leave(db->mutex);
 94131   assert( rc==SQLITE_OK || *ppStmt==0 );
 94132   return rc;
 94135 /*
 94136 ** Rerun the compilation of a statement after a schema change.
 94137 **
 94138 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
 94139 ** if the statement cannot be recompiled because another connection has
 94140 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
 94141 ** occurs, return SQLITE_SCHEMA.
 94142 */
 94143 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
 94144   int rc;
 94145   sqlite3_stmt *pNew;
 94146   const char *zSql;
 94147   sqlite3 *db;
 94149   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
 94150   zSql = sqlite3_sql((sqlite3_stmt *)p);
 94151   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
 94152   db = sqlite3VdbeDb(p);
 94153   assert( sqlite3_mutex_held(db->mutex) );
 94154   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
 94155   if( rc ){
 94156     if( rc==SQLITE_NOMEM ){
 94157       db->mallocFailed = 1;
 94159     assert( pNew==0 );
 94160     return rc;
 94161   }else{
 94162     assert( pNew!=0 );
 94164   sqlite3VdbeSwap((Vdbe*)pNew, p);
 94165   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
 94166   sqlite3VdbeResetStepResult((Vdbe*)pNew);
 94167   sqlite3VdbeFinalize((Vdbe*)pNew);
 94168   return SQLITE_OK;
 94172 /*
 94173 ** Two versions of the official API.  Legacy and new use.  In the legacy
 94174 ** version, the original SQL text is not saved in the prepared statement
 94175 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
 94176 ** sqlite3_step().  In the new version, the original SQL text is retained
 94177 ** and the statement is automatically recompiled if an schema change
 94178 ** occurs.
 94179 */
 94180 SQLITE_API int sqlite3_prepare(
 94181   sqlite3 *db,              /* Database handle. */
 94182   const char *zSql,         /* UTF-8 encoded SQL statement. */
 94183   int nBytes,               /* Length of zSql in bytes. */
 94184   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 94185   const char **pzTail       /* OUT: End of parsed string */
 94186 ){
 94187   int rc;
 94188   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
 94189   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 94190   return rc;
 94192 SQLITE_API int sqlite3_prepare_v2(
 94193   sqlite3 *db,              /* Database handle. */
 94194   const char *zSql,         /* UTF-8 encoded SQL statement. */
 94195   int nBytes,               /* Length of zSql in bytes. */
 94196   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 94197   const char **pzTail       /* OUT: End of parsed string */
 94198 ){
 94199   int rc;
 94200   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
 94201   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 94202   return rc;
 94206 #ifndef SQLITE_OMIT_UTF16
 94207 /*
 94208 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
 94209 */
 94210 static int sqlite3Prepare16(
 94211   sqlite3 *db,              /* Database handle. */ 
 94212   const void *zSql,         /* UTF-16 encoded SQL statement. */
 94213   int nBytes,               /* Length of zSql in bytes. */
 94214   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
 94215   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 94216   const void **pzTail       /* OUT: End of parsed string */
 94217 ){
 94218   /* This function currently works by first transforming the UTF-16
 94219   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
 94220   ** tricky bit is figuring out the pointer to return in *pzTail.
 94221   */
 94222   char *zSql8;
 94223   const char *zTail8 = 0;
 94224   int rc = SQLITE_OK;
 94226   assert( ppStmt );
 94227   *ppStmt = 0;
 94228   if( !sqlite3SafetyCheckOk(db) ){
 94229     return SQLITE_MISUSE_BKPT;
 94231   sqlite3_mutex_enter(db->mutex);
 94232   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
 94233   if( zSql8 ){
 94234     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
 94237   if( zTail8 && pzTail ){
 94238     /* If sqlite3_prepare returns a tail pointer, we calculate the
 94239     ** equivalent pointer into the UTF-16 string by counting the unicode
 94240     ** characters between zSql8 and zTail8, and then returning a pointer
 94241     ** the same number of characters into the UTF-16 string.
 94242     */
 94243     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
 94244     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
 94246   sqlite3DbFree(db, zSql8); 
 94247   rc = sqlite3ApiExit(db, rc);
 94248   sqlite3_mutex_leave(db->mutex);
 94249   return rc;
 94252 /*
 94253 ** Two versions of the official API.  Legacy and new use.  In the legacy
 94254 ** version, the original SQL text is not saved in the prepared statement
 94255 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
 94256 ** sqlite3_step().  In the new version, the original SQL text is retained
 94257 ** and the statement is automatically recompiled if an schema change
 94258 ** occurs.
 94259 */
 94260 SQLITE_API int sqlite3_prepare16(
 94261   sqlite3 *db,              /* Database handle. */ 
 94262   const void *zSql,         /* UTF-16 encoded SQL statement. */
 94263   int nBytes,               /* Length of zSql in bytes. */
 94264   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 94265   const void **pzTail       /* OUT: End of parsed string */
 94266 ){
 94267   int rc;
 94268   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
 94269   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 94270   return rc;
 94272 SQLITE_API int sqlite3_prepare16_v2(
 94273   sqlite3 *db,              /* Database handle. */ 
 94274   const void *zSql,         /* UTF-16 encoded SQL statement. */
 94275   int nBytes,               /* Length of zSql in bytes. */
 94276   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 94277   const void **pzTail       /* OUT: End of parsed string */
 94278 ){
 94279   int rc;
 94280   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
 94281   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 94282   return rc;
 94285 #endif /* SQLITE_OMIT_UTF16 */
 94287 /************** End of prepare.c *********************************************/
 94288 /************** Begin file select.c ******************************************/
 94289 /*
 94290 ** 2001 September 15
 94291 **
 94292 ** The author disclaims copyright to this source code.  In place of
 94293 ** a legal notice, here is a blessing:
 94294 **
 94295 **    May you do good and not evil.
 94296 **    May you find forgiveness for yourself and forgive others.
 94297 **    May you share freely, never taking more than you give.
 94298 **
 94299 *************************************************************************
 94300 ** This file contains C code routines that are called by the parser
 94301 ** to handle SELECT statements in SQLite.
 94302 */
 94305 /*
 94306 ** Delete all the content of a Select structure but do not deallocate
 94307 ** the select structure itself.
 94308 */
 94309 static void clearSelect(sqlite3 *db, Select *p){
 94310   sqlite3ExprListDelete(db, p->pEList);
 94311   sqlite3SrcListDelete(db, p->pSrc);
 94312   sqlite3ExprDelete(db, p->pWhere);
 94313   sqlite3ExprListDelete(db, p->pGroupBy);
 94314   sqlite3ExprDelete(db, p->pHaving);
 94315   sqlite3ExprListDelete(db, p->pOrderBy);
 94316   sqlite3SelectDelete(db, p->pPrior);
 94317   sqlite3ExprDelete(db, p->pLimit);
 94318   sqlite3ExprDelete(db, p->pOffset);
 94321 /*
 94322 ** Initialize a SelectDest structure.
 94323 */
 94324 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
 94325   pDest->eDest = (u8)eDest;
 94326   pDest->iSDParm = iParm;
 94327   pDest->affSdst = 0;
 94328   pDest->iSdst = 0;
 94329   pDest->nSdst = 0;
 94333 /*
 94334 ** Allocate a new Select structure and return a pointer to that
 94335 ** structure.
 94336 */
 94337 SQLITE_PRIVATE Select *sqlite3SelectNew(
 94338   Parse *pParse,        /* Parsing context */
 94339   ExprList *pEList,     /* which columns to include in the result */
 94340   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
 94341   Expr *pWhere,         /* the WHERE clause */
 94342   ExprList *pGroupBy,   /* the GROUP BY clause */
 94343   Expr *pHaving,        /* the HAVING clause */
 94344   ExprList *pOrderBy,   /* the ORDER BY clause */
 94345   int isDistinct,       /* true if the DISTINCT keyword is present */
 94346   Expr *pLimit,         /* LIMIT value.  NULL means not used */
 94347   Expr *pOffset         /* OFFSET value.  NULL means no offset */
 94348 ){
 94349   Select *pNew;
 94350   Select standin;
 94351   sqlite3 *db = pParse->db;
 94352   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
 94353   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
 94354   if( pNew==0 ){
 94355     assert( db->mallocFailed );
 94356     pNew = &standin;
 94357     memset(pNew, 0, sizeof(*pNew));
 94359   if( pEList==0 ){
 94360     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
 94362   pNew->pEList = pEList;
 94363   if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
 94364   pNew->pSrc = pSrc;
 94365   pNew->pWhere = pWhere;
 94366   pNew->pGroupBy = pGroupBy;
 94367   pNew->pHaving = pHaving;
 94368   pNew->pOrderBy = pOrderBy;
 94369   pNew->selFlags = isDistinct ? SF_Distinct : 0;
 94370   pNew->op = TK_SELECT;
 94371   pNew->pLimit = pLimit;
 94372   pNew->pOffset = pOffset;
 94373   assert( pOffset==0 || pLimit!=0 );
 94374   pNew->addrOpenEphm[0] = -1;
 94375   pNew->addrOpenEphm[1] = -1;
 94376   pNew->addrOpenEphm[2] = -1;
 94377   if( db->mallocFailed ) {
 94378     clearSelect(db, pNew);
 94379     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
 94380     pNew = 0;
 94381   }else{
 94382     assert( pNew->pSrc!=0 || pParse->nErr>0 );
 94384   assert( pNew!=&standin );
 94385   return pNew;
 94388 /*
 94389 ** Delete the given Select structure and all of its substructures.
 94390 */
 94391 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
 94392   if( p ){
 94393     clearSelect(db, p);
 94394     sqlite3DbFree(db, p);
 94398 /*
 94399 ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
 94400 ** type of join.  Return an integer constant that expresses that type
 94401 ** in terms of the following bit values:
 94402 **
 94403 **     JT_INNER
 94404 **     JT_CROSS
 94405 **     JT_OUTER
 94406 **     JT_NATURAL
 94407 **     JT_LEFT
 94408 **     JT_RIGHT
 94409 **
 94410 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
 94411 **
 94412 ** If an illegal or unsupported join type is seen, then still return
 94413 ** a join type, but put an error in the pParse structure.
 94414 */
 94415 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
 94416   int jointype = 0;
 94417   Token *apAll[3];
 94418   Token *p;
 94419                              /*   0123456789 123456789 123456789 123 */
 94420   static const char zKeyText[] = "naturaleftouterightfullinnercross";
 94421   static const struct {
 94422     u8 i;        /* Beginning of keyword text in zKeyText[] */
 94423     u8 nChar;    /* Length of the keyword in characters */
 94424     u8 code;     /* Join type mask */
 94425   } aKeyword[] = {
 94426     /* natural */ { 0,  7, JT_NATURAL                },
 94427     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
 94428     /* outer   */ { 10, 5, JT_OUTER                  },
 94429     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
 94430     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
 94431     /* inner   */ { 23, 5, JT_INNER                  },
 94432     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
 94433   };
 94434   int i, j;
 94435   apAll[0] = pA;
 94436   apAll[1] = pB;
 94437   apAll[2] = pC;
 94438   for(i=0; i<3 && apAll[i]; i++){
 94439     p = apAll[i];
 94440     for(j=0; j<ArraySize(aKeyword); j++){
 94441       if( p->n==aKeyword[j].nChar 
 94442           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
 94443         jointype |= aKeyword[j].code;
 94444         break;
 94447     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
 94448     if( j>=ArraySize(aKeyword) ){
 94449       jointype |= JT_ERROR;
 94450       break;
 94453   if(
 94454      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
 94455      (jointype & JT_ERROR)!=0
 94456   ){
 94457     const char *zSp = " ";
 94458     assert( pB!=0 );
 94459     if( pC==0 ){ zSp++; }
 94460     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
 94461        "%T %T%s%T", pA, pB, zSp, pC);
 94462     jointype = JT_INNER;
 94463   }else if( (jointype & JT_OUTER)!=0 
 94464          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
 94465     sqlite3ErrorMsg(pParse, 
 94466       "RIGHT and FULL OUTER JOINs are not currently supported");
 94467     jointype = JT_INNER;
 94469   return jointype;
 94472 /*
 94473 ** Return the index of a column in a table.  Return -1 if the column
 94474 ** is not contained in the table.
 94475 */
 94476 static int columnIndex(Table *pTab, const char *zCol){
 94477   int i;
 94478   for(i=0; i<pTab->nCol; i++){
 94479     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
 94481   return -1;
 94484 /*
 94485 ** Search the first N tables in pSrc, from left to right, looking for a
 94486 ** table that has a column named zCol.  
 94487 **
 94488 ** When found, set *piTab and *piCol to the table index and column index
 94489 ** of the matching column and return TRUE.
 94490 **
 94491 ** If not found, return FALSE.
 94492 */
 94493 static int tableAndColumnIndex(
 94494   SrcList *pSrc,       /* Array of tables to search */
 94495   int N,               /* Number of tables in pSrc->a[] to search */
 94496   const char *zCol,    /* Name of the column we are looking for */
 94497   int *piTab,          /* Write index of pSrc->a[] here */
 94498   int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
 94499 ){
 94500   int i;               /* For looping over tables in pSrc */
 94501   int iCol;            /* Index of column matching zCol */
 94503   assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
 94504   for(i=0; i<N; i++){
 94505     iCol = columnIndex(pSrc->a[i].pTab, zCol);
 94506     if( iCol>=0 ){
 94507       if( piTab ){
 94508         *piTab = i;
 94509         *piCol = iCol;
 94511       return 1;
 94514   return 0;
 94517 /*
 94518 ** This function is used to add terms implied by JOIN syntax to the
 94519 ** WHERE clause expression of a SELECT statement. The new term, which
 94520 ** is ANDed with the existing WHERE clause, is of the form:
 94521 **
 94522 **    (tab1.col1 = tab2.col2)
 94523 **
 94524 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the 
 94525 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
 94526 ** column iColRight of tab2.
 94527 */
 94528 static void addWhereTerm(
 94529   Parse *pParse,                  /* Parsing context */
 94530   SrcList *pSrc,                  /* List of tables in FROM clause */
 94531   int iLeft,                      /* Index of first table to join in pSrc */
 94532   int iColLeft,                   /* Index of column in first table */
 94533   int iRight,                     /* Index of second table in pSrc */
 94534   int iColRight,                  /* Index of column in second table */
 94535   int isOuterJoin,                /* True if this is an OUTER join */
 94536   Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
 94537 ){
 94538   sqlite3 *db = pParse->db;
 94539   Expr *pE1;
 94540   Expr *pE2;
 94541   Expr *pEq;
 94543   assert( iLeft<iRight );
 94544   assert( pSrc->nSrc>iRight );
 94545   assert( pSrc->a[iLeft].pTab );
 94546   assert( pSrc->a[iRight].pTab );
 94548   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
 94549   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
 94551   pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
 94552   if( pEq && isOuterJoin ){
 94553     ExprSetProperty(pEq, EP_FromJoin);
 94554     assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
 94555     ExprSetIrreducible(pEq);
 94556     pEq->iRightJoinTable = (i16)pE2->iTable;
 94558   *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
 94561 /*
 94562 ** Set the EP_FromJoin property on all terms of the given expression.
 94563 ** And set the Expr.iRightJoinTable to iTable for every term in the
 94564 ** expression.
 94565 **
 94566 ** The EP_FromJoin property is used on terms of an expression to tell
 94567 ** the LEFT OUTER JOIN processing logic that this term is part of the
 94568 ** join restriction specified in the ON or USING clause and not a part
 94569 ** of the more general WHERE clause.  These terms are moved over to the
 94570 ** WHERE clause during join processing but we need to remember that they
 94571 ** originated in the ON or USING clause.
 94572 **
 94573 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
 94574 ** expression depends on table iRightJoinTable even if that table is not
 94575 ** explicitly mentioned in the expression.  That information is needed
 94576 ** for cases like this:
 94577 **
 94578 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
 94579 **
 94580 ** The where clause needs to defer the handling of the t1.x=5
 94581 ** term until after the t2 loop of the join.  In that way, a
 94582 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
 94583 ** defer the handling of t1.x=5, it will be processed immediately
 94584 ** after the t1 loop and rows with t1.x!=5 will never appear in
 94585 ** the output, which is incorrect.
 94586 */
 94587 static void setJoinExpr(Expr *p, int iTable){
 94588   while( p ){
 94589     ExprSetProperty(p, EP_FromJoin);
 94590     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
 94591     ExprSetIrreducible(p);
 94592     p->iRightJoinTable = (i16)iTable;
 94593     setJoinExpr(p->pLeft, iTable);
 94594     p = p->pRight;
 94598 /*
 94599 ** This routine processes the join information for a SELECT statement.
 94600 ** ON and USING clauses are converted into extra terms of the WHERE clause.
 94601 ** NATURAL joins also create extra WHERE clause terms.
 94602 **
 94603 ** The terms of a FROM clause are contained in the Select.pSrc structure.
 94604 ** The left most table is the first entry in Select.pSrc.  The right-most
 94605 ** table is the last entry.  The join operator is held in the entry to
 94606 ** the left.  Thus entry 0 contains the join operator for the join between
 94607 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
 94608 ** also attached to the left entry.
 94609 **
 94610 ** This routine returns the number of errors encountered.
 94611 */
 94612 static int sqliteProcessJoin(Parse *pParse, Select *p){
 94613   SrcList *pSrc;                  /* All tables in the FROM clause */
 94614   int i, j;                       /* Loop counters */
 94615   struct SrcList_item *pLeft;     /* Left table being joined */
 94616   struct SrcList_item *pRight;    /* Right table being joined */
 94618   pSrc = p->pSrc;
 94619   pLeft = &pSrc->a[0];
 94620   pRight = &pLeft[1];
 94621   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
 94622     Table *pLeftTab = pLeft->pTab;
 94623     Table *pRightTab = pRight->pTab;
 94624     int isOuter;
 94626     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
 94627     isOuter = (pRight->jointype & JT_OUTER)!=0;
 94629     /* When the NATURAL keyword is present, add WHERE clause terms for
 94630     ** every column that the two tables have in common.
 94631     */
 94632     if( pRight->jointype & JT_NATURAL ){
 94633       if( pRight->pOn || pRight->pUsing ){
 94634         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
 94635            "an ON or USING clause", 0);
 94636         return 1;
 94638       for(j=0; j<pRightTab->nCol; j++){
 94639         char *zName;   /* Name of column in the right table */
 94640         int iLeft;     /* Matching left table */
 94641         int iLeftCol;  /* Matching column in the left table */
 94643         zName = pRightTab->aCol[j].zName;
 94644         if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
 94645           addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
 94646                        isOuter, &p->pWhere);
 94651     /* Disallow both ON and USING clauses in the same join
 94652     */
 94653     if( pRight->pOn && pRight->pUsing ){
 94654       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
 94655         "clauses in the same join");
 94656       return 1;
 94659     /* Add the ON clause to the end of the WHERE clause, connected by
 94660     ** an AND operator.
 94661     */
 94662     if( pRight->pOn ){
 94663       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
 94664       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
 94665       pRight->pOn = 0;
 94668     /* Create extra terms on the WHERE clause for each column named
 94669     ** in the USING clause.  Example: If the two tables to be joined are 
 94670     ** A and B and the USING clause names X, Y, and Z, then add this
 94671     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
 94672     ** Report an error if any column mentioned in the USING clause is
 94673     ** not contained in both tables to be joined.
 94674     */
 94675     if( pRight->pUsing ){
 94676       IdList *pList = pRight->pUsing;
 94677       for(j=0; j<pList->nId; j++){
 94678         char *zName;     /* Name of the term in the USING clause */
 94679         int iLeft;       /* Table on the left with matching column name */
 94680         int iLeftCol;    /* Column number of matching column on the left */
 94681         int iRightCol;   /* Column number of matching column on the right */
 94683         zName = pList->a[j].zName;
 94684         iRightCol = columnIndex(pRightTab, zName);
 94685         if( iRightCol<0
 94686          || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
 94687         ){
 94688           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
 94689             "not present in both tables", zName);
 94690           return 1;
 94692         addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
 94693                      isOuter, &p->pWhere);
 94697   return 0;
 94700 /*
 94701 ** Insert code into "v" that will push the record on the top of the
 94702 ** stack into the sorter.
 94703 */
 94704 static void pushOntoSorter(
 94705   Parse *pParse,         /* Parser context */
 94706   ExprList *pOrderBy,    /* The ORDER BY clause */
 94707   Select *pSelect,       /* The whole SELECT statement */
 94708   int regData            /* Register holding data to be sorted */
 94709 ){
 94710   Vdbe *v = pParse->pVdbe;
 94711   int nExpr = pOrderBy->nExpr;
 94712   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
 94713   int regRecord = sqlite3GetTempReg(pParse);
 94714   int op;
 94715   sqlite3ExprCacheClear(pParse);
 94716   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
 94717   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
 94718   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
 94719   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
 94720   if( pSelect->selFlags & SF_UseSorter ){
 94721     op = OP_SorterInsert;
 94722   }else{
 94723     op = OP_IdxInsert;
 94725   sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
 94726   sqlite3ReleaseTempReg(pParse, regRecord);
 94727   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
 94728   if( pSelect->iLimit ){
 94729     int addr1, addr2;
 94730     int iLimit;
 94731     if( pSelect->iOffset ){
 94732       iLimit = pSelect->iOffset+1;
 94733     }else{
 94734       iLimit = pSelect->iLimit;
 94736     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
 94737     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
 94738     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
 94739     sqlite3VdbeJumpHere(v, addr1);
 94740     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
 94741     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
 94742     sqlite3VdbeJumpHere(v, addr2);
 94746 /*
 94747 ** Add code to implement the OFFSET
 94748 */
 94749 static void codeOffset(
 94750   Vdbe *v,          /* Generate code into this VM */
 94751   Select *p,        /* The SELECT statement being coded */
 94752   int iContinue     /* Jump here to skip the current record */
 94753 ){
 94754   if( p->iOffset && iContinue!=0 ){
 94755     int addr;
 94756     sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
 94757     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
 94758     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
 94759     VdbeComment((v, "skip OFFSET records"));
 94760     sqlite3VdbeJumpHere(v, addr);
 94764 /*
 94765 ** Add code that will check to make sure the N registers starting at iMem
 94766 ** form a distinct entry.  iTab is a sorting index that holds previously
 94767 ** seen combinations of the N values.  A new entry is made in iTab
 94768 ** if the current N values are new.
 94769 **
 94770 ** A jump to addrRepeat is made and the N+1 values are popped from the
 94771 ** stack if the top N elements are not distinct.
 94772 */
 94773 static void codeDistinct(
 94774   Parse *pParse,     /* Parsing and code generating context */
 94775   int iTab,          /* A sorting index used to test for distinctness */
 94776   int addrRepeat,    /* Jump to here if not distinct */
 94777   int N,             /* Number of elements */
 94778   int iMem           /* First element */
 94779 ){
 94780   Vdbe *v;
 94781   int r1;
 94783   v = pParse->pVdbe;
 94784   r1 = sqlite3GetTempReg(pParse);
 94785   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
 94786   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
 94787   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
 94788   sqlite3ReleaseTempReg(pParse, r1);
 94791 #ifndef SQLITE_OMIT_SUBQUERY
 94792 /*
 94793 ** Generate an error message when a SELECT is used within a subexpression
 94794 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
 94795 ** column.  We do this in a subroutine because the error used to occur
 94796 ** in multiple places.  (The error only occurs in one place now, but we
 94797 ** retain the subroutine to minimize code disruption.)
 94798 */
 94799 static int checkForMultiColumnSelectError(
 94800   Parse *pParse,       /* Parse context. */
 94801   SelectDest *pDest,   /* Destination of SELECT results */
 94802   int nExpr            /* Number of result columns returned by SELECT */
 94803 ){
 94804   int eDest = pDest->eDest;
 94805   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
 94806     sqlite3ErrorMsg(pParse, "only a single result allowed for "
 94807        "a SELECT that is part of an expression");
 94808     return 1;
 94809   }else{
 94810     return 0;
 94813 #endif
 94815 /*
 94816 ** An instance of the following object is used to record information about
 94817 ** how to process the DISTINCT keyword, to simplify passing that information
 94818 ** into the selectInnerLoop() routine.
 94819 */
 94820 typedef struct DistinctCtx DistinctCtx;
 94821 struct DistinctCtx {
 94822   u8 isTnct;      /* True if the DISTINCT keyword is present */
 94823   u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
 94824   int tabTnct;    /* Ephemeral table used for DISTINCT processing */
 94825   int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
 94826 };
 94828 /*
 94829 ** This routine generates the code for the inside of the inner loop
 94830 ** of a SELECT.
 94831 **
 94832 ** If srcTab and nColumn are both zero, then the pEList expressions
 94833 ** are evaluated in order to get the data for this row.  If nColumn>0
 94834 ** then data is pulled from srcTab and pEList is used only to get the
 94835 ** datatypes for each column.
 94836 */
 94837 static void selectInnerLoop(
 94838   Parse *pParse,          /* The parser context */
 94839   Select *p,              /* The complete select statement being coded */
 94840   ExprList *pEList,       /* List of values being extracted */
 94841   int srcTab,             /* Pull data from this table */
 94842   int nColumn,            /* Number of columns in the source table */
 94843   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
 94844   DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
 94845   SelectDest *pDest,      /* How to dispose of the results */
 94846   int iContinue,          /* Jump here to continue with next row */
 94847   int iBreak              /* Jump here to break out of the inner loop */
 94848 ){
 94849   Vdbe *v = pParse->pVdbe;
 94850   int i;
 94851   int hasDistinct;        /* True if the DISTINCT keyword is present */
 94852   int regResult;              /* Start of memory holding result set */
 94853   int eDest = pDest->eDest;   /* How to dispose of results */
 94854   int iParm = pDest->iSDParm; /* First argument to disposal method */
 94855   int nResultCol;             /* Number of result columns */
 94857   assert( v );
 94858   if( NEVER(v==0) ) return;
 94859   assert( pEList!=0 );
 94860   hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
 94861   if( pOrderBy==0 && !hasDistinct ){
 94862     codeOffset(v, p, iContinue);
 94865   /* Pull the requested columns.
 94866   */
 94867   if( nColumn>0 ){
 94868     nResultCol = nColumn;
 94869   }else{
 94870     nResultCol = pEList->nExpr;
 94872   if( pDest->iSdst==0 ){
 94873     pDest->iSdst = pParse->nMem+1;
 94874     pDest->nSdst = nResultCol;
 94875     pParse->nMem += nResultCol;
 94876   }else{ 
 94877     assert( pDest->nSdst==nResultCol );
 94879   regResult = pDest->iSdst;
 94880   if( nColumn>0 ){
 94881     for(i=0; i<nColumn; i++){
 94882       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
 94884   }else if( eDest!=SRT_Exists ){
 94885     /* If the destination is an EXISTS(...) expression, the actual
 94886     ** values returned by the SELECT are not required.
 94887     */
 94888     sqlite3ExprCacheClear(pParse);
 94889     sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
 94891   nColumn = nResultCol;
 94893   /* If the DISTINCT keyword was present on the SELECT statement
 94894   ** and this row has been seen before, then do not make this row
 94895   ** part of the result.
 94896   */
 94897   if( hasDistinct ){
 94898     assert( pEList!=0 );
 94899     assert( pEList->nExpr==nColumn );
 94900     switch( pDistinct->eTnctType ){
 94901       case WHERE_DISTINCT_ORDERED: {
 94902         VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
 94903         int iJump;              /* Jump destination */
 94904         int regPrev;            /* Previous row content */
 94906         /* Allocate space for the previous row */
 94907         regPrev = pParse->nMem+1;
 94908         pParse->nMem += nColumn;
 94910         /* Change the OP_OpenEphemeral coded earlier to an OP_Null
 94911         ** sets the MEM_Cleared bit on the first register of the
 94912         ** previous value.  This will cause the OP_Ne below to always
 94913         ** fail on the first iteration of the loop even if the first
 94914         ** row is all NULLs.
 94915         */
 94916         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 94917         pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
 94918         pOp->opcode = OP_Null;
 94919         pOp->p1 = 1;
 94920         pOp->p2 = regPrev;
 94922         iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
 94923         for(i=0; i<nColumn; i++){
 94924           CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
 94925           if( i<nColumn-1 ){
 94926             sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
 94927           }else{
 94928             sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
 94930           sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
 94931           sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 94933         assert( sqlite3VdbeCurrentAddr(v)==iJump );
 94934         sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
 94935         break;
 94938       case WHERE_DISTINCT_UNIQUE: {
 94939         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 94940         break;
 94943       default: {
 94944         assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
 94945         codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
 94946         break;
 94949     if( pOrderBy==0 ){
 94950       codeOffset(v, p, iContinue);
 94954   switch( eDest ){
 94955     /* In this mode, write each query result to the key of the temporary
 94956     ** table iParm.
 94957     */
 94958 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 94959     case SRT_Union: {
 94960       int r1;
 94961       r1 = sqlite3GetTempReg(pParse);
 94962       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
 94963       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 94964       sqlite3ReleaseTempReg(pParse, r1);
 94965       break;
 94968     /* Construct a record from the query result, but instead of
 94969     ** saving that record, use it as a key to delete elements from
 94970     ** the temporary table iParm.
 94971     */
 94972     case SRT_Except: {
 94973       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
 94974       break;
 94976 #endif
 94978     /* Store the result as data using a unique key.
 94979     */
 94980     case SRT_Table:
 94981     case SRT_EphemTab: {
 94982       int r1 = sqlite3GetTempReg(pParse);
 94983       testcase( eDest==SRT_Table );
 94984       testcase( eDest==SRT_EphemTab );
 94985       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
 94986       if( pOrderBy ){
 94987         pushOntoSorter(pParse, pOrderBy, p, r1);
 94988       }else{
 94989         int r2 = sqlite3GetTempReg(pParse);
 94990         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
 94991         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
 94992         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 94993         sqlite3ReleaseTempReg(pParse, r2);
 94995       sqlite3ReleaseTempReg(pParse, r1);
 94996       break;
 94999 #ifndef SQLITE_OMIT_SUBQUERY
 95000     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 95001     ** then there should be a single item on the stack.  Write this
 95002     ** item into the set table with bogus data.
 95003     */
 95004     case SRT_Set: {
 95005       assert( nColumn==1 );
 95006       pDest->affSdst =
 95007                   sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
 95008       if( pOrderBy ){
 95009         /* At first glance you would think we could optimize out the
 95010         ** ORDER BY in this case since the order of entries in the set
 95011         ** does not matter.  But there might be a LIMIT clause, in which
 95012         ** case the order does matter */
 95013         pushOntoSorter(pParse, pOrderBy, p, regResult);
 95014       }else{
 95015         int r1 = sqlite3GetTempReg(pParse);
 95016         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
 95017         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
 95018         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 95019         sqlite3ReleaseTempReg(pParse, r1);
 95021       break;
 95024     /* If any row exist in the result set, record that fact and abort.
 95025     */
 95026     case SRT_Exists: {
 95027       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
 95028       /* The LIMIT clause will terminate the loop for us */
 95029       break;
 95032     /* If this is a scalar select that is part of an expression, then
 95033     ** store the results in the appropriate memory cell and break out
 95034     ** of the scan loop.
 95035     */
 95036     case SRT_Mem: {
 95037       assert( nColumn==1 );
 95038       if( pOrderBy ){
 95039         pushOntoSorter(pParse, pOrderBy, p, regResult);
 95040       }else{
 95041         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
 95042         /* The LIMIT clause will jump out of the loop for us */
 95044       break;
 95046 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 95048     /* Send the data to the callback function or to a subroutine.  In the
 95049     ** case of a subroutine, the subroutine itself is responsible for
 95050     ** popping the data from the stack.
 95051     */
 95052     case SRT_Coroutine:
 95053     case SRT_Output: {
 95054       testcase( eDest==SRT_Coroutine );
 95055       testcase( eDest==SRT_Output );
 95056       if( pOrderBy ){
 95057         int r1 = sqlite3GetTempReg(pParse);
 95058         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
 95059         pushOntoSorter(pParse, pOrderBy, p, r1);
 95060         sqlite3ReleaseTempReg(pParse, r1);
 95061       }else if( eDest==SRT_Coroutine ){
 95062         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 95063       }else{
 95064         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
 95065         sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
 95067       break;
 95070 #if !defined(SQLITE_OMIT_TRIGGER)
 95071     /* Discard the results.  This is used for SELECT statements inside
 95072     ** the body of a TRIGGER.  The purpose of such selects is to call
 95073     ** user-defined functions that have side effects.  We do not care
 95074     ** about the actual results of the select.
 95075     */
 95076     default: {
 95077       assert( eDest==SRT_Discard );
 95078       break;
 95080 #endif
 95083   /* Jump to the end of the loop if the LIMIT is reached.  Except, if
 95084   ** there is a sorter, in which case the sorter has already limited
 95085   ** the output for us.
 95086   */
 95087   if( pOrderBy==0 && p->iLimit ){
 95088     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
 95092 /*
 95093 ** Given an expression list, generate a KeyInfo structure that records
 95094 ** the collating sequence for each expression in that expression list.
 95095 **
 95096 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
 95097 ** KeyInfo structure is appropriate for initializing a virtual index to
 95098 ** implement that clause.  If the ExprList is the result set of a SELECT
 95099 ** then the KeyInfo structure is appropriate for initializing a virtual
 95100 ** index to implement a DISTINCT test.
 95101 **
 95102 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
 95103 ** function is responsible for seeing that this structure is eventually
 95104 ** freed.  Add the KeyInfo structure to the P4 field of an opcode using
 95105 ** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
 95106 */
 95107 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
 95108   sqlite3 *db = pParse->db;
 95109   int nExpr;
 95110   KeyInfo *pInfo;
 95111   struct ExprList_item *pItem;
 95112   int i;
 95114   nExpr = pList->nExpr;
 95115   pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
 95116   if( pInfo ){
 95117     pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
 95118     pInfo->nField = (u16)nExpr;
 95119     pInfo->enc = ENC(db);
 95120     pInfo->db = db;
 95121     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
 95122       CollSeq *pColl;
 95123       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
 95124       if( !pColl ){
 95125         pColl = db->pDfltColl;
 95127       pInfo->aColl[i] = pColl;
 95128       pInfo->aSortOrder[i] = pItem->sortOrder;
 95131   return pInfo;
 95134 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 95135 /*
 95136 ** Name of the connection operator, used for error messages.
 95137 */
 95138 static const char *selectOpName(int id){
 95139   char *z;
 95140   switch( id ){
 95141     case TK_ALL:       z = "UNION ALL";   break;
 95142     case TK_INTERSECT: z = "INTERSECT";   break;
 95143     case TK_EXCEPT:    z = "EXCEPT";      break;
 95144     default:           z = "UNION";       break;
 95146   return z;
 95148 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 95150 #ifndef SQLITE_OMIT_EXPLAIN
 95151 /*
 95152 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
 95153 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
 95154 ** where the caption is of the form:
 95155 **
 95156 **   "USE TEMP B-TREE FOR xxx"
 95157 **
 95158 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
 95159 ** is determined by the zUsage argument.
 95160 */
 95161 static void explainTempTable(Parse *pParse, const char *zUsage){
 95162   if( pParse->explain==2 ){
 95163     Vdbe *v = pParse->pVdbe;
 95164     char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
 95165     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 95169 /*
 95170 ** Assign expression b to lvalue a. A second, no-op, version of this macro
 95171 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
 95172 ** in sqlite3Select() to assign values to structure member variables that
 95173 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
 95174 ** code with #ifndef directives.
 95175 */
 95176 # define explainSetInteger(a, b) a = b
 95178 #else
 95179 /* No-op versions of the explainXXX() functions and macros. */
 95180 # define explainTempTable(y,z)
 95181 # define explainSetInteger(y,z)
 95182 #endif
 95184 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
 95185 /*
 95186 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
 95187 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
 95188 ** where the caption is of one of the two forms:
 95189 **
 95190 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
 95191 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
 95192 **
 95193 ** where iSub1 and iSub2 are the integers passed as the corresponding
 95194 ** function parameters, and op is the text representation of the parameter
 95195 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
 95196 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
 95197 ** false, or the second form if it is true.
 95198 */
 95199 static void explainComposite(
 95200   Parse *pParse,                  /* Parse context */
 95201   int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
 95202   int iSub1,                      /* Subquery id 1 */
 95203   int iSub2,                      /* Subquery id 2 */
 95204   int bUseTmp                     /* True if a temp table was used */
 95205 ){
 95206   assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
 95207   if( pParse->explain==2 ){
 95208     Vdbe *v = pParse->pVdbe;
 95209     char *zMsg = sqlite3MPrintf(
 95210         pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
 95211         bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
 95212     );
 95213     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 95216 #else
 95217 /* No-op versions of the explainXXX() functions and macros. */
 95218 # define explainComposite(v,w,x,y,z)
 95219 #endif
 95221 /*
 95222 ** If the inner loop was generated using a non-null pOrderBy argument,
 95223 ** then the results were placed in a sorter.  After the loop is terminated
 95224 ** we need to run the sorter and output the results.  The following
 95225 ** routine generates the code needed to do that.
 95226 */
 95227 static void generateSortTail(
 95228   Parse *pParse,    /* Parsing context */
 95229   Select *p,        /* The SELECT statement */
 95230   Vdbe *v,          /* Generate code into this VDBE */
 95231   int nColumn,      /* Number of columns of data */
 95232   SelectDest *pDest /* Write the sorted results here */
 95233 ){
 95234   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
 95235   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
 95236   int addr;
 95237   int iTab;
 95238   int pseudoTab = 0;
 95239   ExprList *pOrderBy = p->pOrderBy;
 95241   int eDest = pDest->eDest;
 95242   int iParm = pDest->iSDParm;
 95244   int regRow;
 95245   int regRowid;
 95247   iTab = pOrderBy->iECursor;
 95248   regRow = sqlite3GetTempReg(pParse);
 95249   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 95250     pseudoTab = pParse->nTab++;
 95251     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
 95252     regRowid = 0;
 95253   }else{
 95254     regRowid = sqlite3GetTempReg(pParse);
 95256   if( p->selFlags & SF_UseSorter ){
 95257     int regSortOut = ++pParse->nMem;
 95258     int ptab2 = pParse->nTab++;
 95259     sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
 95260     addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
 95261     codeOffset(v, p, addrContinue);
 95262     sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
 95263     sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
 95264     sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 95265   }else{
 95266     addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
 95267     codeOffset(v, p, addrContinue);
 95268     sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
 95270   switch( eDest ){
 95271     case SRT_Table:
 95272     case SRT_EphemTab: {
 95273       testcase( eDest==SRT_Table );
 95274       testcase( eDest==SRT_EphemTab );
 95275       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
 95276       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
 95277       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 95278       break;
 95280 #ifndef SQLITE_OMIT_SUBQUERY
 95281     case SRT_Set: {
 95282       assert( nColumn==1 );
 95283       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
 95284                         &pDest->affSdst, 1);
 95285       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
 95286       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
 95287       break;
 95289     case SRT_Mem: {
 95290       assert( nColumn==1 );
 95291       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
 95292       /* The LIMIT clause will terminate the loop for us */
 95293       break;
 95295 #endif
 95296     default: {
 95297       int i;
 95298       assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
 95299       testcase( eDest==SRT_Output );
 95300       testcase( eDest==SRT_Coroutine );
 95301       for(i=0; i<nColumn; i++){
 95302         assert( regRow!=pDest->iSdst+i );
 95303         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
 95304         if( i==0 ){
 95305           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 95308       if( eDest==SRT_Output ){
 95309         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
 95310         sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
 95311       }else{
 95312         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 95314       break;
 95317   sqlite3ReleaseTempReg(pParse, regRow);
 95318   sqlite3ReleaseTempReg(pParse, regRowid);
 95320   /* The bottom of the loop
 95321   */
 95322   sqlite3VdbeResolveLabel(v, addrContinue);
 95323   if( p->selFlags & SF_UseSorter ){
 95324     sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
 95325   }else{
 95326     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
 95328   sqlite3VdbeResolveLabel(v, addrBreak);
 95329   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 95330     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
 95334 /*
 95335 ** Return a pointer to a string containing the 'declaration type' of the
 95336 ** expression pExpr. The string may be treated as static by the caller.
 95337 **
 95338 ** The declaration type is the exact datatype definition extracted from the
 95339 ** original CREATE TABLE statement if the expression is a column. The
 95340 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
 95341 ** is considered a column can be complex in the presence of subqueries. The
 95342 ** result-set expression in all of the following SELECT statements is 
 95343 ** considered a column by this function.
 95344 **
 95345 **   SELECT col FROM tbl;
 95346 **   SELECT (SELECT col FROM tbl;
 95347 **   SELECT (SELECT col FROM tbl);
 95348 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
 95349 ** 
 95350 ** The declaration type for any expression other than a column is NULL.
 95351 */
 95352 static const char *columnType(
 95353   NameContext *pNC, 
 95354   Expr *pExpr,
 95355   const char **pzOriginDb,
 95356   const char **pzOriginTab,
 95357   const char **pzOriginCol
 95358 ){
 95359   char const *zType = 0;
 95360   char const *zOriginDb = 0;
 95361   char const *zOriginTab = 0;
 95362   char const *zOriginCol = 0;
 95363   int j;
 95364   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
 95366   switch( pExpr->op ){
 95367     case TK_AGG_COLUMN:
 95368     case TK_COLUMN: {
 95369       /* The expression is a column. Locate the table the column is being
 95370       ** extracted from in NameContext.pSrcList. This table may be real
 95371       ** database table or a subquery.
 95372       */
 95373       Table *pTab = 0;            /* Table structure column is extracted from */
 95374       Select *pS = 0;             /* Select the column is extracted from */
 95375       int iCol = pExpr->iColumn;  /* Index of column in pTab */
 95376       testcase( pExpr->op==TK_AGG_COLUMN );
 95377       testcase( pExpr->op==TK_COLUMN );
 95378       while( pNC && !pTab ){
 95379         SrcList *pTabList = pNC->pSrcList;
 95380         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
 95381         if( j<pTabList->nSrc ){
 95382           pTab = pTabList->a[j].pTab;
 95383           pS = pTabList->a[j].pSelect;
 95384         }else{
 95385           pNC = pNC->pNext;
 95389       if( pTab==0 ){
 95390         /* At one time, code such as "SELECT new.x" within a trigger would
 95391         ** cause this condition to run.  Since then, we have restructured how
 95392         ** trigger code is generated and so this condition is no longer 
 95393         ** possible. However, it can still be true for statements like
 95394         ** the following:
 95395         **
 95396         **   CREATE TABLE t1(col INTEGER);
 95397         **   SELECT (SELECT t1.col) FROM FROM t1;
 95398         **
 95399         ** when columnType() is called on the expression "t1.col" in the 
 95400         ** sub-select. In this case, set the column type to NULL, even
 95401         ** though it should really be "INTEGER".
 95402         **
 95403         ** This is not a problem, as the column type of "t1.col" is never
 95404         ** used. When columnType() is called on the expression 
 95405         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
 95406         ** branch below.  */
 95407         break;
 95410       assert( pTab && pExpr->pTab==pTab );
 95411       if( pS ){
 95412         /* The "table" is actually a sub-select or a view in the FROM clause
 95413         ** of the SELECT statement. Return the declaration type and origin
 95414         ** data for the result-set column of the sub-select.
 95415         */
 95416         if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
 95417           /* If iCol is less than zero, then the expression requests the
 95418           ** rowid of the sub-select or view. This expression is legal (see 
 95419           ** test case misc2.2.2) - it always evaluates to NULL.
 95420           */
 95421           NameContext sNC;
 95422           Expr *p = pS->pEList->a[iCol].pExpr;
 95423           sNC.pSrcList = pS->pSrc;
 95424           sNC.pNext = pNC;
 95425           sNC.pParse = pNC->pParse;
 95426           zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
 95428       }else if( ALWAYS(pTab->pSchema) ){
 95429         /* A real table */
 95430         assert( !pS );
 95431         if( iCol<0 ) iCol = pTab->iPKey;
 95432         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
 95433         if( iCol<0 ){
 95434           zType = "INTEGER";
 95435           zOriginCol = "rowid";
 95436         }else{
 95437           zType = pTab->aCol[iCol].zType;
 95438           zOriginCol = pTab->aCol[iCol].zName;
 95440         zOriginTab = pTab->zName;
 95441         if( pNC->pParse ){
 95442           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
 95443           zOriginDb = pNC->pParse->db->aDb[iDb].zName;
 95446       break;
 95448 #ifndef SQLITE_OMIT_SUBQUERY
 95449     case TK_SELECT: {
 95450       /* The expression is a sub-select. Return the declaration type and
 95451       ** origin info for the single column in the result set of the SELECT
 95452       ** statement.
 95453       */
 95454       NameContext sNC;
 95455       Select *pS = pExpr->x.pSelect;
 95456       Expr *p = pS->pEList->a[0].pExpr;
 95457       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 95458       sNC.pSrcList = pS->pSrc;
 95459       sNC.pNext = pNC;
 95460       sNC.pParse = pNC->pParse;
 95461       zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
 95462       break;
 95464 #endif
 95467   if( pzOriginDb ){
 95468     assert( pzOriginTab && pzOriginCol );
 95469     *pzOriginDb = zOriginDb;
 95470     *pzOriginTab = zOriginTab;
 95471     *pzOriginCol = zOriginCol;
 95473   return zType;
 95476 /*
 95477 ** Generate code that will tell the VDBE the declaration types of columns
 95478 ** in the result set.
 95479 */
 95480 static void generateColumnTypes(
 95481   Parse *pParse,      /* Parser context */
 95482   SrcList *pTabList,  /* List of tables */
 95483   ExprList *pEList    /* Expressions defining the result set */
 95484 ){
 95485 #ifndef SQLITE_OMIT_DECLTYPE
 95486   Vdbe *v = pParse->pVdbe;
 95487   int i;
 95488   NameContext sNC;
 95489   sNC.pSrcList = pTabList;
 95490   sNC.pParse = pParse;
 95491   for(i=0; i<pEList->nExpr; i++){
 95492     Expr *p = pEList->a[i].pExpr;
 95493     const char *zType;
 95494 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 95495     const char *zOrigDb = 0;
 95496     const char *zOrigTab = 0;
 95497     const char *zOrigCol = 0;
 95498     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
 95500     /* The vdbe must make its own copy of the column-type and other 
 95501     ** column specific strings, in case the schema is reset before this
 95502     ** virtual machine is deleted.
 95503     */
 95504     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
 95505     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
 95506     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
 95507 #else
 95508     zType = columnType(&sNC, p, 0, 0, 0);
 95509 #endif
 95510     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
 95512 #endif /* SQLITE_OMIT_DECLTYPE */
 95515 /*
 95516 ** Generate code that will tell the VDBE the names of columns
 95517 ** in the result set.  This information is used to provide the
 95518 ** azCol[] values in the callback.
 95519 */
 95520 static void generateColumnNames(
 95521   Parse *pParse,      /* Parser context */
 95522   SrcList *pTabList,  /* List of tables */
 95523   ExprList *pEList    /* Expressions defining the result set */
 95524 ){
 95525   Vdbe *v = pParse->pVdbe;
 95526   int i, j;
 95527   sqlite3 *db = pParse->db;
 95528   int fullNames, shortNames;
 95530 #ifndef SQLITE_OMIT_EXPLAIN
 95531   /* If this is an EXPLAIN, skip this step */
 95532   if( pParse->explain ){
 95533     return;
 95535 #endif
 95537   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
 95538   pParse->colNamesSet = 1;
 95539   fullNames = (db->flags & SQLITE_FullColNames)!=0;
 95540   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
 95541   sqlite3VdbeSetNumCols(v, pEList->nExpr);
 95542   for(i=0; i<pEList->nExpr; i++){
 95543     Expr *p;
 95544     p = pEList->a[i].pExpr;
 95545     if( NEVER(p==0) ) continue;
 95546     if( pEList->a[i].zName ){
 95547       char *zName = pEList->a[i].zName;
 95548       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
 95549     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
 95550       Table *pTab;
 95551       char *zCol;
 95552       int iCol = p->iColumn;
 95553       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
 95554         if( pTabList->a[j].iCursor==p->iTable ) break;
 95556       assert( j<pTabList->nSrc );
 95557       pTab = pTabList->a[j].pTab;
 95558       if( iCol<0 ) iCol = pTab->iPKey;
 95559       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
 95560       if( iCol<0 ){
 95561         zCol = "rowid";
 95562       }else{
 95563         zCol = pTab->aCol[iCol].zName;
 95565       if( !shortNames && !fullNames ){
 95566         sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
 95567             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
 95568       }else if( fullNames ){
 95569         char *zName = 0;
 95570         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
 95571         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
 95572       }else{
 95573         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
 95575     }else{
 95576       sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
 95577           sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
 95580   generateColumnTypes(pParse, pTabList, pEList);
 95583 /*
 95584 ** Given a an expression list (which is really the list of expressions
 95585 ** that form the result set of a SELECT statement) compute appropriate
 95586 ** column names for a table that would hold the expression list.
 95587 **
 95588 ** All column names will be unique.
 95589 **
 95590 ** Only the column names are computed.  Column.zType, Column.zColl,
 95591 ** and other fields of Column are zeroed.
 95592 **
 95593 ** Return SQLITE_OK on success.  If a memory allocation error occurs,
 95594 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
 95595 */
 95596 static int selectColumnsFromExprList(
 95597   Parse *pParse,          /* Parsing context */
 95598   ExprList *pEList,       /* Expr list from which to derive column names */
 95599   i16 *pnCol,             /* Write the number of columns here */
 95600   Column **paCol          /* Write the new column list here */
 95601 ){
 95602   sqlite3 *db = pParse->db;   /* Database connection */
 95603   int i, j;                   /* Loop counters */
 95604   int cnt;                    /* Index added to make the name unique */
 95605   Column *aCol, *pCol;        /* For looping over result columns */
 95606   int nCol;                   /* Number of columns in the result set */
 95607   Expr *p;                    /* Expression for a single result column */
 95608   char *zName;                /* Column name */
 95609   int nName;                  /* Size of name in zName[] */
 95611   if( pEList ){
 95612     nCol = pEList->nExpr;
 95613     aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
 95614     testcase( aCol==0 );
 95615   }else{
 95616     nCol = 0;
 95617     aCol = 0;
 95619   *pnCol = nCol;
 95620   *paCol = aCol;
 95622   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 95623     /* Get an appropriate name for the column
 95624     */
 95625     p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
 95626     assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
 95627                || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
 95628     if( (zName = pEList->a[i].zName)!=0 ){
 95629       /* If the column contains an "AS <name>" phrase, use <name> as the name */
 95630       zName = sqlite3DbStrDup(db, zName);
 95631     }else{
 95632       Expr *pColExpr = p;  /* The expression that is the result column name */
 95633       Table *pTab;         /* Table associated with this expression */
 95634       while( pColExpr->op==TK_DOT ){
 95635         pColExpr = pColExpr->pRight;
 95636         assert( pColExpr!=0 );
 95638       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
 95639         /* For columns use the column name name */
 95640         int iCol = pColExpr->iColumn;
 95641         pTab = pColExpr->pTab;
 95642         if( iCol<0 ) iCol = pTab->iPKey;
 95643         zName = sqlite3MPrintf(db, "%s",
 95644                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
 95645       }else if( pColExpr->op==TK_ID ){
 95646         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
 95647         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
 95648       }else{
 95649         /* Use the original text of the column expression as its name */
 95650         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
 95653     if( db->mallocFailed ){
 95654       sqlite3DbFree(db, zName);
 95655       break;
 95658     /* Make sure the column name is unique.  If the name is not unique,
 95659     ** append a integer to the name so that it becomes unique.
 95660     */
 95661     nName = sqlite3Strlen30(zName);
 95662     for(j=cnt=0; j<i; j++){
 95663       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
 95664         char *zNewName;
 95665         zName[nName] = 0;
 95666         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
 95667         sqlite3DbFree(db, zName);
 95668         zName = zNewName;
 95669         j = -1;
 95670         if( zName==0 ) break;
 95673     pCol->zName = zName;
 95675   if( db->mallocFailed ){
 95676     for(j=0; j<i; j++){
 95677       sqlite3DbFree(db, aCol[j].zName);
 95679     sqlite3DbFree(db, aCol);
 95680     *paCol = 0;
 95681     *pnCol = 0;
 95682     return SQLITE_NOMEM;
 95684   return SQLITE_OK;
 95687 /*
 95688 ** Add type and collation information to a column list based on
 95689 ** a SELECT statement.
 95690 ** 
 95691 ** The column list presumably came from selectColumnNamesFromExprList().
 95692 ** The column list has only names, not types or collations.  This
 95693 ** routine goes through and adds the types and collations.
 95694 **
 95695 ** This routine requires that all identifiers in the SELECT
 95696 ** statement be resolved.
 95697 */
 95698 static void selectAddColumnTypeAndCollation(
 95699   Parse *pParse,        /* Parsing contexts */
 95700   int nCol,             /* Number of columns */
 95701   Column *aCol,         /* List of columns */
 95702   Select *pSelect       /* SELECT used to determine types and collations */
 95703 ){
 95704   sqlite3 *db = pParse->db;
 95705   NameContext sNC;
 95706   Column *pCol;
 95707   CollSeq *pColl;
 95708   int i;
 95709   Expr *p;
 95710   struct ExprList_item *a;
 95712   assert( pSelect!=0 );
 95713   assert( (pSelect->selFlags & SF_Resolved)!=0 );
 95714   assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
 95715   if( db->mallocFailed ) return;
 95716   memset(&sNC, 0, sizeof(sNC));
 95717   sNC.pSrcList = pSelect->pSrc;
 95718   a = pSelect->pEList->a;
 95719   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 95720     p = a[i].pExpr;
 95721     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
 95722     pCol->affinity = sqlite3ExprAffinity(p);
 95723     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
 95724     pColl = sqlite3ExprCollSeq(pParse, p);
 95725     if( pColl ){
 95726       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
 95731 /*
 95732 ** Given a SELECT statement, generate a Table structure that describes
 95733 ** the result set of that SELECT.
 95734 */
 95735 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
 95736   Table *pTab;
 95737   sqlite3 *db = pParse->db;
 95738   int savedFlags;
 95740   savedFlags = db->flags;
 95741   db->flags &= ~SQLITE_FullColNames;
 95742   db->flags |= SQLITE_ShortColNames;
 95743   sqlite3SelectPrep(pParse, pSelect, 0);
 95744   if( pParse->nErr ) return 0;
 95745   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
 95746   db->flags = savedFlags;
 95747   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
 95748   if( pTab==0 ){
 95749     return 0;
 95751   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
 95752   ** is disabled */
 95753   assert( db->lookaside.bEnabled==0 );
 95754   pTab->nRef = 1;
 95755   pTab->zName = 0;
 95756   pTab->nRowEst = 1000000;
 95757   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
 95758   selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
 95759   pTab->iPKey = -1;
 95760   if( db->mallocFailed ){
 95761     sqlite3DeleteTable(db, pTab);
 95762     return 0;
 95764   return pTab;
 95767 /*
 95768 ** Get a VDBE for the given parser context.  Create a new one if necessary.
 95769 ** If an error occurs, return NULL and leave a message in pParse.
 95770 */
 95771 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
 95772   Vdbe *v = pParse->pVdbe;
 95773   if( v==0 ){
 95774     v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
 95775 #ifndef SQLITE_OMIT_TRACE
 95776     if( v ){
 95777       sqlite3VdbeAddOp0(v, OP_Trace);
 95779 #endif
 95781   return v;
 95785 /*
 95786 ** Compute the iLimit and iOffset fields of the SELECT based on the
 95787 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
 95788 ** that appear in the original SQL statement after the LIMIT and OFFSET
 95789 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
 95790 ** are the integer memory register numbers for counters used to compute 
 95791 ** the limit and offset.  If there is no limit and/or offset, then 
 95792 ** iLimit and iOffset are negative.
 95793 **
 95794 ** This routine changes the values of iLimit and iOffset only if
 95795 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
 95796 ** iOffset should have been preset to appropriate default values
 95797 ** (usually but not always -1) prior to calling this routine.
 95798 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
 95799 ** redefined.  The UNION ALL operator uses this property to force
 95800 ** the reuse of the same limit and offset registers across multiple
 95801 ** SELECT statements.
 95802 */
 95803 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
 95804   Vdbe *v = 0;
 95805   int iLimit = 0;
 95806   int iOffset;
 95807   int addr1, n;
 95808   if( p->iLimit ) return;
 95810   /* 
 95811   ** "LIMIT -1" always shows all rows.  There is some
 95812   ** contraversy about what the correct behavior should be.
 95813   ** The current implementation interprets "LIMIT 0" to mean
 95814   ** no rows.
 95815   */
 95816   sqlite3ExprCacheClear(pParse);
 95817   assert( p->pOffset==0 || p->pLimit!=0 );
 95818   if( p->pLimit ){
 95819     p->iLimit = iLimit = ++pParse->nMem;
 95820     v = sqlite3GetVdbe(pParse);
 95821     if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
 95822     if( sqlite3ExprIsInteger(p->pLimit, &n) ){
 95823       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
 95824       VdbeComment((v, "LIMIT counter"));
 95825       if( n==0 ){
 95826         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
 95827       }else{
 95828         if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
 95830     }else{
 95831       sqlite3ExprCode(pParse, p->pLimit, iLimit);
 95832       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
 95833       VdbeComment((v, "LIMIT counter"));
 95834       sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
 95836     if( p->pOffset ){
 95837       p->iOffset = iOffset = ++pParse->nMem;
 95838       pParse->nMem++;   /* Allocate an extra register for limit+offset */
 95839       sqlite3ExprCode(pParse, p->pOffset, iOffset);
 95840       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
 95841       VdbeComment((v, "OFFSET counter"));
 95842       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
 95843       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
 95844       sqlite3VdbeJumpHere(v, addr1);
 95845       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
 95846       VdbeComment((v, "LIMIT+OFFSET"));
 95847       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
 95848       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
 95849       sqlite3VdbeJumpHere(v, addr1);
 95854 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 95855 /*
 95856 ** Return the appropriate collating sequence for the iCol-th column of
 95857 ** the result set for the compound-select statement "p".  Return NULL if
 95858 ** the column has no default collating sequence.
 95859 **
 95860 ** The collating sequence for the compound select is taken from the
 95861 ** left-most term of the select that has a collating sequence.
 95862 */
 95863 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
 95864   CollSeq *pRet;
 95865   if( p->pPrior ){
 95866     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
 95867   }else{
 95868     pRet = 0;
 95870   assert( iCol>=0 );
 95871   if( pRet==0 && iCol<p->pEList->nExpr ){
 95872     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
 95874   return pRet;
 95876 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 95878 /* Forward reference */
 95879 static int multiSelectOrderBy(
 95880   Parse *pParse,        /* Parsing context */
 95881   Select *p,            /* The right-most of SELECTs to be coded */
 95882   SelectDest *pDest     /* What to do with query results */
 95883 );
 95886 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 95887 /*
 95888 ** This routine is called to process a compound query form from
 95889 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
 95890 ** INTERSECT
 95891 **
 95892 ** "p" points to the right-most of the two queries.  the query on the
 95893 ** left is p->pPrior.  The left query could also be a compound query
 95894 ** in which case this routine will be called recursively. 
 95895 **
 95896 ** The results of the total query are to be written into a destination
 95897 ** of type eDest with parameter iParm.
 95898 **
 95899 ** Example 1:  Consider a three-way compound SQL statement.
 95900 **
 95901 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
 95902 **
 95903 ** This statement is parsed up as follows:
 95904 **
 95905 **     SELECT c FROM t3
 95906 **      |
 95907 **      `----->  SELECT b FROM t2
 95908 **                |
 95909 **                `------>  SELECT a FROM t1
 95910 **
 95911 ** The arrows in the diagram above represent the Select.pPrior pointer.
 95912 ** So if this routine is called with p equal to the t3 query, then
 95913 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
 95914 **
 95915 ** Notice that because of the way SQLite parses compound SELECTs, the
 95916 ** individual selects always group from left to right.
 95917 */
 95918 static int multiSelect(
 95919   Parse *pParse,        /* Parsing context */
 95920   Select *p,            /* The right-most of SELECTs to be coded */
 95921   SelectDest *pDest     /* What to do with query results */
 95922 ){
 95923   int rc = SQLITE_OK;   /* Success code from a subroutine */
 95924   Select *pPrior;       /* Another SELECT immediately to our left */
 95925   Vdbe *v;              /* Generate code to this VDBE */
 95926   SelectDest dest;      /* Alternative data destination */
 95927   Select *pDelete = 0;  /* Chain of simple selects to delete */
 95928   sqlite3 *db;          /* Database connection */
 95929 #ifndef SQLITE_OMIT_EXPLAIN
 95930   int iSub1;            /* EQP id of left-hand query */
 95931   int iSub2;            /* EQP id of right-hand query */
 95932 #endif
 95934   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
 95935   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
 95936   */
 95937   assert( p && p->pPrior );  /* Calling function guarantees this much */
 95938   db = pParse->db;
 95939   pPrior = p->pPrior;
 95940   assert( pPrior->pRightmost!=pPrior );
 95941   assert( pPrior->pRightmost==p->pRightmost );
 95942   dest = *pDest;
 95943   if( pPrior->pOrderBy ){
 95944     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
 95945       selectOpName(p->op));
 95946     rc = 1;
 95947     goto multi_select_end;
 95949   if( pPrior->pLimit ){
 95950     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
 95951       selectOpName(p->op));
 95952     rc = 1;
 95953     goto multi_select_end;
 95956   v = sqlite3GetVdbe(pParse);
 95957   assert( v!=0 );  /* The VDBE already created by calling function */
 95959   /* Create the destination temporary table if necessary
 95960   */
 95961   if( dest.eDest==SRT_EphemTab ){
 95962     assert( p->pEList );
 95963     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
 95964     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 95965     dest.eDest = SRT_Table;
 95968   /* Make sure all SELECTs in the statement have the same number of elements
 95969   ** in their result sets.
 95970   */
 95971   assert( p->pEList && pPrior->pEList );
 95972   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
 95973     if( p->selFlags & SF_Values ){
 95974       sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
 95975     }else{
 95976       sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
 95977         " do not have the same number of result columns", selectOpName(p->op));
 95979     rc = 1;
 95980     goto multi_select_end;
 95983   /* Compound SELECTs that have an ORDER BY clause are handled separately.
 95984   */
 95985   if( p->pOrderBy ){
 95986     return multiSelectOrderBy(pParse, p, pDest);
 95989   /* Generate code for the left and right SELECT statements.
 95990   */
 95991   switch( p->op ){
 95992     case TK_ALL: {
 95993       int addr = 0;
 95994       int nLimit;
 95995       assert( !pPrior->pLimit );
 95996       pPrior->pLimit = p->pLimit;
 95997       pPrior->pOffset = p->pOffset;
 95998       explainSetInteger(iSub1, pParse->iNextSelectId);
 95999       rc = sqlite3Select(pParse, pPrior, &dest);
 96000       p->pLimit = 0;
 96001       p->pOffset = 0;
 96002       if( rc ){
 96003         goto multi_select_end;
 96005       p->pPrior = 0;
 96006       p->iLimit = pPrior->iLimit;
 96007       p->iOffset = pPrior->iOffset;
 96008       if( p->iLimit ){
 96009         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
 96010         VdbeComment((v, "Jump ahead if LIMIT reached"));
 96012       explainSetInteger(iSub2, pParse->iNextSelectId);
 96013       rc = sqlite3Select(pParse, p, &dest);
 96014       testcase( rc!=SQLITE_OK );
 96015       pDelete = p->pPrior;
 96016       p->pPrior = pPrior;
 96017       p->nSelectRow += pPrior->nSelectRow;
 96018       if( pPrior->pLimit
 96019        && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
 96020        && p->nSelectRow > (double)nLimit 
 96021       ){
 96022         p->nSelectRow = (double)nLimit;
 96024       if( addr ){
 96025         sqlite3VdbeJumpHere(v, addr);
 96027       break;
 96029     case TK_EXCEPT:
 96030     case TK_UNION: {
 96031       int unionTab;    /* Cursor number of the temporary table holding result */
 96032       u8 op = 0;       /* One of the SRT_ operations to apply to self */
 96033       int priorOp;     /* The SRT_ operation to apply to prior selects */
 96034       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
 96035       int addr;
 96036       SelectDest uniondest;
 96038       testcase( p->op==TK_EXCEPT );
 96039       testcase( p->op==TK_UNION );
 96040       priorOp = SRT_Union;
 96041       if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
 96042         /* We can reuse a temporary table generated by a SELECT to our
 96043         ** right.
 96044         */
 96045         assert( p->pRightmost!=p );  /* Can only happen for leftward elements
 96046                                      ** of a 3-way or more compound */
 96047         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
 96048         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
 96049         unionTab = dest.iSDParm;
 96050       }else{
 96051         /* We will need to create our own temporary table to hold the
 96052         ** intermediate results.
 96053         */
 96054         unionTab = pParse->nTab++;
 96055         assert( p->pOrderBy==0 );
 96056         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
 96057         assert( p->addrOpenEphm[0] == -1 );
 96058         p->addrOpenEphm[0] = addr;
 96059         p->pRightmost->selFlags |= SF_UsesEphemeral;
 96060         assert( p->pEList );
 96063       /* Code the SELECT statements to our left
 96064       */
 96065       assert( !pPrior->pOrderBy );
 96066       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
 96067       explainSetInteger(iSub1, pParse->iNextSelectId);
 96068       rc = sqlite3Select(pParse, pPrior, &uniondest);
 96069       if( rc ){
 96070         goto multi_select_end;
 96073       /* Code the current SELECT statement
 96074       */
 96075       if( p->op==TK_EXCEPT ){
 96076         op = SRT_Except;
 96077       }else{
 96078         assert( p->op==TK_UNION );
 96079         op = SRT_Union;
 96081       p->pPrior = 0;
 96082       pLimit = p->pLimit;
 96083       p->pLimit = 0;
 96084       pOffset = p->pOffset;
 96085       p->pOffset = 0;
 96086       uniondest.eDest = op;
 96087       explainSetInteger(iSub2, pParse->iNextSelectId);
 96088       rc = sqlite3Select(pParse, p, &uniondest);
 96089       testcase( rc!=SQLITE_OK );
 96090       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
 96091       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
 96092       sqlite3ExprListDelete(db, p->pOrderBy);
 96093       pDelete = p->pPrior;
 96094       p->pPrior = pPrior;
 96095       p->pOrderBy = 0;
 96096       if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
 96097       sqlite3ExprDelete(db, p->pLimit);
 96098       p->pLimit = pLimit;
 96099       p->pOffset = pOffset;
 96100       p->iLimit = 0;
 96101       p->iOffset = 0;
 96103       /* Convert the data in the temporary table into whatever form
 96104       ** it is that we currently need.
 96105       */
 96106       assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
 96107       if( dest.eDest!=priorOp ){
 96108         int iCont, iBreak, iStart;
 96109         assert( p->pEList );
 96110         if( dest.eDest==SRT_Output ){
 96111           Select *pFirst = p;
 96112           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 96113           generateColumnNames(pParse, 0, pFirst->pEList);
 96115         iBreak = sqlite3VdbeMakeLabel(v);
 96116         iCont = sqlite3VdbeMakeLabel(v);
 96117         computeLimitRegisters(pParse, p, iBreak);
 96118         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
 96119         iStart = sqlite3VdbeCurrentAddr(v);
 96120         selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
 96121                         0, 0, &dest, iCont, iBreak);
 96122         sqlite3VdbeResolveLabel(v, iCont);
 96123         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
 96124         sqlite3VdbeResolveLabel(v, iBreak);
 96125         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
 96127       break;
 96129     default: assert( p->op==TK_INTERSECT ); {
 96130       int tab1, tab2;
 96131       int iCont, iBreak, iStart;
 96132       Expr *pLimit, *pOffset;
 96133       int addr;
 96134       SelectDest intersectdest;
 96135       int r1;
 96137       /* INTERSECT is different from the others since it requires
 96138       ** two temporary tables.  Hence it has its own case.  Begin
 96139       ** by allocating the tables we will need.
 96140       */
 96141       tab1 = pParse->nTab++;
 96142       tab2 = pParse->nTab++;
 96143       assert( p->pOrderBy==0 );
 96145       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
 96146       assert( p->addrOpenEphm[0] == -1 );
 96147       p->addrOpenEphm[0] = addr;
 96148       p->pRightmost->selFlags |= SF_UsesEphemeral;
 96149       assert( p->pEList );
 96151       /* Code the SELECTs to our left into temporary table "tab1".
 96152       */
 96153       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
 96154       explainSetInteger(iSub1, pParse->iNextSelectId);
 96155       rc = sqlite3Select(pParse, pPrior, &intersectdest);
 96156       if( rc ){
 96157         goto multi_select_end;
 96160       /* Code the current SELECT into temporary table "tab2"
 96161       */
 96162       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
 96163       assert( p->addrOpenEphm[1] == -1 );
 96164       p->addrOpenEphm[1] = addr;
 96165       p->pPrior = 0;
 96166       pLimit = p->pLimit;
 96167       p->pLimit = 0;
 96168       pOffset = p->pOffset;
 96169       p->pOffset = 0;
 96170       intersectdest.iSDParm = tab2;
 96171       explainSetInteger(iSub2, pParse->iNextSelectId);
 96172       rc = sqlite3Select(pParse, p, &intersectdest);
 96173       testcase( rc!=SQLITE_OK );
 96174       pDelete = p->pPrior;
 96175       p->pPrior = pPrior;
 96176       if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
 96177       sqlite3ExprDelete(db, p->pLimit);
 96178       p->pLimit = pLimit;
 96179       p->pOffset = pOffset;
 96181       /* Generate code to take the intersection of the two temporary
 96182       ** tables.
 96183       */
 96184       assert( p->pEList );
 96185       if( dest.eDest==SRT_Output ){
 96186         Select *pFirst = p;
 96187         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 96188         generateColumnNames(pParse, 0, pFirst->pEList);
 96190       iBreak = sqlite3VdbeMakeLabel(v);
 96191       iCont = sqlite3VdbeMakeLabel(v);
 96192       computeLimitRegisters(pParse, p, iBreak);
 96193       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
 96194       r1 = sqlite3GetTempReg(pParse);
 96195       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
 96196       sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
 96197       sqlite3ReleaseTempReg(pParse, r1);
 96198       selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
 96199                       0, 0, &dest, iCont, iBreak);
 96200       sqlite3VdbeResolveLabel(v, iCont);
 96201       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
 96202       sqlite3VdbeResolveLabel(v, iBreak);
 96203       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
 96204       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
 96205       break;
 96209   explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
 96211   /* Compute collating sequences used by 
 96212   ** temporary tables needed to implement the compound select.
 96213   ** Attach the KeyInfo structure to all temporary tables.
 96214   **
 96215   ** This section is run by the right-most SELECT statement only.
 96216   ** SELECT statements to the left always skip this part.  The right-most
 96217   ** SELECT might also skip this part if it has no ORDER BY clause and
 96218   ** no temp tables are required.
 96219   */
 96220   if( p->selFlags & SF_UsesEphemeral ){
 96221     int i;                        /* Loop counter */
 96222     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
 96223     Select *pLoop;                /* For looping through SELECT statements */
 96224     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
 96225     int nCol;                     /* Number of columns in result set */
 96227     assert( p->pRightmost==p );
 96228     nCol = p->pEList->nExpr;
 96229     pKeyInfo = sqlite3DbMallocZero(db,
 96230                        sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
 96231     if( !pKeyInfo ){
 96232       rc = SQLITE_NOMEM;
 96233       goto multi_select_end;
 96236     pKeyInfo->enc = ENC(db);
 96237     pKeyInfo->nField = (u16)nCol;
 96239     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
 96240       *apColl = multiSelectCollSeq(pParse, p, i);
 96241       if( 0==*apColl ){
 96242         *apColl = db->pDfltColl;
 96245     pKeyInfo->aSortOrder = (u8*)apColl;
 96247     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
 96248       for(i=0; i<2; i++){
 96249         int addr = pLoop->addrOpenEphm[i];
 96250         if( addr<0 ){
 96251           /* If [0] is unused then [1] is also unused.  So we can
 96252           ** always safely abort as soon as the first unused slot is found */
 96253           assert( pLoop->addrOpenEphm[1]<0 );
 96254           break;
 96256         sqlite3VdbeChangeP2(v, addr, nCol);
 96257         sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
 96258         pLoop->addrOpenEphm[i] = -1;
 96261     sqlite3DbFree(db, pKeyInfo);
 96264 multi_select_end:
 96265   pDest->iSdst = dest.iSdst;
 96266   pDest->nSdst = dest.nSdst;
 96267   sqlite3SelectDelete(db, pDelete);
 96268   return rc;
 96270 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 96272 /*
 96273 ** Code an output subroutine for a coroutine implementation of a
 96274 ** SELECT statment.
 96275 **
 96276 ** The data to be output is contained in pIn->iSdst.  There are
 96277 ** pIn->nSdst columns to be output.  pDest is where the output should
 96278 ** be sent.
 96279 **
 96280 ** regReturn is the number of the register holding the subroutine
 96281 ** return address.
 96282 **
 96283 ** If regPrev>0 then it is the first register in a vector that
 96284 ** records the previous output.  mem[regPrev] is a flag that is false
 96285 ** if there has been no previous output.  If regPrev>0 then code is
 96286 ** generated to suppress duplicates.  pKeyInfo is used for comparing
 96287 ** keys.
 96288 **
 96289 ** If the LIMIT found in p->iLimit is reached, jump immediately to
 96290 ** iBreak.
 96291 */
 96292 static int generateOutputSubroutine(
 96293   Parse *pParse,          /* Parsing context */
 96294   Select *p,              /* The SELECT statement */
 96295   SelectDest *pIn,        /* Coroutine supplying data */
 96296   SelectDest *pDest,      /* Where to send the data */
 96297   int regReturn,          /* The return address register */
 96298   int regPrev,            /* Previous result register.  No uniqueness if 0 */
 96299   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
 96300   int p4type,             /* The p4 type for pKeyInfo */
 96301   int iBreak              /* Jump here if we hit the LIMIT */
 96302 ){
 96303   Vdbe *v = pParse->pVdbe;
 96304   int iContinue;
 96305   int addr;
 96307   addr = sqlite3VdbeCurrentAddr(v);
 96308   iContinue = sqlite3VdbeMakeLabel(v);
 96310   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
 96311   */
 96312   if( regPrev ){
 96313     int j1, j2;
 96314     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
 96315     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
 96316                               (char*)pKeyInfo, p4type);
 96317     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
 96318     sqlite3VdbeJumpHere(v, j1);
 96319     sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
 96320     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
 96322   if( pParse->db->mallocFailed ) return 0;
 96324   /* Suppress the first OFFSET entries if there is an OFFSET clause
 96325   */
 96326   codeOffset(v, p, iContinue);
 96328   switch( pDest->eDest ){
 96329     /* Store the result as data using a unique key.
 96330     */
 96331     case SRT_Table:
 96332     case SRT_EphemTab: {
 96333       int r1 = sqlite3GetTempReg(pParse);
 96334       int r2 = sqlite3GetTempReg(pParse);
 96335       testcase( pDest->eDest==SRT_Table );
 96336       testcase( pDest->eDest==SRT_EphemTab );
 96337       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
 96338       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
 96339       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
 96340       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 96341       sqlite3ReleaseTempReg(pParse, r2);
 96342       sqlite3ReleaseTempReg(pParse, r1);
 96343       break;
 96346 #ifndef SQLITE_OMIT_SUBQUERY
 96347     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 96348     ** then there should be a single item on the stack.  Write this
 96349     ** item into the set table with bogus data.
 96350     */
 96351     case SRT_Set: {
 96352       int r1;
 96353       assert( pIn->nSdst==1 );
 96354       pDest->affSdst = 
 96355          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
 96356       r1 = sqlite3GetTempReg(pParse);
 96357       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
 96358       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
 96359       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
 96360       sqlite3ReleaseTempReg(pParse, r1);
 96361       break;
 96364 #if 0  /* Never occurs on an ORDER BY query */
 96365     /* If any row exist in the result set, record that fact and abort.
 96366     */
 96367     case SRT_Exists: {
 96368       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
 96369       /* The LIMIT clause will terminate the loop for us */
 96370       break;
 96372 #endif
 96374     /* If this is a scalar select that is part of an expression, then
 96375     ** store the results in the appropriate memory cell and break out
 96376     ** of the scan loop.
 96377     */
 96378     case SRT_Mem: {
 96379       assert( pIn->nSdst==1 );
 96380       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
 96381       /* The LIMIT clause will jump out of the loop for us */
 96382       break;
 96384 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 96386     /* The results are stored in a sequence of registers
 96387     ** starting at pDest->iSdst.  Then the co-routine yields.
 96388     */
 96389     case SRT_Coroutine: {
 96390       if( pDest->iSdst==0 ){
 96391         pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
 96392         pDest->nSdst = pIn->nSdst;
 96394       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
 96395       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 96396       break;
 96399     /* If none of the above, then the result destination must be
 96400     ** SRT_Output.  This routine is never called with any other
 96401     ** destination other than the ones handled above or SRT_Output.
 96402     **
 96403     ** For SRT_Output, results are stored in a sequence of registers.  
 96404     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
 96405     ** return the next row of result.
 96406     */
 96407     default: {
 96408       assert( pDest->eDest==SRT_Output );
 96409       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
 96410       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
 96411       break;
 96415   /* Jump to the end of the loop if the LIMIT is reached.
 96416   */
 96417   if( p->iLimit ){
 96418     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
 96421   /* Generate the subroutine return
 96422   */
 96423   sqlite3VdbeResolveLabel(v, iContinue);
 96424   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
 96426   return addr;
 96429 /*
 96430 ** Alternative compound select code generator for cases when there
 96431 ** is an ORDER BY clause.
 96432 **
 96433 ** We assume a query of the following form:
 96434 **
 96435 **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
 96436 **
 96437 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
 96438 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
 96439 ** co-routines.  Then run the co-routines in parallel and merge the results
 96440 ** into the output.  In addition to the two coroutines (called selectA and
 96441 ** selectB) there are 7 subroutines:
 96442 **
 96443 **    outA:    Move the output of the selectA coroutine into the output
 96444 **             of the compound query.
 96445 **
 96446 **    outB:    Move the output of the selectB coroutine into the output
 96447 **             of the compound query.  (Only generated for UNION and
 96448 **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
 96449 **             appears only in B.)
 96450 **
 96451 **    AltB:    Called when there is data from both coroutines and A<B.
 96452 **
 96453 **    AeqB:    Called when there is data from both coroutines and A==B.
 96454 **
 96455 **    AgtB:    Called when there is data from both coroutines and A>B.
 96456 **
 96457 **    EofA:    Called when data is exhausted from selectA.
 96458 **
 96459 **    EofB:    Called when data is exhausted from selectB.
 96460 **
 96461 ** The implementation of the latter five subroutines depend on which 
 96462 ** <operator> is used:
 96463 **
 96464 **
 96465 **             UNION ALL         UNION            EXCEPT          INTERSECT
 96466 **          -------------  -----------------  --------------  -----------------
 96467 **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
 96468 **
 96469 **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
 96470 **
 96471 **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
 96472 **
 96473 **   EofA:   outB, nextB      outB, nextB          halt             halt
 96474 **
 96475 **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
 96476 **
 96477 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
 96478 ** causes an immediate jump to EofA and an EOF on B following nextB causes
 96479 ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
 96480 ** following nextX causes a jump to the end of the select processing.
 96481 **
 96482 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
 96483 ** within the output subroutine.  The regPrev register set holds the previously
 96484 ** output value.  A comparison is made against this value and the output
 96485 ** is skipped if the next results would be the same as the previous.
 96486 **
 96487 ** The implementation plan is to implement the two coroutines and seven
 96488 ** subroutines first, then put the control logic at the bottom.  Like this:
 96489 **
 96490 **          goto Init
 96491 **     coA: coroutine for left query (A)
 96492 **     coB: coroutine for right query (B)
 96493 **    outA: output one row of A
 96494 **    outB: output one row of B (UNION and UNION ALL only)
 96495 **    EofA: ...
 96496 **    EofB: ...
 96497 **    AltB: ...
 96498 **    AeqB: ...
 96499 **    AgtB: ...
 96500 **    Init: initialize coroutine registers
 96501 **          yield coA
 96502 **          if eof(A) goto EofA
 96503 **          yield coB
 96504 **          if eof(B) goto EofB
 96505 **    Cmpr: Compare A, B
 96506 **          Jump AltB, AeqB, AgtB
 96507 **     End: ...
 96508 **
 96509 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
 96510 ** actually called using Gosub and they do not Return.  EofA and EofB loop
 96511 ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
 96512 ** and AgtB jump to either L2 or to one of EofA or EofB.
 96513 */
 96514 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 96515 static int multiSelectOrderBy(
 96516   Parse *pParse,        /* Parsing context */
 96517   Select *p,            /* The right-most of SELECTs to be coded */
 96518   SelectDest *pDest     /* What to do with query results */
 96519 ){
 96520   int i, j;             /* Loop counters */
 96521   Select *pPrior;       /* Another SELECT immediately to our left */
 96522   Vdbe *v;              /* Generate code to this VDBE */
 96523   SelectDest destA;     /* Destination for coroutine A */
 96524   SelectDest destB;     /* Destination for coroutine B */
 96525   int regAddrA;         /* Address register for select-A coroutine */
 96526   int regEofA;          /* Flag to indicate when select-A is complete */
 96527   int regAddrB;         /* Address register for select-B coroutine */
 96528   int regEofB;          /* Flag to indicate when select-B is complete */
 96529   int addrSelectA;      /* Address of the select-A coroutine */
 96530   int addrSelectB;      /* Address of the select-B coroutine */
 96531   int regOutA;          /* Address register for the output-A subroutine */
 96532   int regOutB;          /* Address register for the output-B subroutine */
 96533   int addrOutA;         /* Address of the output-A subroutine */
 96534   int addrOutB = 0;     /* Address of the output-B subroutine */
 96535   int addrEofA;         /* Address of the select-A-exhausted subroutine */
 96536   int addrEofB;         /* Address of the select-B-exhausted subroutine */
 96537   int addrAltB;         /* Address of the A<B subroutine */
 96538   int addrAeqB;         /* Address of the A==B subroutine */
 96539   int addrAgtB;         /* Address of the A>B subroutine */
 96540   int regLimitA;        /* Limit register for select-A */
 96541   int regLimitB;        /* Limit register for select-A */
 96542   int regPrev;          /* A range of registers to hold previous output */
 96543   int savedLimit;       /* Saved value of p->iLimit */
 96544   int savedOffset;      /* Saved value of p->iOffset */
 96545   int labelCmpr;        /* Label for the start of the merge algorithm */
 96546   int labelEnd;         /* Label for the end of the overall SELECT stmt */
 96547   int j1;               /* Jump instructions that get retargetted */
 96548   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
 96549   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
 96550   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
 96551   sqlite3 *db;          /* Database connection */
 96552   ExprList *pOrderBy;   /* The ORDER BY clause */
 96553   int nOrderBy;         /* Number of terms in the ORDER BY clause */
 96554   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
 96555 #ifndef SQLITE_OMIT_EXPLAIN
 96556   int iSub1;            /* EQP id of left-hand query */
 96557   int iSub2;            /* EQP id of right-hand query */
 96558 #endif
 96560   assert( p->pOrderBy!=0 );
 96561   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
 96562   db = pParse->db;
 96563   v = pParse->pVdbe;
 96564   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
 96565   labelEnd = sqlite3VdbeMakeLabel(v);
 96566   labelCmpr = sqlite3VdbeMakeLabel(v);
 96569   /* Patch up the ORDER BY clause
 96570   */
 96571   op = p->op;  
 96572   pPrior = p->pPrior;
 96573   assert( pPrior->pOrderBy==0 );
 96574   pOrderBy = p->pOrderBy;
 96575   assert( pOrderBy );
 96576   nOrderBy = pOrderBy->nExpr;
 96578   /* For operators other than UNION ALL we have to make sure that
 96579   ** the ORDER BY clause covers every term of the result set.  Add
 96580   ** terms to the ORDER BY clause as necessary.
 96581   */
 96582   if( op!=TK_ALL ){
 96583     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
 96584       struct ExprList_item *pItem;
 96585       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
 96586         assert( pItem->iOrderByCol>0 );
 96587         if( pItem->iOrderByCol==i ) break;
 96589       if( j==nOrderBy ){
 96590         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 96591         if( pNew==0 ) return SQLITE_NOMEM;
 96592         pNew->flags |= EP_IntValue;
 96593         pNew->u.iValue = i;
 96594         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
 96595         if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
 96600   /* Compute the comparison permutation and keyinfo that is used with
 96601   ** the permutation used to determine if the next
 96602   ** row of results comes from selectA or selectB.  Also add explicit
 96603   ** collations to the ORDER BY clause terms so that when the subqueries
 96604   ** to the right and the left are evaluated, they use the correct
 96605   ** collation.
 96606   */
 96607   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
 96608   if( aPermute ){
 96609     struct ExprList_item *pItem;
 96610     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
 96611       assert( pItem->iOrderByCol>0  && pItem->iOrderByCol<=p->pEList->nExpr );
 96612       aPermute[i] = pItem->iOrderByCol - 1;
 96614     pKeyMerge =
 96615       sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
 96616     if( pKeyMerge ){
 96617       pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
 96618       pKeyMerge->nField = (u16)nOrderBy;
 96619       pKeyMerge->enc = ENC(db);
 96620       for(i=0; i<nOrderBy; i++){
 96621         CollSeq *pColl;
 96622         Expr *pTerm = pOrderBy->a[i].pExpr;
 96623         if( pTerm->flags & EP_Collate ){
 96624           pColl = sqlite3ExprCollSeq(pParse, pTerm);
 96625         }else{
 96626           pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
 96627           if( pColl==0 ) pColl = db->pDfltColl;
 96628           pOrderBy->a[i].pExpr =
 96629              sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
 96631         pKeyMerge->aColl[i] = pColl;
 96632         pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
 96635   }else{
 96636     pKeyMerge = 0;
 96639   /* Reattach the ORDER BY clause to the query.
 96640   */
 96641   p->pOrderBy = pOrderBy;
 96642   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
 96644   /* Allocate a range of temporary registers and the KeyInfo needed
 96645   ** for the logic that removes duplicate result rows when the
 96646   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
 96647   */
 96648   if( op==TK_ALL ){
 96649     regPrev = 0;
 96650   }else{
 96651     int nExpr = p->pEList->nExpr;
 96652     assert( nOrderBy>=nExpr || db->mallocFailed );
 96653     regPrev = sqlite3GetTempRange(pParse, nExpr+1);
 96654     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
 96655     pKeyDup = sqlite3DbMallocZero(db,
 96656                   sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
 96657     if( pKeyDup ){
 96658       pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
 96659       pKeyDup->nField = (u16)nExpr;
 96660       pKeyDup->enc = ENC(db);
 96661       for(i=0; i<nExpr; i++){
 96662         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
 96663         pKeyDup->aSortOrder[i] = 0;
 96668   /* Separate the left and the right query from one another
 96669   */
 96670   p->pPrior = 0;
 96671   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
 96672   if( pPrior->pPrior==0 ){
 96673     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
 96676   /* Compute the limit registers */
 96677   computeLimitRegisters(pParse, p, labelEnd);
 96678   if( p->iLimit && op==TK_ALL ){
 96679     regLimitA = ++pParse->nMem;
 96680     regLimitB = ++pParse->nMem;
 96681     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
 96682                                   regLimitA);
 96683     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
 96684   }else{
 96685     regLimitA = regLimitB = 0;
 96687   sqlite3ExprDelete(db, p->pLimit);
 96688   p->pLimit = 0;
 96689   sqlite3ExprDelete(db, p->pOffset);
 96690   p->pOffset = 0;
 96692   regAddrA = ++pParse->nMem;
 96693   regEofA = ++pParse->nMem;
 96694   regAddrB = ++pParse->nMem;
 96695   regEofB = ++pParse->nMem;
 96696   regOutA = ++pParse->nMem;
 96697   regOutB = ++pParse->nMem;
 96698   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
 96699   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
 96701   /* Jump past the various subroutines and coroutines to the main
 96702   ** merge loop
 96703   */
 96704   j1 = sqlite3VdbeAddOp0(v, OP_Goto);
 96705   addrSelectA = sqlite3VdbeCurrentAddr(v);
 96708   /* Generate a coroutine to evaluate the SELECT statement to the
 96709   ** left of the compound operator - the "A" select.
 96710   */
 96711   VdbeNoopComment((v, "Begin coroutine for left SELECT"));
 96712   pPrior->iLimit = regLimitA;
 96713   explainSetInteger(iSub1, pParse->iNextSelectId);
 96714   sqlite3Select(pParse, pPrior, &destA);
 96715   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
 96716   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 96717   VdbeNoopComment((v, "End coroutine for left SELECT"));
 96719   /* Generate a coroutine to evaluate the SELECT statement on 
 96720   ** the right - the "B" select
 96721   */
 96722   addrSelectB = sqlite3VdbeCurrentAddr(v);
 96723   VdbeNoopComment((v, "Begin coroutine for right SELECT"));
 96724   savedLimit = p->iLimit;
 96725   savedOffset = p->iOffset;
 96726   p->iLimit = regLimitB;
 96727   p->iOffset = 0;  
 96728   explainSetInteger(iSub2, pParse->iNextSelectId);
 96729   sqlite3Select(pParse, p, &destB);
 96730   p->iLimit = savedLimit;
 96731   p->iOffset = savedOffset;
 96732   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
 96733   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
 96734   VdbeNoopComment((v, "End coroutine for right SELECT"));
 96736   /* Generate a subroutine that outputs the current row of the A
 96737   ** select as the next output row of the compound select.
 96738   */
 96739   VdbeNoopComment((v, "Output routine for A"));
 96740   addrOutA = generateOutputSubroutine(pParse,
 96741                  p, &destA, pDest, regOutA,
 96742                  regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
 96744   /* Generate a subroutine that outputs the current row of the B
 96745   ** select as the next output row of the compound select.
 96746   */
 96747   if( op==TK_ALL || op==TK_UNION ){
 96748     VdbeNoopComment((v, "Output routine for B"));
 96749     addrOutB = generateOutputSubroutine(pParse,
 96750                  p, &destB, pDest, regOutB,
 96751                  regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
 96754   /* Generate a subroutine to run when the results from select A
 96755   ** are exhausted and only data in select B remains.
 96756   */
 96757   VdbeNoopComment((v, "eof-A subroutine"));
 96758   if( op==TK_EXCEPT || op==TK_INTERSECT ){
 96759     addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
 96760   }else{  
 96761     addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
 96762     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 96763     sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
 96764     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
 96765     p->nSelectRow += pPrior->nSelectRow;
 96768   /* Generate a subroutine to run when the results from select B
 96769   ** are exhausted and only data in select A remains.
 96770   */
 96771   if( op==TK_INTERSECT ){
 96772     addrEofB = addrEofA;
 96773     if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
 96774   }else{  
 96775     VdbeNoopComment((v, "eof-B subroutine"));
 96776     addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
 96777     sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 96778     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 96779     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
 96782   /* Generate code to handle the case of A<B
 96783   */
 96784   VdbeNoopComment((v, "A-lt-B subroutine"));
 96785   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 96786   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 96787   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
 96788   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 96790   /* Generate code to handle the case of A==B
 96791   */
 96792   if( op==TK_ALL ){
 96793     addrAeqB = addrAltB;
 96794   }else if( op==TK_INTERSECT ){
 96795     addrAeqB = addrAltB;
 96796     addrAltB++;
 96797   }else{
 96798     VdbeNoopComment((v, "A-eq-B subroutine"));
 96799     addrAeqB =
 96800     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 96801     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
 96802     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 96805   /* Generate code to handle the case of A>B
 96806   */
 96807   VdbeNoopComment((v, "A-gt-B subroutine"));
 96808   addrAgtB = sqlite3VdbeCurrentAddr(v);
 96809   if( op==TK_ALL || op==TK_UNION ){
 96810     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 96812   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
 96813   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
 96814   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 96816   /* This code runs once to initialize everything.
 96817   */
 96818   sqlite3VdbeJumpHere(v, j1);
 96819   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
 96820   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
 96821   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
 96822   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
 96823   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
 96824   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
 96826   /* Implement the main merge loop
 96827   */
 96828   sqlite3VdbeResolveLabel(v, labelCmpr);
 96829   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
 96830   sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
 96831                          (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
 96832   sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
 96833   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
 96835   /* Release temporary registers
 96836   */
 96837   if( regPrev ){
 96838     sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
 96841   /* Jump to the this point in order to terminate the query.
 96842   */
 96843   sqlite3VdbeResolveLabel(v, labelEnd);
 96845   /* Set the number of output columns
 96846   */
 96847   if( pDest->eDest==SRT_Output ){
 96848     Select *pFirst = pPrior;
 96849     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 96850     generateColumnNames(pParse, 0, pFirst->pEList);
 96853   /* Reassembly the compound query so that it will be freed correctly
 96854   ** by the calling function */
 96855   if( p->pPrior ){
 96856     sqlite3SelectDelete(db, p->pPrior);
 96858   p->pPrior = pPrior;
 96860   /*** TBD:  Insert subroutine calls to close cursors on incomplete
 96861   **** subqueries ****/
 96862   explainComposite(pParse, p->op, iSub1, iSub2, 0);
 96863   return SQLITE_OK;
 96865 #endif
 96867 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 96868 /* Forward Declarations */
 96869 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
 96870 static void substSelect(sqlite3*, Select *, int, ExprList *);
 96872 /*
 96873 ** Scan through the expression pExpr.  Replace every reference to
 96874 ** a column in table number iTable with a copy of the iColumn-th
 96875 ** entry in pEList.  (But leave references to the ROWID column 
 96876 ** unchanged.)
 96877 **
 96878 ** This routine is part of the flattening procedure.  A subquery
 96879 ** whose result set is defined by pEList appears as entry in the
 96880 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
 96881 ** FORM clause entry is iTable.  This routine make the necessary 
 96882 ** changes to pExpr so that it refers directly to the source table
 96883 ** of the subquery rather the result set of the subquery.
 96884 */
 96885 static Expr *substExpr(
 96886   sqlite3 *db,        /* Report malloc errors to this connection */
 96887   Expr *pExpr,        /* Expr in which substitution occurs */
 96888   int iTable,         /* Table to be substituted */
 96889   ExprList *pEList    /* Substitute expressions */
 96890 ){
 96891   if( pExpr==0 ) return 0;
 96892   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
 96893     if( pExpr->iColumn<0 ){
 96894       pExpr->op = TK_NULL;
 96895     }else{
 96896       Expr *pNew;
 96897       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
 96898       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 96899       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
 96900       sqlite3ExprDelete(db, pExpr);
 96901       pExpr = pNew;
 96903   }else{
 96904     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
 96905     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
 96906     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 96907       substSelect(db, pExpr->x.pSelect, iTable, pEList);
 96908     }else{
 96909       substExprList(db, pExpr->x.pList, iTable, pEList);
 96912   return pExpr;
 96914 static void substExprList(
 96915   sqlite3 *db,         /* Report malloc errors here */
 96916   ExprList *pList,     /* List to scan and in which to make substitutes */
 96917   int iTable,          /* Table to be substituted */
 96918   ExprList *pEList     /* Substitute values */
 96919 ){
 96920   int i;
 96921   if( pList==0 ) return;
 96922   for(i=0; i<pList->nExpr; i++){
 96923     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
 96926 static void substSelect(
 96927   sqlite3 *db,         /* Report malloc errors here */
 96928   Select *p,           /* SELECT statement in which to make substitutions */
 96929   int iTable,          /* Table to be replaced */
 96930   ExprList *pEList     /* Substitute values */
 96931 ){
 96932   SrcList *pSrc;
 96933   struct SrcList_item *pItem;
 96934   int i;
 96935   if( !p ) return;
 96936   substExprList(db, p->pEList, iTable, pEList);
 96937   substExprList(db, p->pGroupBy, iTable, pEList);
 96938   substExprList(db, p->pOrderBy, iTable, pEList);
 96939   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
 96940   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
 96941   substSelect(db, p->pPrior, iTable, pEList);
 96942   pSrc = p->pSrc;
 96943   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
 96944   if( ALWAYS(pSrc) ){
 96945     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
 96946       substSelect(db, pItem->pSelect, iTable, pEList);
 96950 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 96952 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 96953 /*
 96954 ** This routine attempts to flatten subqueries as a performance optimization.
 96955 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
 96956 **
 96957 ** To understand the concept of flattening, consider the following
 96958 ** query:
 96959 **
 96960 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
 96961 **
 96962 ** The default way of implementing this query is to execute the
 96963 ** subquery first and store the results in a temporary table, then
 96964 ** run the outer query on that temporary table.  This requires two
 96965 ** passes over the data.  Furthermore, because the temporary table
 96966 ** has no indices, the WHERE clause on the outer query cannot be
 96967 ** optimized.
 96968 **
 96969 ** This routine attempts to rewrite queries such as the above into
 96970 ** a single flat select, like this:
 96971 **
 96972 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
 96973 **
 96974 ** The code generated for this simpification gives the same result
 96975 ** but only has to scan the data once.  And because indices might 
 96976 ** exist on the table t1, a complete scan of the data might be
 96977 ** avoided.
 96978 **
 96979 ** Flattening is only attempted if all of the following are true:
 96980 **
 96981 **   (1)  The subquery and the outer query do not both use aggregates.
 96982 **
 96983 **   (2)  The subquery is not an aggregate or the outer query is not a join.
 96984 **
 96985 **   (3)  The subquery is not the right operand of a left outer join
 96986 **        (Originally ticket #306.  Strengthened by ticket #3300)
 96987 **
 96988 **   (4)  The subquery is not DISTINCT.
 96989 **
 96990 **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
 96991 **        sub-queries that were excluded from this optimization. Restriction 
 96992 **        (4) has since been expanded to exclude all DISTINCT subqueries.
 96993 **
 96994 **   (6)  The subquery does not use aggregates or the outer query is not
 96995 **        DISTINCT.
 96996 **
 96997 **   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
 96998 **        A FROM clause, consider adding a FROM close with the special
 96999 **        table sqlite_once that consists of a single row containing a
 97000 **        single NULL.
 97001 **
 97002 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
 97003 **
 97004 **   (9)  The subquery does not use LIMIT or the outer query does not use
 97005 **        aggregates.
 97006 **
 97007 **  (10)  The subquery does not use aggregates or the outer query does not
 97008 **        use LIMIT.
 97009 **
 97010 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
 97011 **
 97012 **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
 97013 **        a separate restriction deriving from ticket #350.
 97014 **
 97015 **  (13)  The subquery and outer query do not both use LIMIT.
 97016 **
 97017 **  (14)  The subquery does not use OFFSET.
 97018 **
 97019 **  (15)  The outer query is not part of a compound select or the
 97020 **        subquery does not have a LIMIT clause.
 97021 **        (See ticket #2339 and ticket [02a8e81d44]).
 97022 **
 97023 **  (16)  The outer query is not an aggregate or the subquery does
 97024 **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
 97025 **        until we introduced the group_concat() function.  
 97026 **
 97027 **  (17)  The sub-query is not a compound select, or it is a UNION ALL 
 97028 **        compound clause made up entirely of non-aggregate queries, and 
 97029 **        the parent query:
 97030 **
 97031 **          * is not itself part of a compound select,
 97032 **          * is not an aggregate or DISTINCT query, and
 97033 **          * is not a join
 97034 **
 97035 **        The parent and sub-query may contain WHERE clauses. Subject to
 97036 **        rules (11), (13) and (14), they may also contain ORDER BY,
 97037 **        LIMIT and OFFSET clauses.  The subquery cannot use any compound
 97038 **        operator other than UNION ALL because all the other compound
 97039 **        operators have an implied DISTINCT which is disallowed by
 97040 **        restriction (4).
 97041 **
 97042 **        Also, each component of the sub-query must return the same number
 97043 **        of result columns. This is actually a requirement for any compound
 97044 **        SELECT statement, but all the code here does is make sure that no
 97045 **        such (illegal) sub-query is flattened. The caller will detect the
 97046 **        syntax error and return a detailed message.
 97047 **
 97048 **  (18)  If the sub-query is a compound select, then all terms of the
 97049 **        ORDER by clause of the parent must be simple references to 
 97050 **        columns of the sub-query.
 97051 **
 97052 **  (19)  The subquery does not use LIMIT or the outer query does not
 97053 **        have a WHERE clause.
 97054 **
 97055 **  (20)  If the sub-query is a compound select, then it must not use
 97056 **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
 97057 **        somewhat by saying that the terms of the ORDER BY clause must
 97058 **        appear as unmodified result columns in the outer query.  But we
 97059 **        have other optimizations in mind to deal with that case.
 97060 **
 97061 **  (21)  The subquery does not use LIMIT or the outer query is not
 97062 **        DISTINCT.  (See ticket [752e1646fc]).
 97063 **
 97064 ** In this routine, the "p" parameter is a pointer to the outer query.
 97065 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
 97066 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
 97067 **
 97068 ** If flattening is not attempted, this routine is a no-op and returns 0.
 97069 ** If flattening is attempted this routine returns 1.
 97070 **
 97071 ** All of the expression analysis must occur on both the outer query and
 97072 ** the subquery before this routine runs.
 97073 */
 97074 static int flattenSubquery(
 97075   Parse *pParse,       /* Parsing context */
 97076   Select *p,           /* The parent or outer SELECT statement */
 97077   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
 97078   int isAgg,           /* True if outer SELECT uses aggregate functions */
 97079   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
 97080 ){
 97081   const char *zSavedAuthContext = pParse->zAuthContext;
 97082   Select *pParent;
 97083   Select *pSub;       /* The inner query or "subquery" */
 97084   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
 97085   SrcList *pSrc;      /* The FROM clause of the outer query */
 97086   SrcList *pSubSrc;   /* The FROM clause of the subquery */
 97087   ExprList *pList;    /* The result set of the outer query */
 97088   int iParent;        /* VDBE cursor number of the pSub result set temp table */
 97089   int i;              /* Loop counter */
 97090   Expr *pWhere;                    /* The WHERE clause */
 97091   struct SrcList_item *pSubitem;   /* The subquery */
 97092   sqlite3 *db = pParse->db;
 97094   /* Check to see if flattening is permitted.  Return 0 if not.
 97095   */
 97096   assert( p!=0 );
 97097   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
 97098   if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
 97099   pSrc = p->pSrc;
 97100   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
 97101   pSubitem = &pSrc->a[iFrom];
 97102   iParent = pSubitem->iCursor;
 97103   pSub = pSubitem->pSelect;
 97104   assert( pSub!=0 );
 97105   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
 97106   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
 97107   pSubSrc = pSub->pSrc;
 97108   assert( pSubSrc );
 97109   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
 97110   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
 97111   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
 97112   ** became arbitrary expressions, we were forced to add restrictions (13)
 97113   ** and (14). */
 97114   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
 97115   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
 97116   if( p->pRightmost && pSub->pLimit ){
 97117     return 0;                                            /* Restriction (15) */
 97119   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
 97120   if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
 97121   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
 97122      return 0;         /* Restrictions (8)(9) */
 97124   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
 97125      return 0;         /* Restriction (6)  */
 97127   if( p->pOrderBy && pSub->pOrderBy ){
 97128      return 0;                                           /* Restriction (11) */
 97130   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
 97131   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
 97132   if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
 97133      return 0;         /* Restriction (21) */
 97136   /* OBSOLETE COMMENT 1:
 97137   ** Restriction 3:  If the subquery is a join, make sure the subquery is 
 97138   ** not used as the right operand of an outer join.  Examples of why this
 97139   ** is not allowed:
 97140   **
 97141   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
 97142   **
 97143   ** If we flatten the above, we would get
 97144   **
 97145   **         (t1 LEFT OUTER JOIN t2) JOIN t3
 97146   **
 97147   ** which is not at all the same thing.
 97148   **
 97149   ** OBSOLETE COMMENT 2:
 97150   ** Restriction 12:  If the subquery is the right operand of a left outer
 97151   ** join, make sure the subquery has no WHERE clause.
 97152   ** An examples of why this is not allowed:
 97153   **
 97154   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
 97155   **
 97156   ** If we flatten the above, we would get
 97157   **
 97158   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
 97159   **
 97160   ** But the t2.x>0 test will always fail on a NULL row of t2, which
 97161   ** effectively converts the OUTER JOIN into an INNER JOIN.
 97162   **
 97163   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
 97164   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
 97165   ** is fraught with danger.  Best to avoid the whole thing.  If the
 97166   ** subquery is the right term of a LEFT JOIN, then do not flatten.
 97167   */
 97168   if( (pSubitem->jointype & JT_OUTER)!=0 ){
 97169     return 0;
 97172   /* Restriction 17: If the sub-query is a compound SELECT, then it must
 97173   ** use only the UNION ALL operator. And none of the simple select queries
 97174   ** that make up the compound SELECT are allowed to be aggregate or distinct
 97175   ** queries.
 97176   */
 97177   if( pSub->pPrior ){
 97178     if( pSub->pOrderBy ){
 97179       return 0;  /* Restriction 20 */
 97181     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
 97182       return 0;
 97184     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
 97185       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 97186       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 97187       assert( pSub->pSrc!=0 );
 97188       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
 97189        || (pSub1->pPrior && pSub1->op!=TK_ALL) 
 97190        || pSub1->pSrc->nSrc<1
 97191        || pSub->pEList->nExpr!=pSub1->pEList->nExpr
 97192       ){
 97193         return 0;
 97195       testcase( pSub1->pSrc->nSrc>1 );
 97198     /* Restriction 18. */
 97199     if( p->pOrderBy ){
 97200       int ii;
 97201       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
 97202         if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
 97207   /***** If we reach this point, flattening is permitted. *****/
 97209   /* Authorize the subquery */
 97210   pParse->zAuthContext = pSubitem->zName;
 97211   TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
 97212   testcase( i==SQLITE_DENY );
 97213   pParse->zAuthContext = zSavedAuthContext;
 97215   /* If the sub-query is a compound SELECT statement, then (by restrictions
 97216   ** 17 and 18 above) it must be a UNION ALL and the parent query must 
 97217   ** be of the form:
 97218   **
 97219   **     SELECT <expr-list> FROM (<sub-query>) <where-clause> 
 97220   **
 97221   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
 97222   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or 
 97223   ** OFFSET clauses and joins them to the left-hand-side of the original
 97224   ** using UNION ALL operators. In this case N is the number of simple
 97225   ** select statements in the compound sub-query.
 97226   **
 97227   ** Example:
 97228   **
 97229   **     SELECT a+1 FROM (
 97230   **        SELECT x FROM tab
 97231   **        UNION ALL
 97232   **        SELECT y FROM tab
 97233   **        UNION ALL
 97234   **        SELECT abs(z*2) FROM tab2
 97235   **     ) WHERE a!=5 ORDER BY 1
 97236   **
 97237   ** Transformed into:
 97238   **
 97239   **     SELECT x+1 FROM tab WHERE x+1!=5
 97240   **     UNION ALL
 97241   **     SELECT y+1 FROM tab WHERE y+1!=5
 97242   **     UNION ALL
 97243   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
 97244   **     ORDER BY 1
 97245   **
 97246   ** We call this the "compound-subquery flattening".
 97247   */
 97248   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
 97249     Select *pNew;
 97250     ExprList *pOrderBy = p->pOrderBy;
 97251     Expr *pLimit = p->pLimit;
 97252     Select *pPrior = p->pPrior;
 97253     p->pOrderBy = 0;
 97254     p->pSrc = 0;
 97255     p->pPrior = 0;
 97256     p->pLimit = 0;
 97257     pNew = sqlite3SelectDup(db, p, 0);
 97258     p->pLimit = pLimit;
 97259     p->pOrderBy = pOrderBy;
 97260     p->pSrc = pSrc;
 97261     p->op = TK_ALL;
 97262     p->pRightmost = 0;
 97263     if( pNew==0 ){
 97264       pNew = pPrior;
 97265     }else{
 97266       pNew->pPrior = pPrior;
 97267       pNew->pRightmost = 0;
 97269     p->pPrior = pNew;
 97270     if( db->mallocFailed ) return 1;
 97273   /* Begin flattening the iFrom-th entry of the FROM clause 
 97274   ** in the outer query.
 97275   */
 97276   pSub = pSub1 = pSubitem->pSelect;
 97278   /* Delete the transient table structure associated with the
 97279   ** subquery
 97280   */
 97281   sqlite3DbFree(db, pSubitem->zDatabase);
 97282   sqlite3DbFree(db, pSubitem->zName);
 97283   sqlite3DbFree(db, pSubitem->zAlias);
 97284   pSubitem->zDatabase = 0;
 97285   pSubitem->zName = 0;
 97286   pSubitem->zAlias = 0;
 97287   pSubitem->pSelect = 0;
 97289   /* Defer deleting the Table object associated with the
 97290   ** subquery until code generation is
 97291   ** complete, since there may still exist Expr.pTab entries that
 97292   ** refer to the subquery even after flattening.  Ticket #3346.
 97293   **
 97294   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
 97295   */
 97296   if( ALWAYS(pSubitem->pTab!=0) ){
 97297     Table *pTabToDel = pSubitem->pTab;
 97298     if( pTabToDel->nRef==1 ){
 97299       Parse *pToplevel = sqlite3ParseToplevel(pParse);
 97300       pTabToDel->pNextZombie = pToplevel->pZombieTab;
 97301       pToplevel->pZombieTab = pTabToDel;
 97302     }else{
 97303       pTabToDel->nRef--;
 97305     pSubitem->pTab = 0;
 97308   /* The following loop runs once for each term in a compound-subquery
 97309   ** flattening (as described above).  If we are doing a different kind
 97310   ** of flattening - a flattening other than a compound-subquery flattening -
 97311   ** then this loop only runs once.
 97312   **
 97313   ** This loop moves all of the FROM elements of the subquery into the
 97314   ** the FROM clause of the outer query.  Before doing this, remember
 97315   ** the cursor number for the original outer query FROM element in
 97316   ** iParent.  The iParent cursor will never be used.  Subsequent code
 97317   ** will scan expressions looking for iParent references and replace
 97318   ** those references with expressions that resolve to the subquery FROM
 97319   ** elements we are now copying in.
 97320   */
 97321   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
 97322     int nSubSrc;
 97323     u8 jointype = 0;
 97324     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
 97325     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
 97326     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
 97328     if( pSrc ){
 97329       assert( pParent==p );  /* First time through the loop */
 97330       jointype = pSubitem->jointype;
 97331     }else{
 97332       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
 97333       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
 97334       if( pSrc==0 ){
 97335         assert( db->mallocFailed );
 97336         break;
 97340     /* The subquery uses a single slot of the FROM clause of the outer
 97341     ** query.  If the subquery has more than one element in its FROM clause,
 97342     ** then expand the outer query to make space for it to hold all elements
 97343     ** of the subquery.
 97344     **
 97345     ** Example:
 97346     **
 97347     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
 97348     **
 97349     ** The outer query has 3 slots in its FROM clause.  One slot of the
 97350     ** outer query (the middle slot) is used by the subquery.  The next
 97351     ** block of code will expand the out query to 4 slots.  The middle
 97352     ** slot is expanded to two slots in order to make space for the
 97353     ** two elements in the FROM clause of the subquery.
 97354     */
 97355     if( nSubSrc>1 ){
 97356       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
 97357       if( db->mallocFailed ){
 97358         break;
 97362     /* Transfer the FROM clause terms from the subquery into the
 97363     ** outer query.
 97364     */
 97365     for(i=0; i<nSubSrc; i++){
 97366       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
 97367       pSrc->a[i+iFrom] = pSubSrc->a[i];
 97368       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
 97370     pSrc->a[iFrom].jointype = jointype;
 97372     /* Now begin substituting subquery result set expressions for 
 97373     ** references to the iParent in the outer query.
 97374     ** 
 97375     ** Example:
 97376     **
 97377     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
 97378     **   \                     \_____________ subquery __________/          /
 97379     **    \_____________________ outer query ______________________________/
 97380     **
 97381     ** We look at every expression in the outer query and every place we see
 97382     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
 97383     */
 97384     pList = pParent->pEList;
 97385     for(i=0; i<pList->nExpr; i++){
 97386       if( pList->a[i].zName==0 ){
 97387         char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
 97388         sqlite3Dequote(zName);
 97389         pList->a[i].zName = zName;
 97392     substExprList(db, pParent->pEList, iParent, pSub->pEList);
 97393     if( isAgg ){
 97394       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
 97395       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
 97397     if( pSub->pOrderBy ){
 97398       assert( pParent->pOrderBy==0 );
 97399       pParent->pOrderBy = pSub->pOrderBy;
 97400       pSub->pOrderBy = 0;
 97401     }else if( pParent->pOrderBy ){
 97402       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
 97404     if( pSub->pWhere ){
 97405       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
 97406     }else{
 97407       pWhere = 0;
 97409     if( subqueryIsAgg ){
 97410       assert( pParent->pHaving==0 );
 97411       pParent->pHaving = pParent->pWhere;
 97412       pParent->pWhere = pWhere;
 97413       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
 97414       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
 97415                                   sqlite3ExprDup(db, pSub->pHaving, 0));
 97416       assert( pParent->pGroupBy==0 );
 97417       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
 97418     }else{
 97419       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
 97420       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
 97423     /* The flattened query is distinct if either the inner or the
 97424     ** outer query is distinct. 
 97425     */
 97426     pParent->selFlags |= pSub->selFlags & SF_Distinct;
 97428     /*
 97429     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
 97430     **
 97431     ** One is tempted to try to add a and b to combine the limits.  But this
 97432     ** does not work if either limit is negative.
 97433     */
 97434     if( pSub->pLimit ){
 97435       pParent->pLimit = pSub->pLimit;
 97436       pSub->pLimit = 0;
 97440   /* Finially, delete what is left of the subquery and return
 97441   ** success.
 97442   */
 97443   sqlite3SelectDelete(db, pSub1);
 97445   return 1;
 97447 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 97449 /*
 97450 ** Analyze the SELECT statement passed as an argument to see if it
 97451 ** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if 
 97452 ** it is, or 0 otherwise. At present, a query is considered to be
 97453 ** a min()/max() query if:
 97454 **
 97455 **   1. There is a single object in the FROM clause.
 97456 **
 97457 **   2. There is a single expression in the result set, and it is
 97458 **      either min(x) or max(x), where x is a column reference.
 97459 */
 97460 static u8 minMaxQuery(Select *p){
 97461   Expr *pExpr;
 97462   ExprList *pEList = p->pEList;
 97464   if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
 97465   pExpr = pEList->a[0].pExpr;
 97466   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
 97467   if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
 97468   pEList = pExpr->x.pList;
 97469   if( pEList==0 || pEList->nExpr!=1 ) return 0;
 97470   if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
 97471   assert( !ExprHasProperty(pExpr, EP_IntValue) );
 97472   if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
 97473     return WHERE_ORDERBY_MIN;
 97474   }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
 97475     return WHERE_ORDERBY_MAX;
 97477   return WHERE_ORDERBY_NORMAL;
 97480 /*
 97481 ** The select statement passed as the first argument is an aggregate query.
 97482 ** The second argment is the associated aggregate-info object. This 
 97483 ** function tests if the SELECT is of the form:
 97484 **
 97485 **   SELECT count(*) FROM <tbl>
 97486 **
 97487 ** where table is a database table, not a sub-select or view. If the query
 97488 ** does match this pattern, then a pointer to the Table object representing
 97489 ** <tbl> is returned. Otherwise, 0 is returned.
 97490 */
 97491 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
 97492   Table *pTab;
 97493   Expr *pExpr;
 97495   assert( !p->pGroupBy );
 97497   if( p->pWhere || p->pEList->nExpr!=1 
 97498    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
 97499   ){
 97500     return 0;
 97502   pTab = p->pSrc->a[0].pTab;
 97503   pExpr = p->pEList->a[0].pExpr;
 97504   assert( pTab && !pTab->pSelect && pExpr );
 97506   if( IsVirtual(pTab) ) return 0;
 97507   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
 97508   if( NEVER(pAggInfo->nFunc==0) ) return 0;
 97509   if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
 97510   if( pExpr->flags&EP_Distinct ) return 0;
 97512   return pTab;
 97515 /*
 97516 ** If the source-list item passed as an argument was augmented with an
 97517 ** INDEXED BY clause, then try to locate the specified index. If there
 97518 ** was such a clause and the named index cannot be found, return 
 97519 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
 97520 ** pFrom->pIndex and return SQLITE_OK.
 97521 */
 97522 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
 97523   if( pFrom->pTab && pFrom->zIndex ){
 97524     Table *pTab = pFrom->pTab;
 97525     char *zIndex = pFrom->zIndex;
 97526     Index *pIdx;
 97527     for(pIdx=pTab->pIndex; 
 97528         pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
 97529         pIdx=pIdx->pNext
 97530     );
 97531     if( !pIdx ){
 97532       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
 97533       pParse->checkSchema = 1;
 97534       return SQLITE_ERROR;
 97536     pFrom->pIndex = pIdx;
 97538   return SQLITE_OK;
 97541 /*
 97542 ** This routine is a Walker callback for "expanding" a SELECT statement.
 97543 ** "Expanding" means to do the following:
 97544 **
 97545 **    (1)  Make sure VDBE cursor numbers have been assigned to every
 97546 **         element of the FROM clause.
 97547 **
 97548 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
 97549 **         defines FROM clause.  When views appear in the FROM clause,
 97550 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
 97551 **         that implements the view.  A copy is made of the view's SELECT
 97552 **         statement so that we can freely modify or delete that statement
 97553 **         without worrying about messing up the presistent representation
 97554 **         of the view.
 97555 **
 97556 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
 97557 **         on joins and the ON and USING clause of joins.
 97558 **
 97559 **    (4)  Scan the list of columns in the result set (pEList) looking
 97560 **         for instances of the "*" operator or the TABLE.* operator.
 97561 **         If found, expand each "*" to be every column in every table
 97562 **         and TABLE.* to be every column in TABLE.
 97563 **
 97564 */
 97565 static int selectExpander(Walker *pWalker, Select *p){
 97566   Parse *pParse = pWalker->pParse;
 97567   int i, j, k;
 97568   SrcList *pTabList;
 97569   ExprList *pEList;
 97570   struct SrcList_item *pFrom;
 97571   sqlite3 *db = pParse->db;
 97573   if( db->mallocFailed  ){
 97574     return WRC_Abort;
 97576   if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
 97577     return WRC_Prune;
 97579   p->selFlags |= SF_Expanded;
 97580   pTabList = p->pSrc;
 97581   pEList = p->pEList;
 97583   /* Make sure cursor numbers have been assigned to all entries in
 97584   ** the FROM clause of the SELECT statement.
 97585   */
 97586   sqlite3SrcListAssignCursors(pParse, pTabList);
 97588   /* Look up every table named in the FROM clause of the select.  If
 97589   ** an entry of the FROM clause is a subquery instead of a table or view,
 97590   ** then create a transient table structure to describe the subquery.
 97591   */
 97592   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 97593     Table *pTab;
 97594     if( pFrom->pTab!=0 ){
 97595       /* This statement has already been prepared.  There is no need
 97596       ** to go further. */
 97597       assert( i==0 );
 97598       return WRC_Prune;
 97600     if( pFrom->zName==0 ){
 97601 #ifndef SQLITE_OMIT_SUBQUERY
 97602       Select *pSel = pFrom->pSelect;
 97603       /* A sub-query in the FROM clause of a SELECT */
 97604       assert( pSel!=0 );
 97605       assert( pFrom->pTab==0 );
 97606       sqlite3WalkSelect(pWalker, pSel);
 97607       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
 97608       if( pTab==0 ) return WRC_Abort;
 97609       pTab->nRef = 1;
 97610       pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
 97611       while( pSel->pPrior ){ pSel = pSel->pPrior; }
 97612       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
 97613       pTab->iPKey = -1;
 97614       pTab->nRowEst = 1000000;
 97615       pTab->tabFlags |= TF_Ephemeral;
 97616 #endif
 97617     }else{
 97618       /* An ordinary table or view name in the FROM clause */
 97619       assert( pFrom->pTab==0 );
 97620       pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
 97621       if( pTab==0 ) return WRC_Abort;
 97622       pTab->nRef++;
 97623 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
 97624       if( pTab->pSelect || IsVirtual(pTab) ){
 97625         /* We reach here if the named table is a really a view */
 97626         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
 97627         assert( pFrom->pSelect==0 );
 97628         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
 97629         sqlite3WalkSelect(pWalker, pFrom->pSelect);
 97631 #endif
 97634     /* Locate the index named by the INDEXED BY clause, if any. */
 97635     if( sqlite3IndexedByLookup(pParse, pFrom) ){
 97636       return WRC_Abort;
 97640   /* Process NATURAL keywords, and ON and USING clauses of joins.
 97641   */
 97642   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
 97643     return WRC_Abort;
 97646   /* For every "*" that occurs in the column list, insert the names of
 97647   ** all columns in all tables.  And for every TABLE.* insert the names
 97648   ** of all columns in TABLE.  The parser inserted a special expression
 97649   ** with the TK_ALL operator for each "*" that it found in the column list.
 97650   ** The following code just has to locate the TK_ALL expressions and expand
 97651   ** each one to the list of all columns in all tables.
 97652   **
 97653   ** The first loop just checks to see if there are any "*" operators
 97654   ** that need expanding.
 97655   */
 97656   for(k=0; k<pEList->nExpr; k++){
 97657     Expr *pE = pEList->a[k].pExpr;
 97658     if( pE->op==TK_ALL ) break;
 97659     assert( pE->op!=TK_DOT || pE->pRight!=0 );
 97660     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
 97661     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
 97663   if( k<pEList->nExpr ){
 97664     /*
 97665     ** If we get here it means the result set contains one or more "*"
 97666     ** operators that need to be expanded.  Loop through each expression
 97667     ** in the result set and expand them one by one.
 97668     */
 97669     struct ExprList_item *a = pEList->a;
 97670     ExprList *pNew = 0;
 97671     int flags = pParse->db->flags;
 97672     int longNames = (flags & SQLITE_FullColNames)!=0
 97673                       && (flags & SQLITE_ShortColNames)==0;
 97675     for(k=0; k<pEList->nExpr; k++){
 97676       Expr *pE = a[k].pExpr;
 97677       assert( pE->op!=TK_DOT || pE->pRight!=0 );
 97678       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
 97679         /* This particular expression does not need to be expanded.
 97680         */
 97681         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
 97682         if( pNew ){
 97683           pNew->a[pNew->nExpr-1].zName = a[k].zName;
 97684           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
 97685           a[k].zName = 0;
 97686           a[k].zSpan = 0;
 97688         a[k].pExpr = 0;
 97689       }else{
 97690         /* This expression is a "*" or a "TABLE.*" and needs to be
 97691         ** expanded. */
 97692         int tableSeen = 0;      /* Set to 1 when TABLE matches */
 97693         char *zTName;            /* text of name of TABLE */
 97694         if( pE->op==TK_DOT ){
 97695           assert( pE->pLeft!=0 );
 97696           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
 97697           zTName = pE->pLeft->u.zToken;
 97698         }else{
 97699           zTName = 0;
 97701         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 97702           Table *pTab = pFrom->pTab;
 97703           char *zTabName = pFrom->zAlias;
 97704           if( zTabName==0 ){
 97705             zTabName = pTab->zName;
 97707           if( db->mallocFailed ) break;
 97708           if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
 97709             continue;
 97711           tableSeen = 1;
 97712           for(j=0; j<pTab->nCol; j++){
 97713             Expr *pExpr, *pRight;
 97714             char *zName = pTab->aCol[j].zName;
 97715             char *zColname;  /* The computed column name */
 97716             char *zToFree;   /* Malloced string that needs to be freed */
 97717             Token sColname;  /* Computed column name as a token */
 97719             /* If a column is marked as 'hidden' (currently only possible
 97720             ** for virtual tables), do not include it in the expanded
 97721             ** result-set list.
 97722             */
 97723             if( IsHiddenColumn(&pTab->aCol[j]) ){
 97724               assert(IsVirtual(pTab));
 97725               continue;
 97728             if( i>0 && zTName==0 ){
 97729               if( (pFrom->jointype & JT_NATURAL)!=0
 97730                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
 97731               ){
 97732                 /* In a NATURAL join, omit the join columns from the 
 97733                 ** table to the right of the join */
 97734                 continue;
 97736               if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
 97737                 /* In a join with a USING clause, omit columns in the
 97738                 ** using clause from the table on the right. */
 97739                 continue;
 97742             pRight = sqlite3Expr(db, TK_ID, zName);
 97743             zColname = zName;
 97744             zToFree = 0;
 97745             if( longNames || pTabList->nSrc>1 ){
 97746               Expr *pLeft;
 97747               pLeft = sqlite3Expr(db, TK_ID, zTabName);
 97748               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
 97749               if( longNames ){
 97750                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
 97751                 zToFree = zColname;
 97753             }else{
 97754               pExpr = pRight;
 97756             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
 97757             sColname.z = zColname;
 97758             sColname.n = sqlite3Strlen30(zColname);
 97759             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
 97760             sqlite3DbFree(db, zToFree);
 97763         if( !tableSeen ){
 97764           if( zTName ){
 97765             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
 97766           }else{
 97767             sqlite3ErrorMsg(pParse, "no tables specified");
 97772     sqlite3ExprListDelete(db, pEList);
 97773     p->pEList = pNew;
 97775 #if SQLITE_MAX_COLUMN
 97776   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 97777     sqlite3ErrorMsg(pParse, "too many columns in result set");
 97779 #endif
 97780   return WRC_Continue;
 97783 /*
 97784 ** No-op routine for the parse-tree walker.
 97785 **
 97786 ** When this routine is the Walker.xExprCallback then expression trees
 97787 ** are walked without any actions being taken at each node.  Presumably,
 97788 ** when this routine is used for Walker.xExprCallback then 
 97789 ** Walker.xSelectCallback is set to do something useful for every 
 97790 ** subquery in the parser tree.
 97791 */
 97792 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
 97793   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 97794   return WRC_Continue;
 97797 /*
 97798 ** This routine "expands" a SELECT statement and all of its subqueries.
 97799 ** For additional information on what it means to "expand" a SELECT
 97800 ** statement, see the comment on the selectExpand worker callback above.
 97801 **
 97802 ** Expanding a SELECT statement is the first step in processing a
 97803 ** SELECT statement.  The SELECT statement must be expanded before
 97804 ** name resolution is performed.
 97805 **
 97806 ** If anything goes wrong, an error message is written into pParse.
 97807 ** The calling function can detect the problem by looking at pParse->nErr
 97808 ** and/or pParse->db->mallocFailed.
 97809 */
 97810 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
 97811   Walker w;
 97812   w.xSelectCallback = selectExpander;
 97813   w.xExprCallback = exprWalkNoop;
 97814   w.pParse = pParse;
 97815   sqlite3WalkSelect(&w, pSelect);
 97819 #ifndef SQLITE_OMIT_SUBQUERY
 97820 /*
 97821 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
 97822 ** interface.
 97823 **
 97824 ** For each FROM-clause subquery, add Column.zType and Column.zColl
 97825 ** information to the Table structure that represents the result set
 97826 ** of that subquery.
 97827 **
 97828 ** The Table structure that represents the result set was constructed
 97829 ** by selectExpander() but the type and collation information was omitted
 97830 ** at that point because identifiers had not yet been resolved.  This
 97831 ** routine is called after identifier resolution.
 97832 */
 97833 static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
 97834   Parse *pParse;
 97835   int i;
 97836   SrcList *pTabList;
 97837   struct SrcList_item *pFrom;
 97839   assert( p->selFlags & SF_Resolved );
 97840   if( (p->selFlags & SF_HasTypeInfo)==0 ){
 97841     p->selFlags |= SF_HasTypeInfo;
 97842     pParse = pWalker->pParse;
 97843     pTabList = p->pSrc;
 97844     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 97845       Table *pTab = pFrom->pTab;
 97846       if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
 97847         /* A sub-query in the FROM clause of a SELECT */
 97848         Select *pSel = pFrom->pSelect;
 97849         assert( pSel );
 97850         while( pSel->pPrior ) pSel = pSel->pPrior;
 97851         selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
 97855   return WRC_Continue;
 97857 #endif
 97860 /*
 97861 ** This routine adds datatype and collating sequence information to
 97862 ** the Table structures of all FROM-clause subqueries in a
 97863 ** SELECT statement.
 97864 **
 97865 ** Use this routine after name resolution.
 97866 */
 97867 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
 97868 #ifndef SQLITE_OMIT_SUBQUERY
 97869   Walker w;
 97870   w.xSelectCallback = selectAddSubqueryTypeInfo;
 97871   w.xExprCallback = exprWalkNoop;
 97872   w.pParse = pParse;
 97873   sqlite3WalkSelect(&w, pSelect);
 97874 #endif
 97878 /*
 97879 ** This routine sets up a SELECT statement for processing.  The
 97880 ** following is accomplished:
 97881 **
 97882 **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
 97883 **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
 97884 **     *  ON and USING clauses are shifted into WHERE statements
 97885 **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
 97886 **     *  Identifiers in expression are matched to tables.
 97887 **
 97888 ** This routine acts recursively on all subqueries within the SELECT.
 97889 */
 97890 SQLITE_PRIVATE void sqlite3SelectPrep(
 97891   Parse *pParse,         /* The parser context */
 97892   Select *p,             /* The SELECT statement being coded. */
 97893   NameContext *pOuterNC  /* Name context for container */
 97894 ){
 97895   sqlite3 *db;
 97896   if( NEVER(p==0) ) return;
 97897   db = pParse->db;
 97898   if( p->selFlags & SF_HasTypeInfo ) return;
 97899   sqlite3SelectExpand(pParse, p);
 97900   if( pParse->nErr || db->mallocFailed ) return;
 97901   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
 97902   if( pParse->nErr || db->mallocFailed ) return;
 97903   sqlite3SelectAddTypeInfo(pParse, p);
 97906 /*
 97907 ** Reset the aggregate accumulator.
 97908 **
 97909 ** The aggregate accumulator is a set of memory cells that hold
 97910 ** intermediate results while calculating an aggregate.  This
 97911 ** routine generates code that stores NULLs in all of those memory
 97912 ** cells.
 97913 */
 97914 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
 97915   Vdbe *v = pParse->pVdbe;
 97916   int i;
 97917   struct AggInfo_func *pFunc;
 97918   if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
 97919     return;
 97921   for(i=0; i<pAggInfo->nColumn; i++){
 97922     sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
 97924   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
 97925     sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
 97926     if( pFunc->iDistinct>=0 ){
 97927       Expr *pE = pFunc->pExpr;
 97928       assert( !ExprHasProperty(pE, EP_xIsSelect) );
 97929       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
 97930         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
 97931            "argument");
 97932         pFunc->iDistinct = -1;
 97933       }else{
 97934         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
 97935         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
 97936                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 97942 /*
 97943 ** Invoke the OP_AggFinalize opcode for every aggregate function
 97944 ** in the AggInfo structure.
 97945 */
 97946 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
 97947   Vdbe *v = pParse->pVdbe;
 97948   int i;
 97949   struct AggInfo_func *pF;
 97950   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
 97951     ExprList *pList = pF->pExpr->x.pList;
 97952     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
 97953     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
 97954                       (void*)pF->pFunc, P4_FUNCDEF);
 97958 /*
 97959 ** Update the accumulator memory cells for an aggregate based on
 97960 ** the current cursor position.
 97961 */
 97962 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
 97963   Vdbe *v = pParse->pVdbe;
 97964   int i;
 97965   int regHit = 0;
 97966   int addrHitTest = 0;
 97967   struct AggInfo_func *pF;
 97968   struct AggInfo_col *pC;
 97970   pAggInfo->directMode = 1;
 97971   sqlite3ExprCacheClear(pParse);
 97972   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
 97973     int nArg;
 97974     int addrNext = 0;
 97975     int regAgg;
 97976     ExprList *pList = pF->pExpr->x.pList;
 97977     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
 97978     if( pList ){
 97979       nArg = pList->nExpr;
 97980       regAgg = sqlite3GetTempRange(pParse, nArg);
 97981       sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
 97982     }else{
 97983       nArg = 0;
 97984       regAgg = 0;
 97986     if( pF->iDistinct>=0 ){
 97987       addrNext = sqlite3VdbeMakeLabel(v);
 97988       assert( nArg==1 );
 97989       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
 97991     if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 97992       CollSeq *pColl = 0;
 97993       struct ExprList_item *pItem;
 97994       int j;
 97995       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
 97996       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
 97997         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
 97999       if( !pColl ){
 98000         pColl = pParse->db->pDfltColl;
 98002       if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
 98003       sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
 98005     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
 98006                       (void*)pF->pFunc, P4_FUNCDEF);
 98007     sqlite3VdbeChangeP5(v, (u8)nArg);
 98008     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
 98009     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
 98010     if( addrNext ){
 98011       sqlite3VdbeResolveLabel(v, addrNext);
 98012       sqlite3ExprCacheClear(pParse);
 98016   /* Before populating the accumulator registers, clear the column cache.
 98017   ** Otherwise, if any of the required column values are already present 
 98018   ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
 98019   ** to pC->iMem. But by the time the value is used, the original register
 98020   ** may have been used, invalidating the underlying buffer holding the
 98021   ** text or blob value. See ticket [883034dcb5].
 98022   **
 98023   ** Another solution would be to change the OP_SCopy used to copy cached
 98024   ** values to an OP_Copy.
 98025   */
 98026   if( regHit ){
 98027     addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit);
 98029   sqlite3ExprCacheClear(pParse);
 98030   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
 98031     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
 98033   pAggInfo->directMode = 0;
 98034   sqlite3ExprCacheClear(pParse);
 98035   if( addrHitTest ){
 98036     sqlite3VdbeJumpHere(v, addrHitTest);
 98040 /*
 98041 ** Add a single OP_Explain instruction to the VDBE to explain a simple
 98042 ** count(*) query ("SELECT count(*) FROM pTab").
 98043 */
 98044 #ifndef SQLITE_OMIT_EXPLAIN
 98045 static void explainSimpleCount(
 98046   Parse *pParse,                  /* Parse context */
 98047   Table *pTab,                    /* Table being queried */
 98048   Index *pIdx                     /* Index used to optimize scan, or NULL */
 98049 ){
 98050   if( pParse->explain==2 ){
 98051     char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
 98052         pTab->zName, 
 98053         pIdx ? "USING COVERING INDEX " : "",
 98054         pIdx ? pIdx->zName : "",
 98055         pTab->nRowEst
 98056     );
 98057     sqlite3VdbeAddOp4(
 98058         pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
 98059     );
 98062 #else
 98063 # define explainSimpleCount(a,b,c)
 98064 #endif
 98066 /*
 98067 ** Generate code for the SELECT statement given in the p argument.  
 98068 **
 98069 ** The results are distributed in various ways depending on the
 98070 ** contents of the SelectDest structure pointed to by argument pDest
 98071 ** as follows:
 98072 **
 98073 **     pDest->eDest    Result
 98074 **     ------------    -------------------------------------------
 98075 **     SRT_Output      Generate a row of output (using the OP_ResultRow
 98076 **                     opcode) for each row in the result set.
 98077 **
 98078 **     SRT_Mem         Only valid if the result is a single column.
 98079 **                     Store the first column of the first result row
 98080 **                     in register pDest->iSDParm then abandon the rest
 98081 **                     of the query.  This destination implies "LIMIT 1".
 98082 **
 98083 **     SRT_Set         The result must be a single column.  Store each
 98084 **                     row of result as the key in table pDest->iSDParm. 
 98085 **                     Apply the affinity pDest->affSdst before storing
 98086 **                     results.  Used to implement "IN (SELECT ...)".
 98087 **
 98088 **     SRT_Union       Store results as a key in a temporary table 
 98089 **                     identified by pDest->iSDParm.
 98090 **
 98091 **     SRT_Except      Remove results from the temporary table pDest->iSDParm.
 98092 **
 98093 **     SRT_Table       Store results in temporary table pDest->iSDParm.
 98094 **                     This is like SRT_EphemTab except that the table
 98095 **                     is assumed to already be open.
 98096 **
 98097 **     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
 98098 **                     the result there. The cursor is left open after
 98099 **                     returning.  This is like SRT_Table except that
 98100 **                     this destination uses OP_OpenEphemeral to create
 98101 **                     the table first.
 98102 **
 98103 **     SRT_Coroutine   Generate a co-routine that returns a new row of
 98104 **                     results each time it is invoked.  The entry point
 98105 **                     of the co-routine is stored in register pDest->iSDParm.
 98106 **
 98107 **     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
 98108 **                     set is not empty.
 98109 **
 98110 **     SRT_Discard     Throw the results away.  This is used by SELECT
 98111 **                     statements within triggers whose only purpose is
 98112 **                     the side-effects of functions.
 98113 **
 98114 ** This routine returns the number of errors.  If any errors are
 98115 ** encountered, then an appropriate error message is left in
 98116 ** pParse->zErrMsg.
 98117 **
 98118 ** This routine does NOT free the Select structure passed in.  The
 98119 ** calling function needs to do that.
 98120 */
 98121 SQLITE_PRIVATE int sqlite3Select(
 98122   Parse *pParse,         /* The parser context */
 98123   Select *p,             /* The SELECT statement being coded. */
 98124   SelectDest *pDest      /* What to do with the query results */
 98125 ){
 98126   int i, j;              /* Loop counters */
 98127   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
 98128   Vdbe *v;               /* The virtual machine under construction */
 98129   int isAgg;             /* True for select lists like "count(*)" */
 98130   ExprList *pEList;      /* List of columns to extract. */
 98131   SrcList *pTabList;     /* List of tables to select from */
 98132   Expr *pWhere;          /* The WHERE clause.  May be NULL */
 98133   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
 98134   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
 98135   Expr *pHaving;         /* The HAVING clause.  May be NULL */
 98136   int rc = 1;            /* Value to return from this function */
 98137   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
 98138   DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
 98139   AggInfo sAggInfo;      /* Information used by aggregate queries */
 98140   int iEnd;              /* Address of the end of the query */
 98141   sqlite3 *db;           /* The database connection */
 98143 #ifndef SQLITE_OMIT_EXPLAIN
 98144   int iRestoreSelectId = pParse->iSelectId;
 98145   pParse->iSelectId = pParse->iNextSelectId++;
 98146 #endif
 98148   db = pParse->db;
 98149   if( p==0 || db->mallocFailed || pParse->nErr ){
 98150     return 1;
 98152   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
 98153   memset(&sAggInfo, 0, sizeof(sAggInfo));
 98155   if( IgnorableOrderby(pDest) ){
 98156     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
 98157            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
 98158     /* If ORDER BY makes no difference in the output then neither does
 98159     ** DISTINCT so it can be removed too. */
 98160     sqlite3ExprListDelete(db, p->pOrderBy);
 98161     p->pOrderBy = 0;
 98162     p->selFlags &= ~SF_Distinct;
 98164   sqlite3SelectPrep(pParse, p, 0);
 98165   pOrderBy = p->pOrderBy;
 98166   pTabList = p->pSrc;
 98167   pEList = p->pEList;
 98168   if( pParse->nErr || db->mallocFailed ){
 98169     goto select_end;
 98171   isAgg = (p->selFlags & SF_Aggregate)!=0;
 98172   assert( pEList!=0 );
 98174   /* Begin generating code.
 98175   */
 98176   v = sqlite3GetVdbe(pParse);
 98177   if( v==0 ) goto select_end;
 98179   /* If writing to memory or generating a set
 98180   ** only a single column may be output.
 98181   */
 98182 #ifndef SQLITE_OMIT_SUBQUERY
 98183   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
 98184     goto select_end;
 98186 #endif
 98188   /* Generate code for all sub-queries in the FROM clause
 98189   */
 98190 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 98191   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
 98192     struct SrcList_item *pItem = &pTabList->a[i];
 98193     SelectDest dest;
 98194     Select *pSub = pItem->pSelect;
 98195     int isAggSub;
 98197     if( pSub==0 ) continue;
 98199     /* Sometimes the code for a subquery will be generated more than
 98200     ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
 98201     ** for example.  In that case, do not regenerate the code to manifest
 98202     ** a view or the co-routine to implement a view.  The first instance
 98203     ** is sufficient, though the subroutine to manifest the view does need
 98204     ** to be invoked again. */
 98205     if( pItem->addrFillSub ){
 98206       if( pItem->viaCoroutine==0 ){
 98207         sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
 98209       continue;
 98212     /* Increment Parse.nHeight by the height of the largest expression
 98213     ** tree refered to by this, the parent select. The child select
 98214     ** may contain expression trees of at most
 98215     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
 98216     ** more conservative than necessary, but much easier than enforcing
 98217     ** an exact limit.
 98218     */
 98219     pParse->nHeight += sqlite3SelectExprHeight(p);
 98221     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
 98222     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
 98223       /* This subquery can be absorbed into its parent. */
 98224       if( isAggSub ){
 98225         isAgg = 1;
 98226         p->selFlags |= SF_Aggregate;
 98228       i = -1;
 98229     }else if( pTabList->nSrc==1 && (p->selFlags & SF_Materialize)==0
 98230       && OptimizationEnabled(db, SQLITE_SubqCoroutine)
 98231     ){
 98232       /* Implement a co-routine that will return a single row of the result
 98233       ** set on each invocation.
 98234       */
 98235       int addrTop;
 98236       int addrEof;
 98237       pItem->regReturn = ++pParse->nMem;
 98238       addrEof = ++pParse->nMem;
 98239       /* Before coding the OP_Goto to jump to the start of the main routine,
 98240       ** ensure that the jump to the verify-schema routine has already
 98241       ** been coded. Otherwise, the verify-schema would likely be coded as 
 98242       ** part of the co-routine. If the main routine then accessed the 
 98243       ** database before invoking the co-routine for the first time (for 
 98244       ** example to initialize a LIMIT register from a sub-select), it would 
 98245       ** be doing so without having verified the schema version and obtained 
 98246       ** the required db locks. See ticket d6b36be38.  */
 98247       sqlite3CodeVerifySchema(pParse, -1);
 98248       sqlite3VdbeAddOp0(v, OP_Goto);
 98249       addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
 98250       sqlite3VdbeChangeP5(v, 1);
 98251       VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
 98252       pItem->addrFillSub = addrTop;
 98253       sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
 98254       sqlite3VdbeChangeP5(v, 1);
 98255       sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
 98256       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 98257       sqlite3Select(pParse, pSub, &dest);
 98258       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
 98259       pItem->viaCoroutine = 1;
 98260       sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
 98261       sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
 98262       sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
 98263       sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
 98264       VdbeComment((v, "end %s", pItem->pTab->zName));
 98265       sqlite3VdbeJumpHere(v, addrTop-1);
 98266       sqlite3ClearTempRegCache(pParse);
 98267     }else{
 98268       /* Generate a subroutine that will fill an ephemeral table with
 98269       ** the content of this subquery.  pItem->addrFillSub will point
 98270       ** to the address of the generated subroutine.  pItem->regReturn
 98271       ** is a register allocated to hold the subroutine return address
 98272       */
 98273       int topAddr;
 98274       int onceAddr = 0;
 98275       int retAddr;
 98276       assert( pItem->addrFillSub==0 );
 98277       pItem->regReturn = ++pParse->nMem;
 98278       topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
 98279       pItem->addrFillSub = topAddr+1;
 98280       VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
 98281       if( pItem->isCorrelated==0 ){
 98282         /* If the subquery is no correlated and if we are not inside of
 98283         ** a trigger, then we only need to compute the value of the subquery
 98284         ** once. */
 98285         onceAddr = sqlite3CodeOnce(pParse);
 98287       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
 98288       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 98289       sqlite3Select(pParse, pSub, &dest);
 98290       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
 98291       if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
 98292       retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
 98293       VdbeComment((v, "end %s", pItem->pTab->zName));
 98294       sqlite3VdbeChangeP1(v, topAddr, retAddr);
 98295       sqlite3ClearTempRegCache(pParse);
 98297     if( /*pParse->nErr ||*/ db->mallocFailed ){
 98298       goto select_end;
 98300     pParse->nHeight -= sqlite3SelectExprHeight(p);
 98301     pTabList = p->pSrc;
 98302     if( !IgnorableOrderby(pDest) ){
 98303       pOrderBy = p->pOrderBy;
 98306   pEList = p->pEList;
 98307 #endif
 98308   pWhere = p->pWhere;
 98309   pGroupBy = p->pGroupBy;
 98310   pHaving = p->pHaving;
 98311   sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
 98313 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 98314   /* If there is are a sequence of queries, do the earlier ones first.
 98315   */
 98316   if( p->pPrior ){
 98317     if( p->pRightmost==0 ){
 98318       Select *pLoop, *pRight = 0;
 98319       int cnt = 0;
 98320       int mxSelect;
 98321       for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
 98322         pLoop->pRightmost = p;
 98323         pLoop->pNext = pRight;
 98324         pRight = pLoop;
 98326       mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
 98327       if( mxSelect && cnt>mxSelect ){
 98328         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
 98329         goto select_end;
 98332     rc = multiSelect(pParse, p, pDest);
 98333     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
 98334     return rc;
 98336 #endif
 98338   /* If there is both a GROUP BY and an ORDER BY clause and they are
 98339   ** identical, then disable the ORDER BY clause since the GROUP BY
 98340   ** will cause elements to come out in the correct order.  This is
 98341   ** an optimization - the correct answer should result regardless.
 98342   ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
 98343   ** to disable this optimization for testing purposes.
 98344   */
 98345   if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
 98346          && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
 98347     pOrderBy = 0;
 98350   /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
 98351   ** if the select-list is the same as the ORDER BY list, then this query
 98352   ** can be rewritten as a GROUP BY. In other words, this:
 98353   **
 98354   **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
 98355   **
 98356   ** is transformed to:
 98357   **
 98358   **     SELECT xyz FROM ... GROUP BY xyz
 98359   **
 98360   ** The second form is preferred as a single index (or temp-table) may be 
 98361   ** used for both the ORDER BY and DISTINCT processing. As originally 
 98362   ** written the query must use a temp-table for at least one of the ORDER 
 98363   ** BY and DISTINCT, and an index or separate temp-table for the other.
 98364   */
 98365   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
 98366    && sqlite3ExprListCompare(pOrderBy, p->pEList)==0
 98367   ){
 98368     p->selFlags &= ~SF_Distinct;
 98369     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
 98370     pGroupBy = p->pGroupBy;
 98371     pOrderBy = 0;
 98372     /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
 98373     ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
 98374     ** original setting of the SF_Distinct flag, not the current setting */
 98375     assert( sDistinct.isTnct );
 98378   /* If there is an ORDER BY clause, then this sorting
 98379   ** index might end up being unused if the data can be 
 98380   ** extracted in pre-sorted order.  If that is the case, then the
 98381   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
 98382   ** we figure out that the sorting index is not needed.  The addrSortIndex
 98383   ** variable is used to facilitate that change.
 98384   */
 98385   if( pOrderBy ){
 98386     KeyInfo *pKeyInfo;
 98387     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
 98388     pOrderBy->iECursor = pParse->nTab++;
 98389     p->addrOpenEphm[2] = addrSortIndex =
 98390       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
 98391                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
 98392                            (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 98393   }else{
 98394     addrSortIndex = -1;
 98397   /* If the output is destined for a temporary table, open that table.
 98398   */
 98399   if( pDest->eDest==SRT_EphemTab ){
 98400     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
 98403   /* Set the limiter.
 98404   */
 98405   iEnd = sqlite3VdbeMakeLabel(v);
 98406   p->nSelectRow = (double)LARGEST_INT64;
 98407   computeLimitRegisters(pParse, p, iEnd);
 98408   if( p->iLimit==0 && addrSortIndex>=0 ){
 98409     sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
 98410     p->selFlags |= SF_UseSorter;
 98413   /* Open a virtual index to use for the distinct set.
 98414   */
 98415   if( p->selFlags & SF_Distinct ){
 98416     sDistinct.tabTnct = pParse->nTab++;
 98417     sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
 98418                                 sDistinct.tabTnct, 0, 0,
 98419                                 (char*)keyInfoFromExprList(pParse, p->pEList),
 98420                                 P4_KEYINFO_HANDOFF);
 98421     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 98422     sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
 98423   }else{
 98424     sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
 98427   if( !isAgg && pGroupBy==0 ){
 98428     /* No aggregate functions and no GROUP BY clause */
 98429     ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
 98431     /* Begin the database scan. */
 98432     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
 98433     if( pWInfo==0 ) goto select_end;
 98434     if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
 98435     if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
 98436     if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
 98438     /* If sorting index that was created by a prior OP_OpenEphemeral 
 98439     ** instruction ended up not being needed, then change the OP_OpenEphemeral
 98440     ** into an OP_Noop.
 98441     */
 98442     if( addrSortIndex>=0 && pOrderBy==0 ){
 98443       sqlite3VdbeChangeToNoop(v, addrSortIndex);
 98444       p->addrOpenEphm[2] = -1;
 98447     /* Use the standard inner loop. */
 98448     selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
 98449                     pWInfo->iContinue, pWInfo->iBreak);
 98451     /* End the database scan loop.
 98452     */
 98453     sqlite3WhereEnd(pWInfo);
 98454   }else{
 98455     /* This case when there exist aggregate functions or a GROUP BY clause
 98456     ** or both */
 98457     NameContext sNC;    /* Name context for processing aggregate information */
 98458     int iAMem;          /* First Mem address for storing current GROUP BY */
 98459     int iBMem;          /* First Mem address for previous GROUP BY */
 98460     int iUseFlag;       /* Mem address holding flag indicating that at least
 98461                         ** one row of the input to the aggregator has been
 98462                         ** processed */
 98463     int iAbortFlag;     /* Mem address which causes query abort if positive */
 98464     int groupBySort;    /* Rows come from source in GROUP BY order */
 98465     int addrEnd;        /* End of processing for this SELECT */
 98466     int sortPTab = 0;   /* Pseudotable used to decode sorting results */
 98467     int sortOut = 0;    /* Output register from the sorter */
 98469     /* Remove any and all aliases between the result set and the
 98470     ** GROUP BY clause.
 98471     */
 98472     if( pGroupBy ){
 98473       int k;                        /* Loop counter */
 98474       struct ExprList_item *pItem;  /* For looping over expression in a list */
 98476       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
 98477         pItem->iAlias = 0;
 98479       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
 98480         pItem->iAlias = 0;
 98482       if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
 98483     }else{
 98484       p->nSelectRow = (double)1;
 98488     /* Create a label to jump to when we want to abort the query */
 98489     addrEnd = sqlite3VdbeMakeLabel(v);
 98491     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
 98492     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
 98493     ** SELECT statement.
 98494     */
 98495     memset(&sNC, 0, sizeof(sNC));
 98496     sNC.pParse = pParse;
 98497     sNC.pSrcList = pTabList;
 98498     sNC.pAggInfo = &sAggInfo;
 98499     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
 98500     sAggInfo.pGroupBy = pGroupBy;
 98501     sqlite3ExprAnalyzeAggList(&sNC, pEList);
 98502     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
 98503     if( pHaving ){
 98504       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
 98506     sAggInfo.nAccumulator = sAggInfo.nColumn;
 98507     for(i=0; i<sAggInfo.nFunc; i++){
 98508       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
 98509       sNC.ncFlags |= NC_InAggFunc;
 98510       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
 98511       sNC.ncFlags &= ~NC_InAggFunc;
 98513     if( db->mallocFailed ) goto select_end;
 98515     /* Processing for aggregates with GROUP BY is very different and
 98516     ** much more complex than aggregates without a GROUP BY.
 98517     */
 98518     if( pGroupBy ){
 98519       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
 98520       int j1;             /* A-vs-B comparision jump */
 98521       int addrOutputRow;  /* Start of subroutine that outputs a result row */
 98522       int regOutputRow;   /* Return address register for output subroutine */
 98523       int addrSetAbort;   /* Set the abort flag and return */
 98524       int addrTopOfLoop;  /* Top of the input loop */
 98525       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
 98526       int addrReset;      /* Subroutine for resetting the accumulator */
 98527       int regReset;       /* Return address register for reset subroutine */
 98529       /* If there is a GROUP BY clause we might need a sorting index to
 98530       ** implement it.  Allocate that sorting index now.  If it turns out
 98531       ** that we do not need it after all, the OP_SorterOpen instruction
 98532       ** will be converted into a Noop.  
 98533       */
 98534       sAggInfo.sortingIdx = pParse->nTab++;
 98535       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
 98536       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
 98537           sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
 98538           0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 98540       /* Initialize memory locations used by GROUP BY aggregate processing
 98541       */
 98542       iUseFlag = ++pParse->nMem;
 98543       iAbortFlag = ++pParse->nMem;
 98544       regOutputRow = ++pParse->nMem;
 98545       addrOutputRow = sqlite3VdbeMakeLabel(v);
 98546       regReset = ++pParse->nMem;
 98547       addrReset = sqlite3VdbeMakeLabel(v);
 98548       iAMem = pParse->nMem + 1;
 98549       pParse->nMem += pGroupBy->nExpr;
 98550       iBMem = pParse->nMem + 1;
 98551       pParse->nMem += pGroupBy->nExpr;
 98552       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
 98553       VdbeComment((v, "clear abort flag"));
 98554       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
 98555       VdbeComment((v, "indicate accumulator empty"));
 98556       sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
 98558       /* Begin a loop that will extract all source rows in GROUP BY order.
 98559       ** This might involve two separate loops with an OP_Sort in between, or
 98560       ** it might be a single loop that uses an index to extract information
 98561       ** in the right order to begin with.
 98562       */
 98563       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
 98564       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
 98565       if( pWInfo==0 ) goto select_end;
 98566       if( pWInfo->nOBSat==pGroupBy->nExpr ){
 98567         /* The optimizer is able to deliver rows in group by order so
 98568         ** we do not have to sort.  The OP_OpenEphemeral table will be
 98569         ** cancelled later because we still need to use the pKeyInfo
 98570         */
 98571         groupBySort = 0;
 98572       }else{
 98573         /* Rows are coming out in undetermined order.  We have to push
 98574         ** each row into a sorting index, terminate the first loop,
 98575         ** then loop over the sorting index in order to get the output
 98576         ** in sorted order
 98577         */
 98578         int regBase;
 98579         int regRecord;
 98580         int nCol;
 98581         int nGroupBy;
 98583         explainTempTable(pParse, 
 98584             (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
 98585                     "DISTINCT" : "GROUP BY");
 98587         groupBySort = 1;
 98588         nGroupBy = pGroupBy->nExpr;
 98589         nCol = nGroupBy + 1;
 98590         j = nGroupBy+1;
 98591         for(i=0; i<sAggInfo.nColumn; i++){
 98592           if( sAggInfo.aCol[i].iSorterColumn>=j ){
 98593             nCol++;
 98594             j++;
 98597         regBase = sqlite3GetTempRange(pParse, nCol);
 98598         sqlite3ExprCacheClear(pParse);
 98599         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
 98600         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
 98601         j = nGroupBy+1;
 98602         for(i=0; i<sAggInfo.nColumn; i++){
 98603           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
 98604           if( pCol->iSorterColumn>=j ){
 98605             int r1 = j + regBase;
 98606             int r2;
 98608             r2 = sqlite3ExprCodeGetColumn(pParse, 
 98609                                pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
 98610             if( r1!=r2 ){
 98611               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
 98613             j++;
 98616         regRecord = sqlite3GetTempReg(pParse);
 98617         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
 98618         sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
 98619         sqlite3ReleaseTempReg(pParse, regRecord);
 98620         sqlite3ReleaseTempRange(pParse, regBase, nCol);
 98621         sqlite3WhereEnd(pWInfo);
 98622         sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
 98623         sortOut = sqlite3GetTempReg(pParse);
 98624         sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
 98625         sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
 98626         VdbeComment((v, "GROUP BY sort"));
 98627         sAggInfo.useSortingIdx = 1;
 98628         sqlite3ExprCacheClear(pParse);
 98631       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
 98632       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
 98633       ** Then compare the current GROUP BY terms against the GROUP BY terms
 98634       ** from the previous row currently stored in a0, a1, a2...
 98635       */
 98636       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
 98637       sqlite3ExprCacheClear(pParse);
 98638       if( groupBySort ){
 98639         sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
 98641       for(j=0; j<pGroupBy->nExpr; j++){
 98642         if( groupBySort ){
 98643           sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
 98644           if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 98645         }else{
 98646           sAggInfo.directMode = 1;
 98647           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
 98650       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
 98651                           (char*)pKeyInfo, P4_KEYINFO);
 98652       j1 = sqlite3VdbeCurrentAddr(v);
 98653       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
 98655       /* Generate code that runs whenever the GROUP BY changes.
 98656       ** Changes in the GROUP BY are detected by the previous code
 98657       ** block.  If there were no changes, this block is skipped.
 98658       **
 98659       ** This code copies current group by terms in b0,b1,b2,...
 98660       ** over to a0,a1,a2.  It then calls the output subroutine
 98661       ** and resets the aggregate accumulator registers in preparation
 98662       ** for the next GROUP BY batch.
 98663       */
 98664       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
 98665       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
 98666       VdbeComment((v, "output one row"));
 98667       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
 98668       VdbeComment((v, "check abort flag"));
 98669       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
 98670       VdbeComment((v, "reset accumulator"));
 98672       /* Update the aggregate accumulators based on the content of
 98673       ** the current row
 98674       */
 98675       sqlite3VdbeJumpHere(v, j1);
 98676       updateAccumulator(pParse, &sAggInfo);
 98677       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
 98678       VdbeComment((v, "indicate data in accumulator"));
 98680       /* End of the loop
 98681       */
 98682       if( groupBySort ){
 98683         sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
 98684       }else{
 98685         sqlite3WhereEnd(pWInfo);
 98686         sqlite3VdbeChangeToNoop(v, addrSortingIdx);
 98689       /* Output the final row of result
 98690       */
 98691       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
 98692       VdbeComment((v, "output final row"));
 98694       /* Jump over the subroutines
 98695       */
 98696       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
 98698       /* Generate a subroutine that outputs a single row of the result
 98699       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
 98700       ** is less than or equal to zero, the subroutine is a no-op.  If
 98701       ** the processing calls for the query to abort, this subroutine
 98702       ** increments the iAbortFlag memory location before returning in
 98703       ** order to signal the caller to abort.
 98704       */
 98705       addrSetAbort = sqlite3VdbeCurrentAddr(v);
 98706       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
 98707       VdbeComment((v, "set abort flag"));
 98708       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 98709       sqlite3VdbeResolveLabel(v, addrOutputRow);
 98710       addrOutputRow = sqlite3VdbeCurrentAddr(v);
 98711       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
 98712       VdbeComment((v, "Groupby result generator entry point"));
 98713       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 98714       finalizeAggFunctions(pParse, &sAggInfo);
 98715       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
 98716       selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
 98717                       &sDistinct, pDest,
 98718                       addrOutputRow+1, addrSetAbort);
 98719       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 98720       VdbeComment((v, "end groupby result generator"));
 98722       /* Generate a subroutine that will reset the group-by accumulator
 98723       */
 98724       sqlite3VdbeResolveLabel(v, addrReset);
 98725       resetAccumulator(pParse, &sAggInfo);
 98726       sqlite3VdbeAddOp1(v, OP_Return, regReset);
 98728     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
 98729     else {
 98730       ExprList *pDel = 0;
 98731 #ifndef SQLITE_OMIT_BTREECOUNT
 98732       Table *pTab;
 98733       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
 98734         /* If isSimpleCount() returns a pointer to a Table structure, then
 98735         ** the SQL statement is of the form:
 98736         **
 98737         **   SELECT count(*) FROM <tbl>
 98738         **
 98739         ** where the Table structure returned represents table <tbl>.
 98740         **
 98741         ** This statement is so common that it is optimized specially. The
 98742         ** OP_Count instruction is executed either on the intkey table that
 98743         ** contains the data for table <tbl> or on one of its indexes. It
 98744         ** is better to execute the op on an index, as indexes are almost
 98745         ** always spread across less pages than their corresponding tables.
 98746         */
 98747         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 98748         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
 98749         Index *pIdx;                         /* Iterator variable */
 98750         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
 98751         Index *pBest = 0;                    /* Best index found so far */
 98752         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
 98754         sqlite3CodeVerifySchema(pParse, iDb);
 98755         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 98757         /* Search for the index that has the least amount of columns. If
 98758         ** there is such an index, and it has less columns than the table
 98759         ** does, then we can assume that it consumes less space on disk and
 98760         ** will therefore be cheaper to scan to determine the query result.
 98761         ** In this case set iRoot to the root page number of the index b-tree
 98762         ** and pKeyInfo to the KeyInfo structure required to navigate the
 98763         ** index.
 98764         **
 98765         ** (2011-04-15) Do not do a full scan of an unordered index.
 98766         **
 98767         ** In practice the KeyInfo structure will not be used. It is only 
 98768         ** passed to keep OP_OpenRead happy.
 98769         */
 98770         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 98771           if( pIdx->bUnordered==0 && (!pBest || pIdx->nColumn<pBest->nColumn) ){
 98772             pBest = pIdx;
 98775         if( pBest && pBest->nColumn<pTab->nCol ){
 98776           iRoot = pBest->tnum;
 98777           pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
 98780         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
 98781         sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
 98782         if( pKeyInfo ){
 98783           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
 98785         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
 98786         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
 98787         explainSimpleCount(pParse, pTab, pBest);
 98788       }else
 98789 #endif /* SQLITE_OMIT_BTREECOUNT */
 98791         /* Check if the query is of one of the following forms:
 98792         **
 98793         **   SELECT min(x) FROM ...
 98794         **   SELECT max(x) FROM ...
 98795         **
 98796         ** If it is, then ask the code in where.c to attempt to sort results
 98797         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
 98798         ** If where.c is able to produce results sorted in this order, then
 98799         ** add vdbe code to break out of the processing loop after the 
 98800         ** first iteration (since the first iteration of the loop is 
 98801         ** guaranteed to operate on the row with the minimum or maximum 
 98802         ** value of x, the only row required).
 98803         **
 98804         ** A special flag must be passed to sqlite3WhereBegin() to slightly
 98805         ** modify behaviour as follows:
 98806         **
 98807         **   + If the query is a "SELECT min(x)", then the loop coded by
 98808         **     where.c should not iterate over any values with a NULL value
 98809         **     for x.
 98810         **
 98811         **   + The optimizer code in where.c (the thing that decides which
 98812         **     index or indices to use) should place a different priority on 
 98813         **     satisfying the 'ORDER BY' clause than it does in other cases.
 98814         **     Refer to code and comments in where.c for details.
 98815         */
 98816         ExprList *pMinMax = 0;
 98817         u8 flag = minMaxQuery(p);
 98818         if( flag ){
 98819           assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
 98820           assert( p->pEList->a[0].pExpr->x.pList->nExpr==1 );
 98821           pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
 98822           pDel = pMinMax;
 98823           if( pMinMax && !db->mallocFailed ){
 98824             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
 98825             pMinMax->a[0].pExpr->op = TK_COLUMN;
 98829         /* This case runs if the aggregate has no GROUP BY clause.  The
 98830         ** processing is much simpler since there is only a single row
 98831         ** of output.
 98832         */
 98833         resetAccumulator(pParse, &sAggInfo);
 98834         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
 98835         if( pWInfo==0 ){
 98836           sqlite3ExprListDelete(db, pDel);
 98837           goto select_end;
 98839         updateAccumulator(pParse, &sAggInfo);
 98840         assert( pMinMax==0 || pMinMax->nExpr==1 );
 98841         if( pWInfo->nOBSat>0 ){
 98842           sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
 98843           VdbeComment((v, "%s() by index",
 98844                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
 98846         sqlite3WhereEnd(pWInfo);
 98847         finalizeAggFunctions(pParse, &sAggInfo);
 98850       pOrderBy = 0;
 98851       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
 98852       selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0, 
 98853                       pDest, addrEnd, addrEnd);
 98854       sqlite3ExprListDelete(db, pDel);
 98856     sqlite3VdbeResolveLabel(v, addrEnd);
 98858   } /* endif aggregate query */
 98860   if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
 98861     explainTempTable(pParse, "DISTINCT");
 98864   /* If there is an ORDER BY clause, then we need to sort the results
 98865   ** and send them to the callback one by one.
 98866   */
 98867   if( pOrderBy ){
 98868     explainTempTable(pParse, "ORDER BY");
 98869     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
 98872   /* Jump here to skip this query
 98873   */
 98874   sqlite3VdbeResolveLabel(v, iEnd);
 98876   /* The SELECT was successfully coded.   Set the return code to 0
 98877   ** to indicate no errors.
 98878   */
 98879   rc = 0;
 98881   /* Control jumps to here if an error is encountered above, or upon
 98882   ** successful coding of the SELECT.
 98883   */
 98884 select_end:
 98885   explainSetInteger(pParse->iSelectId, iRestoreSelectId);
 98887   /* Identify column names if results of the SELECT are to be output.
 98888   */
 98889   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
 98890     generateColumnNames(pParse, pTabList, pEList);
 98893   sqlite3DbFree(db, sAggInfo.aCol);
 98894   sqlite3DbFree(db, sAggInfo.aFunc);
 98895   return rc;
 98898 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 98899 /*
 98900 ** Generate a human-readable description of a the Select object.
 98901 */
 98902 static void explainOneSelect(Vdbe *pVdbe, Select *p){
 98903   sqlite3ExplainPrintf(pVdbe, "SELECT ");
 98904   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 98905     if( p->selFlags & SF_Distinct ){
 98906       sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
 98908     if( p->selFlags & SF_Aggregate ){
 98909       sqlite3ExplainPrintf(pVdbe, "agg_flag ");
 98911     sqlite3ExplainNL(pVdbe);
 98912     sqlite3ExplainPrintf(pVdbe, "   ");
 98914   sqlite3ExplainExprList(pVdbe, p->pEList);
 98915   sqlite3ExplainNL(pVdbe);
 98916   if( p->pSrc && p->pSrc->nSrc ){
 98917     int i;
 98918     sqlite3ExplainPrintf(pVdbe, "FROM ");
 98919     sqlite3ExplainPush(pVdbe);
 98920     for(i=0; i<p->pSrc->nSrc; i++){
 98921       struct SrcList_item *pItem = &p->pSrc->a[i];
 98922       sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
 98923       if( pItem->pSelect ){
 98924         sqlite3ExplainSelect(pVdbe, pItem->pSelect);
 98925         if( pItem->pTab ){
 98926           sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
 98928       }else if( pItem->zName ){
 98929         sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
 98931       if( pItem->zAlias ){
 98932         sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
 98934       if( pItem->jointype & JT_LEFT ){
 98935         sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
 98937       sqlite3ExplainNL(pVdbe);
 98939     sqlite3ExplainPop(pVdbe);
 98941   if( p->pWhere ){
 98942     sqlite3ExplainPrintf(pVdbe, "WHERE ");
 98943     sqlite3ExplainExpr(pVdbe, p->pWhere);
 98944     sqlite3ExplainNL(pVdbe);
 98946   if( p->pGroupBy ){
 98947     sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
 98948     sqlite3ExplainExprList(pVdbe, p->pGroupBy);
 98949     sqlite3ExplainNL(pVdbe);
 98951   if( p->pHaving ){
 98952     sqlite3ExplainPrintf(pVdbe, "HAVING ");
 98953     sqlite3ExplainExpr(pVdbe, p->pHaving);
 98954     sqlite3ExplainNL(pVdbe);
 98956   if( p->pOrderBy ){
 98957     sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
 98958     sqlite3ExplainExprList(pVdbe, p->pOrderBy);
 98959     sqlite3ExplainNL(pVdbe);
 98961   if( p->pLimit ){
 98962     sqlite3ExplainPrintf(pVdbe, "LIMIT ");
 98963     sqlite3ExplainExpr(pVdbe, p->pLimit);
 98964     sqlite3ExplainNL(pVdbe);
 98966   if( p->pOffset ){
 98967     sqlite3ExplainPrintf(pVdbe, "OFFSET ");
 98968     sqlite3ExplainExpr(pVdbe, p->pOffset);
 98969     sqlite3ExplainNL(pVdbe);
 98972 SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
 98973   if( p==0 ){
 98974     sqlite3ExplainPrintf(pVdbe, "(null-select)");
 98975     return;
 98977   while( p->pPrior ) p = p->pPrior;
 98978   sqlite3ExplainPush(pVdbe);
 98979   while( p ){
 98980     explainOneSelect(pVdbe, p);
 98981     p = p->pNext;
 98982     if( p==0 ) break;
 98983     sqlite3ExplainNL(pVdbe);
 98984     sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
 98986   sqlite3ExplainPrintf(pVdbe, "END");
 98987   sqlite3ExplainPop(pVdbe);
 98990 /* End of the structure debug printing code
 98991 *****************************************************************************/
 98992 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
 98994 /************** End of select.c **********************************************/
 98995 /************** Begin file table.c *******************************************/
 98996 /*
 98997 ** 2001 September 15
 98998 **
 98999 ** The author disclaims copyright to this source code.  In place of
 99000 ** a legal notice, here is a blessing:
 99001 **
 99002 **    May you do good and not evil.
 99003 **    May you find forgiveness for yourself and forgive others.
 99004 **    May you share freely, never taking more than you give.
 99005 **
 99006 *************************************************************************
 99007 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
 99008 ** interface routines.  These are just wrappers around the main
 99009 ** interface routine of sqlite3_exec().
 99010 **
 99011 ** These routines are in a separate files so that they will not be linked
 99012 ** if they are not used.
 99013 */
 99014 /* #include <stdlib.h> */
 99015 /* #include <string.h> */
 99017 #ifndef SQLITE_OMIT_GET_TABLE
 99019 /*
 99020 ** This structure is used to pass data from sqlite3_get_table() through
 99021 ** to the callback function is uses to build the result.
 99022 */
 99023 typedef struct TabResult {
 99024   char **azResult;   /* Accumulated output */
 99025   char *zErrMsg;     /* Error message text, if an error occurs */
 99026   int nAlloc;        /* Slots allocated for azResult[] */
 99027   int nRow;          /* Number of rows in the result */
 99028   int nColumn;       /* Number of columns in the result */
 99029   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
 99030   int rc;            /* Return code from sqlite3_exec() */
 99031 } TabResult;
 99033 /*
 99034 ** This routine is called once for each row in the result table.  Its job
 99035 ** is to fill in the TabResult structure appropriately, allocating new
 99036 ** memory as necessary.
 99037 */
 99038 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
 99039   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
 99040   int need;                         /* Slots needed in p->azResult[] */
 99041   int i;                            /* Loop counter */
 99042   char *z;                          /* A single column of result */
 99044   /* Make sure there is enough space in p->azResult to hold everything
 99045   ** we need to remember from this invocation of the callback.
 99046   */
 99047   if( p->nRow==0 && argv!=0 ){
 99048     need = nCol*2;
 99049   }else{
 99050     need = nCol;
 99052   if( p->nData + need > p->nAlloc ){
 99053     char **azNew;
 99054     p->nAlloc = p->nAlloc*2 + need;
 99055     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
 99056     if( azNew==0 ) goto malloc_failed;
 99057     p->azResult = azNew;
 99060   /* If this is the first row, then generate an extra row containing
 99061   ** the names of all columns.
 99062   */
 99063   if( p->nRow==0 ){
 99064     p->nColumn = nCol;
 99065     for(i=0; i<nCol; i++){
 99066       z = sqlite3_mprintf("%s", colv[i]);
 99067       if( z==0 ) goto malloc_failed;
 99068       p->azResult[p->nData++] = z;
 99070   }else if( p->nColumn!=nCol ){
 99071     sqlite3_free(p->zErrMsg);
 99072     p->zErrMsg = sqlite3_mprintf(
 99073        "sqlite3_get_table() called with two or more incompatible queries"
 99074     );
 99075     p->rc = SQLITE_ERROR;
 99076     return 1;
 99079   /* Copy over the row data
 99080   */
 99081   if( argv!=0 ){
 99082     for(i=0; i<nCol; i++){
 99083       if( argv[i]==0 ){
 99084         z = 0;
 99085       }else{
 99086         int n = sqlite3Strlen30(argv[i])+1;
 99087         z = sqlite3_malloc( n );
 99088         if( z==0 ) goto malloc_failed;
 99089         memcpy(z, argv[i], n);
 99091       p->azResult[p->nData++] = z;
 99093     p->nRow++;
 99095   return 0;
 99097 malloc_failed:
 99098   p->rc = SQLITE_NOMEM;
 99099   return 1;
 99102 /*
 99103 ** Query the database.  But instead of invoking a callback for each row,
 99104 ** malloc() for space to hold the result and return the entire results
 99105 ** at the conclusion of the call.
 99106 **
 99107 ** The result that is written to ***pazResult is held in memory obtained
 99108 ** from malloc().  But the caller cannot free this memory directly.  
 99109 ** Instead, the entire table should be passed to sqlite3_free_table() when
 99110 ** the calling procedure is finished using it.
 99111 */
 99112 SQLITE_API int sqlite3_get_table(
 99113   sqlite3 *db,                /* The database on which the SQL executes */
 99114   const char *zSql,           /* The SQL to be executed */
 99115   char ***pazResult,          /* Write the result table here */
 99116   int *pnRow,                 /* Write the number of rows in the result here */
 99117   int *pnColumn,              /* Write the number of columns of result here */
 99118   char **pzErrMsg             /* Write error messages here */
 99119 ){
 99120   int rc;
 99121   TabResult res;
 99123   *pazResult = 0;
 99124   if( pnColumn ) *pnColumn = 0;
 99125   if( pnRow ) *pnRow = 0;
 99126   if( pzErrMsg ) *pzErrMsg = 0;
 99127   res.zErrMsg = 0;
 99128   res.nRow = 0;
 99129   res.nColumn = 0;
 99130   res.nData = 1;
 99131   res.nAlloc = 20;
 99132   res.rc = SQLITE_OK;
 99133   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
 99134   if( res.azResult==0 ){
 99135      db->errCode = SQLITE_NOMEM;
 99136      return SQLITE_NOMEM;
 99138   res.azResult[0] = 0;
 99139   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
 99140   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
 99141   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
 99142   if( (rc&0xff)==SQLITE_ABORT ){
 99143     sqlite3_free_table(&res.azResult[1]);
 99144     if( res.zErrMsg ){
 99145       if( pzErrMsg ){
 99146         sqlite3_free(*pzErrMsg);
 99147         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
 99149       sqlite3_free(res.zErrMsg);
 99151     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
 99152     return res.rc;
 99154   sqlite3_free(res.zErrMsg);
 99155   if( rc!=SQLITE_OK ){
 99156     sqlite3_free_table(&res.azResult[1]);
 99157     return rc;
 99159   if( res.nAlloc>res.nData ){
 99160     char **azNew;
 99161     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
 99162     if( azNew==0 ){
 99163       sqlite3_free_table(&res.azResult[1]);
 99164       db->errCode = SQLITE_NOMEM;
 99165       return SQLITE_NOMEM;
 99167     res.azResult = azNew;
 99169   *pazResult = &res.azResult[1];
 99170   if( pnColumn ) *pnColumn = res.nColumn;
 99171   if( pnRow ) *pnRow = res.nRow;
 99172   return rc;
 99175 /*
 99176 ** This routine frees the space the sqlite3_get_table() malloced.
 99177 */
 99178 SQLITE_API void sqlite3_free_table(
 99179   char **azResult            /* Result returned from from sqlite3_get_table() */
 99180 ){
 99181   if( azResult ){
 99182     int i, n;
 99183     azResult--;
 99184     assert( azResult!=0 );
 99185     n = SQLITE_PTR_TO_INT(azResult[0]);
 99186     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
 99187     sqlite3_free(azResult);
 99191 #endif /* SQLITE_OMIT_GET_TABLE */
 99193 /************** End of table.c ***********************************************/
 99194 /************** Begin file trigger.c *****************************************/
 99195 /*
 99196 **
 99197 ** The author disclaims copyright to this source code.  In place of
 99198 ** a legal notice, here is a blessing:
 99199 **
 99200 **    May you do good and not evil.
 99201 **    May you find forgiveness for yourself and forgive others.
 99202 **    May you share freely, never taking more than you give.
 99203 **
 99204 *************************************************************************
 99205 ** This file contains the implementation for TRIGGERs
 99206 */
 99208 #ifndef SQLITE_OMIT_TRIGGER
 99209 /*
 99210 ** Delete a linked list of TriggerStep structures.
 99211 */
 99212 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
 99213   while( pTriggerStep ){
 99214     TriggerStep * pTmp = pTriggerStep;
 99215     pTriggerStep = pTriggerStep->pNext;
 99217     sqlite3ExprDelete(db, pTmp->pWhere);
 99218     sqlite3ExprListDelete(db, pTmp->pExprList);
 99219     sqlite3SelectDelete(db, pTmp->pSelect);
 99220     sqlite3IdListDelete(db, pTmp->pIdList);
 99222     sqlite3DbFree(db, pTmp);
 99226 /*
 99227 ** Given table pTab, return a list of all the triggers attached to 
 99228 ** the table. The list is connected by Trigger.pNext pointers.
 99229 **
 99230 ** All of the triggers on pTab that are in the same database as pTab
 99231 ** are already attached to pTab->pTrigger.  But there might be additional
 99232 ** triggers on pTab in the TEMP schema.  This routine prepends all
 99233 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
 99234 ** and returns the combined list.
 99235 **
 99236 ** To state it another way:  This routine returns a list of all triggers
 99237 ** that fire off of pTab.  The list will include any TEMP triggers on
 99238 ** pTab as well as the triggers lised in pTab->pTrigger.
 99239 */
 99240 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
 99241   Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
 99242   Trigger *pList = 0;                  /* List of triggers to return */
 99244   if( pParse->disableTriggers ){
 99245     return 0;
 99248   if( pTmpSchema!=pTab->pSchema ){
 99249     HashElem *p;
 99250     assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
 99251     for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
 99252       Trigger *pTrig = (Trigger *)sqliteHashData(p);
 99253       if( pTrig->pTabSchema==pTab->pSchema
 99254        && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 
 99255       ){
 99256         pTrig->pNext = (pList ? pList : pTab->pTrigger);
 99257         pList = pTrig;
 99262   return (pList ? pList : pTab->pTrigger);
 99265 /*
 99266 ** This is called by the parser when it sees a CREATE TRIGGER statement
 99267 ** up to the point of the BEGIN before the trigger actions.  A Trigger
 99268 ** structure is generated based on the information available and stored
 99269 ** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
 99270 ** sqlite3FinishTrigger() function is called to complete the trigger
 99271 ** construction process.
 99272 */
 99273 SQLITE_PRIVATE void sqlite3BeginTrigger(
 99274   Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
 99275   Token *pName1,      /* The name of the trigger */
 99276   Token *pName2,      /* The name of the trigger */
 99277   int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
 99278   int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
 99279   IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
 99280   SrcList *pTableName,/* The name of the table/view the trigger applies to */
 99281   Expr *pWhen,        /* WHEN clause */
 99282   int isTemp,         /* True if the TEMPORARY keyword is present */
 99283   int noErr           /* Suppress errors if the trigger already exists */
 99284 ){
 99285   Trigger *pTrigger = 0;  /* The new trigger */
 99286   Table *pTab;            /* Table that the trigger fires off of */
 99287   char *zName = 0;        /* Name of the trigger */
 99288   sqlite3 *db = pParse->db;  /* The database connection */
 99289   int iDb;                /* The database to store the trigger in */
 99290   Token *pName;           /* The unqualified db name */
 99291   DbFixer sFix;           /* State vector for the DB fixer */
 99292   int iTabDb;             /* Index of the database holding pTab */
 99294   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
 99295   assert( pName2!=0 );
 99296   assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
 99297   assert( op>0 && op<0xff );
 99298   if( isTemp ){
 99299     /* If TEMP was specified, then the trigger name may not be qualified. */
 99300     if( pName2->n>0 ){
 99301       sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
 99302       goto trigger_cleanup;
 99304     iDb = 1;
 99305     pName = pName1;
 99306   }else{
 99307     /* Figure out the db that the trigger will be created in */
 99308     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 99309     if( iDb<0 ){
 99310       goto trigger_cleanup;
 99313   if( !pTableName || db->mallocFailed ){
 99314     goto trigger_cleanup;
 99317   /* A long-standing parser bug is that this syntax was allowed:
 99318   **
 99319   **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
 99320   **                                                 ^^^^^^^^
 99321   **
 99322   ** To maintain backwards compatibility, ignore the database
 99323   ** name on pTableName if we are reparsing our of SQLITE_MASTER.
 99324   */
 99325   if( db->init.busy && iDb!=1 ){
 99326     sqlite3DbFree(db, pTableName->a[0].zDatabase);
 99327     pTableName->a[0].zDatabase = 0;
 99330   /* If the trigger name was unqualified, and the table is a temp table,
 99331   ** then set iDb to 1 to create the trigger in the temporary database.
 99332   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
 99333   ** exist, the error is caught by the block below.
 99334   */
 99335   pTab = sqlite3SrcListLookup(pParse, pTableName);
 99336   if( db->init.busy==0 && pName2->n==0 && pTab
 99337         && pTab->pSchema==db->aDb[1].pSchema ){
 99338     iDb = 1;
 99341   /* Ensure the table name matches database name and that the table exists */
 99342   if( db->mallocFailed ) goto trigger_cleanup;
 99343   assert( pTableName->nSrc==1 );
 99344   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
 99345       sqlite3FixSrcList(&sFix, pTableName) ){
 99346     goto trigger_cleanup;
 99348   pTab = sqlite3SrcListLookup(pParse, pTableName);
 99349   if( !pTab ){
 99350     /* The table does not exist. */
 99351     if( db->init.iDb==1 ){
 99352       /* Ticket #3810.
 99353       ** Normally, whenever a table is dropped, all associated triggers are
 99354       ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
 99355       ** and the table is dropped by a different database connection, the
 99356       ** trigger is not visible to the database connection that does the
 99357       ** drop so the trigger cannot be dropped.  This results in an
 99358       ** "orphaned trigger" - a trigger whose associated table is missing.
 99359       */
 99360       db->init.orphanTrigger = 1;
 99362     goto trigger_cleanup;
 99364   if( IsVirtual(pTab) ){
 99365     sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
 99366     goto trigger_cleanup;
 99369   /* Check that the trigger name is not reserved and that no trigger of the
 99370   ** specified name exists */
 99371   zName = sqlite3NameFromToken(db, pName);
 99372   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 99373     goto trigger_cleanup;
 99375   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 99376   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
 99377                       zName, sqlite3Strlen30(zName)) ){
 99378     if( !noErr ){
 99379       sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
 99380     }else{
 99381       assert( !db->init.busy );
 99382       sqlite3CodeVerifySchema(pParse, iDb);
 99384     goto trigger_cleanup;
 99387   /* Do not create a trigger on a system table */
 99388   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
 99389     sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
 99390     pParse->nErr++;
 99391     goto trigger_cleanup;
 99394   /* INSTEAD of triggers are only for views and views only support INSTEAD
 99395   ** of triggers.
 99396   */
 99397   if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
 99398     sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
 99399         (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
 99400     goto trigger_cleanup;
 99402   if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
 99403     sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
 99404         " trigger on table: %S", pTableName, 0);
 99405     goto trigger_cleanup;
 99407   iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 99409 #ifndef SQLITE_OMIT_AUTHORIZATION
 99411     int code = SQLITE_CREATE_TRIGGER;
 99412     const char *zDb = db->aDb[iTabDb].zName;
 99413     const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
 99414     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
 99415     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
 99416       goto trigger_cleanup;
 99418     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
 99419       goto trigger_cleanup;
 99422 #endif
 99424   /* INSTEAD OF triggers can only appear on views and BEFORE triggers
 99425   ** cannot appear on views.  So we might as well translate every
 99426   ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
 99427   ** elsewhere.
 99428   */
 99429   if (tr_tm == TK_INSTEAD){
 99430     tr_tm = TK_BEFORE;
 99433   /* Build the Trigger object */
 99434   pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
 99435   if( pTrigger==0 ) goto trigger_cleanup;
 99436   pTrigger->zName = zName;
 99437   zName = 0;
 99438   pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
 99439   pTrigger->pSchema = db->aDb[iDb].pSchema;
 99440   pTrigger->pTabSchema = pTab->pSchema;
 99441   pTrigger->op = (u8)op;
 99442   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
 99443   pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
 99444   pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
 99445   assert( pParse->pNewTrigger==0 );
 99446   pParse->pNewTrigger = pTrigger;
 99448 trigger_cleanup:
 99449   sqlite3DbFree(db, zName);
 99450   sqlite3SrcListDelete(db, pTableName);
 99451   sqlite3IdListDelete(db, pColumns);
 99452   sqlite3ExprDelete(db, pWhen);
 99453   if( !pParse->pNewTrigger ){
 99454     sqlite3DeleteTrigger(db, pTrigger);
 99455   }else{
 99456     assert( pParse->pNewTrigger==pTrigger );
 99460 /*
 99461 ** This routine is called after all of the trigger actions have been parsed
 99462 ** in order to complete the process of building the trigger.
 99463 */
 99464 SQLITE_PRIVATE void sqlite3FinishTrigger(
 99465   Parse *pParse,          /* Parser context */
 99466   TriggerStep *pStepList, /* The triggered program */
 99467   Token *pAll             /* Token that describes the complete CREATE TRIGGER */
 99468 ){
 99469   Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
 99470   char *zName;                            /* Name of trigger */
 99471   sqlite3 *db = pParse->db;               /* The database */
 99472   DbFixer sFix;                           /* Fixer object */
 99473   int iDb;                                /* Database containing the trigger */
 99474   Token nameToken;                        /* Trigger name for error reporting */
 99476   pParse->pNewTrigger = 0;
 99477   if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
 99478   zName = pTrig->zName;
 99479   iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
 99480   pTrig->step_list = pStepList;
 99481   while( pStepList ){
 99482     pStepList->pTrig = pTrig;
 99483     pStepList = pStepList->pNext;
 99485   nameToken.z = pTrig->zName;
 99486   nameToken.n = sqlite3Strlen30(nameToken.z);
 99487   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 
 99488           && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
 99489     goto triggerfinish_cleanup;
 99492   /* if we are not initializing,
 99493   ** build the sqlite_master entry
 99494   */
 99495   if( !db->init.busy ){
 99496     Vdbe *v;
 99497     char *z;
 99499     /* Make an entry in the sqlite_master table */
 99500     v = sqlite3GetVdbe(pParse);
 99501     if( v==0 ) goto triggerfinish_cleanup;
 99502     sqlite3BeginWriteOperation(pParse, 0, iDb);
 99503     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
 99504     sqlite3NestedParse(pParse,
 99505        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
 99506        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
 99507        pTrig->table, z);
 99508     sqlite3DbFree(db, z);
 99509     sqlite3ChangeCookie(pParse, iDb);
 99510     sqlite3VdbeAddParseSchemaOp(v, iDb,
 99511         sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
 99514   if( db->init.busy ){
 99515     Trigger *pLink = pTrig;
 99516     Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
 99517     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 99518     pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
 99519     if( pTrig ){
 99520       db->mallocFailed = 1;
 99521     }else if( pLink->pSchema==pLink->pTabSchema ){
 99522       Table *pTab;
 99523       int n = sqlite3Strlen30(pLink->table);
 99524       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
 99525       assert( pTab!=0 );
 99526       pLink->pNext = pTab->pTrigger;
 99527       pTab->pTrigger = pLink;
 99531 triggerfinish_cleanup:
 99532   sqlite3DeleteTrigger(db, pTrig);
 99533   assert( !pParse->pNewTrigger );
 99534   sqlite3DeleteTriggerStep(db, pStepList);
 99537 /*
 99538 ** Turn a SELECT statement (that the pSelect parameter points to) into
 99539 ** a trigger step.  Return a pointer to a TriggerStep structure.
 99540 **
 99541 ** The parser calls this routine when it finds a SELECT statement in
 99542 ** body of a TRIGGER.  
 99543 */
 99544 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
 99545   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
 99546   if( pTriggerStep==0 ) {
 99547     sqlite3SelectDelete(db, pSelect);
 99548     return 0;
 99550   pTriggerStep->op = TK_SELECT;
 99551   pTriggerStep->pSelect = pSelect;
 99552   pTriggerStep->orconf = OE_Default;
 99553   return pTriggerStep;
 99556 /*
 99557 ** Allocate space to hold a new trigger step.  The allocated space
 99558 ** holds both the TriggerStep object and the TriggerStep.target.z string.
 99559 **
 99560 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
 99561 */
 99562 static TriggerStep *triggerStepAllocate(
 99563   sqlite3 *db,                /* Database connection */
 99564   u8 op,                      /* Trigger opcode */
 99565   Token *pName                /* The target name */
 99566 ){
 99567   TriggerStep *pTriggerStep;
 99569   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
 99570   if( pTriggerStep ){
 99571     char *z = (char*)&pTriggerStep[1];
 99572     memcpy(z, pName->z, pName->n);
 99573     pTriggerStep->target.z = z;
 99574     pTriggerStep->target.n = pName->n;
 99575     pTriggerStep->op = op;
 99577   return pTriggerStep;
 99580 /*
 99581 ** Build a trigger step out of an INSERT statement.  Return a pointer
 99582 ** to the new trigger step.
 99583 **
 99584 ** The parser calls this routine when it sees an INSERT inside the
 99585 ** body of a trigger.
 99586 */
 99587 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
 99588   sqlite3 *db,        /* The database connection */
 99589   Token *pTableName,  /* Name of the table into which we insert */
 99590   IdList *pColumn,    /* List of columns in pTableName to insert into */
 99591   ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
 99592   Select *pSelect,    /* A SELECT statement that supplies values */
 99593   u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
 99594 ){
 99595   TriggerStep *pTriggerStep;
 99597   assert(pEList == 0 || pSelect == 0);
 99598   assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
 99600   pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
 99601   if( pTriggerStep ){
 99602     pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 99603     pTriggerStep->pIdList = pColumn;
 99604     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
 99605     pTriggerStep->orconf = orconf;
 99606   }else{
 99607     sqlite3IdListDelete(db, pColumn);
 99609   sqlite3ExprListDelete(db, pEList);
 99610   sqlite3SelectDelete(db, pSelect);
 99612   return pTriggerStep;
 99615 /*
 99616 ** Construct a trigger step that implements an UPDATE statement and return
 99617 ** a pointer to that trigger step.  The parser calls this routine when it
 99618 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
 99619 */
 99620 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
 99621   sqlite3 *db,         /* The database connection */
 99622   Token *pTableName,   /* Name of the table to be updated */
 99623   ExprList *pEList,    /* The SET clause: list of column and new values */
 99624   Expr *pWhere,        /* The WHERE clause */
 99625   u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
 99626 ){
 99627   TriggerStep *pTriggerStep;
 99629   pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
 99630   if( pTriggerStep ){
 99631     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
 99632     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 99633     pTriggerStep->orconf = orconf;
 99635   sqlite3ExprListDelete(db, pEList);
 99636   sqlite3ExprDelete(db, pWhere);
 99637   return pTriggerStep;
 99640 /*
 99641 ** Construct a trigger step that implements a DELETE statement and return
 99642 ** a pointer to that trigger step.  The parser calls this routine when it
 99643 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
 99644 */
 99645 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
 99646   sqlite3 *db,            /* Database connection */
 99647   Token *pTableName,      /* The table from which rows are deleted */
 99648   Expr *pWhere            /* The WHERE clause */
 99649 ){
 99650   TriggerStep *pTriggerStep;
 99652   pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
 99653   if( pTriggerStep ){
 99654     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 99655     pTriggerStep->orconf = OE_Default;
 99657   sqlite3ExprDelete(db, pWhere);
 99658   return pTriggerStep;
 99661 /* 
 99662 ** Recursively delete a Trigger structure
 99663 */
 99664 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
 99665   if( pTrigger==0 ) return;
 99666   sqlite3DeleteTriggerStep(db, pTrigger->step_list);
 99667   sqlite3DbFree(db, pTrigger->zName);
 99668   sqlite3DbFree(db, pTrigger->table);
 99669   sqlite3ExprDelete(db, pTrigger->pWhen);
 99670   sqlite3IdListDelete(db, pTrigger->pColumns);
 99671   sqlite3DbFree(db, pTrigger);
 99674 /*
 99675 ** This function is called to drop a trigger from the database schema. 
 99676 **
 99677 ** This may be called directly from the parser and therefore identifies
 99678 ** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
 99679 ** same job as this routine except it takes a pointer to the trigger
 99680 ** instead of the trigger name.
 99681 **/
 99682 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
 99683   Trigger *pTrigger = 0;
 99684   int i;
 99685   const char *zDb;
 99686   const char *zName;
 99687   int nName;
 99688   sqlite3 *db = pParse->db;
 99690   if( db->mallocFailed ) goto drop_trigger_cleanup;
 99691   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 99692     goto drop_trigger_cleanup;
 99695   assert( pName->nSrc==1 );
 99696   zDb = pName->a[0].zDatabase;
 99697   zName = pName->a[0].zName;
 99698   nName = sqlite3Strlen30(zName);
 99699   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 99700   for(i=OMIT_TEMPDB; i<db->nDb; i++){
 99701     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 99702     if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
 99703     assert( sqlite3SchemaMutexHeld(db, j, 0) );
 99704     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
 99705     if( pTrigger ) break;
 99707   if( !pTrigger ){
 99708     if( !noErr ){
 99709       sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
 99710     }else{
 99711       sqlite3CodeVerifyNamedSchema(pParse, zDb);
 99713     pParse->checkSchema = 1;
 99714     goto drop_trigger_cleanup;
 99716   sqlite3DropTriggerPtr(pParse, pTrigger);
 99718 drop_trigger_cleanup:
 99719   sqlite3SrcListDelete(db, pName);
 99722 /*
 99723 ** Return a pointer to the Table structure for the table that a trigger
 99724 ** is set on.
 99725 */
 99726 static Table *tableOfTrigger(Trigger *pTrigger){
 99727   int n = sqlite3Strlen30(pTrigger->table);
 99728   return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
 99732 /*
 99733 ** Drop a trigger given a pointer to that trigger. 
 99734 */
 99735 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
 99736   Table   *pTable;
 99737   Vdbe *v;
 99738   sqlite3 *db = pParse->db;
 99739   int iDb;
 99741   iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
 99742   assert( iDb>=0 && iDb<db->nDb );
 99743   pTable = tableOfTrigger(pTrigger);
 99744   assert( pTable );
 99745   assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
 99746 #ifndef SQLITE_OMIT_AUTHORIZATION
 99748     int code = SQLITE_DROP_TRIGGER;
 99749     const char *zDb = db->aDb[iDb].zName;
 99750     const char *zTab = SCHEMA_TABLE(iDb);
 99751     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
 99752     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
 99753       sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
 99754       return;
 99757 #endif
 99759   /* Generate code to destroy the database record of the trigger.
 99760   */
 99761   assert( pTable!=0 );
 99762   if( (v = sqlite3GetVdbe(pParse))!=0 ){
 99763     int base;
 99764     static const VdbeOpList dropTrigger[] = {
 99765       { OP_Rewind,     0, ADDR(9),  0},
 99766       { OP_String8,    0, 1,        0}, /* 1 */
 99767       { OP_Column,     0, 1,        2},
 99768       { OP_Ne,         2, ADDR(8),  1},
 99769       { OP_String8,    0, 1,        0}, /* 4: "trigger" */
 99770       { OP_Column,     0, 0,        2},
 99771       { OP_Ne,         2, ADDR(8),  1},
 99772       { OP_Delete,     0, 0,        0},
 99773       { OP_Next,       0, ADDR(1),  0}, /* 8 */
 99774     };
 99776     sqlite3BeginWriteOperation(pParse, 0, iDb);
 99777     sqlite3OpenMasterTable(pParse, iDb);
 99778     base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
 99779     sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
 99780     sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
 99781     sqlite3ChangeCookie(pParse, iDb);
 99782     sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
 99783     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
 99784     if( pParse->nMem<3 ){
 99785       pParse->nMem = 3;
 99790 /*
 99791 ** Remove a trigger from the hash tables of the sqlite* pointer.
 99792 */
 99793 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
 99794   Trigger *pTrigger;
 99795   Hash *pHash;
 99797   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 99798   pHash = &(db->aDb[iDb].pSchema->trigHash);
 99799   pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
 99800   if( ALWAYS(pTrigger) ){
 99801     if( pTrigger->pSchema==pTrigger->pTabSchema ){
 99802       Table *pTab = tableOfTrigger(pTrigger);
 99803       Trigger **pp;
 99804       for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
 99805       *pp = (*pp)->pNext;
 99807     sqlite3DeleteTrigger(db, pTrigger);
 99808     db->flags |= SQLITE_InternChanges;
 99812 /*
 99813 ** pEList is the SET clause of an UPDATE statement.  Each entry
 99814 ** in pEList is of the format <id>=<expr>.  If any of the entries
 99815 ** in pEList have an <id> which matches an identifier in pIdList,
 99816 ** then return TRUE.  If pIdList==NULL, then it is considered a
 99817 ** wildcard that matches anything.  Likewise if pEList==NULL then
 99818 ** it matches anything so always return true.  Return false only
 99819 ** if there is no match.
 99820 */
 99821 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
 99822   int e;
 99823   if( pIdList==0 || NEVER(pEList==0) ) return 1;
 99824   for(e=0; e<pEList->nExpr; e++){
 99825     if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
 99827   return 0; 
 99830 /*
 99831 ** Return a list of all triggers on table pTab if there exists at least
 99832 ** one trigger that must be fired when an operation of type 'op' is 
 99833 ** performed on the table, and, if that operation is an UPDATE, if at
 99834 ** least one of the columns in pChanges is being modified.
 99835 */
 99836 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
 99837   Parse *pParse,          /* Parse context */
 99838   Table *pTab,            /* The table the contains the triggers */
 99839   int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
 99840   ExprList *pChanges,     /* Columns that change in an UPDATE statement */
 99841   int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
 99842 ){
 99843   int mask = 0;
 99844   Trigger *pList = 0;
 99845   Trigger *p;
 99847   if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
 99848     pList = sqlite3TriggerList(pParse, pTab);
 99850   assert( pList==0 || IsVirtual(pTab)==0 );
 99851   for(p=pList; p; p=p->pNext){
 99852     if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
 99853       mask |= p->tr_tm;
 99856   if( pMask ){
 99857     *pMask = mask;
 99859   return (mask ? pList : 0);
 99862 /*
 99863 ** Convert the pStep->target token into a SrcList and return a pointer
 99864 ** to that SrcList.
 99865 **
 99866 ** This routine adds a specific database name, if needed, to the target when
 99867 ** forming the SrcList.  This prevents a trigger in one database from
 99868 ** referring to a target in another database.  An exception is when the
 99869 ** trigger is in TEMP in which case it can refer to any other database it
 99870 ** wants.
 99871 */
 99872 static SrcList *targetSrcList(
 99873   Parse *pParse,       /* The parsing context */
 99874   TriggerStep *pStep   /* The trigger containing the target token */
 99875 ){
 99876   int iDb;             /* Index of the database to use */
 99877   SrcList *pSrc;       /* SrcList to be returned */
 99879   pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
 99880   if( pSrc ){
 99881     assert( pSrc->nSrc>0 );
 99882     assert( pSrc->a!=0 );
 99883     iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
 99884     if( iDb==0 || iDb>=2 ){
 99885       sqlite3 *db = pParse->db;
 99886       assert( iDb<pParse->db->nDb );
 99887       pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
 99890   return pSrc;
 99893 /*
 99894 ** Generate VDBE code for the statements inside the body of a single 
 99895 ** trigger.
 99896 */
 99897 static int codeTriggerProgram(
 99898   Parse *pParse,            /* The parser context */
 99899   TriggerStep *pStepList,   /* List of statements inside the trigger body */
 99900   int orconf                /* Conflict algorithm. (OE_Abort, etc) */  
 99901 ){
 99902   TriggerStep *pStep;
 99903   Vdbe *v = pParse->pVdbe;
 99904   sqlite3 *db = pParse->db;
 99906   assert( pParse->pTriggerTab && pParse->pToplevel );
 99907   assert( pStepList );
 99908   assert( v!=0 );
 99909   for(pStep=pStepList; pStep; pStep=pStep->pNext){
 99910     /* Figure out the ON CONFLICT policy that will be used for this step
 99911     ** of the trigger program. If the statement that caused this trigger
 99912     ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
 99913     ** the ON CONFLICT policy that was specified as part of the trigger
 99914     ** step statement. Example:
 99915     **
 99916     **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
 99917     **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
 99918     **   END;
 99919     **
 99920     **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
 99921     **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
 99922     */
 99923     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
 99925     /* Clear the cookieGoto flag. When coding triggers, the cookieGoto 
 99926     ** variable is used as a flag to indicate to sqlite3ExprCodeConstants()
 99927     ** that it is not safe to refactor constants (this happens after the
 99928     ** start of the first loop in the SQL statement is coded - at that 
 99929     ** point code may be conditionally executed, so it is no longer safe to 
 99930     ** initialize constant register values).  */
 99931     assert( pParse->cookieGoto==0 || pParse->cookieGoto==-1 );
 99932     pParse->cookieGoto = 0;
 99934     switch( pStep->op ){
 99935       case TK_UPDATE: {
 99936         sqlite3Update(pParse, 
 99937           targetSrcList(pParse, pStep),
 99938           sqlite3ExprListDup(db, pStep->pExprList, 0), 
 99939           sqlite3ExprDup(db, pStep->pWhere, 0), 
 99940           pParse->eOrconf
 99941         );
 99942         break;
 99944       case TK_INSERT: {
 99945         sqlite3Insert(pParse, 
 99946           targetSrcList(pParse, pStep),
 99947           sqlite3ExprListDup(db, pStep->pExprList, 0), 
 99948           sqlite3SelectDup(db, pStep->pSelect, 0), 
 99949           sqlite3IdListDup(db, pStep->pIdList), 
 99950           pParse->eOrconf
 99951         );
 99952         break;
 99954       case TK_DELETE: {
 99955         sqlite3DeleteFrom(pParse, 
 99956           targetSrcList(pParse, pStep),
 99957           sqlite3ExprDup(db, pStep->pWhere, 0)
 99958         );
 99959         break;
 99961       default: assert( pStep->op==TK_SELECT ); {
 99962         SelectDest sDest;
 99963         Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
 99964         sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
 99965         sqlite3Select(pParse, pSelect, &sDest);
 99966         sqlite3SelectDelete(db, pSelect);
 99967         break;
 99970     if( pStep->op!=TK_SELECT ){
 99971       sqlite3VdbeAddOp0(v, OP_ResetCount);
 99975   return 0;
 99978 #ifdef SQLITE_DEBUG
 99979 /*
 99980 ** This function is used to add VdbeComment() annotations to a VDBE
 99981 ** program. It is not used in production code, only for debugging.
 99982 */
 99983 static const char *onErrorText(int onError){
 99984   switch( onError ){
 99985     case OE_Abort:    return "abort";
 99986     case OE_Rollback: return "rollback";
 99987     case OE_Fail:     return "fail";
 99988     case OE_Replace:  return "replace";
 99989     case OE_Ignore:   return "ignore";
 99990     case OE_Default:  return "default";
 99992   return "n/a";
 99994 #endif
 99996 /*
 99997 ** Parse context structure pFrom has just been used to create a sub-vdbe
 99998 ** (trigger program). If an error has occurred, transfer error information
 99999 ** from pFrom to pTo.
 100001 static void transferParseError(Parse *pTo, Parse *pFrom){
 100002   assert( pFrom->zErrMsg==0 || pFrom->nErr );
 100003   assert( pTo->zErrMsg==0 || pTo->nErr );
 100004   if( pTo->nErr==0 ){
 100005     pTo->zErrMsg = pFrom->zErrMsg;
 100006     pTo->nErr = pFrom->nErr;
 100007   }else{
 100008     sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
 100013 ** Create and populate a new TriggerPrg object with a sub-program 
 100014 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
 100016 static TriggerPrg *codeRowTrigger(
 100017   Parse *pParse,       /* Current parse context */
 100018   Trigger *pTrigger,   /* Trigger to code */
 100019   Table *pTab,         /* The table pTrigger is attached to */
 100020   int orconf           /* ON CONFLICT policy to code trigger program with */
 100022   Parse *pTop = sqlite3ParseToplevel(pParse);
 100023   sqlite3 *db = pParse->db;   /* Database handle */
 100024   TriggerPrg *pPrg;           /* Value to return */
 100025   Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
 100026   Vdbe *v;                    /* Temporary VM */
 100027   NameContext sNC;            /* Name context for sub-vdbe */
 100028   SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
 100029   Parse *pSubParse;           /* Parse context for sub-vdbe */
 100030   int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
 100032   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
 100033   assert( pTop->pVdbe );
 100035   /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
 100036   ** are freed if an error occurs, link them into the Parse.pTriggerPrg 
 100037   ** list of the top-level Parse object sooner rather than later.  */
 100038   pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
 100039   if( !pPrg ) return 0;
 100040   pPrg->pNext = pTop->pTriggerPrg;
 100041   pTop->pTriggerPrg = pPrg;
 100042   pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
 100043   if( !pProgram ) return 0;
 100044   sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
 100045   pPrg->pTrigger = pTrigger;
 100046   pPrg->orconf = orconf;
 100047   pPrg->aColmask[0] = 0xffffffff;
 100048   pPrg->aColmask[1] = 0xffffffff;
 100050   /* Allocate and populate a new Parse context to use for coding the 
 100051   ** trigger sub-program.  */
 100052   pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
 100053   if( !pSubParse ) return 0;
 100054   memset(&sNC, 0, sizeof(sNC));
 100055   sNC.pParse = pSubParse;
 100056   pSubParse->db = db;
 100057   pSubParse->pTriggerTab = pTab;
 100058   pSubParse->pToplevel = pTop;
 100059   pSubParse->zAuthContext = pTrigger->zName;
 100060   pSubParse->eTriggerOp = pTrigger->op;
 100061   pSubParse->nQueryLoop = pParse->nQueryLoop;
 100063   v = sqlite3GetVdbe(pSubParse);
 100064   if( v ){
 100065     VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 
 100066       pTrigger->zName, onErrorText(orconf),
 100067       (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
 100068         (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
 100069         (pTrigger->op==TK_INSERT ? "INSERT" : ""),
 100070         (pTrigger->op==TK_DELETE ? "DELETE" : ""),
 100071       pTab->zName
 100072     ));
 100073 #ifndef SQLITE_OMIT_TRACE
 100074     sqlite3VdbeChangeP4(v, -1, 
 100075       sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
 100077 #endif
 100079     /* If one was specified, code the WHEN clause. If it evaluates to false
 100080     ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
 100081     ** OP_Halt inserted at the end of the program.  */
 100082     if( pTrigger->pWhen ){
 100083       pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
 100084       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 
 100085        && db->mallocFailed==0 
 100087         iEndTrigger = sqlite3VdbeMakeLabel(v);
 100088         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
 100090       sqlite3ExprDelete(db, pWhen);
 100093     /* Code the trigger program into the sub-vdbe. */
 100094     codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
 100096     /* Insert an OP_Halt at the end of the sub-program. */
 100097     if( iEndTrigger ){
 100098       sqlite3VdbeResolveLabel(v, iEndTrigger);
 100100     sqlite3VdbeAddOp0(v, OP_Halt);
 100101     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
 100103     transferParseError(pParse, pSubParse);
 100104     if( db->mallocFailed==0 ){
 100105       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
 100107     pProgram->nMem = pSubParse->nMem;
 100108     pProgram->nCsr = pSubParse->nTab;
 100109     pProgram->nOnce = pSubParse->nOnce;
 100110     pProgram->token = (void *)pTrigger;
 100111     pPrg->aColmask[0] = pSubParse->oldmask;
 100112     pPrg->aColmask[1] = pSubParse->newmask;
 100113     sqlite3VdbeDelete(v);
 100116   assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
 100117   assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
 100118   sqlite3StackFree(db, pSubParse);
 100120   return pPrg;
 100124 ** Return a pointer to a TriggerPrg object containing the sub-program for
 100125 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
 100126 ** TriggerPrg object exists, a new object is allocated and populated before
 100127 ** being returned.
 100129 static TriggerPrg *getRowTrigger(
 100130   Parse *pParse,       /* Current parse context */
 100131   Trigger *pTrigger,   /* Trigger to code */
 100132   Table *pTab,         /* The table trigger pTrigger is attached to */
 100133   int orconf           /* ON CONFLICT algorithm. */
 100135   Parse *pRoot = sqlite3ParseToplevel(pParse);
 100136   TriggerPrg *pPrg;
 100138   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
 100140   /* It may be that this trigger has already been coded (or is in the
 100141   ** process of being coded). If this is the case, then an entry with
 100142   ** a matching TriggerPrg.pTrigger field will be present somewhere
 100143   ** in the Parse.pTriggerPrg list. Search for such an entry.  */
 100144   for(pPrg=pRoot->pTriggerPrg; 
 100145       pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 
 100146       pPrg=pPrg->pNext
 100149   /* If an existing TriggerPrg could not be located, create a new one. */
 100150   if( !pPrg ){
 100151     pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
 100154   return pPrg;
 100158 ** Generate code for the trigger program associated with trigger p on 
 100159 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
 100160 ** function are the same as those described in the header function for
 100161 ** sqlite3CodeRowTrigger()
 100163 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
 100164   Parse *pParse,       /* Parse context */
 100165   Trigger *p,          /* Trigger to code */
 100166   Table *pTab,         /* The table to code triggers from */
 100167   int reg,             /* Reg array containing OLD.* and NEW.* values */
 100168   int orconf,          /* ON CONFLICT policy */
 100169   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
 100171   Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
 100172   TriggerPrg *pPrg;
 100173   pPrg = getRowTrigger(pParse, p, pTab, orconf);
 100174   assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
 100176   /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
 100177   ** is a pointer to the sub-vdbe containing the trigger program.  */
 100178   if( pPrg ){
 100179     int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
 100181     sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
 100182     sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
 100183     VdbeComment(
 100184         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
 100186     /* Set the P5 operand of the OP_Program instruction to non-zero if
 100187     ** recursive invocation of this trigger program is disallowed. Recursive
 100188     ** invocation is disallowed if (a) the sub-program is really a trigger,
 100189     ** not a foreign key action, and (b) the flag to enable recursive triggers
 100190     ** is clear.  */
 100191     sqlite3VdbeChangeP5(v, (u8)bRecursive);
 100196 ** This is called to code the required FOR EACH ROW triggers for an operation
 100197 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
 100198 ** is given by the op paramater. The tr_tm parameter determines whether the
 100199 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
 100200 ** parameter pChanges is passed the list of columns being modified.
 100202 ** If there are no triggers that fire at the specified time for the specified
 100203 ** operation on pTab, this function is a no-op.
 100205 ** The reg argument is the address of the first in an array of registers 
 100206 ** that contain the values substituted for the new.* and old.* references
 100207 ** in the trigger program. If N is the number of columns in table pTab
 100208 ** (a copy of pTab->nCol), then registers are populated as follows:
 100210 **   Register       Contains
 100211 **   ------------------------------------------------------
 100212 **   reg+0          OLD.rowid
 100213 **   reg+1          OLD.* value of left-most column of pTab
 100214 **   ...            ...
 100215 **   reg+N          OLD.* value of right-most column of pTab
 100216 **   reg+N+1        NEW.rowid
 100217 **   reg+N+2        OLD.* value of left-most column of pTab
 100218 **   ...            ...
 100219 **   reg+N+N+1      NEW.* value of right-most column of pTab
 100221 ** For ON DELETE triggers, the registers containing the NEW.* values will
 100222 ** never be accessed by the trigger program, so they are not allocated or 
 100223 ** populated by the caller (there is no data to populate them with anyway). 
 100224 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
 100225 ** are never accessed, and so are not allocated by the caller. So, for an
 100226 ** ON INSERT trigger, the value passed to this function as parameter reg
 100227 ** is not a readable register, although registers (reg+N) through 
 100228 ** (reg+N+N+1) are.
 100230 ** Parameter orconf is the default conflict resolution algorithm for the
 100231 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
 100232 ** is the instruction that control should jump to if a trigger program
 100233 ** raises an IGNORE exception.
 100235 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
 100236   Parse *pParse,       /* Parse context */
 100237   Trigger *pTrigger,   /* List of triggers on table pTab */
 100238   int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
 100239   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
 100240   int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
 100241   Table *pTab,         /* The table to code triggers from */
 100242   int reg,             /* The first in an array of registers (see above) */
 100243   int orconf,          /* ON CONFLICT policy */
 100244   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
 100246   Trigger *p;          /* Used to iterate through pTrigger list */
 100248   assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
 100249   assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
 100250   assert( (op==TK_UPDATE)==(pChanges!=0) );
 100252   for(p=pTrigger; p; p=p->pNext){
 100254     /* Sanity checking:  The schema for the trigger and for the table are
 100255     ** always defined.  The trigger must be in the same schema as the table
 100256     ** or else it must be a TEMP trigger. */
 100257     assert( p->pSchema!=0 );
 100258     assert( p->pTabSchema!=0 );
 100259     assert( p->pSchema==p->pTabSchema 
 100260          || p->pSchema==pParse->db->aDb[1].pSchema );
 100262     /* Determine whether we should code this trigger */
 100263     if( p->op==op 
 100264      && p->tr_tm==tr_tm 
 100265      && checkColumnOverlap(p->pColumns, pChanges)
 100267       sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
 100273 ** Triggers may access values stored in the old.* or new.* pseudo-table. 
 100274 ** This function returns a 32-bit bitmask indicating which columns of the 
 100275 ** old.* or new.* tables actually are used by triggers. This information 
 100276 ** may be used by the caller, for example, to avoid having to load the entire
 100277 ** old.* record into memory when executing an UPDATE or DELETE command.
 100279 ** Bit 0 of the returned mask is set if the left-most column of the
 100280 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
 100281 ** the second leftmost column value is required, and so on. If there
 100282 ** are more than 32 columns in the table, and at least one of the columns
 100283 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
 100285 ** It is not possible to determine if the old.rowid or new.rowid column is 
 100286 ** accessed by triggers. The caller must always assume that it is.
 100288 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
 100289 ** applies to the old.* table. If 1, the new.* table.
 100291 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
 100292 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
 100293 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
 100294 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
 100295 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
 100297 SQLITE_PRIVATE u32 sqlite3TriggerColmask(
 100298   Parse *pParse,       /* Parse context */
 100299   Trigger *pTrigger,   /* List of triggers on table pTab */
 100300   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
 100301   int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
 100302   int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
 100303   Table *pTab,         /* The table to code triggers from */
 100304   int orconf           /* Default ON CONFLICT policy for trigger steps */
 100306   const int op = pChanges ? TK_UPDATE : TK_DELETE;
 100307   u32 mask = 0;
 100308   Trigger *p;
 100310   assert( isNew==1 || isNew==0 );
 100311   for(p=pTrigger; p; p=p->pNext){
 100312     if( p->op==op && (tr_tm&p->tr_tm)
 100313      && checkColumnOverlap(p->pColumns,pChanges)
 100315       TriggerPrg *pPrg;
 100316       pPrg = getRowTrigger(pParse, p, pTab, orconf);
 100317       if( pPrg ){
 100318         mask |= pPrg->aColmask[isNew];
 100323   return mask;
 100326 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
 100328 /************** End of trigger.c *********************************************/
 100329 /************** Begin file update.c ******************************************/
 100331 ** 2001 September 15
 100333 ** The author disclaims copyright to this source code.  In place of
 100334 ** a legal notice, here is a blessing:
 100336 **    May you do good and not evil.
 100337 **    May you find forgiveness for yourself and forgive others.
 100338 **    May you share freely, never taking more than you give.
 100340 *************************************************************************
 100341 ** This file contains C code routines that are called by the parser
 100342 ** to handle UPDATE statements.
 100345 #ifndef SQLITE_OMIT_VIRTUALTABLE
 100346 /* Forward declaration */
 100347 static void updateVirtualTable(
 100348   Parse *pParse,       /* The parsing context */
 100349   SrcList *pSrc,       /* The virtual table to be modified */
 100350   Table *pTab,         /* The virtual table */
 100351   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
 100352   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
 100353   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
 100354   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
 100355   int onError          /* ON CONFLICT strategy */
 100357 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 100360 ** The most recently coded instruction was an OP_Column to retrieve the
 100361 ** i-th column of table pTab. This routine sets the P4 parameter of the 
 100362 ** OP_Column to the default value, if any.
 100364 ** The default value of a column is specified by a DEFAULT clause in the 
 100365 ** column definition. This was either supplied by the user when the table
 100366 ** was created, or added later to the table definition by an ALTER TABLE
 100367 ** command. If the latter, then the row-records in the table btree on disk
 100368 ** may not contain a value for the column and the default value, taken
 100369 ** from the P4 parameter of the OP_Column instruction, is returned instead.
 100370 ** If the former, then all row-records are guaranteed to include a value
 100371 ** for the column and the P4 value is not required.
 100373 ** Column definitions created by an ALTER TABLE command may only have 
 100374 ** literal default values specified: a number, null or a string. (If a more
 100375 ** complicated default expression value was provided, it is evaluated 
 100376 ** when the ALTER TABLE is executed and one of the literal values written
 100377 ** into the sqlite_master table.)
 100379 ** Therefore, the P4 parameter is only required if the default value for
 100380 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
 100381 ** function is capable of transforming these types of expressions into
 100382 ** sqlite3_value objects.
 100384 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
 100385 ** on register iReg. This is used when an equivalent integer value is 
 100386 ** stored in place of an 8-byte floating point value in order to save 
 100387 ** space.
 100389 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
 100390   assert( pTab!=0 );
 100391   if( !pTab->pSelect ){
 100392     sqlite3_value *pValue;
 100393     u8 enc = ENC(sqlite3VdbeDb(v));
 100394     Column *pCol = &pTab->aCol[i];
 100395     VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
 100396     assert( i<pTab->nCol );
 100397     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
 100398                          pCol->affinity, &pValue);
 100399     if( pValue ){
 100400       sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
 100402 #ifndef SQLITE_OMIT_FLOATING_POINT
 100403     if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
 100404       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
 100406 #endif
 100411 ** Process an UPDATE statement.
 100413 **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
 100414 **          \_______/ \________/     \______/       \________________/
 100415 *            onError   pTabList      pChanges             pWhere
 100417 SQLITE_PRIVATE void sqlite3Update(
 100418   Parse *pParse,         /* The parser context */
 100419   SrcList *pTabList,     /* The table in which we should change things */
 100420   ExprList *pChanges,    /* Things to be changed */
 100421   Expr *pWhere,          /* The WHERE clause.  May be null */
 100422   int onError            /* How to handle constraint errors */
 100424   int i, j;              /* Loop counters */
 100425   Table *pTab;           /* The table to be updated */
 100426   int addr = 0;          /* VDBE instruction address of the start of the loop */
 100427   WhereInfo *pWInfo;     /* Information about the WHERE clause */
 100428   Vdbe *v;               /* The virtual database engine */
 100429   Index *pIdx;           /* For looping over indices */
 100430   int nIdx;              /* Number of indices that need updating */
 100431   int iCur;              /* VDBE Cursor number of pTab */
 100432   sqlite3 *db;           /* The database structure */
 100433   int *aRegIdx = 0;      /* One register assigned to each index to be updated */
 100434   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
 100435                          ** an expression for the i-th column of the table.
 100436                          ** aXRef[i]==-1 if the i-th column is not changed. */
 100437   int chngRowid;         /* True if the record number is being changed */
 100438   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
 100439   int openAll = 0;       /* True if all indices need to be opened */
 100440   AuthContext sContext;  /* The authorization context */
 100441   NameContext sNC;       /* The name-context to resolve expressions in */
 100442   int iDb;               /* Database containing the table being updated */
 100443   int okOnePass;         /* True for one-pass algorithm without the FIFO */
 100444   int hasFK;             /* True if foreign key processing is required */
 100446 #ifndef SQLITE_OMIT_TRIGGER
 100447   int isView;            /* True when updating a view (INSTEAD OF trigger) */
 100448   Trigger *pTrigger;     /* List of triggers on pTab, if required */
 100449   int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
 100450 #endif
 100451   int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
 100453   /* Register Allocations */
 100454   int regRowCount = 0;   /* A count of rows changed */
 100455   int regOldRowid;       /* The old rowid */
 100456   int regNewRowid;       /* The new rowid */
 100457   int regNew;            /* Content of the NEW.* table in triggers */
 100458   int regOld = 0;        /* Content of OLD.* table in triggers */
 100459   int regRowSet = 0;     /* Rowset of rows to be updated */
 100461   memset(&sContext, 0, sizeof(sContext));
 100462   db = pParse->db;
 100463   if( pParse->nErr || db->mallocFailed ){
 100464     goto update_cleanup;
 100466   assert( pTabList->nSrc==1 );
 100468   /* Locate the table which we want to update. 
 100470   pTab = sqlite3SrcListLookup(pParse, pTabList);
 100471   if( pTab==0 ) goto update_cleanup;
 100472   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 100474   /* Figure out if we have any triggers and if the table being
 100475   ** updated is a view.
 100477 #ifndef SQLITE_OMIT_TRIGGER
 100478   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
 100479   isView = pTab->pSelect!=0;
 100480   assert( pTrigger || tmask==0 );
 100481 #else
 100482 # define pTrigger 0
 100483 # define isView 0
 100484 # define tmask 0
 100485 #endif
 100486 #ifdef SQLITE_OMIT_VIEW
 100487 # undef isView
 100488 # define isView 0
 100489 #endif
 100491   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 100492     goto update_cleanup;
 100494   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
 100495     goto update_cleanup;
 100497   aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
 100498   if( aXRef==0 ) goto update_cleanup;
 100499   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
 100501   /* Allocate a cursors for the main database table and for all indices.
 100502   ** The index cursors might not be used, but if they are used they
 100503   ** need to occur right after the database cursor.  So go ahead and
 100504   ** allocate enough space, just in case.
 100506   pTabList->a[0].iCursor = iCur = pParse->nTab++;
 100507   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 100508     pParse->nTab++;
 100511   /* Initialize the name-context */
 100512   memset(&sNC, 0, sizeof(sNC));
 100513   sNC.pParse = pParse;
 100514   sNC.pSrcList = pTabList;
 100516   /* Resolve the column names in all the expressions of the
 100517   ** of the UPDATE statement.  Also find the column index
 100518   ** for each column to be updated in the pChanges array.  For each
 100519   ** column to be updated, make sure we have authorization to change
 100520   ** that column.
 100522   chngRowid = 0;
 100523   for(i=0; i<pChanges->nExpr; i++){
 100524     if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
 100525       goto update_cleanup;
 100527     for(j=0; j<pTab->nCol; j++){
 100528       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
 100529         if( j==pTab->iPKey ){
 100530           chngRowid = 1;
 100531           pRowidExpr = pChanges->a[i].pExpr;
 100533         aXRef[j] = i;
 100534         break;
 100537     if( j>=pTab->nCol ){
 100538       if( sqlite3IsRowid(pChanges->a[i].zName) ){
 100539         chngRowid = 1;
 100540         pRowidExpr = pChanges->a[i].pExpr;
 100541       }else{
 100542         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
 100543         pParse->checkSchema = 1;
 100544         goto update_cleanup;
 100547 #ifndef SQLITE_OMIT_AUTHORIZATION
 100549       int rc;
 100550       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
 100551                            pTab->aCol[j].zName, db->aDb[iDb].zName);
 100552       if( rc==SQLITE_DENY ){
 100553         goto update_cleanup;
 100554       }else if( rc==SQLITE_IGNORE ){
 100555         aXRef[j] = -1;
 100558 #endif
 100561   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
 100563   /* Allocate memory for the array aRegIdx[].  There is one entry in the
 100564   ** array for each index associated with table being updated.  Fill in
 100565   ** the value with a register number for indices that are to be used
 100566   ** and with zero for unused indices.
 100568   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
 100569   if( nIdx>0 ){
 100570     aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
 100571     if( aRegIdx==0 ) goto update_cleanup;
 100573   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 100574     int reg;
 100575     if( hasFK || chngRowid ){
 100576       reg = ++pParse->nMem;
 100577     }else{
 100578       reg = 0;
 100579       for(i=0; i<pIdx->nColumn; i++){
 100580         if( aXRef[pIdx->aiColumn[i]]>=0 ){
 100581           reg = ++pParse->nMem;
 100582           break;
 100586     aRegIdx[j] = reg;
 100589   /* Begin generating code. */
 100590   v = sqlite3GetVdbe(pParse);
 100591   if( v==0 ) goto update_cleanup;
 100592   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 100593   sqlite3BeginWriteOperation(pParse, 1, iDb);
 100595 #ifndef SQLITE_OMIT_VIRTUALTABLE
 100596   /* Virtual tables must be handled separately */
 100597   if( IsVirtual(pTab) ){
 100598     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
 100599                        pWhere, onError);
 100600     pWhere = 0;
 100601     pTabList = 0;
 100602     goto update_cleanup;
 100604 #endif
 100606   /* Allocate required registers. */
 100607   regRowSet = ++pParse->nMem;
 100608   regOldRowid = regNewRowid = ++pParse->nMem;
 100609   if( pTrigger || hasFK ){
 100610     regOld = pParse->nMem + 1;
 100611     pParse->nMem += pTab->nCol;
 100613   if( chngRowid || pTrigger || hasFK ){
 100614     regNewRowid = ++pParse->nMem;
 100616   regNew = pParse->nMem + 1;
 100617   pParse->nMem += pTab->nCol;
 100619   /* Start the view context. */
 100620   if( isView ){
 100621     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
 100624   /* If we are trying to update a view, realize that view into
 100625   ** a ephemeral table.
 100627 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 100628   if( isView ){
 100629     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
 100631 #endif
 100633   /* Resolve the column names in all the expressions in the
 100634   ** WHERE clause.
 100636   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
 100637     goto update_cleanup;
 100640   /* Begin the database scan
 100642   sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
 100643   pWInfo = sqlite3WhereBegin(
 100644       pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0
 100646   if( pWInfo==0 ) goto update_cleanup;
 100647   okOnePass = pWInfo->okOnePass;
 100649   /* Remember the rowid of every item to be updated.
 100651   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
 100652   if( !okOnePass ){
 100653     sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
 100656   /* End the database scan loop.
 100658   sqlite3WhereEnd(pWInfo);
 100660   /* Initialize the count of updated rows
 100662   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
 100663     regRowCount = ++pParse->nMem;
 100664     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
 100667   if( !isView ){
 100669     ** Open every index that needs updating.  Note that if any
 100670     ** index could potentially invoke a REPLACE conflict resolution 
 100671     ** action, then we need to open all indices because we might need
 100672     ** to be deleting some records.
 100674     if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 
 100675     if( onError==OE_Replace ){
 100676       openAll = 1;
 100677     }else{
 100678       openAll = 0;
 100679       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 100680         if( pIdx->onError==OE_Replace ){
 100681           openAll = 1;
 100682           break;
 100686     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 100687       assert( aRegIdx );
 100688       if( openAll || aRegIdx[i]>0 ){
 100689         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 100690         sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
 100691                        (char*)pKey, P4_KEYINFO_HANDOFF);
 100692         assert( pParse->nTab>iCur+i+1 );
 100697   /* Top of the update loop */
 100698   if( okOnePass ){
 100699     int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
 100700     addr = sqlite3VdbeAddOp0(v, OP_Goto);
 100701     sqlite3VdbeJumpHere(v, a1);
 100702   }else{
 100703     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
 100706   /* Make cursor iCur point to the record that is being updated. If
 100707   ** this record does not exist for some reason (deleted by a trigger,
 100708   ** for example, then jump to the next iteration of the RowSet loop.  */
 100709   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
 100711   /* If the record number will change, set register regNewRowid to
 100712   ** contain the new value. If the record number is not being modified,
 100713   ** then regNewRowid is the same register as regOldRowid, which is
 100714   ** already populated.  */
 100715   assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
 100716   if( chngRowid ){
 100717     sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
 100718     sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
 100721   /* If there are triggers on this table, populate an array of registers 
 100722   ** with the required old.* column data.  */
 100723   if( hasFK || pTrigger ){
 100724     u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
 100725     oldmask |= sqlite3TriggerColmask(pParse, 
 100726         pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
 100728     for(i=0; i<pTab->nCol; i++){
 100729       if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
 100730         sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
 100731       }else{
 100732         sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
 100735     if( chngRowid==0 ){
 100736       sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
 100740   /* Populate the array of registers beginning at regNew with the new
 100741   ** row data. This array is used to check constaints, create the new
 100742   ** table and index records, and as the values for any new.* references
 100743   ** made by triggers.
 100745   ** If there are one or more BEFORE triggers, then do not populate the
 100746   ** registers associated with columns that are (a) not modified by
 100747   ** this UPDATE statement and (b) not accessed by new.* references. The
 100748   ** values for registers not modified by the UPDATE must be reloaded from 
 100749   ** the database after the BEFORE triggers are fired anyway (as the trigger 
 100750   ** may have modified them). So not loading those that are not going to
 100751   ** be used eliminates some redundant opcodes.
 100753   newmask = sqlite3TriggerColmask(
 100754       pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
 100756   sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);
 100757   for(i=0; i<pTab->nCol; i++){
 100758     if( i==pTab->iPKey ){
 100759       /*sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);*/
 100760     }else{
 100761       j = aXRef[i];
 100762       if( j>=0 ){
 100763         sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
 100764       }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
 100765         /* This branch loads the value of a column that will not be changed 
 100766         ** into a register. This is done if there are no BEFORE triggers, or
 100767         ** if there are one or more BEFORE triggers that use this value via
 100768         ** a new.* reference in a trigger program.
 100770         testcase( i==31 );
 100771         testcase( i==32 );
 100772         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
 100773         sqlite3ColumnDefault(v, pTab, i, regNew+i);
 100778   /* Fire any BEFORE UPDATE triggers. This happens before constraints are
 100779   ** verified. One could argue that this is wrong.
 100781   if( tmask&TRIGGER_BEFORE ){
 100782     sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
 100783     sqlite3TableAffinityStr(v, pTab);
 100784     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
 100785         TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
 100787     /* The row-trigger may have deleted the row being updated. In this
 100788     ** case, jump to the next row. No updates or AFTER triggers are 
 100789     ** required. This behaviour - what happens when the row being updated
 100790     ** is deleted or renamed by a BEFORE trigger - is left undefined in the
 100791     ** documentation.
 100793     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
 100795     /* If it did not delete it, the row-trigger may still have modified 
 100796     ** some of the columns of the row being updated. Load the values for 
 100797     ** all columns not modified by the update statement into their 
 100798     ** registers in case this has happened.
 100800     for(i=0; i<pTab->nCol; i++){
 100801       if( aXRef[i]<0 && i!=pTab->iPKey ){
 100802         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
 100803         sqlite3ColumnDefault(v, pTab, i, regNew+i);
 100808   if( !isView ){
 100809     int j1;                       /* Address of jump instruction */
 100811     /* Do constraint checks. */
 100812     sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
 100813         aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
 100815     /* Do FK constraint checks. */
 100816     if( hasFK ){
 100817       sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
 100820     /* Delete the index entries associated with the current record.  */
 100821     j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
 100822     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
 100824     /* If changing the record number, delete the old record.  */
 100825     if( hasFK || chngRowid ){
 100826       sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
 100828     sqlite3VdbeJumpHere(v, j1);
 100830     if( hasFK ){
 100831       sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
 100834     /* Insert the new index entries and the new record. */
 100835     sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
 100837     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
 100838     ** handle rows (possibly in other tables) that refer via a foreign key
 100839     ** to the row just updated. */ 
 100840     if( hasFK ){
 100841       sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
 100845   /* Increment the row counter 
 100847   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
 100848     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
 100851   sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
 100852       TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
 100854   /* Repeat the above with the next record to be updated, until
 100855   ** all record selected by the WHERE clause have been updated.
 100857   sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
 100858   sqlite3VdbeJumpHere(v, addr);
 100860   /* Close all tables */
 100861   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 100862     assert( aRegIdx );
 100863     if( openAll || aRegIdx[i]>0 ){
 100864       sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
 100867   sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
 100869   /* Update the sqlite_sequence table by storing the content of the
 100870   ** maximum rowid counter values recorded while inserting into
 100871   ** autoincrement tables.
 100873   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 100874     sqlite3AutoincrementEnd(pParse);
 100878   ** Return the number of rows that were changed. If this routine is 
 100879   ** generating code because of a call to sqlite3NestedParse(), do not
 100880   ** invoke the callback function.
 100882   if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
 100883     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
 100884     sqlite3VdbeSetNumCols(v, 1);
 100885     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
 100888 update_cleanup:
 100889   sqlite3AuthContextPop(&sContext);
 100890   sqlite3DbFree(db, aRegIdx);
 100891   sqlite3DbFree(db, aXRef);
 100892   sqlite3SrcListDelete(db, pTabList);
 100893   sqlite3ExprListDelete(db, pChanges);
 100894   sqlite3ExprDelete(db, pWhere);
 100895   return;
 100897 /* Make sure "isView" and other macros defined above are undefined. Otherwise
 100898 ** thely may interfere with compilation of other functions in this file
 100899 ** (or in another file, if this file becomes part of the amalgamation).  */
 100900 #ifdef isView
 100901  #undef isView
 100902 #endif
 100903 #ifdef pTrigger
 100904  #undef pTrigger
 100905 #endif
 100907 #ifndef SQLITE_OMIT_VIRTUALTABLE
 100909 ** Generate code for an UPDATE of a virtual table.
 100911 ** The strategy is that we create an ephemerial table that contains
 100912 ** for each row to be changed:
 100914 **   (A)  The original rowid of that row.
 100915 **   (B)  The revised rowid for the row. (note1)
 100916 **   (C)  The content of every column in the row.
 100918 ** Then we loop over this ephemeral table and for each row in
 100919 ** the ephermeral table call VUpdate.
 100921 ** When finished, drop the ephemeral table.
 100923 ** (note1) Actually, if we know in advance that (A) is always the same
 100924 ** as (B) we only store (A), then duplicate (A) when pulling
 100925 ** it out of the ephemeral table before calling VUpdate.
 100927 static void updateVirtualTable(
 100928   Parse *pParse,       /* The parsing context */
 100929   SrcList *pSrc,       /* The virtual table to be modified */
 100930   Table *pTab,         /* The virtual table */
 100931   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
 100932   Expr *pRowid,        /* Expression used to recompute the rowid */
 100933   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
 100934   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
 100935   int onError          /* ON CONFLICT strategy */
 100937   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
 100938   ExprList *pEList = 0;     /* The result set of the SELECT statement */
 100939   Select *pSelect = 0;      /* The SELECT statement */
 100940   Expr *pExpr;              /* Temporary expression */
 100941   int ephemTab;             /* Table holding the result of the SELECT */
 100942   int i;                    /* Loop counter */
 100943   int addr;                 /* Address of top of loop */
 100944   int iReg;                 /* First register in set passed to OP_VUpdate */
 100945   sqlite3 *db = pParse->db; /* Database connection */
 100946   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
 100947   SelectDest dest;
 100949   /* Construct the SELECT statement that will find the new values for
 100950   ** all updated rows. 
 100952   pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
 100953   if( pRowid ){
 100954     pEList = sqlite3ExprListAppend(pParse, pEList,
 100955                                    sqlite3ExprDup(db, pRowid, 0));
 100957   assert( pTab->iPKey<0 );
 100958   for(i=0; i<pTab->nCol; i++){
 100959     if( aXRef[i]>=0 ){
 100960       pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
 100961     }else{
 100962       pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
 100964     pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
 100966   pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
 100968   /* Create the ephemeral table into which the update results will
 100969   ** be stored.
 100971   assert( v );
 100972   ephemTab = pParse->nTab++;
 100973   sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
 100974   sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 100976   /* fill the ephemeral table 
 100978   sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
 100979   sqlite3Select(pParse, pSelect, &dest);
 100981   /* Generate code to scan the ephemeral table and call VUpdate. */
 100982   iReg = ++pParse->nMem;
 100983   pParse->nMem += pTab->nCol+1;
 100984   addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
 100985   sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
 100986   sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
 100987   for(i=0; i<pTab->nCol; i++){
 100988     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
 100990   sqlite3VtabMakeWritable(pParse, pTab);
 100991   sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
 100992   sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
 100993   sqlite3MayAbort(pParse);
 100994   sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
 100995   sqlite3VdbeJumpHere(v, addr);
 100996   sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
 100998   /* Cleanup */
 100999   sqlite3SelectDelete(db, pSelect);  
 101001 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 101003 /************** End of update.c **********************************************/
 101004 /************** Begin file vacuum.c ******************************************/
 101006 ** 2003 April 6
 101008 ** The author disclaims copyright to this source code.  In place of
 101009 ** a legal notice, here is a blessing:
 101011 **    May you do good and not evil.
 101012 **    May you find forgiveness for yourself and forgive others.
 101013 **    May you share freely, never taking more than you give.
 101015 *************************************************************************
 101016 ** This file contains code used to implement the VACUUM command.
 101018 ** Most of the code in this file may be omitted by defining the
 101019 ** SQLITE_OMIT_VACUUM macro.
 101022 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 101024 ** Finalize a prepared statement.  If there was an error, store the
 101025 ** text of the error message in *pzErrMsg.  Return the result code.
 101027 static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
 101028   int rc;
 101029   rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
 101030   if( rc ){
 101031     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
 101033   return rc;
 101037 ** Execute zSql on database db. Return an error code.
 101039 static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
 101040   sqlite3_stmt *pStmt;
 101041   VVA_ONLY( int rc; )
 101042   if( !zSql ){
 101043     return SQLITE_NOMEM;
 101045   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
 101046     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
 101047     return sqlite3_errcode(db);
 101049   VVA_ONLY( rc = ) sqlite3_step(pStmt);
 101050   assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
 101051   return vacuumFinalize(db, pStmt, pzErrMsg);
 101055 ** Execute zSql on database db. The statement returns exactly
 101056 ** one column. Execute this as SQL on the same database.
 101058 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
 101059   sqlite3_stmt *pStmt;
 101060   int rc;
 101062   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 101063   if( rc!=SQLITE_OK ) return rc;
 101065   while( SQLITE_ROW==sqlite3_step(pStmt) ){
 101066     rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
 101067     if( rc!=SQLITE_OK ){
 101068       vacuumFinalize(db, pStmt, pzErrMsg);
 101069       return rc;
 101073   return vacuumFinalize(db, pStmt, pzErrMsg);
 101077 ** The non-standard VACUUM command is used to clean up the database,
 101078 ** collapse free space, etc.  It is modelled after the VACUUM command
 101079 ** in PostgreSQL.
 101081 ** In version 1.0.x of SQLite, the VACUUM command would call
 101082 ** gdbm_reorganize() on all the database tables.  But beginning
 101083 ** with 2.0.0, SQLite no longer uses GDBM so this command has
 101084 ** become a no-op.
 101086 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
 101087   Vdbe *v = sqlite3GetVdbe(pParse);
 101088   if( v ){
 101089     sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
 101090     sqlite3VdbeUsesBtree(v, 0);
 101092   return;
 101096 ** This routine implements the OP_Vacuum opcode of the VDBE.
 101098 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
 101099   int rc = SQLITE_OK;     /* Return code from service routines */
 101100   Btree *pMain;           /* The database being vacuumed */
 101101   Btree *pTemp;           /* The temporary database we vacuum into */
 101102   char *zSql = 0;         /* SQL statements */
 101103   int saved_flags;        /* Saved value of the db->flags */
 101104   int saved_nChange;      /* Saved value of db->nChange */
 101105   int saved_nTotalChange; /* Saved value of db->nTotalChange */
 101106   void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
 101107   Db *pDb = 0;            /* Database to detach at end of vacuum */
 101108   int isMemDb;            /* True if vacuuming a :memory: database */
 101109   int nRes;               /* Bytes of reserved space at the end of each page */
 101110   int nDb;                /* Number of attached databases */
 101112   if( !db->autoCommit ){
 101113     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
 101114     return SQLITE_ERROR;
 101116   if( db->activeVdbeCnt>1 ){
 101117     sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
 101118     return SQLITE_ERROR;
 101121   /* Save the current value of the database flags so that it can be 
 101122   ** restored before returning. Then set the writable-schema flag, and
 101123   ** disable CHECK and foreign key constraints.  */
 101124   saved_flags = db->flags;
 101125   saved_nChange = db->nChange;
 101126   saved_nTotalChange = db->nTotalChange;
 101127   saved_xTrace = db->xTrace;
 101128   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
 101129   db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
 101130   db->xTrace = 0;
 101132   pMain = db->aDb[0].pBt;
 101133   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
 101135   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
 101136   ** can be set to 'off' for this file, as it is not recovered if a crash
 101137   ** occurs anyway. The integrity of the database is maintained by a
 101138   ** (possibly synchronous) transaction opened on the main database before
 101139   ** sqlite3BtreeCopyFile() is called.
 101141   ** An optimisation would be to use a non-journaled pager.
 101142   ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
 101143   ** that actually made the VACUUM run slower.  Very little journalling
 101144   ** actually occurs when doing a vacuum since the vacuum_db is initially
 101145   ** empty.  Only the journal header is written.  Apparently it takes more
 101146   ** time to parse and run the PRAGMA to turn journalling off than it does
 101147   ** to write the journal header file.
 101149   nDb = db->nDb;
 101150   if( sqlite3TempInMemory(db) ){
 101151     zSql = "ATTACH ':memory:' AS vacuum_db;";
 101152   }else{
 101153     zSql = "ATTACH '' AS vacuum_db;";
 101155   rc = execSql(db, pzErrMsg, zSql);
 101156   if( db->nDb>nDb ){
 101157     pDb = &db->aDb[db->nDb-1];
 101158     assert( strcmp(pDb->zName,"vacuum_db")==0 );
 101160   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101161   pTemp = db->aDb[db->nDb-1].pBt;
 101163   /* The call to execSql() to attach the temp database has left the file
 101164   ** locked (as there was more than one active statement when the transaction
 101165   ** to read the schema was concluded. Unlock it here so that this doesn't
 101166   ** cause problems for the call to BtreeSetPageSize() below.  */
 101167   sqlite3BtreeCommit(pTemp);
 101169   nRes = sqlite3BtreeGetReserve(pMain);
 101171   /* A VACUUM cannot change the pagesize of an encrypted database. */
 101172 #ifdef SQLITE_HAS_CODEC
 101173   if( db->nextPagesize ){
 101174     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
 101175     int nKey;
 101176     char *zKey;
 101177     sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 101178     if( nKey ) db->nextPagesize = 0;
 101180 #endif
 101182   rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
 101183   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101185   /* Begin a transaction and take an exclusive lock on the main database
 101186   ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
 101187   ** to ensure that we do not try to change the page-size on a WAL database.
 101189   rc = execSql(db, pzErrMsg, "BEGIN;");
 101190   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101191   rc = sqlite3BtreeBeginTrans(pMain, 2);
 101192   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101194   /* Do not attempt to change the page size for a WAL database */
 101195   if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
 101196                                                ==PAGER_JOURNALMODE_WAL ){
 101197     db->nextPagesize = 0;
 101200   if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
 101201    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
 101202    || NEVER(db->mallocFailed)
 101204     rc = SQLITE_NOMEM;
 101205     goto end_of_vacuum;
 101208 #ifndef SQLITE_OMIT_AUTOVACUUM
 101209   sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
 101210                                            sqlite3BtreeGetAutoVacuum(pMain));
 101211 #endif
 101213   /* Query the schema of the main database. Create a mirror schema
 101214   ** in the temporary database.
 101216   rc = execExecSql(db, pzErrMsg,
 101217       "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
 101218       "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
 101219       "   AND rootpage>0"
 101221   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101222   rc = execExecSql(db, pzErrMsg,
 101223       "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
 101224       "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
 101225   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101226   rc = execExecSql(db, pzErrMsg,
 101227       "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
 101228       "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
 101229   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101231   /* Loop through the tables in the main database. For each, do
 101232   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
 101233   ** the contents to the temporary database.
 101235   rc = execExecSql(db, pzErrMsg,
 101236       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
 101237       "|| ' SELECT * FROM main.' || quote(name) || ';'"
 101238       "FROM main.sqlite_master "
 101239       "WHERE type = 'table' AND name!='sqlite_sequence' "
 101240       "  AND rootpage>0"
 101242   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101244   /* Copy over the sequence table
 101246   rc = execExecSql(db, pzErrMsg,
 101247       "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
 101248       "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
 101250   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101251   rc = execExecSql(db, pzErrMsg,
 101252       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
 101253       "|| ' SELECT * FROM main.' || quote(name) || ';' "
 101254       "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
 101256   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101259   /* Copy the triggers, views, and virtual tables from the main database
 101260   ** over to the temporary database.  None of these objects has any
 101261   ** associated storage, so all we have to do is copy their entries
 101262   ** from the SQLITE_MASTER table.
 101264   rc = execSql(db, pzErrMsg,
 101265       "INSERT INTO vacuum_db.sqlite_master "
 101266       "  SELECT type, name, tbl_name, rootpage, sql"
 101267       "    FROM main.sqlite_master"
 101268       "   WHERE type='view' OR type='trigger'"
 101269       "      OR (type='table' AND rootpage=0)"
 101271   if( rc ) goto end_of_vacuum;
 101273   /* At this point, there is a write transaction open on both the 
 101274   ** vacuum database and the main database. Assuming no error occurs,
 101275   ** both transactions are closed by this block - the main database
 101276   ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
 101277   ** call to sqlite3BtreeCommit().
 101280     u32 meta;
 101281     int i;
 101283     /* This array determines which meta meta values are preserved in the
 101284     ** vacuum.  Even entries are the meta value number and odd entries
 101285     ** are an increment to apply to the meta value after the vacuum.
 101286     ** The increment is used to increase the schema cookie so that other
 101287     ** connections to the same database will know to reread the schema.
 101289     static const unsigned char aCopy[] = {
 101290        BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
 101291        BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
 101292        BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
 101293        BTREE_USER_VERSION,       0,  /* Preserve the user version */
 101296     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
 101297     assert( 1==sqlite3BtreeIsInTrans(pMain) );
 101299     /* Copy Btree meta values */
 101300     for(i=0; i<ArraySize(aCopy); i+=2){
 101301       /* GetMeta() and UpdateMeta() cannot fail in this context because
 101302       ** we already have page 1 loaded into cache and marked dirty. */
 101303       sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
 101304       rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
 101305       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
 101308     rc = sqlite3BtreeCopyFile(pMain, pTemp);
 101309     if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101310     rc = sqlite3BtreeCommit(pTemp);
 101311     if( rc!=SQLITE_OK ) goto end_of_vacuum;
 101312 #ifndef SQLITE_OMIT_AUTOVACUUM
 101313     sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
 101314 #endif
 101317   assert( rc==SQLITE_OK );
 101318   rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
 101320 end_of_vacuum:
 101321   /* Restore the original value of db->flags */
 101322   db->flags = saved_flags;
 101323   db->nChange = saved_nChange;
 101324   db->nTotalChange = saved_nTotalChange;
 101325   db->xTrace = saved_xTrace;
 101326   sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
 101328   /* Currently there is an SQL level transaction open on the vacuum
 101329   ** database. No locks are held on any other files (since the main file
 101330   ** was committed at the btree level). So it safe to end the transaction
 101331   ** by manually setting the autoCommit flag to true and detaching the
 101332   ** vacuum database. The vacuum_db journal file is deleted when the pager
 101333   ** is closed by the DETACH.
 101335   db->autoCommit = 1;
 101337   if( pDb ){
 101338     sqlite3BtreeClose(pDb->pBt);
 101339     pDb->pBt = 0;
 101340     pDb->pSchema = 0;
 101343   /* This both clears the schemas and reduces the size of the db->aDb[]
 101344   ** array. */ 
 101345   sqlite3ResetAllSchemasOfConnection(db);
 101347   return rc;
 101350 #endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
 101352 /************** End of vacuum.c **********************************************/
 101353 /************** Begin file vtab.c ********************************************/
 101355 ** 2006 June 10
 101357 ** The author disclaims copyright to this source code.  In place of
 101358 ** a legal notice, here is a blessing:
 101360 **    May you do good and not evil.
 101361 **    May you find forgiveness for yourself and forgive others.
 101362 **    May you share freely, never taking more than you give.
 101364 *************************************************************************
 101365 ** This file contains code used to help implement virtual tables.
 101367 #ifndef SQLITE_OMIT_VIRTUALTABLE
 101370 ** Before a virtual table xCreate() or xConnect() method is invoked, the
 101371 ** sqlite3.pVtabCtx member variable is set to point to an instance of
 101372 ** this struct allocated on the stack. It is used by the implementation of 
 101373 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
 101374 ** are invoked only from within xCreate and xConnect methods.
 101376 struct VtabCtx {
 101377   VTable *pVTable;    /* The virtual table being constructed */
 101378   Table *pTab;        /* The Table object to which the virtual table belongs */
 101382 ** The actual function that does the work of creating a new module.
 101383 ** This function implements the sqlite3_create_module() and
 101384 ** sqlite3_create_module_v2() interfaces.
 101386 static int createModule(
 101387   sqlite3 *db,                    /* Database in which module is registered */
 101388   const char *zName,              /* Name assigned to this module */
 101389   const sqlite3_module *pModule,  /* The definition of the module */
 101390   void *pAux,                     /* Context pointer for xCreate/xConnect */
 101391   void (*xDestroy)(void *)        /* Module destructor function */
 101393   int rc = SQLITE_OK;
 101394   int nName;
 101396   sqlite3_mutex_enter(db->mutex);
 101397   nName = sqlite3Strlen30(zName);
 101398   if( sqlite3HashFind(&db->aModule, zName, nName) ){
 101399     rc = SQLITE_MISUSE_BKPT;
 101400   }else{
 101401     Module *pMod;
 101402     pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
 101403     if( pMod ){
 101404       Module *pDel;
 101405       char *zCopy = (char *)(&pMod[1]);
 101406       memcpy(zCopy, zName, nName+1);
 101407       pMod->zName = zCopy;
 101408       pMod->pModule = pModule;
 101409       pMod->pAux = pAux;
 101410       pMod->xDestroy = xDestroy;
 101411       pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,nName,(void*)pMod);
 101412       assert( pDel==0 || pDel==pMod );
 101413       if( pDel ){
 101414         db->mallocFailed = 1;
 101415         sqlite3DbFree(db, pDel);
 101419   rc = sqlite3ApiExit(db, rc);
 101420   if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
 101422   sqlite3_mutex_leave(db->mutex);
 101423   return rc;
 101428 ** External API function used to create a new virtual-table module.
 101430 SQLITE_API int sqlite3_create_module(
 101431   sqlite3 *db,                    /* Database in which module is registered */
 101432   const char *zName,              /* Name assigned to this module */
 101433   const sqlite3_module *pModule,  /* The definition of the module */
 101434   void *pAux                      /* Context pointer for xCreate/xConnect */
 101436   return createModule(db, zName, pModule, pAux, 0);
 101440 ** External API function used to create a new virtual-table module.
 101442 SQLITE_API int sqlite3_create_module_v2(
 101443   sqlite3 *db,                    /* Database in which module is registered */
 101444   const char *zName,              /* Name assigned to this module */
 101445   const sqlite3_module *pModule,  /* The definition of the module */
 101446   void *pAux,                     /* Context pointer for xCreate/xConnect */
 101447   void (*xDestroy)(void *)        /* Module destructor function */
 101449   return createModule(db, zName, pModule, pAux, xDestroy);
 101453 ** Lock the virtual table so that it cannot be disconnected.
 101454 ** Locks nest.  Every lock should have a corresponding unlock.
 101455 ** If an unlock is omitted, resources leaks will occur.  
 101457 ** If a disconnect is attempted while a virtual table is locked,
 101458 ** the disconnect is deferred until all locks have been removed.
 101460 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
 101461   pVTab->nRef++;
 101466 ** pTab is a pointer to a Table structure representing a virtual-table.
 101467 ** Return a pointer to the VTable object used by connection db to access 
 101468 ** this virtual-table, if one has been created, or NULL otherwise.
 101470 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
 101471   VTable *pVtab;
 101472   assert( IsVirtual(pTab) );
 101473   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
 101474   return pVtab;
 101478 ** Decrement the ref-count on a virtual table object. When the ref-count
 101479 ** reaches zero, call the xDisconnect() method to delete the object.
 101481 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
 101482   sqlite3 *db = pVTab->db;
 101484   assert( db );
 101485   assert( pVTab->nRef>0 );
 101486   assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
 101488   pVTab->nRef--;
 101489   if( pVTab->nRef==0 ){
 101490     sqlite3_vtab *p = pVTab->pVtab;
 101491     if( p ){
 101492       p->pModule->xDisconnect(p);
 101494     sqlite3DbFree(db, pVTab);
 101499 ** Table p is a virtual table. This function moves all elements in the
 101500 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
 101501 ** database connections to be disconnected at the next opportunity. 
 101502 ** Except, if argument db is not NULL, then the entry associated with
 101503 ** connection db is left in the p->pVTable list.
 101505 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
 101506   VTable *pRet = 0;
 101507   VTable *pVTable = p->pVTable;
 101508   p->pVTable = 0;
 101510   /* Assert that the mutex (if any) associated with the BtShared database 
 101511   ** that contains table p is held by the caller. See header comments 
 101512   ** above function sqlite3VtabUnlockList() for an explanation of why
 101513   ** this makes it safe to access the sqlite3.pDisconnect list of any
 101514   ** database connection that may have an entry in the p->pVTable list.
 101516   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
 101518   while( pVTable ){
 101519     sqlite3 *db2 = pVTable->db;
 101520     VTable *pNext = pVTable->pNext;
 101521     assert( db2 );
 101522     if( db2==db ){
 101523       pRet = pVTable;
 101524       p->pVTable = pRet;
 101525       pRet->pNext = 0;
 101526     }else{
 101527       pVTable->pNext = db2->pDisconnect;
 101528       db2->pDisconnect = pVTable;
 101530     pVTable = pNext;
 101533   assert( !db || pRet );
 101534   return pRet;
 101538 ** Table *p is a virtual table. This function removes the VTable object
 101539 ** for table *p associated with database connection db from the linked
 101540 ** list in p->pVTab. It also decrements the VTable ref count. This is
 101541 ** used when closing database connection db to free all of its VTable
 101542 ** objects without disturbing the rest of the Schema object (which may
 101543 ** be being used by other shared-cache connections).
 101545 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
 101546   VTable **ppVTab;
 101548   assert( IsVirtual(p) );
 101549   assert( sqlite3BtreeHoldsAllMutexes(db) );
 101550   assert( sqlite3_mutex_held(db->mutex) );
 101552   for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
 101553     if( (*ppVTab)->db==db  ){
 101554       VTable *pVTab = *ppVTab;
 101555       *ppVTab = pVTab->pNext;
 101556       sqlite3VtabUnlock(pVTab);
 101557       break;
 101564 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
 101566 ** This function may only be called when the mutexes associated with all
 101567 ** shared b-tree databases opened using connection db are held by the 
 101568 ** caller. This is done to protect the sqlite3.pDisconnect list. The
 101569 ** sqlite3.pDisconnect list is accessed only as follows:
 101571 **   1) By this function. In this case, all BtShared mutexes and the mutex
 101572 **      associated with the database handle itself must be held.
 101574 **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
 101575 **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
 101576 **      associated with the database the virtual table is stored in is held
 101577 **      or, if the virtual table is stored in a non-sharable database, then
 101578 **      the database handle mutex is held.
 101580 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously 
 101581 ** by multiple threads. It is thread-safe.
 101583 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
 101584   VTable *p = db->pDisconnect;
 101585   db->pDisconnect = 0;
 101587   assert( sqlite3BtreeHoldsAllMutexes(db) );
 101588   assert( sqlite3_mutex_held(db->mutex) );
 101590   if( p ){
 101591     sqlite3ExpirePreparedStatements(db);
 101592     do {
 101593       VTable *pNext = p->pNext;
 101594       sqlite3VtabUnlock(p);
 101595       p = pNext;
 101596     }while( p );
 101601 ** Clear any and all virtual-table information from the Table record.
 101602 ** This routine is called, for example, just before deleting the Table
 101603 ** record.
 101605 ** Since it is a virtual-table, the Table structure contains a pointer
 101606 ** to the head of a linked list of VTable structures. Each VTable 
 101607 ** structure is associated with a single sqlite3* user of the schema.
 101608 ** The reference count of the VTable structure associated with database 
 101609 ** connection db is decremented immediately (which may lead to the 
 101610 ** structure being xDisconnected and free). Any other VTable structures
 101611 ** in the list are moved to the sqlite3.pDisconnect list of the associated 
 101612 ** database connection.
 101614 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
 101615   if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
 101616   if( p->azModuleArg ){
 101617     int i;
 101618     for(i=0; i<p->nModuleArg; i++){
 101619       if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
 101621     sqlite3DbFree(db, p->azModuleArg);
 101626 ** Add a new module argument to pTable->azModuleArg[].
 101627 ** The string is not copied - the pointer is stored.  The
 101628 ** string will be freed automatically when the table is
 101629 ** deleted.
 101631 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
 101632   int i = pTable->nModuleArg++;
 101633   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
 101634   char **azModuleArg;
 101635   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
 101636   if( azModuleArg==0 ){
 101637     int j;
 101638     for(j=0; j<i; j++){
 101639       sqlite3DbFree(db, pTable->azModuleArg[j]);
 101641     sqlite3DbFree(db, zArg);
 101642     sqlite3DbFree(db, pTable->azModuleArg);
 101643     pTable->nModuleArg = 0;
 101644   }else{
 101645     azModuleArg[i] = zArg;
 101646     azModuleArg[i+1] = 0;
 101648   pTable->azModuleArg = azModuleArg;
 101652 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
 101653 ** statement.  The module name has been parsed, but the optional list
 101654 ** of parameters that follow the module name are still pending.
 101656 SQLITE_PRIVATE void sqlite3VtabBeginParse(
 101657   Parse *pParse,        /* Parsing context */
 101658   Token *pName1,        /* Name of new table, or database name */
 101659   Token *pName2,        /* Name of new table or NULL */
 101660   Token *pModuleName,   /* Name of the module for the virtual table */
 101661   int ifNotExists       /* No error if the table already exists */
 101663   int iDb;              /* The database the table is being created in */
 101664   Table *pTable;        /* The new virtual table */
 101665   sqlite3 *db;          /* Database connection */
 101667   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
 101668   pTable = pParse->pNewTable;
 101669   if( pTable==0 ) return;
 101670   assert( 0==pTable->pIndex );
 101672   db = pParse->db;
 101673   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
 101674   assert( iDb>=0 );
 101676   pTable->tabFlags |= TF_Virtual;
 101677   pTable->nModuleArg = 0;
 101678   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
 101679   addModuleArgument(db, pTable, 0);
 101680   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
 101681   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
 101683 #ifndef SQLITE_OMIT_AUTHORIZATION
 101684   /* Creating a virtual table invokes the authorization callback twice.
 101685   ** The first invocation, to obtain permission to INSERT a row into the
 101686   ** sqlite_master table, has already been made by sqlite3StartTable().
 101687   ** The second call, to obtain permission to create the table, is made now.
 101689   if( pTable->azModuleArg ){
 101690     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
 101691             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
 101693 #endif
 101697 ** This routine takes the module argument that has been accumulating
 101698 ** in pParse->zArg[] and appends it to the list of arguments on the
 101699 ** virtual table currently under construction in pParse->pTable.
 101701 static void addArgumentToVtab(Parse *pParse){
 101702   if( pParse->sArg.z && pParse->pNewTable ){
 101703     const char *z = (const char*)pParse->sArg.z;
 101704     int n = pParse->sArg.n;
 101705     sqlite3 *db = pParse->db;
 101706     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
 101711 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
 101712 ** has been completely parsed.
 101714 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
 101715   Table *pTab = pParse->pNewTable;  /* The table being constructed */
 101716   sqlite3 *db = pParse->db;         /* The database connection */
 101718   if( pTab==0 ) return;
 101719   addArgumentToVtab(pParse);
 101720   pParse->sArg.z = 0;
 101721   if( pTab->nModuleArg<1 ) return;
 101723   /* If the CREATE VIRTUAL TABLE statement is being entered for the
 101724   ** first time (in other words if the virtual table is actually being
 101725   ** created now instead of just being read out of sqlite_master) then
 101726   ** do additional initialization work and store the statement text
 101727   ** in the sqlite_master table.
 101729   if( !db->init.busy ){
 101730     char *zStmt;
 101731     char *zWhere;
 101732     int iDb;
 101733     Vdbe *v;
 101735     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
 101736     if( pEnd ){
 101737       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
 101739     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
 101741     /* A slot for the record has already been allocated in the 
 101742     ** SQLITE_MASTER table.  We just need to update that slot with all
 101743     ** the information we've collected.  
 101745     ** The VM register number pParse->regRowid holds the rowid of an
 101746     ** entry in the sqlite_master table tht was created for this vtab
 101747     ** by sqlite3StartTable().
 101749     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 101750     sqlite3NestedParse(pParse,
 101751       "UPDATE %Q.%s "
 101752          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
 101753        "WHERE rowid=#%d",
 101754       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 101755       pTab->zName,
 101756       pTab->zName,
 101757       zStmt,
 101758       pParse->regRowid
 101760     sqlite3DbFree(db, zStmt);
 101761     v = sqlite3GetVdbe(pParse);
 101762     sqlite3ChangeCookie(pParse, iDb);
 101764     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
 101765     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
 101766     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
 101767     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
 101768                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
 101771   /* If we are rereading the sqlite_master table create the in-memory
 101772   ** record of the table. The xConnect() method is not called until
 101773   ** the first time the virtual table is used in an SQL statement. This
 101774   ** allows a schema that contains virtual tables to be loaded before
 101775   ** the required virtual table implementations are registered.  */
 101776   else {
 101777     Table *pOld;
 101778     Schema *pSchema = pTab->pSchema;
 101779     const char *zName = pTab->zName;
 101780     int nName = sqlite3Strlen30(zName);
 101781     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
 101782     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
 101783     if( pOld ){
 101784       db->mallocFailed = 1;
 101785       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
 101786       return;
 101788     pParse->pNewTable = 0;
 101793 ** The parser calls this routine when it sees the first token
 101794 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
 101796 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
 101797   addArgumentToVtab(pParse);
 101798   pParse->sArg.z = 0;
 101799   pParse->sArg.n = 0;
 101803 ** The parser calls this routine for each token after the first token
 101804 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
 101806 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
 101807   Token *pArg = &pParse->sArg;
 101808   if( pArg->z==0 ){
 101809     pArg->z = p->z;
 101810     pArg->n = p->n;
 101811   }else{
 101812     assert(pArg->z < p->z);
 101813     pArg->n = (int)(&p->z[p->n] - pArg->z);
 101818 ** Invoke a virtual table constructor (either xCreate or xConnect). The
 101819 ** pointer to the function to invoke is passed as the fourth parameter
 101820 ** to this procedure.
 101822 static int vtabCallConstructor(
 101823   sqlite3 *db, 
 101824   Table *pTab,
 101825   Module *pMod,
 101826   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
 101827   char **pzErr
 101829   VtabCtx sCtx, *pPriorCtx;
 101830   VTable *pVTable;
 101831   int rc;
 101832   const char *const*azArg = (const char *const*)pTab->azModuleArg;
 101833   int nArg = pTab->nModuleArg;
 101834   char *zErr = 0;
 101835   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
 101836   int iDb;
 101838   if( !zModuleName ){
 101839     return SQLITE_NOMEM;
 101842   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
 101843   if( !pVTable ){
 101844     sqlite3DbFree(db, zModuleName);
 101845     return SQLITE_NOMEM;
 101847   pVTable->db = db;
 101848   pVTable->pMod = pMod;
 101850   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 101851   pTab->azModuleArg[1] = db->aDb[iDb].zName;
 101853   /* Invoke the virtual table constructor */
 101854   assert( &db->pVtabCtx );
 101855   assert( xConstruct );
 101856   sCtx.pTab = pTab;
 101857   sCtx.pVTable = pVTable;
 101858   pPriorCtx = db->pVtabCtx;
 101859   db->pVtabCtx = &sCtx;
 101860   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
 101861   db->pVtabCtx = pPriorCtx;
 101862   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
 101864   if( SQLITE_OK!=rc ){
 101865     if( zErr==0 ){
 101866       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
 101867     }else {
 101868       *pzErr = sqlite3MPrintf(db, "%s", zErr);
 101869       sqlite3_free(zErr);
 101871     sqlite3DbFree(db, pVTable);
 101872   }else if( ALWAYS(pVTable->pVtab) ){
 101873     /* Justification of ALWAYS():  A correct vtab constructor must allocate
 101874     ** the sqlite3_vtab object if successful.  */
 101875     pVTable->pVtab->pModule = pMod->pModule;
 101876     pVTable->nRef = 1;
 101877     if( sCtx.pTab ){
 101878       const char *zFormat = "vtable constructor did not declare schema: %s";
 101879       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
 101880       sqlite3VtabUnlock(pVTable);
 101881       rc = SQLITE_ERROR;
 101882     }else{
 101883       int iCol;
 101884       /* If everything went according to plan, link the new VTable structure
 101885       ** into the linked list headed by pTab->pVTable. Then loop through the 
 101886       ** columns of the table to see if any of them contain the token "hidden".
 101887       ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
 101888       ** the type string.  */
 101889       pVTable->pNext = pTab->pVTable;
 101890       pTab->pVTable = pVTable;
 101892       for(iCol=0; iCol<pTab->nCol; iCol++){
 101893         char *zType = pTab->aCol[iCol].zType;
 101894         int nType;
 101895         int i = 0;
 101896         if( !zType ) continue;
 101897         nType = sqlite3Strlen30(zType);
 101898         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
 101899           for(i=0; i<nType; i++){
 101900             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
 101901              && (zType[i+7]=='\0' || zType[i+7]==' ')
 101903               i++;
 101904               break;
 101908         if( i<nType ){
 101909           int j;
 101910           int nDel = 6 + (zType[i+6] ? 1 : 0);
 101911           for(j=i; (j+nDel)<=nType; j++){
 101912             zType[j] = zType[j+nDel];
 101914           if( zType[i]=='\0' && i>0 ){
 101915             assert(zType[i-1]==' ');
 101916             zType[i-1] = '\0';
 101918           pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
 101924   sqlite3DbFree(db, zModuleName);
 101925   return rc;
 101929 ** This function is invoked by the parser to call the xConnect() method
 101930 ** of the virtual table pTab. If an error occurs, an error code is returned 
 101931 ** and an error left in pParse.
 101933 ** This call is a no-op if table pTab is not a virtual table.
 101935 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
 101936   sqlite3 *db = pParse->db;
 101937   const char *zMod;
 101938   Module *pMod;
 101939   int rc;
 101941   assert( pTab );
 101942   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
 101943     return SQLITE_OK;
 101946   /* Locate the required virtual table module */
 101947   zMod = pTab->azModuleArg[0];
 101948   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
 101950   if( !pMod ){
 101951     const char *zModule = pTab->azModuleArg[0];
 101952     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
 101953     rc = SQLITE_ERROR;
 101954   }else{
 101955     char *zErr = 0;
 101956     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
 101957     if( rc!=SQLITE_OK ){
 101958       sqlite3ErrorMsg(pParse, "%s", zErr);
 101960     sqlite3DbFree(db, zErr);
 101963   return rc;
 101966 ** Grow the db->aVTrans[] array so that there is room for at least one
 101967 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
 101969 static int growVTrans(sqlite3 *db){
 101970   const int ARRAY_INCR = 5;
 101972   /* Grow the sqlite3.aVTrans array if required */
 101973   if( (db->nVTrans%ARRAY_INCR)==0 ){
 101974     VTable **aVTrans;
 101975     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
 101976     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
 101977     if( !aVTrans ){
 101978       return SQLITE_NOMEM;
 101980     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
 101981     db->aVTrans = aVTrans;
 101984   return SQLITE_OK;
 101988 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
 101989 ** have already been reserved using growVTrans().
 101991 static void addToVTrans(sqlite3 *db, VTable *pVTab){
 101992   /* Add pVtab to the end of sqlite3.aVTrans */
 101993   db->aVTrans[db->nVTrans++] = pVTab;
 101994   sqlite3VtabLock(pVTab);
 101998 ** This function is invoked by the vdbe to call the xCreate method
 101999 ** of the virtual table named zTab in database iDb. 
 102001 ** If an error occurs, *pzErr is set to point an an English language
 102002 ** description of the error and an SQLITE_XXX error code is returned.
 102003 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
 102005 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
 102006   int rc = SQLITE_OK;
 102007   Table *pTab;
 102008   Module *pMod;
 102009   const char *zMod;
 102011   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
 102012   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
 102014   /* Locate the required virtual table module */
 102015   zMod = pTab->azModuleArg[0];
 102016   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
 102018   /* If the module has been registered and includes a Create method, 
 102019   ** invoke it now. If the module has not been registered, return an 
 102020   ** error. Otherwise, do nothing.
 102022   if( !pMod ){
 102023     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
 102024     rc = SQLITE_ERROR;
 102025   }else{
 102026     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
 102029   /* Justification of ALWAYS():  The xConstructor method is required to
 102030   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
 102031   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
 102032     rc = growVTrans(db);
 102033     if( rc==SQLITE_OK ){
 102034       addToVTrans(db, sqlite3GetVTable(db, pTab));
 102038   return rc;
 102042 ** This function is used to set the schema of a virtual table.  It is only
 102043 ** valid to call this function from within the xCreate() or xConnect() of a
 102044 ** virtual table module.
 102046 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
 102047   Parse *pParse;
 102049   int rc = SQLITE_OK;
 102050   Table *pTab;
 102051   char *zErr = 0;
 102053   sqlite3_mutex_enter(db->mutex);
 102054   if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
 102055     sqlite3Error(db, SQLITE_MISUSE, 0);
 102056     sqlite3_mutex_leave(db->mutex);
 102057     return SQLITE_MISUSE_BKPT;
 102059   assert( (pTab->tabFlags & TF_Virtual)!=0 );
 102061   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
 102062   if( pParse==0 ){
 102063     rc = SQLITE_NOMEM;
 102064   }else{
 102065     pParse->declareVtab = 1;
 102066     pParse->db = db;
 102067     pParse->nQueryLoop = 1;
 102069     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
 102070      && pParse->pNewTable
 102071      && !db->mallocFailed
 102072      && !pParse->pNewTable->pSelect
 102073      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
 102075       if( !pTab->aCol ){
 102076         pTab->aCol = pParse->pNewTable->aCol;
 102077         pTab->nCol = pParse->pNewTable->nCol;
 102078         pParse->pNewTable->nCol = 0;
 102079         pParse->pNewTable->aCol = 0;
 102081       db->pVtabCtx->pTab = 0;
 102082     }else{
 102083       sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
 102084       sqlite3DbFree(db, zErr);
 102085       rc = SQLITE_ERROR;
 102087     pParse->declareVtab = 0;
 102089     if( pParse->pVdbe ){
 102090       sqlite3VdbeFinalize(pParse->pVdbe);
 102092     sqlite3DeleteTable(db, pParse->pNewTable);
 102093     sqlite3StackFree(db, pParse);
 102096   assert( (rc&0xff)==rc );
 102097   rc = sqlite3ApiExit(db, rc);
 102098   sqlite3_mutex_leave(db->mutex);
 102099   return rc;
 102103 ** This function is invoked by the vdbe to call the xDestroy method
 102104 ** of the virtual table named zTab in database iDb. This occurs
 102105 ** when a DROP TABLE is mentioned.
 102107 ** This call is a no-op if zTab is not a virtual table.
 102109 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
 102110   int rc = SQLITE_OK;
 102111   Table *pTab;
 102113   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
 102114   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
 102115     VTable *p = vtabDisconnectAll(db, pTab);
 102117     assert( rc==SQLITE_OK );
 102118     rc = p->pMod->pModule->xDestroy(p->pVtab);
 102120     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
 102121     if( rc==SQLITE_OK ){
 102122       assert( pTab->pVTable==p && p->pNext==0 );
 102123       p->pVtab = 0;
 102124       pTab->pVTable = 0;
 102125       sqlite3VtabUnlock(p);
 102129   return rc;
 102133 ** This function invokes either the xRollback or xCommit method
 102134 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
 102135 ** called is identified by the second argument, "offset", which is
 102136 ** the offset of the method to call in the sqlite3_module structure.
 102138 ** The array is cleared after invoking the callbacks. 
 102140 static void callFinaliser(sqlite3 *db, int offset){
 102141   int i;
 102142   if( db->aVTrans ){
 102143     for(i=0; i<db->nVTrans; i++){
 102144       VTable *pVTab = db->aVTrans[i];
 102145       sqlite3_vtab *p = pVTab->pVtab;
 102146       if( p ){
 102147         int (*x)(sqlite3_vtab *);
 102148         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
 102149         if( x ) x(p);
 102151       pVTab->iSavepoint = 0;
 102152       sqlite3VtabUnlock(pVTab);
 102154     sqlite3DbFree(db, db->aVTrans);
 102155     db->nVTrans = 0;
 102156     db->aVTrans = 0;
 102161 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
 102162 ** array. Return the error code for the first error that occurs, or
 102163 ** SQLITE_OK if all xSync operations are successful.
 102165 ** Set *pzErrmsg to point to a buffer that should be released using 
 102166 ** sqlite3DbFree() containing an error message, if one is available.
 102168 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
 102169   int i;
 102170   int rc = SQLITE_OK;
 102171   VTable **aVTrans = db->aVTrans;
 102173   db->aVTrans = 0;
 102174   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
 102175     int (*x)(sqlite3_vtab *);
 102176     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
 102177     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
 102178       rc = x(pVtab);
 102179       sqlite3DbFree(db, *pzErrmsg);
 102180       *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
 102181       sqlite3_free(pVtab->zErrMsg);
 102184   db->aVTrans = aVTrans;
 102185   return rc;
 102189 ** Invoke the xRollback method of all virtual tables in the 
 102190 ** sqlite3.aVTrans array. Then clear the array itself.
 102192 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
 102193   callFinaliser(db, offsetof(sqlite3_module,xRollback));
 102194   return SQLITE_OK;
 102198 ** Invoke the xCommit method of all virtual tables in the 
 102199 ** sqlite3.aVTrans array. Then clear the array itself.
 102201 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
 102202   callFinaliser(db, offsetof(sqlite3_module,xCommit));
 102203   return SQLITE_OK;
 102207 ** If the virtual table pVtab supports the transaction interface
 102208 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
 102209 ** not currently open, invoke the xBegin method now.
 102211 ** If the xBegin call is successful, place the sqlite3_vtab pointer
 102212 ** in the sqlite3.aVTrans array.
 102214 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
 102215   int rc = SQLITE_OK;
 102216   const sqlite3_module *pModule;
 102218   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
 102219   ** than zero, then this function is being called from within a
 102220   ** virtual module xSync() callback. It is illegal to write to 
 102221   ** virtual module tables in this case, so return SQLITE_LOCKED.
 102223   if( sqlite3VtabInSync(db) ){
 102224     return SQLITE_LOCKED;
 102226   if( !pVTab ){
 102227     return SQLITE_OK;
 102229   pModule = pVTab->pVtab->pModule;
 102231   if( pModule->xBegin ){
 102232     int i;
 102234     /* If pVtab is already in the aVTrans array, return early */
 102235     for(i=0; i<db->nVTrans; i++){
 102236       if( db->aVTrans[i]==pVTab ){
 102237         return SQLITE_OK;
 102241     /* Invoke the xBegin method. If successful, add the vtab to the 
 102242     ** sqlite3.aVTrans[] array. */
 102243     rc = growVTrans(db);
 102244     if( rc==SQLITE_OK ){
 102245       rc = pModule->xBegin(pVTab->pVtab);
 102246       if( rc==SQLITE_OK ){
 102247         addToVTrans(db, pVTab);
 102251   return rc;
 102255 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
 102256 ** virtual tables that currently have an open transaction. Pass iSavepoint
 102257 ** as the second argument to the virtual table method invoked.
 102259 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
 102260 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is 
 102261 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
 102262 ** an open transaction is invoked.
 102264 ** If any virtual table method returns an error code other than SQLITE_OK, 
 102265 ** processing is abandoned and the error returned to the caller of this
 102266 ** function immediately. If all calls to virtual table methods are successful,
 102267 ** SQLITE_OK is returned.
 102269 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
 102270   int rc = SQLITE_OK;
 102272   assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
 102273   assert( iSavepoint>=0 );
 102274   if( db->aVTrans ){
 102275     int i;
 102276     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
 102277       VTable *pVTab = db->aVTrans[i];
 102278       const sqlite3_module *pMod = pVTab->pMod->pModule;
 102279       if( pVTab->pVtab && pMod->iVersion>=2 ){
 102280         int (*xMethod)(sqlite3_vtab *, int);
 102281         switch( op ){
 102282           case SAVEPOINT_BEGIN:
 102283             xMethod = pMod->xSavepoint;
 102284             pVTab->iSavepoint = iSavepoint+1;
 102285             break;
 102286           case SAVEPOINT_ROLLBACK:
 102287             xMethod = pMod->xRollbackTo;
 102288             break;
 102289           default:
 102290             xMethod = pMod->xRelease;
 102291             break;
 102293         if( xMethod && pVTab->iSavepoint>iSavepoint ){
 102294           rc = xMethod(pVTab->pVtab, iSavepoint);
 102299   return rc;
 102303 ** The first parameter (pDef) is a function implementation.  The
 102304 ** second parameter (pExpr) is the first argument to this function.
 102305 ** If pExpr is a column in a virtual table, then let the virtual
 102306 ** table implementation have an opportunity to overload the function.
 102308 ** This routine is used to allow virtual table implementations to
 102309 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
 102311 ** Return either the pDef argument (indicating no change) or a 
 102312 ** new FuncDef structure that is marked as ephemeral using the
 102313 ** SQLITE_FUNC_EPHEM flag.
 102315 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
 102316   sqlite3 *db,    /* Database connection for reporting malloc problems */
 102317   FuncDef *pDef,  /* Function to possibly overload */
 102318   int nArg,       /* Number of arguments to the function */
 102319   Expr *pExpr     /* First argument to the function */
 102321   Table *pTab;
 102322   sqlite3_vtab *pVtab;
 102323   sqlite3_module *pMod;
 102324   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
 102325   void *pArg = 0;
 102326   FuncDef *pNew;
 102327   int rc = 0;
 102328   char *zLowerName;
 102329   unsigned char *z;
 102332   /* Check to see the left operand is a column in a virtual table */
 102333   if( NEVER(pExpr==0) ) return pDef;
 102334   if( pExpr->op!=TK_COLUMN ) return pDef;
 102335   pTab = pExpr->pTab;
 102336   if( NEVER(pTab==0) ) return pDef;
 102337   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
 102338   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
 102339   assert( pVtab!=0 );
 102340   assert( pVtab->pModule!=0 );
 102341   pMod = (sqlite3_module *)pVtab->pModule;
 102342   if( pMod->xFindFunction==0 ) return pDef;
 102344   /* Call the xFindFunction method on the virtual table implementation
 102345   ** to see if the implementation wants to overload this function 
 102347   zLowerName = sqlite3DbStrDup(db, pDef->zName);
 102348   if( zLowerName ){
 102349     for(z=(unsigned char*)zLowerName; *z; z++){
 102350       *z = sqlite3UpperToLower[*z];
 102352     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
 102353     sqlite3DbFree(db, zLowerName);
 102355   if( rc==0 ){
 102356     return pDef;
 102359   /* Create a new ephemeral function definition for the overloaded
 102360   ** function */
 102361   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
 102362                              + sqlite3Strlen30(pDef->zName) + 1);
 102363   if( pNew==0 ){
 102364     return pDef;
 102366   *pNew = *pDef;
 102367   pNew->zName = (char *)&pNew[1];
 102368   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
 102369   pNew->xFunc = xFunc;
 102370   pNew->pUserData = pArg;
 102371   pNew->flags |= SQLITE_FUNC_EPHEM;
 102372   return pNew;
 102376 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
 102377 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
 102378 ** array if it is missing.  If pTab is already in the array, this routine
 102379 ** is a no-op.
 102381 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
 102382   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 102383   int i, n;
 102384   Table **apVtabLock;
 102386   assert( IsVirtual(pTab) );
 102387   for(i=0; i<pToplevel->nVtabLock; i++){
 102388     if( pTab==pToplevel->apVtabLock[i] ) return;
 102390   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
 102391   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
 102392   if( apVtabLock ){
 102393     pToplevel->apVtabLock = apVtabLock;
 102394     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
 102395   }else{
 102396     pToplevel->db->mallocFailed = 1;
 102401 ** Return the ON CONFLICT resolution mode in effect for the virtual
 102402 ** table update operation currently in progress.
 102404 ** The results of this routine are undefined unless it is called from
 102405 ** within an xUpdate method.
 102407 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
 102408   static const unsigned char aMap[] = { 
 102409     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE 
 102411   assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
 102412   assert( OE_Ignore==4 && OE_Replace==5 );
 102413   assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
 102414   return (int)aMap[db->vtabOnConflict-1];
 102418 ** Call from within the xCreate() or xConnect() methods to provide 
 102419 ** the SQLite core with additional information about the behavior
 102420 ** of the virtual table being implemented.
 102422 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
 102423   va_list ap;
 102424   int rc = SQLITE_OK;
 102426   sqlite3_mutex_enter(db->mutex);
 102428   va_start(ap, op);
 102429   switch( op ){
 102430     case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
 102431       VtabCtx *p = db->pVtabCtx;
 102432       if( !p ){
 102433         rc = SQLITE_MISUSE_BKPT;
 102434       }else{
 102435         assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
 102436         p->pVTable->bConstraint = (u8)va_arg(ap, int);
 102438       break;
 102440     default:
 102441       rc = SQLITE_MISUSE_BKPT;
 102442       break;
 102444   va_end(ap);
 102446   if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
 102447   sqlite3_mutex_leave(db->mutex);
 102448   return rc;
 102451 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 102453 /************** End of vtab.c ************************************************/
 102454 /************** Begin file where.c *******************************************/
 102456 ** 2001 September 15
 102458 ** The author disclaims copyright to this source code.  In place of
 102459 ** a legal notice, here is a blessing:
 102461 **    May you do good and not evil.
 102462 **    May you find forgiveness for yourself and forgive others.
 102463 **    May you share freely, never taking more than you give.
 102465 *************************************************************************
 102466 ** This module contains C code that generates VDBE code used to process
 102467 ** the WHERE clause of SQL statements.  This module is responsible for
 102468 ** generating the code that loops through a table looking for applicable
 102469 ** rows.  Indices are selected and used to speed the search when doing
 102470 ** so is applicable.  Because this module is responsible for selecting
 102471 ** indices, you might also think of this module as the "query optimizer".
 102476 ** Trace output macros
 102478 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
 102479 /***/ int sqlite3WhereTrace = 0;
 102480 #endif
 102481 #if defined(SQLITE_DEBUG) \
 102482     && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
 102483 # define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
 102484 #else
 102485 # define WHERETRACE(X)
 102486 #endif
 102488 /* Forward reference
 102490 typedef struct WhereClause WhereClause;
 102491 typedef struct WhereMaskSet WhereMaskSet;
 102492 typedef struct WhereOrInfo WhereOrInfo;
 102493 typedef struct WhereAndInfo WhereAndInfo;
 102494 typedef struct WhereCost WhereCost;
 102497 ** The query generator uses an array of instances of this structure to
 102498 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
 102499 ** clause subexpression is separated from the others by AND operators,
 102500 ** usually, or sometimes subexpressions separated by OR.
 102502 ** All WhereTerms are collected into a single WhereClause structure.  
 102503 ** The following identity holds:
 102505 **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
 102507 ** When a term is of the form:
 102509 **              X <op> <expr>
 102511 ** where X is a column name and <op> is one of certain operators,
 102512 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
 102513 ** cursor number and column number for X.  WhereTerm.eOperator records
 102514 ** the <op> using a bitmask encoding defined by WO_xxx below.  The
 102515 ** use of a bitmask encoding for the operator allows us to search
 102516 ** quickly for terms that match any of several different operators.
 102518 ** A WhereTerm might also be two or more subterms connected by OR:
 102520 **         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
 102522 ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
 102523 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
 102524 ** is collected about the
 102526 ** If a term in the WHERE clause does not match either of the two previous
 102527 ** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
 102528 ** to the original subexpression content and wtFlags is set up appropriately
 102529 ** but no other fields in the WhereTerm object are meaningful.
 102531 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
 102532 ** but they do so indirectly.  A single WhereMaskSet structure translates
 102533 ** cursor number into bits and the translated bit is stored in the prereq
 102534 ** fields.  The translation is used in order to maximize the number of
 102535 ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
 102536 ** spread out over the non-negative integers.  For example, the cursor
 102537 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
 102538 ** translates these sparse cursor numbers into consecutive integers
 102539 ** beginning with 0 in order to make the best possible use of the available
 102540 ** bits in the Bitmask.  So, in the example above, the cursor numbers
 102541 ** would be mapped into integers 0 through 7.
 102543 ** The number of terms in a join is limited by the number of bits
 102544 ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
 102545 ** is only able to process joins with 64 or fewer tables.
 102547 typedef struct WhereTerm WhereTerm;
 102548 struct WhereTerm {
 102549   Expr *pExpr;            /* Pointer to the subexpression that is this term */
 102550   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
 102551   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
 102552   union {
 102553     int leftColumn;         /* Column number of X in "X <op> <expr>" */
 102554     WhereOrInfo *pOrInfo;   /* Extra information if eOperator==WO_OR */
 102555     WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
 102556   } u;
 102557   u16 eOperator;          /* A WO_xx value describing <op> */
 102558   u8 wtFlags;             /* TERM_xxx bit flags.  See below */
 102559   u8 nChild;              /* Number of children that must disable us */
 102560   WhereClause *pWC;       /* The clause this term is part of */
 102561   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
 102562   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
 102566 ** Allowed values of WhereTerm.wtFlags
 102568 #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
 102569 #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
 102570 #define TERM_CODED      0x04   /* This term is already coded */
 102571 #define TERM_COPIED     0x08   /* Has a child */
 102572 #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
 102573 #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
 102574 #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
 102575 #ifdef SQLITE_ENABLE_STAT3
 102576 #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
 102577 #else
 102578 #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
 102579 #endif
 102582 ** An instance of the following structure holds all information about a
 102583 ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
 102585 ** Explanation of pOuter:  For a WHERE clause of the form
 102587 **           a AND ((b AND c) OR (d AND e)) AND f
 102589 ** There are separate WhereClause objects for the whole clause and for
 102590 ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
 102591 ** subclauses points to the WhereClause object for the whole clause.
 102593 struct WhereClause {
 102594   Parse *pParse;           /* The parser context */
 102595   WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
 102596   Bitmask vmask;           /* Bitmask identifying virtual table cursors */
 102597   WhereClause *pOuter;     /* Outer conjunction */
 102598   u8 op;                   /* Split operator.  TK_AND or TK_OR */
 102599   u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
 102600   int nTerm;               /* Number of terms */
 102601   int nSlot;               /* Number of entries in a[] */
 102602   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
 102603 #if defined(SQLITE_SMALL_STACK)
 102604   WhereTerm aStatic[1];    /* Initial static space for a[] */
 102605 #else
 102606   WhereTerm aStatic[8];    /* Initial static space for a[] */
 102607 #endif
 102611 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
 102612 ** a dynamically allocated instance of the following structure.
 102614 struct WhereOrInfo {
 102615   WhereClause wc;          /* Decomposition into subterms */
 102616   Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
 102620 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
 102621 ** a dynamically allocated instance of the following structure.
 102623 struct WhereAndInfo {
 102624   WhereClause wc;          /* The subexpression broken out */
 102628 ** An instance of the following structure keeps track of a mapping
 102629 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
 102631 ** The VDBE cursor numbers are small integers contained in 
 102632 ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE 
 102633 ** clause, the cursor numbers might not begin with 0 and they might
 102634 ** contain gaps in the numbering sequence.  But we want to make maximum
 102635 ** use of the bits in our bitmasks.  This structure provides a mapping
 102636 ** from the sparse cursor numbers into consecutive integers beginning
 102637 ** with 0.
 102639 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
 102640 ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
 102642 ** For example, if the WHERE clause expression used these VDBE
 102643 ** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
 102644 ** would map those cursor numbers into bits 0 through 5.
 102646 ** Note that the mapping is not necessarily ordered.  In the example
 102647 ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
 102648 ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
 102649 ** does not really matter.  What is important is that sparse cursor
 102650 ** numbers all get mapped into bit numbers that begin with 0 and contain
 102651 ** no gaps.
 102653 struct WhereMaskSet {
 102654   int n;                        /* Number of assigned cursor values */
 102655   int ix[BMS];                  /* Cursor assigned to each bit */
 102659 ** A WhereCost object records a lookup strategy and the estimated
 102660 ** cost of pursuing that strategy.
 102662 struct WhereCost {
 102663   WherePlan plan;    /* The lookup strategy */
 102664   double rCost;      /* Overall cost of pursuing this search strategy */
 102665   Bitmask used;      /* Bitmask of cursors used by this plan */
 102669 ** Bitmasks for the operators that indices are able to exploit.  An
 102670 ** OR-ed combination of these values can be used when searching for
 102671 ** terms in the where clause.
 102673 #define WO_IN     0x001
 102674 #define WO_EQ     0x002
 102675 #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
 102676 #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
 102677 #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
 102678 #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
 102679 #define WO_MATCH  0x040
 102680 #define WO_ISNULL 0x080
 102681 #define WO_OR     0x100       /* Two or more OR-connected terms */
 102682 #define WO_AND    0x200       /* Two or more AND-connected terms */
 102683 #define WO_NOOP   0x800       /* This term does not restrict search space */
 102685 #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
 102686 #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
 102689 ** Value for wsFlags returned by bestIndex() and stored in
 102690 ** WhereLevel.wsFlags.  These flags determine which search
 102691 ** strategies are appropriate.
 102693 ** The least significant 12 bits is reserved as a mask for WO_ values above.
 102694 ** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
 102695 ** But if the table is the right table of a left join, WhereLevel.wsFlags
 102696 ** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
 102697 ** the "op" parameter to findTerm when we are resolving equality constraints.
 102698 ** ISNULL constraints will then not be used on the right table of a left
 102699 ** join.  Tickets #2177 and #2189.
 102701 #define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
 102702 #define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
 102703 #define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
 102704 #define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
 102705 #define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
 102706 #define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
 102707 #define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
 102708 #define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
 102709 #define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
 102710 #define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
 102711 #define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
 102712 #define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
 102713 #define WHERE_IDX_ONLY     0x00400000  /* Use index only - omit table */
 102714 #define WHERE_ORDERED      0x00800000  /* Output will appear in correct order */
 102715 #define WHERE_REVERSE      0x01000000  /* Scan in reverse order */
 102716 #define WHERE_UNIQUE       0x02000000  /* Selects no more than one row */
 102717 #define WHERE_ALL_UNIQUE   0x04000000  /* This and all prior have one row */
 102718 #define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
 102719 #define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
 102720 #define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
 102721 #define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
 102722 #define WHERE_COVER_SCAN   0x80000000  /* Full scan of a covering index */
 102725 ** This module contains many separate subroutines that work together to
 102726 ** find the best indices to use for accessing a particular table in a query.
 102727 ** An instance of the following structure holds context information about the
 102728 ** index search so that it can be more easily passed between the various
 102729 ** routines.
 102731 typedef struct WhereBestIdx WhereBestIdx;
 102732 struct WhereBestIdx {
 102733   Parse *pParse;                  /* Parser context */
 102734   WhereClause *pWC;               /* The WHERE clause */
 102735   struct SrcList_item *pSrc;      /* The FROM clause term to search */
 102736   Bitmask notReady;               /* Mask of cursors not available */
 102737   Bitmask notValid;               /* Cursors not available for any purpose */
 102738   ExprList *pOrderBy;             /* The ORDER BY clause */
 102739   ExprList *pDistinct;            /* The select-list if query is DISTINCT */
 102740   sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
 102741   int i, n;                       /* Which loop is being coded; # of loops */
 102742   WhereLevel *aLevel;             /* Info about outer loops */
 102743   WhereCost cost;                 /* Lowest cost query plan */
 102747 ** Return TRUE if the probe cost is less than the baseline cost
 102749 static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
 102750   if( pProbe->rCost<pBaseline->rCost ) return 1;
 102751   if( pProbe->rCost>pBaseline->rCost ) return 0;
 102752   if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
 102753   if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
 102754   return 0;
 102758 ** Initialize a preallocated WhereClause structure.
 102760 static void whereClauseInit(
 102761   WhereClause *pWC,        /* The WhereClause to be initialized */
 102762   Parse *pParse,           /* The parsing context */
 102763   WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
 102764   u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
 102766   pWC->pParse = pParse;
 102767   pWC->pMaskSet = pMaskSet;
 102768   pWC->pOuter = 0;
 102769   pWC->nTerm = 0;
 102770   pWC->nSlot = ArraySize(pWC->aStatic);
 102771   pWC->a = pWC->aStatic;
 102772   pWC->vmask = 0;
 102773   pWC->wctrlFlags = wctrlFlags;
 102776 /* Forward reference */
 102777 static void whereClauseClear(WhereClause*);
 102780 ** Deallocate all memory associated with a WhereOrInfo object.
 102782 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
 102783   whereClauseClear(&p->wc);
 102784   sqlite3DbFree(db, p);
 102788 ** Deallocate all memory associated with a WhereAndInfo object.
 102790 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
 102791   whereClauseClear(&p->wc);
 102792   sqlite3DbFree(db, p);
 102796 ** Deallocate a WhereClause structure.  The WhereClause structure
 102797 ** itself is not freed.  This routine is the inverse of whereClauseInit().
 102799 static void whereClauseClear(WhereClause *pWC){
 102800   int i;
 102801   WhereTerm *a;
 102802   sqlite3 *db = pWC->pParse->db;
 102803   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
 102804     if( a->wtFlags & TERM_DYNAMIC ){
 102805       sqlite3ExprDelete(db, a->pExpr);
 102807     if( a->wtFlags & TERM_ORINFO ){
 102808       whereOrInfoDelete(db, a->u.pOrInfo);
 102809     }else if( a->wtFlags & TERM_ANDINFO ){
 102810       whereAndInfoDelete(db, a->u.pAndInfo);
 102813   if( pWC->a!=pWC->aStatic ){
 102814     sqlite3DbFree(db, pWC->a);
 102819 ** Add a single new WhereTerm entry to the WhereClause object pWC.
 102820 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
 102821 ** The index in pWC->a[] of the new WhereTerm is returned on success.
 102822 ** 0 is returned if the new WhereTerm could not be added due to a memory
 102823 ** allocation error.  The memory allocation failure will be recorded in
 102824 ** the db->mallocFailed flag so that higher-level functions can detect it.
 102826 ** This routine will increase the size of the pWC->a[] array as necessary.
 102828 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
 102829 ** for freeing the expression p is assumed by the WhereClause object pWC.
 102830 ** This is true even if this routine fails to allocate a new WhereTerm.
 102832 ** WARNING:  This routine might reallocate the space used to store
 102833 ** WhereTerms.  All pointers to WhereTerms should be invalidated after
 102834 ** calling this routine.  Such pointers may be reinitialized by referencing
 102835 ** the pWC->a[] array.
 102837 static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
 102838   WhereTerm *pTerm;
 102839   int idx;
 102840   testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
 102841   if( pWC->nTerm>=pWC->nSlot ){
 102842     WhereTerm *pOld = pWC->a;
 102843     sqlite3 *db = pWC->pParse->db;
 102844     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
 102845     if( pWC->a==0 ){
 102846       if( wtFlags & TERM_DYNAMIC ){
 102847         sqlite3ExprDelete(db, p);
 102849       pWC->a = pOld;
 102850       return 0;
 102852     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
 102853     if( pOld!=pWC->aStatic ){
 102854       sqlite3DbFree(db, pOld);
 102856     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
 102858   pTerm = &pWC->a[idx = pWC->nTerm++];
 102859   pTerm->pExpr = p;
 102860   pTerm->wtFlags = wtFlags;
 102861   pTerm->pWC = pWC;
 102862   pTerm->iParent = -1;
 102863   return idx;
 102867 ** This routine identifies subexpressions in the WHERE clause where
 102868 ** each subexpression is separated by the AND operator or some other
 102869 ** operator specified in the op parameter.  The WhereClause structure
 102870 ** is filled with pointers to subexpressions.  For example:
 102872 **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
 102873 **           \________/     \_______________/     \________________/
 102874 **            slot[0]            slot[1]               slot[2]
 102876 ** The original WHERE clause in pExpr is unaltered.  All this routine
 102877 ** does is make slot[] entries point to substructure within pExpr.
 102879 ** In the previous sentence and in the diagram, "slot[]" refers to
 102880 ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
 102881 ** all terms of the WHERE clause.
 102883 static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
 102884   pWC->op = (u8)op;
 102885   if( pExpr==0 ) return;
 102886   if( pExpr->op!=op ){
 102887     whereClauseInsert(pWC, pExpr, 0);
 102888   }else{
 102889     whereSplit(pWC, pExpr->pLeft, op);
 102890     whereSplit(pWC, pExpr->pRight, op);
 102895 ** Initialize an expression mask set (a WhereMaskSet object)
 102897 #define initMaskSet(P)  memset(P, 0, sizeof(*P))
 102900 ** Return the bitmask for the given cursor number.  Return 0 if
 102901 ** iCursor is not in the set.
 102903 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
 102904   int i;
 102905   assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
 102906   for(i=0; i<pMaskSet->n; i++){
 102907     if( pMaskSet->ix[i]==iCursor ){
 102908       return ((Bitmask)1)<<i;
 102911   return 0;
 102915 ** Create a new mask for cursor iCursor.
 102917 ** There is one cursor per table in the FROM clause.  The number of
 102918 ** tables in the FROM clause is limited by a test early in the
 102919 ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
 102920 ** array will never overflow.
 102922 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
 102923   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
 102924   pMaskSet->ix[pMaskSet->n++] = iCursor;
 102928 ** This routine walks (recursively) an expression tree and generates
 102929 ** a bitmask indicating which tables are used in that expression
 102930 ** tree.
 102932 ** In order for this routine to work, the calling function must have
 102933 ** previously invoked sqlite3ResolveExprNames() on the expression.  See
 102934 ** the header comment on that routine for additional information.
 102935 ** The sqlite3ResolveExprNames() routines looks for column names and
 102936 ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
 102937 ** the VDBE cursor number of the table.  This routine just has to
 102938 ** translate the cursor numbers into bitmask values and OR all
 102939 ** the bitmasks together.
 102941 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
 102942 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
 102943 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
 102944   Bitmask mask = 0;
 102945   if( p==0 ) return 0;
 102946   if( p->op==TK_COLUMN ){
 102947     mask = getMask(pMaskSet, p->iTable);
 102948     return mask;
 102950   mask = exprTableUsage(pMaskSet, p->pRight);
 102951   mask |= exprTableUsage(pMaskSet, p->pLeft);
 102952   if( ExprHasProperty(p, EP_xIsSelect) ){
 102953     mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
 102954   }else{
 102955     mask |= exprListTableUsage(pMaskSet, p->x.pList);
 102957   return mask;
 102959 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
 102960   int i;
 102961   Bitmask mask = 0;
 102962   if( pList ){
 102963     for(i=0; i<pList->nExpr; i++){
 102964       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
 102967   return mask;
 102969 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
 102970   Bitmask mask = 0;
 102971   while( pS ){
 102972     SrcList *pSrc = pS->pSrc;
 102973     mask |= exprListTableUsage(pMaskSet, pS->pEList);
 102974     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
 102975     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
 102976     mask |= exprTableUsage(pMaskSet, pS->pWhere);
 102977     mask |= exprTableUsage(pMaskSet, pS->pHaving);
 102978     if( ALWAYS(pSrc!=0) ){
 102979       int i;
 102980       for(i=0; i<pSrc->nSrc; i++){
 102981         mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
 102982         mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
 102985     pS = pS->pPrior;
 102987   return mask;
 102991 ** Return TRUE if the given operator is one of the operators that is
 102992 ** allowed for an indexable WHERE clause term.  The allowed operators are
 102993 ** "=", "<", ">", "<=", ">=", and "IN".
 102995 ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
 102996 ** of one of the following forms: column = expression column > expression
 102997 ** column >= expression column < expression column <= expression
 102998 ** expression = column expression > column expression >= column
 102999 ** expression < column expression <= column column IN
 103000 ** (expression-list) column IN (subquery) column IS NULL
 103002 static int allowedOp(int op){
 103003   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
 103004   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
 103005   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
 103006   assert( TK_GE==TK_EQ+4 );
 103007   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
 103011 ** Swap two objects of type TYPE.
 103013 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
 103016 ** Commute a comparison operator.  Expressions of the form "X op Y"
 103017 ** are converted into "Y op X".
 103019 ** If left/right precendence rules come into play when determining the
 103020 ** collating
 103021 ** side of the comparison, it remains associated with the same side after
 103022 ** the commutation. So "Y collate NOCASE op X" becomes 
 103023 ** "X op Y". This is because any collation sequence on
 103024 ** the left hand side of a comparison overrides any collation sequence 
 103025 ** attached to the right. For the same reason the EP_Collate flag
 103026 ** is not commuted.
 103028 static void exprCommute(Parse *pParse, Expr *pExpr){
 103029   u16 expRight = (pExpr->pRight->flags & EP_Collate);
 103030   u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
 103031   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
 103032   if( expRight==expLeft ){
 103033     /* Either X and Y both have COLLATE operator or neither do */
 103034     if( expRight ){
 103035       /* Both X and Y have COLLATE operators.  Make sure X is always
 103036       ** used by clearing the EP_Collate flag from Y. */
 103037       pExpr->pRight->flags &= ~EP_Collate;
 103038     }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
 103039       /* Neither X nor Y have COLLATE operators, but X has a non-default
 103040       ** collating sequence.  So add the EP_Collate marker on X to cause
 103041       ** it to be searched first. */
 103042       pExpr->pLeft->flags |= EP_Collate;
 103045   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
 103046   if( pExpr->op>=TK_GT ){
 103047     assert( TK_LT==TK_GT+2 );
 103048     assert( TK_GE==TK_LE+2 );
 103049     assert( TK_GT>TK_EQ );
 103050     assert( TK_GT<TK_LE );
 103051     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
 103052     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
 103057 ** Translate from TK_xx operator to WO_xx bitmask.
 103059 static u16 operatorMask(int op){
 103060   u16 c;
 103061   assert( allowedOp(op) );
 103062   if( op==TK_IN ){
 103063     c = WO_IN;
 103064   }else if( op==TK_ISNULL ){
 103065     c = WO_ISNULL;
 103066   }else{
 103067     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
 103068     c = (u16)(WO_EQ<<(op-TK_EQ));
 103070   assert( op!=TK_ISNULL || c==WO_ISNULL );
 103071   assert( op!=TK_IN || c==WO_IN );
 103072   assert( op!=TK_EQ || c==WO_EQ );
 103073   assert( op!=TK_LT || c==WO_LT );
 103074   assert( op!=TK_LE || c==WO_LE );
 103075   assert( op!=TK_GT || c==WO_GT );
 103076   assert( op!=TK_GE || c==WO_GE );
 103077   return c;
 103081 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
 103082 ** where X is a reference to the iColumn of table iCur and <op> is one of
 103083 ** the WO_xx operator codes specified by the op parameter.
 103084 ** Return a pointer to the term.  Return 0 if not found.
 103086 static WhereTerm *findTerm(
 103087   WhereClause *pWC,     /* The WHERE clause to be searched */
 103088   int iCur,             /* Cursor number of LHS */
 103089   int iColumn,          /* Column number of LHS */
 103090   Bitmask notReady,     /* RHS must not overlap with this mask */
 103091   u32 op,               /* Mask of WO_xx values describing operator */
 103092   Index *pIdx           /* Must be compatible with this index, if not NULL */
 103094   WhereTerm *pTerm;
 103095   int k;
 103096   assert( iCur>=0 );
 103097   op &= WO_ALL;
 103098   for(; pWC; pWC=pWC->pOuter){
 103099     for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
 103100       if( pTerm->leftCursor==iCur
 103101          && (pTerm->prereqRight & notReady)==0
 103102          && pTerm->u.leftColumn==iColumn
 103103          && (pTerm->eOperator & op)!=0
 103105         if( iColumn>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
 103106           Expr *pX = pTerm->pExpr;
 103107           CollSeq *pColl;
 103108           char idxaff;
 103109           int j;
 103110           Parse *pParse = pWC->pParse;
 103112           idxaff = pIdx->pTable->aCol[iColumn].affinity;
 103113           if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
 103115           /* Figure out the collation sequence required from an index for
 103116           ** it to be useful for optimising expression pX. Store this
 103117           ** value in variable pColl.
 103119           assert(pX->pLeft);
 103120           pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
 103121           if( pColl==0 ) pColl = pParse->db->pDfltColl;
 103123           for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
 103124             if( NEVER(j>=pIdx->nColumn) ) return 0;
 103126           if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
 103128         return pTerm;
 103132   return 0;
 103135 /* Forward reference */
 103136 static void exprAnalyze(SrcList*, WhereClause*, int);
 103139 ** Call exprAnalyze on all terms in a WHERE clause.  
 103143 static void exprAnalyzeAll(
 103144   SrcList *pTabList,       /* the FROM clause */
 103145   WhereClause *pWC         /* the WHERE clause to be analyzed */
 103147   int i;
 103148   for(i=pWC->nTerm-1; i>=0; i--){
 103149     exprAnalyze(pTabList, pWC, i);
 103153 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
 103155 ** Check to see if the given expression is a LIKE or GLOB operator that
 103156 ** can be optimized using inequality constraints.  Return TRUE if it is
 103157 ** so and false if not.
 103159 ** In order for the operator to be optimizible, the RHS must be a string
 103160 ** literal that does not begin with a wildcard.  
 103162 static int isLikeOrGlob(
 103163   Parse *pParse,    /* Parsing and code generating context */
 103164   Expr *pExpr,      /* Test this expression */
 103165   Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
 103166   int *pisComplete, /* True if the only wildcard is % in the last character */
 103167   int *pnoCase      /* True if uppercase is equivalent to lowercase */
 103169   const char *z = 0;         /* String on RHS of LIKE operator */
 103170   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
 103171   ExprList *pList;           /* List of operands to the LIKE operator */
 103172   int c;                     /* One character in z[] */
 103173   int cnt;                   /* Number of non-wildcard prefix characters */
 103174   char wc[3];                /* Wildcard characters */
 103175   sqlite3 *db = pParse->db;  /* Database connection */
 103176   sqlite3_value *pVal = 0;
 103177   int op;                    /* Opcode of pRight */
 103179   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
 103180     return 0;
 103182 #ifdef SQLITE_EBCDIC
 103183   if( *pnoCase ) return 0;
 103184 #endif
 103185   pList = pExpr->x.pList;
 103186   pLeft = pList->a[1].pExpr;
 103187   if( pLeft->op!=TK_COLUMN 
 103188    || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
 103189    || IsVirtual(pLeft->pTab)
 103191     /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
 103192     ** be the name of an indexed column with TEXT affinity. */
 103193     return 0;
 103195   assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
 103197   pRight = pList->a[0].pExpr;
 103198   op = pRight->op;
 103199   if( op==TK_REGISTER ){
 103200     op = pRight->op2;
 103202   if( op==TK_VARIABLE ){
 103203     Vdbe *pReprepare = pParse->pReprepare;
 103204     int iCol = pRight->iColumn;
 103205     pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
 103206     if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
 103207       z = (char *)sqlite3_value_text(pVal);
 103209     sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
 103210     assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
 103211   }else if( op==TK_STRING ){
 103212     z = pRight->u.zToken;
 103214   if( z ){
 103215     cnt = 0;
 103216     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
 103217       cnt++;
 103219     if( cnt!=0 && 255!=(u8)z[cnt-1] ){
 103220       Expr *pPrefix;
 103221       *pisComplete = c==wc[0] && z[cnt+1]==0;
 103222       pPrefix = sqlite3Expr(db, TK_STRING, z);
 103223       if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
 103224       *ppPrefix = pPrefix;
 103225       if( op==TK_VARIABLE ){
 103226         Vdbe *v = pParse->pVdbe;
 103227         sqlite3VdbeSetVarmask(v, pRight->iColumn);
 103228         if( *pisComplete && pRight->u.zToken[1] ){
 103229           /* If the rhs of the LIKE expression is a variable, and the current
 103230           ** value of the variable means there is no need to invoke the LIKE
 103231           ** function, then no OP_Variable will be added to the program.
 103232           ** This causes problems for the sqlite3_bind_parameter_name()
 103233           ** API. To workaround them, add a dummy OP_Variable here.
 103235           int r1 = sqlite3GetTempReg(pParse);
 103236           sqlite3ExprCodeTarget(pParse, pRight, r1);
 103237           sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
 103238           sqlite3ReleaseTempReg(pParse, r1);
 103241     }else{
 103242       z = 0;
 103246   sqlite3ValueFree(pVal);
 103247   return (z!=0);
 103249 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
 103252 #ifndef SQLITE_OMIT_VIRTUALTABLE
 103254 ** Check to see if the given expression is of the form
 103256 **         column MATCH expr
 103258 ** If it is then return TRUE.  If not, return FALSE.
 103260 static int isMatchOfColumn(
 103261   Expr *pExpr      /* Test this expression */
 103263   ExprList *pList;
 103265   if( pExpr->op!=TK_FUNCTION ){
 103266     return 0;
 103268   if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
 103269     return 0;
 103271   pList = pExpr->x.pList;
 103272   if( pList->nExpr!=2 ){
 103273     return 0;
 103275   if( pList->a[1].pExpr->op != TK_COLUMN ){
 103276     return 0;
 103278   return 1;
 103280 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 103283 ** If the pBase expression originated in the ON or USING clause of
 103284 ** a join, then transfer the appropriate markings over to derived.
 103286 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
 103287   pDerived->flags |= pBase->flags & EP_FromJoin;
 103288   pDerived->iRightJoinTable = pBase->iRightJoinTable;
 103291 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
 103293 ** Analyze a term that consists of two or more OR-connected
 103294 ** subterms.  So in:
 103296 **     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
 103297 **                          ^^^^^^^^^^^^^^^^^^^^
 103299 ** This routine analyzes terms such as the middle term in the above example.
 103300 ** A WhereOrTerm object is computed and attached to the term under
 103301 ** analysis, regardless of the outcome of the analysis.  Hence:
 103303 **     WhereTerm.wtFlags   |=  TERM_ORINFO
 103304 **     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
 103306 ** The term being analyzed must have two or more of OR-connected subterms.
 103307 ** A single subterm might be a set of AND-connected sub-subterms.
 103308 ** Examples of terms under analysis:
 103310 **     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
 103311 **     (B)     x=expr1 OR expr2=x OR x=expr3
 103312 **     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
 103313 **     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
 103314 **     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
 103316 ** CASE 1:
 103318 ** If all subterms are of the form T.C=expr for some single column of C
 103319 ** a single table T (as shown in example B above) then create a new virtual
 103320 ** term that is an equivalent IN expression.  In other words, if the term
 103321 ** being analyzed is:
 103323 **      x = expr1  OR  expr2 = x  OR  x = expr3
 103325 ** then create a new virtual term like this:
 103327 **      x IN (expr1,expr2,expr3)
 103329 ** CASE 2:
 103331 ** If all subterms are indexable by a single table T, then set
 103333 **     WhereTerm.eOperator              =  WO_OR
 103334 **     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
 103336 ** A subterm is "indexable" if it is of the form
 103337 ** "T.C <op> <expr>" where C is any column of table T and 
 103338 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
 103339 ** A subterm is also indexable if it is an AND of two or more
 103340 ** subsubterms at least one of which is indexable.  Indexable AND 
 103341 ** subterms have their eOperator set to WO_AND and they have
 103342 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
 103344 ** From another point of view, "indexable" means that the subterm could
 103345 ** potentially be used with an index if an appropriate index exists.
 103346 ** This analysis does not consider whether or not the index exists; that
 103347 ** is something the bestIndex() routine will determine.  This analysis
 103348 ** only looks at whether subterms appropriate for indexing exist.
 103350 ** All examples A through E above all satisfy case 2.  But if a term
 103351 ** also statisfies case 1 (such as B) we know that the optimizer will
 103352 ** always prefer case 1, so in that case we pretend that case 2 is not
 103353 ** satisfied.
 103355 ** It might be the case that multiple tables are indexable.  For example,
 103356 ** (E) above is indexable on tables P, Q, and R.
 103358 ** Terms that satisfy case 2 are candidates for lookup by using
 103359 ** separate indices to find rowids for each subterm and composing
 103360 ** the union of all rowids using a RowSet object.  This is similar
 103361 ** to "bitmap indices" in other database engines.
 103363 ** OTHERWISE:
 103365 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
 103366 ** zero.  This term is not useful for search.
 103368 static void exprAnalyzeOrTerm(
 103369   SrcList *pSrc,            /* the FROM clause */
 103370   WhereClause *pWC,         /* the complete WHERE clause */
 103371   int idxTerm               /* Index of the OR-term to be analyzed */
 103373   Parse *pParse = pWC->pParse;            /* Parser context */
 103374   sqlite3 *db = pParse->db;               /* Database connection */
 103375   WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
 103376   Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
 103377   WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
 103378   int i;                                  /* Loop counters */
 103379   WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
 103380   WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
 103381   WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
 103382   Bitmask chngToIN;         /* Tables that might satisfy case 1 */
 103383   Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
 103386   ** Break the OR clause into its separate subterms.  The subterms are
 103387   ** stored in a WhereClause structure containing within the WhereOrInfo
 103388   ** object that is attached to the original OR clause term.
 103390   assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
 103391   assert( pExpr->op==TK_OR );
 103392   pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
 103393   if( pOrInfo==0 ) return;
 103394   pTerm->wtFlags |= TERM_ORINFO;
 103395   pOrWc = &pOrInfo->wc;
 103396   whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
 103397   whereSplit(pOrWc, pExpr, TK_OR);
 103398   exprAnalyzeAll(pSrc, pOrWc);
 103399   if( db->mallocFailed ) return;
 103400   assert( pOrWc->nTerm>=2 );
 103403   ** Compute the set of tables that might satisfy cases 1 or 2.
 103405   indexable = ~(Bitmask)0;
 103406   chngToIN = ~(pWC->vmask);
 103407   for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
 103408     if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
 103409       WhereAndInfo *pAndInfo;
 103410       assert( pOrTerm->eOperator==0 );
 103411       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
 103412       chngToIN = 0;
 103413       pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
 103414       if( pAndInfo ){
 103415         WhereClause *pAndWC;
 103416         WhereTerm *pAndTerm;
 103417         int j;
 103418         Bitmask b = 0;
 103419         pOrTerm->u.pAndInfo = pAndInfo;
 103420         pOrTerm->wtFlags |= TERM_ANDINFO;
 103421         pOrTerm->eOperator = WO_AND;
 103422         pAndWC = &pAndInfo->wc;
 103423         whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
 103424         whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
 103425         exprAnalyzeAll(pSrc, pAndWC);
 103426         pAndWC->pOuter = pWC;
 103427         testcase( db->mallocFailed );
 103428         if( !db->mallocFailed ){
 103429           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
 103430             assert( pAndTerm->pExpr );
 103431             if( allowedOp(pAndTerm->pExpr->op) ){
 103432               b |= getMask(pMaskSet, pAndTerm->leftCursor);
 103436         indexable &= b;
 103438     }else if( pOrTerm->wtFlags & TERM_COPIED ){
 103439       /* Skip this term for now.  We revisit it when we process the
 103440       ** corresponding TERM_VIRTUAL term */
 103441     }else{
 103442       Bitmask b;
 103443       b = getMask(pMaskSet, pOrTerm->leftCursor);
 103444       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
 103445         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
 103446         b |= getMask(pMaskSet, pOther->leftCursor);
 103448       indexable &= b;
 103449       if( pOrTerm->eOperator!=WO_EQ ){
 103450         chngToIN = 0;
 103451       }else{
 103452         chngToIN &= b;
 103458   ** Record the set of tables that satisfy case 2.  The set might be
 103459   ** empty.
 103461   pOrInfo->indexable = indexable;
 103462   pTerm->eOperator = indexable==0 ? 0 : WO_OR;
 103465   ** chngToIN holds a set of tables that *might* satisfy case 1.  But
 103466   ** we have to do some additional checking to see if case 1 really
 103467   ** is satisfied.
 103469   ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
 103470   ** that there is no possibility of transforming the OR clause into an
 103471   ** IN operator because one or more terms in the OR clause contain
 103472   ** something other than == on a column in the single table.  The 1-bit
 103473   ** case means that every term of the OR clause is of the form
 103474   ** "table.column=expr" for some single table.  The one bit that is set
 103475   ** will correspond to the common table.  We still need to check to make
 103476   ** sure the same column is used on all terms.  The 2-bit case is when
 103477   ** the all terms are of the form "table1.column=table2.column".  It
 103478   ** might be possible to form an IN operator with either table1.column
 103479   ** or table2.column as the LHS if either is common to every term of
 103480   ** the OR clause.
 103482   ** Note that terms of the form "table.column1=table.column2" (the
 103483   ** same table on both sizes of the ==) cannot be optimized.
 103485   if( chngToIN ){
 103486     int okToChngToIN = 0;     /* True if the conversion to IN is valid */
 103487     int iColumn = -1;         /* Column index on lhs of IN operator */
 103488     int iCursor = -1;         /* Table cursor common to all terms */
 103489     int j = 0;                /* Loop counter */
 103491     /* Search for a table and column that appears on one side or the
 103492     ** other of the == operator in every subterm.  That table and column
 103493     ** will be recorded in iCursor and iColumn.  There might not be any
 103494     ** such table and column.  Set okToChngToIN if an appropriate table
 103495     ** and column is found but leave okToChngToIN false if not found.
 103497     for(j=0; j<2 && !okToChngToIN; j++){
 103498       pOrTerm = pOrWc->a;
 103499       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
 103500         assert( pOrTerm->eOperator==WO_EQ );
 103501         pOrTerm->wtFlags &= ~TERM_OR_OK;
 103502         if( pOrTerm->leftCursor==iCursor ){
 103503           /* This is the 2-bit case and we are on the second iteration and
 103504           ** current term is from the first iteration.  So skip this term. */
 103505           assert( j==1 );
 103506           continue;
 103508         if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
 103509           /* This term must be of the form t1.a==t2.b where t2 is in the
 103510           ** chngToIN set but t1 is not.  This term will be either preceeded
 103511           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
 103512           ** and use its inversion. */
 103513           testcase( pOrTerm->wtFlags & TERM_COPIED );
 103514           testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
 103515           assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
 103516           continue;
 103518         iColumn = pOrTerm->u.leftColumn;
 103519         iCursor = pOrTerm->leftCursor;
 103520         break;
 103522       if( i<0 ){
 103523         /* No candidate table+column was found.  This can only occur
 103524         ** on the second iteration */
 103525         assert( j==1 );
 103526         assert( (chngToIN&(chngToIN-1))==0 );
 103527         assert( chngToIN==getMask(pMaskSet, iCursor) );
 103528         break;
 103530       testcase( j==1 );
 103532       /* We have found a candidate table and column.  Check to see if that
 103533       ** table and column is common to every term in the OR clause */
 103534       okToChngToIN = 1;
 103535       for(; i>=0 && okToChngToIN; i--, pOrTerm++){
 103536         assert( pOrTerm->eOperator==WO_EQ );
 103537         if( pOrTerm->leftCursor!=iCursor ){
 103538           pOrTerm->wtFlags &= ~TERM_OR_OK;
 103539         }else if( pOrTerm->u.leftColumn!=iColumn ){
 103540           okToChngToIN = 0;
 103541         }else{
 103542           int affLeft, affRight;
 103543           /* If the right-hand side is also a column, then the affinities
 103544           ** of both right and left sides must be such that no type
 103545           ** conversions are required on the right.  (Ticket #2249)
 103547           affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
 103548           affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
 103549           if( affRight!=0 && affRight!=affLeft ){
 103550             okToChngToIN = 0;
 103551           }else{
 103552             pOrTerm->wtFlags |= TERM_OR_OK;
 103558     /* At this point, okToChngToIN is true if original pTerm satisfies
 103559     ** case 1.  In that case, construct a new virtual term that is 
 103560     ** pTerm converted into an IN operator.
 103562     ** EV: R-00211-15100
 103564     if( okToChngToIN ){
 103565       Expr *pDup;            /* A transient duplicate expression */
 103566       ExprList *pList = 0;   /* The RHS of the IN operator */
 103567       Expr *pLeft = 0;       /* The LHS of the IN operator */
 103568       Expr *pNew;            /* The complete IN operator */
 103570       for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
 103571         if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
 103572         assert( pOrTerm->eOperator==WO_EQ );
 103573         assert( pOrTerm->leftCursor==iCursor );
 103574         assert( pOrTerm->u.leftColumn==iColumn );
 103575         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
 103576         pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
 103577         pLeft = pOrTerm->pExpr->pLeft;
 103579       assert( pLeft!=0 );
 103580       pDup = sqlite3ExprDup(db, pLeft, 0);
 103581       pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
 103582       if( pNew ){
 103583         int idxNew;
 103584         transferJoinMarkings(pNew, pExpr);
 103585         assert( !ExprHasProperty(pNew, EP_xIsSelect) );
 103586         pNew->x.pList = pList;
 103587         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
 103588         testcase( idxNew==0 );
 103589         exprAnalyze(pSrc, pWC, idxNew);
 103590         pTerm = &pWC->a[idxTerm];
 103591         pWC->a[idxNew].iParent = idxTerm;
 103592         pTerm->nChild = 1;
 103593       }else{
 103594         sqlite3ExprListDelete(db, pList);
 103596       pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
 103600 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
 103604 ** The input to this routine is an WhereTerm structure with only the
 103605 ** "pExpr" field filled in.  The job of this routine is to analyze the
 103606 ** subexpression and populate all the other fields of the WhereTerm
 103607 ** structure.
 103609 ** If the expression is of the form "<expr> <op> X" it gets commuted
 103610 ** to the standard form of "X <op> <expr>".
 103612 ** If the expression is of the form "X <op> Y" where both X and Y are
 103613 ** columns, then the original expression is unchanged and a new virtual
 103614 ** term of the form "Y <op> X" is added to the WHERE clause and
 103615 ** analyzed separately.  The original term is marked with TERM_COPIED
 103616 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
 103617 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
 103618 ** is a commuted copy of a prior term.)  The original term has nChild=1
 103619 ** and the copy has idxParent set to the index of the original term.
 103621 static void exprAnalyze(
 103622   SrcList *pSrc,            /* the FROM clause */
 103623   WhereClause *pWC,         /* the WHERE clause */
 103624   int idxTerm               /* Index of the term to be analyzed */
 103626   WhereTerm *pTerm;                /* The term to be analyzed */
 103627   WhereMaskSet *pMaskSet;          /* Set of table index masks */
 103628   Expr *pExpr;                     /* The expression to be analyzed */
 103629   Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
 103630   Bitmask prereqAll;               /* Prerequesites of pExpr */
 103631   Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
 103632   Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
 103633   int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
 103634   int noCase = 0;                  /* LIKE/GLOB distinguishes case */
 103635   int op;                          /* Top-level operator.  pExpr->op */
 103636   Parse *pParse = pWC->pParse;     /* Parsing context */
 103637   sqlite3 *db = pParse->db;        /* Database connection */
 103639   if( db->mallocFailed ){
 103640     return;
 103642   pTerm = &pWC->a[idxTerm];
 103643   pMaskSet = pWC->pMaskSet;
 103644   pExpr = sqlite3ExprSkipCollate(pTerm->pExpr);
 103645   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
 103646   op = pExpr->op;
 103647   if( op==TK_IN ){
 103648     assert( pExpr->pRight==0 );
 103649     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 103650       pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
 103651     }else{
 103652       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
 103654   }else if( op==TK_ISNULL ){
 103655     pTerm->prereqRight = 0;
 103656   }else{
 103657     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
 103659   prereqAll = exprTableUsage(pMaskSet, pExpr);
 103660   if( ExprHasProperty(pExpr, EP_FromJoin) ){
 103661     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
 103662     prereqAll |= x;
 103663     extraRight = x-1;  /* ON clause terms may not be used with an index
 103664                        ** on left table of a LEFT JOIN.  Ticket #3015 */
 103666   pTerm->prereqAll = prereqAll;
 103667   pTerm->leftCursor = -1;
 103668   pTerm->iParent = -1;
 103669   pTerm->eOperator = 0;
 103670   if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
 103671     Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
 103672     Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
 103673     if( pLeft->op==TK_COLUMN ){
 103674       pTerm->leftCursor = pLeft->iTable;
 103675       pTerm->u.leftColumn = pLeft->iColumn;
 103676       pTerm->eOperator = operatorMask(op);
 103678     if( pRight && pRight->op==TK_COLUMN ){
 103679       WhereTerm *pNew;
 103680       Expr *pDup;
 103681       if( pTerm->leftCursor>=0 ){
 103682         int idxNew;
 103683         pDup = sqlite3ExprDup(db, pExpr, 0);
 103684         if( db->mallocFailed ){
 103685           sqlite3ExprDelete(db, pDup);
 103686           return;
 103688         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
 103689         if( idxNew==0 ) return;
 103690         pNew = &pWC->a[idxNew];
 103691         pNew->iParent = idxTerm;
 103692         pTerm = &pWC->a[idxTerm];
 103693         pTerm->nChild = 1;
 103694         pTerm->wtFlags |= TERM_COPIED;
 103695       }else{
 103696         pDup = pExpr;
 103697         pNew = pTerm;
 103699       exprCommute(pParse, pDup);
 103700       pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
 103701       pNew->leftCursor = pLeft->iTable;
 103702       pNew->u.leftColumn = pLeft->iColumn;
 103703       testcase( (prereqLeft | extraRight) != prereqLeft );
 103704       pNew->prereqRight = prereqLeft | extraRight;
 103705       pNew->prereqAll = prereqAll;
 103706       pNew->eOperator = operatorMask(pDup->op);
 103710 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
 103711   /* If a term is the BETWEEN operator, create two new virtual terms
 103712   ** that define the range that the BETWEEN implements.  For example:
 103714   **      a BETWEEN b AND c
 103716   ** is converted into:
 103718   **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
 103720   ** The two new terms are added onto the end of the WhereClause object.
 103721   ** The new terms are "dynamic" and are children of the original BETWEEN
 103722   ** term.  That means that if the BETWEEN term is coded, the children are
 103723   ** skipped.  Or, if the children are satisfied by an index, the original
 103724   ** BETWEEN term is skipped.
 103726   else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
 103727     ExprList *pList = pExpr->x.pList;
 103728     int i;
 103729     static const u8 ops[] = {TK_GE, TK_LE};
 103730     assert( pList!=0 );
 103731     assert( pList->nExpr==2 );
 103732     for(i=0; i<2; i++){
 103733       Expr *pNewExpr;
 103734       int idxNew;
 103735       pNewExpr = sqlite3PExpr(pParse, ops[i], 
 103736                              sqlite3ExprDup(db, pExpr->pLeft, 0),
 103737                              sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
 103738       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
 103739       testcase( idxNew==0 );
 103740       exprAnalyze(pSrc, pWC, idxNew);
 103741       pTerm = &pWC->a[idxTerm];
 103742       pWC->a[idxNew].iParent = idxTerm;
 103744     pTerm->nChild = 2;
 103746 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
 103748 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
 103749   /* Analyze a term that is composed of two or more subterms connected by
 103750   ** an OR operator.
 103752   else if( pExpr->op==TK_OR ){
 103753     assert( pWC->op==TK_AND );
 103754     exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
 103755     pTerm = &pWC->a[idxTerm];
 103757 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
 103759 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
 103760   /* Add constraints to reduce the search space on a LIKE or GLOB
 103761   ** operator.
 103763   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
 103765   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
 103767   ** The last character of the prefix "abc" is incremented to form the
 103768   ** termination condition "abd".
 103770   if( pWC->op==TK_AND 
 103771    && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
 103773     Expr *pLeft;       /* LHS of LIKE/GLOB operator */
 103774     Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
 103775     Expr *pNewExpr1;
 103776     Expr *pNewExpr2;
 103777     int idxNew1;
 103778     int idxNew2;
 103779     Token sCollSeqName;  /* Name of collating sequence */
 103781     pLeft = pExpr->x.pList->a[1].pExpr;
 103782     pStr2 = sqlite3ExprDup(db, pStr1, 0);
 103783     if( !db->mallocFailed ){
 103784       u8 c, *pC;       /* Last character before the first wildcard */
 103785       pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
 103786       c = *pC;
 103787       if( noCase ){
 103788         /* The point is to increment the last character before the first
 103789         ** wildcard.  But if we increment '@', that will push it into the
 103790         ** alphabetic range where case conversions will mess up the 
 103791         ** inequality.  To avoid this, make sure to also run the full
 103792         ** LIKE on all candidate expressions by clearing the isComplete flag
 103794         if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
 103797         c = sqlite3UpperToLower[c];
 103799       *pC = c + 1;
 103801     sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
 103802     sCollSeqName.n = 6;
 103803     pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
 103804     pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
 103805            sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
 103806            pStr1, 0);
 103807     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
 103808     testcase( idxNew1==0 );
 103809     exprAnalyze(pSrc, pWC, idxNew1);
 103810     pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
 103811     pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
 103812            sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
 103813            pStr2, 0);
 103814     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
 103815     testcase( idxNew2==0 );
 103816     exprAnalyze(pSrc, pWC, idxNew2);
 103817     pTerm = &pWC->a[idxTerm];
 103818     if( isComplete ){
 103819       pWC->a[idxNew1].iParent = idxTerm;
 103820       pWC->a[idxNew2].iParent = idxTerm;
 103821       pTerm->nChild = 2;
 103824 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
 103826 #ifndef SQLITE_OMIT_VIRTUALTABLE
 103827   /* Add a WO_MATCH auxiliary term to the constraint set if the
 103828   ** current expression is of the form:  column MATCH expr.
 103829   ** This information is used by the xBestIndex methods of
 103830   ** virtual tables.  The native query optimizer does not attempt
 103831   ** to do anything with MATCH functions.
 103833   if( isMatchOfColumn(pExpr) ){
 103834     int idxNew;
 103835     Expr *pRight, *pLeft;
 103836     WhereTerm *pNewTerm;
 103837     Bitmask prereqColumn, prereqExpr;
 103839     pRight = pExpr->x.pList->a[0].pExpr;
 103840     pLeft = pExpr->x.pList->a[1].pExpr;
 103841     prereqExpr = exprTableUsage(pMaskSet, pRight);
 103842     prereqColumn = exprTableUsage(pMaskSet, pLeft);
 103843     if( (prereqExpr & prereqColumn)==0 ){
 103844       Expr *pNewExpr;
 103845       pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
 103846                               0, sqlite3ExprDup(db, pRight, 0), 0);
 103847       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
 103848       testcase( idxNew==0 );
 103849       pNewTerm = &pWC->a[idxNew];
 103850       pNewTerm->prereqRight = prereqExpr;
 103851       pNewTerm->leftCursor = pLeft->iTable;
 103852       pNewTerm->u.leftColumn = pLeft->iColumn;
 103853       pNewTerm->eOperator = WO_MATCH;
 103854       pNewTerm->iParent = idxTerm;
 103855       pTerm = &pWC->a[idxTerm];
 103856       pTerm->nChild = 1;
 103857       pTerm->wtFlags |= TERM_COPIED;
 103858       pNewTerm->prereqAll = pTerm->prereqAll;
 103861 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 103863 #ifdef SQLITE_ENABLE_STAT3
 103864   /* When sqlite_stat3 histogram data is available an operator of the
 103865   ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
 103866   ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
 103867   ** virtual term of that form.
 103869   ** Note that the virtual term must be tagged with TERM_VNULL.  This
 103870   ** TERM_VNULL tag will suppress the not-null check at the beginning
 103871   ** of the loop.  Without the TERM_VNULL flag, the not-null check at
 103872   ** the start of the loop will prevent any results from being returned.
 103874   if( pExpr->op==TK_NOTNULL
 103875    && pExpr->pLeft->op==TK_COLUMN
 103876    && pExpr->pLeft->iColumn>=0
 103878     Expr *pNewExpr;
 103879     Expr *pLeft = pExpr->pLeft;
 103880     int idxNew;
 103881     WhereTerm *pNewTerm;
 103883     pNewExpr = sqlite3PExpr(pParse, TK_GT,
 103884                             sqlite3ExprDup(db, pLeft, 0),
 103885                             sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
 103887     idxNew = whereClauseInsert(pWC, pNewExpr,
 103888                               TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
 103889     if( idxNew ){
 103890       pNewTerm = &pWC->a[idxNew];
 103891       pNewTerm->prereqRight = 0;
 103892       pNewTerm->leftCursor = pLeft->iTable;
 103893       pNewTerm->u.leftColumn = pLeft->iColumn;
 103894       pNewTerm->eOperator = WO_GT;
 103895       pNewTerm->iParent = idxTerm;
 103896       pTerm = &pWC->a[idxTerm];
 103897       pTerm->nChild = 1;
 103898       pTerm->wtFlags |= TERM_COPIED;
 103899       pNewTerm->prereqAll = pTerm->prereqAll;
 103902 #endif /* SQLITE_ENABLE_STAT */
 103904   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
 103905   ** an index for tables to the left of the join.
 103907   pTerm->prereqRight |= extraRight;
 103911 ** This function searches the expression list passed as the second argument
 103912 ** for an expression of type TK_COLUMN that refers to the same column and
 103913 ** uses the same collation sequence as the iCol'th column of index pIdx.
 103914 ** Argument iBase is the cursor number used for the table that pIdx refers
 103915 ** to.
 103917 ** If such an expression is found, its index in pList->a[] is returned. If
 103918 ** no expression is found, -1 is returned.
 103920 static int findIndexCol(
 103921   Parse *pParse,                  /* Parse context */
 103922   ExprList *pList,                /* Expression list to search */
 103923   int iBase,                      /* Cursor for table associated with pIdx */
 103924   Index *pIdx,                    /* Index to match column of */
 103925   int iCol                        /* Column of index to match */
 103927   int i;
 103928   const char *zColl = pIdx->azColl[iCol];
 103930   for(i=0; i<pList->nExpr; i++){
 103931     Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
 103932     if( p->op==TK_COLUMN
 103933      && p->iColumn==pIdx->aiColumn[iCol]
 103934      && p->iTable==iBase
 103936       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
 103937       if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
 103938         return i;
 103943   return -1;
 103947 ** This routine determines if pIdx can be used to assist in processing a
 103948 ** DISTINCT qualifier. In other words, it tests whether or not using this
 103949 ** index for the outer loop guarantees that rows with equal values for
 103950 ** all expressions in the pDistinct list are delivered grouped together.
 103952 ** For example, the query 
 103954 **   SELECT DISTINCT a, b, c FROM tbl WHERE a = ?
 103956 ** can benefit from any index on columns "b" and "c".
 103958 static int isDistinctIndex(
 103959   Parse *pParse,                  /* Parsing context */
 103960   WhereClause *pWC,               /* The WHERE clause */
 103961   Index *pIdx,                    /* The index being considered */
 103962   int base,                       /* Cursor number for the table pIdx is on */
 103963   ExprList *pDistinct,            /* The DISTINCT expressions */
 103964   int nEqCol                      /* Number of index columns with == */
 103966   Bitmask mask = 0;               /* Mask of unaccounted for pDistinct exprs */
 103967   int i;                          /* Iterator variable */
 103969   assert( pDistinct!=0 );
 103970   if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
 103971   testcase( pDistinct->nExpr==BMS-1 );
 103973   /* Loop through all the expressions in the distinct list. If any of them
 103974   ** are not simple column references, return early. Otherwise, test if the
 103975   ** WHERE clause contains a "col=X" clause. If it does, the expression
 103976   ** can be ignored. If it does not, and the column does not belong to the
 103977   ** same table as index pIdx, return early. Finally, if there is no
 103978   ** matching "col=X" expression and the column is on the same table as pIdx,
 103979   ** set the corresponding bit in variable mask.
 103981   for(i=0; i<pDistinct->nExpr; i++){
 103982     WhereTerm *pTerm;
 103983     Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
 103984     if( p->op!=TK_COLUMN ) return 0;
 103985     pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
 103986     if( pTerm ){
 103987       Expr *pX = pTerm->pExpr;
 103988       CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
 103989       CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
 103990       if( p1==p2 ) continue;
 103992     if( p->iTable!=base ) return 0;
 103993     mask |= (((Bitmask)1) << i);
 103996   for(i=nEqCol; mask && i<pIdx->nColumn; i++){
 103997     int iExpr = findIndexCol(pParse, pDistinct, base, pIdx, i);
 103998     if( iExpr<0 ) break;
 103999     mask &= ~(((Bitmask)1) << iExpr);
 104002   return (mask==0);
 104007 ** Return true if the DISTINCT expression-list passed as the third argument
 104008 ** is redundant. A DISTINCT list is redundant if the database contains a
 104009 ** UNIQUE index that guarantees that the result of the query will be distinct
 104010 ** anyway.
 104012 static int isDistinctRedundant(
 104013   Parse *pParse,
 104014   SrcList *pTabList,
 104015   WhereClause *pWC,
 104016   ExprList *pDistinct
 104018   Table *pTab;
 104019   Index *pIdx;
 104020   int i;                          
 104021   int iBase;
 104023   /* If there is more than one table or sub-select in the FROM clause of
 104024   ** this query, then it will not be possible to show that the DISTINCT 
 104025   ** clause is redundant. */
 104026   if( pTabList->nSrc!=1 ) return 0;
 104027   iBase = pTabList->a[0].iCursor;
 104028   pTab = pTabList->a[0].pTab;
 104030   /* If any of the expressions is an IPK column on table iBase, then return 
 104031   ** true. Note: The (p->iTable==iBase) part of this test may be false if the
 104032   ** current SELECT is a correlated sub-query.
 104034   for(i=0; i<pDistinct->nExpr; i++){
 104035     Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
 104036     if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
 104039   /* Loop through all indices on the table, checking each to see if it makes
 104040   ** the DISTINCT qualifier redundant. It does so if:
 104042   **   1. The index is itself UNIQUE, and
 104044   **   2. All of the columns in the index are either part of the pDistinct
 104045   **      list, or else the WHERE clause contains a term of the form "col=X",
 104046   **      where X is a constant value. The collation sequences of the
 104047   **      comparison and select-list expressions must match those of the index.
 104049   **   3. All of those index columns for which the WHERE clause does not
 104050   **      contain a "col=X" term are subject to a NOT NULL constraint.
 104052   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 104053     if( pIdx->onError==OE_None ) continue;
 104054     for(i=0; i<pIdx->nColumn; i++){
 104055       int iCol = pIdx->aiColumn[i];
 104056       if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
 104057         int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
 104058         if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){
 104059           break;
 104063     if( i==pIdx->nColumn ){
 104064       /* This index implies that the DISTINCT qualifier is redundant. */
 104065       return 1;
 104069   return 0;
 104073 ** Prepare a crude estimate of the logarithm of the input value.
 104074 ** The results need not be exact.  This is only used for estimating
 104075 ** the total cost of performing operations with O(logN) or O(NlogN)
 104076 ** complexity.  Because N is just a guess, it is no great tragedy if
 104077 ** logN is a little off.
 104079 static double estLog(double N){
 104080   double logN = 1;
 104081   double x = 10;
 104082   while( N>x ){
 104083     logN += 1;
 104084     x *= 10;
 104086   return logN;
 104090 ** Two routines for printing the content of an sqlite3_index_info
 104091 ** structure.  Used for testing and debugging only.  If neither
 104092 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
 104093 ** are no-ops.
 104095 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
 104096 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
 104097   int i;
 104098   if( !sqlite3WhereTrace ) return;
 104099   for(i=0; i<p->nConstraint; i++){
 104100     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
 104102        p->aConstraint[i].iColumn,
 104103        p->aConstraint[i].iTermOffset,
 104104        p->aConstraint[i].op,
 104105        p->aConstraint[i].usable);
 104107   for(i=0; i<p->nOrderBy; i++){
 104108     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
 104110        p->aOrderBy[i].iColumn,
 104111        p->aOrderBy[i].desc);
 104114 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
 104115   int i;
 104116   if( !sqlite3WhereTrace ) return;
 104117   for(i=0; i<p->nConstraint; i++){
 104118     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
 104120        p->aConstraintUsage[i].argvIndex,
 104121        p->aConstraintUsage[i].omit);
 104123   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
 104124   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
 104125   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
 104126   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
 104128 #else
 104129 #define TRACE_IDX_INPUTS(A)
 104130 #define TRACE_IDX_OUTPUTS(A)
 104131 #endif
 104134 ** Required because bestIndex() is called by bestOrClauseIndex() 
 104136 static void bestIndex(WhereBestIdx*);
 104139 ** This routine attempts to find an scanning strategy that can be used 
 104140 ** to optimize an 'OR' expression that is part of a WHERE clause. 
 104142 ** The table associated with FROM clause term pSrc may be either a
 104143 ** regular B-Tree table or a virtual table.
 104145 static void bestOrClauseIndex(WhereBestIdx *p){
 104146 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
 104147   WhereClause *pWC = p->pWC;           /* The WHERE clause */
 104148   struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 104149   const int iCur = pSrc->iCursor;      /* The cursor of the table  */
 104150   const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
 104151   WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
 104152   WhereTerm *pTerm;                    /* A single term of the WHERE clause */
 104154   /* The OR-clause optimization is disallowed if the INDEXED BY or
 104155   ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
 104156   if( pSrc->notIndexed || pSrc->pIndex!=0 ){
 104157     return;
 104159   if( pWC->wctrlFlags & WHERE_AND_ONLY ){
 104160     return;
 104163   /* Search the WHERE clause terms for a usable WO_OR term. */
 104164   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 104165     if( pTerm->eOperator==WO_OR 
 104166      && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
 104167      && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
 104169       WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
 104170       WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
 104171       WhereTerm *pOrTerm;
 104172       int flags = WHERE_MULTI_OR;
 104173       double rTotal = 0;
 104174       double nRow = 0;
 104175       Bitmask used = 0;
 104176       WhereBestIdx sBOI;
 104178       sBOI = *p;
 104179       sBOI.pOrderBy = 0;
 104180       sBOI.pDistinct = 0;
 104181       sBOI.ppIdxInfo = 0;
 104182       for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
 104183         WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
 104184           (pOrTerm - pOrWC->a), (pTerm - pWC->a)
 104185         ));
 104186         if( pOrTerm->eOperator==WO_AND ){
 104187           sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
 104188           bestIndex(&sBOI);
 104189         }else if( pOrTerm->leftCursor==iCur ){
 104190           WhereClause tempWC;
 104191           tempWC.pParse = pWC->pParse;
 104192           tempWC.pMaskSet = pWC->pMaskSet;
 104193           tempWC.pOuter = pWC;
 104194           tempWC.op = TK_AND;
 104195           tempWC.a = pOrTerm;
 104196           tempWC.wctrlFlags = 0;
 104197           tempWC.nTerm = 1;
 104198           sBOI.pWC = &tempWC;
 104199           bestIndex(&sBOI);
 104200         }else{
 104201           continue;
 104203         rTotal += sBOI.cost.rCost;
 104204         nRow += sBOI.cost.plan.nRow;
 104205         used |= sBOI.cost.used;
 104206         if( rTotal>=p->cost.rCost ) break;
 104209       /* If there is an ORDER BY clause, increase the scan cost to account 
 104210       ** for the cost of the sort. */
 104211       if( p->pOrderBy!=0 ){
 104212         WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
 104213                     rTotal, rTotal+nRow*estLog(nRow)));
 104214         rTotal += nRow*estLog(nRow);
 104217       /* If the cost of scanning using this OR term for optimization is
 104218       ** less than the current cost stored in pCost, replace the contents
 104219       ** of pCost. */
 104220       WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
 104221       if( rTotal<p->cost.rCost ){
 104222         p->cost.rCost = rTotal;
 104223         p->cost.used = used;
 104224         p->cost.plan.nRow = nRow;
 104225         p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
 104226         p->cost.plan.wsFlags = flags;
 104227         p->cost.plan.u.pTerm = pTerm;
 104231 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
 104234 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 104236 ** Return TRUE if the WHERE clause term pTerm is of a form where it
 104237 ** could be used with an index to access pSrc, assuming an appropriate
 104238 ** index existed.
 104240 static int termCanDriveIndex(
 104241   WhereTerm *pTerm,              /* WHERE clause term to check */
 104242   struct SrcList_item *pSrc,     /* Table we are trying to access */
 104243   Bitmask notReady               /* Tables in outer loops of the join */
 104245   char aff;
 104246   if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
 104247   if( pTerm->eOperator!=WO_EQ ) return 0;
 104248   if( (pTerm->prereqRight & notReady)!=0 ) return 0;
 104249   aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
 104250   if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
 104251   return 1;
 104253 #endif
 104255 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 104257 ** If the query plan for pSrc specified in pCost is a full table scan
 104258 ** and indexing is allows (if there is no NOT INDEXED clause) and it
 104259 ** possible to construct a transient index that would perform better
 104260 ** than a full table scan even when the cost of constructing the index
 104261 ** is taken into account, then alter the query plan to use the
 104262 ** transient index.
 104264 static void bestAutomaticIndex(WhereBestIdx *p){
 104265   Parse *pParse = p->pParse;            /* The parsing context */
 104266   WhereClause *pWC = p->pWC;            /* The WHERE clause */
 104267   struct SrcList_item *pSrc = p->pSrc;  /* The FROM clause term to search */
 104268   double nTableRow;                     /* Rows in the input table */
 104269   double logN;                          /* log(nTableRow) */
 104270   double costTempIdx;         /* per-query cost of the transient index */
 104271   WhereTerm *pTerm;           /* A single term of the WHERE clause */
 104272   WhereTerm *pWCEnd;          /* End of pWC->a[] */
 104273   Table *pTable;              /* Table tht might be indexed */
 104275   if( pParse->nQueryLoop<=(double)1 ){
 104276     /* There is no point in building an automatic index for a single scan */
 104277     return;
 104279   if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
 104280     /* Automatic indices are disabled at run-time */
 104281     return;
 104283   if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0
 104284    && (p->cost.plan.wsFlags & WHERE_COVER_SCAN)==0
 104286     /* We already have some kind of index in use for this query. */
 104287     return;
 104289   if( pSrc->viaCoroutine ){
 104290     /* Cannot index a co-routine */
 104291     return;
 104293   if( pSrc->notIndexed ){
 104294     /* The NOT INDEXED clause appears in the SQL. */
 104295     return;
 104297   if( pSrc->isCorrelated ){
 104298     /* The source is a correlated sub-query. No point in indexing it. */
 104299     return;
 104302   assert( pParse->nQueryLoop >= (double)1 );
 104303   pTable = pSrc->pTab;
 104304   nTableRow = pTable->nRowEst;
 104305   logN = estLog(nTableRow);
 104306   costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
 104307   if( costTempIdx>=p->cost.rCost ){
 104308     /* The cost of creating the transient table would be greater than
 104309     ** doing the full table scan */
 104310     return;
 104313   /* Search for any equality comparison term */
 104314   pWCEnd = &pWC->a[pWC->nTerm];
 104315   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 104316     if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
 104317       WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
 104318                     p->cost.rCost, costTempIdx));
 104319       p->cost.rCost = costTempIdx;
 104320       p->cost.plan.nRow = logN + 1;
 104321       p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
 104322       p->cost.used = pTerm->prereqRight;
 104323       break;
 104327 #else
 104328 # define bestAutomaticIndex(A)  /* no-op */
 104329 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
 104332 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 104334 ** Generate code to construct the Index object for an automatic index
 104335 ** and to set up the WhereLevel object pLevel so that the code generator
 104336 ** makes use of the automatic index.
 104338 static void constructAutomaticIndex(
 104339   Parse *pParse,              /* The parsing context */
 104340   WhereClause *pWC,           /* The WHERE clause */
 104341   struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
 104342   Bitmask notReady,           /* Mask of cursors that are not available */
 104343   WhereLevel *pLevel          /* Write new index here */
 104345   int nColumn;                /* Number of columns in the constructed index */
 104346   WhereTerm *pTerm;           /* A single term of the WHERE clause */
 104347   WhereTerm *pWCEnd;          /* End of pWC->a[] */
 104348   int nByte;                  /* Byte of memory needed for pIdx */
 104349   Index *pIdx;                /* Object describing the transient index */
 104350   Vdbe *v;                    /* Prepared statement under construction */
 104351   int addrInit;               /* Address of the initialization bypass jump */
 104352   Table *pTable;              /* The table being indexed */
 104353   KeyInfo *pKeyinfo;          /* Key information for the index */   
 104354   int addrTop;                /* Top of the index fill loop */
 104355   int regRecord;              /* Register holding an index record */
 104356   int n;                      /* Column counter */
 104357   int i;                      /* Loop counter */
 104358   int mxBitCol;               /* Maximum column in pSrc->colUsed */
 104359   CollSeq *pColl;             /* Collating sequence to on a column */
 104360   Bitmask idxCols;            /* Bitmap of columns used for indexing */
 104361   Bitmask extraCols;          /* Bitmap of additional columns */
 104363   /* Generate code to skip over the creation and initialization of the
 104364   ** transient index on 2nd and subsequent iterations of the loop. */
 104365   v = pParse->pVdbe;
 104366   assert( v!=0 );
 104367   addrInit = sqlite3CodeOnce(pParse);
 104369   /* Count the number of columns that will be added to the index
 104370   ** and used to match WHERE clause constraints */
 104371   nColumn = 0;
 104372   pTable = pSrc->pTab;
 104373   pWCEnd = &pWC->a[pWC->nTerm];
 104374   idxCols = 0;
 104375   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 104376     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
 104377       int iCol = pTerm->u.leftColumn;
 104378       Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
 104379       testcase( iCol==BMS );
 104380       testcase( iCol==BMS-1 );
 104381       if( (idxCols & cMask)==0 ){
 104382         nColumn++;
 104383         idxCols |= cMask;
 104387   assert( nColumn>0 );
 104388   pLevel->plan.nEq = nColumn;
 104390   /* Count the number of additional columns needed to create a
 104391   ** covering index.  A "covering index" is an index that contains all
 104392   ** columns that are needed by the query.  With a covering index, the
 104393   ** original table never needs to be accessed.  Automatic indices must
 104394   ** be a covering index because the index will not be updated if the
 104395   ** original table changes and the index and table cannot both be used
 104396   ** if they go out of sync.
 104398   extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
 104399   mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
 104400   testcase( pTable->nCol==BMS-1 );
 104401   testcase( pTable->nCol==BMS-2 );
 104402   for(i=0; i<mxBitCol; i++){
 104403     if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
 104405   if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
 104406     nColumn += pTable->nCol - BMS + 1;
 104408   pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
 104410   /* Construct the Index object to describe this index */
 104411   nByte = sizeof(Index);
 104412   nByte += nColumn*sizeof(int);     /* Index.aiColumn */
 104413   nByte += nColumn*sizeof(char*);   /* Index.azColl */
 104414   nByte += nColumn;                 /* Index.aSortOrder */
 104415   pIdx = sqlite3DbMallocZero(pParse->db, nByte);
 104416   if( pIdx==0 ) return;
 104417   pLevel->plan.u.pIdx = pIdx;
 104418   pIdx->azColl = (char**)&pIdx[1];
 104419   pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
 104420   pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
 104421   pIdx->zName = "auto-index";
 104422   pIdx->nColumn = nColumn;
 104423   pIdx->pTable = pTable;
 104424   n = 0;
 104425   idxCols = 0;
 104426   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 104427     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
 104428       int iCol = pTerm->u.leftColumn;
 104429       Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
 104430       if( (idxCols & cMask)==0 ){
 104431         Expr *pX = pTerm->pExpr;
 104432         idxCols |= cMask;
 104433         pIdx->aiColumn[n] = pTerm->u.leftColumn;
 104434         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
 104435         pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
 104436         n++;
 104440   assert( (u32)n==pLevel->plan.nEq );
 104442   /* Add additional columns needed to make the automatic index into
 104443   ** a covering index */
 104444   for(i=0; i<mxBitCol; i++){
 104445     if( extraCols & (((Bitmask)1)<<i) ){
 104446       pIdx->aiColumn[n] = i;
 104447       pIdx->azColl[n] = "BINARY";
 104448       n++;
 104451   if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
 104452     for(i=BMS-1; i<pTable->nCol; i++){
 104453       pIdx->aiColumn[n] = i;
 104454       pIdx->azColl[n] = "BINARY";
 104455       n++;
 104458   assert( n==nColumn );
 104460   /* Create the automatic index */
 104461   pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
 104462   assert( pLevel->iIdxCur>=0 );
 104463   sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
 104464                     (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
 104465   VdbeComment((v, "for %s", pTable->zName));
 104467   /* Fill the automatic index with content */
 104468   addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
 104469   regRecord = sqlite3GetTempReg(pParse);
 104470   sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
 104471   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
 104472   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 104473   sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
 104474   sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
 104475   sqlite3VdbeJumpHere(v, addrTop);
 104476   sqlite3ReleaseTempReg(pParse, regRecord);
 104478   /* Jump here when skipping the initialization */
 104479   sqlite3VdbeJumpHere(v, addrInit);
 104481 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
 104483 #ifndef SQLITE_OMIT_VIRTUALTABLE
 104485 ** Allocate and populate an sqlite3_index_info structure. It is the 
 104486 ** responsibility of the caller to eventually release the structure
 104487 ** by passing the pointer returned by this function to sqlite3_free().
 104489 static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
 104490   Parse *pParse = p->pParse; 
 104491   WhereClause *pWC = p->pWC;
 104492   struct SrcList_item *pSrc = p->pSrc;
 104493   ExprList *pOrderBy = p->pOrderBy;
 104494   int i, j;
 104495   int nTerm;
 104496   struct sqlite3_index_constraint *pIdxCons;
 104497   struct sqlite3_index_orderby *pIdxOrderBy;
 104498   struct sqlite3_index_constraint_usage *pUsage;
 104499   WhereTerm *pTerm;
 104500   int nOrderBy;
 104501   sqlite3_index_info *pIdxInfo;
 104503   WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
 104505   /* Count the number of possible WHERE clause constraints referring
 104506   ** to this virtual table */
 104507   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
 104508     if( pTerm->leftCursor != pSrc->iCursor ) continue;
 104509     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
 104510     testcase( pTerm->eOperator==WO_IN );
 104511     testcase( pTerm->eOperator==WO_ISNULL );
 104512     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
 104513     if( pTerm->wtFlags & TERM_VNULL ) continue;
 104514     nTerm++;
 104517   /* If the ORDER BY clause contains only columns in the current 
 104518   ** virtual table then allocate space for the aOrderBy part of
 104519   ** the sqlite3_index_info structure.
 104521   nOrderBy = 0;
 104522   if( pOrderBy ){
 104523     int n = pOrderBy->nExpr;
 104524     for(i=0; i<n; i++){
 104525       Expr *pExpr = pOrderBy->a[i].pExpr;
 104526       if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
 104528     if( i==n){
 104529       nOrderBy = n;
 104533   /* Allocate the sqlite3_index_info structure
 104535   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
 104536                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
 104537                            + sizeof(*pIdxOrderBy)*nOrderBy );
 104538   if( pIdxInfo==0 ){
 104539     sqlite3ErrorMsg(pParse, "out of memory");
 104540     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 104541     return 0;
 104544   /* Initialize the structure.  The sqlite3_index_info structure contains
 104545   ** many fields that are declared "const" to prevent xBestIndex from
 104546   ** changing them.  We have to do some funky casting in order to
 104547   ** initialize those fields.
 104549   pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
 104550   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
 104551   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
 104552   *(int*)&pIdxInfo->nConstraint = nTerm;
 104553   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
 104554   *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
 104555   *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
 104556   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
 104557                                                                    pUsage;
 104559   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
 104560     if( pTerm->leftCursor != pSrc->iCursor ) continue;
 104561     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
 104562     testcase( pTerm->eOperator==WO_IN );
 104563     testcase( pTerm->eOperator==WO_ISNULL );
 104564     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
 104565     if( pTerm->wtFlags & TERM_VNULL ) continue;
 104566     pIdxCons[j].iColumn = pTerm->u.leftColumn;
 104567     pIdxCons[j].iTermOffset = i;
 104568     pIdxCons[j].op = (u8)pTerm->eOperator;
 104569     /* The direct assignment in the previous line is possible only because
 104570     ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
 104571     ** following asserts verify this fact. */
 104572     assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
 104573     assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
 104574     assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
 104575     assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
 104576     assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
 104577     assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
 104578     assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
 104579     j++;
 104581   for(i=0; i<nOrderBy; i++){
 104582     Expr *pExpr = pOrderBy->a[i].pExpr;
 104583     pIdxOrderBy[i].iColumn = pExpr->iColumn;
 104584     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
 104587   return pIdxInfo;
 104591 ** The table object reference passed as the second argument to this function
 104592 ** must represent a virtual table. This function invokes the xBestIndex()
 104593 ** method of the virtual table with the sqlite3_index_info pointer passed
 104594 ** as the argument.
 104596 ** If an error occurs, pParse is populated with an error message and a
 104597 ** non-zero value is returned. Otherwise, 0 is returned and the output
 104598 ** part of the sqlite3_index_info structure is left populated.
 104600 ** Whether or not an error is returned, it is the responsibility of the
 104601 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
 104602 ** that this is required.
 104604 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
 104605   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
 104606   int i;
 104607   int rc;
 104609   WHERETRACE(("xBestIndex for %s\n", pTab->zName));
 104610   TRACE_IDX_INPUTS(p);
 104611   rc = pVtab->pModule->xBestIndex(pVtab, p);
 104612   TRACE_IDX_OUTPUTS(p);
 104614   if( rc!=SQLITE_OK ){
 104615     if( rc==SQLITE_NOMEM ){
 104616       pParse->db->mallocFailed = 1;
 104617     }else if( !pVtab->zErrMsg ){
 104618       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
 104619     }else{
 104620       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
 104623   sqlite3_free(pVtab->zErrMsg);
 104624   pVtab->zErrMsg = 0;
 104626   for(i=0; i<p->nConstraint; i++){
 104627     if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
 104628       sqlite3ErrorMsg(pParse, 
 104629           "table %s: xBestIndex returned an invalid plan", pTab->zName);
 104633   return pParse->nErr;
 104638 ** Compute the best index for a virtual table.
 104640 ** The best index is computed by the xBestIndex method of the virtual
 104641 ** table module.  This routine is really just a wrapper that sets up
 104642 ** the sqlite3_index_info structure that is used to communicate with
 104643 ** xBestIndex.
 104645 ** In a join, this routine might be called multiple times for the
 104646 ** same virtual table.  The sqlite3_index_info structure is created
 104647 ** and initialized on the first invocation and reused on all subsequent
 104648 ** invocations.  The sqlite3_index_info structure is also used when
 104649 ** code is generated to access the virtual table.  The whereInfoDelete() 
 104650 ** routine takes care of freeing the sqlite3_index_info structure after
 104651 ** everybody has finished with it.
 104653 static void bestVirtualIndex(WhereBestIdx *p){
 104654   Parse *pParse = p->pParse;      /* The parsing context */
 104655   WhereClause *pWC = p->pWC;      /* The WHERE clause */
 104656   struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 104657   Table *pTab = pSrc->pTab;
 104658   sqlite3_index_info *pIdxInfo;
 104659   struct sqlite3_index_constraint *pIdxCons;
 104660   struct sqlite3_index_constraint_usage *pUsage;
 104661   WhereTerm *pTerm;
 104662   int i, j;
 104663   int nOrderBy;
 104664   double rCost;
 104666   /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
 104667   ** malloc in allocateIndexInfo() fails and this function returns leaving
 104668   ** wsFlags in an uninitialized state, the caller may behave unpredictably.
 104670   memset(&p->cost, 0, sizeof(p->cost));
 104671   p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
 104673   /* If the sqlite3_index_info structure has not been previously
 104674   ** allocated and initialized, then allocate and initialize it now.
 104676   pIdxInfo = *p->ppIdxInfo;
 104677   if( pIdxInfo==0 ){
 104678     *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
 104680   if( pIdxInfo==0 ){
 104681     return;
 104684   /* At this point, the sqlite3_index_info structure that pIdxInfo points
 104685   ** to will have been initialized, either during the current invocation or
 104686   ** during some prior invocation.  Now we just have to customize the
 104687   ** details of pIdxInfo for the current invocation and pass it to
 104688   ** xBestIndex.
 104691   /* The module name must be defined. Also, by this point there must
 104692   ** be a pointer to an sqlite3_vtab structure. Otherwise
 104693   ** sqlite3ViewGetColumnNames() would have picked up the error. 
 104695   assert( pTab->azModuleArg && pTab->azModuleArg[0] );
 104696   assert( sqlite3GetVTable(pParse->db, pTab) );
 104698   /* Set the aConstraint[].usable fields and initialize all 
 104699   ** output variables to zero.
 104701   ** aConstraint[].usable is true for constraints where the right-hand
 104702   ** side contains only references to tables to the left of the current
 104703   ** table.  In other words, if the constraint is of the form:
 104705   **           column = expr
 104707   ** and we are evaluating a join, then the constraint on column is 
 104708   ** only valid if all tables referenced in expr occur to the left
 104709   ** of the table containing column.
 104711   ** The aConstraints[] array contains entries for all constraints
 104712   ** on the current table.  That way we only have to compute it once
 104713   ** even though we might try to pick the best index multiple times.
 104714   ** For each attempt at picking an index, the order of tables in the
 104715   ** join might be different so we have to recompute the usable flag
 104716   ** each time.
 104718   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
 104719   pUsage = pIdxInfo->aConstraintUsage;
 104720   for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
 104721     j = pIdxCons->iTermOffset;
 104722     pTerm = &pWC->a[j];
 104723     pIdxCons->usable = (pTerm->prereqRight&p->notReady) ? 0 : 1;
 104725   memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
 104726   if( pIdxInfo->needToFreeIdxStr ){
 104727     sqlite3_free(pIdxInfo->idxStr);
 104729   pIdxInfo->idxStr = 0;
 104730   pIdxInfo->idxNum = 0;
 104731   pIdxInfo->needToFreeIdxStr = 0;
 104732   pIdxInfo->orderByConsumed = 0;
 104733   /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
 104734   pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
 104735   nOrderBy = pIdxInfo->nOrderBy;
 104736   if( !p->pOrderBy ){
 104737     pIdxInfo->nOrderBy = 0;
 104740   if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
 104741     return;
 104744   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
 104745   for(i=0; i<pIdxInfo->nConstraint; i++){
 104746     if( pUsage[i].argvIndex>0 ){
 104747       p->cost.used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
 104751   /* If there is an ORDER BY clause, and the selected virtual table index
 104752   ** does not satisfy it, increase the cost of the scan accordingly. This
 104753   ** matches the processing for non-virtual tables in bestBtreeIndex().
 104755   rCost = pIdxInfo->estimatedCost;
 104756   if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
 104757     rCost += estLog(rCost)*rCost;
 104760   /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
 104761   ** inital value of lowestCost in this loop. If it is, then the
 104762   ** (cost<lowestCost) test below will never be true.
 104764   ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
 104765   ** is defined.
 104767   if( (SQLITE_BIG_DBL/((double)2))<rCost ){
 104768     p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
 104769   }else{
 104770     p->cost.rCost = rCost;
 104772   p->cost.plan.u.pVtabIdx = pIdxInfo;
 104773   if( pIdxInfo->orderByConsumed ){
 104774     p->cost.plan.wsFlags |= WHERE_ORDERED;
 104775     p->cost.plan.nOBSat = nOrderBy;
 104776   }else{
 104777     p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
 104779   p->cost.plan.nEq = 0;
 104780   pIdxInfo->nOrderBy = nOrderBy;
 104782   /* Try to find a more efficient access pattern by using multiple indexes
 104783   ** to optimize an OR expression within the WHERE clause. 
 104785   bestOrClauseIndex(p);
 104787 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 104789 #ifdef SQLITE_ENABLE_STAT3
 104791 ** Estimate the location of a particular key among all keys in an
 104792 ** index.  Store the results in aStat as follows:
 104794 **    aStat[0]      Est. number of rows less than pVal
 104795 **    aStat[1]      Est. number of rows equal to pVal
 104797 ** Return SQLITE_OK on success.
 104799 static int whereKeyStats(
 104800   Parse *pParse,              /* Database connection */
 104801   Index *pIdx,                /* Index to consider domain of */
 104802   sqlite3_value *pVal,        /* Value to consider */
 104803   int roundUp,                /* Round up if true.  Round down if false */
 104804   tRowcnt *aStat              /* OUT: stats written here */
 104806   tRowcnt n;
 104807   IndexSample *aSample;
 104808   int i, eType;
 104809   int isEq = 0;
 104810   i64 v;
 104811   double r, rS;
 104813   assert( roundUp==0 || roundUp==1 );
 104814   assert( pIdx->nSample>0 );
 104815   if( pVal==0 ) return SQLITE_ERROR;
 104816   n = pIdx->aiRowEst[0];
 104817   aSample = pIdx->aSample;
 104818   eType = sqlite3_value_type(pVal);
 104820   if( eType==SQLITE_INTEGER ){
 104821     v = sqlite3_value_int64(pVal);
 104822     r = (i64)v;
 104823     for(i=0; i<pIdx->nSample; i++){
 104824       if( aSample[i].eType==SQLITE_NULL ) continue;
 104825       if( aSample[i].eType>=SQLITE_TEXT ) break;
 104826       if( aSample[i].eType==SQLITE_INTEGER ){
 104827         if( aSample[i].u.i>=v ){
 104828           isEq = aSample[i].u.i==v;
 104829           break;
 104831       }else{
 104832         assert( aSample[i].eType==SQLITE_FLOAT );
 104833         if( aSample[i].u.r>=r ){
 104834           isEq = aSample[i].u.r==r;
 104835           break;
 104839   }else if( eType==SQLITE_FLOAT ){
 104840     r = sqlite3_value_double(pVal);
 104841     for(i=0; i<pIdx->nSample; i++){
 104842       if( aSample[i].eType==SQLITE_NULL ) continue;
 104843       if( aSample[i].eType>=SQLITE_TEXT ) break;
 104844       if( aSample[i].eType==SQLITE_FLOAT ){
 104845         rS = aSample[i].u.r;
 104846       }else{
 104847         rS = aSample[i].u.i;
 104849       if( rS>=r ){
 104850         isEq = rS==r;
 104851         break;
 104854   }else if( eType==SQLITE_NULL ){
 104855     i = 0;
 104856     if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
 104857   }else{
 104858     assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
 104859     for(i=0; i<pIdx->nSample; i++){
 104860       if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
 104861         break;
 104864     if( i<pIdx->nSample ){      
 104865       sqlite3 *db = pParse->db;
 104866       CollSeq *pColl;
 104867       const u8 *z;
 104868       if( eType==SQLITE_BLOB ){
 104869         z = (const u8 *)sqlite3_value_blob(pVal);
 104870         pColl = db->pDfltColl;
 104871         assert( pColl->enc==SQLITE_UTF8 );
 104872       }else{
 104873         pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
 104874         if( pColl==0 ){
 104875           return SQLITE_ERROR;
 104877         z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
 104878         if( !z ){
 104879           return SQLITE_NOMEM;
 104881         assert( z && pColl && pColl->xCmp );
 104883       n = sqlite3ValueBytes(pVal, pColl->enc);
 104885       for(; i<pIdx->nSample; i++){
 104886         int c;
 104887         int eSampletype = aSample[i].eType;
 104888         if( eSampletype<eType ) continue;
 104889         if( eSampletype!=eType ) break;
 104890 #ifndef SQLITE_OMIT_UTF16
 104891         if( pColl->enc!=SQLITE_UTF8 ){
 104892           int nSample;
 104893           char *zSample = sqlite3Utf8to16(
 104894               db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
 104896           if( !zSample ){
 104897             assert( db->mallocFailed );
 104898             return SQLITE_NOMEM;
 104900           c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
 104901           sqlite3DbFree(db, zSample);
 104902         }else
 104903 #endif
 104905           c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
 104907         if( c>=0 ){
 104908           if( c==0 ) isEq = 1;
 104909           break;
 104915   /* At this point, aSample[i] is the first sample that is greater than
 104916   ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
 104917   ** than pVal.  If aSample[i]==pVal, then isEq==1.
 104919   if( isEq ){
 104920     assert( i<pIdx->nSample );
 104921     aStat[0] = aSample[i].nLt;
 104922     aStat[1] = aSample[i].nEq;
 104923   }else{
 104924     tRowcnt iLower, iUpper, iGap;
 104925     if( i==0 ){
 104926       iLower = 0;
 104927       iUpper = aSample[0].nLt;
 104928     }else{
 104929       iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
 104930       iLower = aSample[i-1].nEq + aSample[i-1].nLt;
 104932     aStat[1] = pIdx->avgEq;
 104933     if( iLower>=iUpper ){
 104934       iGap = 0;
 104935     }else{
 104936       iGap = iUpper - iLower;
 104938     if( roundUp ){
 104939       iGap = (iGap*2)/3;
 104940     }else{
 104941       iGap = iGap/3;
 104943     aStat[0] = iLower + iGap;
 104945   return SQLITE_OK;
 104947 #endif /* SQLITE_ENABLE_STAT3 */
 104950 ** If expression pExpr represents a literal value, set *pp to point to
 104951 ** an sqlite3_value structure containing the same value, with affinity
 104952 ** aff applied to it, before returning. It is the responsibility of the 
 104953 ** caller to eventually release this structure by passing it to 
 104954 ** sqlite3ValueFree().
 104956 ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
 104957 ** is an SQL variable that currently has a non-NULL value bound to it,
 104958 ** create an sqlite3_value structure containing this value, again with
 104959 ** affinity aff applied to it, instead.
 104961 ** If neither of the above apply, set *pp to NULL.
 104963 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
 104965 #ifdef SQLITE_ENABLE_STAT3
 104966 static int valueFromExpr(
 104967   Parse *pParse, 
 104968   Expr *pExpr, 
 104969   u8 aff, 
 104970   sqlite3_value **pp
 104972   if( pExpr->op==TK_VARIABLE
 104973    || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
 104975     int iVar = pExpr->iColumn;
 104976     sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
 104977     *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
 104978     return SQLITE_OK;
 104980   return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
 104982 #endif
 104985 ** This function is used to estimate the number of rows that will be visited
 104986 ** by scanning an index for a range of values. The range may have an upper
 104987 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
 104988 ** and lower bounds are represented by pLower and pUpper respectively. For
 104989 ** example, assuming that index p is on t1(a):
 104991 **   ... FROM t1 WHERE a > ? AND a < ? ...
 104992 **                    |_____|   |_____|
 104993 **                       |         |
 104994 **                     pLower    pUpper
 104996 ** If either of the upper or lower bound is not present, then NULL is passed in
 104997 ** place of the corresponding WhereTerm.
 104999 ** The nEq parameter is passed the index of the index column subject to the
 105000 ** range constraint. Or, equivalently, the number of equality constraints
 105001 ** optimized by the proposed index scan. For example, assuming index p is
 105002 ** on t1(a, b), and the SQL query is:
 105004 **   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
 105006 ** then nEq should be passed the value 1 (as the range restricted column,
 105007 ** b, is the second left-most column of the index). Or, if the query is:
 105009 **   ... FROM t1 WHERE a > ? AND a < ? ...
 105011 ** then nEq should be passed 0.
 105013 ** The returned value is an integer divisor to reduce the estimated
 105014 ** search space.  A return value of 1 means that range constraints are
 105015 ** no help at all.  A return value of 2 means range constraints are
 105016 ** expected to reduce the search space by half.  And so forth...
 105018 ** In the absence of sqlite_stat3 ANALYZE data, each range inequality
 105019 ** reduces the search space by a factor of 4.  Hence a single constraint (x>?)
 105020 ** results in a return of 4 and a range constraint (x>? AND x<?) results
 105021 ** in a return of 16.
 105023 static int whereRangeScanEst(
 105024   Parse *pParse,       /* Parsing & code generating context */
 105025   Index *p,            /* The index containing the range-compared column; "x" */
 105026   int nEq,             /* index into p->aCol[] of the range-compared column */
 105027   WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
 105028   WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
 105029   double *pRangeDiv   /* OUT: Reduce search space by this divisor */
 105031   int rc = SQLITE_OK;
 105033 #ifdef SQLITE_ENABLE_STAT3
 105035   if( nEq==0 && p->nSample ){
 105036     sqlite3_value *pRangeVal;
 105037     tRowcnt iLower = 0;
 105038     tRowcnt iUpper = p->aiRowEst[0];
 105039     tRowcnt a[2];
 105040     u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
 105042     if( pLower ){
 105043       Expr *pExpr = pLower->pExpr->pRight;
 105044       rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
 105045       assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
 105046       if( rc==SQLITE_OK
 105047        && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
 105049         iLower = a[0];
 105050         if( pLower->eOperator==WO_GT ) iLower += a[1];
 105052       sqlite3ValueFree(pRangeVal);
 105054     if( rc==SQLITE_OK && pUpper ){
 105055       Expr *pExpr = pUpper->pExpr->pRight;
 105056       rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
 105057       assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
 105058       if( rc==SQLITE_OK
 105059        && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
 105061         iUpper = a[0];
 105062         if( pUpper->eOperator==WO_LE ) iUpper += a[1];
 105064       sqlite3ValueFree(pRangeVal);
 105066     if( rc==SQLITE_OK ){
 105067       if( iUpper<=iLower ){
 105068         *pRangeDiv = (double)p->aiRowEst[0];
 105069       }else{
 105070         *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
 105072       WHERETRACE(("range scan regions: %u..%u  div=%g\n",
 105073                   (u32)iLower, (u32)iUpper, *pRangeDiv));
 105074       return SQLITE_OK;
 105077 #else
 105078   UNUSED_PARAMETER(pParse);
 105079   UNUSED_PARAMETER(p);
 105080   UNUSED_PARAMETER(nEq);
 105081 #endif
 105082   assert( pLower || pUpper );
 105083   *pRangeDiv = (double)1;
 105084   if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
 105085   if( pUpper ) *pRangeDiv *= (double)4;
 105086   return rc;
 105089 #ifdef SQLITE_ENABLE_STAT3
 105091 ** Estimate the number of rows that will be returned based on
 105092 ** an equality constraint x=VALUE and where that VALUE occurs in
 105093 ** the histogram data.  This only works when x is the left-most
 105094 ** column of an index and sqlite_stat3 histogram data is available
 105095 ** for that index.  When pExpr==NULL that means the constraint is
 105096 ** "x IS NULL" instead of "x=VALUE".
 105098 ** Write the estimated row count into *pnRow and return SQLITE_OK. 
 105099 ** If unable to make an estimate, leave *pnRow unchanged and return
 105100 ** non-zero.
 105102 ** This routine can fail if it is unable to load a collating sequence
 105103 ** required for string comparison, or if unable to allocate memory
 105104 ** for a UTF conversion required for comparison.  The error is stored
 105105 ** in the pParse structure.
 105107 static int whereEqualScanEst(
 105108   Parse *pParse,       /* Parsing & code generating context */
 105109   Index *p,            /* The index whose left-most column is pTerm */
 105110   Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
 105111   double *pnRow        /* Write the revised row estimate here */
 105113   sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
 105114   u8 aff;                   /* Column affinity */
 105115   int rc;                   /* Subfunction return code */
 105116   tRowcnt a[2];             /* Statistics */
 105118   assert( p->aSample!=0 );
 105119   assert( p->nSample>0 );
 105120   aff = p->pTable->aCol[p->aiColumn[0]].affinity;
 105121   if( pExpr ){
 105122     rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
 105123     if( rc ) goto whereEqualScanEst_cancel;
 105124   }else{
 105125     pRhs = sqlite3ValueNew(pParse->db);
 105127   if( pRhs==0 ) return SQLITE_NOTFOUND;
 105128   rc = whereKeyStats(pParse, p, pRhs, 0, a);
 105129   if( rc==SQLITE_OK ){
 105130     WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
 105131     *pnRow = a[1];
 105133 whereEqualScanEst_cancel:
 105134   sqlite3ValueFree(pRhs);
 105135   return rc;
 105137 #endif /* defined(SQLITE_ENABLE_STAT3) */
 105139 #ifdef SQLITE_ENABLE_STAT3
 105141 ** Estimate the number of rows that will be returned based on
 105142 ** an IN constraint where the right-hand side of the IN operator
 105143 ** is a list of values.  Example:
 105145 **        WHERE x IN (1,2,3,4)
 105147 ** Write the estimated row count into *pnRow and return SQLITE_OK. 
 105148 ** If unable to make an estimate, leave *pnRow unchanged and return
 105149 ** non-zero.
 105151 ** This routine can fail if it is unable to load a collating sequence
 105152 ** required for string comparison, or if unable to allocate memory
 105153 ** for a UTF conversion required for comparison.  The error is stored
 105154 ** in the pParse structure.
 105156 static int whereInScanEst(
 105157   Parse *pParse,       /* Parsing & code generating context */
 105158   Index *p,            /* The index whose left-most column is pTerm */
 105159   ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
 105160   double *pnRow        /* Write the revised row estimate here */
 105162   int rc = SQLITE_OK;         /* Subfunction return code */
 105163   double nEst;                /* Number of rows for a single term */
 105164   double nRowEst = (double)0; /* New estimate of the number of rows */
 105165   int i;                      /* Loop counter */
 105167   assert( p->aSample!=0 );
 105168   for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
 105169     nEst = p->aiRowEst[0];
 105170     rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
 105171     nRowEst += nEst;
 105173   if( rc==SQLITE_OK ){
 105174     if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
 105175     *pnRow = nRowEst;
 105176     WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
 105178   return rc;
 105180 #endif /* defined(SQLITE_ENABLE_STAT3) */
 105183 ** Check to see if column iCol of the table with cursor iTab will appear
 105184 ** in sorted order according to the current query plan.
 105186 ** Return values:
 105188 **    0   iCol is not ordered
 105189 **    1   iCol has only a single value
 105190 **    2   iCol is in ASC order
 105191 **    3   iCol is in DESC order
 105193 static int isOrderedColumn(
 105194   WhereBestIdx *p,
 105195   int iTab,
 105196   int iCol
 105198   int i, j;
 105199   WhereLevel *pLevel = &p->aLevel[p->i-1];
 105200   Index *pIdx;
 105201   u8 sortOrder;
 105202   for(i=p->i-1; i>=0; i--, pLevel--){
 105203     if( pLevel->iTabCur!=iTab ) continue;
 105204     if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
 105205       return 1;
 105207     assert( (pLevel->plan.wsFlags & WHERE_ORDERED)!=0 );
 105208     if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
 105209       if( iCol<0 ){
 105210         sortOrder = 0;
 105211         testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
 105212       }else{
 105213         int n = pIdx->nColumn;
 105214         for(j=0; j<n; j++){
 105215           if( iCol==pIdx->aiColumn[j] ) break;
 105217         if( j>=n ) return 0;
 105218         sortOrder = pIdx->aSortOrder[j];
 105219         testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
 105221     }else{
 105222       if( iCol!=(-1) ) return 0;
 105223       sortOrder = 0;
 105224       testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
 105226     if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
 105227       assert( sortOrder==0 || sortOrder==1 );
 105228       testcase( sortOrder==1 );
 105229       sortOrder = 1 - sortOrder;
 105231     return sortOrder+2;
 105233   return 0;
 105237 ** This routine decides if pIdx can be used to satisfy the ORDER BY
 105238 ** clause, either in whole or in part.  The return value is the 
 105239 ** cumulative number of terms in the ORDER BY clause that are satisfied
 105240 ** by the index pIdx and other indices in outer loops.
 105242 ** The table being queried has a cursor number of "base".  pIdx is the
 105243 ** index that is postulated for use to access the table.
 105245 ** The *pbRev value is set to 0 order 1 depending on whether or not
 105246 ** pIdx should be run in the forward order or in reverse order.
 105248 static int isSortingIndex(
 105249   WhereBestIdx *p,    /* Best index search context */
 105250   Index *pIdx,        /* The index we are testing */
 105251   int base,           /* Cursor number for the table to be sorted */
 105252   int *pbRev          /* Set to 1 for reverse-order scan of pIdx */
 105254   int i;                        /* Number of pIdx terms used */
 105255   int j;                        /* Number of ORDER BY terms satisfied */
 105256   int sortOrder = 2;            /* 0: forward.  1: backward.  2: unknown */
 105257   int nTerm;                    /* Number of ORDER BY terms */
 105258   struct ExprList_item *pOBItem;/* A term of the ORDER BY clause */
 105259   Table *pTab = pIdx->pTable;   /* Table that owns index pIdx */
 105260   ExprList *pOrderBy;           /* The ORDER BY clause */
 105261   Parse *pParse = p->pParse;    /* Parser context */
 105262   sqlite3 *db = pParse->db;     /* Database connection */
 105263   int nPriorSat;                /* ORDER BY terms satisfied by outer loops */
 105264   int seenRowid = 0;            /* True if an ORDER BY rowid term is seen */
 105265   int uniqueNotNull;            /* pIdx is UNIQUE with all terms are NOT NULL */
 105267   if( p->i==0 ){
 105268     nPriorSat = 0;
 105269   }else{
 105270     nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
 105271     if( (p->aLevel[p->i-1].plan.wsFlags & WHERE_ORDERED)==0 ){
 105272       /* This loop cannot be ordered unless the next outer loop is
 105273       ** also ordered */
 105274       return nPriorSat;
 105276     if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){
 105277       /* Only look at the outer-most loop if the OrderByIdxJoin
 105278       ** optimization is disabled */
 105279       return nPriorSat;
 105282   pOrderBy = p->pOrderBy;
 105283   assert( pOrderBy!=0 );
 105284   if( pIdx->bUnordered ){
 105285     /* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot
 105286     ** be used for sorting */
 105287     return nPriorSat;
 105289   nTerm = pOrderBy->nExpr;
 105290   uniqueNotNull = pIdx->onError!=OE_None;
 105291   assert( nTerm>0 );
 105293   /* Argument pIdx must either point to a 'real' named index structure, 
 105294   ** or an index structure allocated on the stack by bestBtreeIndex() to
 105295   ** represent the rowid index that is part of every table.  */
 105296   assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
 105298   /* Match terms of the ORDER BY clause against columns of
 105299   ** the index.
 105301   ** Note that indices have pIdx->nColumn regular columns plus
 105302   ** one additional column containing the rowid.  The rowid column
 105303   ** of the index is also allowed to match against the ORDER BY
 105304   ** clause.
 105306   j = nPriorSat;
 105307   for(i=0,pOBItem=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
 105308     Expr *pOBExpr;          /* The expression of the ORDER BY pOBItem */
 105309     CollSeq *pColl;         /* The collating sequence of pOBExpr */
 105310     int termSortOrder;      /* Sort order for this term */
 105311     int iColumn;            /* The i-th column of the index.  -1 for rowid */
 105312     int iSortOrder;         /* 1 for DESC, 0 for ASC on the i-th index term */
 105313     int isEq;               /* Subject to an == or IS NULL constraint */
 105314     int isMatch;            /* ORDER BY term matches the index term */
 105315     const char *zColl;      /* Name of collating sequence for i-th index term */
 105316     WhereTerm *pConstraint; /* A constraint in the WHERE clause */
 105318     /* If the next term of the ORDER BY clause refers to anything other than
 105319     ** a column in the "base" table, then this index will not be of any
 105320     ** further use in handling the ORDER BY. */
 105321     pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
 105322     if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
 105323       break;
 105326     /* Find column number and collating sequence for the next entry
 105327     ** in the index */
 105328     if( pIdx->zName && i<pIdx->nColumn ){
 105329       iColumn = pIdx->aiColumn[i];
 105330       if( iColumn==pIdx->pTable->iPKey ){
 105331         iColumn = -1;
 105333       iSortOrder = pIdx->aSortOrder[i];
 105334       zColl = pIdx->azColl[i];
 105335       assert( zColl!=0 );
 105336     }else{
 105337       iColumn = -1;
 105338       iSortOrder = 0;
 105339       zColl = 0;
 105342     /* Check to see if the column number and collating sequence of the
 105343     ** index match the column number and collating sequence of the ORDER BY
 105344     ** clause entry.  Set isMatch to 1 if they both match. */
 105345     if( pOBExpr->iColumn==iColumn ){
 105346       if( zColl ){
 105347         pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
 105348         if( !pColl ) pColl = db->pDfltColl;
 105349         isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
 105350       }else{
 105351         isMatch = 1;
 105353     }else{
 105354       isMatch = 0;
 105357     /* termSortOrder is 0 or 1 for whether or not the access loop should
 105358     ** run forward or backwards (respectively) in order to satisfy this 
 105359     ** term of the ORDER BY clause. */
 105360     assert( pOBItem->sortOrder==0 || pOBItem->sortOrder==1 );
 105361     assert( iSortOrder==0 || iSortOrder==1 );
 105362     termSortOrder = iSortOrder ^ pOBItem->sortOrder;
 105364     /* If X is the column in the index and ORDER BY clause, check to see
 105365     ** if there are any X= or X IS NULL constraints in the WHERE clause. */
 105366     pConstraint = findTerm(p->pWC, base, iColumn, p->notReady,
 105367                            WO_EQ|WO_ISNULL|WO_IN, pIdx);
 105368     if( pConstraint==0 ){
 105369       isEq = 0;
 105370     }else if( pConstraint->eOperator==WO_IN ){
 105371       /* Constraints of the form: "X IN ..." cannot be used with an ORDER BY
 105372       ** because we do not know in what order the values on the RHS of the IN
 105373       ** operator will occur. */
 105374       break;
 105375     }else if( pConstraint->eOperator==WO_ISNULL ){
 105376       uniqueNotNull = 0;
 105377       isEq = 1;  /* "X IS NULL" means X has only a single value */
 105378     }else if( pConstraint->prereqRight==0 ){
 105379       isEq = 1;  /* Constraint "X=constant" means X has only a single value */
 105380     }else{
 105381       Expr *pRight = pConstraint->pExpr->pRight;
 105382       if( pRight->op==TK_COLUMN ){
 105383         WHERETRACE(("       .. isOrderedColumn(tab=%d,col=%d)",
 105384                     pRight->iTable, pRight->iColumn));
 105385         isEq = isOrderedColumn(p, pRight->iTable, pRight->iColumn);
 105386         WHERETRACE((" -> isEq=%d\n", isEq));
 105388         /* If the constraint is of the form X=Y where Y is an ordered value
 105389         ** in an outer loop, then make sure the sort order of Y matches the
 105390         ** sort order required for X. */
 105391         if( isMatch && isEq>=2 && isEq!=pOBItem->sortOrder+2 ){
 105392           testcase( isEq==2 );
 105393           testcase( isEq==3 );
 105394           break;
 105396       }else{
 105397         isEq = 0;  /* "X=expr" places no ordering constraints on X */
 105400     if( !isMatch ){
 105401       if( isEq==0 ){
 105402         break;
 105403       }else{
 105404         continue;
 105406     }else if( isEq!=1 ){
 105407       if( sortOrder==2 ){
 105408         sortOrder = termSortOrder;
 105409       }else if( termSortOrder!=sortOrder ){
 105410         break;
 105413     j++;
 105414     pOBItem++;
 105415     if( iColumn<0 ){
 105416       seenRowid = 1;
 105417       break;
 105418     }else if( pTab->aCol[iColumn].notNull==0 && isEq!=1 ){
 105419       testcase( isEq==0 );
 105420       testcase( isEq==2 );
 105421       testcase( isEq==3 );
 105422       uniqueNotNull = 0;
 105426   /* If we have not found at least one ORDER BY term that matches the
 105427   ** index, then show no progress. */
 105428   if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat;
 105430   /* Return the necessary scan order back to the caller */
 105431   *pbRev = sortOrder & 1;
 105433   /* If there was an "ORDER BY rowid" term that matched, or it is only
 105434   ** possible for a single row from this table to match, then skip over
 105435   ** any additional ORDER BY terms dealing with this table.
 105437   if( seenRowid || (uniqueNotNull && i>=pIdx->nColumn) ){
 105438     /* Advance j over additional ORDER BY terms associated with base */
 105439     WhereMaskSet *pMS = p->pWC->pMaskSet;
 105440     Bitmask m = ~getMask(pMS, base);
 105441     while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
 105442       j++;
 105445   return j;
 105449 ** Find the best query plan for accessing a particular table.  Write the
 105450 ** best query plan and its cost into the p->cost.
 105452 ** The lowest cost plan wins.  The cost is an estimate of the amount of
 105453 ** CPU and disk I/O needed to process the requested result.
 105454 ** Factors that influence cost include:
 105456 **    *  The estimated number of rows that will be retrieved.  (The
 105457 **       fewer the better.)
 105459 **    *  Whether or not sorting must occur.
 105461 **    *  Whether or not there must be separate lookups in the
 105462 **       index and in the main table.
 105464 ** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
 105465 ** the SQL statement, then this function only considers plans using the 
 105466 ** named index. If no such plan is found, then the returned cost is
 105467 ** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
 105468 ** then the cost is calculated in the usual way.
 105470 ** If a NOT INDEXED clause was attached to the table 
 105471 ** in the SELECT statement, then no indexes are considered. However, the 
 105472 ** selected plan may still take advantage of the built-in rowid primary key
 105473 ** index.
 105475 static void bestBtreeIndex(WhereBestIdx *p){
 105476   Parse *pParse = p->pParse;  /* The parsing context */
 105477   WhereClause *pWC = p->pWC;  /* The WHERE clause */
 105478   struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 105479   int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
 105480   Index *pProbe;              /* An index we are evaluating */
 105481   Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
 105482   int eqTermMask;             /* Current mask of valid equality operators */
 105483   int idxEqTermMask;          /* Index mask of valid equality operators */
 105484   Index sPk;                  /* A fake index object for the primary key */
 105485   tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
 105486   int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
 105487   int wsFlagMask;             /* Allowed flags in p->cost.plan.wsFlag */
 105488   int nPriorSat;              /* ORDER BY terms satisfied by outer loops */
 105489   int nOrderBy;               /* Number of ORDER BY terms */
 105490   char bSortInit;             /* Initializer for bSort in inner loop */
 105491   char bDistInit;             /* Initializer for bDist in inner loop */
 105494   /* Initialize the cost to a worst-case value */
 105495   memset(&p->cost, 0, sizeof(p->cost));
 105496   p->cost.rCost = SQLITE_BIG_DBL;
 105498   /* If the pSrc table is the right table of a LEFT JOIN then we may not
 105499   ** use an index to satisfy IS NULL constraints on that table.  This is
 105500   ** because columns might end up being NULL if the table does not match -
 105501   ** a circumstance which the index cannot help us discover.  Ticket #2177.
 105503   if( pSrc->jointype & JT_LEFT ){
 105504     idxEqTermMask = WO_EQ|WO_IN;
 105505   }else{
 105506     idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
 105509   if( pSrc->pIndex ){
 105510     /* An INDEXED BY clause specifies a particular index to use */
 105511     pIdx = pProbe = pSrc->pIndex;
 105512     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
 105513     eqTermMask = idxEqTermMask;
 105514   }else{
 105515     /* There is no INDEXED BY clause.  Create a fake Index object in local
 105516     ** variable sPk to represent the rowid primary key index.  Make this
 105517     ** fake index the first in a chain of Index objects with all of the real
 105518     ** indices to follow */
 105519     Index *pFirst;                  /* First of real indices on the table */
 105520     memset(&sPk, 0, sizeof(Index));
 105521     sPk.nColumn = 1;
 105522     sPk.aiColumn = &aiColumnPk;
 105523     sPk.aiRowEst = aiRowEstPk;
 105524     sPk.onError = OE_Replace;
 105525     sPk.pTable = pSrc->pTab;
 105526     aiRowEstPk[0] = pSrc->pTab->nRowEst;
 105527     aiRowEstPk[1] = 1;
 105528     pFirst = pSrc->pTab->pIndex;
 105529     if( pSrc->notIndexed==0 ){
 105530       /* The real indices of the table are only considered if the
 105531       ** NOT INDEXED qualifier is omitted from the FROM clause */
 105532       sPk.pNext = pFirst;
 105534     pProbe = &sPk;
 105535     wsFlagMask = ~(
 105536         WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
 105538     eqTermMask = WO_EQ|WO_IN;
 105539     pIdx = 0;
 105542   nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
 105543   if( p->i ){
 105544     nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
 105545     bSortInit = nPriorSat<nOrderBy;
 105546     bDistInit = 0;
 105547   }else{
 105548     nPriorSat = 0;
 105549     bSortInit = nOrderBy>0;
 105550     bDistInit = p->pDistinct!=0;
 105553   /* Loop over all indices looking for the best one to use
 105555   for(; pProbe; pIdx=pProbe=pProbe->pNext){
 105556     const tRowcnt * const aiRowEst = pProbe->aiRowEst;
 105557     WhereCost pc;               /* Cost of using pProbe */
 105558     double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
 105560     /* The following variables are populated based on the properties of
 105561     ** index being evaluated. They are then used to determine the expected
 105562     ** cost and number of rows returned.
 105564     **  pc.plan.nEq: 
 105565     **    Number of equality terms that can be implemented using the index.
 105566     **    In other words, the number of initial fields in the index that
 105567     **    are used in == or IN or NOT NULL constraints of the WHERE clause.
 105569     **  nInMul:  
 105570     **    The "in-multiplier". This is an estimate of how many seek operations 
 105571     **    SQLite must perform on the index in question. For example, if the 
 105572     **    WHERE clause is:
 105574     **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
 105576     **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is 
 105577     **    set to 9. Given the same schema and either of the following WHERE 
 105578     **    clauses:
 105580     **      WHERE a =  1
 105581     **      WHERE a >= 2
 105583     **    nInMul is set to 1.
 105585     **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
 105586     **    the sub-select is assumed to return 25 rows for the purposes of 
 105587     **    determining nInMul.
 105589     **  bInEst:  
 105590     **    Set to true if there was at least one "x IN (SELECT ...)" term used 
 105591     **    in determining the value of nInMul.  Note that the RHS of the
 105592     **    IN operator must be a SELECT, not a value list, for this variable
 105593     **    to be true.
 105595     **  rangeDiv:
 105596     **    An estimate of a divisor by which to reduce the search space due
 105597     **    to inequality constraints.  In the absence of sqlite_stat3 ANALYZE
 105598     **    data, a single inequality reduces the search space to 1/4rd its
 105599     **    original size (rangeDiv==4).  Two inequalities reduce the search
 105600     **    space to 1/16th of its original size (rangeDiv==16).
 105602     **  bSort:   
 105603     **    Boolean. True if there is an ORDER BY clause that will require an 
 105604     **    external sort (i.e. scanning the index being evaluated will not 
 105605     **    correctly order records).
 105607     **  bDist:
 105608     **    Boolean. True if there is a DISTINCT clause that will require an 
 105609     **    external btree.
 105611     **  bLookup: 
 105612     **    Boolean. True if a table lookup is required for each index entry
 105613     **    visited.  In other words, true if this is not a covering index.
 105614     **    This is always false for the rowid primary key index of a table.
 105615     **    For other indexes, it is true unless all the columns of the table
 105616     **    used by the SELECT statement are present in the index (such an
 105617     **    index is sometimes described as a covering index).
 105618     **    For example, given the index on (a, b), the second of the following 
 105619     **    two queries requires table b-tree lookups in order to find the value
 105620     **    of column c, but the first does not because columns a and b are
 105621     **    both available in the index.
 105623     **             SELECT a, b    FROM tbl WHERE a = 1;
 105624     **             SELECT a, b, c FROM tbl WHERE a = 1;
 105626     int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
 105627     int nInMul = 1;               /* Number of distinct equalities to lookup */
 105628     double rangeDiv = (double)1;  /* Estimated reduction in search space */
 105629     int nBound = 0;               /* Number of range constraints seen */
 105630     char bSort = bSortInit;       /* True if external sort required */
 105631     char bDist = bDistInit;       /* True if index cannot help with DISTINCT */
 105632     char bLookup = 0;             /* True if not a covering index */
 105633     WhereTerm *pTerm;             /* A single term of the WHERE clause */
 105634 #ifdef SQLITE_ENABLE_STAT3
 105635     WhereTerm *pFirstTerm = 0;    /* First term matching the index */
 105636 #endif
 105638     WHERETRACE((
 105639       "   %s(%s):\n",
 105640       pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk")
 105641     ));
 105642     memset(&pc, 0, sizeof(pc));
 105643     pc.plan.nOBSat = nPriorSat;
 105645     /* Determine the values of pc.plan.nEq and nInMul */
 105646     for(pc.plan.nEq=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
 105647       int j = pProbe->aiColumn[pc.plan.nEq];
 105648       pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
 105649       if( pTerm==0 ) break;
 105650       pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
 105651       testcase( pTerm->pWC!=pWC );
 105652       if( pTerm->eOperator & WO_IN ){
 105653         Expr *pExpr = pTerm->pExpr;
 105654         pc.plan.wsFlags |= WHERE_COLUMN_IN;
 105655         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 105656           /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
 105657           nInMul *= 25;
 105658           bInEst = 1;
 105659         }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
 105660           /* "x IN (value, value, ...)" */
 105661           nInMul *= pExpr->x.pList->nExpr;
 105663       }else if( pTerm->eOperator & WO_ISNULL ){
 105664         pc.plan.wsFlags |= WHERE_COLUMN_NULL;
 105666 #ifdef SQLITE_ENABLE_STAT3
 105667       if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
 105668 #endif
 105669       pc.used |= pTerm->prereqRight;
 105672     /* If the index being considered is UNIQUE, and there is an equality 
 105673     ** constraint for all columns in the index, then this search will find
 105674     ** at most a single row. In this case set the WHERE_UNIQUE flag to 
 105675     ** indicate this to the caller.
 105677     ** Otherwise, if the search may find more than one row, test to see if
 105678     ** there is a range constraint on indexed column (pc.plan.nEq+1) that can be 
 105679     ** optimized using the index. 
 105681     if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
 105682       testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
 105683       testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
 105684       if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
 105685         pc.plan.wsFlags |= WHERE_UNIQUE;
 105686         if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
 105687           pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
 105690     }else if( pProbe->bUnordered==0 ){
 105691       int j;
 105692       j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
 105693       if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
 105694         WhereTerm *pTop, *pBtm;
 105695         pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
 105696         pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
 105697         whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
 105698         if( pTop ){
 105699           nBound = 1;
 105700           pc.plan.wsFlags |= WHERE_TOP_LIMIT;
 105701           pc.used |= pTop->prereqRight;
 105702           testcase( pTop->pWC!=pWC );
 105704         if( pBtm ){
 105705           nBound++;
 105706           pc.plan.wsFlags |= WHERE_BTM_LIMIT;
 105707           pc.used |= pBtm->prereqRight;
 105708           testcase( pBtm->pWC!=pWC );
 105710         pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
 105714     /* If there is an ORDER BY clause and the index being considered will
 105715     ** naturally scan rows in the required order, set the appropriate flags
 105716     ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
 105717     ** the index will scan rows in a different order, set the bSort
 105718     ** variable.  */
 105719     if( bSort && (pSrc->jointype & JT_LEFT)==0 ){
 105720       int bRev = 2;
 105721       WHERETRACE(("      --> before isSortingIndex: nPriorSat=%d\n",nPriorSat));
 105722       pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, &bRev);
 105723       WHERETRACE(("      --> after  isSortingIndex: bRev=%d nOBSat=%d\n",
 105724                   bRev, pc.plan.nOBSat));
 105725       if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_UNIQUE)!=0 ){
 105726         pc.plan.wsFlags |= WHERE_ORDERED;
 105728       if( nOrderBy==pc.plan.nOBSat ){
 105729         bSort = 0;
 105730         pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
 105732       if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
 105735     /* If there is a DISTINCT qualifier and this index will scan rows in
 105736     ** order of the DISTINCT expressions, clear bDist and set the appropriate
 105737     ** flags in pc.plan.wsFlags. */
 105738     if( bDist
 105739      && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
 105740      && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
 105742       bDist = 0;
 105743       pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
 105746     /* If currently calculating the cost of using an index (not the IPK
 105747     ** index), determine if all required column data may be obtained without 
 105748     ** using the main table (i.e. if the index is a covering
 105749     ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
 105750     ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true.  */
 105751     if( pIdx ){
 105752       Bitmask m = pSrc->colUsed;
 105753       int j;
 105754       for(j=0; j<pIdx->nColumn; j++){
 105755         int x = pIdx->aiColumn[j];
 105756         if( x<BMS-1 ){
 105757           m &= ~(((Bitmask)1)<<x);
 105760       if( m==0 ){
 105761         pc.plan.wsFlags |= WHERE_IDX_ONLY;
 105762       }else{
 105763         bLookup = 1;
 105768     ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
 105769     ** constraint, do not let the estimate exceed half the rows in the table.
 105771     pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
 105772     if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
 105773       pc.plan.nRow = aiRowEst[0]/2;
 105774       nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
 105777 #ifdef SQLITE_ENABLE_STAT3
 105778     /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
 105779     ** and we do not think that values of x are unique and if histogram
 105780     ** data is available for column x, then it might be possible
 105781     ** to get a better estimate on the number of rows based on
 105782     ** VALUE and how common that value is according to the histogram.
 105784     if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
 105785      && pFirstTerm!=0 && aiRowEst[1]>1 ){
 105786       assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
 105787       if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
 105788         testcase( pFirstTerm->eOperator==WO_EQ );
 105789         testcase( pFirstTerm->eOperator==WO_ISNULL );
 105790         whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
 105791                           &pc.plan.nRow);
 105792       }else if( bInEst==0 ){
 105793         assert( pFirstTerm->eOperator==WO_IN );
 105794         whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
 105795                        &pc.plan.nRow);
 105798 #endif /* SQLITE_ENABLE_STAT3 */
 105800     /* Adjust the number of output rows and downward to reflect rows
 105801     ** that are excluded by range constraints.
 105803     pc.plan.nRow = pc.plan.nRow/rangeDiv;
 105804     if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
 105806     /* Experiments run on real SQLite databases show that the time needed
 105807     ** to do a binary search to locate a row in a table or index is roughly
 105808     ** log10(N) times the time to move from one row to the next row within
 105809     ** a table or index.  The actual times can vary, with the size of
 105810     ** records being an important factor.  Both moves and searches are
 105811     ** slower with larger records, presumably because fewer records fit
 105812     ** on one page and hence more pages have to be fetched.
 105814     ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
 105815     ** not give us data on the relative sizes of table and index records.
 105816     ** So this computation assumes table records are about twice as big
 105817     ** as index records
 105819     if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED))==WHERE_IDX_ONLY
 105820      && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
 105821      && sqlite3GlobalConfig.bUseCis
 105822      && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
 105824       /* This index is not useful for indexing, but it is a covering index.
 105825       ** A full-scan of the index might be a little faster than a full-scan
 105826       ** of the table, so give this case a cost slightly less than a table
 105827       ** scan. */
 105828       pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
 105829       pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
 105830     }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
 105831       /* The cost of a full table scan is a number of move operations equal
 105832       ** to the number of rows in the table.
 105834       ** We add an additional 4x penalty to full table scans.  This causes
 105835       ** the cost function to err on the side of choosing an index over
 105836       ** choosing a full scan.  This 4x full-scan penalty is an arguable
 105837       ** decision and one which we expect to revisit in the future.  But
 105838       ** it seems to be working well enough at the moment.
 105840       pc.rCost = aiRowEst[0]*4;
 105841       pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
 105842       if( pIdx ){
 105843         pc.plan.wsFlags &= ~WHERE_ORDERED;
 105844         pc.plan.nOBSat = nPriorSat;
 105846     }else{
 105847       log10N = estLog(aiRowEst[0]);
 105848       pc.rCost = pc.plan.nRow;
 105849       if( pIdx ){
 105850         if( bLookup ){
 105851           /* For an index lookup followed by a table lookup:
 105852           **    nInMul index searches to find the start of each index range
 105853           **  + nRow steps through the index
 105854           **  + nRow table searches to lookup the table entry using the rowid
 105856           pc.rCost += (nInMul + pc.plan.nRow)*log10N;
 105857         }else{
 105858           /* For a covering index:
 105859           **     nInMul index searches to find the initial entry 
 105860           **   + nRow steps through the index
 105862           pc.rCost += nInMul*log10N;
 105864       }else{
 105865         /* For a rowid primary key lookup:
 105866         **    nInMult table searches to find the initial entry for each range
 105867         **  + nRow steps through the table
 105869         pc.rCost += nInMul*log10N;
 105873     /* Add in the estimated cost of sorting the result.  Actual experimental
 105874     ** measurements of sorting performance in SQLite show that sorting time
 105875     ** adds C*N*log10(N) to the cost, where N is the number of rows to be 
 105876     ** sorted and C is a factor between 1.95 and 4.3.  We will split the
 105877     ** difference and select C of 3.0.
 105879     if( bSort ){
 105880       double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
 105881       m *= (double)(pc.plan.nOBSat ? 2 : 3);
 105882       pc.rCost += pc.plan.nRow*m;
 105884     if( bDist ){
 105885       pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
 105888     /**** Cost of using this index has now been computed ****/
 105890     /* If there are additional constraints on this table that cannot
 105891     ** be used with the current index, but which might lower the number
 105892     ** of output rows, adjust the nRow value accordingly.  This only 
 105893     ** matters if the current index is the least costly, so do not bother
 105894     ** with this step if we already know this index will not be chosen.
 105895     ** Also, never reduce the output row count below 2 using this step.
 105897     ** It is critical that the notValid mask be used here instead of
 105898     ** the notReady mask.  When computing an "optimal" index, the notReady
 105899     ** mask will only have one bit set - the bit for the current table.
 105900     ** The notValid mask, on the other hand, always has all bits set for
 105901     ** tables that are not in outer loops.  If notReady is used here instead
 105902     ** of notValid, then a optimal index that depends on inner joins loops
 105903     ** might be selected even when there exists an optimal index that has
 105904     ** no such dependency.
 105906     if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
 105907       int k;                       /* Loop counter */
 105908       int nSkipEq = pc.plan.nEq;   /* Number of == constraints to skip */
 105909       int nSkipRange = nBound;     /* Number of < constraints to skip */
 105910       Bitmask thisTab;             /* Bitmap for pSrc */
 105912       thisTab = getMask(pWC->pMaskSet, iCur);
 105913       for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
 105914         if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
 105915         if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
 105916         if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
 105917           if( nSkipEq ){
 105918             /* Ignore the first pc.plan.nEq equality matches since the index
 105919             ** has already accounted for these */
 105920             nSkipEq--;
 105921           }else{
 105922             /* Assume each additional equality match reduces the result
 105923             ** set size by a factor of 10 */
 105924             pc.plan.nRow /= 10;
 105926         }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
 105927           if( nSkipRange ){
 105928             /* Ignore the first nSkipRange range constraints since the index
 105929             ** has already accounted for these */
 105930             nSkipRange--;
 105931           }else{
 105932             /* Assume each additional range constraint reduces the result
 105933             ** set size by a factor of 3.  Indexed range constraints reduce
 105934             ** the search space by a larger factor: 4.  We make indexed range
 105935             ** more selective intentionally because of the subjective 
 105936             ** observation that indexed range constraints really are more
 105937             ** selective in practice, on average. */
 105938             pc.plan.nRow /= 3;
 105940         }else if( pTerm->eOperator!=WO_NOOP ){
 105941           /* Any other expression lowers the output row count by half */
 105942           pc.plan.nRow /= 2;
 105945       if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
 105949     WHERETRACE((
 105950       "      nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
 105951       "      notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
 105952       "      used=0x%llx nOBSat=%d\n",
 105953       pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
 105954       p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used,
 105955       pc.plan.nOBSat
 105956     ));
 105958     /* If this index is the best we have seen so far, then record this
 105959     ** index and its cost in the p->cost structure.
 105961     if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
 105962       p->cost = pc;
 105963       p->cost.plan.wsFlags &= wsFlagMask;
 105964       p->cost.plan.u.pIdx = pIdx;
 105967     /* If there was an INDEXED BY clause, then only that one index is
 105968     ** considered. */
 105969     if( pSrc->pIndex ) break;
 105971     /* Reset masks for the next index in the loop */
 105972     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
 105973     eqTermMask = idxEqTermMask;
 105976   /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
 105977   ** is set, then reverse the order that the index will be scanned
 105978   ** in. This is used for application testing, to help find cases
 105979   ** where application behaviour depends on the (undefined) order that
 105980   ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
 105981   if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
 105982     p->cost.plan.wsFlags |= WHERE_REVERSE;
 105985   assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
 105986   assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
 105987   assert( pSrc->pIndex==0 
 105988        || p->cost.plan.u.pIdx==0 
 105989        || p->cost.plan.u.pIdx==pSrc->pIndex 
 105992   WHERETRACE(("   best index is: %s\n",
 105993          p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk"));
 105995   bestOrClauseIndex(p);
 105996   bestAutomaticIndex(p);
 105997   p->cost.plan.wsFlags |= eqTermMask;
 106001 ** Find the query plan for accessing table pSrc->pTab. Write the
 106002 ** best query plan and its cost into the WhereCost object supplied 
 106003 ** as the last parameter. This function may calculate the cost of
 106004 ** both real and virtual table scans.
 106006 ** This function does not take ORDER BY or DISTINCT into account.  Nor
 106007 ** does it remember the virtual table query plan.  All it does is compute
 106008 ** the cost while determining if an OR optimization is applicable.  The
 106009 ** details will be reconsidered later if the optimization is found to be
 106010 ** applicable.
 106012 static void bestIndex(WhereBestIdx *p){
 106013 #ifndef SQLITE_OMIT_VIRTUALTABLE
 106014   if( IsVirtual(p->pSrc->pTab) ){
 106015     sqlite3_index_info *pIdxInfo = 0;
 106016     p->ppIdxInfo = &pIdxInfo;
 106017     bestVirtualIndex(p);
 106018     if( pIdxInfo->needToFreeIdxStr ){
 106019       sqlite3_free(pIdxInfo->idxStr);
 106021     sqlite3DbFree(p->pParse->db, pIdxInfo);
 106022   }else
 106023 #endif
 106025     bestBtreeIndex(p);
 106030 ** Disable a term in the WHERE clause.  Except, do not disable the term
 106031 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
 106032 ** or USING clause of that join.
 106034 ** Consider the term t2.z='ok' in the following queries:
 106036 **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
 106037 **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
 106038 **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
 106040 ** The t2.z='ok' is disabled in the in (2) because it originates
 106041 ** in the ON clause.  The term is disabled in (3) because it is not part
 106042 ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
 106044 ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
 106045 ** completely satisfied by indices.
 106047 ** Disabling a term causes that term to not be tested in the inner loop
 106048 ** of the join.  Disabling is an optimization.  When terms are satisfied
 106049 ** by indices, we disable them to prevent redundant tests in the inner
 106050 ** loop.  We would get the correct results if nothing were ever disabled,
 106051 ** but joins might run a little slower.  The trick is to disable as much
 106052 ** as we can without disabling too much.  If we disabled in (1), we'd get
 106053 ** the wrong answer.  See ticket #813.
 106055 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
 106056   if( pTerm
 106057       && (pTerm->wtFlags & TERM_CODED)==0
 106058       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
 106060     pTerm->wtFlags |= TERM_CODED;
 106061     if( pTerm->iParent>=0 ){
 106062       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
 106063       if( (--pOther->nChild)==0 ){
 106064         disableTerm(pLevel, pOther);
 106071 ** Code an OP_Affinity opcode to apply the column affinity string zAff
 106072 ** to the n registers starting at base. 
 106074 ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
 106075 ** beginning and end of zAff are ignored.  If all entries in zAff are
 106076 ** SQLITE_AFF_NONE, then no code gets generated.
 106078 ** This routine makes its own copy of zAff so that the caller is free
 106079 ** to modify zAff after this routine returns.
 106081 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
 106082   Vdbe *v = pParse->pVdbe;
 106083   if( zAff==0 ){
 106084     assert( pParse->db->mallocFailed );
 106085     return;
 106087   assert( v!=0 );
 106089   /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
 106090   ** and end of the affinity string.
 106092   while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
 106093     n--;
 106094     base++;
 106095     zAff++;
 106097   while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
 106098     n--;
 106101   /* Code the OP_Affinity opcode if there is anything left to do. */
 106102   if( n>0 ){
 106103     sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
 106104     sqlite3VdbeChangeP4(v, -1, zAff, n);
 106105     sqlite3ExprCacheAffinityChange(pParse, base, n);
 106111 ** Generate code for a single equality term of the WHERE clause.  An equality
 106112 ** term can be either X=expr or X IN (...).   pTerm is the term to be 
 106113 ** coded.
 106115 ** The current value for the constraint is left in register iReg.
 106117 ** For a constraint of the form X=expr, the expression is evaluated and its
 106118 ** result is left on the stack.  For constraints of the form X IN (...)
 106119 ** this routine sets up a loop that will iterate over all values of X.
 106121 static int codeEqualityTerm(
 106122   Parse *pParse,      /* The parsing context */
 106123   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
 106124   WhereLevel *pLevel, /* When level of the FROM clause we are working on */
 106125   int iTarget         /* Attempt to leave results in this register */
 106127   Expr *pX = pTerm->pExpr;
 106128   Vdbe *v = pParse->pVdbe;
 106129   int iReg;                  /* Register holding results */
 106131   assert( iTarget>0 );
 106132   if( pX->op==TK_EQ ){
 106133     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
 106134   }else if( pX->op==TK_ISNULL ){
 106135     iReg = iTarget;
 106136     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
 106137 #ifndef SQLITE_OMIT_SUBQUERY
 106138   }else{
 106139     int eType;
 106140     int iTab;
 106141     struct InLoop *pIn;
 106143     assert( pX->op==TK_IN );
 106144     iReg = iTarget;
 106145     eType = sqlite3FindInIndex(pParse, pX, 0);
 106146     iTab = pX->iTable;
 106147     sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
 106148     assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
 106149     if( pLevel->u.in.nIn==0 ){
 106150       pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
 106152     pLevel->u.in.nIn++;
 106153     pLevel->u.in.aInLoop =
 106154        sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
 106155                               sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
 106156     pIn = pLevel->u.in.aInLoop;
 106157     if( pIn ){
 106158       pIn += pLevel->u.in.nIn - 1;
 106159       pIn->iCur = iTab;
 106160       if( eType==IN_INDEX_ROWID ){
 106161         pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
 106162       }else{
 106163         pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
 106165       sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
 106166     }else{
 106167       pLevel->u.in.nIn = 0;
 106169 #endif
 106171   disableTerm(pLevel, pTerm);
 106172   return iReg;
 106176 ** Generate code that will evaluate all == and IN constraints for an
 106177 ** index.
 106179 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
 106180 ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
 106181 ** The index has as many as three equality constraints, but in this
 106182 ** example, the third "c" value is an inequality.  So only two 
 106183 ** constraints are coded.  This routine will generate code to evaluate
 106184 ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
 106185 ** in consecutive registers and the index of the first register is returned.
 106187 ** In the example above nEq==2.  But this subroutine works for any value
 106188 ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
 106189 ** The only thing it does is allocate the pLevel->iMem memory cell and
 106190 ** compute the affinity string.
 106192 ** This routine always allocates at least one memory cell and returns
 106193 ** the index of that memory cell. The code that
 106194 ** calls this routine will use that memory cell to store the termination
 106195 ** key value of the loop.  If one or more IN operators appear, then
 106196 ** this routine allocates an additional nEq memory cells for internal
 106197 ** use.
 106199 ** Before returning, *pzAff is set to point to a buffer containing a
 106200 ** copy of the column affinity string of the index allocated using
 106201 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
 106202 ** with equality constraints that use NONE affinity are set to
 106203 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
 106205 **   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
 106206 **   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
 106208 ** In the example above, the index on t1(a) has TEXT affinity. But since
 106209 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
 106210 ** no conversion should be attempted before using a t2.b value as part of
 106211 ** a key to search the index. Hence the first byte in the returned affinity
 106212 ** string in this example would be set to SQLITE_AFF_NONE.
 106214 static int codeAllEqualityTerms(
 106215   Parse *pParse,        /* Parsing context */
 106216   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
 106217   WhereClause *pWC,     /* The WHERE clause */
 106218   Bitmask notReady,     /* Which parts of FROM have not yet been coded */
 106219   int nExtraReg,        /* Number of extra registers to allocate */
 106220   char **pzAff          /* OUT: Set to point to affinity string */
 106222   int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
 106223   Vdbe *v = pParse->pVdbe;      /* The vm under construction */
 106224   Index *pIdx;                  /* The index being used for this loop */
 106225   int iCur = pLevel->iTabCur;   /* The cursor of the table */
 106226   WhereTerm *pTerm;             /* A single constraint term */
 106227   int j;                        /* Loop counter */
 106228   int regBase;                  /* Base register */
 106229   int nReg;                     /* Number of registers to allocate */
 106230   char *zAff;                   /* Affinity string to return */
 106232   /* This module is only called on query plans that use an index. */
 106233   assert( pLevel->plan.wsFlags & WHERE_INDEXED );
 106234   pIdx = pLevel->plan.u.pIdx;
 106236   /* Figure out how many memory cells we will need then allocate them.
 106238   regBase = pParse->nMem + 1;
 106239   nReg = pLevel->plan.nEq + nExtraReg;
 106240   pParse->nMem += nReg;
 106242   zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
 106243   if( !zAff ){
 106244     pParse->db->mallocFailed = 1;
 106247   /* Evaluate the equality constraints
 106249   assert( pIdx->nColumn>=nEq );
 106250   for(j=0; j<nEq; j++){
 106251     int r1;
 106252     int k = pIdx->aiColumn[j];
 106253     pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
 106254     if( pTerm==0 ) break;
 106255     /* The following true for indices with redundant columns. 
 106256     ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
 106257     testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
 106258     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
 106259     r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
 106260     if( r1!=regBase+j ){
 106261       if( nReg==1 ){
 106262         sqlite3ReleaseTempReg(pParse, regBase);
 106263         regBase = r1;
 106264       }else{
 106265         sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
 106268     testcase( pTerm->eOperator & WO_ISNULL );
 106269     testcase( pTerm->eOperator & WO_IN );
 106270     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
 106271       Expr *pRight = pTerm->pExpr->pRight;
 106272       sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
 106273       if( zAff ){
 106274         if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
 106275           zAff[j] = SQLITE_AFF_NONE;
 106277         if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
 106278           zAff[j] = SQLITE_AFF_NONE;
 106283   *pzAff = zAff;
 106284   return regBase;
 106287 #ifndef SQLITE_OMIT_EXPLAIN
 106289 ** This routine is a helper for explainIndexRange() below
 106291 ** pStr holds the text of an expression that we are building up one term
 106292 ** at a time.  This routine adds a new term to the end of the expression.
 106293 ** Terms are separated by AND so add the "AND" text for second and subsequent
 106294 ** terms only.
 106296 static void explainAppendTerm(
 106297   StrAccum *pStr,             /* The text expression being built */
 106298   int iTerm,                  /* Index of this term.  First is zero */
 106299   const char *zColumn,        /* Name of the column */
 106300   const char *zOp             /* Name of the operator */
 106302   if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
 106303   sqlite3StrAccumAppend(pStr, zColumn, -1);
 106304   sqlite3StrAccumAppend(pStr, zOp, 1);
 106305   sqlite3StrAccumAppend(pStr, "?", 1);
 106309 ** Argument pLevel describes a strategy for scanning table pTab. This 
 106310 ** function returns a pointer to a string buffer containing a description
 106311 ** of the subset of table rows scanned by the strategy in the form of an
 106312 ** SQL expression. Or, if all rows are scanned, NULL is returned.
 106314 ** For example, if the query:
 106316 **   SELECT * FROM t1 WHERE a=1 AND b>2;
 106318 ** is run and there is an index on (a, b), then this function returns a
 106319 ** string similar to:
 106321 **   "a=? AND b>?"
 106323 ** The returned pointer points to memory obtained from sqlite3DbMalloc().
 106324 ** It is the responsibility of the caller to free the buffer when it is
 106325 ** no longer required.
 106327 static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
 106328   WherePlan *pPlan = &pLevel->plan;
 106329   Index *pIndex = pPlan->u.pIdx;
 106330   int nEq = pPlan->nEq;
 106331   int i, j;
 106332   Column *aCol = pTab->aCol;
 106333   int *aiColumn = pIndex->aiColumn;
 106334   StrAccum txt;
 106336   if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
 106337     return 0;
 106339   sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
 106340   txt.db = db;
 106341   sqlite3StrAccumAppend(&txt, " (", 2);
 106342   for(i=0; i<nEq; i++){
 106343     explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
 106346   j = i;
 106347   if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
 106348     char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
 106349     explainAppendTerm(&txt, i++, z, ">");
 106351   if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
 106352     char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
 106353     explainAppendTerm(&txt, i, z, "<");
 106355   sqlite3StrAccumAppend(&txt, ")", 1);
 106356   return sqlite3StrAccumFinish(&txt);
 106360 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
 106361 ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
 106362 ** record is added to the output to describe the table scan strategy in 
 106363 ** pLevel.
 106365 static void explainOneScan(
 106366   Parse *pParse,                  /* Parse context */
 106367   SrcList *pTabList,              /* Table list this loop refers to */
 106368   WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
 106369   int iLevel,                     /* Value for "level" column of output */
 106370   int iFrom,                      /* Value for "from" column of output */
 106371   u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
 106373   if( pParse->explain==2 ){
 106374     u32 flags = pLevel->plan.wsFlags;
 106375     struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
 106376     Vdbe *v = pParse->pVdbe;      /* VM being constructed */
 106377     sqlite3 *db = pParse->db;     /* Database handle */
 106378     char *zMsg;                   /* Text to add to EQP output */
 106379     sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
 106380     int iId = pParse->iSelectId;  /* Select id (left-most output column) */
 106381     int isSearch;                 /* True for a SEARCH. False for SCAN. */
 106383     if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
 106385     isSearch = (pLevel->plan.nEq>0)
 106386              || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
 106387              || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
 106389     zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
 106390     if( pItem->pSelect ){
 106391       zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
 106392     }else{
 106393       zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
 106396     if( pItem->zAlias ){
 106397       zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
 106399     if( (flags & WHERE_INDEXED)!=0 ){
 106400       char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
 106401       zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, 
 106402           ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
 106403           ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
 106404           ((flags & WHERE_TEMP_INDEX)?"":" "),
 106405           ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
 106406           zWhere
 106408       sqlite3DbFree(db, zWhere);
 106409     }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
 106410       zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
 106412       if( flags&WHERE_ROWID_EQ ){
 106413         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
 106414       }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
 106415         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
 106416       }else if( flags&WHERE_BTM_LIMIT ){
 106417         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
 106418       }else if( flags&WHERE_TOP_LIMIT ){
 106419         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
 106422 #ifndef SQLITE_OMIT_VIRTUALTABLE
 106423     else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
 106424       sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
 106425       zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
 106426                   pVtabIdx->idxNum, pVtabIdx->idxStr);
 106428 #endif
 106429     if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
 106430       testcase( wctrlFlags & WHERE_ORDERBY_MIN );
 106431       nRow = 1;
 106432     }else{
 106433       nRow = (sqlite3_int64)pLevel->plan.nRow;
 106435     zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
 106436     sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
 106439 #else
 106440 # define explainOneScan(u,v,w,x,y,z)
 106441 #endif /* SQLITE_OMIT_EXPLAIN */
 106445 ** Generate code for the start of the iLevel-th loop in the WHERE clause
 106446 ** implementation described by pWInfo.
 106448 static Bitmask codeOneLoopStart(
 106449   WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
 106450   int iLevel,          /* Which level of pWInfo->a[] should be coded */
 106451   u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
 106452   Bitmask notReady     /* Which tables are currently available */
 106454   int j, k;            /* Loop counters */
 106455   int iCur;            /* The VDBE cursor for the table */
 106456   int addrNxt;         /* Where to jump to continue with the next IN case */
 106457   int omitTable;       /* True if we use the index only */
 106458   int bRev;            /* True if we need to scan in reverse order */
 106459   WhereLevel *pLevel;  /* The where level to be coded */
 106460   WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
 106461   WhereTerm *pTerm;               /* A WHERE clause term */
 106462   Parse *pParse;                  /* Parsing context */
 106463   Vdbe *v;                        /* The prepared stmt under constructions */
 106464   struct SrcList_item *pTabItem;  /* FROM clause term being coded */
 106465   int addrBrk;                    /* Jump here to break out of the loop */
 106466   int addrCont;                   /* Jump here to continue with next cycle */
 106467   int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
 106468   int iReleaseReg = 0;      /* Temp register to free before returning */
 106470   pParse = pWInfo->pParse;
 106471   v = pParse->pVdbe;
 106472   pWC = pWInfo->pWC;
 106473   pLevel = &pWInfo->a[iLevel];
 106474   pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
 106475   iCur = pTabItem->iCursor;
 106476   bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
 106477   omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 
 106478            && (wctrlFlags & WHERE_FORCE_TABLE)==0;
 106480   /* Create labels for the "break" and "continue" instructions
 106481   ** for the current loop.  Jump to addrBrk to break out of a loop.
 106482   ** Jump to cont to go immediately to the next iteration of the
 106483   ** loop.
 106485   ** When there is an IN operator, we also have a "addrNxt" label that
 106486   ** means to continue with the next IN value combination.  When
 106487   ** there are no IN operators in the constraints, the "addrNxt" label
 106488   ** is the same as "addrBrk".
 106490   addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
 106491   addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
 106493   /* If this is the right table of a LEFT OUTER JOIN, allocate and
 106494   ** initialize a memory cell that records if this table matches any
 106495   ** row of the left table of the join.
 106497   if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
 106498     pLevel->iLeftJoin = ++pParse->nMem;
 106499     sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
 106500     VdbeComment((v, "init LEFT JOIN no-match flag"));
 106503   /* Special case of a FROM clause subquery implemented as a co-routine */
 106504   if( pTabItem->viaCoroutine ){
 106505     int regYield = pTabItem->regReturn;
 106506     sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield);
 106507     pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
 106508     VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
 106509     sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
 106510     pLevel->op = OP_Goto;
 106511   }else
 106513 #ifndef SQLITE_OMIT_VIRTUALTABLE
 106514   if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
 106515     /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
 106516     **          to access the data.
 106518     int iReg;   /* P3 Value for OP_VFilter */
 106519     sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
 106520     int nConstraint = pVtabIdx->nConstraint;
 106521     struct sqlite3_index_constraint_usage *aUsage =
 106522                                                 pVtabIdx->aConstraintUsage;
 106523     const struct sqlite3_index_constraint *aConstraint =
 106524                                                 pVtabIdx->aConstraint;
 106526     sqlite3ExprCachePush(pParse);
 106527     iReg = sqlite3GetTempRange(pParse, nConstraint+2);
 106528     for(j=1; j<=nConstraint; j++){
 106529       for(k=0; k<nConstraint; k++){
 106530         if( aUsage[k].argvIndex==j ){
 106531           int iTerm = aConstraint[k].iTermOffset;
 106532           sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
 106533           break;
 106536       if( k==nConstraint ) break;
 106538     sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
 106539     sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
 106540     sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
 106541                       pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
 106542     pVtabIdx->needToFreeIdxStr = 0;
 106543     for(j=0; j<nConstraint; j++){
 106544       if( aUsage[j].omit ){
 106545         int iTerm = aConstraint[j].iTermOffset;
 106546         disableTerm(pLevel, &pWC->a[iTerm]);
 106549     pLevel->op = OP_VNext;
 106550     pLevel->p1 = iCur;
 106551     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
 106552     sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
 106553     sqlite3ExprCachePop(pParse, 1);
 106554   }else
 106555 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 106557   if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
 106558     /* Case 1:  We can directly reference a single row using an
 106559     **          equality comparison against the ROWID field.  Or
 106560     **          we reference multiple rows using a "rowid IN (...)"
 106561     **          construct.
 106563     iReleaseReg = sqlite3GetTempReg(pParse);
 106564     pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
 106565     assert( pTerm!=0 );
 106566     assert( pTerm->pExpr!=0 );
 106567     assert( pTerm->leftCursor==iCur );
 106568     assert( omitTable==0 );
 106569     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
 106570     iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
 106571     addrNxt = pLevel->addrNxt;
 106572     sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
 106573     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
 106574     sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
 106575     VdbeComment((v, "pk"));
 106576     pLevel->op = OP_Noop;
 106577   }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
 106578     /* Case 2:  We have an inequality comparison against the ROWID field.
 106580     int testOp = OP_Noop;
 106581     int start;
 106582     int memEndValue = 0;
 106583     WhereTerm *pStart, *pEnd;
 106585     assert( omitTable==0 );
 106586     pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
 106587     pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
 106588     if( bRev ){
 106589       pTerm = pStart;
 106590       pStart = pEnd;
 106591       pEnd = pTerm;
 106593     if( pStart ){
 106594       Expr *pX;             /* The expression that defines the start bound */
 106595       int r1, rTemp;        /* Registers for holding the start boundary */
 106597       /* The following constant maps TK_xx codes into corresponding 
 106598       ** seek opcodes.  It depends on a particular ordering of TK_xx
 106600       const u8 aMoveOp[] = {
 106601            /* TK_GT */  OP_SeekGt,
 106602            /* TK_LE */  OP_SeekLe,
 106603            /* TK_LT */  OP_SeekLt,
 106604            /* TK_GE */  OP_SeekGe
 106606       assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
 106607       assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
 106608       assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
 106610       testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
 106611       pX = pStart->pExpr;
 106612       assert( pX!=0 );
 106613       assert( pStart->leftCursor==iCur );
 106614       r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
 106615       sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
 106616       VdbeComment((v, "pk"));
 106617       sqlite3ExprCacheAffinityChange(pParse, r1, 1);
 106618       sqlite3ReleaseTempReg(pParse, rTemp);
 106619       disableTerm(pLevel, pStart);
 106620     }else{
 106621       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
 106623     if( pEnd ){
 106624       Expr *pX;
 106625       pX = pEnd->pExpr;
 106626       assert( pX!=0 );
 106627       assert( pEnd->leftCursor==iCur );
 106628       testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
 106629       memEndValue = ++pParse->nMem;
 106630       sqlite3ExprCode(pParse, pX->pRight, memEndValue);
 106631       if( pX->op==TK_LT || pX->op==TK_GT ){
 106632         testOp = bRev ? OP_Le : OP_Ge;
 106633       }else{
 106634         testOp = bRev ? OP_Lt : OP_Gt;
 106636       disableTerm(pLevel, pEnd);
 106638     start = sqlite3VdbeCurrentAddr(v);
 106639     pLevel->op = bRev ? OP_Prev : OP_Next;
 106640     pLevel->p1 = iCur;
 106641     pLevel->p2 = start;
 106642     if( pStart==0 && pEnd==0 ){
 106643       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
 106644     }else{
 106645       assert( pLevel->p5==0 );
 106647     if( testOp!=OP_Noop ){
 106648       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
 106649       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
 106650       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
 106651       sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
 106652       sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
 106654   }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
 106655     /* Case 3: A scan using an index.
 106657     **         The WHERE clause may contain zero or more equality 
 106658     **         terms ("==" or "IN" operators) that refer to the N
 106659     **         left-most columns of the index. It may also contain
 106660     **         inequality constraints (>, <, >= or <=) on the indexed
 106661     **         column that immediately follows the N equalities. Only 
 106662     **         the right-most column can be an inequality - the rest must
 106663     **         use the "==" and "IN" operators. For example, if the 
 106664     **         index is on (x,y,z), then the following clauses are all 
 106665     **         optimized:
 106667     **            x=5
 106668     **            x=5 AND y=10
 106669     **            x=5 AND y<10
 106670     **            x=5 AND y>5 AND y<10
 106671     **            x=5 AND y=5 AND z<=10
 106673     **         The z<10 term of the following cannot be used, only
 106674     **         the x=5 term:
 106676     **            x=5 AND z<10
 106678     **         N may be zero if there are inequality constraints.
 106679     **         If there are no inequality constraints, then N is at
 106680     **         least one.
 106682     **         This case is also used when there are no WHERE clause
 106683     **         constraints but an index is selected anyway, in order
 106684     **         to force the output order to conform to an ORDER BY.
 106686     static const u8 aStartOp[] = {
 106689       OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
 106690       OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
 106691       OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
 106692       OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
 106693       OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
 106694       OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
 106696     static const u8 aEndOp[] = {
 106697       OP_Noop,             /* 0: (!end_constraints) */
 106698       OP_IdxGE,            /* 1: (end_constraints && !bRev) */
 106699       OP_IdxLT             /* 2: (end_constraints && bRev) */
 106701     int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
 106702     int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
 106703     int regBase;                 /* Base register holding constraint values */
 106704     int r1;                      /* Temp register */
 106705     WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
 106706     WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
 106707     int startEq;                 /* True if range start uses ==, >= or <= */
 106708     int endEq;                   /* True if range end uses ==, >= or <= */
 106709     int start_constraints;       /* Start of range is constrained */
 106710     int nConstraint;             /* Number of constraint terms */
 106711     Index *pIdx;                 /* The index we will be using */
 106712     int iIdxCur;                 /* The VDBE cursor for the index */
 106713     int nExtraReg = 0;           /* Number of extra registers needed */
 106714     int op;                      /* Instruction opcode */
 106715     char *zStartAff;             /* Affinity for start of range constraint */
 106716     char *zEndAff;               /* Affinity for end of range constraint */
 106718     pIdx = pLevel->plan.u.pIdx;
 106719     iIdxCur = pLevel->iIdxCur;
 106720     k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
 106722     /* If this loop satisfies a sort order (pOrderBy) request that 
 106723     ** was passed to this function to implement a "SELECT min(x) ..." 
 106724     ** query, then the caller will only allow the loop to run for
 106725     ** a single iteration. This means that the first row returned
 106726     ** should not have a NULL value stored in 'x'. If column 'x' is
 106727     ** the first one after the nEq equality constraints in the index,
 106728     ** this requires some special handling.
 106730     if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
 106731      && (pLevel->plan.wsFlags&WHERE_ORDERED)
 106732      && (pIdx->nColumn>nEq)
 106734       /* assert( pOrderBy->nExpr==1 ); */
 106735       /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
 106736       isMinQuery = 1;
 106737       nExtraReg = 1;
 106740     /* Find any inequality constraint terms for the start and end 
 106741     ** of the range. 
 106743     if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
 106744       pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
 106745       nExtraReg = 1;
 106747     if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
 106748       pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
 106749       nExtraReg = 1;
 106752     /* Generate code to evaluate all constraint terms using == or IN
 106753     ** and store the values of those terms in an array of registers
 106754     ** starting at regBase.
 106756     regBase = codeAllEqualityTerms(
 106757         pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
 106759     zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
 106760     addrNxt = pLevel->addrNxt;
 106762     /* If we are doing a reverse order scan on an ascending index, or
 106763     ** a forward order scan on a descending index, interchange the 
 106764     ** start and end terms (pRangeStart and pRangeEnd).
 106766     if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
 106767      || (bRev && pIdx->nColumn==nEq)
 106769       SWAP(WhereTerm *, pRangeEnd, pRangeStart);
 106772     testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
 106773     testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
 106774     testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
 106775     testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
 106776     startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
 106777     endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
 106778     start_constraints = pRangeStart || nEq>0;
 106780     /* Seek the index cursor to the start of the range. */
 106781     nConstraint = nEq;
 106782     if( pRangeStart ){
 106783       Expr *pRight = pRangeStart->pExpr->pRight;
 106784       sqlite3ExprCode(pParse, pRight, regBase+nEq);
 106785       if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
 106786         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
 106788       if( zStartAff ){
 106789         if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
 106790           /* Since the comparison is to be performed with no conversions
 106791           ** applied to the operands, set the affinity to apply to pRight to 
 106792           ** SQLITE_AFF_NONE.  */
 106793           zStartAff[nEq] = SQLITE_AFF_NONE;
 106795         if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
 106796           zStartAff[nEq] = SQLITE_AFF_NONE;
 106799       nConstraint++;
 106800       testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
 106801     }else if( isMinQuery ){
 106802       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
 106803       nConstraint++;
 106804       startEq = 0;
 106805       start_constraints = 1;
 106807     codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
 106808     op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
 106809     assert( op!=0 );
 106810     testcase( op==OP_Rewind );
 106811     testcase( op==OP_Last );
 106812     testcase( op==OP_SeekGt );
 106813     testcase( op==OP_SeekGe );
 106814     testcase( op==OP_SeekLe );
 106815     testcase( op==OP_SeekLt );
 106816     sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
 106818     /* Load the value for the inequality constraint at the end of the
 106819     ** range (if any).
 106821     nConstraint = nEq;
 106822     if( pRangeEnd ){
 106823       Expr *pRight = pRangeEnd->pExpr->pRight;
 106824       sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
 106825       sqlite3ExprCode(pParse, pRight, regBase+nEq);
 106826       if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
 106827         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
 106829       if( zEndAff ){
 106830         if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
 106831           /* Since the comparison is to be performed with no conversions
 106832           ** applied to the operands, set the affinity to apply to pRight to 
 106833           ** SQLITE_AFF_NONE.  */
 106834           zEndAff[nEq] = SQLITE_AFF_NONE;
 106836         if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
 106837           zEndAff[nEq] = SQLITE_AFF_NONE;
 106840       codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
 106841       nConstraint++;
 106842       testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
 106844     sqlite3DbFree(pParse->db, zStartAff);
 106845     sqlite3DbFree(pParse->db, zEndAff);
 106847     /* Top of the loop body */
 106848     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
 106850     /* Check if the index cursor is past the end of the range. */
 106851     op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
 106852     testcase( op==OP_Noop );
 106853     testcase( op==OP_IdxGE );
 106854     testcase( op==OP_IdxLT );
 106855     if( op!=OP_Noop ){
 106856       sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
 106857       sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
 106860     /* If there are inequality constraints, check that the value
 106861     ** of the table column that the inequality contrains is not NULL.
 106862     ** If it is, jump to the next iteration of the loop.
 106864     r1 = sqlite3GetTempReg(pParse);
 106865     testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
 106866     testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
 106867     if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
 106868       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
 106869       sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
 106871     sqlite3ReleaseTempReg(pParse, r1);
 106873     /* Seek the table cursor, if required */
 106874     disableTerm(pLevel, pRangeStart);
 106875     disableTerm(pLevel, pRangeEnd);
 106876     if( !omitTable ){
 106877       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
 106878       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
 106879       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
 106880       sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
 106883     /* Record the instruction used to terminate the loop. Disable 
 106884     ** WHERE clause terms made redundant by the index range scan.
 106886     if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
 106887       pLevel->op = OP_Noop;
 106888     }else if( bRev ){
 106889       pLevel->op = OP_Prev;
 106890     }else{
 106891       pLevel->op = OP_Next;
 106893     pLevel->p1 = iIdxCur;
 106894     if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
 106895       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
 106896     }else{
 106897       assert( pLevel->p5==0 );
 106899   }else
 106901 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
 106902   if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
 106903     /* Case 4:  Two or more separately indexed terms connected by OR
 106905     ** Example:
 106907     **   CREATE TABLE t1(a,b,c,d);
 106908     **   CREATE INDEX i1 ON t1(a);
 106909     **   CREATE INDEX i2 ON t1(b);
 106910     **   CREATE INDEX i3 ON t1(c);
 106912     **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
 106914     ** In the example, there are three indexed terms connected by OR.
 106915     ** The top of the loop looks like this:
 106917     **          Null       1                # Zero the rowset in reg 1
 106919     ** Then, for each indexed term, the following. The arguments to
 106920     ** RowSetTest are such that the rowid of the current row is inserted
 106921     ** into the RowSet. If it is already present, control skips the
 106922     ** Gosub opcode and jumps straight to the code generated by WhereEnd().
 106924     **        sqlite3WhereBegin(<term>)
 106925     **          RowSetTest                  # Insert rowid into rowset
 106926     **          Gosub      2 A
 106927     **        sqlite3WhereEnd()
 106929     ** Following the above, code to terminate the loop. Label A, the target
 106930     ** of the Gosub above, jumps to the instruction right after the Goto.
 106932     **          Null       1                # Zero the rowset in reg 1
 106933     **          Goto       B                # The loop is finished.
 106935     **       A: <loop body>                 # Return data, whatever.
 106937     **          Return     2                # Jump back to the Gosub
 106939     **       B: <after the loop>
 106942     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
 106943     SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
 106944     Index *pCov = 0;             /* Potential covering index (or NULL) */
 106945     int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
 106947     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
 106948     int regRowset = 0;                        /* Register for RowSet object */
 106949     int regRowid = 0;                         /* Register holding rowid */
 106950     int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
 106951     int iRetInit;                             /* Address of regReturn init */
 106952     int untestedTerms = 0;             /* Some terms not completely tested */
 106953     int ii;                            /* Loop counter */
 106954     Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
 106956     pTerm = pLevel->plan.u.pTerm;
 106957     assert( pTerm!=0 );
 106958     assert( pTerm->eOperator==WO_OR );
 106959     assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
 106960     pOrWc = &pTerm->u.pOrInfo->wc;
 106961     pLevel->op = OP_Return;
 106962     pLevel->p1 = regReturn;
 106964     /* Set up a new SrcList in pOrTab containing the table being scanned
 106965     ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
 106966     ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
 106968     if( pWInfo->nLevel>1 ){
 106969       int nNotReady;                 /* The number of notReady tables */
 106970       struct SrcList_item *origSrc;     /* Original list of tables */
 106971       nNotReady = pWInfo->nLevel - iLevel - 1;
 106972       pOrTab = sqlite3StackAllocRaw(pParse->db,
 106973                             sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
 106974       if( pOrTab==0 ) return notReady;
 106975       pOrTab->nAlloc = (i16)(nNotReady + 1);
 106976       pOrTab->nSrc = pOrTab->nAlloc;
 106977       memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
 106978       origSrc = pWInfo->pTabList->a;
 106979       for(k=1; k<=nNotReady; k++){
 106980         memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
 106982     }else{
 106983       pOrTab = pWInfo->pTabList;
 106986     /* Initialize the rowset register to contain NULL. An SQL NULL is 
 106987     ** equivalent to an empty rowset.
 106989     ** Also initialize regReturn to contain the address of the instruction 
 106990     ** immediately following the OP_Return at the bottom of the loop. This
 106991     ** is required in a few obscure LEFT JOIN cases where control jumps
 106992     ** over the top of the loop into the body of it. In this case the 
 106993     ** correct response for the end-of-loop code (the OP_Return) is to 
 106994     ** fall through to the next instruction, just as an OP_Next does if
 106995     ** called on an uninitialized cursor.
 106997     if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
 106998       regRowset = ++pParse->nMem;
 106999       regRowid = ++pParse->nMem;
 107000       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
 107002     iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
 107004     /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
 107005     ** Then for every term xN, evaluate as the subexpression: xN AND z
 107006     ** That way, terms in y that are factored into the disjunction will
 107007     ** be picked up by the recursive calls to sqlite3WhereBegin() below.
 107009     ** Actually, each subexpression is converted to "xN AND w" where w is
 107010     ** the "interesting" terms of z - terms that did not originate in the
 107011     ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
 107012     ** indices.
 107014     if( pWC->nTerm>1 ){
 107015       int iTerm;
 107016       for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
 107017         Expr *pExpr = pWC->a[iTerm].pExpr;
 107018         if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
 107019         if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue;
 107020         if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
 107021         pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
 107022         pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr);
 107024       if( pAndExpr ){
 107025         pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
 107029     for(ii=0; ii<pOrWc->nTerm; ii++){
 107030       WhereTerm *pOrTerm = &pOrWc->a[ii];
 107031       if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
 107032         WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
 107033         Expr *pOrExpr = pOrTerm->pExpr;
 107034         if( pAndExpr ){
 107035           pAndExpr->pLeft = pOrExpr;
 107036           pOrExpr = pAndExpr;
 107038         /* Loop through table entries that match term pOrTerm. */
 107039         pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
 107040                         WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
 107041                         WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
 107042         assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
 107043         if( pSubWInfo ){
 107044           WhereLevel *pLvl;
 107045           explainOneScan(
 107046               pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
 107048           if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
 107049             int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
 107050             int r;
 107051             r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
 107052                                          regRowid, 0);
 107053             sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
 107054                                  sqlite3VdbeCurrentAddr(v)+2, r, iSet);
 107056           sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
 107058           /* The pSubWInfo->untestedTerms flag means that this OR term
 107059           ** contained one or more AND term from a notReady table.  The
 107060           ** terms from the notReady table could not be tested and will
 107061           ** need to be tested later.
 107063           if( pSubWInfo->untestedTerms ) untestedTerms = 1;
 107065           /* If all of the OR-connected terms are optimized using the same
 107066           ** index, and the index is opened using the same cursor number
 107067           ** by each call to sqlite3WhereBegin() made by this loop, it may
 107068           ** be possible to use that index as a covering index.
 107070           ** If the call to sqlite3WhereBegin() above resulted in a scan that
 107071           ** uses an index, and this is either the first OR-connected term
 107072           ** processed or the index is the same as that used by all previous
 107073           ** terms, set pCov to the candidate covering index. Otherwise, set 
 107074           ** pCov to NULL to indicate that no candidate covering index will 
 107075           ** be available.
 107077           pLvl = &pSubWInfo->a[0];
 107078           if( (pLvl->plan.wsFlags & WHERE_INDEXED)!=0
 107079            && (pLvl->plan.wsFlags & WHERE_TEMP_INDEX)==0
 107080            && (ii==0 || pLvl->plan.u.pIdx==pCov)
 107082             assert( pLvl->iIdxCur==iCovCur );
 107083             pCov = pLvl->plan.u.pIdx;
 107084           }else{
 107085             pCov = 0;
 107088           /* Finish the loop through table entries that match term pOrTerm. */
 107089           sqlite3WhereEnd(pSubWInfo);
 107093     pLevel->u.pCovidx = pCov;
 107094     if( pCov ) pLevel->iIdxCur = iCovCur;
 107095     if( pAndExpr ){
 107096       pAndExpr->pLeft = 0;
 107097       sqlite3ExprDelete(pParse->db, pAndExpr);
 107099     sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
 107100     sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
 107101     sqlite3VdbeResolveLabel(v, iLoopBody);
 107103     if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
 107104     if( !untestedTerms ) disableTerm(pLevel, pTerm);
 107105   }else
 107106 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
 107109     /* Case 5:  There is no usable index.  We must do a complete
 107110     **          scan of the entire table.
 107112     static const u8 aStep[] = { OP_Next, OP_Prev };
 107113     static const u8 aStart[] = { OP_Rewind, OP_Last };
 107114     assert( bRev==0 || bRev==1 );
 107115     assert( omitTable==0 );
 107116     pLevel->op = aStep[bRev];
 107117     pLevel->p1 = iCur;
 107118     pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
 107119     pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
 107121   notReady &= ~getMask(pWC->pMaskSet, iCur);
 107123   /* Insert code to test every subexpression that can be completely
 107124   ** computed using the current set of tables.
 107126   ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
 107127   ** the use of indices become tests that are evaluated against each row of
 107128   ** the relevant input tables.
 107130   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
 107131     Expr *pE;
 107132     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
 107133     testcase( pTerm->wtFlags & TERM_CODED );
 107134     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
 107135     if( (pTerm->prereqAll & notReady)!=0 ){
 107136       testcase( pWInfo->untestedTerms==0
 107137                && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
 107138       pWInfo->untestedTerms = 1;
 107139       continue;
 107141     pE = pTerm->pExpr;
 107142     assert( pE!=0 );
 107143     if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
 107144       continue;
 107146     sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
 107147     pTerm->wtFlags |= TERM_CODED;
 107150   /* For a LEFT OUTER JOIN, generate code that will record the fact that
 107151   ** at least one row of the right table has matched the left table.  
 107153   if( pLevel->iLeftJoin ){
 107154     pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
 107155     sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
 107156     VdbeComment((v, "record LEFT JOIN hit"));
 107157     sqlite3ExprCacheClear(pParse);
 107158     for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
 107159       testcase( pTerm->wtFlags & TERM_VIRTUAL );  /* IMP: R-30575-11662 */
 107160       testcase( pTerm->wtFlags & TERM_CODED );
 107161       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
 107162       if( (pTerm->prereqAll & notReady)!=0 ){
 107163         assert( pWInfo->untestedTerms );
 107164         continue;
 107166       assert( pTerm->pExpr );
 107167       sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
 107168       pTerm->wtFlags |= TERM_CODED;
 107171   sqlite3ReleaseTempReg(pParse, iReleaseReg);
 107173   return notReady;
 107176 #if defined(SQLITE_TEST)
 107178 ** The following variable holds a text description of query plan generated
 107179 ** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
 107180 ** overwrites the previous.  This information is used for testing and
 107181 ** analysis only.
 107183 SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
 107184 static int nQPlan = 0;              /* Next free slow in _query_plan[] */
 107186 #endif /* SQLITE_TEST */
 107190 ** Free a WhereInfo structure
 107192 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
 107193   if( ALWAYS(pWInfo) ){
 107194     int i;
 107195     for(i=0; i<pWInfo->nLevel; i++){
 107196       sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
 107197       if( pInfo ){
 107198         /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
 107199         if( pInfo->needToFreeIdxStr ){
 107200           sqlite3_free(pInfo->idxStr);
 107202         sqlite3DbFree(db, pInfo);
 107204       if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
 107205         Index *pIdx = pWInfo->a[i].plan.u.pIdx;
 107206         if( pIdx ){
 107207           sqlite3DbFree(db, pIdx->zColAff);
 107208           sqlite3DbFree(db, pIdx);
 107212     whereClauseClear(pWInfo->pWC);
 107213     sqlite3DbFree(db, pWInfo);
 107219 ** Generate the beginning of the loop used for WHERE clause processing.
 107220 ** The return value is a pointer to an opaque structure that contains
 107221 ** information needed to terminate the loop.  Later, the calling routine
 107222 ** should invoke sqlite3WhereEnd() with the return value of this function
 107223 ** in order to complete the WHERE clause processing.
 107225 ** If an error occurs, this routine returns NULL.
 107227 ** The basic idea is to do a nested loop, one loop for each table in
 107228 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
 107229 ** same as a SELECT with only a single table in the FROM clause.)  For
 107230 ** example, if the SQL is this:
 107232 **       SELECT * FROM t1, t2, t3 WHERE ...;
 107234 ** Then the code generated is conceptually like the following:
 107236 **      foreach row1 in t1 do       \    Code generated
 107237 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
 107238 **          foreach row3 in t3 do   /
 107239 **            ...
 107240 **          end                     \    Code generated
 107241 **        end                        |-- by sqlite3WhereEnd()
 107242 **      end                         /
 107244 ** Note that the loops might not be nested in the order in which they
 107245 ** appear in the FROM clause if a different order is better able to make
 107246 ** use of indices.  Note also that when the IN operator appears in
 107247 ** the WHERE clause, it might result in additional nested loops for
 107248 ** scanning through all values on the right-hand side of the IN.
 107250 ** There are Btree cursors associated with each table.  t1 uses cursor
 107251 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
 107252 ** And so forth.  This routine generates code to open those VDBE cursors
 107253 ** and sqlite3WhereEnd() generates the code to close them.
 107255 ** The code that sqlite3WhereBegin() generates leaves the cursors named
 107256 ** in pTabList pointing at their appropriate entries.  The [...] code
 107257 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
 107258 ** data from the various tables of the loop.
 107260 ** If the WHERE clause is empty, the foreach loops must each scan their
 107261 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
 107262 ** the tables have indices and there are terms in the WHERE clause that
 107263 ** refer to those indices, a complete table scan can be avoided and the
 107264 ** code will run much faster.  Most of the work of this routine is checking
 107265 ** to see if there are indices that can be used to speed up the loop.
 107267 ** Terms of the WHERE clause are also used to limit which rows actually
 107268 ** make it to the "..." in the middle of the loop.  After each "foreach",
 107269 ** terms of the WHERE clause that use only terms in that loop and outer
 107270 ** loops are evaluated and if false a jump is made around all subsequent
 107271 ** inner loops (or around the "..." if the test occurs within the inner-
 107272 ** most loop)
 107274 ** OUTER JOINS
 107276 ** An outer join of tables t1 and t2 is conceptally coded as follows:
 107278 **    foreach row1 in t1 do
 107279 **      flag = 0
 107280 **      foreach row2 in t2 do
 107281 **        start:
 107282 **          ...
 107283 **          flag = 1
 107284 **      end
 107285 **      if flag==0 then
 107286 **        move the row2 cursor to a null row
 107287 **        goto start
 107288 **      fi
 107289 **    end
 107291 ** ORDER BY CLAUSE PROCESSING
 107293 ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
 107294 ** if there is one.  If there is no ORDER BY clause or if this routine
 107295 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
 107297 ** If an index can be used so that the natural output order of the table
 107298 ** scan is correct for the ORDER BY clause, then that index is used and
 107299 ** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr.  This
 107300 ** is an optimization that prevents an unnecessary sort of the result set
 107301 ** if an index appropriate for the ORDER BY clause already exists.
 107303 ** If the where clause loops cannot be arranged to provide the correct
 107304 ** output order, then WhereInfo.nOBSat is 0.
 107306 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
 107307   Parse *pParse,        /* The parser context */
 107308   SrcList *pTabList,    /* A list of all tables to be scanned */
 107309   Expr *pWhere,         /* The WHERE clause */
 107310   ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
 107311   ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
 107312   u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
 107313   int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
 107315   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
 107316   int nTabList;              /* Number of elements in pTabList */
 107317   WhereInfo *pWInfo;         /* Will become the return value of this function */
 107318   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
 107319   Bitmask notReady;          /* Cursors that are not yet positioned */
 107320   WhereBestIdx sWBI;         /* Best index search context */
 107321   WhereMaskSet *pMaskSet;    /* The expression mask set */
 107322   WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
 107323   int iFrom;                 /* First unused FROM clause element */
 107324   int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
 107325   int ii;                    /* Loop counter */
 107326   sqlite3 *db;               /* Database connection */
 107329   /* Variable initialization */
 107330   memset(&sWBI, 0, sizeof(sWBI));
 107331   sWBI.pParse = pParse;
 107333   /* The number of tables in the FROM clause is limited by the number of
 107334   ** bits in a Bitmask 
 107336   testcase( pTabList->nSrc==BMS );
 107337   if( pTabList->nSrc>BMS ){
 107338     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
 107339     return 0;
 107342   /* This function normally generates a nested loop for all tables in 
 107343   ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
 107344   ** only generate code for the first table in pTabList and assume that
 107345   ** any cursors associated with subsequent tables are uninitialized.
 107347   nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
 107349   /* Allocate and initialize the WhereInfo structure that will become the
 107350   ** return value. A single allocation is used to store the WhereInfo
 107351   ** struct, the contents of WhereInfo.a[], the WhereClause structure
 107352   ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
 107353   ** field (type Bitmask) it must be aligned on an 8-byte boundary on
 107354   ** some architectures. Hence the ROUND8() below.
 107356   db = pParse->db;
 107357   nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
 107358   pWInfo = sqlite3DbMallocZero(db, 
 107359       nByteWInfo + 
 107360       sizeof(WhereClause) +
 107361       sizeof(WhereMaskSet)
 107363   if( db->mallocFailed ){
 107364     sqlite3DbFree(db, pWInfo);
 107365     pWInfo = 0;
 107366     goto whereBeginError;
 107368   pWInfo->nLevel = nTabList;
 107369   pWInfo->pParse = pParse;
 107370   pWInfo->pTabList = pTabList;
 107371   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
 107372   pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
 107373   pWInfo->wctrlFlags = wctrlFlags;
 107374   pWInfo->savedNQueryLoop = pParse->nQueryLoop;
 107375   pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
 107376   sWBI.aLevel = pWInfo->a;
 107378   /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
 107379   ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
 107380   if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
 107382   /* Split the WHERE clause into separate subexpressions where each
 107383   ** subexpression is separated by an AND operator.
 107385   initMaskSet(pMaskSet);
 107386   whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
 107387   sqlite3ExprCodeConstants(pParse, pWhere);
 107388   whereSplit(sWBI.pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
 107390   /* Special case: a WHERE clause that is constant.  Evaluate the
 107391   ** expression and either jump over all of the code or fall thru.
 107393   if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
 107394     sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
 107395     pWhere = 0;
 107398   /* Assign a bit from the bitmask to every term in the FROM clause.
 107400   ** When assigning bitmask values to FROM clause cursors, it must be
 107401   ** the case that if X is the bitmask for the N-th FROM clause term then
 107402   ** the bitmask for all FROM clause terms to the left of the N-th term
 107403   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
 107404   ** its Expr.iRightJoinTable value to find the bitmask of the right table
 107405   ** of the join.  Subtracting one from the right table bitmask gives a
 107406   ** bitmask for all tables to the left of the join.  Knowing the bitmask
 107407   ** for all tables to the left of a left join is important.  Ticket #3015.
 107409   ** Configure the WhereClause.vmask variable so that bits that correspond
 107410   ** to virtual table cursors are set. This is used to selectively disable 
 107411   ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful 
 107412   ** with virtual tables.
 107414   ** Note that bitmasks are created for all pTabList->nSrc tables in
 107415   ** pTabList, not just the first nTabList tables.  nTabList is normally
 107416   ** equal to pTabList->nSrc but might be shortened to 1 if the
 107417   ** WHERE_ONETABLE_ONLY flag is set.
 107419   assert( sWBI.pWC->vmask==0 && pMaskSet->n==0 );
 107420   for(ii=0; ii<pTabList->nSrc; ii++){
 107421     createMask(pMaskSet, pTabList->a[ii].iCursor);
 107422 #ifndef SQLITE_OMIT_VIRTUALTABLE
 107423     if( ALWAYS(pTabList->a[ii].pTab) && IsVirtual(pTabList->a[ii].pTab) ){
 107424       sWBI.pWC->vmask |= ((Bitmask)1 << ii);
 107426 #endif
 107428 #ifndef NDEBUG
 107430     Bitmask toTheLeft = 0;
 107431     for(ii=0; ii<pTabList->nSrc; ii++){
 107432       Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
 107433       assert( (m-1)==toTheLeft );
 107434       toTheLeft |= m;
 107437 #endif
 107439   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
 107440   ** add new virtual terms onto the end of the WHERE clause.  We do not
 107441   ** want to analyze these virtual terms, so start analyzing at the end
 107442   ** and work forward so that the added virtual terms are never processed.
 107444   exprAnalyzeAll(pTabList, sWBI.pWC);
 107445   if( db->mallocFailed ){
 107446     goto whereBeginError;
 107449   /* Check if the DISTINCT qualifier, if there is one, is redundant. 
 107450   ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
 107451   ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
 107453   if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
 107454     pDistinct = 0;
 107455     pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
 107458   /* Chose the best index to use for each table in the FROM clause.
 107460   ** This loop fills in the following fields:
 107462   **   pWInfo->a[].pIdx      The index to use for this level of the loop.
 107463   **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
 107464   **   pWInfo->a[].nEq       The number of == and IN constraints
 107465   **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
 107466   **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
 107467   **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
 107468   **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
 107470   ** This loop also figures out the nesting order of tables in the FROM
 107471   ** clause.
 107473   sWBI.notValid = ~(Bitmask)0;
 107474   sWBI.pOrderBy = pOrderBy;
 107475   sWBI.n = nTabList;
 107476   sWBI.pDistinct = pDistinct;
 107477   andFlags = ~0;
 107478   WHERETRACE(("*** Optimizer Start ***\n"));
 107479   for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
 107480     WhereCost bestPlan;         /* Most efficient plan seen so far */
 107481     Index *pIdx;                /* Index for FROM table at pTabItem */
 107482     int j;                      /* For looping over FROM tables */
 107483     int bestJ = -1;             /* The value of j */
 107484     Bitmask m;                  /* Bitmask value for j or bestJ */
 107485     int isOptimal;              /* Iterator for optimal/non-optimal search */
 107486     int nUnconstrained;         /* Number tables without INDEXED BY */
 107487     Bitmask notIndexed;         /* Mask of tables that cannot use an index */
 107489     memset(&bestPlan, 0, sizeof(bestPlan));
 107490     bestPlan.rCost = SQLITE_BIG_DBL;
 107491     WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
 107493     /* Loop through the remaining entries in the FROM clause to find the
 107494     ** next nested loop. The loop tests all FROM clause entries
 107495     ** either once or twice. 
 107497     ** The first test is always performed if there are two or more entries
 107498     ** remaining and never performed if there is only one FROM clause entry
 107499     ** to choose from.  The first test looks for an "optimal" scan.  In
 107500     ** this context an optimal scan is one that uses the same strategy
 107501     ** for the given FROM clause entry as would be selected if the entry
 107502     ** were used as the innermost nested loop.  In other words, a table
 107503     ** is chosen such that the cost of running that table cannot be reduced
 107504     ** by waiting for other tables to run first.  This "optimal" test works
 107505     ** by first assuming that the FROM clause is on the inner loop and finding
 107506     ** its query plan, then checking to see if that query plan uses any
 107507     ** other FROM clause terms that are sWBI.notValid.  If no notValid terms
 107508     ** are used then the "optimal" query plan works.
 107510     ** Note that the WhereCost.nRow parameter for an optimal scan might
 107511     ** not be as small as it would be if the table really were the innermost
 107512     ** join.  The nRow value can be reduced by WHERE clause constraints
 107513     ** that do not use indices.  But this nRow reduction only happens if the
 107514     ** table really is the innermost join.  
 107516     ** The second loop iteration is only performed if no optimal scan
 107517     ** strategies were found by the first iteration. This second iteration
 107518     ** is used to search for the lowest cost scan overall.
 107520     ** Previous versions of SQLite performed only the second iteration -
 107521     ** the next outermost loop was always that with the lowest overall
 107522     ** cost. However, this meant that SQLite could select the wrong plan
 107523     ** for scripts such as the following:
 107525     **   CREATE TABLE t1(a, b); 
 107526     **   CREATE TABLE t2(c, d);
 107527     **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
 107529     ** The best strategy is to iterate through table t1 first. However it
 107530     ** is not possible to determine this with a simple greedy algorithm.
 107531     ** Since the cost of a linear scan through table t2 is the same 
 107532     ** as the cost of a linear scan through table t1, a simple greedy 
 107533     ** algorithm may choose to use t2 for the outer loop, which is a much
 107534     ** costlier approach.
 107536     nUnconstrained = 0;
 107537     notIndexed = 0;
 107538     for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
 107539       for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
 107540         int doNotReorder;    /* True if this table should not be reordered */
 107542         doNotReorder =  (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0;
 107543         if( j!=iFrom && doNotReorder ) break;
 107544         m = getMask(pMaskSet, sWBI.pSrc->iCursor);
 107545         if( (m & sWBI.notValid)==0 ){
 107546           if( j==iFrom ) iFrom++;
 107547           continue;
 107549         sWBI.notReady = (isOptimal ? m : sWBI.notValid);
 107550         if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
 107552         WHERETRACE(("   === trying table %d (%s) with isOptimal=%d ===\n",
 107553                     j, sWBI.pSrc->pTab->zName, isOptimal));
 107554         assert( sWBI.pSrc->pTab );
 107555 #ifndef SQLITE_OMIT_VIRTUALTABLE
 107556         if( IsVirtual(sWBI.pSrc->pTab) ){
 107557           sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
 107558           bestVirtualIndex(&sWBI);
 107559         }else 
 107560 #endif
 107562           bestBtreeIndex(&sWBI);
 107564         assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
 107566         /* If an INDEXED BY clause is present, then the plan must use that
 107567         ** index if it uses any index at all */
 107568         assert( sWBI.pSrc->pIndex==0 
 107569                   || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
 107570                   || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
 107572         if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
 107573           notIndexed |= m;
 107575         if( isOptimal ){
 107576           pWInfo->a[j].rOptCost = sWBI.cost.rCost;
 107577         }else if( iFrom<nTabList-1 ){
 107578           /* If two or more tables have nearly the same outer loop cost,
 107579           ** very different inner loop (optimal) cost, we want to choose
 107580           ** for the outer loop that table which benefits the least from
 107581           ** being in the inner loop.  The following code scales the 
 107582           ** outer loop cost estimate to accomplish that. */
 107583           WHERETRACE(("   scaling cost from %.1f to %.1f\n",
 107584                       sWBI.cost.rCost,
 107585                       sWBI.cost.rCost/pWInfo->a[j].rOptCost));
 107586           sWBI.cost.rCost /= pWInfo->a[j].rOptCost;
 107589         /* Conditions under which this table becomes the best so far:
 107591         **   (1) The table must not depend on other tables that have not
 107592         **       yet run.  (In other words, it must not depend on tables
 107593         **       in inner loops.)
 107595         **   (2) (This rule was removed on 2012-11-09.  The scaling of the
 107596         **       cost using the optimal scan cost made this rule obsolete.)
 107598         **   (3) All tables have an INDEXED BY clause or this table lacks an
 107599         **       INDEXED BY clause or this table uses the specific
 107600         **       index specified by its INDEXED BY clause.  This rule ensures
 107601         **       that a best-so-far is always selected even if an impossible
 107602         **       combination of INDEXED BY clauses are given.  The error
 107603         **       will be detected and relayed back to the application later.
 107604         **       The NEVER() comes about because rule (2) above prevents
 107605         **       An indexable full-table-scan from reaching rule (3).
 107607         **   (4) The plan cost must be lower than prior plans, where "cost"
 107608         **       is defined by the compareCost() function above. 
 107610         if( (sWBI.cost.used&sWBI.notValid)==0                    /* (1) */
 107611             && (nUnconstrained==0 || sWBI.pSrc->pIndex==0        /* (3) */
 107612                 || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
 107613             && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan))   /* (4) */
 107615           WHERETRACE(("   === table %d (%s) is best so far\n"
 107616                       "       cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
 107617                       j, sWBI.pSrc->pTab->zName,
 107618                       sWBI.cost.rCost, sWBI.cost.plan.nRow,
 107619                       sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
 107620           bestPlan = sWBI.cost;
 107621           bestJ = j;
 107623         if( doNotReorder ) break;
 107626     assert( bestJ>=0 );
 107627     assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
 107628     WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
 107629                 "    cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
 107630                 bestJ, pTabList->a[bestJ].pTab->zName,
 107631                 pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
 107632                 bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
 107633     if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
 107634       assert( pWInfo->eDistinct==0 );
 107635       pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
 107637     andFlags &= bestPlan.plan.wsFlags;
 107638     pLevel->plan = bestPlan.plan;
 107639     pLevel->iTabCur = pTabList->a[bestJ].iCursor;
 107640     testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
 107641     testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
 107642     if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
 107643       if( (wctrlFlags & WHERE_ONETABLE_ONLY) 
 107644        && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0 
 107646         pLevel->iIdxCur = iIdxCur;
 107647       }else{
 107648         pLevel->iIdxCur = pParse->nTab++;
 107650     }else{
 107651       pLevel->iIdxCur = -1;
 107653     sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
 107654     pLevel->iFrom = (u8)bestJ;
 107655     if( bestPlan.plan.nRow>=(double)1 ){
 107656       pParse->nQueryLoop *= bestPlan.plan.nRow;
 107659     /* Check that if the table scanned by this loop iteration had an
 107660     ** INDEXED BY clause attached to it, that the named index is being
 107661     ** used for the scan. If not, then query compilation has failed.
 107662     ** Return an error.
 107664     pIdx = pTabList->a[bestJ].pIndex;
 107665     if( pIdx ){
 107666       if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
 107667         sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
 107668         goto whereBeginError;
 107669       }else{
 107670         /* If an INDEXED BY clause is used, the bestIndex() function is
 107671         ** guaranteed to find the index specified in the INDEXED BY clause
 107672         ** if it find an index at all. */
 107673         assert( bestPlan.plan.u.pIdx==pIdx );
 107677   WHERETRACE(("*** Optimizer Finished ***\n"));
 107678   if( pParse->nErr || db->mallocFailed ){
 107679     goto whereBeginError;
 107681   if( nTabList ){
 107682     pLevel--;
 107683     pWInfo->nOBSat = pLevel->plan.nOBSat;
 107684   }else{
 107685     pWInfo->nOBSat = 0;
 107688   /* If the total query only selects a single row, then the ORDER BY
 107689   ** clause is irrelevant.
 107691   if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
 107692     assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
 107693     pWInfo->nOBSat = pOrderBy->nExpr;
 107696   /* If the caller is an UPDATE or DELETE statement that is requesting
 107697   ** to use a one-pass algorithm, determine if this is appropriate.
 107698   ** The one-pass algorithm only works if the WHERE clause constraints
 107699   ** the statement to update a single row.
 107701   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
 107702   if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
 107703     pWInfo->okOnePass = 1;
 107704     pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
 107707   /* Open all tables in the pTabList and any indices selected for
 107708   ** searching those tables.
 107710   sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
 107711   notReady = ~(Bitmask)0;
 107712   pWInfo->nRowOut = (double)1;
 107713   for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
 107714     Table *pTab;     /* Table to open */
 107715     int iDb;         /* Index of database containing table/index */
 107716     struct SrcList_item *pTabItem;
 107718     pTabItem = &pTabList->a[pLevel->iFrom];
 107719     pTab = pTabItem->pTab;
 107720     pWInfo->nRowOut *= pLevel->plan.nRow;
 107721     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 107722     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
 107723       /* Do nothing */
 107724     }else
 107725 #ifndef SQLITE_OMIT_VIRTUALTABLE
 107726     if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
 107727       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 107728       int iCur = pTabItem->iCursor;
 107729       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
 107730     }else if( IsVirtual(pTab) ){
 107731       /* noop */
 107732     }else
 107733 #endif
 107734     if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
 107735          && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
 107736       int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
 107737       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
 107738       testcase( pTab->nCol==BMS-1 );
 107739       testcase( pTab->nCol==BMS );
 107740       if( !pWInfo->okOnePass && pTab->nCol<BMS ){
 107741         Bitmask b = pTabItem->colUsed;
 107742         int n = 0;
 107743         for(; b; b=b>>1, n++){}
 107744         sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
 107745                             SQLITE_INT_TO_PTR(n), P4_INT32);
 107746         assert( n<=pTab->nCol );
 107748     }else{
 107749       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 107751 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 107752     if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
 107753       constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
 107754     }else
 107755 #endif
 107756     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
 107757       Index *pIx = pLevel->plan.u.pIdx;
 107758       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
 107759       int iIndexCur = pLevel->iIdxCur;
 107760       assert( pIx->pSchema==pTab->pSchema );
 107761       assert( iIndexCur>=0 );
 107762       sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
 107763                         (char*)pKey, P4_KEYINFO_HANDOFF);
 107764       VdbeComment((v, "%s", pIx->zName));
 107766     sqlite3CodeVerifySchema(pParse, iDb);
 107767     notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
 107769   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
 107770   if( db->mallocFailed ) goto whereBeginError;
 107772   /* Generate the code to do the search.  Each iteration of the for
 107773   ** loop below generates code for a single nested loop of the VM
 107774   ** program.
 107776   notReady = ~(Bitmask)0;
 107777   for(ii=0; ii<nTabList; ii++){
 107778     pLevel = &pWInfo->a[ii];
 107779     explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
 107780     notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
 107781     pWInfo->iContinue = pLevel->addrCont;
 107784 #ifdef SQLITE_TEST  /* For testing and debugging use only */
 107785   /* Record in the query plan information about the current table
 107786   ** and the index used to access it (if any).  If the table itself
 107787   ** is not used, its name is just '{}'.  If no index is used
 107788   ** the index is listed as "{}".  If the primary key is used the
 107789   ** index name is '*'.
 107791   for(ii=0; ii<nTabList; ii++){
 107792     char *z;
 107793     int n;
 107794     int w;
 107795     struct SrcList_item *pTabItem;
 107797     pLevel = &pWInfo->a[ii];
 107798     w = pLevel->plan.wsFlags;
 107799     pTabItem = &pTabList->a[pLevel->iFrom];
 107800     z = pTabItem->zAlias;
 107801     if( z==0 ) z = pTabItem->pTab->zName;
 107802     n = sqlite3Strlen30(z);
 107803     if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
 107804       if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
 107805         memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
 107806         nQPlan += 2;
 107807       }else{
 107808         memcpy(&sqlite3_query_plan[nQPlan], z, n);
 107809         nQPlan += n;
 107811       sqlite3_query_plan[nQPlan++] = ' ';
 107813     testcase( w & WHERE_ROWID_EQ );
 107814     testcase( w & WHERE_ROWID_RANGE );
 107815     if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
 107816       memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
 107817       nQPlan += 2;
 107818     }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
 107819       n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
 107820       if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
 107821         memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
 107822         nQPlan += n;
 107823         sqlite3_query_plan[nQPlan++] = ' ';
 107825     }else{
 107826       memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
 107827       nQPlan += 3;
 107830   while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
 107831     sqlite3_query_plan[--nQPlan] = 0;
 107833   sqlite3_query_plan[nQPlan] = 0;
 107834   nQPlan = 0;
 107835 #endif /* SQLITE_TEST // Testing and debugging use only */
 107837   /* Record the continuation address in the WhereInfo structure.  Then
 107838   ** clean up and return.
 107840   return pWInfo;
 107842   /* Jump here if malloc fails */
 107843 whereBeginError:
 107844   if( pWInfo ){
 107845     pParse->nQueryLoop = pWInfo->savedNQueryLoop;
 107846     whereInfoFree(db, pWInfo);
 107848   return 0;
 107852 ** Generate the end of the WHERE loop.  See comments on 
 107853 ** sqlite3WhereBegin() for additional information.
 107855 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
 107856   Parse *pParse = pWInfo->pParse;
 107857   Vdbe *v = pParse->pVdbe;
 107858   int i;
 107859   WhereLevel *pLevel;
 107860   SrcList *pTabList = pWInfo->pTabList;
 107861   sqlite3 *db = pParse->db;
 107863   /* Generate loop termination code.
 107865   sqlite3ExprCacheClear(pParse);
 107866   for(i=pWInfo->nLevel-1; i>=0; i--){
 107867     pLevel = &pWInfo->a[i];
 107868     sqlite3VdbeResolveLabel(v, pLevel->addrCont);
 107869     if( pLevel->op!=OP_Noop ){
 107870       sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
 107871       sqlite3VdbeChangeP5(v, pLevel->p5);
 107873     if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
 107874       struct InLoop *pIn;
 107875       int j;
 107876       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
 107877       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
 107878         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
 107879         sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
 107880         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
 107882       sqlite3DbFree(db, pLevel->u.in.aInLoop);
 107884     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
 107885     if( pLevel->iLeftJoin ){
 107886       int addr;
 107887       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
 107888       assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
 107889            || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
 107890       if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
 107891         sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
 107893       if( pLevel->iIdxCur>=0 ){
 107894         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
 107896       if( pLevel->op==OP_Return ){
 107897         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
 107898       }else{
 107899         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
 107901       sqlite3VdbeJumpHere(v, addr);
 107905   /* The "break" point is here, just past the end of the outer loop.
 107906   ** Set it.
 107908   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
 107910   /* Close all of the cursors that were opened by sqlite3WhereBegin.
 107912   assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
 107913   for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
 107914     Index *pIdx = 0;
 107915     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
 107916     Table *pTab = pTabItem->pTab;
 107917     assert( pTab!=0 );
 107918     if( (pTab->tabFlags & TF_Ephemeral)==0
 107919      && pTab->pSelect==0
 107920      && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
 107922       int ws = pLevel->plan.wsFlags;
 107923       if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
 107924         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
 107926       if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
 107927         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
 107931     /* If this scan uses an index, make code substitutions to read data
 107932     ** from the index in preference to the table. Sometimes, this means
 107933     ** the table need never be read from. This is a performance boost,
 107934     ** as the vdbe level waits until the table is read before actually
 107935     ** seeking the table cursor to the record corresponding to the current
 107936     ** position in the index.
 107938     ** Calls to the code generator in between sqlite3WhereBegin and
 107939     ** sqlite3WhereEnd will have created code that references the table
 107940     ** directly.  This loop scans all that code looking for opcodes
 107941     ** that reference the table and converts them into opcodes that
 107942     ** reference the index.
 107944     if( pLevel->plan.wsFlags & WHERE_INDEXED ){
 107945       pIdx = pLevel->plan.u.pIdx;
 107946     }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
 107947       pIdx = pLevel->u.pCovidx;
 107949     if( pIdx && !db->mallocFailed){
 107950       int k, j, last;
 107951       VdbeOp *pOp;
 107953       pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
 107954       last = sqlite3VdbeCurrentAddr(v);
 107955       for(k=pWInfo->iTop; k<last; k++, pOp++){
 107956         if( pOp->p1!=pLevel->iTabCur ) continue;
 107957         if( pOp->opcode==OP_Column ){
 107958           for(j=0; j<pIdx->nColumn; j++){
 107959             if( pOp->p2==pIdx->aiColumn[j] ){
 107960               pOp->p2 = j;
 107961               pOp->p1 = pLevel->iIdxCur;
 107962               break;
 107965           assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
 107966                || j<pIdx->nColumn );
 107967         }else if( pOp->opcode==OP_Rowid ){
 107968           pOp->p1 = pLevel->iIdxCur;
 107969           pOp->opcode = OP_IdxRowid;
 107975   /* Final cleanup
 107977   pParse->nQueryLoop = pWInfo->savedNQueryLoop;
 107978   whereInfoFree(db, pWInfo);
 107979   return;
 107982 /************** End of where.c ***********************************************/
 107983 /************** Begin file parse.c *******************************************/
 107984 /* Driver template for the LEMON parser generator.
 107985 ** The author disclaims copyright to this source code.
 107987 ** This version of "lempar.c" is modified, slightly, for use by SQLite.
 107988 ** The only modifications are the addition of a couple of NEVER()
 107989 ** macros to disable tests that are needed in the case of a general
 107990 ** LALR(1) grammar but which are always false in the
 107991 ** specific grammar used by SQLite.
 107993 /* First off, code is included that follows the "include" declaration
 107994 ** in the input grammar file. */
 107995 /* #include <stdio.h> */
 107999 ** Disable all error recovery processing in the parser push-down
 108000 ** automaton.
 108002 #define YYNOERRORRECOVERY 1
 108005 ** Make yytestcase() the same as testcase()
 108007 #define yytestcase(X) testcase(X)
 108010 ** An instance of this structure holds information about the
 108011 ** LIMIT clause of a SELECT statement.
 108013 struct LimitVal {
 108014   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
 108015   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
 108019 ** An instance of this structure is used to store the LIKE,
 108020 ** GLOB, NOT LIKE, and NOT GLOB operators.
 108022 struct LikeOp {
 108023   Token eOperator;  /* "like" or "glob" or "regexp" */
 108024   int bNot;         /* True if the NOT keyword is present */
 108028 ** An instance of the following structure describes the event of a
 108029 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
 108030 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
 108032 **      UPDATE ON (a,b,c)
 108034 ** Then the "b" IdList records the list "a,b,c".
 108036 struct TrigEvent { int a; IdList * b; };
 108039 ** An instance of this structure holds the ATTACH key and the key type.
 108041 struct AttachKey { int type;  Token key; };
 108044 ** One or more VALUES claues
 108046 struct ValueList {
 108047   ExprList *pList;
 108048   Select *pSelect;
 108052   /* This is a utility routine used to set the ExprSpan.zStart and
 108053   ** ExprSpan.zEnd values of pOut so that the span covers the complete
 108054   ** range of text beginning with pStart and going to the end of pEnd.
 108056   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
 108057     pOut->zStart = pStart->z;
 108058     pOut->zEnd = &pEnd->z[pEnd->n];
 108061   /* Construct a new Expr object from a single identifier.  Use the
 108062   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
 108063   ** that created the expression.
 108065   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
 108066     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
 108067     pOut->zStart = pValue->z;
 108068     pOut->zEnd = &pValue->z[pValue->n];
 108071   /* This routine constructs a binary expression node out of two ExprSpan
 108072   ** objects and uses the result to populate a new ExprSpan object.
 108074   static void spanBinaryExpr(
 108075     ExprSpan *pOut,     /* Write the result here */
 108076     Parse *pParse,      /* The parsing context.  Errors accumulate here */
 108077     int op,             /* The binary operation */
 108078     ExprSpan *pLeft,    /* The left operand */
 108079     ExprSpan *pRight    /* The right operand */
 108081     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
 108082     pOut->zStart = pLeft->zStart;
 108083     pOut->zEnd = pRight->zEnd;
 108086   /* Construct an expression node for a unary postfix operator
 108088   static void spanUnaryPostfix(
 108089     ExprSpan *pOut,        /* Write the new expression node here */
 108090     Parse *pParse,         /* Parsing context to record errors */
 108091     int op,                /* The operator */
 108092     ExprSpan *pOperand,    /* The operand */
 108093     Token *pPostOp         /* The operand token for setting the span */
 108095     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
 108096     pOut->zStart = pOperand->zStart;
 108097     pOut->zEnd = &pPostOp->z[pPostOp->n];
 108100   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
 108101   ** unary TK_ISNULL or TK_NOTNULL expression. */
 108102   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
 108103     sqlite3 *db = pParse->db;
 108104     if( db->mallocFailed==0 && pY->op==TK_NULL ){
 108105       pA->op = (u8)op;
 108106       sqlite3ExprDelete(db, pA->pRight);
 108107       pA->pRight = 0;
 108111   /* Construct an expression node for a unary prefix operator
 108113   static void spanUnaryPrefix(
 108114     ExprSpan *pOut,        /* Write the new expression node here */
 108115     Parse *pParse,         /* Parsing context to record errors */
 108116     int op,                /* The operator */
 108117     ExprSpan *pOperand,    /* The operand */
 108118     Token *pPreOp         /* The operand token for setting the span */
 108120     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
 108121     pOut->zStart = pPreOp->z;
 108122     pOut->zEnd = pOperand->zEnd;
 108124 /* Next is all token values, in a form suitable for use by makeheaders.
 108125 ** This section will be null unless lemon is run with the -m switch.
 108128 ** These constants (all generated automatically by the parser generator)
 108129 ** specify the various kinds of tokens (terminals) that the parser
 108130 ** understands. 
 108132 ** Each symbol here is a terminal symbol in the grammar.
 108134 /* Make sure the INTERFACE macro is defined.
 108136 #ifndef INTERFACE
 108137 # define INTERFACE 1
 108138 #endif
 108139 /* The next thing included is series of defines which control
 108140 ** various aspects of the generated parser.
 108141 **    YYCODETYPE         is the data type used for storing terminal
 108142 **                       and nonterminal numbers.  "unsigned char" is
 108143 **                       used if there are fewer than 250 terminals
 108144 **                       and nonterminals.  "int" is used otherwise.
 108145 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
 108146 **                       to no legal terminal or nonterminal number.  This
 108147 **                       number is used to fill in empty slots of the hash 
 108148 **                       table.
 108149 **    YYFALLBACK         If defined, this indicates that one or more tokens
 108150 **                       have fall-back values which should be used if the
 108151 **                       original value of the token will not parse.
 108152 **    YYACTIONTYPE       is the data type used for storing terminal
 108153 **                       and nonterminal numbers.  "unsigned char" is
 108154 **                       used if there are fewer than 250 rules and
 108155 **                       states combined.  "int" is used otherwise.
 108156 **    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given 
 108157 **                       directly to the parser from the tokenizer.
 108158 **    YYMINORTYPE        is the data type used for all minor tokens.
 108159 **                       This is typically a union of many types, one of
 108160 **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
 108161 **                       for base tokens is called "yy0".
 108162 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
 108163 **                       zero the stack is dynamically sized using realloc()
 108164 **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
 108165 **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
 108166 **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
 108167 **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
 108168 **    YYNSTATE           the combined number of states.
 108169 **    YYNRULE            the number of rules in the grammar
 108170 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
 108171 **                       defined, then do no error processing.
 108173 #define YYCODETYPE unsigned char
 108174 #define YYNOCODE 251
 108175 #define YYACTIONTYPE unsigned short int
 108176 #define YYWILDCARD 67
 108177 #define sqlite3ParserTOKENTYPE Token
 108178 typedef union {
 108179   int yyinit;
 108180   sqlite3ParserTOKENTYPE yy0;
 108181   struct LimitVal yy64;
 108182   Expr* yy122;
 108183   Select* yy159;
 108184   IdList* yy180;
 108185   struct {int value; int mask;} yy207;
 108186   u8 yy258;
 108187   struct LikeOp yy318;
 108188   TriggerStep* yy327;
 108189   ExprSpan yy342;
 108190   SrcList* yy347;
 108191   int yy392;
 108192   struct TrigEvent yy410;
 108193   ExprList* yy442;
 108194   struct ValueList yy487;
 108195 } YYMINORTYPE;
 108196 #ifndef YYSTACKDEPTH
 108197 #define YYSTACKDEPTH 100
 108198 #endif
 108199 #define sqlite3ParserARG_SDECL Parse *pParse;
 108200 #define sqlite3ParserARG_PDECL ,Parse *pParse
 108201 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
 108202 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
 108203 #define YYNSTATE 627
 108204 #define YYNRULE 327
 108205 #define YYFALLBACK 1
 108206 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
 108207 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
 108208 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
 108210 /* The yyzerominor constant is used to initialize instances of
 108211 ** YYMINORTYPE objects to zero. */
 108212 static const YYMINORTYPE yyzerominor = { 0 };
 108214 /* Define the yytestcase() macro to be a no-op if is not already defined
 108215 ** otherwise.
 108217 ** Applications can choose to define yytestcase() in the %include section
 108218 ** to a macro that can assist in verifying code coverage.  For production
 108219 ** code the yytestcase() macro should be turned off.  But it is useful
 108220 ** for testing.
 108222 #ifndef yytestcase
 108223 # define yytestcase(X)
 108224 #endif
 108227 /* Next are the tables used to determine what action to take based on the
 108228 ** current state and lookahead token.  These tables are used to implement
 108229 ** functions that take a state number and lookahead value and return an
 108230 ** action integer.  
 108232 ** Suppose the action integer is N.  Then the action is determined as
 108233 ** follows
 108235 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
 108236 **                                      token onto the stack and goto state N.
 108238 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
 108240 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
 108242 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
 108244 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
 108245 **                                      slots in the yy_action[] table.
 108247 ** The action table is constructed as a single large table named yy_action[].
 108248 ** Given state S and lookahead X, the action is computed as
 108250 **      yy_action[ yy_shift_ofst[S] + X ]
 108252 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
 108253 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
 108254 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
 108255 ** and that yy_default[S] should be used instead.  
 108257 ** The formula above is for computing the action when the lookahead is
 108258 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
 108259 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
 108260 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
 108261 ** YY_SHIFT_USE_DFLT.
 108263 ** The following are the tables generated in this section:
 108265 **  yy_action[]        A single table containing all actions.
 108266 **  yy_lookahead[]     A table containing the lookahead for each entry in
 108267 **                     yy_action.  Used to detect hash collisions.
 108268 **  yy_shift_ofst[]    For each state, the offset into yy_action for
 108269 **                     shifting terminals.
 108270 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
 108271 **                     shifting non-terminals after a reduce.
 108272 **  yy_default[]       Default action for each state.
 108274 #define YY_ACTTAB_COUNT (1564)
 108275 static const YYACTIONTYPE yy_action[] = {
 108276  /*     0 */   309,  955,  184,  417,    2,  171,  624,  594,   56,   56,
 108277  /*    10 */    56,   56,   49,   54,   54,   54,   54,   53,   53,   52,
 108278  /*    20 */    52,   52,   51,  233,  620,  619,  298,  620,  619,  234,
 108279  /*    30 */   587,  581,   56,   56,   56,   56,   19,   54,   54,   54,
 108280  /*    40 */    54,   53,   53,   52,   52,   52,   51,  233,  605,   57,
 108281  /*    50 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
 108282  /*    60 */    56,   56,  541,   54,   54,   54,   54,   53,   53,   52,
 108283  /*    70 */    52,   52,   51,  233,  309,  594,  325,  196,  195,  194,
 108284  /*    80 */    33,   54,   54,   54,   54,   53,   53,   52,   52,   52,
 108285  /*    90 */    51,  233,  617,  616,  165,  617,  616,  380,  377,  376,
 108286  /*   100 */   407,  532,  576,  576,  587,  581,  303,  422,  375,   59,
 108287  /*   110 */    53,   53,   52,   52,   52,   51,  233,   50,   47,  146,
 108288  /*   120 */   574,  545,   65,   57,   58,   48,  579,  578,  580,  580,
 108289  /*   130 */    55,   55,   56,   56,   56,   56,  213,   54,   54,   54,
 108290  /*   140 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  223,
 108291  /*   150 */   539,  420,  170,  176,  138,  280,  383,  275,  382,  168,
 108292  /*   160 */   489,  551,  409,  668,  620,  619,  271,  438,  409,  438,
 108293  /*   170 */   550,  604,   67,  482,  507,  618,  599,  412,  587,  581,
 108294  /*   180 */   600,  483,  618,  412,  618,  598,   91,  439,  440,  439,
 108295  /*   190 */   335,  598,   73,  669,  222,  266,  480,   57,   58,   48,
 108296  /*   200 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
 108297  /*   210 */   670,   54,   54,   54,   54,   53,   53,   52,   52,   52,
 108298  /*   220 */    51,  233,  309,  279,  232,  231,    1,  132,  200,  385,
 108299  /*   230 */   620,  619,  617,  616,  278,  435,  289,  563,  175,  262,
 108300  /*   240 */   409,  264,  437,  497,  436,  166,  441,  568,  336,  568,
 108301  /*   250 */   201,  537,  587,  581,  599,  412,  165,  594,  600,  380,
 108302  /*   260 */   377,  376,  597,  598,   92,  523,  618,  569,  569,  592,
 108303  /*   270 */   375,   57,   58,   48,  579,  578,  580,  580,   55,   55,
 108304  /*   280 */    56,   56,   56,   56,  597,   54,   54,   54,   54,   53,
 108305  /*   290 */    53,   52,   52,   52,   51,  233,  309,  463,  617,  616,
 108306  /*   300 */   590,  590,  590,  174,  272,  396,  409,  272,  409,  548,
 108307  /*   310 */   397,  620,  619,   68,  326,  620,  619,  620,  619,  618,
 108308  /*   320 */   546,  412,  618,  412,  471,  594,  587,  581,  472,  598,
 108309  /*   330 */    92,  598,   92,   52,   52,   52,   51,  233,  513,  512,
 108310  /*   340 */   206,  322,  363,  464,  221,   57,   58,   48,  579,  578,
 108311  /*   350 */   580,  580,   55,   55,   56,   56,   56,   56,  529,   54,
 108312  /*   360 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
 108313  /*   370 */   309,  396,  409,  396,  597,  372,  386,  530,  347,  617,
 108314  /*   380 */   616,  575,  202,  617,  616,  617,  616,  412,  620,  619,
 108315  /*   390 */   145,  255,  346,  254,  577,  598,   74,  351,   45,  489,
 108316  /*   400 */   587,  581,  235,  189,  464,  544,  167,  296,  187,  469,
 108317  /*   410 */   479,   67,   62,   39,  618,  546,  597,  345,  573,   57,
 108318  /*   420 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
 108319  /*   430 */    56,   56,    6,   54,   54,   54,   54,   53,   53,   52,
 108320  /*   440 */    52,   52,   51,  233,  309,  562,  558,  407,  528,  576,
 108321  /*   450 */   576,  344,  255,  346,  254,  182,  617,  616,  503,  504,
 108322  /*   460 */   314,  409,  557,  235,  166,  271,  409,  352,  564,  181,
 108323  /*   470 */   407,  546,  576,  576,  587,  581,  412,  537,  556,  561,
 108324  /*   480 */   517,  412,  618,  249,  598,   16,    7,   36,  467,  598,
 108325  /*   490 */    92,  516,  618,   57,   58,   48,  579,  578,  580,  580,
 108326  /*   500 */    55,   55,   56,   56,   56,   56,  541,   54,   54,   54,
 108327  /*   510 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  327,
 108328  /*   520 */   572,  571,  525,  558,  560,  394,  871,  246,  409,  248,
 108329  /*   530 */   171,  392,  594,  219,  407,  409,  576,  576,  502,  557,
 108330  /*   540 */   364,  145,  510,  412,  407,  229,  576,  576,  587,  581,
 108331  /*   550 */   412,  598,   92,  381,  269,  556,  166,  400,  598,   69,
 108332  /*   560 */   501,  419,  945,  199,  945,  198,  546,   57,   58,   48,
 108333  /*   570 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
 108334  /*   580 */   568,   54,   54,   54,   54,   53,   53,   52,   52,   52,
 108335  /*   590 */    51,  233,  309,  317,  419,  944,  508,  944,  308,  597,
 108336  /*   600 */   594,  565,  490,  212,  173,  247,  423,  615,  614,  613,
 108337  /*   610 */   323,  197,  143,  405,  572,  571,  489,   66,   50,   47,
 108338  /*   620 */   146,  594,  587,  581,  232,  231,  559,  427,   67,  555,
 108339  /*   630 */    15,  618,  186,  543,  303,  421,   35,  206,  432,  423,
 108340  /*   640 */   552,   57,   58,   48,  579,  578,  580,  580,   55,   55,
 108341  /*   650 */    56,   56,   56,   56,  205,   54,   54,   54,   54,   53,
 108342  /*   660 */    53,   52,   52,   52,   51,  233,  309,  569,  569,  260,
 108343  /*   670 */   268,  597,   12,  373,  568,  166,  409,  313,  409,  420,
 108344  /*   680 */   409,  473,  473,  365,  618,   50,   47,  146,  597,  594,
 108345  /*   690 */   468,  412,  166,  412,  351,  412,  587,  581,   32,  598,
 108346  /*   700 */    94,  598,   97,  598,   95,  627,  625,  329,  142,   50,
 108347  /*   710 */    47,  146,  333,  349,  358,   57,   58,   48,  579,  578,
 108348  /*   720 */   580,  580,   55,   55,   56,   56,   56,   56,  409,   54,
 108349  /*   730 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
 108350  /*   740 */   309,  409,  388,  412,  409,   22,  565,  404,  212,  362,
 108351  /*   750 */   389,  598,  104,  359,  409,  156,  412,  409,  603,  412,
 108352  /*   760 */   537,  331,  569,  569,  598,  103,  493,  598,  105,  412,
 108353  /*   770 */   587,  581,  412,  260,  549,  618,   11,  598,  106,  521,
 108354  /*   780 */   598,  133,  169,  457,  456,  170,   35,  601,  618,   57,
 108355  /*   790 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
 108356  /*   800 */    56,   56,  409,   54,   54,   54,   54,   53,   53,   52,
 108357  /*   810 */    52,   52,   51,  233,  309,  409,  259,  412,  409,   50,
 108358  /*   820 */    47,  146,  357,  318,  355,  598,  134,  527,  352,  337,
 108359  /*   830 */   412,  409,  356,  412,  357,  409,  357,  618,  598,   98,
 108360  /*   840 */   129,  598,  102,  618,  587,  581,  412,   21,  235,  618,
 108361  /*   850 */   412,  618,  211,  143,  598,  101,   30,  167,  598,   93,
 108362  /*   860 */   350,  535,  203,   57,   58,   48,  579,  578,  580,  580,
 108363  /*   870 */    55,   55,   56,   56,   56,   56,  409,   54,   54,   54,
 108364  /*   880 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  409,
 108365  /*   890 */   526,  412,  409,  425,  215,  305,  597,  551,  141,  598,
 108366  /*   900 */   100,   40,  409,   38,  412,  409,  550,  412,  409,  228,
 108367  /*   910 */   220,  314,  598,   77,  500,  598,   96,  412,  587,  581,
 108368  /*   920 */   412,  338,  253,  412,  218,  598,  137,  379,  598,  136,
 108369  /*   930 */    28,  598,  135,  270,  715,  210,  481,   57,   58,   48,
 108370  /*   940 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
 108371  /*   950 */   409,   54,   54,   54,   54,   53,   53,   52,   52,   52,
 108372  /*   960 */    51,  233,  309,  409,  272,  412,  409,  315,  147,  597,
 108373  /*   970 */   272,  626,    2,  598,   76,  209,  409,  127,  412,  618,
 108374  /*   980 */   126,  412,  409,  621,  235,  618,  598,   90,  374,  598,
 108375  /*   990 */    89,  412,  587,  581,   27,  260,  350,  412,  618,  598,
 108376  /*  1000 */    75,  321,  541,  541,  125,  598,   88,  320,  278,  597,
 108377  /*  1010 */   618,   57,   46,   48,  579,  578,  580,  580,   55,   55,
 108378  /*  1020 */    56,   56,   56,   56,  409,   54,   54,   54,   54,   53,
 108379  /*  1030 */    53,   52,   52,   52,   51,  233,  309,  409,  450,  412,
 108380  /*  1040 */   164,  284,  282,  272,  609,  424,  304,  598,   87,  370,
 108381  /*  1050 */   409,  477,  412,  409,  608,  409,  607,  602,  618,  618,
 108382  /*  1060 */   598,   99,  586,  585,  122,  412,  587,  581,  412,  618,
 108383  /*  1070 */   412,  618,  618,  598,   86,  366,  598,   17,  598,   85,
 108384  /*  1080 */   319,  185,  519,  518,  583,  582,   58,   48,  579,  578,
 108385  /*  1090 */   580,  580,   55,   55,   56,   56,   56,   56,  409,   54,
 108386  /*  1100 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
 108387  /*  1110 */   309,  584,  409,  412,  409,  260,  260,  260,  408,  591,
 108388  /*  1120 */   474,  598,   84,  170,  409,  466,  518,  412,  121,  412,
 108389  /*  1130 */   618,  618,  618,  618,  618,  598,   83,  598,   72,  412,
 108390  /*  1140 */   587,  581,   51,  233,  625,  329,  470,  598,   71,  257,
 108391  /*  1150 */   159,  120,   14,  462,  157,  158,  117,  260,  448,  447,
 108392  /*  1160 */   446,   48,  579,  578,  580,  580,   55,   55,   56,   56,
 108393  /*  1170 */    56,   56,  618,   54,   54,   54,   54,   53,   53,   52,
 108394  /*  1180 */    52,   52,   51,  233,   44,  403,  260,    3,  409,  459,
 108395  /*  1190 */   260,  413,  619,  118,  398,   10,   25,   24,  554,  348,
 108396  /*  1200 */   217,  618,  406,  412,  409,  618,    4,   44,  403,  618,
 108397  /*  1210 */     3,  598,   82,  618,  413,  619,  455,  542,  115,  412,
 108398  /*  1220 */   538,  401,  536,  274,  506,  406,  251,  598,   81,  216,
 108399  /*  1230 */   273,  563,  618,  243,  453,  618,  154,  618,  618,  618,
 108400  /*  1240 */   449,  416,  623,  110,  401,  618,  409,  236,   64,  123,
 108401  /*  1250 */   487,   41,   42,  531,  563,  204,  409,  267,   43,  411,
 108402  /*  1260 */   410,  412,  265,  592,  108,  618,  107,  434,  332,  598,
 108403  /*  1270 */    80,  412,  618,  263,   41,   42,  443,  618,  409,  598,
 108404  /*  1280 */    70,   43,  411,  410,  433,  261,  592,  149,  618,  597,
 108405  /*  1290 */   256,  237,  188,  412,  590,  590,  590,  589,  588,   13,
 108406  /*  1300 */   618,  598,   18,  328,  235,  618,   44,  403,  360,    3,
 108407  /*  1310 */   418,  461,  339,  413,  619,  227,  124,  590,  590,  590,
 108408  /*  1320 */   589,  588,   13,  618,  406,  409,  618,  409,  139,   34,
 108409  /*  1330 */   403,  387,    3,  148,  622,  312,  413,  619,  311,  330,
 108410  /*  1340 */   412,  460,  412,  401,  180,  353,  412,  406,  598,   79,
 108411  /*  1350 */   598,   78,  250,  563,  598,    9,  618,  612,  611,  610,
 108412  /*  1360 */   618,    8,  452,  442,  242,  415,  401,  618,  239,  235,
 108413  /*  1370 */   179,  238,  428,   41,   42,  288,  563,  618,  618,  618,
 108414  /*  1380 */    43,  411,  410,  618,  144,  592,  618,  618,  177,   61,
 108415  /*  1390 */   618,  596,  391,  620,  619,  287,   41,   42,  414,  618,
 108416  /*  1400 */   293,   30,  393,   43,  411,  410,  292,  618,  592,   31,
 108417  /*  1410 */   618,  395,  291,   60,  230,   37,  590,  590,  590,  589,
 108418  /*  1420 */   588,   13,  214,  553,  183,  290,  172,  301,  300,  299,
 108419  /*  1430 */   178,  297,  595,  563,  451,   29,  285,  390,  540,  590,
 108420  /*  1440 */   590,  590,  589,  588,   13,  283,  520,  534,  150,  533,
 108421  /*  1450 */   241,  281,  384,  192,  191,  324,  515,  514,  276,  240,
 108422  /*  1460 */   510,  523,  307,  511,  128,  592,  509,  225,  226,  486,
 108423  /*  1470 */   485,  224,  152,  491,  464,  306,  484,  163,  153,  371,
 108424  /*  1480 */   478,  151,  162,  258,  369,  161,  367,  208,  475,  476,
 108425  /*  1490 */    26,  160,  465,  140,  361,  131,  590,  590,  590,  116,
 108426  /*  1500 */   119,  454,  343,  155,  114,  342,  113,  112,  445,  111,
 108427  /*  1510 */   130,  109,  431,  316,  426,  430,   23,  429,   20,  606,
 108428  /*  1520 */   190,  507,  255,  341,  244,   63,  294,  593,  310,  570,
 108429  /*  1530 */   277,  402,  354,  235,  567,  496,  495,  492,  494,  302,
 108430  /*  1540 */   458,  378,  286,  245,  566,    5,  252,  547,  193,  444,
 108431  /*  1550 */   233,  340,  207,  524,  368,  505,  334,  522,  499,  399,
 108432  /*  1560 */   295,  498,  956,  488,
 108434 static const YYCODETYPE yy_lookahead[] = {
 108435  /*     0 */    19,  142,  143,  144,  145,   24,    1,   26,   77,   78,
 108436  /*    10 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
 108437  /*    20 */    89,   90,   91,   92,   26,   27,   15,   26,   27,  197,
 108438  /*    30 */    49,   50,   77,   78,   79,   80,  204,   82,   83,   84,
 108439  /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   23,   68,
 108440  /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
 108441  /*    60 */    79,   80,  166,   82,   83,   84,   85,   86,   87,   88,
 108442  /*    70 */    89,   90,   91,   92,   19,   94,   19,  105,  106,  107,
 108443  /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 108444  /*    90 */    91,   92,   94,   95,   96,   94,   95,   99,  100,  101,
 108445  /*   100 */   112,  205,  114,  115,   49,   50,   22,   23,  110,   54,
 108446  /*   110 */    86,   87,   88,   89,   90,   91,   92,  221,  222,  223,
 108447  /*   120 */    23,  120,   25,   68,   69,   70,   71,   72,   73,   74,
 108448  /*   130 */    75,   76,   77,   78,   79,   80,   22,   82,   83,   84,
 108449  /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   92,
 108450  /*   150 */    23,   67,   25,   96,   97,   98,   99,  100,  101,  102,
 108451  /*   160 */   150,   32,  150,  118,   26,   27,  109,  150,  150,  150,
 108452  /*   170 */    41,  161,  162,  180,  181,  165,  113,  165,   49,   50,
 108453  /*   180 */   117,  188,  165,  165,  165,  173,  174,  170,  171,  170,
 108454  /*   190 */   171,  173,  174,  118,  184,   16,  186,   68,   69,   70,
 108455  /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
 108456  /*   210 */   118,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 108457  /*   220 */    91,   92,   19,   98,   86,   87,   22,   24,  160,   88,
 108458  /*   230 */    26,   27,   94,   95,  109,   97,  224,   66,  118,   60,
 108459  /*   240 */   150,   62,  104,   23,  106,   25,  229,  230,  229,  230,
 108460  /*   250 */   160,  150,   49,   50,  113,  165,   96,   26,  117,   99,
 108461  /*   260 */   100,  101,  194,  173,  174,   94,  165,  129,  130,   98,
 108462  /*   270 */   110,   68,   69,   70,   71,   72,   73,   74,   75,   76,
 108463  /*   280 */    77,   78,   79,   80,  194,   82,   83,   84,   85,   86,
 108464  /*   290 */    87,   88,   89,   90,   91,   92,   19,   11,   94,   95,
 108465  /*   300 */   129,  130,  131,  118,  150,  215,  150,  150,  150,   25,
 108466  /*   310 */   220,   26,   27,   22,  213,   26,   27,   26,   27,  165,
 108467  /*   320 */    25,  165,  165,  165,   30,   94,   49,   50,   34,  173,
 108468  /*   330 */   174,  173,  174,   88,   89,   90,   91,   92,    7,    8,
 108469  /*   340 */   160,  187,   48,   57,  187,   68,   69,   70,   71,   72,
 108470  /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,   23,   82,
 108471  /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
 108472  /*   370 */    19,  215,  150,  215,  194,   19,  220,   88,  220,   94,
 108473  /*   380 */    95,   23,  160,   94,   95,   94,   95,  165,   26,   27,
 108474  /*   390 */    95,  105,  106,  107,  113,  173,  174,  217,   22,  150,
 108475  /*   400 */    49,   50,  116,  119,   57,  120,   50,  158,   22,   21,
 108476  /*   410 */   161,  162,  232,  136,  165,  120,  194,  237,   23,   68,
 108477  /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
 108478  /*   430 */    79,   80,   22,   82,   83,   84,   85,   86,   87,   88,
 108479  /*   440 */    89,   90,   91,   92,   19,   23,   12,  112,   23,  114,
 108480  /*   450 */   115,   63,  105,  106,  107,   23,   94,   95,   97,   98,
 108481  /*   460 */   104,  150,   28,  116,   25,  109,  150,  150,   23,   23,
 108482  /*   470 */   112,   25,  114,  115,   49,   50,  165,  150,   44,   11,
 108483  /*   480 */    46,  165,  165,   16,  173,  174,   76,  136,  100,  173,
 108484  /*   490 */   174,   57,  165,   68,   69,   70,   71,   72,   73,   74,
 108485  /*   500 */    75,   76,   77,   78,   79,   80,  166,   82,   83,   84,
 108486  /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  169,
 108487  /*   520 */   170,  171,   23,   12,   23,  214,  138,   60,  150,   62,
 108488  /*   530 */    24,  215,   26,  216,  112,  150,  114,  115,   36,   28,
 108489  /*   540 */   213,   95,  103,  165,  112,  205,  114,  115,   49,   50,
 108490  /*   550 */   165,  173,  174,   51,   23,   44,   25,   46,  173,  174,
 108491  /*   560 */    58,   22,   23,   22,   25,  160,  120,   68,   69,   70,
 108492  /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
 108493  /*   580 */   230,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 108494  /*   590 */    91,   92,   19,  215,   22,   23,   23,   25,  163,  194,
 108495  /*   600 */    94,  166,  167,  168,   25,  138,   67,    7,    8,    9,
 108496  /*   610 */   108,  206,  207,  169,  170,  171,  150,   22,  221,  222,
 108497  /*   620 */   223,   26,   49,   50,   86,   87,   23,  161,  162,   23,
 108498  /*   630 */    22,  165,   24,  120,   22,   23,   25,  160,  241,   67,
 108499  /*   640 */   176,   68,   69,   70,   71,   72,   73,   74,   75,   76,
 108500  /*   650 */    77,   78,   79,   80,  160,   82,   83,   84,   85,   86,
 108501  /*   660 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  150,
 108502  /*   670 */    23,  194,   35,   23,  230,   25,  150,  155,  150,   67,
 108503  /*   680 */   150,  105,  106,  107,  165,  221,  222,  223,  194,   94,
 108504  /*   690 */    23,  165,   25,  165,  217,  165,   49,   50,   25,  173,
 108505  /*   700 */   174,  173,  174,  173,  174,    0,    1,    2,  118,  221,
 108506  /*   710 */   222,  223,  193,  219,  237,   68,   69,   70,   71,   72,
 108507  /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
 108508  /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
 108509  /*   740 */    19,  150,   19,  165,  150,   24,  166,  167,  168,  227,
 108510  /*   750 */    27,  173,  174,  231,  150,   25,  165,  150,  172,  165,
 108511  /*   760 */   150,  242,  129,  130,  173,  174,  180,  173,  174,  165,
 108512  /*   770 */    49,   50,  165,  150,  176,  165,   35,  173,  174,  165,
 108513  /*   780 */   173,  174,   35,   23,   23,   25,   25,  173,  165,   68,
 108514  /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
 108515  /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
 108516  /*   810 */    89,   90,   91,   92,   19,  150,  193,  165,  150,  221,
 108517  /*   820 */   222,  223,  150,  213,   19,  173,  174,   23,  150,   97,
 108518  /*   830 */   165,  150,   27,  165,  150,  150,  150,  165,  173,  174,
 108519  /*   840 */    22,  173,  174,  165,   49,   50,  165,   52,  116,  165,
 108520  /*   850 */   165,  165,  206,  207,  173,  174,  126,   50,  173,  174,
 108521  /*   860 */   128,   27,  160,   68,   69,   70,   71,   72,   73,   74,
 108522  /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
 108523  /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
 108524  /*   890 */    23,  165,  150,   23,  216,   25,  194,   32,   39,  173,
 108525  /*   900 */   174,  135,  150,  137,  165,  150,   41,  165,  150,   52,
 108526  /*   910 */   238,  104,  173,  174,   29,  173,  174,  165,   49,   50,
 108527  /*   920 */   165,  219,  238,  165,  238,  173,  174,   52,  173,  174,
 108528  /*   930 */    22,  173,  174,   23,   23,  160,   25,   68,   69,   70,
 108529  /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
 108530  /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 108531  /*   960 */    91,   92,   19,  150,  150,  165,  150,  245,  246,  194,
 108532  /*   970 */   150,  144,  145,  173,  174,  160,  150,   22,  165,  165,
 108533  /*   980 */    22,  165,  150,  150,  116,  165,  173,  174,   52,  173,
 108534  /*   990 */   174,  165,   49,   50,   22,  150,  128,  165,  165,  173,
 108535  /*  1000 */   174,  187,  166,  166,   22,  173,  174,  187,  109,  194,
 108536  /*  1010 */   165,   68,   69,   70,   71,   72,   73,   74,   75,   76,
 108537  /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
 108538  /*  1030 */    87,   88,   89,   90,   91,   92,   19,  150,  193,  165,
 108539  /*  1040 */   102,  205,  205,  150,  150,  247,  248,  173,  174,   19,
 108540  /*  1050 */   150,   20,  165,  150,  150,  150,  150,  150,  165,  165,
 108541  /*  1060 */   173,  174,   49,   50,  104,  165,   49,   50,  165,  165,
 108542  /*  1070 */   165,  165,  165,  173,  174,   43,  173,  174,  173,  174,
 108543  /*  1080 */   187,   24,  190,  191,   71,   72,   69,   70,   71,   72,
 108544  /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
 108545  /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
 108546  /*  1110 */    19,   98,  150,  165,  150,  150,  150,  150,  150,  150,
 108547  /*  1120 */    59,  173,  174,   25,  150,  190,  191,  165,   53,  165,
 108548  /*  1130 */   165,  165,  165,  165,  165,  173,  174,  173,  174,  165,
 108549  /*  1140 */    49,   50,   91,   92,    1,    2,   53,  173,  174,  138,
 108550  /*  1150 */   104,   22,    5,    1,   35,  118,  127,  150,  193,  193,
 108551  /*  1160 */   193,   70,   71,   72,   73,   74,   75,   76,   77,   78,
 108552  /*  1170 */    79,   80,  165,   82,   83,   84,   85,   86,   87,   88,
 108553  /*  1180 */    89,   90,   91,   92,   19,   20,  150,   22,  150,   27,
 108554  /*  1190 */   150,   26,   27,  108,  150,   22,   76,   76,  150,   25,
 108555  /*  1200 */   193,  165,   37,  165,  150,  165,   22,   19,   20,  165,
 108556  /*  1210 */    22,  173,  174,  165,   26,   27,   23,  150,  119,  165,
 108557  /*  1220 */   150,   56,  150,  150,  150,   37,   16,  173,  174,  193,
 108558  /*  1230 */   150,   66,  165,  193,    1,  165,  121,  165,  165,  165,
 108559  /*  1240 */    20,  146,  147,  119,   56,  165,  150,  152,   16,  154,
 108560  /*  1250 */   150,   86,   87,   88,   66,  160,  150,  150,   93,   94,
 108561  /*  1260 */    95,  165,  150,   98,  108,  165,  127,   23,   65,  173,
 108562  /*  1270 */   174,  165,  165,  150,   86,   87,  128,  165,  150,  173,
 108563  /*  1280 */   174,   93,   94,   95,   23,  150,   98,   15,  165,  194,
 108564  /*  1290 */   150,  140,   22,  165,  129,  130,  131,  132,  133,  134,
 108565  /*  1300 */   165,  173,  174,    3,  116,  165,   19,   20,  150,   22,
 108566  /*  1310 */     4,  150,  217,   26,   27,  179,  179,  129,  130,  131,
 108567  /*  1320 */   132,  133,  134,  165,   37,  150,  165,  150,  164,   19,
 108568  /*  1330 */    20,  150,   22,  246,  149,  249,   26,   27,  249,  244,
 108569  /*  1340 */   165,  150,  165,   56,    6,  150,  165,   37,  173,  174,
 108570  /*  1350 */   173,  174,  150,   66,  173,  174,  165,  149,  149,   13,
 108571  /*  1360 */   165,   25,  150,  150,  150,  149,   56,  165,  150,  116,
 108572  /*  1370 */   151,  150,  150,   86,   87,  150,   66,  165,  165,  165,
 108573  /*  1380 */    93,   94,   95,  165,  150,   98,  165,  165,  151,   22,
 108574  /*  1390 */   165,  194,  150,   26,   27,  150,   86,   87,  159,  165,
 108575  /*  1400 */   199,  126,  123,   93,   94,   95,  200,  165,   98,  124,
 108576  /*  1410 */   165,  122,  201,  125,  225,  135,  129,  130,  131,  132,
 108577  /*  1420 */   133,  134,    5,  157,  157,  202,  118,   10,   11,   12,
 108578  /*  1430 */    13,   14,  203,   66,   17,  104,  210,  121,  211,  129,
 108579  /*  1440 */   130,  131,  132,  133,  134,  210,  175,  211,   31,  211,
 108580  /*  1450 */    33,  210,  104,   86,   87,   47,  175,  183,  175,   42,
 108581  /*  1460 */   103,   94,  178,  177,   22,   98,  175,   92,  228,  175,
 108582  /*  1470 */   175,  228,   55,  183,   57,  178,  175,  156,   61,   18,
 108583  /*  1480 */   157,   64,  156,  235,  157,  156,   45,  157,  236,  157,
 108584  /*  1490 */   135,  156,  189,   68,  157,  218,  129,  130,  131,   22,
 108585  /*  1500 */   189,  199,  157,  156,  192,   18,  192,  192,  199,  192,
 108586  /*  1510 */   218,  189,   40,  157,   38,  157,  240,  157,  240,  153,
 108587  /*  1520 */   196,  181,  105,  106,  107,  243,  198,  166,  111,  230,
 108588  /*  1530 */   176,  226,  239,  116,  230,  176,  166,  166,  176,  148,
 108589  /*  1540 */   199,  177,  209,  209,  166,  196,  239,  208,  185,  199,
 108590  /*  1550 */    92,  209,  233,  173,  234,  182,  139,  173,  182,  191,
 108591  /*  1560 */   195,  182,  250,  186,
 108593 #define YY_SHIFT_USE_DFLT (-70)
 108594 #define YY_SHIFT_COUNT (416)
 108595 #define YY_SHIFT_MIN   (-69)
 108596 #define YY_SHIFT_MAX   (1487)
 108597 static const short yy_shift_ofst[] = {
 108598  /*     0 */  1143, 1188, 1417, 1188, 1287, 1287,  138,  138,   -2,  -19,
 108599  /*    10 */  1287, 1287, 1287, 1287,  347,  362,  129,  129,  795, 1165,
 108600  /*    20 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
 108601  /*    30 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
 108602  /*    40 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1310, 1287,
 108603  /*    50 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
 108604  /*    60 */  1287, 1287,  286,  362,  362,  538,  538,  231, 1253,   55,
 108605  /*    70 */   721,  647,  573,  499,  425,  351,  277,  203,  869,  869,
 108606  /*    80 */   869,  869,  869,  869,  869,  869,  869,  869,  869,  869,
 108607  /*    90 */   869,  869,  869,  943,  869, 1017, 1091, 1091,  -69,  -45,
 108608  /*   100 */   -45,  -45,  -45,  -45,   -1,   24,  245,  362,  362,  362,
 108609  /*   110 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
 108610  /*   120 */   362,  362,  362,  388,  356,  362,  362,  362,  362,  362,
 108611  /*   130 */   732,  868,  231, 1051, 1458,  -70,  -70,  -70, 1367,   57,
 108612  /*   140 */   434,  434,  289,  291,  285,    1,  204,  572,  539,  362,
 108613  /*   150 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
 108614  /*   160 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
 108615  /*   170 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
 108616  /*   180 */   362,  506,  506,  506,  705, 1253, 1253, 1253,  -70,  -70,
 108617  /*   190 */   -70,  171,  171,  160,  502,  502,  502,  446,  432,  511,
 108618  /*   200 */   422,  358,  335,  -12,  -12,  -12,  -12,  576,  294,  -12,
 108619  /*   210 */   -12,  295,  595,  141,  600,  730,  723,  723,  805,  730,
 108620  /*   220 */   805,  439,  911,  231,  865,  231,  865,  807,  865,  723,
 108621  /*   230 */   766,  633,  633,  231,  284,   63,  608, 1476, 1308, 1308,
 108622  /*   240 */  1472, 1472, 1308, 1477, 1425, 1275, 1487, 1487, 1487, 1487,
 108623  /*   250 */  1308, 1461, 1275, 1477, 1425, 1425, 1308, 1461, 1355, 1441,
 108624  /*   260 */  1308, 1308, 1461, 1308, 1461, 1308, 1461, 1442, 1348, 1348,
 108625  /*   270 */  1348, 1408, 1375, 1375, 1442, 1348, 1357, 1348, 1408, 1348,
 108626  /*   280 */  1348, 1316, 1331, 1316, 1331, 1316, 1331, 1308, 1308, 1280,
 108627  /*   290 */  1288, 1289, 1285, 1279, 1275, 1253, 1336, 1346, 1346, 1338,
 108628  /*   300 */  1338, 1338, 1338,  -70,  -70,  -70,  -70,  -70,  -70, 1013,
 108629  /*   310 */   467,  612,   84,  179,  -28,  870,  410,  761,  760,  667,
 108630  /*   320 */   650,  531,  220,  361,  331,  125,  127,   97, 1306, 1300,
 108631  /*   330 */  1270, 1151, 1272, 1203, 1232, 1261, 1244, 1148, 1174, 1139,
 108632  /*   340 */  1156, 1124, 1220, 1115, 1210, 1233, 1099, 1193, 1184, 1174,
 108633  /*   350 */  1173, 1029, 1121, 1120, 1085, 1162, 1119, 1037, 1152, 1147,
 108634  /*   360 */  1129, 1046, 1011, 1093, 1098, 1075, 1061, 1032,  960, 1057,
 108635  /*   370 */  1031, 1030,  899,  938,  982,  936,  972,  958,  910,  955,
 108636  /*   380 */   875,  885,  908,  857,  859,  867,  804,  590,  834,  747,
 108637  /*   390 */   818,  513,  611,  741,  673,  637,  611,  606,  603,  579,
 108638  /*   400 */   501,  541,  468,  386,  445,  395,  376,  281,  185,  120,
 108639  /*   410 */    92,   75,   45,  114,   25,   11,    5,
 108641 #define YY_REDUCE_USE_DFLT (-169)
 108642 #define YY_REDUCE_COUNT (308)
 108643 #define YY_REDUCE_MIN   (-168)
 108644 #define YY_REDUCE_MAX   (1391)
 108645 static const short yy_reduce_ofst[] = {
 108646  /*     0 */  -141,   90, 1095,  222,  158,  156,   19,   17,   10, -104,
 108647  /*    10 */   378,  316,  311,   12,  180,  249,  598,  464,  397, 1181,
 108648  /*    20 */  1177, 1175, 1128, 1106, 1096, 1054, 1038,  974,  964,  962,
 108649  /*    30 */   948,  905,  903,  900,  887,  874,  832,  826,  816,  813,
 108650  /*    40 */   800,  758,  755,  752,  742,  739,  726,  685,  681,  668,
 108651  /*    50 */   665,  652,  607,  604,  594,  591,  578,  530,  528,  526,
 108652  /*    60 */   385,   18,  477,  466,  519,  444,  350,  435,  405,  488,
 108653  /*    70 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
 108654  /*    80 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
 108655  /*    90 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
 108656  /*   100 */   488,  488,  488,  488,  488,  488,  488, 1040,  678, 1036,
 108657  /*   110 */  1007,  967,  966,  965,  845,  686,  610,  684,  317,  672,
 108658  /*   120 */   893,  327,  623,  522,   -7,  820,  814,  157,  154,  101,
 108659  /*   130 */   702,  494,  580,  488,  488,  488,  488,  488,  614,  586,
 108660  /*   140 */   935,  892,  968, 1245, 1242, 1234, 1225,  798,  798, 1222,
 108661  /*   150 */  1221, 1218, 1214, 1213, 1212, 1202, 1195, 1191, 1161, 1158,
 108662  /*   160 */  1140, 1135, 1123, 1112, 1107, 1100, 1080, 1074, 1073, 1072,
 108663  /*   170 */  1070, 1067, 1048, 1044,  969,  968,  907,  906,  904,  894,
 108664  /*   180 */   833,  837,  836,  340,  827,  815,  775,   68,  722,  646,
 108665  /*   190 */  -168, 1384, 1380, 1377, 1379, 1376, 1373, 1339, 1365, 1368,
 108666  /*   200 */  1365, 1365, 1365, 1365, 1365, 1365, 1365, 1320, 1319, 1365,
 108667  /*   210 */  1365, 1339, 1378, 1349, 1391, 1350, 1342, 1334, 1307, 1341,
 108668  /*   220 */  1293, 1364, 1363, 1371, 1362, 1370, 1359, 1340, 1354, 1333,
 108669  /*   230 */  1305, 1304, 1299, 1361, 1328, 1324, 1366, 1282, 1360, 1358,
 108670  /*   240 */  1278, 1276, 1356, 1292, 1322, 1309, 1317, 1315, 1314, 1312,
 108671  /*   250 */  1345, 1347, 1302, 1277, 1311, 1303, 1337, 1335, 1252, 1248,
 108672  /*   260 */  1332, 1330, 1329, 1327, 1326, 1323, 1321, 1297, 1301, 1295,
 108673  /*   270 */  1294, 1290, 1243, 1240, 1284, 1291, 1286, 1283, 1274, 1281,
 108674  /*   280 */  1271, 1238, 1241, 1236, 1235, 1227, 1226, 1267, 1266, 1189,
 108675  /*   290 */  1229, 1223, 1211, 1206, 1201, 1197, 1239, 1237, 1219, 1216,
 108676  /*   300 */  1209, 1208, 1185, 1089, 1086, 1087, 1137, 1136, 1164,
 108678 static const YYACTIONTYPE yy_default[] = {
 108679  /*     0 */   632,  866,  954,  954,  866,  866,  954,  954,  954,  756,
 108680  /*    10 */   954,  954,  954,  864,  954,  954,  784,  784,  928,  954,
 108681  /*    20 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108682  /*    30 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108683  /*    40 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108684  /*    50 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108685  /*    60 */   954,  954,  954,  954,  954,  954,  954,  671,  760,  790,
 108686  /*    70 */   954,  954,  954,  954,  954,  954,  954,  954,  927,  929,
 108687  /*    80 */   798,  797,  907,  771,  795,  788,  792,  867,  860,  861,
 108688  /*    90 */   859,  863,  868,  954,  791,  827,  844,  826,  838,  843,
 108689  /*   100 */   850,  842,  839,  829,  828,  830,  831,  954,  954,  954,
 108690  /*   110 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108691  /*   120 */   954,  954,  954,  658,  725,  954,  954,  954,  954,  954,
 108692  /*   130 */   954,  954,  954,  832,  833,  847,  846,  845,  954,  663,
 108693  /*   140 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108694  /*   150 */   934,  932,  954,  879,  954,  954,  954,  954,  954,  954,
 108695  /*   160 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108696  /*   170 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108697  /*   180 */   638,  756,  756,  756,  632,  954,  954,  954,  946,  760,
 108698  /*   190 */   750,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108699  /*   200 */   954,  954,  954,  800,  739,  917,  919,  954,  900,  737,
 108700  /*   210 */   660,  758,  673,  748,  640,  794,  773,  773,  912,  794,
 108701  /*   220 */   912,  696,  719,  954,  784,  954,  784,  693,  784,  773,
 108702  /*   230 */   862,  954,  954,  954,  757,  748,  954,  939,  764,  764,
 108703  /*   240 */   931,  931,  764,  806,  729,  794,  736,  736,  736,  736,
 108704  /*   250 */   764,  655,  794,  806,  729,  729,  764,  655,  906,  904,
 108705  /*   260 */   764,  764,  655,  764,  655,  764,  655,  872,  727,  727,
 108706  /*   270 */   727,  711,  876,  876,  872,  727,  696,  727,  711,  727,
 108707  /*   280 */   727,  777,  772,  777,  772,  777,  772,  764,  764,  954,
 108708  /*   290 */   789,  778,  787,  785,  794,  954,  714,  648,  648,  637,
 108709  /*   300 */   637,  637,  637,  951,  951,  946,  698,  698,  681,  954,
 108710  /*   310 */   954,  954,  954,  954,  954,  954,  881,  954,  954,  954,
 108711  /*   320 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  633,
 108712  /*   330 */   941,  954,  954,  938,  954,  954,  954,  954,  799,  954,
 108713  /*   340 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  916,
 108714  /*   350 */   954,  954,  954,  954,  954,  954,  954,  910,  954,  954,
 108715  /*   360 */   954,  954,  954,  954,  903,  902,  954,  954,  954,  954,
 108716  /*   370 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108717  /*   380 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
 108718  /*   390 */   954,  954,  786,  954,  779,  954,  865,  954,  954,  954,
 108719  /*   400 */   954,  954,  954,  954,  954,  954,  954,  742,  815,  954,
 108720  /*   410 */   814,  818,  813,  665,  954,  646,  954,  629,  634,  950,
 108721  /*   420 */   953,  952,  949,  948,  947,  942,  940,  937,  936,  935,
 108722  /*   430 */   933,  930,  926,  885,  883,  890,  889,  888,  887,  886,
 108723  /*   440 */   884,  882,  880,  801,  796,  793,  925,  878,  738,  735,
 108724  /*   450 */   734,  654,  943,  909,  918,  805,  804,  807,  915,  914,
 108725  /*   460 */   913,  911,  908,  895,  803,  802,  730,  870,  869,  657,
 108726  /*   470 */   899,  898,  897,  901,  905,  896,  766,  656,  653,  662,
 108727  /*   480 */   717,  718,  726,  724,  723,  722,  721,  720,  716,  664,
 108728  /*   490 */   672,  710,  695,  694,  875,  877,  874,  873,  703,  702,
 108729  /*   500 */   708,  707,  706,  705,  704,  701,  700,  699,  692,  691,
 108730  /*   510 */   697,  690,  713,  712,  709,  689,  733,  732,  731,  728,
 108731  /*   520 */   688,  687,  686,  818,  685,  684,  824,  823,  811,  854,
 108732  /*   530 */   753,  752,  751,  763,  762,  775,  774,  809,  808,  776,
 108733  /*   540 */   761,  755,  754,  770,  769,  768,  767,  759,  749,  781,
 108734  /*   550 */   783,  782,  780,  856,  765,  853,  924,  923,  922,  921,
 108735  /*   560 */   920,  858,  857,  825,  822,  676,  677,  893,  892,  894,
 108736  /*   570 */   891,  679,  678,  675,  674,  855,  744,  743,  851,  848,
 108737  /*   580 */   840,  836,  852,  849,  841,  837,  835,  834,  820,  819,
 108738  /*   590 */   817,  816,  812,  821,  667,  745,  741,  740,  810,  747,
 108739  /*   600 */   746,  683,  682,  680,  661,  659,  652,  650,  649,  651,
 108740  /*   610 */   647,  645,  644,  643,  642,  641,  670,  669,  668,  666,
 108741  /*   620 */   665,  639,  636,  635,  631,  630,  628,
 108744 /* The next table maps tokens into fallback tokens.  If a construct
 108745 ** like the following:
 108747 **      %fallback ID X Y Z.
 108749 ** appears in the grammar, then ID becomes a fallback token for X, Y,
 108750 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
 108751 ** but it does not parse, the type of the token is changed to ID and
 108752 ** the parse is retried before an error is thrown.
 108754 #ifdef YYFALLBACK
 108755 static const YYCODETYPE yyFallback[] = {
 108756     0,  /*          $ => nothing */
 108757     0,  /*       SEMI => nothing */
 108758    26,  /*    EXPLAIN => ID */
 108759    26,  /*      QUERY => ID */
 108760    26,  /*       PLAN => ID */
 108761    26,  /*      BEGIN => ID */
 108762     0,  /* TRANSACTION => nothing */
 108763    26,  /*   DEFERRED => ID */
 108764    26,  /*  IMMEDIATE => ID */
 108765    26,  /*  EXCLUSIVE => ID */
 108766     0,  /*     COMMIT => nothing */
 108767    26,  /*        END => ID */
 108768    26,  /*   ROLLBACK => ID */
 108769    26,  /*  SAVEPOINT => ID */
 108770    26,  /*    RELEASE => ID */
 108771     0,  /*         TO => nothing */
 108772     0,  /*      TABLE => nothing */
 108773     0,  /*     CREATE => nothing */
 108774    26,  /*         IF => ID */
 108775     0,  /*        NOT => nothing */
 108776     0,  /*     EXISTS => nothing */
 108777    26,  /*       TEMP => ID */
 108778     0,  /*         LP => nothing */
 108779     0,  /*         RP => nothing */
 108780     0,  /*         AS => nothing */
 108781     0,  /*      COMMA => nothing */
 108782     0,  /*         ID => nothing */
 108783     0,  /*    INDEXED => nothing */
 108784    26,  /*      ABORT => ID */
 108785    26,  /*     ACTION => ID */
 108786    26,  /*      AFTER => ID */
 108787    26,  /*    ANALYZE => ID */
 108788    26,  /*        ASC => ID */
 108789    26,  /*     ATTACH => ID */
 108790    26,  /*     BEFORE => ID */
 108791    26,  /*         BY => ID */
 108792    26,  /*    CASCADE => ID */
 108793    26,  /*       CAST => ID */
 108794    26,  /*   COLUMNKW => ID */
 108795    26,  /*   CONFLICT => ID */
 108796    26,  /*   DATABASE => ID */
 108797    26,  /*       DESC => ID */
 108798    26,  /*     DETACH => ID */
 108799    26,  /*       EACH => ID */
 108800    26,  /*       FAIL => ID */
 108801    26,  /*        FOR => ID */
 108802    26,  /*     IGNORE => ID */
 108803    26,  /*  INITIALLY => ID */
 108804    26,  /*    INSTEAD => ID */
 108805    26,  /*    LIKE_KW => ID */
 108806    26,  /*      MATCH => ID */
 108807    26,  /*         NO => ID */
 108808    26,  /*        KEY => ID */
 108809    26,  /*         OF => ID */
 108810    26,  /*     OFFSET => ID */
 108811    26,  /*     PRAGMA => ID */
 108812    26,  /*      RAISE => ID */
 108813    26,  /*    REPLACE => ID */
 108814    26,  /*   RESTRICT => ID */
 108815    26,  /*        ROW => ID */
 108816    26,  /*    TRIGGER => ID */
 108817    26,  /*     VACUUM => ID */
 108818    26,  /*       VIEW => ID */
 108819    26,  /*    VIRTUAL => ID */
 108820    26,  /*    REINDEX => ID */
 108821    26,  /*     RENAME => ID */
 108822    26,  /*   CTIME_KW => ID */
 108824 #endif /* YYFALLBACK */
 108826 /* The following structure represents a single element of the
 108827 ** parser's stack.  Information stored includes:
 108829 **   +  The state number for the parser at this level of the stack.
 108831 **   +  The value of the token stored at this level of the stack.
 108832 **      (In other words, the "major" token.)
 108834 **   +  The semantic value stored at this level of the stack.  This is
 108835 **      the information used by the action routines in the grammar.
 108836 **      It is sometimes called the "minor" token.
 108838 struct yyStackEntry {
 108839   YYACTIONTYPE stateno;  /* The state-number */
 108840   YYCODETYPE major;      /* The major token value.  This is the code
 108841                          ** number for the token at this stack level */
 108842   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
 108843                          ** is the value of the token  */
 108845 typedef struct yyStackEntry yyStackEntry;
 108847 /* The state of the parser is completely contained in an instance of
 108848 ** the following structure */
 108849 struct yyParser {
 108850   int yyidx;                    /* Index of top element in stack */
 108851 #ifdef YYTRACKMAXSTACKDEPTH
 108852   int yyidxMax;                 /* Maximum value of yyidx */
 108853 #endif
 108854   int yyerrcnt;                 /* Shifts left before out of the error */
 108855   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
 108856 #if YYSTACKDEPTH<=0
 108857   int yystksz;                  /* Current side of the stack */
 108858   yyStackEntry *yystack;        /* The parser's stack */
 108859 #else
 108860   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
 108861 #endif
 108863 typedef struct yyParser yyParser;
 108865 #ifndef NDEBUG
 108866 /* #include <stdio.h> */
 108867 static FILE *yyTraceFILE = 0;
 108868 static char *yyTracePrompt = 0;
 108869 #endif /* NDEBUG */
 108871 #ifndef NDEBUG
 108873 ** Turn parser tracing on by giving a stream to which to write the trace
 108874 ** and a prompt to preface each trace message.  Tracing is turned off
 108875 ** by making either argument NULL 
 108877 ** Inputs:
 108878 ** <ul>
 108879 ** <li> A FILE* to which trace output should be written.
 108880 **      If NULL, then tracing is turned off.
 108881 ** <li> A prefix string written at the beginning of every
 108882 **      line of trace output.  If NULL, then tracing is
 108883 **      turned off.
 108884 ** </ul>
 108886 ** Outputs:
 108887 ** None.
 108889 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
 108890   yyTraceFILE = TraceFILE;
 108891   yyTracePrompt = zTracePrompt;
 108892   if( yyTraceFILE==0 ) yyTracePrompt = 0;
 108893   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
 108895 #endif /* NDEBUG */
 108897 #ifndef NDEBUG
 108898 /* For tracing shifts, the names of all terminals and nonterminals
 108899 ** are required.  The following table supplies these names */
 108900 static const char *const yyTokenName[] = { 
 108901   "$",             "SEMI",          "EXPLAIN",       "QUERY",       
 108902   "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
 108903   "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
 108904   "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
 108905   "TABLE",         "CREATE",        "IF",            "NOT",         
 108906   "EXISTS",        "TEMP",          "LP",            "RP",          
 108907   "AS",            "COMMA",         "ID",            "INDEXED",     
 108908   "ABORT",         "ACTION",        "AFTER",         "ANALYZE",     
 108909   "ASC",           "ATTACH",        "BEFORE",        "BY",          
 108910   "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",    
 108911   "DATABASE",      "DESC",          "DETACH",        "EACH",        
 108912   "FAIL",          "FOR",           "IGNORE",        "INITIALLY",   
 108913   "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",          
 108914   "KEY",           "OF",            "OFFSET",        "PRAGMA",      
 108915   "RAISE",         "REPLACE",       "RESTRICT",      "ROW",         
 108916   "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",     
 108917   "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",         
 108918   "OR",            "AND",           "IS",            "BETWEEN",     
 108919   "IN",            "ISNULL",        "NOTNULL",       "NE",          
 108920   "EQ",            "GT",            "LE",            "LT",          
 108921   "GE",            "ESCAPE",        "BITAND",        "BITOR",       
 108922   "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",       
 108923   "STAR",          "SLASH",         "REM",           "CONCAT",      
 108924   "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",     
 108925   "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",     
 108926   "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",    
 108927   "ON",            "INSERT",        "DELETE",        "UPDATE",      
 108928   "SET",           "DEFERRABLE",    "FOREIGN",       "DROP",        
 108929   "UNION",         "ALL",           "EXCEPT",        "INTERSECT",   
 108930   "SELECT",        "DISTINCT",      "DOT",           "FROM",        
 108931   "JOIN",          "USING",         "ORDER",         "GROUP",       
 108932   "HAVING",        "LIMIT",         "WHERE",         "INTO",        
 108933   "VALUES",        "INTEGER",       "FLOAT",         "BLOB",        
 108934   "REGISTER",      "VARIABLE",      "CASE",          "WHEN",        
 108935   "THEN",          "ELSE",          "INDEX",         "ALTER",       
 108936   "ADD",           "error",         "input",         "cmdlist",     
 108937   "ecmd",          "explain",       "cmdx",          "cmd",         
 108938   "transtype",     "trans_opt",     "nm",            "savepoint_opt",
 108939   "create_table",  "create_table_args",  "createkw",      "temp",        
 108940   "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
 108941   "select",        "column",        "columnid",      "type",        
 108942   "carglist",      "id",            "ids",           "typetoken",   
 108943   "typename",      "signed",        "plus_num",      "minus_num",   
 108944   "ccons",         "term",          "expr",          "onconf",      
 108945   "sortorder",     "autoinc",       "idxlist_opt",   "refargs",     
 108946   "defer_subclause",  "refarg",        "refact",        "init_deferred_pred_opt",
 108947   "conslist",      "tconscomma",    "tcons",         "idxlist",     
 108948   "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
 108949   "ifexists",      "fullname",      "oneselect",     "multiselect_op",
 108950   "distinct",      "selcollist",    "from",          "where_opt",   
 108951   "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",   
 108952   "sclp",          "as",            "seltablist",    "stl_prefix",  
 108953   "joinop",        "indexed_opt",   "on_opt",        "using_opt",   
 108954   "joinop2",       "inscollist",    "sortlist",      "nexprlist",   
 108955   "setlist",       "insert_cmd",    "inscollist_opt",  "valuelist",   
 108956   "exprlist",      "likeop",        "between_op",    "in_op",       
 108957   "case_operand",  "case_exprlist",  "case_else",     "uniqueflag",  
 108958   "collate",       "nmnum",         "number",        "trigger_decl",
 108959   "trigger_cmd_list",  "trigger_time",  "trigger_event",  "foreach_clause",
 108960   "when_clause",   "trigger_cmd",   "trnm",          "tridxby",     
 108961   "database_kw_opt",  "key_opt",       "add_column_fullname",  "kwcolumn_opt",
 108962   "create_vtab",   "vtabarglist",   "vtabarg",       "vtabargtoken",
 108963   "lp",            "anylist",     
 108965 #endif /* NDEBUG */
 108967 #ifndef NDEBUG
 108968 /* For tracing reduce actions, the names of all rules are required.
 108970 static const char *const yyRuleName[] = {
 108971  /*   0 */ "input ::= cmdlist",
 108972  /*   1 */ "cmdlist ::= cmdlist ecmd",
 108973  /*   2 */ "cmdlist ::= ecmd",
 108974  /*   3 */ "ecmd ::= SEMI",
 108975  /*   4 */ "ecmd ::= explain cmdx SEMI",
 108976  /*   5 */ "explain ::=",
 108977  /*   6 */ "explain ::= EXPLAIN",
 108978  /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
 108979  /*   8 */ "cmdx ::= cmd",
 108980  /*   9 */ "cmd ::= BEGIN transtype trans_opt",
 108981  /*  10 */ "trans_opt ::=",
 108982  /*  11 */ "trans_opt ::= TRANSACTION",
 108983  /*  12 */ "trans_opt ::= TRANSACTION nm",
 108984  /*  13 */ "transtype ::=",
 108985  /*  14 */ "transtype ::= DEFERRED",
 108986  /*  15 */ "transtype ::= IMMEDIATE",
 108987  /*  16 */ "transtype ::= EXCLUSIVE",
 108988  /*  17 */ "cmd ::= COMMIT trans_opt",
 108989  /*  18 */ "cmd ::= END trans_opt",
 108990  /*  19 */ "cmd ::= ROLLBACK trans_opt",
 108991  /*  20 */ "savepoint_opt ::= SAVEPOINT",
 108992  /*  21 */ "savepoint_opt ::=",
 108993  /*  22 */ "cmd ::= SAVEPOINT nm",
 108994  /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
 108995  /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
 108996  /*  25 */ "cmd ::= create_table create_table_args",
 108997  /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
 108998  /*  27 */ "createkw ::= CREATE",
 108999  /*  28 */ "ifnotexists ::=",
 109000  /*  29 */ "ifnotexists ::= IF NOT EXISTS",
 109001  /*  30 */ "temp ::= TEMP",
 109002  /*  31 */ "temp ::=",
 109003  /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP",
 109004  /*  33 */ "create_table_args ::= AS select",
 109005  /*  34 */ "columnlist ::= columnlist COMMA column",
 109006  /*  35 */ "columnlist ::= column",
 109007  /*  36 */ "column ::= columnid type carglist",
 109008  /*  37 */ "columnid ::= nm",
 109009  /*  38 */ "id ::= ID",
 109010  /*  39 */ "id ::= INDEXED",
 109011  /*  40 */ "ids ::= ID|STRING",
 109012  /*  41 */ "nm ::= id",
 109013  /*  42 */ "nm ::= STRING",
 109014  /*  43 */ "nm ::= JOIN_KW",
 109015  /*  44 */ "type ::=",
 109016  /*  45 */ "type ::= typetoken",
 109017  /*  46 */ "typetoken ::= typename",
 109018  /*  47 */ "typetoken ::= typename LP signed RP",
 109019  /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
 109020  /*  49 */ "typename ::= ids",
 109021  /*  50 */ "typename ::= typename ids",
 109022  /*  51 */ "signed ::= plus_num",
 109023  /*  52 */ "signed ::= minus_num",
 109024  /*  53 */ "carglist ::= carglist ccons",
 109025  /*  54 */ "carglist ::=",
 109026  /*  55 */ "ccons ::= CONSTRAINT nm",
 109027  /*  56 */ "ccons ::= DEFAULT term",
 109028  /*  57 */ "ccons ::= DEFAULT LP expr RP",
 109029  /*  58 */ "ccons ::= DEFAULT PLUS term",
 109030  /*  59 */ "ccons ::= DEFAULT MINUS term",
 109031  /*  60 */ "ccons ::= DEFAULT id",
 109032  /*  61 */ "ccons ::= NULL onconf",
 109033  /*  62 */ "ccons ::= NOT NULL onconf",
 109034  /*  63 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
 109035  /*  64 */ "ccons ::= UNIQUE onconf",
 109036  /*  65 */ "ccons ::= CHECK LP expr RP",
 109037  /*  66 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
 109038  /*  67 */ "ccons ::= defer_subclause",
 109039  /*  68 */ "ccons ::= COLLATE ids",
 109040  /*  69 */ "autoinc ::=",
 109041  /*  70 */ "autoinc ::= AUTOINCR",
 109042  /*  71 */ "refargs ::=",
 109043  /*  72 */ "refargs ::= refargs refarg",
 109044  /*  73 */ "refarg ::= MATCH nm",
 109045  /*  74 */ "refarg ::= ON INSERT refact",
 109046  /*  75 */ "refarg ::= ON DELETE refact",
 109047  /*  76 */ "refarg ::= ON UPDATE refact",
 109048  /*  77 */ "refact ::= SET NULL",
 109049  /*  78 */ "refact ::= SET DEFAULT",
 109050  /*  79 */ "refact ::= CASCADE",
 109051  /*  80 */ "refact ::= RESTRICT",
 109052  /*  81 */ "refact ::= NO ACTION",
 109053  /*  82 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
 109054  /*  83 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
 109055  /*  84 */ "init_deferred_pred_opt ::=",
 109056  /*  85 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
 109057  /*  86 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
 109058  /*  87 */ "conslist_opt ::=",
 109059  /*  88 */ "conslist_opt ::= COMMA conslist",
 109060  /*  89 */ "conslist ::= conslist tconscomma tcons",
 109061  /*  90 */ "conslist ::= tcons",
 109062  /*  91 */ "tconscomma ::= COMMA",
 109063  /*  92 */ "tconscomma ::=",
 109064  /*  93 */ "tcons ::= CONSTRAINT nm",
 109065  /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
 109066  /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
 109067  /*  96 */ "tcons ::= CHECK LP expr RP onconf",
 109068  /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
 109069  /*  98 */ "defer_subclause_opt ::=",
 109070  /*  99 */ "defer_subclause_opt ::= defer_subclause",
 109071  /* 100 */ "onconf ::=",
 109072  /* 101 */ "onconf ::= ON CONFLICT resolvetype",
 109073  /* 102 */ "orconf ::=",
 109074  /* 103 */ "orconf ::= OR resolvetype",
 109075  /* 104 */ "resolvetype ::= raisetype",
 109076  /* 105 */ "resolvetype ::= IGNORE",
 109077  /* 106 */ "resolvetype ::= REPLACE",
 109078  /* 107 */ "cmd ::= DROP TABLE ifexists fullname",
 109079  /* 108 */ "ifexists ::= IF EXISTS",
 109080  /* 109 */ "ifexists ::=",
 109081  /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
 109082  /* 111 */ "cmd ::= DROP VIEW ifexists fullname",
 109083  /* 112 */ "cmd ::= select",
 109084  /* 113 */ "select ::= oneselect",
 109085  /* 114 */ "select ::= select multiselect_op oneselect",
 109086  /* 115 */ "multiselect_op ::= UNION",
 109087  /* 116 */ "multiselect_op ::= UNION ALL",
 109088  /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
 109089  /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
 109090  /* 119 */ "distinct ::= DISTINCT",
 109091  /* 120 */ "distinct ::= ALL",
 109092  /* 121 */ "distinct ::=",
 109093  /* 122 */ "sclp ::= selcollist COMMA",
 109094  /* 123 */ "sclp ::=",
 109095  /* 124 */ "selcollist ::= sclp expr as",
 109096  /* 125 */ "selcollist ::= sclp STAR",
 109097  /* 126 */ "selcollist ::= sclp nm DOT STAR",
 109098  /* 127 */ "as ::= AS nm",
 109099  /* 128 */ "as ::= ids",
 109100  /* 129 */ "as ::=",
 109101  /* 130 */ "from ::=",
 109102  /* 131 */ "from ::= FROM seltablist",
 109103  /* 132 */ "stl_prefix ::= seltablist joinop",
 109104  /* 133 */ "stl_prefix ::=",
 109105  /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
 109106  /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
 109107  /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
 109108  /* 137 */ "dbnm ::=",
 109109  /* 138 */ "dbnm ::= DOT nm",
 109110  /* 139 */ "fullname ::= nm dbnm",
 109111  /* 140 */ "joinop ::= COMMA|JOIN",
 109112  /* 141 */ "joinop ::= JOIN_KW JOIN",
 109113  /* 142 */ "joinop ::= JOIN_KW nm JOIN",
 109114  /* 143 */ "joinop ::= JOIN_KW nm nm JOIN",
 109115  /* 144 */ "on_opt ::= ON expr",
 109116  /* 145 */ "on_opt ::=",
 109117  /* 146 */ "indexed_opt ::=",
 109118  /* 147 */ "indexed_opt ::= INDEXED BY nm",
 109119  /* 148 */ "indexed_opt ::= NOT INDEXED",
 109120  /* 149 */ "using_opt ::= USING LP inscollist RP",
 109121  /* 150 */ "using_opt ::=",
 109122  /* 151 */ "orderby_opt ::=",
 109123  /* 152 */ "orderby_opt ::= ORDER BY sortlist",
 109124  /* 153 */ "sortlist ::= sortlist COMMA expr sortorder",
 109125  /* 154 */ "sortlist ::= expr sortorder",
 109126  /* 155 */ "sortorder ::= ASC",
 109127  /* 156 */ "sortorder ::= DESC",
 109128  /* 157 */ "sortorder ::=",
 109129  /* 158 */ "groupby_opt ::=",
 109130  /* 159 */ "groupby_opt ::= GROUP BY nexprlist",
 109131  /* 160 */ "having_opt ::=",
 109132  /* 161 */ "having_opt ::= HAVING expr",
 109133  /* 162 */ "limit_opt ::=",
 109134  /* 163 */ "limit_opt ::= LIMIT expr",
 109135  /* 164 */ "limit_opt ::= LIMIT expr OFFSET expr",
 109136  /* 165 */ "limit_opt ::= LIMIT expr COMMA expr",
 109137  /* 166 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
 109138  /* 167 */ "where_opt ::=",
 109139  /* 168 */ "where_opt ::= WHERE expr",
 109140  /* 169 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
 109141  /* 170 */ "setlist ::= setlist COMMA nm EQ expr",
 109142  /* 171 */ "setlist ::= nm EQ expr",
 109143  /* 172 */ "cmd ::= insert_cmd INTO fullname inscollist_opt valuelist",
 109144  /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
 109145  /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
 109146  /* 175 */ "insert_cmd ::= INSERT orconf",
 109147  /* 176 */ "insert_cmd ::= REPLACE",
 109148  /* 177 */ "valuelist ::= VALUES LP nexprlist RP",
 109149  /* 178 */ "valuelist ::= valuelist COMMA LP exprlist RP",
 109150  /* 179 */ "inscollist_opt ::=",
 109151  /* 180 */ "inscollist_opt ::= LP inscollist RP",
 109152  /* 181 */ "inscollist ::= inscollist COMMA nm",
 109153  /* 182 */ "inscollist ::= nm",
 109154  /* 183 */ "expr ::= term",
 109155  /* 184 */ "expr ::= LP expr RP",
 109156  /* 185 */ "term ::= NULL",
 109157  /* 186 */ "expr ::= id",
 109158  /* 187 */ "expr ::= JOIN_KW",
 109159  /* 188 */ "expr ::= nm DOT nm",
 109160  /* 189 */ "expr ::= nm DOT nm DOT nm",
 109161  /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
 109162  /* 191 */ "term ::= STRING",
 109163  /* 192 */ "expr ::= REGISTER",
 109164  /* 193 */ "expr ::= VARIABLE",
 109165  /* 194 */ "expr ::= expr COLLATE ids",
 109166  /* 195 */ "expr ::= CAST LP expr AS typetoken RP",
 109167  /* 196 */ "expr ::= ID LP distinct exprlist RP",
 109168  /* 197 */ "expr ::= ID LP STAR RP",
 109169  /* 198 */ "term ::= CTIME_KW",
 109170  /* 199 */ "expr ::= expr AND expr",
 109171  /* 200 */ "expr ::= expr OR expr",
 109172  /* 201 */ "expr ::= expr LT|GT|GE|LE expr",
 109173  /* 202 */ "expr ::= expr EQ|NE expr",
 109174  /* 203 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
 109175  /* 204 */ "expr ::= expr PLUS|MINUS expr",
 109176  /* 205 */ "expr ::= expr STAR|SLASH|REM expr",
 109177  /* 206 */ "expr ::= expr CONCAT expr",
 109178  /* 207 */ "likeop ::= LIKE_KW",
 109179  /* 208 */ "likeop ::= NOT LIKE_KW",
 109180  /* 209 */ "likeop ::= MATCH",
 109181  /* 210 */ "likeop ::= NOT MATCH",
 109182  /* 211 */ "expr ::= expr likeop expr",
 109183  /* 212 */ "expr ::= expr likeop expr ESCAPE expr",
 109184  /* 213 */ "expr ::= expr ISNULL|NOTNULL",
 109185  /* 214 */ "expr ::= expr NOT NULL",
 109186  /* 215 */ "expr ::= expr IS expr",
 109187  /* 216 */ "expr ::= expr IS NOT expr",
 109188  /* 217 */ "expr ::= NOT expr",
 109189  /* 218 */ "expr ::= BITNOT expr",
 109190  /* 219 */ "expr ::= MINUS expr",
 109191  /* 220 */ "expr ::= PLUS expr",
 109192  /* 221 */ "between_op ::= BETWEEN",
 109193  /* 222 */ "between_op ::= NOT BETWEEN",
 109194  /* 223 */ "expr ::= expr between_op expr AND expr",
 109195  /* 224 */ "in_op ::= IN",
 109196  /* 225 */ "in_op ::= NOT IN",
 109197  /* 226 */ "expr ::= expr in_op LP exprlist RP",
 109198  /* 227 */ "expr ::= LP select RP",
 109199  /* 228 */ "expr ::= expr in_op LP select RP",
 109200  /* 229 */ "expr ::= expr in_op nm dbnm",
 109201  /* 230 */ "expr ::= EXISTS LP select RP",
 109202  /* 231 */ "expr ::= CASE case_operand case_exprlist case_else END",
 109203  /* 232 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
 109204  /* 233 */ "case_exprlist ::= WHEN expr THEN expr",
 109205  /* 234 */ "case_else ::= ELSE expr",
 109206  /* 235 */ "case_else ::=",
 109207  /* 236 */ "case_operand ::= expr",
 109208  /* 237 */ "case_operand ::=",
 109209  /* 238 */ "exprlist ::= nexprlist",
 109210  /* 239 */ "exprlist ::=",
 109211  /* 240 */ "nexprlist ::= nexprlist COMMA expr",
 109212  /* 241 */ "nexprlist ::= expr",
 109213  /* 242 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
 109214  /* 243 */ "uniqueflag ::= UNIQUE",
 109215  /* 244 */ "uniqueflag ::=",
 109216  /* 245 */ "idxlist_opt ::=",
 109217  /* 246 */ "idxlist_opt ::= LP idxlist RP",
 109218  /* 247 */ "idxlist ::= idxlist COMMA nm collate sortorder",
 109219  /* 248 */ "idxlist ::= nm collate sortorder",
 109220  /* 249 */ "collate ::=",
 109221  /* 250 */ "collate ::= COLLATE ids",
 109222  /* 251 */ "cmd ::= DROP INDEX ifexists fullname",
 109223  /* 252 */ "cmd ::= VACUUM",
 109224  /* 253 */ "cmd ::= VACUUM nm",
 109225  /* 254 */ "cmd ::= PRAGMA nm dbnm",
 109226  /* 255 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
 109227  /* 256 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
 109228  /* 257 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
 109229  /* 258 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
 109230  /* 259 */ "nmnum ::= plus_num",
 109231  /* 260 */ "nmnum ::= nm",
 109232  /* 261 */ "nmnum ::= ON",
 109233  /* 262 */ "nmnum ::= DELETE",
 109234  /* 263 */ "nmnum ::= DEFAULT",
 109235  /* 264 */ "plus_num ::= PLUS number",
 109236  /* 265 */ "plus_num ::= number",
 109237  /* 266 */ "minus_num ::= MINUS number",
 109238  /* 267 */ "number ::= INTEGER|FLOAT",
 109239  /* 268 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
 109240  /* 269 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
 109241  /* 270 */ "trigger_time ::= BEFORE",
 109242  /* 271 */ "trigger_time ::= AFTER",
 109243  /* 272 */ "trigger_time ::= INSTEAD OF",
 109244  /* 273 */ "trigger_time ::=",
 109245  /* 274 */ "trigger_event ::= DELETE|INSERT",
 109246  /* 275 */ "trigger_event ::= UPDATE",
 109247  /* 276 */ "trigger_event ::= UPDATE OF inscollist",
 109248  /* 277 */ "foreach_clause ::=",
 109249  /* 278 */ "foreach_clause ::= FOR EACH ROW",
 109250  /* 279 */ "when_clause ::=",
 109251  /* 280 */ "when_clause ::= WHEN expr",
 109252  /* 281 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
 109253  /* 282 */ "trigger_cmd_list ::= trigger_cmd SEMI",
 109254  /* 283 */ "trnm ::= nm",
 109255  /* 284 */ "trnm ::= nm DOT nm",
 109256  /* 285 */ "tridxby ::=",
 109257  /* 286 */ "tridxby ::= INDEXED BY nm",
 109258  /* 287 */ "tridxby ::= NOT INDEXED",
 109259  /* 288 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
 109260  /* 289 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt valuelist",
 109261  /* 290 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
 109262  /* 291 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
 109263  /* 292 */ "trigger_cmd ::= select",
 109264  /* 293 */ "expr ::= RAISE LP IGNORE RP",
 109265  /* 294 */ "expr ::= RAISE LP raisetype COMMA nm RP",
 109266  /* 295 */ "raisetype ::= ROLLBACK",
 109267  /* 296 */ "raisetype ::= ABORT",
 109268  /* 297 */ "raisetype ::= FAIL",
 109269  /* 298 */ "cmd ::= DROP TRIGGER ifexists fullname",
 109270  /* 299 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
 109271  /* 300 */ "cmd ::= DETACH database_kw_opt expr",
 109272  /* 301 */ "key_opt ::=",
 109273  /* 302 */ "key_opt ::= KEY expr",
 109274  /* 303 */ "database_kw_opt ::= DATABASE",
 109275  /* 304 */ "database_kw_opt ::=",
 109276  /* 305 */ "cmd ::= REINDEX",
 109277  /* 306 */ "cmd ::= REINDEX nm dbnm",
 109278  /* 307 */ "cmd ::= ANALYZE",
 109279  /* 308 */ "cmd ::= ANALYZE nm dbnm",
 109280  /* 309 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
 109281  /* 310 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
 109282  /* 311 */ "add_column_fullname ::= fullname",
 109283  /* 312 */ "kwcolumn_opt ::=",
 109284  /* 313 */ "kwcolumn_opt ::= COLUMNKW",
 109285  /* 314 */ "cmd ::= create_vtab",
 109286  /* 315 */ "cmd ::= create_vtab LP vtabarglist RP",
 109287  /* 316 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
 109288  /* 317 */ "vtabarglist ::= vtabarg",
 109289  /* 318 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
 109290  /* 319 */ "vtabarg ::=",
 109291  /* 320 */ "vtabarg ::= vtabarg vtabargtoken",
 109292  /* 321 */ "vtabargtoken ::= ANY",
 109293  /* 322 */ "vtabargtoken ::= lp anylist RP",
 109294  /* 323 */ "lp ::= LP",
 109295  /* 324 */ "anylist ::=",
 109296  /* 325 */ "anylist ::= anylist LP anylist RP",
 109297  /* 326 */ "anylist ::= anylist ANY",
 109299 #endif /* NDEBUG */
 109302 #if YYSTACKDEPTH<=0
 109304 ** Try to increase the size of the parser stack.
 109306 static void yyGrowStack(yyParser *p){
 109307   int newSize;
 109308   yyStackEntry *pNew;
 109310   newSize = p->yystksz*2 + 100;
 109311   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
 109312   if( pNew ){
 109313     p->yystack = pNew;
 109314     p->yystksz = newSize;
 109315 #ifndef NDEBUG
 109316     if( yyTraceFILE ){
 109317       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
 109318               yyTracePrompt, p->yystksz);
 109320 #endif
 109323 #endif
 109326 ** This function allocates a new parser.
 109327 ** The only argument is a pointer to a function which works like
 109328 ** malloc.
 109330 ** Inputs:
 109331 ** A pointer to the function used to allocate memory.
 109333 ** Outputs:
 109334 ** A pointer to a parser.  This pointer is used in subsequent calls
 109335 ** to sqlite3Parser and sqlite3ParserFree.
 109337 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
 109338   yyParser *pParser;
 109339   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
 109340   if( pParser ){
 109341     pParser->yyidx = -1;
 109342 #ifdef YYTRACKMAXSTACKDEPTH
 109343     pParser->yyidxMax = 0;
 109344 #endif
 109345 #if YYSTACKDEPTH<=0
 109346     pParser->yystack = NULL;
 109347     pParser->yystksz = 0;
 109348     yyGrowStack(pParser);
 109349 #endif
 109351   return pParser;
 109354 /* The following function deletes the value associated with a
 109355 ** symbol.  The symbol can be either a terminal or nonterminal.
 109356 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
 109357 ** the value.
 109359 static void yy_destructor(
 109360   yyParser *yypParser,    /* The parser */
 109361   YYCODETYPE yymajor,     /* Type code for object to destroy */
 109362   YYMINORTYPE *yypminor   /* The object to be destroyed */
 109364   sqlite3ParserARG_FETCH;
 109365   switch( yymajor ){
 109366     /* Here is inserted the actions which take place when a
 109367     ** terminal or non-terminal is destroyed.  This can happen
 109368     ** when the symbol is popped from the stack during a
 109369     ** reduce or during error processing or when a parser is 
 109370     ** being destroyed before it is finished parsing.
 109372     ** Note: during a reduce, the only symbols destroyed are those
 109373     ** which appear on the RHS of the rule, but which are not used
 109374     ** inside the C code.
 109376     case 160: /* select */
 109377     case 194: /* oneselect */
 109379 sqlite3SelectDelete(pParse->db, (yypminor->yy159));
 109381       break;
 109382     case 173: /* term */
 109383     case 174: /* expr */
 109385 sqlite3ExprDelete(pParse->db, (yypminor->yy342).pExpr);
 109387       break;
 109388     case 178: /* idxlist_opt */
 109389     case 187: /* idxlist */
 109390     case 197: /* selcollist */
 109391     case 200: /* groupby_opt */
 109392     case 202: /* orderby_opt */
 109393     case 204: /* sclp */
 109394     case 214: /* sortlist */
 109395     case 215: /* nexprlist */
 109396     case 216: /* setlist */
 109397     case 220: /* exprlist */
 109398     case 225: /* case_exprlist */
 109400 sqlite3ExprListDelete(pParse->db, (yypminor->yy442));
 109402       break;
 109403     case 193: /* fullname */
 109404     case 198: /* from */
 109405     case 206: /* seltablist */
 109406     case 207: /* stl_prefix */
 109408 sqlite3SrcListDelete(pParse->db, (yypminor->yy347));
 109410       break;
 109411     case 199: /* where_opt */
 109412     case 201: /* having_opt */
 109413     case 210: /* on_opt */
 109414     case 224: /* case_operand */
 109415     case 226: /* case_else */
 109416     case 236: /* when_clause */
 109417     case 241: /* key_opt */
 109419 sqlite3ExprDelete(pParse->db, (yypminor->yy122));
 109421       break;
 109422     case 211: /* using_opt */
 109423     case 213: /* inscollist */
 109424     case 218: /* inscollist_opt */
 109426 sqlite3IdListDelete(pParse->db, (yypminor->yy180));
 109428       break;
 109429     case 219: /* valuelist */
 109432   sqlite3ExprListDelete(pParse->db, (yypminor->yy487).pList);
 109433   sqlite3SelectDelete(pParse->db, (yypminor->yy487).pSelect);
 109436       break;
 109437     case 232: /* trigger_cmd_list */
 109438     case 237: /* trigger_cmd */
 109440 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy327));
 109442       break;
 109443     case 234: /* trigger_event */
 109445 sqlite3IdListDelete(pParse->db, (yypminor->yy410).b);
 109447       break;
 109448     default:  break;   /* If no destructor action specified: do nothing */
 109453 ** Pop the parser's stack once.
 109455 ** If there is a destructor routine associated with the token which
 109456 ** is popped from the stack, then call it.
 109458 ** Return the major token number for the symbol popped.
 109460 static int yy_pop_parser_stack(yyParser *pParser){
 109461   YYCODETYPE yymajor;
 109462   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
 109464   /* There is no mechanism by which the parser stack can be popped below
 109465   ** empty in SQLite.  */
 109466   if( NEVER(pParser->yyidx<0) ) return 0;
 109467 #ifndef NDEBUG
 109468   if( yyTraceFILE && pParser->yyidx>=0 ){
 109469     fprintf(yyTraceFILE,"%sPopping %s\n",
 109470       yyTracePrompt,
 109471       yyTokenName[yytos->major]);
 109473 #endif
 109474   yymajor = yytos->major;
 109475   yy_destructor(pParser, yymajor, &yytos->minor);
 109476   pParser->yyidx--;
 109477   return yymajor;
 109481 ** Deallocate and destroy a parser.  Destructors are all called for
 109482 ** all stack elements before shutting the parser down.
 109484 ** Inputs:
 109485 ** <ul>
 109486 ** <li>  A pointer to the parser.  This should be a pointer
 109487 **       obtained from sqlite3ParserAlloc.
 109488 ** <li>  A pointer to a function used to reclaim memory obtained
 109489 **       from malloc.
 109490 ** </ul>
 109492 SQLITE_PRIVATE void sqlite3ParserFree(
 109493   void *p,                    /* The parser to be deleted */
 109494   void (*freeProc)(void*)     /* Function used to reclaim memory */
 109496   yyParser *pParser = (yyParser*)p;
 109497   /* In SQLite, we never try to destroy a parser that was not successfully
 109498   ** created in the first place. */
 109499   if( NEVER(pParser==0) ) return;
 109500   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
 109501 #if YYSTACKDEPTH<=0
 109502   free(pParser->yystack);
 109503 #endif
 109504   (*freeProc)((void*)pParser);
 109508 ** Return the peak depth of the stack for a parser.
 109510 #ifdef YYTRACKMAXSTACKDEPTH
 109511 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
 109512   yyParser *pParser = (yyParser*)p;
 109513   return pParser->yyidxMax;
 109515 #endif
 109518 ** Find the appropriate action for a parser given the terminal
 109519 ** look-ahead token iLookAhead.
 109521 ** If the look-ahead token is YYNOCODE, then check to see if the action is
 109522 ** independent of the look-ahead.  If it is, return the action, otherwise
 109523 ** return YY_NO_ACTION.
 109525 static int yy_find_shift_action(
 109526   yyParser *pParser,        /* The parser */
 109527   YYCODETYPE iLookAhead     /* The look-ahead token */
 109529   int i;
 109530   int stateno = pParser->yystack[pParser->yyidx].stateno;
 109532   if( stateno>YY_SHIFT_COUNT
 109533    || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
 109534     return yy_default[stateno];
 109536   assert( iLookAhead!=YYNOCODE );
 109537   i += iLookAhead;
 109538   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
 109539     if( iLookAhead>0 ){
 109540 #ifdef YYFALLBACK
 109541       YYCODETYPE iFallback;            /* Fallback token */
 109542       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
 109543              && (iFallback = yyFallback[iLookAhead])!=0 ){
 109544 #ifndef NDEBUG
 109545         if( yyTraceFILE ){
 109546           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
 109547              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
 109549 #endif
 109550         return yy_find_shift_action(pParser, iFallback);
 109552 #endif
 109553 #ifdef YYWILDCARD
 109555         int j = i - iLookAhead + YYWILDCARD;
 109556         if( 
 109557 #if YY_SHIFT_MIN+YYWILDCARD<0
 109558           j>=0 &&
 109559 #endif
 109560 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
 109561           j<YY_ACTTAB_COUNT &&
 109562 #endif
 109563           yy_lookahead[j]==YYWILDCARD
 109565 #ifndef NDEBUG
 109566           if( yyTraceFILE ){
 109567             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
 109568                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
 109570 #endif /* NDEBUG */
 109571           return yy_action[j];
 109574 #endif /* YYWILDCARD */
 109576     return yy_default[stateno];
 109577   }else{
 109578     return yy_action[i];
 109583 ** Find the appropriate action for a parser given the non-terminal
 109584 ** look-ahead token iLookAhead.
 109586 ** If the look-ahead token is YYNOCODE, then check to see if the action is
 109587 ** independent of the look-ahead.  If it is, return the action, otherwise
 109588 ** return YY_NO_ACTION.
 109590 static int yy_find_reduce_action(
 109591   int stateno,              /* Current state number */
 109592   YYCODETYPE iLookAhead     /* The look-ahead token */
 109594   int i;
 109595 #ifdef YYERRORSYMBOL
 109596   if( stateno>YY_REDUCE_COUNT ){
 109597     return yy_default[stateno];
 109599 #else
 109600   assert( stateno<=YY_REDUCE_COUNT );
 109601 #endif
 109602   i = yy_reduce_ofst[stateno];
 109603   assert( i!=YY_REDUCE_USE_DFLT );
 109604   assert( iLookAhead!=YYNOCODE );
 109605   i += iLookAhead;
 109606 #ifdef YYERRORSYMBOL
 109607   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
 109608     return yy_default[stateno];
 109610 #else
 109611   assert( i>=0 && i<YY_ACTTAB_COUNT );
 109612   assert( yy_lookahead[i]==iLookAhead );
 109613 #endif
 109614   return yy_action[i];
 109618 ** The following routine is called if the stack overflows.
 109620 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
 109621    sqlite3ParserARG_FETCH;
 109622    yypParser->yyidx--;
 109623 #ifndef NDEBUG
 109624    if( yyTraceFILE ){
 109625      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
 109627 #endif
 109628    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
 109629    /* Here code is inserted which will execute if the parser
 109630    ** stack every overflows */
 109632   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
 109633   sqlite3ErrorMsg(pParse, "parser stack overflow");
 109634    sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
 109638 ** Perform a shift action.
 109640 static void yy_shift(
 109641   yyParser *yypParser,          /* The parser to be shifted */
 109642   int yyNewState,               /* The new state to shift in */
 109643   int yyMajor,                  /* The major token to shift in */
 109644   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
 109646   yyStackEntry *yytos;
 109647   yypParser->yyidx++;
 109648 #ifdef YYTRACKMAXSTACKDEPTH
 109649   if( yypParser->yyidx>yypParser->yyidxMax ){
 109650     yypParser->yyidxMax = yypParser->yyidx;
 109652 #endif
 109653 #if YYSTACKDEPTH>0 
 109654   if( yypParser->yyidx>=YYSTACKDEPTH ){
 109655     yyStackOverflow(yypParser, yypMinor);
 109656     return;
 109658 #else
 109659   if( yypParser->yyidx>=yypParser->yystksz ){
 109660     yyGrowStack(yypParser);
 109661     if( yypParser->yyidx>=yypParser->yystksz ){
 109662       yyStackOverflow(yypParser, yypMinor);
 109663       return;
 109666 #endif
 109667   yytos = &yypParser->yystack[yypParser->yyidx];
 109668   yytos->stateno = (YYACTIONTYPE)yyNewState;
 109669   yytos->major = (YYCODETYPE)yyMajor;
 109670   yytos->minor = *yypMinor;
 109671 #ifndef NDEBUG
 109672   if( yyTraceFILE && yypParser->yyidx>0 ){
 109673     int i;
 109674     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
 109675     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
 109676     for(i=1; i<=yypParser->yyidx; i++)
 109677       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
 109678     fprintf(yyTraceFILE,"\n");
 109680 #endif
 109683 /* The following table contains information about every rule that
 109684 ** is used during the reduce.
 109686 static const struct {
 109687   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
 109688   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
 109689 } yyRuleInfo[] = {
 109690   { 142, 1 },
 109691   { 143, 2 },
 109692   { 143, 1 },
 109693   { 144, 1 },
 109694   { 144, 3 },
 109695   { 145, 0 },
 109696   { 145, 1 },
 109697   { 145, 3 },
 109698   { 146, 1 },
 109699   { 147, 3 },
 109700   { 149, 0 },
 109701   { 149, 1 },
 109702   { 149, 2 },
 109703   { 148, 0 },
 109704   { 148, 1 },
 109705   { 148, 1 },
 109706   { 148, 1 },
 109707   { 147, 2 },
 109708   { 147, 2 },
 109709   { 147, 2 },
 109710   { 151, 1 },
 109711   { 151, 0 },
 109712   { 147, 2 },
 109713   { 147, 3 },
 109714   { 147, 5 },
 109715   { 147, 2 },
 109716   { 152, 6 },
 109717   { 154, 1 },
 109718   { 156, 0 },
 109719   { 156, 3 },
 109720   { 155, 1 },
 109721   { 155, 0 },
 109722   { 153, 4 },
 109723   { 153, 2 },
 109724   { 158, 3 },
 109725   { 158, 1 },
 109726   { 161, 3 },
 109727   { 162, 1 },
 109728   { 165, 1 },
 109729   { 165, 1 },
 109730   { 166, 1 },
 109731   { 150, 1 },
 109732   { 150, 1 },
 109733   { 150, 1 },
 109734   { 163, 0 },
 109735   { 163, 1 },
 109736   { 167, 1 },
 109737   { 167, 4 },
 109738   { 167, 6 },
 109739   { 168, 1 },
 109740   { 168, 2 },
 109741   { 169, 1 },
 109742   { 169, 1 },
 109743   { 164, 2 },
 109744   { 164, 0 },
 109745   { 172, 2 },
 109746   { 172, 2 },
 109747   { 172, 4 },
 109748   { 172, 3 },
 109749   { 172, 3 },
 109750   { 172, 2 },
 109751   { 172, 2 },
 109752   { 172, 3 },
 109753   { 172, 5 },
 109754   { 172, 2 },
 109755   { 172, 4 },
 109756   { 172, 4 },
 109757   { 172, 1 },
 109758   { 172, 2 },
 109759   { 177, 0 },
 109760   { 177, 1 },
 109761   { 179, 0 },
 109762   { 179, 2 },
 109763   { 181, 2 },
 109764   { 181, 3 },
 109765   { 181, 3 },
 109766   { 181, 3 },
 109767   { 182, 2 },
 109768   { 182, 2 },
 109769   { 182, 1 },
 109770   { 182, 1 },
 109771   { 182, 2 },
 109772   { 180, 3 },
 109773   { 180, 2 },
 109774   { 183, 0 },
 109775   { 183, 2 },
 109776   { 183, 2 },
 109777   { 159, 0 },
 109778   { 159, 2 },
 109779   { 184, 3 },
 109780   { 184, 1 },
 109781   { 185, 1 },
 109782   { 185, 0 },
 109783   { 186, 2 },
 109784   { 186, 7 },
 109785   { 186, 5 },
 109786   { 186, 5 },
 109787   { 186, 10 },
 109788   { 188, 0 },
 109789   { 188, 1 },
 109790   { 175, 0 },
 109791   { 175, 3 },
 109792   { 189, 0 },
 109793   { 189, 2 },
 109794   { 190, 1 },
 109795   { 190, 1 },
 109796   { 190, 1 },
 109797   { 147, 4 },
 109798   { 192, 2 },
 109799   { 192, 0 },
 109800   { 147, 8 },
 109801   { 147, 4 },
 109802   { 147, 1 },
 109803   { 160, 1 },
 109804   { 160, 3 },
 109805   { 195, 1 },
 109806   { 195, 2 },
 109807   { 195, 1 },
 109808   { 194, 9 },
 109809   { 196, 1 },
 109810   { 196, 1 },
 109811   { 196, 0 },
 109812   { 204, 2 },
 109813   { 204, 0 },
 109814   { 197, 3 },
 109815   { 197, 2 },
 109816   { 197, 4 },
 109817   { 205, 2 },
 109818   { 205, 1 },
 109819   { 205, 0 },
 109820   { 198, 0 },
 109821   { 198, 2 },
 109822   { 207, 2 },
 109823   { 207, 0 },
 109824   { 206, 7 },
 109825   { 206, 7 },
 109826   { 206, 7 },
 109827   { 157, 0 },
 109828   { 157, 2 },
 109829   { 193, 2 },
 109830   { 208, 1 },
 109831   { 208, 2 },
 109832   { 208, 3 },
 109833   { 208, 4 },
 109834   { 210, 2 },
 109835   { 210, 0 },
 109836   { 209, 0 },
 109837   { 209, 3 },
 109838   { 209, 2 },
 109839   { 211, 4 },
 109840   { 211, 0 },
 109841   { 202, 0 },
 109842   { 202, 3 },
 109843   { 214, 4 },
 109844   { 214, 2 },
 109845   { 176, 1 },
 109846   { 176, 1 },
 109847   { 176, 0 },
 109848   { 200, 0 },
 109849   { 200, 3 },
 109850   { 201, 0 },
 109851   { 201, 2 },
 109852   { 203, 0 },
 109853   { 203, 2 },
 109854   { 203, 4 },
 109855   { 203, 4 },
 109856   { 147, 5 },
 109857   { 199, 0 },
 109858   { 199, 2 },
 109859   { 147, 7 },
 109860   { 216, 5 },
 109861   { 216, 3 },
 109862   { 147, 5 },
 109863   { 147, 5 },
 109864   { 147, 6 },
 109865   { 217, 2 },
 109866   { 217, 1 },
 109867   { 219, 4 },
 109868   { 219, 5 },
 109869   { 218, 0 },
 109870   { 218, 3 },
 109871   { 213, 3 },
 109872   { 213, 1 },
 109873   { 174, 1 },
 109874   { 174, 3 },
 109875   { 173, 1 },
 109876   { 174, 1 },
 109877   { 174, 1 },
 109878   { 174, 3 },
 109879   { 174, 5 },
 109880   { 173, 1 },
 109881   { 173, 1 },
 109882   { 174, 1 },
 109883   { 174, 1 },
 109884   { 174, 3 },
 109885   { 174, 6 },
 109886   { 174, 5 },
 109887   { 174, 4 },
 109888   { 173, 1 },
 109889   { 174, 3 },
 109890   { 174, 3 },
 109891   { 174, 3 },
 109892   { 174, 3 },
 109893   { 174, 3 },
 109894   { 174, 3 },
 109895   { 174, 3 },
 109896   { 174, 3 },
 109897   { 221, 1 },
 109898   { 221, 2 },
 109899   { 221, 1 },
 109900   { 221, 2 },
 109901   { 174, 3 },
 109902   { 174, 5 },
 109903   { 174, 2 },
 109904   { 174, 3 },
 109905   { 174, 3 },
 109906   { 174, 4 },
 109907   { 174, 2 },
 109908   { 174, 2 },
 109909   { 174, 2 },
 109910   { 174, 2 },
 109911   { 222, 1 },
 109912   { 222, 2 },
 109913   { 174, 5 },
 109914   { 223, 1 },
 109915   { 223, 2 },
 109916   { 174, 5 },
 109917   { 174, 3 },
 109918   { 174, 5 },
 109919   { 174, 4 },
 109920   { 174, 4 },
 109921   { 174, 5 },
 109922   { 225, 5 },
 109923   { 225, 4 },
 109924   { 226, 2 },
 109925   { 226, 0 },
 109926   { 224, 1 },
 109927   { 224, 0 },
 109928   { 220, 1 },
 109929   { 220, 0 },
 109930   { 215, 3 },
 109931   { 215, 1 },
 109932   { 147, 11 },
 109933   { 227, 1 },
 109934   { 227, 0 },
 109935   { 178, 0 },
 109936   { 178, 3 },
 109937   { 187, 5 },
 109938   { 187, 3 },
 109939   { 228, 0 },
 109940   { 228, 2 },
 109941   { 147, 4 },
 109942   { 147, 1 },
 109943   { 147, 2 },
 109944   { 147, 3 },
 109945   { 147, 5 },
 109946   { 147, 6 },
 109947   { 147, 5 },
 109948   { 147, 6 },
 109949   { 229, 1 },
 109950   { 229, 1 },
 109951   { 229, 1 },
 109952   { 229, 1 },
 109953   { 229, 1 },
 109954   { 170, 2 },
 109955   { 170, 1 },
 109956   { 171, 2 },
 109957   { 230, 1 },
 109958   { 147, 5 },
 109959   { 231, 11 },
 109960   { 233, 1 },
 109961   { 233, 1 },
 109962   { 233, 2 },
 109963   { 233, 0 },
 109964   { 234, 1 },
 109965   { 234, 1 },
 109966   { 234, 3 },
 109967   { 235, 0 },
 109968   { 235, 3 },
 109969   { 236, 0 },
 109970   { 236, 2 },
 109971   { 232, 3 },
 109972   { 232, 2 },
 109973   { 238, 1 },
 109974   { 238, 3 },
 109975   { 239, 0 },
 109976   { 239, 3 },
 109977   { 239, 2 },
 109978   { 237, 7 },
 109979   { 237, 5 },
 109980   { 237, 5 },
 109981   { 237, 5 },
 109982   { 237, 1 },
 109983   { 174, 4 },
 109984   { 174, 6 },
 109985   { 191, 1 },
 109986   { 191, 1 },
 109987   { 191, 1 },
 109988   { 147, 4 },
 109989   { 147, 6 },
 109990   { 147, 3 },
 109991   { 241, 0 },
 109992   { 241, 2 },
 109993   { 240, 1 },
 109994   { 240, 0 },
 109995   { 147, 1 },
 109996   { 147, 3 },
 109997   { 147, 1 },
 109998   { 147, 3 },
 109999   { 147, 6 },
 110000   { 147, 6 },
 110001   { 242, 1 },
 110002   { 243, 0 },
 110003   { 243, 1 },
 110004   { 147, 1 },
 110005   { 147, 4 },
 110006   { 244, 8 },
 110007   { 245, 1 },
 110008   { 245, 3 },
 110009   { 246, 0 },
 110010   { 246, 2 },
 110011   { 247, 1 },
 110012   { 247, 3 },
 110013   { 248, 1 },
 110014   { 249, 0 },
 110015   { 249, 4 },
 110016   { 249, 2 },
 110019 static void yy_accept(yyParser*);  /* Forward Declaration */
 110022 ** Perform a reduce action and the shift that must immediately
 110023 ** follow the reduce.
 110025 static void yy_reduce(
 110026   yyParser *yypParser,         /* The parser */
 110027   int yyruleno                 /* Number of the rule by which to reduce */
 110029   int yygoto;                     /* The next state */
 110030   int yyact;                      /* The next action */
 110031   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
 110032   yyStackEntry *yymsp;            /* The top of the parser's stack */
 110033   int yysize;                     /* Amount to pop the stack */
 110034   sqlite3ParserARG_FETCH;
 110035   yymsp = &yypParser->yystack[yypParser->yyidx];
 110036 #ifndef NDEBUG
 110037   if( yyTraceFILE && yyruleno>=0 
 110038         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
 110039     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
 110040       yyRuleName[yyruleno]);
 110042 #endif /* NDEBUG */
 110044   /* Silence complaints from purify about yygotominor being uninitialized
 110045   ** in some cases when it is copied into the stack after the following
 110046   ** switch.  yygotominor is uninitialized when a rule reduces that does
 110047   ** not set the value of its left-hand side nonterminal.  Leaving the
 110048   ** value of the nonterminal uninitialized is utterly harmless as long
 110049   ** as the value is never used.  So really the only thing this code
 110050   ** accomplishes is to quieten purify.  
 110052   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
 110053   ** without this code, their parser segfaults.  I'm not sure what there
 110054   ** parser is doing to make this happen.  This is the second bug report
 110055   ** from wireshark this week.  Clearly they are stressing Lemon in ways
 110056   ** that it has not been previously stressed...  (SQLite ticket #2172)
 110058   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
 110059   yygotominor = yyzerominor;
 110062   switch( yyruleno ){
 110063   /* Beginning here are the reduction cases.  A typical example
 110064   ** follows:
 110065   **   case 0:
 110066   **  #line <lineno> <grammarfile>
 110067   **     { ... }           // User supplied code
 110068   **  #line <lineno> <thisfile>
 110069   **     break;
 110071       case 5: /* explain ::= */
 110072 { sqlite3BeginParse(pParse, 0); }
 110073         break;
 110074       case 6: /* explain ::= EXPLAIN */
 110075 { sqlite3BeginParse(pParse, 1); }
 110076         break;
 110077       case 7: /* explain ::= EXPLAIN QUERY PLAN */
 110078 { sqlite3BeginParse(pParse, 2); }
 110079         break;
 110080       case 8: /* cmdx ::= cmd */
 110081 { sqlite3FinishCoding(pParse); }
 110082         break;
 110083       case 9: /* cmd ::= BEGIN transtype trans_opt */
 110084 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy392);}
 110085         break;
 110086       case 13: /* transtype ::= */
 110087 {yygotominor.yy392 = TK_DEFERRED;}
 110088         break;
 110089       case 14: /* transtype ::= DEFERRED */
 110090       case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
 110091       case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
 110092       case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
 110093       case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
 110094 {yygotominor.yy392 = yymsp[0].major;}
 110095         break;
 110096       case 17: /* cmd ::= COMMIT trans_opt */
 110097       case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
 110098 {sqlite3CommitTransaction(pParse);}
 110099         break;
 110100       case 19: /* cmd ::= ROLLBACK trans_opt */
 110101 {sqlite3RollbackTransaction(pParse);}
 110102         break;
 110103       case 22: /* cmd ::= SAVEPOINT nm */
 110105   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
 110107         break;
 110108       case 23: /* cmd ::= RELEASE savepoint_opt nm */
 110110   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
 110112         break;
 110113       case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
 110115   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
 110117         break;
 110118       case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
 110120    sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy392,0,0,yymsp[-2].minor.yy392);
 110122         break;
 110123       case 27: /* createkw ::= CREATE */
 110125   pParse->db->lookaside.bEnabled = 0;
 110126   yygotominor.yy0 = yymsp[0].minor.yy0;
 110128         break;
 110129       case 28: /* ifnotexists ::= */
 110130       case 31: /* temp ::= */ yytestcase(yyruleno==31);
 110131       case 69: /* autoinc ::= */ yytestcase(yyruleno==69);
 110132       case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==82);
 110133       case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
 110134       case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
 110135       case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
 110136       case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
 110137       case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
 110138       case 121: /* distinct ::= */ yytestcase(yyruleno==121);
 110139       case 221: /* between_op ::= BETWEEN */ yytestcase(yyruleno==221);
 110140       case 224: /* in_op ::= IN */ yytestcase(yyruleno==224);
 110141 {yygotominor.yy392 = 0;}
 110142         break;
 110143       case 29: /* ifnotexists ::= IF NOT EXISTS */
 110144       case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
 110145       case 70: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==70);
 110146       case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
 110147       case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
 110148       case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
 110149       case 222: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==222);
 110150       case 225: /* in_op ::= NOT IN */ yytestcase(yyruleno==225);
 110151 {yygotominor.yy392 = 1;}
 110152         break;
 110153       case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
 110155   sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
 110157         break;
 110158       case 33: /* create_table_args ::= AS select */
 110160   sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy159);
 110161   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy159);
 110163         break;
 110164       case 36: /* column ::= columnid type carglist */
 110166   yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
 110167   yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
 110169         break;
 110170       case 37: /* columnid ::= nm */
 110172   sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
 110173   yygotominor.yy0 = yymsp[0].minor.yy0;
 110174   pParse->constraintName.n = 0;
 110176         break;
 110177       case 38: /* id ::= ID */
 110178       case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
 110179       case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
 110180       case 41: /* nm ::= id */ yytestcase(yyruleno==41);
 110181       case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
 110182       case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
 110183       case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
 110184       case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
 110185       case 127: /* as ::= AS nm */ yytestcase(yyruleno==127);
 110186       case 128: /* as ::= ids */ yytestcase(yyruleno==128);
 110187       case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138);
 110188       case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147);
 110189       case 250: /* collate ::= COLLATE ids */ yytestcase(yyruleno==250);
 110190       case 259: /* nmnum ::= plus_num */ yytestcase(yyruleno==259);
 110191       case 260: /* nmnum ::= nm */ yytestcase(yyruleno==260);
 110192       case 261: /* nmnum ::= ON */ yytestcase(yyruleno==261);
 110193       case 262: /* nmnum ::= DELETE */ yytestcase(yyruleno==262);
 110194       case 263: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==263);
 110195       case 264: /* plus_num ::= PLUS number */ yytestcase(yyruleno==264);
 110196       case 265: /* plus_num ::= number */ yytestcase(yyruleno==265);
 110197       case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
 110198       case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
 110199       case 283: /* trnm ::= nm */ yytestcase(yyruleno==283);
 110200 {yygotominor.yy0 = yymsp[0].minor.yy0;}
 110201         break;
 110202       case 45: /* type ::= typetoken */
 110203 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
 110204         break;
 110205       case 47: /* typetoken ::= typename LP signed RP */
 110207   yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
 110208   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
 110210         break;
 110211       case 48: /* typetoken ::= typename LP signed COMMA signed RP */
 110213   yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
 110214   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
 110216         break;
 110217       case 50: /* typename ::= typename ids */
 110218 {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);}
 110219         break;
 110220       case 55: /* ccons ::= CONSTRAINT nm */
 110221       case 93: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
 110222 {pParse->constraintName = yymsp[0].minor.yy0;}
 110223         break;
 110224       case 56: /* ccons ::= DEFAULT term */
 110225       case 58: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==58);
 110226 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy342);}
 110227         break;
 110228       case 57: /* ccons ::= DEFAULT LP expr RP */
 110229 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy342);}
 110230         break;
 110231       case 59: /* ccons ::= DEFAULT MINUS term */
 110233   ExprSpan v;
 110234   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy342.pExpr, 0, 0);
 110235   v.zStart = yymsp[-1].minor.yy0.z;
 110236   v.zEnd = yymsp[0].minor.yy342.zEnd;
 110237   sqlite3AddDefaultValue(pParse,&v);
 110239         break;
 110240       case 60: /* ccons ::= DEFAULT id */
 110242   ExprSpan v;
 110243   spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
 110244   sqlite3AddDefaultValue(pParse,&v);
 110246         break;
 110247       case 62: /* ccons ::= NOT NULL onconf */
 110248 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy392);}
 110249         break;
 110250       case 63: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
 110251 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy392,yymsp[0].minor.yy392,yymsp[-2].minor.yy392);}
 110252         break;
 110253       case 64: /* ccons ::= UNIQUE onconf */
 110254 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy392,0,0,0,0);}
 110255         break;
 110256       case 65: /* ccons ::= CHECK LP expr RP */
 110257 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy342.pExpr);}
 110258         break;
 110259       case 66: /* ccons ::= REFERENCES nm idxlist_opt refargs */
 110260 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy442,yymsp[0].minor.yy392);}
 110261         break;
 110262       case 67: /* ccons ::= defer_subclause */
 110263 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy392);}
 110264         break;
 110265       case 68: /* ccons ::= COLLATE ids */
 110266 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
 110267         break;
 110268       case 71: /* refargs ::= */
 110269 { yygotominor.yy392 = OE_None*0x0101; /* EV: R-19803-45884 */}
 110270         break;
 110271       case 72: /* refargs ::= refargs refarg */
 110272 { yygotominor.yy392 = (yymsp[-1].minor.yy392 & ~yymsp[0].minor.yy207.mask) | yymsp[0].minor.yy207.value; }
 110273         break;
 110274       case 73: /* refarg ::= MATCH nm */
 110275       case 74: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==74);
 110276 { yygotominor.yy207.value = 0;     yygotominor.yy207.mask = 0x000000; }
 110277         break;
 110278       case 75: /* refarg ::= ON DELETE refact */
 110279 { yygotominor.yy207.value = yymsp[0].minor.yy392;     yygotominor.yy207.mask = 0x0000ff; }
 110280         break;
 110281       case 76: /* refarg ::= ON UPDATE refact */
 110282 { yygotominor.yy207.value = yymsp[0].minor.yy392<<8;  yygotominor.yy207.mask = 0x00ff00; }
 110283         break;
 110284       case 77: /* refact ::= SET NULL */
 110285 { yygotominor.yy392 = OE_SetNull;  /* EV: R-33326-45252 */}
 110286         break;
 110287       case 78: /* refact ::= SET DEFAULT */
 110288 { yygotominor.yy392 = OE_SetDflt;  /* EV: R-33326-45252 */}
 110289         break;
 110290       case 79: /* refact ::= CASCADE */
 110291 { yygotominor.yy392 = OE_Cascade;  /* EV: R-33326-45252 */}
 110292         break;
 110293       case 80: /* refact ::= RESTRICT */
 110294 { yygotominor.yy392 = OE_Restrict; /* EV: R-33326-45252 */}
 110295         break;
 110296       case 81: /* refact ::= NO ACTION */
 110297 { yygotominor.yy392 = OE_None;     /* EV: R-33326-45252 */}
 110298         break;
 110299       case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
 110300       case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
 110301       case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
 110302       case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
 110303 {yygotominor.yy392 = yymsp[0].minor.yy392;}
 110304         break;
 110305       case 87: /* conslist_opt ::= */
 110306 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
 110307         break;
 110308       case 88: /* conslist_opt ::= COMMA conslist */
 110309 {yygotominor.yy0 = yymsp[-1].minor.yy0;}
 110310         break;
 110311       case 91: /* tconscomma ::= COMMA */
 110312 {pParse->constraintName.n = 0;}
 110313         break;
 110314       case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
 110315 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy442,yymsp[0].minor.yy392,yymsp[-2].minor.yy392,0);}
 110316         break;
 110317       case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
 110318 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy442,yymsp[0].minor.yy392,0,0,0,0);}
 110319         break;
 110320       case 96: /* tcons ::= CHECK LP expr RP onconf */
 110321 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy342.pExpr);}
 110322         break;
 110323       case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
 110325     sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy442, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy442, yymsp[-1].minor.yy392);
 110326     sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy392);
 110328         break;
 110329       case 100: /* onconf ::= */
 110330 {yygotominor.yy392 = OE_Default;}
 110331         break;
 110332       case 102: /* orconf ::= */
 110333 {yygotominor.yy258 = OE_Default;}
 110334         break;
 110335       case 103: /* orconf ::= OR resolvetype */
 110336 {yygotominor.yy258 = (u8)yymsp[0].minor.yy392;}
 110337         break;
 110338       case 105: /* resolvetype ::= IGNORE */
 110339 {yygotominor.yy392 = OE_Ignore;}
 110340         break;
 110341       case 106: /* resolvetype ::= REPLACE */
 110342 {yygotominor.yy392 = OE_Replace;}
 110343         break;
 110344       case 107: /* cmd ::= DROP TABLE ifexists fullname */
 110346   sqlite3DropTable(pParse, yymsp[0].minor.yy347, 0, yymsp[-1].minor.yy392);
 110348         break;
 110349       case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
 110351   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);
 110353         break;
 110354       case 111: /* cmd ::= DROP VIEW ifexists fullname */
 110356   sqlite3DropTable(pParse, yymsp[0].minor.yy347, 1, yymsp[-1].minor.yy392);
 110358         break;
 110359       case 112: /* cmd ::= select */
 110361   SelectDest dest = {SRT_Output, 0, 0, 0, 0};
 110362   sqlite3Select(pParse, yymsp[0].minor.yy159, &dest);
 110363   sqlite3ExplainBegin(pParse->pVdbe);
 110364   sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy159);
 110365   sqlite3ExplainFinish(pParse->pVdbe);
 110366   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy159);
 110368         break;
 110369       case 113: /* select ::= oneselect */
 110370 {yygotominor.yy159 = yymsp[0].minor.yy159;}
 110371         break;
 110372       case 114: /* select ::= select multiselect_op oneselect */
 110374   if( yymsp[0].minor.yy159 ){
 110375     yymsp[0].minor.yy159->op = (u8)yymsp[-1].minor.yy392;
 110376     yymsp[0].minor.yy159->pPrior = yymsp[-2].minor.yy159;
 110377   }else{
 110378     sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy159);
 110380   yygotominor.yy159 = yymsp[0].minor.yy159;
 110382         break;
 110383       case 116: /* multiselect_op ::= UNION ALL */
 110384 {yygotominor.yy392 = TK_ALL;}
 110385         break;
 110386       case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
 110388   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);
 110390         break;
 110391       case 122: /* sclp ::= selcollist COMMA */
 110392       case 246: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==246);
 110393 {yygotominor.yy442 = yymsp[-1].minor.yy442;}
 110394         break;
 110395       case 123: /* sclp ::= */
 110396       case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
 110397       case 158: /* groupby_opt ::= */ yytestcase(yyruleno==158);
 110398       case 239: /* exprlist ::= */ yytestcase(yyruleno==239);
 110399       case 245: /* idxlist_opt ::= */ yytestcase(yyruleno==245);
 110400 {yygotominor.yy442 = 0;}
 110401         break;
 110402       case 124: /* selcollist ::= sclp expr as */
 110404    yygotominor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy442, yymsp[-1].minor.yy342.pExpr);
 110405    if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[0].minor.yy0, 1);
 110406    sqlite3ExprListSetSpan(pParse,yygotominor.yy442,&yymsp[-1].minor.yy342);
 110408         break;
 110409       case 125: /* selcollist ::= sclp STAR */
 110411   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
 110412   yygotominor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy442, p);
 110414         break;
 110415       case 126: /* selcollist ::= sclp nm DOT STAR */
 110417   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
 110418   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
 110419   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
 110420   yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy442, pDot);
 110422         break;
 110423       case 129: /* as ::= */
 110424 {yygotominor.yy0.n = 0;}
 110425         break;
 110426       case 130: /* from ::= */
 110427 {yygotominor.yy347 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy347));}
 110428         break;
 110429       case 131: /* from ::= FROM seltablist */
 110431   yygotominor.yy347 = yymsp[0].minor.yy347;
 110432   sqlite3SrcListShiftJoinType(yygotominor.yy347);
 110434         break;
 110435       case 132: /* stl_prefix ::= seltablist joinop */
 110437    yygotominor.yy347 = yymsp[-1].minor.yy347;
 110438    if( ALWAYS(yygotominor.yy347 && yygotominor.yy347->nSrc>0) ) yygotominor.yy347->a[yygotominor.yy347->nSrc-1].jointype = (u8)yymsp[0].minor.yy392;
 110440         break;
 110441       case 133: /* stl_prefix ::= */
 110442 {yygotominor.yy347 = 0;}
 110443         break;
 110444       case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
 110446   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);
 110447   sqlite3SrcListIndexedBy(pParse, yygotominor.yy347, &yymsp[-2].minor.yy0);
 110449         break;
 110450       case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
 110452     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);
 110454         break;
 110455       case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
 110457     if( yymsp[-6].minor.yy347==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy122==0 && yymsp[0].minor.yy180==0 ){
 110458       yygotominor.yy347 = yymsp[-4].minor.yy347;
 110459     }else{
 110460       Select *pSubquery;
 110461       sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy347);
 110462       pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy347,0,0,0,0,0,0,0);
 110463       yygotominor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy122,yymsp[0].minor.yy180);
 110466         break;
 110467       case 137: /* dbnm ::= */
 110468       case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
 110469 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
 110470         break;
 110471       case 139: /* fullname ::= nm dbnm */
 110472 {yygotominor.yy347 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
 110473         break;
 110474       case 140: /* joinop ::= COMMA|JOIN */
 110475 { yygotominor.yy392 = JT_INNER; }
 110476         break;
 110477       case 141: /* joinop ::= JOIN_KW JOIN */
 110478 { yygotominor.yy392 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
 110479         break;
 110480       case 142: /* joinop ::= JOIN_KW nm JOIN */
 110481 { yygotominor.yy392 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
 110482         break;
 110483       case 143: /* joinop ::= JOIN_KW nm nm JOIN */
 110484 { yygotominor.yy392 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
 110485         break;
 110486       case 144: /* on_opt ::= ON expr */
 110487       case 161: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==161);
 110488       case 168: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==168);
 110489       case 234: /* case_else ::= ELSE expr */ yytestcase(yyruleno==234);
 110490       case 236: /* case_operand ::= expr */ yytestcase(yyruleno==236);
 110491 {yygotominor.yy122 = yymsp[0].minor.yy342.pExpr;}
 110492         break;
 110493       case 145: /* on_opt ::= */
 110494       case 160: /* having_opt ::= */ yytestcase(yyruleno==160);
 110495       case 167: /* where_opt ::= */ yytestcase(yyruleno==167);
 110496       case 235: /* case_else ::= */ yytestcase(yyruleno==235);
 110497       case 237: /* case_operand ::= */ yytestcase(yyruleno==237);
 110498 {yygotominor.yy122 = 0;}
 110499         break;
 110500       case 148: /* indexed_opt ::= NOT INDEXED */
 110501 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
 110502         break;
 110503       case 149: /* using_opt ::= USING LP inscollist RP */
 110504       case 180: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==180);
 110505 {yygotominor.yy180 = yymsp[-1].minor.yy180;}
 110506         break;
 110507       case 150: /* using_opt ::= */
 110508       case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
 110509 {yygotominor.yy180 = 0;}
 110510         break;
 110511       case 152: /* orderby_opt ::= ORDER BY sortlist */
 110512       case 159: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==159);
 110513       case 238: /* exprlist ::= nexprlist */ yytestcase(yyruleno==238);
 110514 {yygotominor.yy442 = yymsp[0].minor.yy442;}
 110515         break;
 110516       case 153: /* sortlist ::= sortlist COMMA expr sortorder */
 110518   yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy442,yymsp[-1].minor.yy342.pExpr);
 110519   if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
 110521         break;
 110522       case 154: /* sortlist ::= expr sortorder */
 110524   yygotominor.yy442 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy342.pExpr);
 110525   if( yygotominor.yy442 && ALWAYS(yygotominor.yy442->a) ) yygotominor.yy442->a[0].sortOrder = (u8)yymsp[0].minor.yy392;
 110527         break;
 110528       case 155: /* sortorder ::= ASC */
 110529       case 157: /* sortorder ::= */ yytestcase(yyruleno==157);
 110530 {yygotominor.yy392 = SQLITE_SO_ASC;}
 110531         break;
 110532       case 156: /* sortorder ::= DESC */
 110533 {yygotominor.yy392 = SQLITE_SO_DESC;}
 110534         break;
 110535       case 162: /* limit_opt ::= */
 110536 {yygotominor.yy64.pLimit = 0; yygotominor.yy64.pOffset = 0;}
 110537         break;
 110538       case 163: /* limit_opt ::= LIMIT expr */
 110539 {yygotominor.yy64.pLimit = yymsp[0].minor.yy342.pExpr; yygotominor.yy64.pOffset = 0;}
 110540         break;
 110541       case 164: /* limit_opt ::= LIMIT expr OFFSET expr */
 110542 {yygotominor.yy64.pLimit = yymsp[-2].minor.yy342.pExpr; yygotominor.yy64.pOffset = yymsp[0].minor.yy342.pExpr;}
 110543         break;
 110544       case 165: /* limit_opt ::= LIMIT expr COMMA expr */
 110545 {yygotominor.yy64.pOffset = yymsp[-2].minor.yy342.pExpr; yygotominor.yy64.pLimit = yymsp[0].minor.yy342.pExpr;}
 110546         break;
 110547       case 166: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
 110549   sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy347, &yymsp[-1].minor.yy0);
 110550   sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy347,yymsp[0].minor.yy122);
 110552         break;
 110553       case 169: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
 110555   sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy347, &yymsp[-3].minor.yy0);
 110556   sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy442,"set list"); 
 110557   sqlite3Update(pParse,yymsp[-4].minor.yy347,yymsp[-1].minor.yy442,yymsp[0].minor.yy122,yymsp[-5].minor.yy258);
 110559         break;
 110560       case 170: /* setlist ::= setlist COMMA nm EQ expr */
 110562   yygotominor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy442, yymsp[0].minor.yy342.pExpr);
 110563   sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
 110565         break;
 110566       case 171: /* setlist ::= nm EQ expr */
 110568   yygotominor.yy442 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy342.pExpr);
 110569   sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
 110571         break;
 110572       case 172: /* cmd ::= insert_cmd INTO fullname inscollist_opt valuelist */
 110573 {sqlite3Insert(pParse, yymsp[-2].minor.yy347, yymsp[0].minor.yy487.pList, yymsp[0].minor.yy487.pSelect, yymsp[-1].minor.yy180, yymsp[-4].minor.yy258);}
 110574         break;
 110575       case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
 110576 {sqlite3Insert(pParse, yymsp[-2].minor.yy347, 0, yymsp[0].minor.yy159, yymsp[-1].minor.yy180, yymsp[-4].minor.yy258);}
 110577         break;
 110578       case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
 110579 {sqlite3Insert(pParse, yymsp[-3].minor.yy347, 0, 0, yymsp[-2].minor.yy180, yymsp[-5].minor.yy258);}
 110580         break;
 110581       case 175: /* insert_cmd ::= INSERT orconf */
 110582 {yygotominor.yy258 = yymsp[0].minor.yy258;}
 110583         break;
 110584       case 176: /* insert_cmd ::= REPLACE */
 110585 {yygotominor.yy258 = OE_Replace;}
 110586         break;
 110587       case 177: /* valuelist ::= VALUES LP nexprlist RP */
 110589   yygotominor.yy487.pList = yymsp[-1].minor.yy442;
 110590   yygotominor.yy487.pSelect = 0;
 110592         break;
 110593       case 178: /* valuelist ::= valuelist COMMA LP exprlist RP */
 110595   Select *pRight = sqlite3SelectNew(pParse, yymsp[-1].minor.yy442, 0, 0, 0, 0, 0, 0, 0, 0);
 110596   if( yymsp[-4].minor.yy487.pList ){
 110597     yymsp[-4].minor.yy487.pSelect = sqlite3SelectNew(pParse, yymsp[-4].minor.yy487.pList, 0, 0, 0, 0, 0, 0, 0, 0);
 110598     yymsp[-4].minor.yy487.pList = 0;
 110600   yygotominor.yy487.pList = 0;
 110601   if( yymsp[-4].minor.yy487.pSelect==0 || pRight==0 ){
 110602     sqlite3SelectDelete(pParse->db, pRight);
 110603     sqlite3SelectDelete(pParse->db, yymsp[-4].minor.yy487.pSelect);
 110604     yygotominor.yy487.pSelect = 0;
 110605   }else{
 110606     pRight->op = TK_ALL;
 110607     pRight->pPrior = yymsp[-4].minor.yy487.pSelect;
 110608     pRight->selFlags |= SF_Values;
 110609     pRight->pPrior->selFlags |= SF_Values;
 110610     yygotominor.yy487.pSelect = pRight;
 110613         break;
 110614       case 181: /* inscollist ::= inscollist COMMA nm */
 110615 {yygotominor.yy180 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy180,&yymsp[0].minor.yy0);}
 110616         break;
 110617       case 182: /* inscollist ::= nm */
 110618 {yygotominor.yy180 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
 110619         break;
 110620       case 183: /* expr ::= term */
 110621 {yygotominor.yy342 = yymsp[0].minor.yy342;}
 110622         break;
 110623       case 184: /* expr ::= LP expr RP */
 110624 {yygotominor.yy342.pExpr = yymsp[-1].minor.yy342.pExpr; spanSet(&yygotominor.yy342,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
 110625         break;
 110626       case 185: /* term ::= NULL */
 110627       case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
 110628       case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
 110629 {spanExpr(&yygotominor.yy342, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
 110630         break;
 110631       case 186: /* expr ::= id */
 110632       case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
 110633 {spanExpr(&yygotominor.yy342, pParse, TK_ID, &yymsp[0].minor.yy0);}
 110634         break;
 110635       case 188: /* expr ::= nm DOT nm */
 110637   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
 110638   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
 110639   yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
 110640   spanSet(&yygotominor.yy342,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
 110642         break;
 110643       case 189: /* expr ::= nm DOT nm DOT nm */
 110645   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
 110646   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
 110647   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
 110648   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
 110649   yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
 110650   spanSet(&yygotominor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
 110652         break;
 110653       case 192: /* expr ::= REGISTER */
 110655   /* When doing a nested parse, one can include terms in an expression
 110656   ** that look like this:   #1 #2 ...  These terms refer to registers
 110657   ** in the virtual machine.  #N is the N-th register. */
 110658   if( pParse->nested==0 ){
 110659     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
 110660     yygotominor.yy342.pExpr = 0;
 110661   }else{
 110662     yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
 110663     if( yygotominor.yy342.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy342.pExpr->iTable);
 110665   spanSet(&yygotominor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
 110667         break;
 110668       case 193: /* expr ::= VARIABLE */
 110670   spanExpr(&yygotominor.yy342, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
 110671   sqlite3ExprAssignVarNumber(pParse, yygotominor.yy342.pExpr);
 110672   spanSet(&yygotominor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
 110674         break;
 110675       case 194: /* expr ::= expr COLLATE ids */
 110677   yygotominor.yy342.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy342.pExpr, &yymsp[0].minor.yy0);
 110678   yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
 110679   yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 110681         break;
 110682       case 195: /* expr ::= CAST LP expr AS typetoken RP */
 110684   yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy342.pExpr, 0, &yymsp[-1].minor.yy0);
 110685   spanSet(&yygotominor.yy342,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
 110687         break;
 110688       case 196: /* expr ::= ID LP distinct exprlist RP */
 110690   if( yymsp[-1].minor.yy442 && yymsp[-1].minor.yy442->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
 110691     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
 110693   yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy442, &yymsp[-4].minor.yy0);
 110694   spanSet(&yygotominor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
 110695   if( yymsp[-2].minor.yy392 && yygotominor.yy342.pExpr ){
 110696     yygotominor.yy342.pExpr->flags |= EP_Distinct;
 110699         break;
 110700       case 197: /* expr ::= ID LP STAR RP */
 110702   yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
 110703   spanSet(&yygotominor.yy342,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
 110705         break;
 110706       case 198: /* term ::= CTIME_KW */
 110708   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
 110709   ** treated as functions that return constants */
 110710   yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
 110711   if( yygotominor.yy342.pExpr ){
 110712     yygotominor.yy342.pExpr->op = TK_CONST_FUNC;  
 110714   spanSet(&yygotominor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
 110716         break;
 110717       case 199: /* expr ::= expr AND expr */
 110718       case 200: /* expr ::= expr OR expr */ yytestcase(yyruleno==200);
 110719       case 201: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==201);
 110720       case 202: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==202);
 110721       case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==203);
 110722       case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
 110723       case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205);
 110724       case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
 110725 {spanBinaryExpr(&yygotominor.yy342,pParse,yymsp[-1].major,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342);}
 110726         break;
 110727       case 207: /* likeop ::= LIKE_KW */
 110728       case 209: /* likeop ::= MATCH */ yytestcase(yyruleno==209);
 110729 {yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 0;}
 110730         break;
 110731       case 208: /* likeop ::= NOT LIKE_KW */
 110732       case 210: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==210);
 110733 {yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 1;}
 110734         break;
 110735       case 211: /* expr ::= expr likeop expr */
 110737   ExprList *pList;
 110738   pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy342.pExpr);
 110739   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy342.pExpr);
 110740   yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy318.eOperator);
 110741   if( yymsp[-1].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
 110742   yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
 110743   yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
 110744   if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
 110746         break;
 110747       case 212: /* expr ::= expr likeop expr ESCAPE expr */
 110749   ExprList *pList;
 110750   pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
 110751   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy342.pExpr);
 110752   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr);
 110753   yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy318.eOperator);
 110754   if( yymsp[-3].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
 110755   yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
 110756   yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
 110757   if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
 110759         break;
 110760       case 213: /* expr ::= expr ISNULL|NOTNULL */
 110761 {spanUnaryPostfix(&yygotominor.yy342,pParse,yymsp[0].major,&yymsp[-1].minor.yy342,&yymsp[0].minor.yy0);}
 110762         break;
 110763       case 214: /* expr ::= expr NOT NULL */
 110764 {spanUnaryPostfix(&yygotominor.yy342,pParse,TK_NOTNULL,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy0);}
 110765         break;
 110766       case 215: /* expr ::= expr IS expr */
 110768   spanBinaryExpr(&yygotominor.yy342,pParse,TK_IS,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342);
 110769   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy342.pExpr, yygotominor.yy342.pExpr, TK_ISNULL);
 110771         break;
 110772       case 216: /* expr ::= expr IS NOT expr */
 110774   spanBinaryExpr(&yygotominor.yy342,pParse,TK_ISNOT,&yymsp[-3].minor.yy342,&yymsp[0].minor.yy342);
 110775   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy342.pExpr, yygotominor.yy342.pExpr, TK_NOTNULL);
 110777         break;
 110778       case 217: /* expr ::= NOT expr */
 110779       case 218: /* expr ::= BITNOT expr */ yytestcase(yyruleno==218);
 110780 {spanUnaryPrefix(&yygotominor.yy342,pParse,yymsp[-1].major,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);}
 110781         break;
 110782       case 219: /* expr ::= MINUS expr */
 110783 {spanUnaryPrefix(&yygotominor.yy342,pParse,TK_UMINUS,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);}
 110784         break;
 110785       case 220: /* expr ::= PLUS expr */
 110786 {spanUnaryPrefix(&yygotominor.yy342,pParse,TK_UPLUS,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);}
 110787         break;
 110788       case 223: /* expr ::= expr between_op expr AND expr */
 110790   ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
 110791   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr);
 110792   yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy342.pExpr, 0, 0);
 110793   if( yygotominor.yy342.pExpr ){
 110794     yygotominor.yy342.pExpr->x.pList = pList;
 110795   }else{
 110796     sqlite3ExprListDelete(pParse->db, pList);
 110798   if( yymsp[-3].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
 110799   yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
 110800   yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
 110802         break;
 110803       case 226: /* expr ::= expr in_op LP exprlist RP */
 110805     if( yymsp[-1].minor.yy442==0 ){
 110806       /* Expressions of the form
 110808       **      expr1 IN ()
 110809       **      expr1 NOT IN ()
 110811       ** simplify to constants 0 (false) and 1 (true), respectively,
 110812       ** regardless of the value of expr1.
 110814       yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy392]);
 110815       sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy342.pExpr);
 110816     }else{
 110817       yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy342.pExpr, 0, 0);
 110818       if( yygotominor.yy342.pExpr ){
 110819         yygotominor.yy342.pExpr->x.pList = yymsp[-1].minor.yy442;
 110820         sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
 110821       }else{
 110822         sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy442);
 110824       if( yymsp[-3].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
 110826     yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
 110827     yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 110829         break;
 110830       case 227: /* expr ::= LP select RP */
 110832     yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
 110833     if( yygotominor.yy342.pExpr ){
 110834       yygotominor.yy342.pExpr->x.pSelect = yymsp[-1].minor.yy159;
 110835       ExprSetProperty(yygotominor.yy342.pExpr, EP_xIsSelect);
 110836       sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
 110837     }else{
 110838       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy159);
 110840     yygotominor.yy342.zStart = yymsp[-2].minor.yy0.z;
 110841     yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 110843         break;
 110844       case 228: /* expr ::= expr in_op LP select RP */
 110846     yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy342.pExpr, 0, 0);
 110847     if( yygotominor.yy342.pExpr ){
 110848       yygotominor.yy342.pExpr->x.pSelect = yymsp[-1].minor.yy159;
 110849       ExprSetProperty(yygotominor.yy342.pExpr, EP_xIsSelect);
 110850       sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
 110851     }else{
 110852       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy159);
 110854     if( yymsp[-3].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
 110855     yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
 110856     yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 110858         break;
 110859       case 229: /* expr ::= expr in_op nm dbnm */
 110861     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
 110862     yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy342.pExpr, 0, 0);
 110863     if( yygotominor.yy342.pExpr ){
 110864       yygotominor.yy342.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
 110865       ExprSetProperty(yygotominor.yy342.pExpr, EP_xIsSelect);
 110866       sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
 110867     }else{
 110868       sqlite3SrcListDelete(pParse->db, pSrc);
 110870     if( yymsp[-2].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
 110871     yygotominor.yy342.zStart = yymsp[-3].minor.yy342.zStart;
 110872     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];
 110874         break;
 110875       case 230: /* expr ::= EXISTS LP select RP */
 110877     Expr *p = yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
 110878     if( p ){
 110879       p->x.pSelect = yymsp[-1].minor.yy159;
 110880       ExprSetProperty(p, EP_xIsSelect);
 110881       sqlite3ExprSetHeight(pParse, p);
 110882     }else{
 110883       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy159);
 110885     yygotominor.yy342.zStart = yymsp[-3].minor.yy0.z;
 110886     yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 110888         break;
 110889       case 231: /* expr ::= CASE case_operand case_exprlist case_else END */
 110891   yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy122, yymsp[-1].minor.yy122, 0);
 110892   if( yygotominor.yy342.pExpr ){
 110893     yygotominor.yy342.pExpr->x.pList = yymsp[-2].minor.yy442;
 110894     sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
 110895   }else{
 110896     sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy442);
 110898   yygotominor.yy342.zStart = yymsp[-4].minor.yy0.z;
 110899   yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 110901         break;
 110902       case 232: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
 110904   yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, yymsp[-2].minor.yy342.pExpr);
 110905   yygotominor.yy442 = sqlite3ExprListAppend(pParse,yygotominor.yy442, yymsp[0].minor.yy342.pExpr);
 110907         break;
 110908       case 233: /* case_exprlist ::= WHEN expr THEN expr */
 110910   yygotominor.yy442 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
 110911   yygotominor.yy442 = sqlite3ExprListAppend(pParse,yygotominor.yy442, yymsp[0].minor.yy342.pExpr);
 110913         break;
 110914       case 240: /* nexprlist ::= nexprlist COMMA expr */
 110915 {yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy442,yymsp[0].minor.yy342.pExpr);}
 110916         break;
 110917       case 241: /* nexprlist ::= expr */
 110918 {yygotominor.yy442 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy342.pExpr);}
 110919         break;
 110920       case 242: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
 110922   sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0, 
 110923                      sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy442, yymsp[-9].minor.yy392,
 110924                       &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy392);
 110926         break;
 110927       case 243: /* uniqueflag ::= UNIQUE */
 110928       case 296: /* raisetype ::= ABORT */ yytestcase(yyruleno==296);
 110929 {yygotominor.yy392 = OE_Abort;}
 110930         break;
 110931       case 244: /* uniqueflag ::= */
 110932 {yygotominor.yy392 = OE_None;}
 110933         break;
 110934       case 247: /* idxlist ::= idxlist COMMA nm collate sortorder */
 110936   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
 110937   yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, p);
 110938   sqlite3ExprListSetName(pParse,yygotominor.yy442,&yymsp[-2].minor.yy0,1);
 110939   sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
 110940   if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
 110942         break;
 110943       case 248: /* idxlist ::= nm collate sortorder */
 110945   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
 110946   yygotominor.yy442 = sqlite3ExprListAppend(pParse,0, p);
 110947   sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
 110948   sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
 110949   if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
 110951         break;
 110952       case 249: /* collate ::= */
 110953 {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
 110954         break;
 110955       case 251: /* cmd ::= DROP INDEX ifexists fullname */
 110956 {sqlite3DropIndex(pParse, yymsp[0].minor.yy347, yymsp[-1].minor.yy392);}
 110957         break;
 110958       case 252: /* cmd ::= VACUUM */
 110959       case 253: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==253);
 110960 {sqlite3Vacuum(pParse);}
 110961         break;
 110962       case 254: /* cmd ::= PRAGMA nm dbnm */
 110963 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
 110964         break;
 110965       case 255: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
 110966 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
 110967         break;
 110968       case 256: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
 110969 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
 110970         break;
 110971       case 257: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
 110972 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
 110973         break;
 110974       case 258: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
 110975 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
 110976         break;
 110977       case 268: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
 110979   Token all;
 110980   all.z = yymsp[-3].minor.yy0.z;
 110981   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
 110982   sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy327, &all);
 110984         break;
 110985       case 269: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
 110987   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);
 110988   yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
 110990         break;
 110991       case 270: /* trigger_time ::= BEFORE */
 110992       case 273: /* trigger_time ::= */ yytestcase(yyruleno==273);
 110993 { yygotominor.yy392 = TK_BEFORE; }
 110994         break;
 110995       case 271: /* trigger_time ::= AFTER */
 110996 { yygotominor.yy392 = TK_AFTER;  }
 110997         break;
 110998       case 272: /* trigger_time ::= INSTEAD OF */
 110999 { yygotominor.yy392 = TK_INSTEAD;}
 111000         break;
 111001       case 274: /* trigger_event ::= DELETE|INSERT */
 111002       case 275: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==275);
 111003 {yygotominor.yy410.a = yymsp[0].major; yygotominor.yy410.b = 0;}
 111004         break;
 111005       case 276: /* trigger_event ::= UPDATE OF inscollist */
 111006 {yygotominor.yy410.a = TK_UPDATE; yygotominor.yy410.b = yymsp[0].minor.yy180;}
 111007         break;
 111008       case 279: /* when_clause ::= */
 111009       case 301: /* key_opt ::= */ yytestcase(yyruleno==301);
 111010 { yygotominor.yy122 = 0; }
 111011         break;
 111012       case 280: /* when_clause ::= WHEN expr */
 111013       case 302: /* key_opt ::= KEY expr */ yytestcase(yyruleno==302);
 111014 { yygotominor.yy122 = yymsp[0].minor.yy342.pExpr; }
 111015         break;
 111016       case 281: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
 111018   assert( yymsp[-2].minor.yy327!=0 );
 111019   yymsp[-2].minor.yy327->pLast->pNext = yymsp[-1].minor.yy327;
 111020   yymsp[-2].minor.yy327->pLast = yymsp[-1].minor.yy327;
 111021   yygotominor.yy327 = yymsp[-2].minor.yy327;
 111023         break;
 111024       case 282: /* trigger_cmd_list ::= trigger_cmd SEMI */
 111026   assert( yymsp[-1].minor.yy327!=0 );
 111027   yymsp[-1].minor.yy327->pLast = yymsp[-1].minor.yy327;
 111028   yygotominor.yy327 = yymsp[-1].minor.yy327;
 111030         break;
 111031       case 284: /* trnm ::= nm DOT nm */
 111033   yygotominor.yy0 = yymsp[0].minor.yy0;
 111034   sqlite3ErrorMsg(pParse, 
 111035         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
 111036         "statements within triggers");
 111038         break;
 111039       case 286: /* tridxby ::= INDEXED BY nm */
 111041   sqlite3ErrorMsg(pParse,
 111042         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
 111043         "within triggers");
 111045         break;
 111046       case 287: /* tridxby ::= NOT INDEXED */
 111048   sqlite3ErrorMsg(pParse,
 111049         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
 111050         "within triggers");
 111052         break;
 111053       case 288: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
 111054 { yygotominor.yy327 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy442, yymsp[0].minor.yy122, yymsp[-5].minor.yy258); }
 111055         break;
 111056       case 289: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt valuelist */
 111057 {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);}
 111058         break;
 111059       case 290: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
 111060 {yygotominor.yy327 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy180, 0, yymsp[0].minor.yy159, yymsp[-4].minor.yy258);}
 111061         break;
 111062       case 291: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
 111063 {yygotominor.yy327 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy122);}
 111064         break;
 111065       case 292: /* trigger_cmd ::= select */
 111066 {yygotominor.yy327 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy159); }
 111067         break;
 111068       case 293: /* expr ::= RAISE LP IGNORE RP */
 111070   yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
 111071   if( yygotominor.yy342.pExpr ){
 111072     yygotominor.yy342.pExpr->affinity = OE_Ignore;
 111074   yygotominor.yy342.zStart = yymsp[-3].minor.yy0.z;
 111075   yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 111077         break;
 111078       case 294: /* expr ::= RAISE LP raisetype COMMA nm RP */
 111080   yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); 
 111081   if( yygotominor.yy342.pExpr ) {
 111082     yygotominor.yy342.pExpr->affinity = (char)yymsp[-3].minor.yy392;
 111084   yygotominor.yy342.zStart = yymsp[-5].minor.yy0.z;
 111085   yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 111087         break;
 111088       case 295: /* raisetype ::= ROLLBACK */
 111089 {yygotominor.yy392 = OE_Rollback;}
 111090         break;
 111091       case 297: /* raisetype ::= FAIL */
 111092 {yygotominor.yy392 = OE_Fail;}
 111093         break;
 111094       case 298: /* cmd ::= DROP TRIGGER ifexists fullname */
 111096   sqlite3DropTrigger(pParse,yymsp[0].minor.yy347,yymsp[-1].minor.yy392);
 111098         break;
 111099       case 299: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
 111101   sqlite3Attach(pParse, yymsp[-3].minor.yy342.pExpr, yymsp[-1].minor.yy342.pExpr, yymsp[0].minor.yy122);
 111103         break;
 111104       case 300: /* cmd ::= DETACH database_kw_opt expr */
 111106   sqlite3Detach(pParse, yymsp[0].minor.yy342.pExpr);
 111108         break;
 111109       case 305: /* cmd ::= REINDEX */
 111110 {sqlite3Reindex(pParse, 0, 0);}
 111111         break;
 111112       case 306: /* cmd ::= REINDEX nm dbnm */
 111113 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
 111114         break;
 111115       case 307: /* cmd ::= ANALYZE */
 111116 {sqlite3Analyze(pParse, 0, 0);}
 111117         break;
 111118       case 308: /* cmd ::= ANALYZE nm dbnm */
 111119 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
 111120         break;
 111121       case 309: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
 111123   sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy347,&yymsp[0].minor.yy0);
 111125         break;
 111126       case 310: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
 111128   sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
 111130         break;
 111131       case 311: /* add_column_fullname ::= fullname */
 111133   pParse->db->lookaside.bEnabled = 0;
 111134   sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy347);
 111136         break;
 111137       case 314: /* cmd ::= create_vtab */
 111138 {sqlite3VtabFinishParse(pParse,0);}
 111139         break;
 111140       case 315: /* cmd ::= create_vtab LP vtabarglist RP */
 111141 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
 111142         break;
 111143       case 316: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
 111145     sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy392);
 111147         break;
 111148       case 319: /* vtabarg ::= */
 111149 {sqlite3VtabArgInit(pParse);}
 111150         break;
 111151       case 321: /* vtabargtoken ::= ANY */
 111152       case 322: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==322);
 111153       case 323: /* lp ::= LP */ yytestcase(yyruleno==323);
 111154 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
 111155         break;
 111156       default:
 111157       /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
 111158       /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
 111159       /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
 111160       /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
 111161       /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
 111162       /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
 111163       /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
 111164       /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
 111165       /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
 111166       /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
 111167       /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
 111168       /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
 111169       /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
 111170       /* (44) type ::= */ yytestcase(yyruleno==44);
 111171       /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
 111172       /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
 111173       /* (53) carglist ::= carglist ccons */ yytestcase(yyruleno==53);
 111174       /* (54) carglist ::= */ yytestcase(yyruleno==54);
 111175       /* (61) ccons ::= NULL onconf */ yytestcase(yyruleno==61);
 111176       /* (89) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==89);
 111177       /* (90) conslist ::= tcons */ yytestcase(yyruleno==90);
 111178       /* (92) tconscomma ::= */ yytestcase(yyruleno==92);
 111179       /* (277) foreach_clause ::= */ yytestcase(yyruleno==277);
 111180       /* (278) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==278);
 111181       /* (285) tridxby ::= */ yytestcase(yyruleno==285);
 111182       /* (303) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==303);
 111183       /* (304) database_kw_opt ::= */ yytestcase(yyruleno==304);
 111184       /* (312) kwcolumn_opt ::= */ yytestcase(yyruleno==312);
 111185       /* (313) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==313);
 111186       /* (317) vtabarglist ::= vtabarg */ yytestcase(yyruleno==317);
 111187       /* (318) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==318);
 111188       /* (320) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==320);
 111189       /* (324) anylist ::= */ yytestcase(yyruleno==324);
 111190       /* (325) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==325);
 111191       /* (326) anylist ::= anylist ANY */ yytestcase(yyruleno==326);
 111192         break;
 111194   assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
 111195   yygoto = yyRuleInfo[yyruleno].lhs;
 111196   yysize = yyRuleInfo[yyruleno].nrhs;
 111197   yypParser->yyidx -= yysize;
 111198   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
 111199   if( yyact < YYNSTATE ){
 111200 #ifdef NDEBUG
 111201     /* If we are not debugging and the reduce action popped at least
 111202     ** one element off the stack, then we can push the new element back
 111203     ** onto the stack here, and skip the stack overflow test in yy_shift().
 111204     ** That gives a significant speed improvement. */
 111205     if( yysize ){
 111206       yypParser->yyidx++;
 111207       yymsp -= yysize-1;
 111208       yymsp->stateno = (YYACTIONTYPE)yyact;
 111209       yymsp->major = (YYCODETYPE)yygoto;
 111210       yymsp->minor = yygotominor;
 111211     }else
 111212 #endif
 111214       yy_shift(yypParser,yyact,yygoto,&yygotominor);
 111216   }else{
 111217     assert( yyact == YYNSTATE + YYNRULE + 1 );
 111218     yy_accept(yypParser);
 111223 ** The following code executes when the parse fails
 111225 #ifndef YYNOERRORRECOVERY
 111226 static void yy_parse_failed(
 111227   yyParser *yypParser           /* The parser */
 111229   sqlite3ParserARG_FETCH;
 111230 #ifndef NDEBUG
 111231   if( yyTraceFILE ){
 111232     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
 111234 #endif
 111235   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
 111236   /* Here code is inserted which will be executed whenever the
 111237   ** parser fails */
 111238   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
 111240 #endif /* YYNOERRORRECOVERY */
 111243 ** The following code executes when a syntax error first occurs.
 111245 static void yy_syntax_error(
 111246   yyParser *yypParser,           /* The parser */
 111247   int yymajor,                   /* The major type of the error token */
 111248   YYMINORTYPE yyminor            /* The minor type of the error token */
 111250   sqlite3ParserARG_FETCH;
 111251 #define TOKEN (yyminor.yy0)
 111253   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
 111254   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
 111255   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
 111256   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
 111260 ** The following is executed when the parser accepts
 111262 static void yy_accept(
 111263   yyParser *yypParser           /* The parser */
 111265   sqlite3ParserARG_FETCH;
 111266 #ifndef NDEBUG
 111267   if( yyTraceFILE ){
 111268     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
 111270 #endif
 111271   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
 111272   /* Here code is inserted which will be executed whenever the
 111273   ** parser accepts */
 111274   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
 111277 /* The main parser program.
 111278 ** The first argument is a pointer to a structure obtained from
 111279 ** "sqlite3ParserAlloc" which describes the current state of the parser.
 111280 ** The second argument is the major token number.  The third is
 111281 ** the minor token.  The fourth optional argument is whatever the
 111282 ** user wants (and specified in the grammar) and is available for
 111283 ** use by the action routines.
 111285 ** Inputs:
 111286 ** <ul>
 111287 ** <li> A pointer to the parser (an opaque structure.)
 111288 ** <li> The major token number.
 111289 ** <li> The minor token number.
 111290 ** <li> An option argument of a grammar-specified type.
 111291 ** </ul>
 111293 ** Outputs:
 111294 ** None.
 111296 SQLITE_PRIVATE void sqlite3Parser(
 111297   void *yyp,                   /* The parser */
 111298   int yymajor,                 /* The major token code number */
 111299   sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
 111300   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
 111302   YYMINORTYPE yyminorunion;
 111303   int yyact;            /* The parser action. */
 111304 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
 111305   int yyendofinput;     /* True if we are at the end of input */
 111306 #endif
 111307 #ifdef YYERRORSYMBOL
 111308   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
 111309 #endif
 111310   yyParser *yypParser;  /* The parser */
 111312   /* (re)initialize the parser, if necessary */
 111313   yypParser = (yyParser*)yyp;
 111314   if( yypParser->yyidx<0 ){
 111315 #if YYSTACKDEPTH<=0
 111316     if( yypParser->yystksz <=0 ){
 111317       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
 111318       yyminorunion = yyzerominor;
 111319       yyStackOverflow(yypParser, &yyminorunion);
 111320       return;
 111322 #endif
 111323     yypParser->yyidx = 0;
 111324     yypParser->yyerrcnt = -1;
 111325     yypParser->yystack[0].stateno = 0;
 111326     yypParser->yystack[0].major = 0;
 111328   yyminorunion.yy0 = yyminor;
 111329 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
 111330   yyendofinput = (yymajor==0);
 111331 #endif
 111332   sqlite3ParserARG_STORE;
 111334 #ifndef NDEBUG
 111335   if( yyTraceFILE ){
 111336     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
 111338 #endif
 111340   do{
 111341     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
 111342     if( yyact<YYNSTATE ){
 111343       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
 111344       yypParser->yyerrcnt--;
 111345       yymajor = YYNOCODE;
 111346     }else if( yyact < YYNSTATE + YYNRULE ){
 111347       yy_reduce(yypParser,yyact-YYNSTATE);
 111348     }else{
 111349       assert( yyact == YY_ERROR_ACTION );
 111350 #ifdef YYERRORSYMBOL
 111351       int yymx;
 111352 #endif
 111353 #ifndef NDEBUG
 111354       if( yyTraceFILE ){
 111355         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
 111357 #endif
 111358 #ifdef YYERRORSYMBOL
 111359       /* A syntax error has occurred.
 111360       ** The response to an error depends upon whether or not the
 111361       ** grammar defines an error token "ERROR".  
 111363       ** This is what we do if the grammar does define ERROR:
 111365       **  * Call the %syntax_error function.
 111367       **  * Begin popping the stack until we enter a state where
 111368       **    it is legal to shift the error symbol, then shift
 111369       **    the error symbol.
 111371       **  * Set the error count to three.
 111373       **  * Begin accepting and shifting new tokens.  No new error
 111374       **    processing will occur until three tokens have been
 111375       **    shifted successfully.
 111378       if( yypParser->yyerrcnt<0 ){
 111379         yy_syntax_error(yypParser,yymajor,yyminorunion);
 111381       yymx = yypParser->yystack[yypParser->yyidx].major;
 111382       if( yymx==YYERRORSYMBOL || yyerrorhit ){
 111383 #ifndef NDEBUG
 111384         if( yyTraceFILE ){
 111385           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
 111386              yyTracePrompt,yyTokenName[yymajor]);
 111388 #endif
 111389         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
 111390         yymajor = YYNOCODE;
 111391       }else{
 111392          while(
 111393           yypParser->yyidx >= 0 &&
 111394           yymx != YYERRORSYMBOL &&
 111395           (yyact = yy_find_reduce_action(
 111396                         yypParser->yystack[yypParser->yyidx].stateno,
 111397                         YYERRORSYMBOL)) >= YYNSTATE
 111399           yy_pop_parser_stack(yypParser);
 111401         if( yypParser->yyidx < 0 || yymajor==0 ){
 111402           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
 111403           yy_parse_failed(yypParser);
 111404           yymajor = YYNOCODE;
 111405         }else if( yymx!=YYERRORSYMBOL ){
 111406           YYMINORTYPE u2;
 111407           u2.YYERRSYMDT = 0;
 111408           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
 111411       yypParser->yyerrcnt = 3;
 111412       yyerrorhit = 1;
 111413 #elif defined(YYNOERRORRECOVERY)
 111414       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
 111415       ** do any kind of error recovery.  Instead, simply invoke the syntax
 111416       ** error routine and continue going as if nothing had happened.
 111418       ** Applications can set this macro (for example inside %include) if
 111419       ** they intend to abandon the parse upon the first syntax error seen.
 111421       yy_syntax_error(yypParser,yymajor,yyminorunion);
 111422       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
 111423       yymajor = YYNOCODE;
 111425 #else  /* YYERRORSYMBOL is not defined */
 111426       /* This is what we do if the grammar does not define ERROR:
 111428       **  * Report an error message, and throw away the input token.
 111430       **  * If the input token is $, then fail the parse.
 111432       ** As before, subsequent error messages are suppressed until
 111433       ** three input tokens have been successfully shifted.
 111435       if( yypParser->yyerrcnt<=0 ){
 111436         yy_syntax_error(yypParser,yymajor,yyminorunion);
 111438       yypParser->yyerrcnt = 3;
 111439       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
 111440       if( yyendofinput ){
 111441         yy_parse_failed(yypParser);
 111443       yymajor = YYNOCODE;
 111444 #endif
 111446   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
 111447   return;
 111450 /************** End of parse.c ***********************************************/
 111451 /************** Begin file tokenize.c ****************************************/
 111453 ** 2001 September 15
 111455 ** The author disclaims copyright to this source code.  In place of
 111456 ** a legal notice, here is a blessing:
 111458 **    May you do good and not evil.
 111459 **    May you find forgiveness for yourself and forgive others.
 111460 **    May you share freely, never taking more than you give.
 111462 *************************************************************************
 111463 ** An tokenizer for SQL
 111465 ** This file contains C code that splits an SQL input string up into
 111466 ** individual tokens and sends those tokens one-by-one over to the
 111467 ** parser for analysis.
 111469 /* #include <stdlib.h> */
 111472 ** The charMap() macro maps alphabetic characters into their
 111473 ** lower-case ASCII equivalent.  On ASCII machines, this is just
 111474 ** an upper-to-lower case map.  On EBCDIC machines we also need
 111475 ** to adjust the encoding.  Only alphabetic characters and underscores
 111476 ** need to be translated.
 111478 #ifdef SQLITE_ASCII
 111479 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
 111480 #endif
 111481 #ifdef SQLITE_EBCDIC
 111482 # define charMap(X) ebcdicToAscii[(unsigned char)X]
 111483 const unsigned char ebcdicToAscii[] = {
 111484 /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
 111485    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
 111486    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
 111487    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
 111488    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
 111489    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
 111490    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
 111491    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
 111492    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
 111493    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
 111494    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
 111495    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
 111496    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
 111497    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
 111498    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
 111499    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
 111500    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
 111502 #endif
 111505 ** The sqlite3KeywordCode function looks up an identifier to determine if
 111506 ** it is a keyword.  If it is a keyword, the token code of that keyword is 
 111507 ** returned.  If the input is not a keyword, TK_ID is returned.
 111509 ** The implementation of this routine was generated by a program,
 111510 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
 111511 ** The output of the mkkeywordhash.c program is written into a file
 111512 ** named keywordhash.h and then included into this source file by
 111513 ** the #include below.
 111515 /************** Include keywordhash.h in the middle of tokenize.c ************/
 111516 /************** Begin file keywordhash.h *************************************/
 111517 /***** This file contains automatically generated code ******
 111519 ** The code in this file has been automatically generated by
 111521 **   sqlite/tool/mkkeywordhash.c
 111523 ** The code in this file implements a function that determines whether
 111524 ** or not a given identifier is really an SQL keyword.  The same thing
 111525 ** might be implemented more directly using a hand-written hash table.
 111526 ** But by using this automatically generated code, the size of the code
 111527 ** is substantially reduced.  This is important for embedded applications
 111528 ** on platforms with limited memory.
 111530 /* Hash score: 175 */
 111531 static int keywordCode(const char *z, int n){
 111532   /* zText[] encodes 811 bytes of keywords in 541 bytes */
 111533   /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
 111534   /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
 111535   /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
 111536   /*   UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE          */
 111537   /*   CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN        */
 111538   /*   SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME         */
 111539   /*   AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS     */
 111540   /*   CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF      */
 111541   /*   ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW         */
 111542   /*   INITIALLY                                                          */
 111543   static const char zText[540] = {
 111544     'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
 111545     'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
 111546     'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
 111547     'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
 111548     'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
 111549     'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
 111550     'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
 111551     'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
 111552     'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
 111553     'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
 111554     'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
 111555     'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
 111556     'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
 111557     'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
 111558     'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
 111559     'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
 111560     'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
 111561     'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
 111562     'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
 111563     'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
 111564     'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
 111565     'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
 111566     'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
 111567     'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
 111568     'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
 111569     'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
 111570     'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
 111571     'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
 111572     'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
 111573     'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
 111575   static const unsigned char aHash[127] = {
 111576       72, 101, 114,  70,   0,  45,   0,   0,  78,   0,  73,   0,   0,
 111577       42,  12,  74,  15,   0, 113,  81,  50, 108,   0,  19,   0,   0,
 111578      118,   0, 116, 111,   0,  22,  89,   0,   9,   0,   0,  66,  67,
 111579        0,  65,   6,   0,  48,  86,  98,   0, 115,  97,   0,   0,  44,
 111580        0,  99,  24,   0,  17,   0, 119,  49,  23,   0,   5, 106,  25,
 111581       92,   0,   0, 121, 102,  56, 120,  53,  28,  51,   0,  87,   0,
 111582       96,  26,   0,  95,   0,   0,   0,  91,  88,  93,  84, 105,  14,
 111583       39, 104,   0,  77,   0,  18,  85, 107,  32,   0, 117,  76, 109,
 111584       58,  46,  80,   0,   0,  90,  40,   0, 112,   0,  36,   0,   0,
 111585       29,   0,  82,  59,  60,   0,  20,  57,   0,  52,
 111587   static const unsigned char aNext[121] = {
 111588        0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
 111589        0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
 111590        0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
 111591        0,   0,   0,   0,  33,   0,  21,   0,   0,   0,  43,   3,  47,
 111592        0,   0,   0,   0,  30,   0,  54,   0,  38,   0,   0,   0,   1,
 111593       62,   0,   0,  63,   0,  41,   0,   0,   0,   0,   0,   0,   0,
 111594       61,   0,   0,   0,   0,  31,  55,  16,  34,  10,   0,   0,   0,
 111595        0,   0,   0,   0,  11,  68,  75,   0,   8,   0, 100,  94,   0,
 111596      103,   0,  83,   0,  71,   0,   0, 110,  27,  37,  69,  79,   0,
 111597       35,  64,   0,   0,
 111599   static const unsigned char aLen[121] = {
 111600        7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
 111601        7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
 111602       11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
 111603        4,   6,   2,   3,   9,   4,   2,   6,   5,   6,   6,   5,   6,
 111604        5,   5,   7,   7,   7,   3,   2,   4,   4,   7,   3,   6,   4,
 111605        7,   6,  12,   6,   9,   4,   6,   5,   4,   7,   6,   5,   6,
 111606        7,   5,   4,   5,   6,   5,   7,   3,   7,  13,   2,   2,   4,
 111607        6,   6,   8,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
 111608        4,   4,   2,   2,   6,   5,   8,   5,   5,   8,   3,   5,   5,
 111609        6,   4,   9,   3,
 111611   static const unsigned short int aOffset[121] = {
 111612        0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
 111613       36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
 111614       86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
 111615      159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
 111616      203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
 111617      248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
 111618      326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
 111619      387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
 111620      462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
 111621      521, 527, 531, 536,
 111623   static const unsigned char aCode[121] = {
 111624     TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
 111625     TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
 111626     TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
 111627     TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
 111628     TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
 111629     TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,    
 111630     TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  
 111631     TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,       
 111632     TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
 111633     TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_ATTACH,     TK_HAVING,     
 111634     TK_GROUP,      TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RELEASE,    
 111635     TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       
 111636     TK_LIKE_KW,    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       
 111637     TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  
 111638     TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    
 111639     TK_PRAGMA,     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      
 111640     TK_WHEN,       TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    
 111641     TK_AND,        TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         
 111642     TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    
 111643     TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   
 111644     TK_IS,         TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    
 111645     TK_LIKE_KW,    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      
 111646     TK_RESTRICT,   TK_JOIN_KW,    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        
 111647     TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  
 111648     TK_ALL,        
 111650   int h, i;
 111651   if( n<2 ) return TK_ID;
 111652   h = ((charMap(z[0])*4) ^
 111653       (charMap(z[n-1])*3) ^
 111654       n) % 127;
 111655   for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
 111656     if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
 111657       testcase( i==0 ); /* REINDEX */
 111658       testcase( i==1 ); /* INDEXED */
 111659       testcase( i==2 ); /* INDEX */
 111660       testcase( i==3 ); /* DESC */
 111661       testcase( i==4 ); /* ESCAPE */
 111662       testcase( i==5 ); /* EACH */
 111663       testcase( i==6 ); /* CHECK */
 111664       testcase( i==7 ); /* KEY */
 111665       testcase( i==8 ); /* BEFORE */
 111666       testcase( i==9 ); /* FOREIGN */
 111667       testcase( i==10 ); /* FOR */
 111668       testcase( i==11 ); /* IGNORE */
 111669       testcase( i==12 ); /* REGEXP */
 111670       testcase( i==13 ); /* EXPLAIN */
 111671       testcase( i==14 ); /* INSTEAD */
 111672       testcase( i==15 ); /* ADD */
 111673       testcase( i==16 ); /* DATABASE */
 111674       testcase( i==17 ); /* AS */
 111675       testcase( i==18 ); /* SELECT */
 111676       testcase( i==19 ); /* TABLE */
 111677       testcase( i==20 ); /* LEFT */
 111678       testcase( i==21 ); /* THEN */
 111679       testcase( i==22 ); /* END */
 111680       testcase( i==23 ); /* DEFERRABLE */
 111681       testcase( i==24 ); /* ELSE */
 111682       testcase( i==25 ); /* EXCEPT */
 111683       testcase( i==26 ); /* TRANSACTION */
 111684       testcase( i==27 ); /* ACTION */
 111685       testcase( i==28 ); /* ON */
 111686       testcase( i==29 ); /* NATURAL */
 111687       testcase( i==30 ); /* ALTER */
 111688       testcase( i==31 ); /* RAISE */
 111689       testcase( i==32 ); /* EXCLUSIVE */
 111690       testcase( i==33 ); /* EXISTS */
 111691       testcase( i==34 ); /* SAVEPOINT */
 111692       testcase( i==35 ); /* INTERSECT */
 111693       testcase( i==36 ); /* TRIGGER */
 111694       testcase( i==37 ); /* REFERENCES */
 111695       testcase( i==38 ); /* CONSTRAINT */
 111696       testcase( i==39 ); /* INTO */
 111697       testcase( i==40 ); /* OFFSET */
 111698       testcase( i==41 ); /* OF */
 111699       testcase( i==42 ); /* SET */
 111700       testcase( i==43 ); /* TEMPORARY */
 111701       testcase( i==44 ); /* TEMP */
 111702       testcase( i==45 ); /* OR */
 111703       testcase( i==46 ); /* UNIQUE */
 111704       testcase( i==47 ); /* QUERY */
 111705       testcase( i==48 ); /* ATTACH */
 111706       testcase( i==49 ); /* HAVING */
 111707       testcase( i==50 ); /* GROUP */
 111708       testcase( i==51 ); /* UPDATE */
 111709       testcase( i==52 ); /* BEGIN */
 111710       testcase( i==53 ); /* INNER */
 111711       testcase( i==54 ); /* RELEASE */
 111712       testcase( i==55 ); /* BETWEEN */
 111713       testcase( i==56 ); /* NOTNULL */
 111714       testcase( i==57 ); /* NOT */
 111715       testcase( i==58 ); /* NO */
 111716       testcase( i==59 ); /* NULL */
 111717       testcase( i==60 ); /* LIKE */
 111718       testcase( i==61 ); /* CASCADE */
 111719       testcase( i==62 ); /* ASC */
 111720       testcase( i==63 ); /* DELETE */
 111721       testcase( i==64 ); /* CASE */
 111722       testcase( i==65 ); /* COLLATE */
 111723       testcase( i==66 ); /* CREATE */
 111724       testcase( i==67 ); /* CURRENT_DATE */
 111725       testcase( i==68 ); /* DETACH */
 111726       testcase( i==69 ); /* IMMEDIATE */
 111727       testcase( i==70 ); /* JOIN */
 111728       testcase( i==71 ); /* INSERT */
 111729       testcase( i==72 ); /* MATCH */
 111730       testcase( i==73 ); /* PLAN */
 111731       testcase( i==74 ); /* ANALYZE */
 111732       testcase( i==75 ); /* PRAGMA */
 111733       testcase( i==76 ); /* ABORT */
 111734       testcase( i==77 ); /* VALUES */
 111735       testcase( i==78 ); /* VIRTUAL */
 111736       testcase( i==79 ); /* LIMIT */
 111737       testcase( i==80 ); /* WHEN */
 111738       testcase( i==81 ); /* WHERE */
 111739       testcase( i==82 ); /* RENAME */
 111740       testcase( i==83 ); /* AFTER */
 111741       testcase( i==84 ); /* REPLACE */
 111742       testcase( i==85 ); /* AND */
 111743       testcase( i==86 ); /* DEFAULT */
 111744       testcase( i==87 ); /* AUTOINCREMENT */
 111745       testcase( i==88 ); /* TO */
 111746       testcase( i==89 ); /* IN */
 111747       testcase( i==90 ); /* CAST */
 111748       testcase( i==91 ); /* COLUMN */
 111749       testcase( i==92 ); /* COMMIT */
 111750       testcase( i==93 ); /* CONFLICT */
 111751       testcase( i==94 ); /* CROSS */
 111752       testcase( i==95 ); /* CURRENT_TIMESTAMP */
 111753       testcase( i==96 ); /* CURRENT_TIME */
 111754       testcase( i==97 ); /* PRIMARY */
 111755       testcase( i==98 ); /* DEFERRED */
 111756       testcase( i==99 ); /* DISTINCT */
 111757       testcase( i==100 ); /* IS */
 111758       testcase( i==101 ); /* DROP */
 111759       testcase( i==102 ); /* FAIL */
 111760       testcase( i==103 ); /* FROM */
 111761       testcase( i==104 ); /* FULL */
 111762       testcase( i==105 ); /* GLOB */
 111763       testcase( i==106 ); /* BY */
 111764       testcase( i==107 ); /* IF */
 111765       testcase( i==108 ); /* ISNULL */
 111766       testcase( i==109 ); /* ORDER */
 111767       testcase( i==110 ); /* RESTRICT */
 111768       testcase( i==111 ); /* OUTER */
 111769       testcase( i==112 ); /* RIGHT */
 111770       testcase( i==113 ); /* ROLLBACK */
 111771       testcase( i==114 ); /* ROW */
 111772       testcase( i==115 ); /* UNION */
 111773       testcase( i==116 ); /* USING */
 111774       testcase( i==117 ); /* VACUUM */
 111775       testcase( i==118 ); /* VIEW */
 111776       testcase( i==119 ); /* INITIALLY */
 111777       testcase( i==120 ); /* ALL */
 111778       return aCode[i];
 111781   return TK_ID;
 111783 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
 111784   return keywordCode((char*)z, n);
 111786 #define SQLITE_N_KEYWORD 121
 111788 /************** End of keywordhash.h *****************************************/
 111789 /************** Continuing where we left off in tokenize.c *******************/
 111793 ** If X is a character that can be used in an identifier then
 111794 ** IdChar(X) will be true.  Otherwise it is false.
 111796 ** For ASCII, any character with the high-order bit set is
 111797 ** allowed in an identifier.  For 7-bit characters, 
 111798 ** sqlite3IsIdChar[X] must be 1.
 111800 ** For EBCDIC, the rules are more complex but have the same
 111801 ** end result.
 111803 ** Ticket #1066.  the SQL standard does not allow '$' in the
 111804 ** middle of identfiers.  But many SQL implementations do. 
 111805 ** SQLite will allow '$' in identifiers for compatibility.
 111806 ** But the feature is undocumented.
 111808 #ifdef SQLITE_ASCII
 111809 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
 111810 #endif
 111811 #ifdef SQLITE_EBCDIC
 111812 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
 111813 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
 111814     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
 111815     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
 111816     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
 111817     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
 111818     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
 111819     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
 111820     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
 111821     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
 111822     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
 111823     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
 111824     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
 111825     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
 111827 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
 111828 #endif
 111832 ** Return the length of the token that begins at z[0]. 
 111833 ** Store the token type in *tokenType before returning.
 111835 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
 111836   int i, c;
 111837   switch( *z ){
 111838     case ' ': case '\t': case '\n': case '\f': case '\r': {
 111839       testcase( z[0]==' ' );
 111840       testcase( z[0]=='\t' );
 111841       testcase( z[0]=='\n' );
 111842       testcase( z[0]=='\f' );
 111843       testcase( z[0]=='\r' );
 111844       for(i=1; sqlite3Isspace(z[i]); i++){}
 111845       *tokenType = TK_SPACE;
 111846       return i;
 111848     case '-': {
 111849       if( z[1]=='-' ){
 111850         /* IMP: R-50417-27976 -- syntax diagram for comments */
 111851         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
 111852         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
 111853         return i;
 111855       *tokenType = TK_MINUS;
 111856       return 1;
 111858     case '(': {
 111859       *tokenType = TK_LP;
 111860       return 1;
 111862     case ')': {
 111863       *tokenType = TK_RP;
 111864       return 1;
 111866     case ';': {
 111867       *tokenType = TK_SEMI;
 111868       return 1;
 111870     case '+': {
 111871       *tokenType = TK_PLUS;
 111872       return 1;
 111874     case '*': {
 111875       *tokenType = TK_STAR;
 111876       return 1;
 111878     case '/': {
 111879       if( z[1]!='*' || z[2]==0 ){
 111880         *tokenType = TK_SLASH;
 111881         return 1;
 111883       /* IMP: R-50417-27976 -- syntax diagram for comments */
 111884       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
 111885       if( c ) i++;
 111886       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
 111887       return i;
 111889     case '%': {
 111890       *tokenType = TK_REM;
 111891       return 1;
 111893     case '=': {
 111894       *tokenType = TK_EQ;
 111895       return 1 + (z[1]=='=');
 111897     case '<': {
 111898       if( (c=z[1])=='=' ){
 111899         *tokenType = TK_LE;
 111900         return 2;
 111901       }else if( c=='>' ){
 111902         *tokenType = TK_NE;
 111903         return 2;
 111904       }else if( c=='<' ){
 111905         *tokenType = TK_LSHIFT;
 111906         return 2;
 111907       }else{
 111908         *tokenType = TK_LT;
 111909         return 1;
 111912     case '>': {
 111913       if( (c=z[1])=='=' ){
 111914         *tokenType = TK_GE;
 111915         return 2;
 111916       }else if( c=='>' ){
 111917         *tokenType = TK_RSHIFT;
 111918         return 2;
 111919       }else{
 111920         *tokenType = TK_GT;
 111921         return 1;
 111924     case '!': {
 111925       if( z[1]!='=' ){
 111926         *tokenType = TK_ILLEGAL;
 111927         return 2;
 111928       }else{
 111929         *tokenType = TK_NE;
 111930         return 2;
 111933     case '|': {
 111934       if( z[1]!='|' ){
 111935         *tokenType = TK_BITOR;
 111936         return 1;
 111937       }else{
 111938         *tokenType = TK_CONCAT;
 111939         return 2;
 111942     case ',': {
 111943       *tokenType = TK_COMMA;
 111944       return 1;
 111946     case '&': {
 111947       *tokenType = TK_BITAND;
 111948       return 1;
 111950     case '~': {
 111951       *tokenType = TK_BITNOT;
 111952       return 1;
 111954     case '`':
 111955     case '\'':
 111956     case '"': {
 111957       int delim = z[0];
 111958       testcase( delim=='`' );
 111959       testcase( delim=='\'' );
 111960       testcase( delim=='"' );
 111961       for(i=1; (c=z[i])!=0; i++){
 111962         if( c==delim ){
 111963           if( z[i+1]==delim ){
 111964             i++;
 111965           }else{
 111966             break;
 111970       if( c=='\'' ){
 111971         *tokenType = TK_STRING;
 111972         return i+1;
 111973       }else if( c!=0 ){
 111974         *tokenType = TK_ID;
 111975         return i+1;
 111976       }else{
 111977         *tokenType = TK_ILLEGAL;
 111978         return i;
 111981     case '.': {
 111982 #ifndef SQLITE_OMIT_FLOATING_POINT
 111983       if( !sqlite3Isdigit(z[1]) )
 111984 #endif
 111986         *tokenType = TK_DOT;
 111987         return 1;
 111989       /* If the next character is a digit, this is a floating point
 111990       ** number that begins with ".".  Fall thru into the next case */
 111992     case '0': case '1': case '2': case '3': case '4':
 111993     case '5': case '6': case '7': case '8': case '9': {
 111994       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
 111995       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
 111996       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
 111997       testcase( z[0]=='9' );
 111998       *tokenType = TK_INTEGER;
 111999       for(i=0; sqlite3Isdigit(z[i]); i++){}
 112000 #ifndef SQLITE_OMIT_FLOATING_POINT
 112001       if( z[i]=='.' ){
 112002         i++;
 112003         while( sqlite3Isdigit(z[i]) ){ i++; }
 112004         *tokenType = TK_FLOAT;
 112006       if( (z[i]=='e' || z[i]=='E') &&
 112007            ( sqlite3Isdigit(z[i+1]) 
 112008             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
 112011         i += 2;
 112012         while( sqlite3Isdigit(z[i]) ){ i++; }
 112013         *tokenType = TK_FLOAT;
 112015 #endif
 112016       while( IdChar(z[i]) ){
 112017         *tokenType = TK_ILLEGAL;
 112018         i++;
 112020       return i;
 112022     case '[': {
 112023       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
 112024       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
 112025       return i;
 112027     case '?': {
 112028       *tokenType = TK_VARIABLE;
 112029       for(i=1; sqlite3Isdigit(z[i]); i++){}
 112030       return i;
 112032     case '#': {
 112033       for(i=1; sqlite3Isdigit(z[i]); i++){}
 112034       if( i>1 ){
 112035         /* Parameters of the form #NNN (where NNN is a number) are used
 112036         ** internally by sqlite3NestedParse.  */
 112037         *tokenType = TK_REGISTER;
 112038         return i;
 112040       /* Fall through into the next case if the '#' is not followed by
 112041       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
 112043 #ifndef SQLITE_OMIT_TCL_VARIABLE
 112044     case '$':
 112045 #endif
 112046     case '@':  /* For compatibility with MS SQL Server */
 112047     case ':': {
 112048       int n = 0;
 112049       testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
 112050       *tokenType = TK_VARIABLE;
 112051       for(i=1; (c=z[i])!=0; i++){
 112052         if( IdChar(c) ){
 112053           n++;
 112054 #ifndef SQLITE_OMIT_TCL_VARIABLE
 112055         }else if( c=='(' && n>0 ){
 112056           do{
 112057             i++;
 112058           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
 112059           if( c==')' ){
 112060             i++;
 112061           }else{
 112062             *tokenType = TK_ILLEGAL;
 112064           break;
 112065         }else if( c==':' && z[i+1]==':' ){
 112066           i++;
 112067 #endif
 112068         }else{
 112069           break;
 112072       if( n==0 ) *tokenType = TK_ILLEGAL;
 112073       return i;
 112075 #ifndef SQLITE_OMIT_BLOB_LITERAL
 112076     case 'x': case 'X': {
 112077       testcase( z[0]=='x' ); testcase( z[0]=='X' );
 112078       if( z[1]=='\'' ){
 112079         *tokenType = TK_BLOB;
 112080         for(i=2; sqlite3Isxdigit(z[i]); i++){}
 112081         if( z[i]!='\'' || i%2 ){
 112082           *tokenType = TK_ILLEGAL;
 112083           while( z[i] && z[i]!='\'' ){ i++; }
 112085         if( z[i] ) i++;
 112086         return i;
 112088       /* Otherwise fall through to the next case */
 112090 #endif
 112091     default: {
 112092       if( !IdChar(*z) ){
 112093         break;
 112095       for(i=1; IdChar(z[i]); i++){}
 112096       *tokenType = keywordCode((char*)z, i);
 112097       return i;
 112100   *tokenType = TK_ILLEGAL;
 112101   return 1;
 112105 ** Run the parser on the given SQL string.  The parser structure is
 112106 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
 112107 ** then an and attempt is made to write an error message into 
 112108 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
 112109 ** error message.
 112111 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
 112112   int nErr = 0;                   /* Number of errors encountered */
 112113   int i;                          /* Loop counter */
 112114   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
 112115   int tokenType;                  /* type of the next token */
 112116   int lastTokenParsed = -1;       /* type of the previous token */
 112117   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
 112118   sqlite3 *db = pParse->db;       /* The database connection */
 112119   int mxSqlLen;                   /* Max length of an SQL string */
 112122   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
 112123   if( db->activeVdbeCnt==0 ){
 112124     db->u1.isInterrupted = 0;
 112126   pParse->rc = SQLITE_OK;
 112127   pParse->zTail = zSql;
 112128   i = 0;
 112129   assert( pzErrMsg!=0 );
 112130   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
 112131   if( pEngine==0 ){
 112132     db->mallocFailed = 1;
 112133     return SQLITE_NOMEM;
 112135   assert( pParse->pNewTable==0 );
 112136   assert( pParse->pNewTrigger==0 );
 112137   assert( pParse->nVar==0 );
 112138   assert( pParse->nzVar==0 );
 112139   assert( pParse->azVar==0 );
 112140   enableLookaside = db->lookaside.bEnabled;
 112141   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
 112142   while( !db->mallocFailed && zSql[i]!=0 ){
 112143     assert( i>=0 );
 112144     pParse->sLastToken.z = &zSql[i];
 112145     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
 112146     i += pParse->sLastToken.n;
 112147     if( i>mxSqlLen ){
 112148       pParse->rc = SQLITE_TOOBIG;
 112149       break;
 112151     switch( tokenType ){
 112152       case TK_SPACE: {
 112153         if( db->u1.isInterrupted ){
 112154           sqlite3ErrorMsg(pParse, "interrupt");
 112155           pParse->rc = SQLITE_INTERRUPT;
 112156           goto abort_parse;
 112158         break;
 112160       case TK_ILLEGAL: {
 112161         sqlite3DbFree(db, *pzErrMsg);
 112162         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
 112163                         &pParse->sLastToken);
 112164         nErr++;
 112165         goto abort_parse;
 112167       case TK_SEMI: {
 112168         pParse->zTail = &zSql[i];
 112169         /* Fall thru into the default case */
 112171       default: {
 112172         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
 112173         lastTokenParsed = tokenType;
 112174         if( pParse->rc!=SQLITE_OK ){
 112175           goto abort_parse;
 112177         break;
 112181 abort_parse:
 112182   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
 112183     if( lastTokenParsed!=TK_SEMI ){
 112184       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
 112185       pParse->zTail = &zSql[i];
 112187     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
 112189 #ifdef YYTRACKMAXSTACKDEPTH
 112190   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
 112191       sqlite3ParserStackPeak(pEngine)
 112193 #endif /* YYDEBUG */
 112194   sqlite3ParserFree(pEngine, sqlite3_free);
 112195   db->lookaside.bEnabled = enableLookaside;
 112196   if( db->mallocFailed ){
 112197     pParse->rc = SQLITE_NOMEM;
 112199   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
 112200     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
 112202   assert( pzErrMsg!=0 );
 112203   if( pParse->zErrMsg ){
 112204     *pzErrMsg = pParse->zErrMsg;
 112205     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
 112206     pParse->zErrMsg = 0;
 112207     nErr++;
 112209   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
 112210     sqlite3VdbeDelete(pParse->pVdbe);
 112211     pParse->pVdbe = 0;
 112213 #ifndef SQLITE_OMIT_SHARED_CACHE
 112214   if( pParse->nested==0 ){
 112215     sqlite3DbFree(db, pParse->aTableLock);
 112216     pParse->aTableLock = 0;
 112217     pParse->nTableLock = 0;
 112219 #endif
 112220 #ifndef SQLITE_OMIT_VIRTUALTABLE
 112221   sqlite3_free(pParse->apVtabLock);
 112222 #endif
 112224   if( !IN_DECLARE_VTAB ){
 112225     /* If the pParse->declareVtab flag is set, do not delete any table 
 112226     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
 112227     ** will take responsibility for freeing the Table structure.
 112229     sqlite3DeleteTable(db, pParse->pNewTable);
 112232   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
 112233   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
 112234   sqlite3DbFree(db, pParse->azVar);
 112235   sqlite3DbFree(db, pParse->aAlias);
 112236   while( pParse->pAinc ){
 112237     AutoincInfo *p = pParse->pAinc;
 112238     pParse->pAinc = p->pNext;
 112239     sqlite3DbFree(db, p);
 112241   while( pParse->pZombieTab ){
 112242     Table *p = pParse->pZombieTab;
 112243     pParse->pZombieTab = p->pNextZombie;
 112244     sqlite3DeleteTable(db, p);
 112246   if( nErr>0 && pParse->rc==SQLITE_OK ){
 112247     pParse->rc = SQLITE_ERROR;
 112249   return nErr;
 112252 /************** End of tokenize.c ********************************************/
 112253 /************** Begin file complete.c ****************************************/
 112255 ** 2001 September 15
 112257 ** The author disclaims copyright to this source code.  In place of
 112258 ** a legal notice, here is a blessing:
 112260 **    May you do good and not evil.
 112261 **    May you find forgiveness for yourself and forgive others.
 112262 **    May you share freely, never taking more than you give.
 112264 *************************************************************************
 112265 ** An tokenizer for SQL
 112267 ** This file contains C code that implements the sqlite3_complete() API.
 112268 ** This code used to be part of the tokenizer.c source file.  But by
 112269 ** separating it out, the code will be automatically omitted from
 112270 ** static links that do not use it.
 112272 #ifndef SQLITE_OMIT_COMPLETE
 112275 ** This is defined in tokenize.c.  We just have to import the definition.
 112277 #ifndef SQLITE_AMALGAMATION
 112278 #ifdef SQLITE_ASCII
 112279 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
 112280 #endif
 112281 #ifdef SQLITE_EBCDIC
 112282 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
 112283 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
 112284 #endif
 112285 #endif /* SQLITE_AMALGAMATION */
 112289 ** Token types used by the sqlite3_complete() routine.  See the header
 112290 ** comments on that procedure for additional information.
 112292 #define tkSEMI    0
 112293 #define tkWS      1
 112294 #define tkOTHER   2
 112295 #ifndef SQLITE_OMIT_TRIGGER
 112296 #define tkEXPLAIN 3
 112297 #define tkCREATE  4
 112298 #define tkTEMP    5
 112299 #define tkTRIGGER 6
 112300 #define tkEND     7
 112301 #endif
 112304 ** Return TRUE if the given SQL string ends in a semicolon.
 112306 ** Special handling is require for CREATE TRIGGER statements.
 112307 ** Whenever the CREATE TRIGGER keywords are seen, the statement
 112308 ** must end with ";END;".
 112310 ** This implementation uses a state machine with 8 states:
 112312 **   (0) INVALID   We have not yet seen a non-whitespace character.
 112314 **   (1) START     At the beginning or end of an SQL statement.  This routine
 112315 **                 returns 1 if it ends in the START state and 0 if it ends
 112316 **                 in any other state.
 112318 **   (2) NORMAL    We are in the middle of statement which ends with a single
 112319 **                 semicolon.
 112321 **   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
 112322 **                 a statement.
 112324 **   (4) CREATE    The keyword CREATE has been seen at the beginning of a
 112325 **                 statement, possibly preceeded by EXPLAIN and/or followed by
 112326 **                 TEMP or TEMPORARY
 112328 **   (5) TRIGGER   We are in the middle of a trigger definition that must be
 112329 **                 ended by a semicolon, the keyword END, and another semicolon.
 112331 **   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
 112332 **                 the end of a trigger definition.
 112334 **   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
 112335 **                 of a trigger difinition.
 112337 ** Transitions between states above are determined by tokens extracted
 112338 ** from the input.  The following tokens are significant:
 112340 **   (0) tkSEMI      A semicolon.
 112341 **   (1) tkWS        Whitespace.
 112342 **   (2) tkOTHER     Any other SQL token.
 112343 **   (3) tkEXPLAIN   The "explain" keyword.
 112344 **   (4) tkCREATE    The "create" keyword.
 112345 **   (5) tkTEMP      The "temp" or "temporary" keyword.
 112346 **   (6) tkTRIGGER   The "trigger" keyword.
 112347 **   (7) tkEND       The "end" keyword.
 112349 ** Whitespace never causes a state transition and is always ignored.
 112350 ** This means that a SQL string of all whitespace is invalid.
 112352 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
 112353 ** to recognize the end of a trigger can be omitted.  All we have to do
 112354 ** is look for a semicolon that is not part of an string or comment.
 112356 SQLITE_API int sqlite3_complete(const char *zSql){
 112357   u8 state = 0;   /* Current state, using numbers defined in header comment */
 112358   u8 token;       /* Value of the next token */
 112360 #ifndef SQLITE_OMIT_TRIGGER
 112361   /* A complex statement machine used to detect the end of a CREATE TRIGGER
 112362   ** statement.  This is the normal case.
 112364   static const u8 trans[8][8] = {
 112365                      /* Token:                                                */
 112366      /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
 112367      /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
 112368      /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
 112369      /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
 112370      /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
 112371      /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
 112372      /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
 112373      /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
 112374      /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
 112376 #else
 112377   /* If triggers are not supported by this compile then the statement machine
 112378   ** used to detect the end of a statement is much simplier
 112380   static const u8 trans[3][3] = {
 112381                      /* Token:           */
 112382      /* State:       **  SEMI  WS  OTHER */
 112383      /* 0 INVALID: */ {    1,  0,     2, },
 112384      /* 1   START: */ {    1,  1,     2, },
 112385      /* 2  NORMAL: */ {    1,  2,     2, },
 112387 #endif /* SQLITE_OMIT_TRIGGER */
 112389   while( *zSql ){
 112390     switch( *zSql ){
 112391       case ';': {  /* A semicolon */
 112392         token = tkSEMI;
 112393         break;
 112395       case ' ':
 112396       case '\r':
 112397       case '\t':
 112398       case '\n':
 112399       case '\f': {  /* White space is ignored */
 112400         token = tkWS;
 112401         break;
 112403       case '/': {   /* C-style comments */
 112404         if( zSql[1]!='*' ){
 112405           token = tkOTHER;
 112406           break;
 112408         zSql += 2;
 112409         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
 112410         if( zSql[0]==0 ) return 0;
 112411         zSql++;
 112412         token = tkWS;
 112413         break;
 112415       case '-': {   /* SQL-style comments from "--" to end of line */
 112416         if( zSql[1]!='-' ){
 112417           token = tkOTHER;
 112418           break;
 112420         while( *zSql && *zSql!='\n' ){ zSql++; }
 112421         if( *zSql==0 ) return state==1;
 112422         token = tkWS;
 112423         break;
 112425       case '[': {   /* Microsoft-style identifiers in [...] */
 112426         zSql++;
 112427         while( *zSql && *zSql!=']' ){ zSql++; }
 112428         if( *zSql==0 ) return 0;
 112429         token = tkOTHER;
 112430         break;
 112432       case '`':     /* Grave-accent quoted symbols used by MySQL */
 112433       case '"':     /* single- and double-quoted strings */
 112434       case '\'': {
 112435         int c = *zSql;
 112436         zSql++;
 112437         while( *zSql && *zSql!=c ){ zSql++; }
 112438         if( *zSql==0 ) return 0;
 112439         token = tkOTHER;
 112440         break;
 112442       default: {
 112443 #ifdef SQLITE_EBCDIC
 112444         unsigned char c;
 112445 #endif
 112446         if( IdChar((u8)*zSql) ){
 112447           /* Keywords and unquoted identifiers */
 112448           int nId;
 112449           for(nId=1; IdChar(zSql[nId]); nId++){}
 112450 #ifdef SQLITE_OMIT_TRIGGER
 112451           token = tkOTHER;
 112452 #else
 112453           switch( *zSql ){
 112454             case 'c': case 'C': {
 112455               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
 112456                 token = tkCREATE;
 112457               }else{
 112458                 token = tkOTHER;
 112460               break;
 112462             case 't': case 'T': {
 112463               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
 112464                 token = tkTRIGGER;
 112465               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
 112466                 token = tkTEMP;
 112467               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
 112468                 token = tkTEMP;
 112469               }else{
 112470                 token = tkOTHER;
 112472               break;
 112474             case 'e':  case 'E': {
 112475               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
 112476                 token = tkEND;
 112477               }else
 112478 #ifndef SQLITE_OMIT_EXPLAIN
 112479               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
 112480                 token = tkEXPLAIN;
 112481               }else
 112482 #endif
 112484                 token = tkOTHER;
 112486               break;
 112488             default: {
 112489               token = tkOTHER;
 112490               break;
 112493 #endif /* SQLITE_OMIT_TRIGGER */
 112494           zSql += nId-1;
 112495         }else{
 112496           /* Operators and special symbols */
 112497           token = tkOTHER;
 112499         break;
 112502     state = trans[state][token];
 112503     zSql++;
 112505   return state==1;
 112508 #ifndef SQLITE_OMIT_UTF16
 112510 ** This routine is the same as the sqlite3_complete() routine described
 112511 ** above, except that the parameter is required to be UTF-16 encoded, not
 112512 ** UTF-8.
 112514 SQLITE_API int sqlite3_complete16(const void *zSql){
 112515   sqlite3_value *pVal;
 112516   char const *zSql8;
 112517   int rc = SQLITE_NOMEM;
 112519 #ifndef SQLITE_OMIT_AUTOINIT
 112520   rc = sqlite3_initialize();
 112521   if( rc ) return rc;
 112522 #endif
 112523   pVal = sqlite3ValueNew(0);
 112524   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
 112525   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
 112526   if( zSql8 ){
 112527     rc = sqlite3_complete(zSql8);
 112528   }else{
 112529     rc = SQLITE_NOMEM;
 112531   sqlite3ValueFree(pVal);
 112532   return sqlite3ApiExit(0, rc);
 112534 #endif /* SQLITE_OMIT_UTF16 */
 112535 #endif /* SQLITE_OMIT_COMPLETE */
 112537 /************** End of complete.c ********************************************/
 112538 /************** Begin file main.c ********************************************/
 112540 ** 2001 September 15
 112542 ** The author disclaims copyright to this source code.  In place of
 112543 ** a legal notice, here is a blessing:
 112545 **    May you do good and not evil.
 112546 **    May you find forgiveness for yourself and forgive others.
 112547 **    May you share freely, never taking more than you give.
 112549 *************************************************************************
 112550 ** Main file for the SQLite library.  The routines in this file
 112551 ** implement the programmer interface to the library.  Routines in
 112552 ** other files are for internal use by SQLite and should not be
 112553 ** accessed by users of the library.
 112556 #ifdef SQLITE_ENABLE_FTS3
 112557 /************** Include fts3.h in the middle of main.c ***********************/
 112558 /************** Begin file fts3.h ********************************************/
 112560 ** 2006 Oct 10
 112562 ** The author disclaims copyright to this source code.  In place of
 112563 ** a legal notice, here is a blessing:
 112565 **    May you do good and not evil.
 112566 **    May you find forgiveness for yourself and forgive others.
 112567 **    May you share freely, never taking more than you give.
 112569 ******************************************************************************
 112571 ** This header file is used by programs that want to link against the
 112572 ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
 112575 #if 0
 112576 extern "C" {
 112577 #endif  /* __cplusplus */
 112579 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
 112581 #if 0
 112582 }  /* extern "C" */
 112583 #endif  /* __cplusplus */
 112585 /************** End of fts3.h ************************************************/
 112586 /************** Continuing where we left off in main.c ***********************/
 112587 #endif
 112588 #ifdef SQLITE_ENABLE_RTREE
 112589 /************** Include rtree.h in the middle of main.c **********************/
 112590 /************** Begin file rtree.h *******************************************/
 112592 ** 2008 May 26
 112594 ** The author disclaims copyright to this source code.  In place of
 112595 ** a legal notice, here is a blessing:
 112597 **    May you do good and not evil.
 112598 **    May you find forgiveness for yourself and forgive others.
 112599 **    May you share freely, never taking more than you give.
 112601 ******************************************************************************
 112603 ** This header file is used by programs that want to link against the
 112604 ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
 112607 #if 0
 112608 extern "C" {
 112609 #endif  /* __cplusplus */
 112611 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
 112613 #if 0
 112614 }  /* extern "C" */
 112615 #endif  /* __cplusplus */
 112617 /************** End of rtree.h ***********************************************/
 112618 /************** Continuing where we left off in main.c ***********************/
 112619 #endif
 112620 #ifdef SQLITE_ENABLE_ICU
 112621 /************** Include sqliteicu.h in the middle of main.c ******************/
 112622 /************** Begin file sqliteicu.h ***************************************/
 112624 ** 2008 May 26
 112626 ** The author disclaims copyright to this source code.  In place of
 112627 ** a legal notice, here is a blessing:
 112629 **    May you do good and not evil.
 112630 **    May you find forgiveness for yourself and forgive others.
 112631 **    May you share freely, never taking more than you give.
 112633 ******************************************************************************
 112635 ** This header file is used by programs that want to link against the
 112636 ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
 112639 #if 0
 112640 extern "C" {
 112641 #endif  /* __cplusplus */
 112643 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
 112645 #if 0
 112646 }  /* extern "C" */
 112647 #endif  /* __cplusplus */
 112650 /************** End of sqliteicu.h *******************************************/
 112651 /************** Continuing where we left off in main.c ***********************/
 112652 #endif
 112654 #ifndef SQLITE_AMALGAMATION
 112655 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
 112656 ** contains the text of SQLITE_VERSION macro. 
 112658 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
 112659 #endif
 112661 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
 112662 ** a pointer to the to the sqlite3_version[] string constant. 
 112664 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
 112666 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
 112667 ** pointer to a string constant whose value is the same as the
 112668 ** SQLITE_SOURCE_ID C preprocessor macro. 
 112670 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
 112672 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
 112673 ** returns an integer equal to SQLITE_VERSION_NUMBER.
 112675 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
 112677 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
 112678 ** zero if and only if SQLite was compiled with mutexing code omitted due to
 112679 ** the SQLITE_THREADSAFE compile-time option being set to 0.
 112681 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
 112683 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
 112685 ** If the following function pointer is not NULL and if
 112686 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
 112687 ** I/O active are written using this function.  These messages
 112688 ** are intended for debugging activity only.
 112690 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
 112691 #endif
 112694 ** If the following global variable points to a string which is the
 112695 ** name of a directory, then that directory will be used to store
 112696 ** temporary files.
 112698 ** See also the "PRAGMA temp_store_directory" SQL command.
 112700 SQLITE_API char *sqlite3_temp_directory = 0;
 112703 ** If the following global variable points to a string which is the
 112704 ** name of a directory, then that directory will be used to store
 112705 ** all database files specified with a relative pathname.
 112707 ** See also the "PRAGMA data_store_directory" SQL command.
 112709 SQLITE_API char *sqlite3_data_directory = 0;
 112712 ** Initialize SQLite.  
 112714 ** This routine must be called to initialize the memory allocation,
 112715 ** VFS, and mutex subsystems prior to doing any serious work with
 112716 ** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
 112717 ** this routine will be called automatically by key routines such as
 112718 ** sqlite3_open().  
 112720 ** This routine is a no-op except on its very first call for the process,
 112721 ** or for the first call after a call to sqlite3_shutdown.
 112723 ** The first thread to call this routine runs the initialization to
 112724 ** completion.  If subsequent threads call this routine before the first
 112725 ** thread has finished the initialization process, then the subsequent
 112726 ** threads must block until the first thread finishes with the initialization.
 112728 ** The first thread might call this routine recursively.  Recursive
 112729 ** calls to this routine should not block, of course.  Otherwise the
 112730 ** initialization process would never complete.
 112732 ** Let X be the first thread to enter this routine.  Let Y be some other
 112733 ** thread.  Then while the initial invocation of this routine by X is
 112734 ** incomplete, it is required that:
 112736 **    *  Calls to this routine from Y must block until the outer-most
 112737 **       call by X completes.
 112739 **    *  Recursive calls to this routine from thread X return immediately
 112740 **       without blocking.
 112742 SQLITE_API int sqlite3_initialize(void){
 112743   MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
 112744   int rc;                                      /* Result code */
 112746 #ifdef SQLITE_OMIT_WSD
 112747   rc = sqlite3_wsd_init(4096, 24);
 112748   if( rc!=SQLITE_OK ){
 112749     return rc;
 112751 #endif
 112753   /* If SQLite is already completely initialized, then this call
 112754   ** to sqlite3_initialize() should be a no-op.  But the initialization
 112755   ** must be complete.  So isInit must not be set until the very end
 112756   ** of this routine.
 112758   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
 112760 #ifdef SQLITE_ENABLE_SQLLOG
 112762     extern void sqlite3_init_sqllog(void);
 112763     sqlite3_init_sqllog();
 112765 #endif
 112767   /* Make sure the mutex subsystem is initialized.  If unable to 
 112768   ** initialize the mutex subsystem, return early with the error.
 112769   ** If the system is so sick that we are unable to allocate a mutex,
 112770   ** there is not much SQLite is going to be able to do.
 112772   ** The mutex subsystem must take care of serializing its own
 112773   ** initialization.
 112775   rc = sqlite3MutexInit();
 112776   if( rc ) return rc;
 112778   /* Initialize the malloc() system and the recursive pInitMutex mutex.
 112779   ** This operation is protected by the STATIC_MASTER mutex.  Note that
 112780   ** MutexAlloc() is called for a static mutex prior to initializing the
 112781   ** malloc subsystem - this implies that the allocation of a static
 112782   ** mutex must not require support from the malloc subsystem.
 112784   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 112785   sqlite3_mutex_enter(pMaster);
 112786   sqlite3GlobalConfig.isMutexInit = 1;
 112787   if( !sqlite3GlobalConfig.isMallocInit ){
 112788     rc = sqlite3MallocInit();
 112790   if( rc==SQLITE_OK ){
 112791     sqlite3GlobalConfig.isMallocInit = 1;
 112792     if( !sqlite3GlobalConfig.pInitMutex ){
 112793       sqlite3GlobalConfig.pInitMutex =
 112794            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
 112795       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
 112796         rc = SQLITE_NOMEM;
 112800   if( rc==SQLITE_OK ){
 112801     sqlite3GlobalConfig.nRefInitMutex++;
 112803   sqlite3_mutex_leave(pMaster);
 112805   /* If rc is not SQLITE_OK at this point, then either the malloc
 112806   ** subsystem could not be initialized or the system failed to allocate
 112807   ** the pInitMutex mutex. Return an error in either case.  */
 112808   if( rc!=SQLITE_OK ){
 112809     return rc;
 112812   /* Do the rest of the initialization under the recursive mutex so
 112813   ** that we will be able to handle recursive calls into
 112814   ** sqlite3_initialize().  The recursive calls normally come through
 112815   ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
 112816   ** recursive calls might also be possible.
 112818   ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
 112819   ** to the xInit method, so the xInit method need not be threadsafe.
 112821   ** The following mutex is what serializes access to the appdef pcache xInit
 112822   ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
 112823   ** call to sqlite3PcacheInitialize().
 112825   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
 112826   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
 112827     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 112828     sqlite3GlobalConfig.inProgress = 1;
 112829     memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
 112830     sqlite3RegisterGlobalFunctions();
 112831     if( sqlite3GlobalConfig.isPCacheInit==0 ){
 112832       rc = sqlite3PcacheInitialize();
 112834     if( rc==SQLITE_OK ){
 112835       sqlite3GlobalConfig.isPCacheInit = 1;
 112836       rc = sqlite3OsInit();
 112838     if( rc==SQLITE_OK ){
 112839       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
 112840           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
 112841       sqlite3GlobalConfig.isInit = 1;
 112843     sqlite3GlobalConfig.inProgress = 0;
 112845   sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
 112847   /* Go back under the static mutex and clean up the recursive
 112848   ** mutex to prevent a resource leak.
 112850   sqlite3_mutex_enter(pMaster);
 112851   sqlite3GlobalConfig.nRefInitMutex--;
 112852   if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
 112853     assert( sqlite3GlobalConfig.nRefInitMutex==0 );
 112854     sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
 112855     sqlite3GlobalConfig.pInitMutex = 0;
 112857   sqlite3_mutex_leave(pMaster);
 112859   /* The following is just a sanity check to make sure SQLite has
 112860   ** been compiled correctly.  It is important to run this code, but
 112861   ** we don't want to run it too often and soak up CPU cycles for no
 112862   ** reason.  So we run it once during initialization.
 112864 #ifndef NDEBUG
 112865 #ifndef SQLITE_OMIT_FLOATING_POINT
 112866   /* This section of code's only "output" is via assert() statements. */
 112867   if ( rc==SQLITE_OK ){
 112868     u64 x = (((u64)1)<<63)-1;
 112869     double y;
 112870     assert(sizeof(x)==8);
 112871     assert(sizeof(x)==sizeof(y));
 112872     memcpy(&y, &x, 8);
 112873     assert( sqlite3IsNaN(y) );
 112875 #endif
 112876 #endif
 112878   /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
 112879   ** compile-time option.
 112881 #ifdef SQLITE_EXTRA_INIT
 112882   if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
 112883     int SQLITE_EXTRA_INIT(const char*);
 112884     rc = SQLITE_EXTRA_INIT(0);
 112886 #endif
 112888   return rc;
 112892 ** Undo the effects of sqlite3_initialize().  Must not be called while
 112893 ** there are outstanding database connections or memory allocations or
 112894 ** while any part of SQLite is otherwise in use in any thread.  This
 112895 ** routine is not threadsafe.  But it is safe to invoke this routine
 112896 ** on when SQLite is already shut down.  If SQLite is already shut down
 112897 ** when this routine is invoked, then this routine is a harmless no-op.
 112899 SQLITE_API int sqlite3_shutdown(void){
 112900   if( sqlite3GlobalConfig.isInit ){
 112901 #ifdef SQLITE_EXTRA_SHUTDOWN
 112902     void SQLITE_EXTRA_SHUTDOWN(void);
 112903     SQLITE_EXTRA_SHUTDOWN();
 112904 #endif
 112905     sqlite3_os_end();
 112906     sqlite3_reset_auto_extension();
 112907     sqlite3GlobalConfig.isInit = 0;
 112909   if( sqlite3GlobalConfig.isPCacheInit ){
 112910     sqlite3PcacheShutdown();
 112911     sqlite3GlobalConfig.isPCacheInit = 0;
 112913   if( sqlite3GlobalConfig.isMallocInit ){
 112914     sqlite3MallocEnd();
 112915     sqlite3GlobalConfig.isMallocInit = 0;
 112917 #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
 112918     /* The heap subsystem has now been shutdown and these values are supposed
 112919     ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
 112920     ** which would rely on that heap subsystem; therefore, make sure these
 112921     ** values cannot refer to heap memory that was just invalidated when the
 112922     ** heap subsystem was shutdown.  This is only done if the current call to
 112923     ** this function resulted in the heap subsystem actually being shutdown.
 112925     sqlite3_data_directory = 0;
 112926     sqlite3_temp_directory = 0;
 112927 #endif
 112929   if( sqlite3GlobalConfig.isMutexInit ){
 112930     sqlite3MutexEnd();
 112931     sqlite3GlobalConfig.isMutexInit = 0;
 112934   return SQLITE_OK;
 112938 ** This API allows applications to modify the global configuration of
 112939 ** the SQLite library at run-time.
 112941 ** This routine should only be called when there are no outstanding
 112942 ** database connections or memory allocations.  This routine is not
 112943 ** threadsafe.  Failure to heed these warnings can lead to unpredictable
 112944 ** behavior.
 112946 SQLITE_API int sqlite3_config(int op, ...){
 112947   va_list ap;
 112948   int rc = SQLITE_OK;
 112950   /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
 112951   ** the SQLite library is in use. */
 112952   if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
 112954   va_start(ap, op);
 112955   switch( op ){
 112957     /* Mutex configuration options are only available in a threadsafe
 112958     ** compile. 
 112960 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
 112961     case SQLITE_CONFIG_SINGLETHREAD: {
 112962       /* Disable all mutexing */
 112963       sqlite3GlobalConfig.bCoreMutex = 0;
 112964       sqlite3GlobalConfig.bFullMutex = 0;
 112965       break;
 112967     case SQLITE_CONFIG_MULTITHREAD: {
 112968       /* Disable mutexing of database connections */
 112969       /* Enable mutexing of core data structures */
 112970       sqlite3GlobalConfig.bCoreMutex = 1;
 112971       sqlite3GlobalConfig.bFullMutex = 0;
 112972       break;
 112974     case SQLITE_CONFIG_SERIALIZED: {
 112975       /* Enable all mutexing */
 112976       sqlite3GlobalConfig.bCoreMutex = 1;
 112977       sqlite3GlobalConfig.bFullMutex = 1;
 112978       break;
 112980     case SQLITE_CONFIG_MUTEX: {
 112981       /* Specify an alternative mutex implementation */
 112982       sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
 112983       break;
 112985     case SQLITE_CONFIG_GETMUTEX: {
 112986       /* Retrieve the current mutex implementation */
 112987       *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
 112988       break;
 112990 #endif
 112993     case SQLITE_CONFIG_MALLOC: {
 112994       /* Specify an alternative malloc implementation */
 112995       sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
 112996       break;
 112998     case SQLITE_CONFIG_GETMALLOC: {
 112999       /* Retrieve the current malloc() implementation */
 113000       if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
 113001       *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
 113002       break;
 113004     case SQLITE_CONFIG_MEMSTATUS: {
 113005       /* Enable or disable the malloc status collection */
 113006       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
 113007       break;
 113009     case SQLITE_CONFIG_SCRATCH: {
 113010       /* Designate a buffer for scratch memory space */
 113011       sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
 113012       sqlite3GlobalConfig.szScratch = va_arg(ap, int);
 113013       sqlite3GlobalConfig.nScratch = va_arg(ap, int);
 113014       break;
 113016     case SQLITE_CONFIG_PAGECACHE: {
 113017       /* Designate a buffer for page cache memory space */
 113018       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
 113019       sqlite3GlobalConfig.szPage = va_arg(ap, int);
 113020       sqlite3GlobalConfig.nPage = va_arg(ap, int);
 113021       break;
 113024     case SQLITE_CONFIG_PCACHE: {
 113025       /* no-op */
 113026       break;
 113028     case SQLITE_CONFIG_GETPCACHE: {
 113029       /* now an error */
 113030       rc = SQLITE_ERROR;
 113031       break;
 113034     case SQLITE_CONFIG_PCACHE2: {
 113035       /* Specify an alternative page cache implementation */
 113036       sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
 113037       break;
 113039     case SQLITE_CONFIG_GETPCACHE2: {
 113040       if( sqlite3GlobalConfig.pcache2.xInit==0 ){
 113041         sqlite3PCacheSetDefault();
 113043       *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
 113044       break;
 113047 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
 113048     case SQLITE_CONFIG_HEAP: {
 113049       /* Designate a buffer for heap memory space */
 113050       sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
 113051       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
 113052       sqlite3GlobalConfig.mnReq = va_arg(ap, int);
 113054       if( sqlite3GlobalConfig.mnReq<1 ){
 113055         sqlite3GlobalConfig.mnReq = 1;
 113056       }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
 113057         /* cap min request size at 2^12 */
 113058         sqlite3GlobalConfig.mnReq = (1<<12);
 113061       if( sqlite3GlobalConfig.pHeap==0 ){
 113062         /* If the heap pointer is NULL, then restore the malloc implementation
 113063         ** back to NULL pointers too.  This will cause the malloc to go
 113064         ** back to its default implementation when sqlite3_initialize() is
 113065         ** run.
 113067         memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
 113068       }else{
 113069         /* The heap pointer is not NULL, then install one of the
 113070         ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
 113071         ** ENABLE_MEMSYS5 is defined, return an error.
 113073 #ifdef SQLITE_ENABLE_MEMSYS3
 113074         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
 113075 #endif
 113076 #ifdef SQLITE_ENABLE_MEMSYS5
 113077         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
 113078 #endif
 113080       break;
 113082 #endif
 113084     case SQLITE_CONFIG_LOOKASIDE: {
 113085       sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
 113086       sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
 113087       break;
 113090     /* Record a pointer to the logger funcction and its first argument.
 113091     ** The default is NULL.  Logging is disabled if the function pointer is
 113092     ** NULL.
 113094     case SQLITE_CONFIG_LOG: {
 113095       /* MSVC is picky about pulling func ptrs from va lists.
 113096       ** http://support.microsoft.com/kb/47961
 113097       ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
 113099       typedef void(*LOGFUNC_t)(void*,int,const char*);
 113100       sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
 113101       sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
 113102       break;
 113105     case SQLITE_CONFIG_URI: {
 113106       sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
 113107       break;
 113110     case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
 113111       sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
 113112       break;
 113115 #ifdef SQLITE_ENABLE_SQLLOG
 113116     case SQLITE_CONFIG_SQLLOG: {
 113117       typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
 113118       sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
 113119       sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
 113120       break;
 113122 #endif
 113124     default: {
 113125       rc = SQLITE_ERROR;
 113126       break;
 113129   va_end(ap);
 113130   return rc;
 113134 ** Set up the lookaside buffers for a database connection.
 113135 ** Return SQLITE_OK on success.  
 113136 ** If lookaside is already active, return SQLITE_BUSY.
 113138 ** The sz parameter is the number of bytes in each lookaside slot.
 113139 ** The cnt parameter is the number of slots.  If pStart is NULL the
 113140 ** space for the lookaside memory is obtained from sqlite3_malloc().
 113141 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
 113142 ** the lookaside memory.
 113144 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
 113145   void *pStart;
 113146   if( db->lookaside.nOut ){
 113147     return SQLITE_BUSY;
 113149   /* Free any existing lookaside buffer for this handle before
 113150   ** allocating a new one so we don't have to have space for 
 113151   ** both at the same time.
 113153   if( db->lookaside.bMalloced ){
 113154     sqlite3_free(db->lookaside.pStart);
 113156   /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
 113157   ** than a pointer to be useful.
 113159   sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
 113160   if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
 113161   if( cnt<0 ) cnt = 0;
 113162   if( sz==0 || cnt==0 ){
 113163     sz = 0;
 113164     pStart = 0;
 113165   }else if( pBuf==0 ){
 113166     sqlite3BeginBenignMalloc();
 113167     pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
 113168     sqlite3EndBenignMalloc();
 113169     if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
 113170   }else{
 113171     pStart = pBuf;
 113173   db->lookaside.pStart = pStart;
 113174   db->lookaside.pFree = 0;
 113175   db->lookaside.sz = (u16)sz;
 113176   if( pStart ){
 113177     int i;
 113178     LookasideSlot *p;
 113179     assert( sz > (int)sizeof(LookasideSlot*) );
 113180     p = (LookasideSlot*)pStart;
 113181     for(i=cnt-1; i>=0; i--){
 113182       p->pNext = db->lookaside.pFree;
 113183       db->lookaside.pFree = p;
 113184       p = (LookasideSlot*)&((u8*)p)[sz];
 113186     db->lookaside.pEnd = p;
 113187     db->lookaside.bEnabled = 1;
 113188     db->lookaside.bMalloced = pBuf==0 ?1:0;
 113189   }else{
 113190     db->lookaside.pEnd = 0;
 113191     db->lookaside.bEnabled = 0;
 113192     db->lookaside.bMalloced = 0;
 113194   return SQLITE_OK;
 113198 ** Return the mutex associated with a database connection.
 113200 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
 113201   return db->mutex;
 113205 ** Free up as much memory as we can from the given database
 113206 ** connection.
 113208 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
 113209   int i;
 113210   sqlite3_mutex_enter(db->mutex);
 113211   sqlite3BtreeEnterAll(db);
 113212   for(i=0; i<db->nDb; i++){
 113213     Btree *pBt = db->aDb[i].pBt;
 113214     if( pBt ){
 113215       Pager *pPager = sqlite3BtreePager(pBt);
 113216       sqlite3PagerShrink(pPager);
 113219   sqlite3BtreeLeaveAll(db);
 113220   sqlite3_mutex_leave(db->mutex);
 113221   return SQLITE_OK;
 113225 ** Configuration settings for an individual database connection
 113227 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
 113228   va_list ap;
 113229   int rc;
 113230   va_start(ap, op);
 113231   switch( op ){
 113232     case SQLITE_DBCONFIG_LOOKASIDE: {
 113233       void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
 113234       int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
 113235       int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
 113236       rc = setupLookaside(db, pBuf, sz, cnt);
 113237       break;
 113239     default: {
 113240       static const struct {
 113241         int op;      /* The opcode */
 113242         u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
 113243       } aFlagOp[] = {
 113244         { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
 113245         { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
 113247       unsigned int i;
 113248       rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
 113249       for(i=0; i<ArraySize(aFlagOp); i++){
 113250         if( aFlagOp[i].op==op ){
 113251           int onoff = va_arg(ap, int);
 113252           int *pRes = va_arg(ap, int*);
 113253           int oldFlags = db->flags;
 113254           if( onoff>0 ){
 113255             db->flags |= aFlagOp[i].mask;
 113256           }else if( onoff==0 ){
 113257             db->flags &= ~aFlagOp[i].mask;
 113259           if( oldFlags!=db->flags ){
 113260             sqlite3ExpirePreparedStatements(db);
 113262           if( pRes ){
 113263             *pRes = (db->flags & aFlagOp[i].mask)!=0;
 113265           rc = SQLITE_OK;
 113266           break;
 113269       break;
 113272   va_end(ap);
 113273   return rc;
 113278 ** Return true if the buffer z[0..n-1] contains all spaces.
 113280 static int allSpaces(const char *z, int n){
 113281   while( n>0 && z[n-1]==' ' ){ n--; }
 113282   return n==0;
 113286 ** This is the default collating function named "BINARY" which is always
 113287 ** available.
 113289 ** If the padFlag argument is not NULL then space padding at the end
 113290 ** of strings is ignored.  This implements the RTRIM collation.
 113292 static int binCollFunc(
 113293   void *padFlag,
 113294   int nKey1, const void *pKey1,
 113295   int nKey2, const void *pKey2
 113297   int rc, n;
 113298   n = nKey1<nKey2 ? nKey1 : nKey2;
 113299   rc = memcmp(pKey1, pKey2, n);
 113300   if( rc==0 ){
 113301     if( padFlag
 113302      && allSpaces(((char*)pKey1)+n, nKey1-n)
 113303      && allSpaces(((char*)pKey2)+n, nKey2-n)
 113305       /* Leave rc unchanged at 0 */
 113306     }else{
 113307       rc = nKey1 - nKey2;
 113310   return rc;
 113314 ** Another built-in collating sequence: NOCASE. 
 113316 ** This collating sequence is intended to be used for "case independant
 113317 ** comparison". SQLite's knowledge of upper and lower case equivalents
 113318 ** extends only to the 26 characters used in the English language.
 113320 ** At the moment there is only a UTF-8 implementation.
 113322 static int nocaseCollatingFunc(
 113323   void *NotUsed,
 113324   int nKey1, const void *pKey1,
 113325   int nKey2, const void *pKey2
 113327   int r = sqlite3StrNICmp(
 113328       (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
 113329   UNUSED_PARAMETER(NotUsed);
 113330   if( 0==r ){
 113331     r = nKey1-nKey2;
 113333   return r;
 113337 ** Return the ROWID of the most recent insert
 113339 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
 113340   return db->lastRowid;
 113344 ** Return the number of changes in the most recent call to sqlite3_exec().
 113346 SQLITE_API int sqlite3_changes(sqlite3 *db){
 113347   return db->nChange;
 113351 ** Return the number of changes since the database handle was opened.
 113353 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
 113354   return db->nTotalChange;
 113358 ** Close all open savepoints. This function only manipulates fields of the
 113359 ** database handle object, it does not close any savepoints that may be open
 113360 ** at the b-tree/pager level.
 113362 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
 113363   while( db->pSavepoint ){
 113364     Savepoint *pTmp = db->pSavepoint;
 113365     db->pSavepoint = pTmp->pNext;
 113366     sqlite3DbFree(db, pTmp);
 113368   db->nSavepoint = 0;
 113369   db->nStatement = 0;
 113370   db->isTransactionSavepoint = 0;
 113374 ** Invoke the destructor function associated with FuncDef p, if any. Except,
 113375 ** if this is not the last copy of the function, do not invoke it. Multiple
 113376 ** copies of a single function are created when create_function() is called
 113377 ** with SQLITE_ANY as the encoding.
 113379 static void functionDestroy(sqlite3 *db, FuncDef *p){
 113380   FuncDestructor *pDestructor = p->pDestructor;
 113381   if( pDestructor ){
 113382     pDestructor->nRef--;
 113383     if( pDestructor->nRef==0 ){
 113384       pDestructor->xDestroy(pDestructor->pUserData);
 113385       sqlite3DbFree(db, pDestructor);
 113391 ** Disconnect all sqlite3_vtab objects that belong to database connection
 113392 ** db. This is called when db is being closed.
 113394 static void disconnectAllVtab(sqlite3 *db){
 113395 #ifndef SQLITE_OMIT_VIRTUALTABLE
 113396   int i;
 113397   sqlite3BtreeEnterAll(db);
 113398   for(i=0; i<db->nDb; i++){
 113399     Schema *pSchema = db->aDb[i].pSchema;
 113400     if( db->aDb[i].pSchema ){
 113401       HashElem *p;
 113402       for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
 113403         Table *pTab = (Table *)sqliteHashData(p);
 113404         if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
 113408   sqlite3BtreeLeaveAll(db);
 113409 #else
 113410   UNUSED_PARAMETER(db);
 113411 #endif
 113415 ** Return TRUE if database connection db has unfinalized prepared
 113416 ** statements or unfinished sqlite3_backup objects.  
 113418 static int connectionIsBusy(sqlite3 *db){
 113419   int j;
 113420   assert( sqlite3_mutex_held(db->mutex) );
 113421   if( db->pVdbe ) return 1;
 113422   for(j=0; j<db->nDb; j++){
 113423     Btree *pBt = db->aDb[j].pBt;
 113424     if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
 113426   return 0;
 113430 ** Close an existing SQLite database
 113432 static int sqlite3Close(sqlite3 *db, int forceZombie){
 113433   if( !db ){
 113434     return SQLITE_OK;
 113436   if( !sqlite3SafetyCheckSickOrOk(db) ){
 113437     return SQLITE_MISUSE_BKPT;
 113439   sqlite3_mutex_enter(db->mutex);
 113441   /* Force xDisconnect calls on all virtual tables */
 113442   disconnectAllVtab(db);
 113444   /* If a transaction is open, the disconnectAllVtab() call above
 113445   ** will not have called the xDisconnect() method on any virtual
 113446   ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
 113447   ** call will do so. We need to do this before the check for active
 113448   ** SQL statements below, as the v-table implementation may be storing
 113449   ** some prepared statements internally.
 113451   sqlite3VtabRollback(db);
 113453   /* Legacy behavior (sqlite3_close() behavior) is to return
 113454   ** SQLITE_BUSY if the connection can not be closed immediately.
 113456   if( !forceZombie && connectionIsBusy(db) ){
 113457     sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
 113458        "statements or unfinished backups");
 113459     sqlite3_mutex_leave(db->mutex);
 113460     return SQLITE_BUSY;
 113463 #ifdef SQLITE_ENABLE_SQLLOG
 113464   if( sqlite3GlobalConfig.xSqllog ){
 113465     /* Closing the handle. Fourth parameter is passed the value 2. */
 113466     sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
 113468 #endif
 113470   /* Convert the connection into a zombie and then close it.
 113472   db->magic = SQLITE_MAGIC_ZOMBIE;
 113473   sqlite3LeaveMutexAndCloseZombie(db);
 113474   return SQLITE_OK;
 113478 ** Two variations on the public interface for closing a database
 113479 ** connection. The sqlite3_close() version returns SQLITE_BUSY and
 113480 ** leaves the connection option if there are unfinalized prepared
 113481 ** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
 113482 ** version forces the connection to become a zombie if there are
 113483 ** unclosed resources, and arranges for deallocation when the last
 113484 ** prepare statement or sqlite3_backup closes.
 113486 SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
 113487 SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
 113491 ** Close the mutex on database connection db.
 113493 ** Furthermore, if database connection db is a zombie (meaning that there
 113494 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
 113495 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has
 113496 ** finished, then free all resources.
 113498 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
 113499   HashElem *i;                    /* Hash table iterator */
 113500   int j;
 113502   /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
 113503   ** or if the connection has not yet been closed by sqlite3_close_v2(),
 113504   ** then just leave the mutex and return.
 113506   if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
 113507     sqlite3_mutex_leave(db->mutex);
 113508     return;
 113511   /* If we reach this point, it means that the database connection has
 113512   ** closed all sqlite3_stmt and sqlite3_backup objects and has been
 113513   ** pased to sqlite3_close (meaning that it is a zombie).  Therefore,
 113514   ** go ahead and free all resources.
 113517   /* Free any outstanding Savepoint structures. */
 113518   sqlite3CloseSavepoints(db);
 113520   /* Close all database connections */
 113521   for(j=0; j<db->nDb; j++){
 113522     struct Db *pDb = &db->aDb[j];
 113523     if( pDb->pBt ){
 113524       sqlite3BtreeClose(pDb->pBt);
 113525       pDb->pBt = 0;
 113526       if( j!=1 ){
 113527         pDb->pSchema = 0;
 113531   /* Clear the TEMP schema separately and last */
 113532   if( db->aDb[1].pSchema ){
 113533     sqlite3SchemaClear(db->aDb[1].pSchema);
 113535   sqlite3VtabUnlockList(db);
 113537   /* Free up the array of auxiliary databases */
 113538   sqlite3CollapseDatabaseArray(db);
 113539   assert( db->nDb<=2 );
 113540   assert( db->aDb==db->aDbStatic );
 113542   /* Tell the code in notify.c that the connection no longer holds any
 113543   ** locks and does not require any further unlock-notify callbacks.
 113545   sqlite3ConnectionClosed(db);
 113547   for(j=0; j<ArraySize(db->aFunc.a); j++){
 113548     FuncDef *pNext, *pHash, *p;
 113549     for(p=db->aFunc.a[j]; p; p=pHash){
 113550       pHash = p->pHash;
 113551       while( p ){
 113552         functionDestroy(db, p);
 113553         pNext = p->pNext;
 113554         sqlite3DbFree(db, p);
 113555         p = pNext;
 113559   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
 113560     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
 113561     /* Invoke any destructors registered for collation sequence user data. */
 113562     for(j=0; j<3; j++){
 113563       if( pColl[j].xDel ){
 113564         pColl[j].xDel(pColl[j].pUser);
 113567     sqlite3DbFree(db, pColl);
 113569   sqlite3HashClear(&db->aCollSeq);
 113570 #ifndef SQLITE_OMIT_VIRTUALTABLE
 113571   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
 113572     Module *pMod = (Module *)sqliteHashData(i);
 113573     if( pMod->xDestroy ){
 113574       pMod->xDestroy(pMod->pAux);
 113576     sqlite3DbFree(db, pMod);
 113578   sqlite3HashClear(&db->aModule);
 113579 #endif
 113581   sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
 113582   if( db->pErr ){
 113583     sqlite3ValueFree(db->pErr);
 113585   sqlite3CloseExtensions(db);
 113587   db->magic = SQLITE_MAGIC_ERROR;
 113589   /* The temp-database schema is allocated differently from the other schema
 113590   ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
 113591   ** So it needs to be freed here. Todo: Why not roll the temp schema into
 113592   ** the same sqliteMalloc() as the one that allocates the database 
 113593   ** structure?
 113595   sqlite3DbFree(db, db->aDb[1].pSchema);
 113596   sqlite3_mutex_leave(db->mutex);
 113597   db->magic = SQLITE_MAGIC_CLOSED;
 113598   sqlite3_mutex_free(db->mutex);
 113599   assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
 113600   if( db->lookaside.bMalloced ){
 113601     sqlite3_free(db->lookaside.pStart);
 113603   sqlite3_free(db);
 113607 ** Rollback all database files.  If tripCode is not SQLITE_OK, then
 113608 ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
 113609 ** breaker") and made to return tripCode if there are any further
 113610 ** attempts to use that cursor.
 113612 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
 113613   int i;
 113614   int inTrans = 0;
 113615   assert( sqlite3_mutex_held(db->mutex) );
 113616   sqlite3BeginBenignMalloc();
 113617   for(i=0; i<db->nDb; i++){
 113618     Btree *p = db->aDb[i].pBt;
 113619     if( p ){
 113620       if( sqlite3BtreeIsInTrans(p) ){
 113621         inTrans = 1;
 113623       sqlite3BtreeRollback(p, tripCode);
 113624       db->aDb[i].inTrans = 0;
 113627   sqlite3VtabRollback(db);
 113628   sqlite3EndBenignMalloc();
 113630   if( db->flags&SQLITE_InternChanges ){
 113631     sqlite3ExpirePreparedStatements(db);
 113632     sqlite3ResetAllSchemasOfConnection(db);
 113635   /* Any deferred constraint violations have now been resolved. */
 113636   db->nDeferredCons = 0;
 113638   /* If one has been configured, invoke the rollback-hook callback */
 113639   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
 113640     db->xRollbackCallback(db->pRollbackArg);
 113645 ** Return a static string that describes the kind of error specified in the
 113646 ** argument.
 113648 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
 113649   static const char* const aMsg[] = {
 113650     /* SQLITE_OK          */ "not an error",
 113651     /* SQLITE_ERROR       */ "SQL logic error or missing database",
 113652     /* SQLITE_INTERNAL    */ 0,
 113653     /* SQLITE_PERM        */ "access permission denied",
 113654     /* SQLITE_ABORT       */ "callback requested query abort",
 113655     /* SQLITE_BUSY        */ "database is locked",
 113656     /* SQLITE_LOCKED      */ "database table is locked",
 113657     /* SQLITE_NOMEM       */ "out of memory",
 113658     /* SQLITE_READONLY    */ "attempt to write a readonly database",
 113659     /* SQLITE_INTERRUPT   */ "interrupted",
 113660     /* SQLITE_IOERR       */ "disk I/O error",
 113661     /* SQLITE_CORRUPT     */ "database disk image is malformed",
 113662     /* SQLITE_NOTFOUND    */ "unknown operation",
 113663     /* SQLITE_FULL        */ "database or disk is full",
 113664     /* SQLITE_CANTOPEN    */ "unable to open database file",
 113665     /* SQLITE_PROTOCOL    */ "locking protocol",
 113666     /* SQLITE_EMPTY       */ "table contains no data",
 113667     /* SQLITE_SCHEMA      */ "database schema has changed",
 113668     /* SQLITE_TOOBIG      */ "string or blob too big",
 113669     /* SQLITE_CONSTRAINT  */ "constraint failed",
 113670     /* SQLITE_MISMATCH    */ "datatype mismatch",
 113671     /* SQLITE_MISUSE      */ "library routine called out of sequence",
 113672     /* SQLITE_NOLFS       */ "large file support is disabled",
 113673     /* SQLITE_AUTH        */ "authorization denied",
 113674     /* SQLITE_FORMAT      */ "auxiliary database format error",
 113675     /* SQLITE_RANGE       */ "bind or column index out of range",
 113676     /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
 113678   const char *zErr = "unknown error";
 113679   switch( rc ){
 113680     case SQLITE_ABORT_ROLLBACK: {
 113681       zErr = "abort due to ROLLBACK";
 113682       break;
 113684     default: {
 113685       rc &= 0xff;
 113686       if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
 113687         zErr = aMsg[rc];
 113689       break;
 113692   return zErr;
 113696 ** This routine implements a busy callback that sleeps and tries
 113697 ** again until a timeout value is reached.  The timeout value is
 113698 ** an integer number of milliseconds passed in as the first
 113699 ** argument.
 113701 static int sqliteDefaultBusyCallback(
 113702  void *ptr,               /* Database connection */
 113703  int count                /* Number of times table has been busy */
 113705 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
 113706   static const u8 delays[] =
 113707      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
 113708   static const u8 totals[] =
 113709      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
 113710 # define NDELAY ArraySize(delays)
 113711   sqlite3 *db = (sqlite3 *)ptr;
 113712   int timeout = db->busyTimeout;
 113713   int delay, prior;
 113715   assert( count>=0 );
 113716   if( count < NDELAY ){
 113717     delay = delays[count];
 113718     prior = totals[count];
 113719   }else{
 113720     delay = delays[NDELAY-1];
 113721     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
 113723   if( prior + delay > timeout ){
 113724     delay = timeout - prior;
 113725     if( delay<=0 ) return 0;
 113727   sqlite3OsSleep(db->pVfs, delay*1000);
 113728   return 1;
 113729 #else
 113730   sqlite3 *db = (sqlite3 *)ptr;
 113731   int timeout = ((sqlite3 *)ptr)->busyTimeout;
 113732   if( (count+1)*1000 > timeout ){
 113733     return 0;
 113735   sqlite3OsSleep(db->pVfs, 1000000);
 113736   return 1;
 113737 #endif
 113741 ** Invoke the given busy handler.
 113743 ** This routine is called when an operation failed with a lock.
 113744 ** If this routine returns non-zero, the lock is retried.  If it
 113745 ** returns 0, the operation aborts with an SQLITE_BUSY error.
 113747 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
 113748   int rc;
 113749   if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
 113750   rc = p->xFunc(p->pArg, p->nBusy);
 113751   if( rc==0 ){
 113752     p->nBusy = -1;
 113753   }else{
 113754     p->nBusy++;
 113756   return rc; 
 113760 ** This routine sets the busy callback for an Sqlite database to the
 113761 ** given callback function with the given argument.
 113763 SQLITE_API int sqlite3_busy_handler(
 113764   sqlite3 *db,
 113765   int (*xBusy)(void*,int),
 113766   void *pArg
 113768   sqlite3_mutex_enter(db->mutex);
 113769   db->busyHandler.xFunc = xBusy;
 113770   db->busyHandler.pArg = pArg;
 113771   db->busyHandler.nBusy = 0;
 113772   db->busyTimeout = 0;
 113773   sqlite3_mutex_leave(db->mutex);
 113774   return SQLITE_OK;
 113777 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 113779 ** This routine sets the progress callback for an Sqlite database to the
 113780 ** given callback function with the given argument. The progress callback will
 113781 ** be invoked every nOps opcodes.
 113783 SQLITE_API void sqlite3_progress_handler(
 113784   sqlite3 *db, 
 113785   int nOps,
 113786   int (*xProgress)(void*), 
 113787   void *pArg
 113789   sqlite3_mutex_enter(db->mutex);
 113790   if( nOps>0 ){
 113791     db->xProgress = xProgress;
 113792     db->nProgressOps = nOps;
 113793     db->pProgressArg = pArg;
 113794   }else{
 113795     db->xProgress = 0;
 113796     db->nProgressOps = 0;
 113797     db->pProgressArg = 0;
 113799   sqlite3_mutex_leave(db->mutex);
 113801 #endif
 113805 ** This routine installs a default busy handler that waits for the
 113806 ** specified number of milliseconds before returning 0.
 113808 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
 113809   if( ms>0 ){
 113810     sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
 113811     db->busyTimeout = ms;
 113812   }else{
 113813     sqlite3_busy_handler(db, 0, 0);
 113815   return SQLITE_OK;
 113819 ** Cause any pending operation to stop at its earliest opportunity.
 113821 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
 113822   db->u1.isInterrupted = 1;
 113827 ** This function is exactly the same as sqlite3_create_function(), except
 113828 ** that it is designed to be called by internal code. The difference is
 113829 ** that if a malloc() fails in sqlite3_create_function(), an error code
 113830 ** is returned and the mallocFailed flag cleared. 
 113832 SQLITE_PRIVATE int sqlite3CreateFunc(
 113833   sqlite3 *db,
 113834   const char *zFunctionName,
 113835   int nArg,
 113836   int enc,
 113837   void *pUserData,
 113838   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
 113839   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
 113840   void (*xFinal)(sqlite3_context*),
 113841   FuncDestructor *pDestructor
 113843   FuncDef *p;
 113844   int nName;
 113846   assert( sqlite3_mutex_held(db->mutex) );
 113847   if( zFunctionName==0 ||
 113848       (xFunc && (xFinal || xStep)) || 
 113849       (!xFunc && (xFinal && !xStep)) ||
 113850       (!xFunc && (!xFinal && xStep)) ||
 113851       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
 113852       (255<(nName = sqlite3Strlen30( zFunctionName))) ){
 113853     return SQLITE_MISUSE_BKPT;
 113856 #ifndef SQLITE_OMIT_UTF16
 113857   /* If SQLITE_UTF16 is specified as the encoding type, transform this
 113858   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
 113859   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
 113861   ** If SQLITE_ANY is specified, add three versions of the function
 113862   ** to the hash table.
 113864   if( enc==SQLITE_UTF16 ){
 113865     enc = SQLITE_UTF16NATIVE;
 113866   }else if( enc==SQLITE_ANY ){
 113867     int rc;
 113868     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
 113869          pUserData, xFunc, xStep, xFinal, pDestructor);
 113870     if( rc==SQLITE_OK ){
 113871       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
 113872           pUserData, xFunc, xStep, xFinal, pDestructor);
 113874     if( rc!=SQLITE_OK ){
 113875       return rc;
 113877     enc = SQLITE_UTF16BE;
 113879 #else
 113880   enc = SQLITE_UTF8;
 113881 #endif
 113883   /* Check if an existing function is being overridden or deleted. If so,
 113884   ** and there are active VMs, then return SQLITE_BUSY. If a function
 113885   ** is being overridden/deleted but there are no active VMs, allow the
 113886   ** operation to continue but invalidate all precompiled statements.
 113888   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
 113889   if( p && p->iPrefEnc==enc && p->nArg==nArg ){
 113890     if( db->activeVdbeCnt ){
 113891       sqlite3Error(db, SQLITE_BUSY, 
 113892         "unable to delete/modify user-function due to active statements");
 113893       assert( !db->mallocFailed );
 113894       return SQLITE_BUSY;
 113895     }else{
 113896       sqlite3ExpirePreparedStatements(db);
 113900   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
 113901   assert(p || db->mallocFailed);
 113902   if( !p ){
 113903     return SQLITE_NOMEM;
 113906   /* If an older version of the function with a configured destructor is
 113907   ** being replaced invoke the destructor function here. */
 113908   functionDestroy(db, p);
 113910   if( pDestructor ){
 113911     pDestructor->nRef++;
 113913   p->pDestructor = pDestructor;
 113914   p->flags = 0;
 113915   p->xFunc = xFunc;
 113916   p->xStep = xStep;
 113917   p->xFinalize = xFinal;
 113918   p->pUserData = pUserData;
 113919   p->nArg = (u16)nArg;
 113920   return SQLITE_OK;
 113924 ** Create new user functions.
 113926 SQLITE_API int sqlite3_create_function(
 113927   sqlite3 *db,
 113928   const char *zFunc,
 113929   int nArg,
 113930   int enc,
 113931   void *p,
 113932   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
 113933   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
 113934   void (*xFinal)(sqlite3_context*)
 113936   return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
 113937                                     xFinal, 0);
 113940 SQLITE_API int sqlite3_create_function_v2(
 113941   sqlite3 *db,
 113942   const char *zFunc,
 113943   int nArg,
 113944   int enc,
 113945   void *p,
 113946   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
 113947   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
 113948   void (*xFinal)(sqlite3_context*),
 113949   void (*xDestroy)(void *)
 113951   int rc = SQLITE_ERROR;
 113952   FuncDestructor *pArg = 0;
 113953   sqlite3_mutex_enter(db->mutex);
 113954   if( xDestroy ){
 113955     pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
 113956     if( !pArg ){
 113957       xDestroy(p);
 113958       goto out;
 113960     pArg->xDestroy = xDestroy;
 113961     pArg->pUserData = p;
 113963   rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
 113964   if( pArg && pArg->nRef==0 ){
 113965     assert( rc!=SQLITE_OK );
 113966     xDestroy(p);
 113967     sqlite3DbFree(db, pArg);
 113970  out:
 113971   rc = sqlite3ApiExit(db, rc);
 113972   sqlite3_mutex_leave(db->mutex);
 113973   return rc;
 113976 #ifndef SQLITE_OMIT_UTF16
 113977 SQLITE_API int sqlite3_create_function16(
 113978   sqlite3 *db,
 113979   const void *zFunctionName,
 113980   int nArg,
 113981   int eTextRep,
 113982   void *p,
 113983   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 113984   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 113985   void (*xFinal)(sqlite3_context*)
 113987   int rc;
 113988   char *zFunc8;
 113989   sqlite3_mutex_enter(db->mutex);
 113990   assert( !db->mallocFailed );
 113991   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
 113992   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
 113993   sqlite3DbFree(db, zFunc8);
 113994   rc = sqlite3ApiExit(db, rc);
 113995   sqlite3_mutex_leave(db->mutex);
 113996   return rc;
 113998 #endif
 114002 ** Declare that a function has been overloaded by a virtual table.
 114004 ** If the function already exists as a regular global function, then
 114005 ** this routine is a no-op.  If the function does not exist, then create
 114006 ** a new one that always throws a run-time error.  
 114008 ** When virtual tables intend to provide an overloaded function, they
 114009 ** should call this routine to make sure the global function exists.
 114010 ** A global function must exist in order for name resolution to work
 114011 ** properly.
 114013 SQLITE_API int sqlite3_overload_function(
 114014   sqlite3 *db,
 114015   const char *zName,
 114016   int nArg
 114018   int nName = sqlite3Strlen30(zName);
 114019   int rc = SQLITE_OK;
 114020   sqlite3_mutex_enter(db->mutex);
 114021   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
 114022     rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
 114023                            0, sqlite3InvalidFunction, 0, 0, 0);
 114025   rc = sqlite3ApiExit(db, rc);
 114026   sqlite3_mutex_leave(db->mutex);
 114027   return rc;
 114030 #ifndef SQLITE_OMIT_TRACE
 114032 ** Register a trace function.  The pArg from the previously registered trace
 114033 ** is returned.  
 114035 ** A NULL trace function means that no tracing is executes.  A non-NULL
 114036 ** trace is a pointer to a function that is invoked at the start of each
 114037 ** SQL statement.
 114039 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
 114040   void *pOld;
 114041   sqlite3_mutex_enter(db->mutex);
 114042   pOld = db->pTraceArg;
 114043   db->xTrace = xTrace;
 114044   db->pTraceArg = pArg;
 114045   sqlite3_mutex_leave(db->mutex);
 114046   return pOld;
 114049 ** Register a profile function.  The pArg from the previously registered 
 114050 ** profile function is returned.  
 114052 ** A NULL profile function means that no profiling is executes.  A non-NULL
 114053 ** profile is a pointer to a function that is invoked at the conclusion of
 114054 ** each SQL statement that is run.
 114056 SQLITE_API void *sqlite3_profile(
 114057   sqlite3 *db,
 114058   void (*xProfile)(void*,const char*,sqlite_uint64),
 114059   void *pArg
 114061   void *pOld;
 114062   sqlite3_mutex_enter(db->mutex);
 114063   pOld = db->pProfileArg;
 114064   db->xProfile = xProfile;
 114065   db->pProfileArg = pArg;
 114066   sqlite3_mutex_leave(db->mutex);
 114067   return pOld;
 114069 #endif /* SQLITE_OMIT_TRACE */
 114072 ** Register a function to be invoked when a transaction commits.
 114073 ** If the invoked function returns non-zero, then the commit becomes a
 114074 ** rollback.
 114076 SQLITE_API void *sqlite3_commit_hook(
 114077   sqlite3 *db,              /* Attach the hook to this database */
 114078   int (*xCallback)(void*),  /* Function to invoke on each commit */
 114079   void *pArg                /* Argument to the function */
 114081   void *pOld;
 114082   sqlite3_mutex_enter(db->mutex);
 114083   pOld = db->pCommitArg;
 114084   db->xCommitCallback = xCallback;
 114085   db->pCommitArg = pArg;
 114086   sqlite3_mutex_leave(db->mutex);
 114087   return pOld;
 114091 ** Register a callback to be invoked each time a row is updated,
 114092 ** inserted or deleted using this database connection.
 114094 SQLITE_API void *sqlite3_update_hook(
 114095   sqlite3 *db,              /* Attach the hook to this database */
 114096   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
 114097   void *pArg                /* Argument to the function */
 114099   void *pRet;
 114100   sqlite3_mutex_enter(db->mutex);
 114101   pRet = db->pUpdateArg;
 114102   db->xUpdateCallback = xCallback;
 114103   db->pUpdateArg = pArg;
 114104   sqlite3_mutex_leave(db->mutex);
 114105   return pRet;
 114109 ** Register a callback to be invoked each time a transaction is rolled
 114110 ** back by this database connection.
 114112 SQLITE_API void *sqlite3_rollback_hook(
 114113   sqlite3 *db,              /* Attach the hook to this database */
 114114   void (*xCallback)(void*), /* Callback function */
 114115   void *pArg                /* Argument to the function */
 114117   void *pRet;
 114118   sqlite3_mutex_enter(db->mutex);
 114119   pRet = db->pRollbackArg;
 114120   db->xRollbackCallback = xCallback;
 114121   db->pRollbackArg = pArg;
 114122   sqlite3_mutex_leave(db->mutex);
 114123   return pRet;
 114126 #ifndef SQLITE_OMIT_WAL
 114128 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
 114129 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
 114130 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
 114131 ** wal_autocheckpoint()).
 114133 SQLITE_PRIVATE int sqlite3WalDefaultHook(
 114134   void *pClientData,     /* Argument */
 114135   sqlite3 *db,           /* Connection */
 114136   const char *zDb,       /* Database */
 114137   int nFrame             /* Size of WAL */
 114139   if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
 114140     sqlite3BeginBenignMalloc();
 114141     sqlite3_wal_checkpoint(db, zDb);
 114142     sqlite3EndBenignMalloc();
 114144   return SQLITE_OK;
 114146 #endif /* SQLITE_OMIT_WAL */
 114149 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
 114150 ** a database after committing a transaction if there are nFrame or
 114151 ** more frames in the log file. Passing zero or a negative value as the
 114152 ** nFrame parameter disables automatic checkpoints entirely.
 114154 ** The callback registered by this function replaces any existing callback
 114155 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
 114156 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
 114157 ** configured by this function.
 114159 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
 114160 #ifdef SQLITE_OMIT_WAL
 114161   UNUSED_PARAMETER(db);
 114162   UNUSED_PARAMETER(nFrame);
 114163 #else
 114164   if( nFrame>0 ){
 114165     sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
 114166   }else{
 114167     sqlite3_wal_hook(db, 0, 0);
 114169 #endif
 114170   return SQLITE_OK;
 114174 ** Register a callback to be invoked each time a transaction is written
 114175 ** into the write-ahead-log by this database connection.
 114177 SQLITE_API void *sqlite3_wal_hook(
 114178   sqlite3 *db,                    /* Attach the hook to this db handle */
 114179   int(*xCallback)(void *, sqlite3*, const char*, int),
 114180   void *pArg                      /* First argument passed to xCallback() */
 114182 #ifndef SQLITE_OMIT_WAL
 114183   void *pRet;
 114184   sqlite3_mutex_enter(db->mutex);
 114185   pRet = db->pWalArg;
 114186   db->xWalCallback = xCallback;
 114187   db->pWalArg = pArg;
 114188   sqlite3_mutex_leave(db->mutex);
 114189   return pRet;
 114190 #else
 114191   return 0;
 114192 #endif
 114196 ** Checkpoint database zDb.
 114198 SQLITE_API int sqlite3_wal_checkpoint_v2(
 114199   sqlite3 *db,                    /* Database handle */
 114200   const char *zDb,                /* Name of attached database (or NULL) */
 114201   int eMode,                      /* SQLITE_CHECKPOINT_* value */
 114202   int *pnLog,                     /* OUT: Size of WAL log in frames */
 114203   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
 114205 #ifdef SQLITE_OMIT_WAL
 114206   return SQLITE_OK;
 114207 #else
 114208   int rc;                         /* Return code */
 114209   int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
 114211   /* Initialize the output variables to -1 in case an error occurs. */
 114212   if( pnLog ) *pnLog = -1;
 114213   if( pnCkpt ) *pnCkpt = -1;
 114215   assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
 114216   assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
 114217   assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
 114218   if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
 114219     return SQLITE_MISUSE;
 114222   sqlite3_mutex_enter(db->mutex);
 114223   if( zDb && zDb[0] ){
 114224     iDb = sqlite3FindDbName(db, zDb);
 114226   if( iDb<0 ){
 114227     rc = SQLITE_ERROR;
 114228     sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
 114229   }else{
 114230     rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
 114231     sqlite3Error(db, rc, 0);
 114233   rc = sqlite3ApiExit(db, rc);
 114234   sqlite3_mutex_leave(db->mutex);
 114235   return rc;
 114236 #endif
 114241 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
 114242 ** to contains a zero-length string, all attached databases are 
 114243 ** checkpointed.
 114245 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
 114246   return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
 114249 #ifndef SQLITE_OMIT_WAL
 114251 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
 114252 ** not currently open in WAL mode.
 114254 ** If a transaction is open on the database being checkpointed, this 
 114255 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 
 114256 ** an error occurs while running the checkpoint, an SQLite error code is 
 114257 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
 114259 ** The mutex on database handle db should be held by the caller. The mutex
 114260 ** associated with the specific b-tree being checkpointed is taken by
 114261 ** this function while the checkpoint is running.
 114263 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
 114264 ** checkpointed. If an error is encountered it is returned immediately -
 114265 ** no attempt is made to checkpoint any remaining databases.
 114267 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 114269 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
 114270   int rc = SQLITE_OK;             /* Return code */
 114271   int i;                          /* Used to iterate through attached dbs */
 114272   int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
 114274   assert( sqlite3_mutex_held(db->mutex) );
 114275   assert( !pnLog || *pnLog==-1 );
 114276   assert( !pnCkpt || *pnCkpt==-1 );
 114278   for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
 114279     if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
 114280       rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
 114281       pnLog = 0;
 114282       pnCkpt = 0;
 114283       if( rc==SQLITE_BUSY ){
 114284         bBusy = 1;
 114285         rc = SQLITE_OK;
 114290   return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
 114292 #endif /* SQLITE_OMIT_WAL */
 114295 ** This function returns true if main-memory should be used instead of
 114296 ** a temporary file for transient pager files and statement journals.
 114297 ** The value returned depends on the value of db->temp_store (runtime
 114298 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
 114299 ** following table describes the relationship between these two values
 114300 ** and this functions return value.
 114302 **   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
 114303 **   -----------------     --------------     ------------------------------
 114304 **   0                     any                file      (return 0)
 114305 **   1                     1                  file      (return 0)
 114306 **   1                     2                  memory    (return 1)
 114307 **   1                     0                  file      (return 0)
 114308 **   2                     1                  file      (return 0)
 114309 **   2                     2                  memory    (return 1)
 114310 **   2                     0                  memory    (return 1)
 114311 **   3                     any                memory    (return 1)
 114313 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
 114314 #if SQLITE_TEMP_STORE==1
 114315   return ( db->temp_store==2 );
 114316 #endif
 114317 #if SQLITE_TEMP_STORE==2
 114318   return ( db->temp_store!=1 );
 114319 #endif
 114320 #if SQLITE_TEMP_STORE==3
 114321   return 1;
 114322 #endif
 114323 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
 114324   return 0;
 114325 #endif
 114329 ** Return UTF-8 encoded English language explanation of the most recent
 114330 ** error.
 114332 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
 114333   const char *z;
 114334   if( !db ){
 114335     return sqlite3ErrStr(SQLITE_NOMEM);
 114337   if( !sqlite3SafetyCheckSickOrOk(db) ){
 114338     return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
 114340   sqlite3_mutex_enter(db->mutex);
 114341   if( db->mallocFailed ){
 114342     z = sqlite3ErrStr(SQLITE_NOMEM);
 114343   }else{
 114344     z = (char*)sqlite3_value_text(db->pErr);
 114345     assert( !db->mallocFailed );
 114346     if( z==0 ){
 114347       z = sqlite3ErrStr(db->errCode);
 114350   sqlite3_mutex_leave(db->mutex);
 114351   return z;
 114354 #ifndef SQLITE_OMIT_UTF16
 114356 ** Return UTF-16 encoded English language explanation of the most recent
 114357 ** error.
 114359 SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
 114360   static const u16 outOfMem[] = {
 114361     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
 114363   static const u16 misuse[] = {
 114364     'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', 
 114365     'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', 
 114366     'c', 'a', 'l', 'l', 'e', 'd', ' ', 
 114367     'o', 'u', 't', ' ', 
 114368     'o', 'f', ' ', 
 114369     's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
 114372   const void *z;
 114373   if( !db ){
 114374     return (void *)outOfMem;
 114376   if( !sqlite3SafetyCheckSickOrOk(db) ){
 114377     return (void *)misuse;
 114379   sqlite3_mutex_enter(db->mutex);
 114380   if( db->mallocFailed ){
 114381     z = (void *)outOfMem;
 114382   }else{
 114383     z = sqlite3_value_text16(db->pErr);
 114384     if( z==0 ){
 114385       sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
 114386            SQLITE_UTF8, SQLITE_STATIC);
 114387       z = sqlite3_value_text16(db->pErr);
 114389     /* A malloc() may have failed within the call to sqlite3_value_text16()
 114390     ** above. If this is the case, then the db->mallocFailed flag needs to
 114391     ** be cleared before returning. Do this directly, instead of via
 114392     ** sqlite3ApiExit(), to avoid setting the database handle error message.
 114394     db->mallocFailed = 0;
 114396   sqlite3_mutex_leave(db->mutex);
 114397   return z;
 114399 #endif /* SQLITE_OMIT_UTF16 */
 114402 ** Return the most recent error code generated by an SQLite routine. If NULL is
 114403 ** passed to this function, we assume a malloc() failed during sqlite3_open().
 114405 SQLITE_API int sqlite3_errcode(sqlite3 *db){
 114406   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
 114407     return SQLITE_MISUSE_BKPT;
 114409   if( !db || db->mallocFailed ){
 114410     return SQLITE_NOMEM;
 114412   return db->errCode & db->errMask;
 114414 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
 114415   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
 114416     return SQLITE_MISUSE_BKPT;
 114418   if( !db || db->mallocFailed ){
 114419     return SQLITE_NOMEM;
 114421   return db->errCode;
 114425 ** Return a string that describes the kind of error specified in the
 114426 ** argument.  For now, this simply calls the internal sqlite3ErrStr()
 114427 ** function.
 114429 SQLITE_API const char *sqlite3_errstr(int rc){
 114430   return sqlite3ErrStr(rc);
 114434 ** Create a new collating function for database "db".  The name is zName
 114435 ** and the encoding is enc.
 114437 static int createCollation(
 114438   sqlite3* db,
 114439   const char *zName, 
 114440   u8 enc,
 114441   void* pCtx,
 114442   int(*xCompare)(void*,int,const void*,int,const void*),
 114443   void(*xDel)(void*)
 114445   CollSeq *pColl;
 114446   int enc2;
 114447   int nName = sqlite3Strlen30(zName);
 114449   assert( sqlite3_mutex_held(db->mutex) );
 114451   /* If SQLITE_UTF16 is specified as the encoding type, transform this
 114452   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
 114453   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
 114455   enc2 = enc;
 114456   testcase( enc2==SQLITE_UTF16 );
 114457   testcase( enc2==SQLITE_UTF16_ALIGNED );
 114458   if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
 114459     enc2 = SQLITE_UTF16NATIVE;
 114461   if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
 114462     return SQLITE_MISUSE_BKPT;
 114465   /* Check if this call is removing or replacing an existing collation 
 114466   ** sequence. If so, and there are active VMs, return busy. If there
 114467   ** are no active VMs, invalidate any pre-compiled statements.
 114469   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
 114470   if( pColl && pColl->xCmp ){
 114471     if( db->activeVdbeCnt ){
 114472       sqlite3Error(db, SQLITE_BUSY, 
 114473         "unable to delete/modify collation sequence due to active statements");
 114474       return SQLITE_BUSY;
 114476     sqlite3ExpirePreparedStatements(db);
 114478     /* If collation sequence pColl was created directly by a call to
 114479     ** sqlite3_create_collation, and not generated by synthCollSeq(),
 114480     ** then any copies made by synthCollSeq() need to be invalidated.
 114481     ** Also, collation destructor - CollSeq.xDel() - function may need
 114482     ** to be called.
 114484     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
 114485       CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
 114486       int j;
 114487       for(j=0; j<3; j++){
 114488         CollSeq *p = &aColl[j];
 114489         if( p->enc==pColl->enc ){
 114490           if( p->xDel ){
 114491             p->xDel(p->pUser);
 114493           p->xCmp = 0;
 114499   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
 114500   if( pColl==0 ) return SQLITE_NOMEM;
 114501   pColl->xCmp = xCompare;
 114502   pColl->pUser = pCtx;
 114503   pColl->xDel = xDel;
 114504   pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
 114505   sqlite3Error(db, SQLITE_OK, 0);
 114506   return SQLITE_OK;
 114511 ** This array defines hard upper bounds on limit values.  The
 114512 ** initializer must be kept in sync with the SQLITE_LIMIT_*
 114513 ** #defines in sqlite3.h.
 114515 static const int aHardLimit[] = {
 114516   SQLITE_MAX_LENGTH,
 114517   SQLITE_MAX_SQL_LENGTH,
 114518   SQLITE_MAX_COLUMN,
 114519   SQLITE_MAX_EXPR_DEPTH,
 114520   SQLITE_MAX_COMPOUND_SELECT,
 114521   SQLITE_MAX_VDBE_OP,
 114522   SQLITE_MAX_FUNCTION_ARG,
 114523   SQLITE_MAX_ATTACHED,
 114524   SQLITE_MAX_LIKE_PATTERN_LENGTH,
 114525   SQLITE_MAX_VARIABLE_NUMBER,
 114526   SQLITE_MAX_TRIGGER_DEPTH,
 114530 ** Make sure the hard limits are set to reasonable values
 114532 #if SQLITE_MAX_LENGTH<100
 114533 # error SQLITE_MAX_LENGTH must be at least 100
 114534 #endif
 114535 #if SQLITE_MAX_SQL_LENGTH<100
 114536 # error SQLITE_MAX_SQL_LENGTH must be at least 100
 114537 #endif
 114538 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
 114539 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
 114540 #endif
 114541 #if SQLITE_MAX_COMPOUND_SELECT<2
 114542 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
 114543 #endif
 114544 #if SQLITE_MAX_VDBE_OP<40
 114545 # error SQLITE_MAX_VDBE_OP must be at least 40
 114546 #endif
 114547 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
 114548 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
 114549 #endif
 114550 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
 114551 # error SQLITE_MAX_ATTACHED must be between 0 and 62
 114552 #endif
 114553 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
 114554 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
 114555 #endif
 114556 #if SQLITE_MAX_COLUMN>32767
 114557 # error SQLITE_MAX_COLUMN must not exceed 32767
 114558 #endif
 114559 #if SQLITE_MAX_TRIGGER_DEPTH<1
 114560 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
 114561 #endif
 114565 ** Change the value of a limit.  Report the old value.
 114566 ** If an invalid limit index is supplied, report -1.
 114567 ** Make no changes but still report the old value if the
 114568 ** new limit is negative.
 114570 ** A new lower limit does not shrink existing constructs.
 114571 ** It merely prevents new constructs that exceed the limit
 114572 ** from forming.
 114574 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
 114575   int oldLimit;
 114578   /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
 114579   ** there is a hard upper bound set at compile-time by a C preprocessor
 114580   ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
 114581   ** "_MAX_".)
 114583   assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
 114584   assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
 114585   assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
 114586   assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
 114587   assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
 114588   assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
 114589   assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
 114590   assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
 114591   assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
 114592                                                SQLITE_MAX_LIKE_PATTERN_LENGTH );
 114593   assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
 114594   assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
 114595   assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
 114598   if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
 114599     return -1;
 114601   oldLimit = db->aLimit[limitId];
 114602   if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
 114603     if( newLimit>aHardLimit[limitId] ){
 114604       newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
 114606     db->aLimit[limitId] = newLimit;
 114608   return oldLimit;                     /* IMP: R-53341-35419 */
 114612 ** This function is used to parse both URIs and non-URI filenames passed by the
 114613 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
 114614 ** URIs specified as part of ATTACH statements.
 114616 ** The first argument to this function is the name of the VFS to use (or
 114617 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
 114618 ** query parameter. The second argument contains the URI (or non-URI filename)
 114619 ** itself. When this function is called the *pFlags variable should contain
 114620 ** the default flags to open the database handle with. The value stored in
 114621 ** *pFlags may be updated before returning if the URI filename contains 
 114622 ** "cache=xxx" or "mode=xxx" query parameters.
 114624 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
 114625 ** the VFS that should be used to open the database file. *pzFile is set to
 114626 ** point to a buffer containing the name of the file to open. It is the 
 114627 ** responsibility of the caller to eventually call sqlite3_free() to release
 114628 ** this buffer.
 114630 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
 114631 ** may be set to point to a buffer containing an English language error 
 114632 ** message. It is the responsibility of the caller to eventually release
 114633 ** this buffer by calling sqlite3_free().
 114635 SQLITE_PRIVATE int sqlite3ParseUri(
 114636   const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
 114637   const char *zUri,               /* Nul-terminated URI to parse */
 114638   unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
 114639   sqlite3_vfs **ppVfs,            /* OUT: VFS to use */ 
 114640   char **pzFile,                  /* OUT: Filename component of URI */
 114641   char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
 114643   int rc = SQLITE_OK;
 114644   unsigned int flags = *pFlags;
 114645   const char *zVfs = zDefaultVfs;
 114646   char *zFile;
 114647   char c;
 114648   int nUri = sqlite3Strlen30(zUri);
 114650   assert( *pzErrMsg==0 );
 114652   if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) 
 114653    && nUri>=5 && memcmp(zUri, "file:", 5)==0 
 114655     char *zOpt;
 114656     int eState;                   /* Parser state when parsing URI */
 114657     int iIn;                      /* Input character index */
 114658     int iOut = 0;                 /* Output character index */
 114659     int nByte = nUri+2;           /* Bytes of space to allocate */
 114661     /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 
 114662     ** method that there may be extra parameters following the file-name.  */
 114663     flags |= SQLITE_OPEN_URI;
 114665     for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
 114666     zFile = sqlite3_malloc(nByte);
 114667     if( !zFile ) return SQLITE_NOMEM;
 114669     /* Discard the scheme and authority segments of the URI. */
 114670     if( zUri[5]=='/' && zUri[6]=='/' ){
 114671       iIn = 7;
 114672       while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
 114674       if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
 114675         *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", 
 114676             iIn-7, &zUri[7]);
 114677         rc = SQLITE_ERROR;
 114678         goto parse_uri_out;
 114680     }else{
 114681       iIn = 5;
 114684     /* Copy the filename and any query parameters into the zFile buffer. 
 114685     ** Decode %HH escape codes along the way. 
 114687     ** Within this loop, variable eState may be set to 0, 1 or 2, depending
 114688     ** on the parsing context. As follows:
 114690     **   0: Parsing file-name.
 114691     **   1: Parsing name section of a name=value query parameter.
 114692     **   2: Parsing value section of a name=value query parameter.
 114694     eState = 0;
 114695     while( (c = zUri[iIn])!=0 && c!='#' ){
 114696       iIn++;
 114697       if( c=='%' 
 114698        && sqlite3Isxdigit(zUri[iIn]) 
 114699        && sqlite3Isxdigit(zUri[iIn+1]) 
 114701         int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
 114702         octet += sqlite3HexToInt(zUri[iIn++]);
 114704         assert( octet>=0 && octet<256 );
 114705         if( octet==0 ){
 114706           /* This branch is taken when "%00" appears within the URI. In this
 114707           ** case we ignore all text in the remainder of the path, name or
 114708           ** value currently being parsed. So ignore the current character
 114709           ** and skip to the next "?", "=" or "&", as appropriate. */
 114710           while( (c = zUri[iIn])!=0 && c!='#' 
 114711               && (eState!=0 || c!='?')
 114712               && (eState!=1 || (c!='=' && c!='&'))
 114713               && (eState!=2 || c!='&')
 114715             iIn++;
 114717           continue;
 114719         c = octet;
 114720       }else if( eState==1 && (c=='&' || c=='=') ){
 114721         if( zFile[iOut-1]==0 ){
 114722           /* An empty option name. Ignore this option altogether. */
 114723           while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
 114724           continue;
 114726         if( c=='&' ){
 114727           zFile[iOut++] = '\0';
 114728         }else{
 114729           eState = 2;
 114731         c = 0;
 114732       }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
 114733         c = 0;
 114734         eState = 1;
 114736       zFile[iOut++] = c;
 114738     if( eState==1 ) zFile[iOut++] = '\0';
 114739     zFile[iOut++] = '\0';
 114740     zFile[iOut++] = '\0';
 114742     /* Check if there were any options specified that should be interpreted 
 114743     ** here. Options that are interpreted here include "vfs" and those that
 114744     ** correspond to flags that may be passed to the sqlite3_open_v2()
 114745     ** method. */
 114746     zOpt = &zFile[sqlite3Strlen30(zFile)+1];
 114747     while( zOpt[0] ){
 114748       int nOpt = sqlite3Strlen30(zOpt);
 114749       char *zVal = &zOpt[nOpt+1];
 114750       int nVal = sqlite3Strlen30(zVal);
 114752       if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
 114753         zVfs = zVal;
 114754       }else{
 114755         struct OpenMode {
 114756           const char *z;
 114757           int mode;
 114758         } *aMode = 0;
 114759         char *zModeType = 0;
 114760         int mask = 0;
 114761         int limit = 0;
 114763         if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
 114764           static struct OpenMode aCacheMode[] = {
 114765             { "shared",  SQLITE_OPEN_SHAREDCACHE },
 114766             { "private", SQLITE_OPEN_PRIVATECACHE },
 114767             { 0, 0 }
 114770           mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
 114771           aMode = aCacheMode;
 114772           limit = mask;
 114773           zModeType = "cache";
 114775         if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
 114776           static struct OpenMode aOpenMode[] = {
 114777             { "ro",  SQLITE_OPEN_READONLY },
 114778             { "rw",  SQLITE_OPEN_READWRITE }, 
 114779             { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
 114780             { "memory", SQLITE_OPEN_MEMORY },
 114781             { 0, 0 }
 114784           mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
 114785                    | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
 114786           aMode = aOpenMode;
 114787           limit = mask & flags;
 114788           zModeType = "access";
 114791         if( aMode ){
 114792           int i;
 114793           int mode = 0;
 114794           for(i=0; aMode[i].z; i++){
 114795             const char *z = aMode[i].z;
 114796             if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
 114797               mode = aMode[i].mode;
 114798               break;
 114801           if( mode==0 ){
 114802             *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
 114803             rc = SQLITE_ERROR;
 114804             goto parse_uri_out;
 114806           if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
 114807             *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
 114808                                         zModeType, zVal);
 114809             rc = SQLITE_PERM;
 114810             goto parse_uri_out;
 114812           flags = (flags & ~mask) | mode;
 114816       zOpt = &zVal[nVal+1];
 114819   }else{
 114820     zFile = sqlite3_malloc(nUri+2);
 114821     if( !zFile ) return SQLITE_NOMEM;
 114822     memcpy(zFile, zUri, nUri);
 114823     zFile[nUri] = '\0';
 114824     zFile[nUri+1] = '\0';
 114825     flags &= ~SQLITE_OPEN_URI;
 114828   *ppVfs = sqlite3_vfs_find(zVfs);
 114829   if( *ppVfs==0 ){
 114830     *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
 114831     rc = SQLITE_ERROR;
 114833  parse_uri_out:
 114834   if( rc!=SQLITE_OK ){
 114835     sqlite3_free(zFile);
 114836     zFile = 0;
 114838   *pFlags = flags;
 114839   *pzFile = zFile;
 114840   return rc;
 114845 ** This routine does the work of opening a database on behalf of
 114846 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
 114847 ** is UTF-8 encoded.
 114849 static int openDatabase(
 114850   const char *zFilename, /* Database filename UTF-8 encoded */
 114851   sqlite3 **ppDb,        /* OUT: Returned database handle */
 114852   unsigned int flags,    /* Operational flags */
 114853   const char *zVfs       /* Name of the VFS to use */
 114855   sqlite3 *db;                    /* Store allocated handle here */
 114856   int rc;                         /* Return code */
 114857   int isThreadsafe;               /* True for threadsafe connections */
 114858   char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
 114859   char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
 114861   *ppDb = 0;
 114862 #ifndef SQLITE_OMIT_AUTOINIT
 114863   rc = sqlite3_initialize();
 114864   if( rc ) return rc;
 114865 #endif
 114867   /* Only allow sensible combinations of bits in the flags argument.  
 114868   ** Throw an error if any non-sense combination is used.  If we
 114869   ** do not block illegal combinations here, it could trigger
 114870   ** assert() statements in deeper layers.  Sensible combinations
 114871   ** are:
 114873   **  1:  SQLITE_OPEN_READONLY
 114874   **  2:  SQLITE_OPEN_READWRITE
 114875   **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
 114877   assert( SQLITE_OPEN_READONLY  == 0x01 );
 114878   assert( SQLITE_OPEN_READWRITE == 0x02 );
 114879   assert( SQLITE_OPEN_CREATE    == 0x04 );
 114880   testcase( (1<<(flags&7))==0x02 ); /* READONLY */
 114881   testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
 114882   testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
 114883   if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
 114885   if( sqlite3GlobalConfig.bCoreMutex==0 ){
 114886     isThreadsafe = 0;
 114887   }else if( flags & SQLITE_OPEN_NOMUTEX ){
 114888     isThreadsafe = 0;
 114889   }else if( flags & SQLITE_OPEN_FULLMUTEX ){
 114890     isThreadsafe = 1;
 114891   }else{
 114892     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
 114894   if( flags & SQLITE_OPEN_PRIVATECACHE ){
 114895     flags &= ~SQLITE_OPEN_SHAREDCACHE;
 114896   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
 114897     flags |= SQLITE_OPEN_SHAREDCACHE;
 114900   /* Remove harmful bits from the flags parameter
 114902   ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
 114903   ** dealt with in the previous code block.  Besides these, the only
 114904   ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
 114905   ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
 114906   ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
 114907   ** off all other flags.
 114909   flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
 114910                SQLITE_OPEN_EXCLUSIVE |
 114911                SQLITE_OPEN_MAIN_DB |
 114912                SQLITE_OPEN_TEMP_DB | 
 114913                SQLITE_OPEN_TRANSIENT_DB | 
 114914                SQLITE_OPEN_MAIN_JOURNAL | 
 114915                SQLITE_OPEN_TEMP_JOURNAL | 
 114916                SQLITE_OPEN_SUBJOURNAL | 
 114917                SQLITE_OPEN_MASTER_JOURNAL |
 114918                SQLITE_OPEN_NOMUTEX |
 114919                SQLITE_OPEN_FULLMUTEX |
 114920                SQLITE_OPEN_WAL
 114923   /* Allocate the sqlite data structure */
 114924   db = sqlite3MallocZero( sizeof(sqlite3) );
 114925   if( db==0 ) goto opendb_out;
 114926   if( isThreadsafe ){
 114927     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
 114928     if( db->mutex==0 ){
 114929       sqlite3_free(db);
 114930       db = 0;
 114931       goto opendb_out;
 114934   sqlite3_mutex_enter(db->mutex);
 114935   db->errMask = 0xff;
 114936   db->nDb = 2;
 114937   db->magic = SQLITE_MAGIC_BUSY;
 114938   db->aDb = db->aDbStatic;
 114940   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
 114941   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
 114942   db->autoCommit = 1;
 114943   db->nextAutovac = -1;
 114944   db->nextPagesize = 0;
 114945   db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
 114946 #if SQLITE_DEFAULT_FILE_FORMAT<4
 114947                  | SQLITE_LegacyFileFmt
 114948 #endif
 114949 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
 114950                  | SQLITE_LoadExtension
 114951 #endif
 114952 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
 114953                  | SQLITE_RecTriggers
 114954 #endif
 114955 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
 114956                  | SQLITE_ForeignKeys
 114957 #endif
 114959   sqlite3HashInit(&db->aCollSeq);
 114960 #ifndef SQLITE_OMIT_VIRTUALTABLE
 114961   sqlite3HashInit(&db->aModule);
 114962 #endif
 114964   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
 114965   ** and UTF-16, so add a version for each to avoid any unnecessary
 114966   ** conversions. The only error that can occur here is a malloc() failure.
 114968   createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
 114969   createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
 114970   createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
 114971   createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
 114972   if( db->mallocFailed ){
 114973     goto opendb_out;
 114975   db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
 114976   assert( db->pDfltColl!=0 );
 114978   /* Also add a UTF-8 case-insensitive collation sequence. */
 114979   createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
 114981   /* Parse the filename/URI argument. */
 114982   db->openFlags = flags;
 114983   rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
 114984   if( rc!=SQLITE_OK ){
 114985     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
 114986     sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
 114987     sqlite3_free(zErrMsg);
 114988     goto opendb_out;
 114991   /* Open the backend database driver */
 114992   rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
 114993                         flags | SQLITE_OPEN_MAIN_DB);
 114994   if( rc!=SQLITE_OK ){
 114995     if( rc==SQLITE_IOERR_NOMEM ){
 114996       rc = SQLITE_NOMEM;
 114998     sqlite3Error(db, rc, 0);
 114999     goto opendb_out;
 115001   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
 115002   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
 115005   /* The default safety_level for the main database is 'full'; for the temp
 115006   ** database it is 'NONE'. This matches the pager layer defaults.  
 115008   db->aDb[0].zName = "main";
 115009   db->aDb[0].safety_level = 3;
 115010   db->aDb[1].zName = "temp";
 115011   db->aDb[1].safety_level = 1;
 115013   db->magic = SQLITE_MAGIC_OPEN;
 115014   if( db->mallocFailed ){
 115015     goto opendb_out;
 115018   /* Register all built-in functions, but do not attempt to read the
 115019   ** database schema yet. This is delayed until the first time the database
 115020   ** is accessed.
 115022   sqlite3Error(db, SQLITE_OK, 0);
 115023   sqlite3RegisterBuiltinFunctions(db);
 115025   /* Load automatic extensions - extensions that have been registered
 115026   ** using the sqlite3_automatic_extension() API.
 115028   rc = sqlite3_errcode(db);
 115029   if( rc==SQLITE_OK ){
 115030     sqlite3AutoLoadExtensions(db);
 115031     rc = sqlite3_errcode(db);
 115032     if( rc!=SQLITE_OK ){
 115033       goto opendb_out;
 115037 #ifdef SQLITE_ENABLE_FTS1
 115038   if( !db->mallocFailed ){
 115039     extern int sqlite3Fts1Init(sqlite3*);
 115040     rc = sqlite3Fts1Init(db);
 115042 #endif
 115044 #ifdef SQLITE_ENABLE_FTS2
 115045   if( !db->mallocFailed && rc==SQLITE_OK ){
 115046     extern int sqlite3Fts2Init(sqlite3*);
 115047     rc = sqlite3Fts2Init(db);
 115049 #endif
 115051 #ifdef SQLITE_ENABLE_FTS3
 115052   if( !db->mallocFailed && rc==SQLITE_OK ){
 115053     rc = sqlite3Fts3Init(db);
 115055 #endif
 115057 #ifdef SQLITE_ENABLE_ICU
 115058   if( !db->mallocFailed && rc==SQLITE_OK ){
 115059     rc = sqlite3IcuInit(db);
 115061 #endif
 115063 #ifdef SQLITE_ENABLE_RTREE
 115064   if( !db->mallocFailed && rc==SQLITE_OK){
 115065     rc = sqlite3RtreeInit(db);
 115067 #endif
 115069   sqlite3Error(db, rc, 0);
 115071   /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
 115072   ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
 115073   ** mode.  Doing nothing at all also makes NORMAL the default.
 115075 #ifdef SQLITE_DEFAULT_LOCKING_MODE
 115076   db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
 115077   sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
 115078                           SQLITE_DEFAULT_LOCKING_MODE);
 115079 #endif
 115081   /* Enable the lookaside-malloc subsystem */
 115082   setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
 115083                         sqlite3GlobalConfig.nLookaside);
 115085   sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
 115087 opendb_out:
 115088   sqlite3_free(zOpen);
 115089   if( db ){
 115090     assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
 115091     sqlite3_mutex_leave(db->mutex);
 115093   rc = sqlite3_errcode(db);
 115094   assert( db!=0 || rc==SQLITE_NOMEM );
 115095   if( rc==SQLITE_NOMEM ){
 115096     sqlite3_close(db);
 115097     db = 0;
 115098   }else if( rc!=SQLITE_OK ){
 115099     db->magic = SQLITE_MAGIC_SICK;
 115101   *ppDb = db;
 115102 #ifdef SQLITE_ENABLE_SQLLOG
 115103   if( sqlite3GlobalConfig.xSqllog ){
 115104     /* Opening a db handle. Fourth parameter is passed 0. */
 115105     void *pArg = sqlite3GlobalConfig.pSqllogArg;
 115106     sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
 115108 #endif
 115109   return sqlite3ApiExit(0, rc);
 115113 ** Open a new database handle.
 115115 SQLITE_API int sqlite3_open(
 115116   const char *zFilename, 
 115117   sqlite3 **ppDb 
 115119   return openDatabase(zFilename, ppDb,
 115120                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
 115122 SQLITE_API int sqlite3_open_v2(
 115123   const char *filename,   /* Database filename (UTF-8) */
 115124   sqlite3 **ppDb,         /* OUT: SQLite db handle */
 115125   int flags,              /* Flags */
 115126   const char *zVfs        /* Name of VFS module to use */
 115128   return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
 115131 #ifndef SQLITE_OMIT_UTF16
 115133 ** Open a new database handle.
 115135 SQLITE_API int sqlite3_open16(
 115136   const void *zFilename, 
 115137   sqlite3 **ppDb
 115139   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
 115140   sqlite3_value *pVal;
 115141   int rc;
 115143   assert( zFilename );
 115144   assert( ppDb );
 115145   *ppDb = 0;
 115146 #ifndef SQLITE_OMIT_AUTOINIT
 115147   rc = sqlite3_initialize();
 115148   if( rc ) return rc;
 115149 #endif
 115150   pVal = sqlite3ValueNew(0);
 115151   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
 115152   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
 115153   if( zFilename8 ){
 115154     rc = openDatabase(zFilename8, ppDb,
 115155                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
 115156     assert( *ppDb || rc==SQLITE_NOMEM );
 115157     if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
 115158       ENC(*ppDb) = SQLITE_UTF16NATIVE;
 115160   }else{
 115161     rc = SQLITE_NOMEM;
 115163   sqlite3ValueFree(pVal);
 115165   return sqlite3ApiExit(0, rc);
 115167 #endif /* SQLITE_OMIT_UTF16 */
 115170 ** Register a new collation sequence with the database handle db.
 115172 SQLITE_API int sqlite3_create_collation(
 115173   sqlite3* db, 
 115174   const char *zName, 
 115175   int enc, 
 115176   void* pCtx,
 115177   int(*xCompare)(void*,int,const void*,int,const void*)
 115179   int rc;
 115180   sqlite3_mutex_enter(db->mutex);
 115181   assert( !db->mallocFailed );
 115182   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
 115183   rc = sqlite3ApiExit(db, rc);
 115184   sqlite3_mutex_leave(db->mutex);
 115185   return rc;
 115189 ** Register a new collation sequence with the database handle db.
 115191 SQLITE_API int sqlite3_create_collation_v2(
 115192   sqlite3* db, 
 115193   const char *zName, 
 115194   int enc, 
 115195   void* pCtx,
 115196   int(*xCompare)(void*,int,const void*,int,const void*),
 115197   void(*xDel)(void*)
 115199   int rc;
 115200   sqlite3_mutex_enter(db->mutex);
 115201   assert( !db->mallocFailed );
 115202   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
 115203   rc = sqlite3ApiExit(db, rc);
 115204   sqlite3_mutex_leave(db->mutex);
 115205   return rc;
 115208 #ifndef SQLITE_OMIT_UTF16
 115210 ** Register a new collation sequence with the database handle db.
 115212 SQLITE_API int sqlite3_create_collation16(
 115213   sqlite3* db, 
 115214   const void *zName,
 115215   int enc, 
 115216   void* pCtx,
 115217   int(*xCompare)(void*,int,const void*,int,const void*)
 115219   int rc = SQLITE_OK;
 115220   char *zName8;
 115221   sqlite3_mutex_enter(db->mutex);
 115222   assert( !db->mallocFailed );
 115223   zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
 115224   if( zName8 ){
 115225     rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
 115226     sqlite3DbFree(db, zName8);
 115228   rc = sqlite3ApiExit(db, rc);
 115229   sqlite3_mutex_leave(db->mutex);
 115230   return rc;
 115232 #endif /* SQLITE_OMIT_UTF16 */
 115235 ** Register a collation sequence factory callback with the database handle
 115236 ** db. Replace any previously installed collation sequence factory.
 115238 SQLITE_API int sqlite3_collation_needed(
 115239   sqlite3 *db, 
 115240   void *pCollNeededArg, 
 115241   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
 115243   sqlite3_mutex_enter(db->mutex);
 115244   db->xCollNeeded = xCollNeeded;
 115245   db->xCollNeeded16 = 0;
 115246   db->pCollNeededArg = pCollNeededArg;
 115247   sqlite3_mutex_leave(db->mutex);
 115248   return SQLITE_OK;
 115251 #ifndef SQLITE_OMIT_UTF16
 115253 ** Register a collation sequence factory callback with the database handle
 115254 ** db. Replace any previously installed collation sequence factory.
 115256 SQLITE_API int sqlite3_collation_needed16(
 115257   sqlite3 *db, 
 115258   void *pCollNeededArg, 
 115259   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
 115261   sqlite3_mutex_enter(db->mutex);
 115262   db->xCollNeeded = 0;
 115263   db->xCollNeeded16 = xCollNeeded16;
 115264   db->pCollNeededArg = pCollNeededArg;
 115265   sqlite3_mutex_leave(db->mutex);
 115266   return SQLITE_OK;
 115268 #endif /* SQLITE_OMIT_UTF16 */
 115270 #ifndef SQLITE_OMIT_DEPRECATED
 115272 ** This function is now an anachronism. It used to be used to recover from a
 115273 ** malloc() failure, but SQLite now does this automatically.
 115275 SQLITE_API int sqlite3_global_recover(void){
 115276   return SQLITE_OK;
 115278 #endif
 115281 ** Test to see whether or not the database connection is in autocommit
 115282 ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
 115283 ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
 115284 ** by the next COMMIT or ROLLBACK.
 115286 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
 115288 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
 115289   return db->autoCommit;
 115293 ** The following routines are subtitutes for constants SQLITE_CORRUPT,
 115294 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
 115295 ** constants.  They server two purposes:
 115297 **   1.  Serve as a convenient place to set a breakpoint in a debugger
 115298 **       to detect when version error conditions occurs.
 115300 **   2.  Invoke sqlite3_log() to provide the source code location where
 115301 **       a low-level error is first detected.
 115303 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
 115304   testcase( sqlite3GlobalConfig.xLog!=0 );
 115305   sqlite3_log(SQLITE_CORRUPT,
 115306               "database corruption at line %d of [%.10s]",
 115307               lineno, 20+sqlite3_sourceid());
 115308   return SQLITE_CORRUPT;
 115310 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
 115311   testcase( sqlite3GlobalConfig.xLog!=0 );
 115312   sqlite3_log(SQLITE_MISUSE, 
 115313               "misuse at line %d of [%.10s]",
 115314               lineno, 20+sqlite3_sourceid());
 115315   return SQLITE_MISUSE;
 115317 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
 115318   testcase( sqlite3GlobalConfig.xLog!=0 );
 115319   sqlite3_log(SQLITE_CANTOPEN, 
 115320               "cannot open file at line %d of [%.10s]",
 115321               lineno, 20+sqlite3_sourceid());
 115322   return SQLITE_CANTOPEN;
 115326 #ifndef SQLITE_OMIT_DEPRECATED
 115328 ** This is a convenience routine that makes sure that all thread-specific
 115329 ** data for this thread has been deallocated.
 115331 ** SQLite no longer uses thread-specific data so this routine is now a
 115332 ** no-op.  It is retained for historical compatibility.
 115334 SQLITE_API void sqlite3_thread_cleanup(void){
 115336 #endif
 115339 ** Return meta information about a specific column of a database table.
 115340 ** See comment in sqlite3.h (sqlite.h.in) for details.
 115342 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 115343 SQLITE_API int sqlite3_table_column_metadata(
 115344   sqlite3 *db,                /* Connection handle */
 115345   const char *zDbName,        /* Database name or NULL */
 115346   const char *zTableName,     /* Table name */
 115347   const char *zColumnName,    /* Column name */
 115348   char const **pzDataType,    /* OUTPUT: Declared data type */
 115349   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
 115350   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
 115351   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
 115352   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
 115354   int rc;
 115355   char *zErrMsg = 0;
 115356   Table *pTab = 0;
 115357   Column *pCol = 0;
 115358   int iCol;
 115360   char const *zDataType = 0;
 115361   char const *zCollSeq = 0;
 115362   int notnull = 0;
 115363   int primarykey = 0;
 115364   int autoinc = 0;
 115366   /* Ensure the database schema has been loaded */
 115367   sqlite3_mutex_enter(db->mutex);
 115368   sqlite3BtreeEnterAll(db);
 115369   rc = sqlite3Init(db, &zErrMsg);
 115370   if( SQLITE_OK!=rc ){
 115371     goto error_out;
 115374   /* Locate the table in question */
 115375   pTab = sqlite3FindTable(db, zTableName, zDbName);
 115376   if( !pTab || pTab->pSelect ){
 115377     pTab = 0;
 115378     goto error_out;
 115381   /* Find the column for which info is requested */
 115382   if( sqlite3IsRowid(zColumnName) ){
 115383     iCol = pTab->iPKey;
 115384     if( iCol>=0 ){
 115385       pCol = &pTab->aCol[iCol];
 115387   }else{
 115388     for(iCol=0; iCol<pTab->nCol; iCol++){
 115389       pCol = &pTab->aCol[iCol];
 115390       if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
 115391         break;
 115394     if( iCol==pTab->nCol ){
 115395       pTab = 0;
 115396       goto error_out;
 115400   /* The following block stores the meta information that will be returned
 115401   ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
 115402   ** and autoinc. At this point there are two possibilities:
 115404   **     1. The specified column name was rowid", "oid" or "_rowid_" 
 115405   **        and there is no explicitly declared IPK column. 
 115407   **     2. The table is not a view and the column name identified an 
 115408   **        explicitly declared column. Copy meta information from *pCol.
 115410   if( pCol ){
 115411     zDataType = pCol->zType;
 115412     zCollSeq = pCol->zColl;
 115413     notnull = pCol->notNull!=0;
 115414     primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
 115415     autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
 115416   }else{
 115417     zDataType = "INTEGER";
 115418     primarykey = 1;
 115420   if( !zCollSeq ){
 115421     zCollSeq = "BINARY";
 115424 error_out:
 115425   sqlite3BtreeLeaveAll(db);
 115427   /* Whether the function call succeeded or failed, set the output parameters
 115428   ** to whatever their local counterparts contain. If an error did occur,
 115429   ** this has the effect of zeroing all output parameters.
 115431   if( pzDataType ) *pzDataType = zDataType;
 115432   if( pzCollSeq ) *pzCollSeq = zCollSeq;
 115433   if( pNotNull ) *pNotNull = notnull;
 115434   if( pPrimaryKey ) *pPrimaryKey = primarykey;
 115435   if( pAutoinc ) *pAutoinc = autoinc;
 115437   if( SQLITE_OK==rc && !pTab ){
 115438     sqlite3DbFree(db, zErrMsg);
 115439     zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
 115440         zColumnName);
 115441     rc = SQLITE_ERROR;
 115443   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
 115444   sqlite3DbFree(db, zErrMsg);
 115445   rc = sqlite3ApiExit(db, rc);
 115446   sqlite3_mutex_leave(db->mutex);
 115447   return rc;
 115449 #endif
 115452 ** Sleep for a little while.  Return the amount of time slept.
 115454 SQLITE_API int sqlite3_sleep(int ms){
 115455   sqlite3_vfs *pVfs;
 115456   int rc;
 115457   pVfs = sqlite3_vfs_find(0);
 115458   if( pVfs==0 ) return 0;
 115460   /* This function works in milliseconds, but the underlying OsSleep() 
 115461   ** API uses microseconds. Hence the 1000's.
 115463   rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
 115464   return rc;
 115468 ** Enable or disable the extended result codes.
 115470 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
 115471   sqlite3_mutex_enter(db->mutex);
 115472   db->errMask = onoff ? 0xffffffff : 0xff;
 115473   sqlite3_mutex_leave(db->mutex);
 115474   return SQLITE_OK;
 115478 ** Invoke the xFileControl method on a particular database.
 115480 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
 115481   int rc = SQLITE_ERROR;
 115482   Btree *pBtree;
 115484   sqlite3_mutex_enter(db->mutex);
 115485   pBtree = sqlite3DbNameToBtree(db, zDbName);
 115486   if( pBtree ){
 115487     Pager *pPager;
 115488     sqlite3_file *fd;
 115489     sqlite3BtreeEnter(pBtree);
 115490     pPager = sqlite3BtreePager(pBtree);
 115491     assert( pPager!=0 );
 115492     fd = sqlite3PagerFile(pPager);
 115493     assert( fd!=0 );
 115494     if( op==SQLITE_FCNTL_FILE_POINTER ){
 115495       *(sqlite3_file**)pArg = fd;
 115496       rc = SQLITE_OK;
 115497     }else if( fd->pMethods ){
 115498       rc = sqlite3OsFileControl(fd, op, pArg);
 115499     }else{
 115500       rc = SQLITE_NOTFOUND;
 115502     sqlite3BtreeLeave(pBtree);
 115504   sqlite3_mutex_leave(db->mutex);
 115505   return rc;   
 115509 ** Interface to the testing logic.
 115511 SQLITE_API int sqlite3_test_control(int op, ...){
 115512   int rc = 0;
 115513 #ifndef SQLITE_OMIT_BUILTIN_TEST
 115514   va_list ap;
 115515   va_start(ap, op);
 115516   switch( op ){
 115519     ** Save the current state of the PRNG.
 115521     case SQLITE_TESTCTRL_PRNG_SAVE: {
 115522       sqlite3PrngSaveState();
 115523       break;
 115527     ** Restore the state of the PRNG to the last state saved using
 115528     ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
 115529     ** this verb acts like PRNG_RESET.
 115531     case SQLITE_TESTCTRL_PRNG_RESTORE: {
 115532       sqlite3PrngRestoreState();
 115533       break;
 115537     ** Reset the PRNG back to its uninitialized state.  The next call
 115538     ** to sqlite3_randomness() will reseed the PRNG using a single call
 115539     ** to the xRandomness method of the default VFS.
 115541     case SQLITE_TESTCTRL_PRNG_RESET: {
 115542       sqlite3PrngResetState();
 115543       break;
 115547     **  sqlite3_test_control(BITVEC_TEST, size, program)
 115549     ** Run a test against a Bitvec object of size.  The program argument
 115550     ** is an array of integers that defines the test.  Return -1 on a
 115551     ** memory allocation error, 0 on success, or non-zero for an error.
 115552     ** See the sqlite3BitvecBuiltinTest() for additional information.
 115554     case SQLITE_TESTCTRL_BITVEC_TEST: {
 115555       int sz = va_arg(ap, int);
 115556       int *aProg = va_arg(ap, int*);
 115557       rc = sqlite3BitvecBuiltinTest(sz, aProg);
 115558       break;
 115562     **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
 115564     ** Register hooks to call to indicate which malloc() failures 
 115565     ** are benign.
 115567     case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
 115568       typedef void (*void_function)(void);
 115569       void_function xBenignBegin;
 115570       void_function xBenignEnd;
 115571       xBenignBegin = va_arg(ap, void_function);
 115572       xBenignEnd = va_arg(ap, void_function);
 115573       sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
 115574       break;
 115578     **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
 115580     ** Set the PENDING byte to the value in the argument, if X>0.
 115581     ** Make no changes if X==0.  Return the value of the pending byte
 115582     ** as it existing before this routine was called.
 115584     ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
 115585     ** an incompatible database file format.  Changing the PENDING byte
 115586     ** while any database connection is open results in undefined and
 115587     ** dileterious behavior.
 115589     case SQLITE_TESTCTRL_PENDING_BYTE: {
 115590       rc = PENDING_BYTE;
 115591 #ifndef SQLITE_OMIT_WSD
 115593         unsigned int newVal = va_arg(ap, unsigned int);
 115594         if( newVal ) sqlite3PendingByte = newVal;
 115596 #endif
 115597       break;
 115601     **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
 115603     ** This action provides a run-time test to see whether or not
 115604     ** assert() was enabled at compile-time.  If X is true and assert()
 115605     ** is enabled, then the return value is true.  If X is true and
 115606     ** assert() is disabled, then the return value is zero.  If X is
 115607     ** false and assert() is enabled, then the assertion fires and the
 115608     ** process aborts.  If X is false and assert() is disabled, then the
 115609     ** return value is zero.
 115611     case SQLITE_TESTCTRL_ASSERT: {
 115612       volatile int x = 0;
 115613       assert( (x = va_arg(ap,int))!=0 );
 115614       rc = x;
 115615       break;
 115620     **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
 115622     ** This action provides a run-time test to see how the ALWAYS and
 115623     ** NEVER macros were defined at compile-time.
 115625     ** The return value is ALWAYS(X).  
 115627     ** The recommended test is X==2.  If the return value is 2, that means
 115628     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
 115629     ** default setting.  If the return value is 1, then ALWAYS() is either
 115630     ** hard-coded to true or else it asserts if its argument is false.
 115631     ** The first behavior (hard-coded to true) is the case if
 115632     ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
 115633     ** behavior (assert if the argument to ALWAYS() is false) is the case if
 115634     ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
 115636     ** The run-time test procedure might look something like this:
 115638     **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
 115639     **      // ALWAYS() and NEVER() are no-op pass-through macros
 115640     **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
 115641     **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
 115642     **    }else{
 115643     **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
 115644     **    }
 115646     case SQLITE_TESTCTRL_ALWAYS: {
 115647       int x = va_arg(ap,int);
 115648       rc = ALWAYS(x);
 115649       break;
 115652     /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
 115654     ** Set the nReserve size to N for the main database on the database
 115655     ** connection db.
 115657     case SQLITE_TESTCTRL_RESERVE: {
 115658       sqlite3 *db = va_arg(ap, sqlite3*);
 115659       int x = va_arg(ap,int);
 115660       sqlite3_mutex_enter(db->mutex);
 115661       sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
 115662       sqlite3_mutex_leave(db->mutex);
 115663       break;
 115666     /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
 115668     ** Enable or disable various optimizations for testing purposes.  The 
 115669     ** argument N is a bitmask of optimizations to be disabled.  For normal
 115670     ** operation N should be 0.  The idea is that a test program (like the
 115671     ** SQL Logic Test or SLT test module) can run the same SQL multiple times
 115672     ** with various optimizations disabled to verify that the same answer
 115673     ** is obtained in every case.
 115675     case SQLITE_TESTCTRL_OPTIMIZATIONS: {
 115676       sqlite3 *db = va_arg(ap, sqlite3*);
 115677       db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
 115678       break;
 115681 #ifdef SQLITE_N_KEYWORD
 115682     /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
 115684     ** If zWord is a keyword recognized by the parser, then return the
 115685     ** number of keywords.  Or if zWord is not a keyword, return 0.
 115687     ** This test feature is only available in the amalgamation since
 115688     ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
 115689     ** is built using separate source files.
 115691     case SQLITE_TESTCTRL_ISKEYWORD: {
 115692       const char *zWord = va_arg(ap, const char*);
 115693       int n = sqlite3Strlen30(zWord);
 115694       rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
 115695       break;
 115697 #endif 
 115699     /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
 115701     ** Pass pFree into sqlite3ScratchFree(). 
 115702     ** If sz>0 then allocate a scratch buffer into pNew.  
 115704     case SQLITE_TESTCTRL_SCRATCHMALLOC: {
 115705       void *pFree, **ppNew;
 115706       int sz;
 115707       sz = va_arg(ap, int);
 115708       ppNew = va_arg(ap, void**);
 115709       pFree = va_arg(ap, void*);
 115710       if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
 115711       sqlite3ScratchFree(pFree);
 115712       break;
 115715     /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
 115717     ** If parameter onoff is non-zero, configure the wrappers so that all
 115718     ** subsequent calls to localtime() and variants fail. If onoff is zero,
 115719     ** undo this setting.
 115721     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
 115722       sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
 115723       break;
 115726 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 115727     /*   sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
 115728     **                        sqlite3_stmt*,const char**);
 115730     ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
 115731     ** a string that describes the optimized parse tree.  This test-control
 115732     ** returns a pointer to that string.
 115734     case SQLITE_TESTCTRL_EXPLAIN_STMT: {
 115735       sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
 115736       const char **pzRet = va_arg(ap, const char**);
 115737       *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
 115738       break;
 115740 #endif
 115743   va_end(ap);
 115744 #endif /* SQLITE_OMIT_BUILTIN_TEST */
 115745   return rc;
 115749 ** This is a utility routine, useful to VFS implementations, that checks
 115750 ** to see if a database file was a URI that contained a specific query 
 115751 ** parameter, and if so obtains the value of the query parameter.
 115753 ** The zFilename argument is the filename pointer passed into the xOpen()
 115754 ** method of a VFS implementation.  The zParam argument is the name of the
 115755 ** query parameter we seek.  This routine returns the value of the zParam
 115756 ** parameter if it exists.  If the parameter does not exist, this routine
 115757 ** returns a NULL pointer.
 115759 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
 115760   if( zFilename==0 ) return 0;
 115761   zFilename += sqlite3Strlen30(zFilename) + 1;
 115762   while( zFilename[0] ){
 115763     int x = strcmp(zFilename, zParam);
 115764     zFilename += sqlite3Strlen30(zFilename) + 1;
 115765     if( x==0 ) return zFilename;
 115766     zFilename += sqlite3Strlen30(zFilename) + 1;
 115768   return 0;
 115772 ** Return a boolean value for a query parameter.
 115774 SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
 115775   const char *z = sqlite3_uri_parameter(zFilename, zParam);
 115776   bDflt = bDflt!=0;
 115777   return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
 115781 ** Return a 64-bit integer value for a query parameter.
 115783 SQLITE_API sqlite3_int64 sqlite3_uri_int64(
 115784   const char *zFilename,    /* Filename as passed to xOpen */
 115785   const char *zParam,       /* URI parameter sought */
 115786   sqlite3_int64 bDflt       /* return if parameter is missing */
 115788   const char *z = sqlite3_uri_parameter(zFilename, zParam);
 115789   sqlite3_int64 v;
 115790   if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
 115791     bDflt = v;
 115793   return bDflt;
 115797 ** Return the Btree pointer identified by zDbName.  Return NULL if not found.
 115799 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
 115800   int i;
 115801   for(i=0; i<db->nDb; i++){
 115802     if( db->aDb[i].pBt
 115803      && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
 115805       return db->aDb[i].pBt;
 115808   return 0;
 115812 ** Return the filename of the database associated with a database
 115813 ** connection.
 115815 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
 115816   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
 115817   return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
 115821 ** Return 1 if database is read-only or 0 if read/write.  Return -1 if
 115822 ** no such database exists.
 115824 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
 115825   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
 115826   return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
 115829 /************** End of main.c ************************************************/
 115830 /************** Begin file notify.c ******************************************/
 115832 ** 2009 March 3
 115834 ** The author disclaims copyright to this source code.  In place of
 115835 ** a legal notice, here is a blessing:
 115837 **    May you do good and not evil.
 115838 **    May you find forgiveness for yourself and forgive others.
 115839 **    May you share freely, never taking more than you give.
 115841 *************************************************************************
 115843 ** This file contains the implementation of the sqlite3_unlock_notify()
 115844 ** API method and its associated functionality.
 115847 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
 115848 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 115851 ** Public interfaces:
 115853 **   sqlite3ConnectionBlocked()
 115854 **   sqlite3ConnectionUnlocked()
 115855 **   sqlite3ConnectionClosed()
 115856 **   sqlite3_unlock_notify()
 115859 #define assertMutexHeld() \
 115860   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
 115863 ** Head of a linked list of all sqlite3 objects created by this process
 115864 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
 115865 ** is not NULL. This variable may only accessed while the STATIC_MASTER
 115866 ** mutex is held.
 115868 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
 115870 #ifndef NDEBUG
 115872 ** This function is a complex assert() that verifies the following 
 115873 ** properties of the blocked connections list:
 115875 **   1) Each entry in the list has a non-NULL value for either 
 115876 **      pUnlockConnection or pBlockingConnection, or both.
 115878 **   2) All entries in the list that share a common value for 
 115879 **      xUnlockNotify are grouped together.
 115881 **   3) If the argument db is not NULL, then none of the entries in the
 115882 **      blocked connections list have pUnlockConnection or pBlockingConnection
 115883 **      set to db. This is used when closing connection db.
 115885 static void checkListProperties(sqlite3 *db){
 115886   sqlite3 *p;
 115887   for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
 115888     int seen = 0;
 115889     sqlite3 *p2;
 115891     /* Verify property (1) */
 115892     assert( p->pUnlockConnection || p->pBlockingConnection );
 115894     /* Verify property (2) */
 115895     for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
 115896       if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
 115897       assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
 115898       assert( db==0 || p->pUnlockConnection!=db );
 115899       assert( db==0 || p->pBlockingConnection!=db );
 115903 #else
 115904 # define checkListProperties(x)
 115905 #endif
 115908 ** Remove connection db from the blocked connections list. If connection
 115909 ** db is not currently a part of the list, this function is a no-op.
 115911 static void removeFromBlockedList(sqlite3 *db){
 115912   sqlite3 **pp;
 115913   assertMutexHeld();
 115914   for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
 115915     if( *pp==db ){
 115916       *pp = (*pp)->pNextBlocked;
 115917       break;
 115923 ** Add connection db to the blocked connections list. It is assumed
 115924 ** that it is not already a part of the list.
 115926 static void addToBlockedList(sqlite3 *db){
 115927   sqlite3 **pp;
 115928   assertMutexHeld();
 115929   for(
 115930     pp=&sqlite3BlockedList; 
 115931     *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; 
 115932     pp=&(*pp)->pNextBlocked
 115934   db->pNextBlocked = *pp;
 115935   *pp = db;
 115939 ** Obtain the STATIC_MASTER mutex.
 115941 static void enterMutex(void){
 115942   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 115943   checkListProperties(0);
 115947 ** Release the STATIC_MASTER mutex.
 115949 static void leaveMutex(void){
 115950   assertMutexHeld();
 115951   checkListProperties(0);
 115952   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 115956 ** Register an unlock-notify callback.
 115958 ** This is called after connection "db" has attempted some operation
 115959 ** but has received an SQLITE_LOCKED error because another connection
 115960 ** (call it pOther) in the same process was busy using the same shared
 115961 ** cache.  pOther is found by looking at db->pBlockingConnection.
 115963 ** If there is no blocking connection, the callback is invoked immediately,
 115964 ** before this routine returns.
 115966 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
 115967 ** a deadlock.
 115969 ** Otherwise, make arrangements to invoke xNotify when pOther drops
 115970 ** its locks.
 115972 ** Each call to this routine overrides any prior callbacks registered
 115973 ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
 115974 ** cancelled.
 115976 SQLITE_API int sqlite3_unlock_notify(
 115977   sqlite3 *db,
 115978   void (*xNotify)(void **, int),
 115979   void *pArg
 115981   int rc = SQLITE_OK;
 115983   sqlite3_mutex_enter(db->mutex);
 115984   enterMutex();
 115986   if( xNotify==0 ){
 115987     removeFromBlockedList(db);
 115988     db->pBlockingConnection = 0;
 115989     db->pUnlockConnection = 0;
 115990     db->xUnlockNotify = 0;
 115991     db->pUnlockArg = 0;
 115992   }else if( 0==db->pBlockingConnection ){
 115993     /* The blocking transaction has been concluded. Or there never was a 
 115994     ** blocking transaction. In either case, invoke the notify callback
 115995     ** immediately. 
 115997     xNotify(&pArg, 1);
 115998   }else{
 115999     sqlite3 *p;
 116001     for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
 116002     if( p ){
 116003       rc = SQLITE_LOCKED;              /* Deadlock detected. */
 116004     }else{
 116005       db->pUnlockConnection = db->pBlockingConnection;
 116006       db->xUnlockNotify = xNotify;
 116007       db->pUnlockArg = pArg;
 116008       removeFromBlockedList(db);
 116009       addToBlockedList(db);
 116013   leaveMutex();
 116014   assert( !db->mallocFailed );
 116015   sqlite3Error(db, rc, (rc?"database is deadlocked":0));
 116016   sqlite3_mutex_leave(db->mutex);
 116017   return rc;
 116021 ** This function is called while stepping or preparing a statement 
 116022 ** associated with connection db. The operation will return SQLITE_LOCKED
 116023 ** to the user because it requires a lock that will not be available
 116024 ** until connection pBlocker concludes its current transaction.
 116026 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
 116027   enterMutex();
 116028   if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
 116029     addToBlockedList(db);
 116031   db->pBlockingConnection = pBlocker;
 116032   leaveMutex();
 116036 ** This function is called when
 116037 ** the transaction opened by database db has just finished. Locks held 
 116038 ** by database connection db have been released.
 116040 ** This function loops through each entry in the blocked connections
 116041 ** list and does the following:
 116043 **   1) If the sqlite3.pBlockingConnection member of a list entry is
 116044 **      set to db, then set pBlockingConnection=0.
 116046 **   2) If the sqlite3.pUnlockConnection member of a list entry is
 116047 **      set to db, then invoke the configured unlock-notify callback and
 116048 **      set pUnlockConnection=0.
 116050 **   3) If the two steps above mean that pBlockingConnection==0 and
 116051 **      pUnlockConnection==0, remove the entry from the blocked connections
 116052 **      list.
 116054 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
 116055   void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
 116056   int nArg = 0;                            /* Number of entries in aArg[] */
 116057   sqlite3 **pp;                            /* Iterator variable */
 116058   void **aArg;               /* Arguments to the unlock callback */
 116059   void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
 116060   void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
 116062   aArg = aStatic;
 116063   enterMutex();         /* Enter STATIC_MASTER mutex */
 116065   /* This loop runs once for each entry in the blocked-connections list. */
 116066   for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
 116067     sqlite3 *p = *pp;
 116069     /* Step 1. */
 116070     if( p->pBlockingConnection==db ){
 116071       p->pBlockingConnection = 0;
 116074     /* Step 2. */
 116075     if( p->pUnlockConnection==db ){
 116076       assert( p->xUnlockNotify );
 116077       if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
 116078         xUnlockNotify(aArg, nArg);
 116079         nArg = 0;
 116082       sqlite3BeginBenignMalloc();
 116083       assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
 116084       assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
 116085       if( (!aDyn && nArg==(int)ArraySize(aStatic))
 116086        || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
 116088         /* The aArg[] array needs to grow. */
 116089         void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
 116090         if( pNew ){
 116091           memcpy(pNew, aArg, nArg*sizeof(void *));
 116092           sqlite3_free(aDyn);
 116093           aDyn = aArg = pNew;
 116094         }else{
 116095           /* This occurs when the array of context pointers that need to
 116096           ** be passed to the unlock-notify callback is larger than the
 116097           ** aStatic[] array allocated on the stack and the attempt to 
 116098           ** allocate a larger array from the heap has failed.
 116100           ** This is a difficult situation to handle. Returning an error
 116101           ** code to the caller is insufficient, as even if an error code
 116102           ** is returned the transaction on connection db will still be
 116103           ** closed and the unlock-notify callbacks on blocked connections
 116104           ** will go unissued. This might cause the application to wait
 116105           ** indefinitely for an unlock-notify callback that will never 
 116106           ** arrive.
 116108           ** Instead, invoke the unlock-notify callback with the context
 116109           ** array already accumulated. We can then clear the array and
 116110           ** begin accumulating any further context pointers without 
 116111           ** requiring any dynamic allocation. This is sub-optimal because
 116112           ** it means that instead of one callback with a large array of
 116113           ** context pointers the application will receive two or more
 116114           ** callbacks with smaller arrays of context pointers, which will
 116115           ** reduce the applications ability to prioritize multiple 
 116116           ** connections. But it is the best that can be done under the
 116117           ** circumstances.
 116119           xUnlockNotify(aArg, nArg);
 116120           nArg = 0;
 116123       sqlite3EndBenignMalloc();
 116125       aArg[nArg++] = p->pUnlockArg;
 116126       xUnlockNotify = p->xUnlockNotify;
 116127       p->pUnlockConnection = 0;
 116128       p->xUnlockNotify = 0;
 116129       p->pUnlockArg = 0;
 116132     /* Step 3. */
 116133     if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
 116134       /* Remove connection p from the blocked connections list. */
 116135       *pp = p->pNextBlocked;
 116136       p->pNextBlocked = 0;
 116137     }else{
 116138       pp = &p->pNextBlocked;
 116142   if( nArg!=0 ){
 116143     xUnlockNotify(aArg, nArg);
 116145   sqlite3_free(aDyn);
 116146   leaveMutex();         /* Leave STATIC_MASTER mutex */
 116150 ** This is called when the database connection passed as an argument is 
 116151 ** being closed. The connection is removed from the blocked list.
 116153 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
 116154   sqlite3ConnectionUnlocked(db);
 116155   enterMutex();
 116156   removeFromBlockedList(db);
 116157   checkListProperties(db);
 116158   leaveMutex();
 116160 #endif
 116162 /************** End of notify.c **********************************************/
 116163 /************** Begin file fts3.c ********************************************/
 116165 ** 2006 Oct 10
 116167 ** The author disclaims copyright to this source code.  In place of
 116168 ** a legal notice, here is a blessing:
 116170 **    May you do good and not evil.
 116171 **    May you find forgiveness for yourself and forgive others.
 116172 **    May you share freely, never taking more than you give.
 116174 ******************************************************************************
 116176 ** This is an SQLite module implementing full-text search.
 116180 ** The code in this file is only compiled if:
 116182 **     * The FTS3 module is being built as an extension
 116183 **       (in which case SQLITE_CORE is not defined), or
 116185 **     * The FTS3 module is being built into the core of
 116186 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 116189 /* The full-text index is stored in a series of b+tree (-like)
 116190 ** structures called segments which map terms to doclists.  The
 116191 ** structures are like b+trees in layout, but are constructed from the
 116192 ** bottom up in optimal fashion and are not updatable.  Since trees
 116193 ** are built from the bottom up, things will be described from the
 116194 ** bottom up.
 116197 **** Varints ****
 116198 ** The basic unit of encoding is a variable-length integer called a
 116199 ** varint.  We encode variable-length integers in little-endian order
 116200 ** using seven bits * per byte as follows:
 116202 ** KEY:
 116203 **         A = 0xxxxxxx    7 bits of data and one flag bit
 116204 **         B = 1xxxxxxx    7 bits of data and one flag bit
 116206 **  7 bits - A
 116207 ** 14 bits - BA
 116208 ** 21 bits - BBA
 116209 ** and so on.
 116211 ** This is similar in concept to how sqlite encodes "varints" but
 116212 ** the encoding is not the same.  SQLite varints are big-endian
 116213 ** are are limited to 9 bytes in length whereas FTS3 varints are
 116214 ** little-endian and can be up to 10 bytes in length (in theory).
 116216 ** Example encodings:
 116218 **     1:    0x01
 116219 **   127:    0x7f
 116220 **   128:    0x81 0x00
 116223 **** Document lists ****
 116224 ** A doclist (document list) holds a docid-sorted list of hits for a
 116225 ** given term.  Doclists hold docids and associated token positions.
 116226 ** A docid is the unique integer identifier for a single document.
 116227 ** A position is the index of a word within the document.  The first 
 116228 ** word of the document has a position of 0.
 116230 ** FTS3 used to optionally store character offsets using a compile-time
 116231 ** option.  But that functionality is no longer supported.
 116233 ** A doclist is stored like this:
 116235 ** array {
 116236 **   varint docid;          (delta from previous doclist)
 116237 **   array {                (position list for column 0)
 116238 **     varint position;     (2 more than the delta from previous position)
 116239 **   }
 116240 **   array {
 116241 **     varint POS_COLUMN;   (marks start of position list for new column)
 116242 **     varint column;       (index of new column)
 116243 **     array {
 116244 **       varint position;   (2 more than the delta from previous position)
 116245 **     }
 116246 **   }
 116247 **   varint POS_END;        (marks end of positions for this document.
 116248 ** }
 116250 ** Here, array { X } means zero or more occurrences of X, adjacent in
 116251 ** memory.  A "position" is an index of a token in the token stream
 116252 ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur 
 116253 ** in the same logical place as the position element, and act as sentinals
 116254 ** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
 116255 ** The positions numbers are not stored literally but rather as two more
 116256 ** than the difference from the prior position, or the just the position plus
 116257 ** 2 for the first position.  Example:
 116259 **   label:       A B C D E  F  G H   I  J K
 116260 **   value:     123 5 9 1 1 14 35 0 234 72 0
 116262 ** The 123 value is the first docid.  For column zero in this document
 116263 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
 116264 ** at D signals the start of a new column; the 1 at E indicates that the
 116265 ** new column is column number 1.  There are two positions at 12 and 45
 116266 ** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
 116267 ** 234 at I is the delta to next docid (357).  It has one position 70
 116268 ** (72-2) and then terminates with the 0 at K.
 116270 ** A "position-list" is the list of positions for multiple columns for
 116271 ** a single docid.  A "column-list" is the set of positions for a single
 116272 ** column.  Hence, a position-list consists of one or more column-lists,
 116273 ** a document record consists of a docid followed by a position-list and
 116274 ** a doclist consists of one or more document records.
 116276 ** A bare doclist omits the position information, becoming an 
 116277 ** array of varint-encoded docids.
 116279 **** Segment leaf nodes ****
 116280 ** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
 116281 ** nodes are written using LeafWriter, and read using LeafReader (to
 116282 ** iterate through a single leaf node's data) and LeavesReader (to
 116283 ** iterate through a segment's entire leaf layer).  Leaf nodes have
 116284 ** the format:
 116286 ** varint iHeight;             (height from leaf level, always 0)
 116287 ** varint nTerm;               (length of first term)
 116288 ** char pTerm[nTerm];          (content of first term)
 116289 ** varint nDoclist;            (length of term's associated doclist)
 116290 ** char pDoclist[nDoclist];    (content of doclist)
 116291 ** array {
 116292 **                             (further terms are delta-encoded)
 116293 **   varint nPrefix;           (length of prefix shared with previous term)
 116294 **   varint nSuffix;           (length of unshared suffix)
 116295 **   char pTermSuffix[nSuffix];(unshared suffix of next term)
 116296 **   varint nDoclist;          (length of term's associated doclist)
 116297 **   char pDoclist[nDoclist];  (content of doclist)
 116298 ** }
 116300 ** Here, array { X } means zero or more occurrences of X, adjacent in
 116301 ** memory.
 116303 ** Leaf nodes are broken into blocks which are stored contiguously in
 116304 ** the %_segments table in sorted order.  This means that when the end
 116305 ** of a node is reached, the next term is in the node with the next
 116306 ** greater node id.
 116308 ** New data is spilled to a new leaf node when the current node
 116309 ** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
 116310 ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
 116311 ** node (a leaf node with a single term and doclist).  The goal of
 116312 ** these settings is to pack together groups of small doclists while
 116313 ** making it efficient to directly access large doclists.  The
 116314 ** assumption is that large doclists represent terms which are more
 116315 ** likely to be query targets.
 116317 ** TODO(shess) It may be useful for blocking decisions to be more
 116318 ** dynamic.  For instance, it may make more sense to have a 2.5k leaf
 116319 ** node rather than splitting into 2k and .5k nodes.  My intuition is
 116320 ** that this might extend through 2x or 4x the pagesize.
 116323 **** Segment interior nodes ****
 116324 ** Segment interior nodes store blockids for subtree nodes and terms
 116325 ** to describe what data is stored by the each subtree.  Interior
 116326 ** nodes are written using InteriorWriter, and read using
 116327 ** InteriorReader.  InteriorWriters are created as needed when
 116328 ** SegmentWriter creates new leaf nodes, or when an interior node
 116329 ** itself grows too big and must be split.  The format of interior
 116330 ** nodes:
 116332 ** varint iHeight;           (height from leaf level, always >0)
 116333 ** varint iBlockid;          (block id of node's leftmost subtree)
 116334 ** optional {
 116335 **   varint nTerm;           (length of first term)
 116336 **   char pTerm[nTerm];      (content of first term)
 116337 **   array {
 116338 **                                (further terms are delta-encoded)
 116339 **     varint nPrefix;            (length of shared prefix with previous term)
 116340 **     varint nSuffix;            (length of unshared suffix)
 116341 **     char pTermSuffix[nSuffix]; (unshared suffix of next term)
 116342 **   }
 116343 ** }
 116345 ** Here, optional { X } means an optional element, while array { X }
 116346 ** means zero or more occurrences of X, adjacent in memory.
 116348 ** An interior node encodes n terms separating n+1 subtrees.  The
 116349 ** subtree blocks are contiguous, so only the first subtree's blockid
 116350 ** is encoded.  The subtree at iBlockid will contain all terms less
 116351 ** than the first term encoded (or all terms if no term is encoded).
 116352 ** Otherwise, for terms greater than or equal to pTerm[i] but less
 116353 ** than pTerm[i+1], the subtree for that term will be rooted at
 116354 ** iBlockid+i.  Interior nodes only store enough term data to
 116355 ** distinguish adjacent children (if the rightmost term of the left
 116356 ** child is "something", and the leftmost term of the right child is
 116357 ** "wicked", only "w" is stored).
 116359 ** New data is spilled to a new interior node at the same height when
 116360 ** the current node exceeds INTERIOR_MAX bytes (default 2048).
 116361 ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
 116362 ** interior nodes and making the tree too skinny.  The interior nodes
 116363 ** at a given height are naturally tracked by interior nodes at
 116364 ** height+1, and so on.
 116367 **** Segment directory ****
 116368 ** The segment directory in table %_segdir stores meta-information for
 116369 ** merging and deleting segments, and also the root node of the
 116370 ** segment's tree.
 116372 ** The root node is the top node of the segment's tree after encoding
 116373 ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
 116374 ** This could be either a leaf node or an interior node.  If the top
 116375 ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
 116376 ** and a new root interior node is generated (which should always fit
 116377 ** within ROOT_MAX because it only needs space for 2 varints, the
 116378 ** height and the blockid of the previous root).
 116380 ** The meta-information in the segment directory is:
 116381 **   level               - segment level (see below)
 116382 **   idx                 - index within level
 116383 **                       - (level,idx uniquely identify a segment)
 116384 **   start_block         - first leaf node
 116385 **   leaves_end_block    - last leaf node
 116386 **   end_block           - last block (including interior nodes)
 116387 **   root                - contents of root node
 116389 ** If the root node is a leaf node, then start_block,
 116390 ** leaves_end_block, and end_block are all 0.
 116393 **** Segment merging ****
 116394 ** To amortize update costs, segments are grouped into levels and
 116395 ** merged in batches.  Each increase in level represents exponentially
 116396 ** more documents.
 116398 ** New documents (actually, document updates) are tokenized and
 116399 ** written individually (using LeafWriter) to a level 0 segment, with
 116400 ** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
 116401 ** level 0 segments are merged into a single level 1 segment.  Level 1
 116402 ** is populated like level 0, and eventually MERGE_COUNT level 1
 116403 ** segments are merged to a single level 2 segment (representing
 116404 ** MERGE_COUNT^2 updates), and so on.
 116406 ** A segment merge traverses all segments at a given level in
 116407 ** parallel, performing a straightforward sorted merge.  Since segment
 116408 ** leaf nodes are written in to the %_segments table in order, this
 116409 ** merge traverses the underlying sqlite disk structures efficiently.
 116410 ** After the merge, all segment blocks from the merged level are
 116411 ** deleted.
 116413 ** MERGE_COUNT controls how often we merge segments.  16 seems to be
 116414 ** somewhat of a sweet spot for insertion performance.  32 and 64 show
 116415 ** very similar performance numbers to 16 on insertion, though they're
 116416 ** a tiny bit slower (perhaps due to more overhead in merge-time
 116417 ** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
 116418 ** 16, 2 about 66% slower than 16.
 116420 ** At query time, high MERGE_COUNT increases the number of segments
 116421 ** which need to be scanned and merged.  For instance, with 100k docs
 116422 ** inserted:
 116424 **    MERGE_COUNT   segments
 116425 **       16           25
 116426 **        8           12
 116427 **        4           10
 116428 **        2            6
 116430 ** This appears to have only a moderate impact on queries for very
 116431 ** frequent terms (which are somewhat dominated by segment merge
 116432 ** costs), and infrequent and non-existent terms still seem to be fast
 116433 ** even with many segments.
 116435 ** TODO(shess) That said, it would be nice to have a better query-side
 116436 ** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
 116437 ** optimizations to things like doclist merging will swing the sweet
 116438 ** spot around.
 116442 **** Handling of deletions and updates ****
 116443 ** Since we're using a segmented structure, with no docid-oriented
 116444 ** index into the term index, we clearly cannot simply update the term
 116445 ** index when a document is deleted or updated.  For deletions, we
 116446 ** write an empty doclist (varint(docid) varint(POS_END)), for updates
 116447 ** we simply write the new doclist.  Segment merges overwrite older
 116448 ** data for a particular docid with newer data, so deletes or updates
 116449 ** will eventually overtake the earlier data and knock it out.  The
 116450 ** query logic likewise merges doclists so that newer data knocks out
 116451 ** older data.
 116454 /************** Include fts3Int.h in the middle of fts3.c ********************/
 116455 /************** Begin file fts3Int.h *****************************************/
 116457 ** 2009 Nov 12
 116459 ** The author disclaims copyright to this source code.  In place of
 116460 ** a legal notice, here is a blessing:
 116462 **    May you do good and not evil.
 116463 **    May you find forgiveness for yourself and forgive others.
 116464 **    May you share freely, never taking more than you give.
 116466 ******************************************************************************
 116469 #ifndef _FTSINT_H
 116470 #define _FTSINT_H
 116472 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
 116473 # define NDEBUG 1
 116474 #endif
 116477 ** FTS4 is really an extension for FTS3.  It is enabled using the
 116478 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
 116479 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
 116481 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
 116482 # define SQLITE_ENABLE_FTS3
 116483 #endif
 116485 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 116487 /* If not building as part of the core, include sqlite3ext.h. */
 116488 #ifndef SQLITE_CORE
 116489 SQLITE_API extern const sqlite3_api_routines *sqlite3_api;
 116490 #endif
 116492 /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
 116493 /************** Begin file fts3_tokenizer.h **********************************/
 116495 ** 2006 July 10
 116497 ** The author disclaims copyright to this source code.
 116499 *************************************************************************
 116500 ** Defines the interface to tokenizers used by fulltext-search.  There
 116501 ** are three basic components:
 116503 ** sqlite3_tokenizer_module is a singleton defining the tokenizer
 116504 ** interface functions.  This is essentially the class structure for
 116505 ** tokenizers.
 116507 ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
 116508 ** including customization information defined at creation time.
 116510 ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
 116511 ** tokens from a particular input.
 116513 #ifndef _FTS3_TOKENIZER_H_
 116514 #define _FTS3_TOKENIZER_H_
 116516 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
 116517 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
 116518 ** we will need a way to register the API consistently.
 116522 ** Structures used by the tokenizer interface. When a new tokenizer
 116523 ** implementation is registered, the caller provides a pointer to
 116524 ** an sqlite3_tokenizer_module containing pointers to the callback
 116525 ** functions that make up an implementation.
 116527 ** When an fts3 table is created, it passes any arguments passed to
 116528 ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
 116529 ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
 116530 ** implementation. The xCreate() function in turn returns an 
 116531 ** sqlite3_tokenizer structure representing the specific tokenizer to
 116532 ** be used for the fts3 table (customized by the tokenizer clause arguments).
 116534 ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
 116535 ** method is called. It returns an sqlite3_tokenizer_cursor object
 116536 ** that may be used to tokenize a specific input buffer based on
 116537 ** the tokenization rules supplied by a specific sqlite3_tokenizer
 116538 ** object.
 116540 typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
 116541 typedef struct sqlite3_tokenizer sqlite3_tokenizer;
 116542 typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
 116544 struct sqlite3_tokenizer_module {
 116547   ** Structure version. Should always be set to 0 or 1.
 116549   int iVersion;
 116552   ** Create a new tokenizer. The values in the argv[] array are the
 116553   ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
 116554   ** TABLE statement that created the fts3 table. For example, if
 116555   ** the following SQL is executed:
 116557   **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
 116559   ** then argc is set to 2, and the argv[] array contains pointers
 116560   ** to the strings "arg1" and "arg2".
 116562   ** This method should return either SQLITE_OK (0), or an SQLite error 
 116563   ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
 116564   ** to point at the newly created tokenizer structure. The generic
 116565   ** sqlite3_tokenizer.pModule variable should not be initialised by
 116566   ** this callback. The caller will do so.
 116568   int (*xCreate)(
 116569     int argc,                           /* Size of argv array */
 116570     const char *const*argv,             /* Tokenizer argument strings */
 116571     sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
 116575   ** Destroy an existing tokenizer. The fts3 module calls this method
 116576   ** exactly once for each successful call to xCreate().
 116578   int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
 116581   ** Create a tokenizer cursor to tokenize an input buffer. The caller
 116582   ** is responsible for ensuring that the input buffer remains valid
 116583   ** until the cursor is closed (using the xClose() method). 
 116585   int (*xOpen)(
 116586     sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
 116587     const char *pInput, int nBytes,      /* Input buffer */
 116588     sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
 116592   ** Destroy an existing tokenizer cursor. The fts3 module calls this 
 116593   ** method exactly once for each successful call to xOpen().
 116595   int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
 116598   ** Retrieve the next token from the tokenizer cursor pCursor. This
 116599   ** method should either return SQLITE_OK and set the values of the
 116600   ** "OUT" variables identified below, or SQLITE_DONE to indicate that
 116601   ** the end of the buffer has been reached, or an SQLite error code.
 116603   ** *ppToken should be set to point at a buffer containing the 
 116604   ** normalized version of the token (i.e. after any case-folding and/or
 116605   ** stemming has been performed). *pnBytes should be set to the length
 116606   ** of this buffer in bytes. The input text that generated the token is
 116607   ** identified by the byte offsets returned in *piStartOffset and
 116608   ** *piEndOffset. *piStartOffset should be set to the index of the first
 116609   ** byte of the token in the input buffer. *piEndOffset should be set
 116610   ** to the index of the first byte just past the end of the token in
 116611   ** the input buffer.
 116613   ** The buffer *ppToken is set to point at is managed by the tokenizer
 116614   ** implementation. It is only required to be valid until the next call
 116615   ** to xNext() or xClose(). 
 116617   /* TODO(shess) current implementation requires pInput to be
 116618   ** nul-terminated.  This should either be fixed, or pInput/nBytes
 116619   ** should be converted to zInput.
 116621   int (*xNext)(
 116622     sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
 116623     const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
 116624     int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
 116625     int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
 116626     int *piPosition      /* OUT: Number of tokens returned before this one */
 116629   /***********************************************************************
 116630   ** Methods below this point are only available if iVersion>=1.
 116634   ** Configure the language id of a tokenizer cursor.
 116636   int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
 116639 struct sqlite3_tokenizer {
 116640   const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
 116641   /* Tokenizer implementations will typically add additional fields */
 116644 struct sqlite3_tokenizer_cursor {
 116645   sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
 116646   /* Tokenizer implementations will typically add additional fields */
 116649 int fts3_global_term_cnt(int iTerm, int iCol);
 116650 int fts3_term_cnt(int iTerm, int iCol);
 116653 #endif /* _FTS3_TOKENIZER_H_ */
 116655 /************** End of fts3_tokenizer.h **************************************/
 116656 /************** Continuing where we left off in fts3Int.h ********************/
 116657 /************** Include fts3_hash.h in the middle of fts3Int.h ***************/
 116658 /************** Begin file fts3_hash.h ***************************************/
 116660 ** 2001 September 22
 116662 ** The author disclaims copyright to this source code.  In place of
 116663 ** a legal notice, here is a blessing:
 116665 **    May you do good and not evil.
 116666 **    May you find forgiveness for yourself and forgive others.
 116667 **    May you share freely, never taking more than you give.
 116669 *************************************************************************
 116670 ** This is the header file for the generic hash-table implemenation
 116671 ** used in SQLite.  We've modified it slightly to serve as a standalone
 116672 ** hash table implementation for the full-text indexing module.
 116675 #ifndef _FTS3_HASH_H_
 116676 #define _FTS3_HASH_H_
 116678 /* Forward declarations of structures. */
 116679 typedef struct Fts3Hash Fts3Hash;
 116680 typedef struct Fts3HashElem Fts3HashElem;
 116682 /* A complete hash table is an instance of the following structure.
 116683 ** The internals of this structure are intended to be opaque -- client
 116684 ** code should not attempt to access or modify the fields of this structure
 116685 ** directly.  Change this structure only by using the routines below.
 116686 ** However, many of the "procedures" and "functions" for modifying and
 116687 ** accessing this structure are really macros, so we can't really make
 116688 ** this structure opaque.
 116690 struct Fts3Hash {
 116691   char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
 116692   char copyKey;           /* True if copy of key made on insert */
 116693   int count;              /* Number of entries in this table */
 116694   Fts3HashElem *first;    /* The first element of the array */
 116695   int htsize;             /* Number of buckets in the hash table */
 116696   struct _fts3ht {        /* the hash table */
 116697     int count;               /* Number of entries with this hash */
 116698     Fts3HashElem *chain;     /* Pointer to first entry with this hash */
 116699   } *ht;
 116702 /* Each element in the hash table is an instance of the following 
 116703 ** structure.  All elements are stored on a single doubly-linked list.
 116705 ** Again, this structure is intended to be opaque, but it can't really
 116706 ** be opaque because it is used by macros.
 116708 struct Fts3HashElem {
 116709   Fts3HashElem *next, *prev; /* Next and previous elements in the table */
 116710   void *data;                /* Data associated with this element */
 116711   void *pKey; int nKey;      /* Key associated with this element */
 116715 ** There are 2 different modes of operation for a hash table:
 116717 **   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
 116718 **                           (including the null-terminator, if any).  Case
 116719 **                           is respected in comparisons.
 116721 **   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long. 
 116722 **                           memcmp() is used to compare keys.
 116724 ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.  
 116726 #define FTS3_HASH_STRING    1
 116727 #define FTS3_HASH_BINARY    2
 116730 ** Access routines.  To delete, insert a NULL pointer.
 116732 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
 116733 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
 116734 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
 116735 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
 116736 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
 116739 ** Shorthand for the functions above
 116741 #define fts3HashInit     sqlite3Fts3HashInit
 116742 #define fts3HashInsert   sqlite3Fts3HashInsert
 116743 #define fts3HashFind     sqlite3Fts3HashFind
 116744 #define fts3HashClear    sqlite3Fts3HashClear
 116745 #define fts3HashFindElem sqlite3Fts3HashFindElem
 116748 ** Macros for looping over all elements of a hash table.  The idiom is
 116749 ** like this:
 116751 **   Fts3Hash h;
 116752 **   Fts3HashElem *p;
 116753 **   ...
 116754 **   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
 116755 **     SomeStructure *pData = fts3HashData(p);
 116756 **     // do something with pData
 116757 **   }
 116759 #define fts3HashFirst(H)  ((H)->first)
 116760 #define fts3HashNext(E)   ((E)->next)
 116761 #define fts3HashData(E)   ((E)->data)
 116762 #define fts3HashKey(E)    ((E)->pKey)
 116763 #define fts3HashKeysize(E) ((E)->nKey)
 116766 ** Number of entries in a hash table
 116768 #define fts3HashCount(H)  ((H)->count)
 116770 #endif /* _FTS3_HASH_H_ */
 116772 /************** End of fts3_hash.h *******************************************/
 116773 /************** Continuing where we left off in fts3Int.h ********************/
 116776 ** This constant controls how often segments are merged. Once there are
 116777 ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
 116778 ** segment of level N+1.
 116780 #define FTS3_MERGE_COUNT 16
 116783 ** This is the maximum amount of data (in bytes) to store in the 
 116784 ** Fts3Table.pendingTerms hash table. Normally, the hash table is
 116785 ** populated as documents are inserted/updated/deleted in a transaction
 116786 ** and used to create a new segment when the transaction is committed.
 116787 ** However if this limit is reached midway through a transaction, a new 
 116788 ** segment is created and the hash table cleared immediately.
 116790 #define FTS3_MAX_PENDING_DATA (1*1024*1024)
 116793 ** Macro to return the number of elements in an array. SQLite has a
 116794 ** similar macro called ArraySize(). Use a different name to avoid
 116795 ** a collision when building an amalgamation with built-in FTS3.
 116797 #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
 116800 #ifndef MIN
 116801 # define MIN(x,y) ((x)<(y)?(x):(y))
 116802 #endif
 116803 #ifndef MAX
 116804 # define MAX(x,y) ((x)>(y)?(x):(y))
 116805 #endif
 116808 ** Maximum length of a varint encoded integer. The varint format is different
 116809 ** from that used by SQLite, so the maximum length is 10, not 9.
 116811 #define FTS3_VARINT_MAX 10
 116814 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
 116815 ** in the document set and zero or more prefix indexes. All indexes are stored
 116816 ** as one or more b+-trees in the %_segments and %_segdir tables. 
 116818 ** It is possible to determine which index a b+-tree belongs to based on the
 116819 ** value stored in the "%_segdir.level" column. Given this value L, the index
 116820 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
 116821 ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
 116822 ** between 1024 and 2047 to index 1, and so on.
 116824 ** It is considered impossible for an index to use more than 1024 levels. In 
 116825 ** theory though this may happen, but only after at least 
 116826 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
 116828 #define FTS3_SEGDIR_MAXLEVEL      1024
 116829 #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
 116832 ** The testcase() macro is only used by the amalgamation.  If undefined,
 116833 ** make it a no-op.
 116835 #ifndef testcase
 116836 # define testcase(X)
 116837 #endif
 116840 ** Terminator values for position-lists and column-lists.
 116842 #define POS_COLUMN  (1)     /* Column-list terminator */
 116843 #define POS_END     (0)     /* Position-list terminator */ 
 116846 ** This section provides definitions to allow the
 116847 ** FTS3 extension to be compiled outside of the 
 116848 ** amalgamation.
 116850 #ifndef SQLITE_AMALGAMATION
 116852 ** Macros indicating that conditional expressions are always true or
 116853 ** false.
 116855 #ifdef SQLITE_COVERAGE_TEST
 116856 # define ALWAYS(x) (1)
 116857 # define NEVER(X)  (0)
 116858 #else
 116859 # define ALWAYS(x) (x)
 116860 # define NEVER(x)  (x)
 116861 #endif
 116864 ** Internal types used by SQLite.
 116866 typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
 116867 typedef short int i16;            /* 2-byte (or larger) signed integer */
 116868 typedef unsigned int u32;         /* 4-byte unsigned integer */
 116869 typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
 116870 typedef sqlite3_int64 i64;        /* 8-byte signed integer */
 116873 ** Macro used to suppress compiler warnings for unused parameters.
 116875 #define UNUSED_PARAMETER(x) (void)(x)
 116878 ** Activate assert() only if SQLITE_TEST is enabled.
 116880 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
 116881 # define NDEBUG 1
 116882 #endif
 116885 ** The TESTONLY macro is used to enclose variable declarations or
 116886 ** other bits of code that are needed to support the arguments
 116887 ** within testcase() and assert() macros.
 116889 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 116890 # define TESTONLY(X)  X
 116891 #else
 116892 # define TESTONLY(X)
 116893 #endif
 116895 #endif /* SQLITE_AMALGAMATION */
 116897 #ifdef SQLITE_DEBUG
 116898 SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
 116899 # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
 116900 #else
 116901 # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
 116902 #endif
 116904 typedef struct Fts3Table Fts3Table;
 116905 typedef struct Fts3Cursor Fts3Cursor;
 116906 typedef struct Fts3Expr Fts3Expr;
 116907 typedef struct Fts3Phrase Fts3Phrase;
 116908 typedef struct Fts3PhraseToken Fts3PhraseToken;
 116910 typedef struct Fts3Doclist Fts3Doclist;
 116911 typedef struct Fts3SegFilter Fts3SegFilter;
 116912 typedef struct Fts3DeferredToken Fts3DeferredToken;
 116913 typedef struct Fts3SegReader Fts3SegReader;
 116914 typedef struct Fts3MultiSegReader Fts3MultiSegReader;
 116917 ** A connection to a fulltext index is an instance of the following
 116918 ** structure. The xCreate and xConnect methods create an instance
 116919 ** of this structure and xDestroy and xDisconnect free that instance.
 116920 ** All other methods receive a pointer to the structure as one of their
 116921 ** arguments.
 116923 struct Fts3Table {
 116924   sqlite3_vtab base;              /* Base class used by SQLite core */
 116925   sqlite3 *db;                    /* The database connection */
 116926   const char *zDb;                /* logical database name */
 116927   const char *zName;              /* virtual table name */
 116928   int nColumn;                    /* number of named columns in virtual table */
 116929   char **azColumn;                /* column names.  malloced */
 116930   sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
 116931   char *zContentTbl;              /* content=xxx option, or NULL */
 116932   char *zLanguageid;              /* languageid=xxx option, or NULL */
 116933   u8 bAutoincrmerge;              /* True if automerge=1 */
 116934   u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
 116936   /* Precompiled statements used by the implementation. Each of these 
 116937   ** statements is run and reset within a single virtual table API call. 
 116939   sqlite3_stmt *aStmt[37];
 116941   char *zReadExprlist;
 116942   char *zWriteExprlist;
 116944   int nNodeSize;                  /* Soft limit for node size */
 116945   u8 bFts4;                       /* True for FTS4, false for FTS3 */
 116946   u8 bHasStat;                    /* True if %_stat table exists */
 116947   u8 bHasDocsize;                 /* True if %_docsize table exists */
 116948   u8 bDescIdx;                    /* True if doclists are in reverse order */
 116949   u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
 116950   int nPgsz;                      /* Page size for host database */
 116951   char *zSegmentsTbl;             /* Name of %_segments table */
 116952   sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
 116955   ** The following array of hash tables is used to buffer pending index 
 116956   ** updates during transactions. All pending updates buffered at any one
 116957   ** time must share a common language-id (see the FTS4 langid= feature).
 116958   ** The current language id is stored in variable iPrevLangid.
 116960   ** A single FTS4 table may have multiple full-text indexes. For each index
 116961   ** there is an entry in the aIndex[] array. Index 0 is an index of all the
 116962   ** terms that appear in the document set. Each subsequent index in aIndex[]
 116963   ** is an index of prefixes of a specific length.
 116965   ** Variable nPendingData contains an estimate the memory consumed by the 
 116966   ** pending data structures, including hash table overhead, but not including
 116967   ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
 116968   ** tables are flushed to disk. Variable iPrevDocid is the docid of the most 
 116969   ** recently inserted record.
 116971   int nIndex;                     /* Size of aIndex[] */
 116972   struct Fts3Index {
 116973     int nPrefix;                  /* Prefix length (0 for main terms index) */
 116974     Fts3Hash hPending;            /* Pending terms table for this index */
 116975   } *aIndex;
 116976   int nMaxPendingData;            /* Max pending data before flush to disk */
 116977   int nPendingData;               /* Current bytes of pending data */
 116978   sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
 116979   int iPrevLangid;                /* Langid of recently inserted document */
 116981 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 116982   /* State variables used for validating that the transaction control
 116983   ** methods of the virtual table are called at appropriate times.  These
 116984   ** values do not contribute to FTS functionality; they are used for
 116985   ** verifying the operation of the SQLite core.
 116987   int inTransaction;     /* True after xBegin but before xCommit/xRollback */
 116988   int mxSavepoint;       /* Largest valid xSavepoint integer */
 116989 #endif
 116993 ** When the core wants to read from the virtual table, it creates a
 116994 ** virtual table cursor (an instance of the following structure) using
 116995 ** the xOpen method. Cursors are destroyed using the xClose method.
 116997 struct Fts3Cursor {
 116998   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
 116999   i16 eSearch;                    /* Search strategy (see below) */
 117000   u8 isEof;                       /* True if at End Of Results */
 117001   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
 117002   sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
 117003   Fts3Expr *pExpr;                /* Parsed MATCH query string */
 117004   int iLangid;                    /* Language being queried for */
 117005   int nPhrase;                    /* Number of matchable phrases in query */
 117006   Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
 117007   sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
 117008   char *pNextId;                  /* Pointer into the body of aDoclist */
 117009   char *aDoclist;                 /* List of docids for full-text queries */
 117010   int nDoclist;                   /* Size of buffer at aDoclist */
 117011   u8 bDesc;                       /* True to sort in descending order */
 117012   int eEvalmode;                  /* An FTS3_EVAL_XX constant */
 117013   int nRowAvg;                    /* Average size of database rows, in pages */
 117014   sqlite3_int64 nDoc;             /* Documents in table */
 117016   int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
 117017   u32 *aMatchinfo;                /* Information about most recent match */
 117018   int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
 117019   char *zMatchinfo;               /* Matchinfo specification */
 117022 #define FTS3_EVAL_FILTER    0
 117023 #define FTS3_EVAL_NEXT      1
 117024 #define FTS3_EVAL_MATCHINFO 2
 117027 ** The Fts3Cursor.eSearch member is always set to one of the following.
 117028 ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
 117029 ** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
 117030 ** of the column to be searched.  For example, in
 117032 **     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
 117033 **     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
 117035 ** Because the LHS of the MATCH operator is 2nd column "b",
 117036 ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
 117037 ** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1" 
 117038 ** indicating that all columns should be searched,
 117039 ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
 117041 #define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
 117042 #define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
 117043 #define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
 117046 struct Fts3Doclist {
 117047   char *aAll;                    /* Array containing doclist (or NULL) */
 117048   int nAll;                      /* Size of a[] in bytes */
 117049   char *pNextDocid;              /* Pointer to next docid */
 117051   sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
 117052   int bFreeList;                 /* True if pList should be sqlite3_free()d */
 117053   char *pList;                   /* Pointer to position list following iDocid */
 117054   int nList;                     /* Length of position list */
 117058 ** A "phrase" is a sequence of one or more tokens that must match in
 117059 ** sequence.  A single token is the base case and the most common case.
 117060 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
 117061 ** nToken will be the number of tokens in the string.
 117063 struct Fts3PhraseToken {
 117064   char *z;                        /* Text of the token */
 117065   int n;                          /* Number of bytes in buffer z */
 117066   int isPrefix;                   /* True if token ends with a "*" character */
 117067   int bFirst;                     /* True if token must appear at position 0 */
 117069   /* Variables above this point are populated when the expression is
 117070   ** parsed (by code in fts3_expr.c). Below this point the variables are
 117071   ** used when evaluating the expression. */
 117072   Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
 117073   Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
 117076 struct Fts3Phrase {
 117077   /* Cache of doclist for this phrase. */
 117078   Fts3Doclist doclist;
 117079   int bIncr;                 /* True if doclist is loaded incrementally */
 117080   int iDoclistToken;
 117082   /* Variables below this point are populated by fts3_expr.c when parsing 
 117083   ** a MATCH expression. Everything above is part of the evaluation phase. 
 117085   int nToken;                /* Number of tokens in the phrase */
 117086   int iColumn;               /* Index of column this phrase must match */
 117087   Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
 117091 ** A tree of these objects forms the RHS of a MATCH operator.
 117093 ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist 
 117094 ** points to a malloced buffer, size nDoclist bytes, containing the results 
 117095 ** of this phrase query in FTS3 doclist format. As usual, the initial 
 117096 ** "Length" field found in doclists stored on disk is omitted from this 
 117097 ** buffer.
 117099 ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
 117100 ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
 117101 ** where nCol is the number of columns in the queried FTS table. The array
 117102 ** is populated as follows:
 117104 **   aMI[iCol*3 + 0] = Undefined
 117105 **   aMI[iCol*3 + 1] = Number of occurrences
 117106 **   aMI[iCol*3 + 2] = Number of rows containing at least one instance
 117108 ** The aMI array is allocated using sqlite3_malloc(). It should be freed 
 117109 ** when the expression node is.
 117111 struct Fts3Expr {
 117112   int eType;                 /* One of the FTSQUERY_XXX values defined below */
 117113   int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
 117114   Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
 117115   Fts3Expr *pLeft;           /* Left operand */
 117116   Fts3Expr *pRight;          /* Right operand */
 117117   Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
 117119   /* The following are used by the fts3_eval.c module. */
 117120   sqlite3_int64 iDocid;      /* Current docid */
 117121   u8 bEof;                   /* True this expression is at EOF already */
 117122   u8 bStart;                 /* True if iDocid is valid */
 117123   u8 bDeferred;              /* True if this expression is entirely deferred */
 117125   u32 *aMI;
 117129 ** Candidate values for Fts3Query.eType. Note that the order of the first
 117130 ** four values is in order of precedence when parsing expressions. For 
 117131 ** example, the following:
 117133 **   "a OR b AND c NOT d NEAR e"
 117135 ** is equivalent to:
 117137 **   "a OR (b AND (c NOT (d NEAR e)))"
 117139 #define FTSQUERY_NEAR   1
 117140 #define FTSQUERY_NOT    2
 117141 #define FTSQUERY_AND    3
 117142 #define FTSQUERY_OR     4
 117143 #define FTSQUERY_PHRASE 5
 117146 /* fts3_write.c */
 117147 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
 117148 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
 117149 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
 117150 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
 117151 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
 117152   sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
 117153 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
 117154   Fts3Table*,int,const char*,int,int,Fts3SegReader**);
 117155 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
 117156 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
 117157 SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
 117158 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
 117160 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
 117161 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
 117163 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 117164 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
 117165 SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
 117166 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
 117167 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
 117168 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
 117169 #else
 117170 # define sqlite3Fts3FreeDeferredTokens(x)
 117171 # define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
 117172 # define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
 117173 # define sqlite3Fts3FreeDeferredDoclists(x)
 117174 # define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
 117175 #endif
 117177 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
 117178 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
 117180 /* Special values interpreted by sqlite3SegReaderCursor() */
 117181 #define FTS3_SEGCURSOR_PENDING        -1
 117182 #define FTS3_SEGCURSOR_ALL            -2
 117184 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
 117185 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
 117186 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
 117188 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *, 
 117189     int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
 117191 /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
 117192 #define FTS3_SEGMENT_REQUIRE_POS   0x00000001
 117193 #define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
 117194 #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
 117195 #define FTS3_SEGMENT_PREFIX        0x00000008
 117196 #define FTS3_SEGMENT_SCAN          0x00000010
 117197 #define FTS3_SEGMENT_FIRST         0x00000020
 117199 /* Type passed as 4th argument to SegmentReaderIterate() */
 117200 struct Fts3SegFilter {
 117201   const char *zTerm;
 117202   int nTerm;
 117203   int iCol;
 117204   int flags;
 117207 struct Fts3MultiSegReader {
 117208   /* Used internally by sqlite3Fts3SegReaderXXX() calls */
 117209   Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
 117210   int nSegment;                   /* Size of apSegment array */
 117211   int nAdvance;                   /* How many seg-readers to advance */
 117212   Fts3SegFilter *pFilter;         /* Pointer to filter object */
 117213   char *aBuffer;                  /* Buffer to merge doclists in */
 117214   int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
 117216   int iColFilter;                 /* If >=0, filter for this column */
 117217   int bRestart;
 117219   /* Used by fts3.c only. */
 117220   int nCost;                      /* Cost of running iterator */
 117221   int bLookup;                    /* True if a lookup of a single entry. */
 117223   /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
 117224   char *zTerm;                    /* Pointer to term buffer */
 117225   int nTerm;                      /* Size of zTerm in bytes */
 117226   char *aDoclist;                 /* Pointer to doclist buffer */
 117227   int nDoclist;                   /* Size of aDoclist[] in bytes */
 117230 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
 117232 /* fts3.c */
 117233 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
 117234 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
 117235 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
 117236 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
 117237 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
 117238 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
 117239 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
 117240 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
 117241 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
 117243 /* fts3_tokenizer.c */
 117244 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
 117245 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
 117246 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
 117247     sqlite3_tokenizer **, char **
 117249 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
 117251 /* fts3_snippet.c */
 117252 SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
 117253 SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
 117254   const char *, const char *, int, int
 117256 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
 117258 /* fts3_expr.c */
 117259 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
 117260   char **, int, int, int, const char *, int, Fts3Expr **
 117262 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
 117263 #ifdef SQLITE_TEST
 117264 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
 117265 SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
 117266 #endif
 117268 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
 117269   sqlite3_tokenizer_cursor **
 117272 /* fts3_aux.c */
 117273 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
 117275 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
 117277 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
 117278     Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
 117279 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
 117280     Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
 117281 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); 
 117282 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
 117283 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
 117285 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
 117286 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 117287 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
 117288 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
 117289 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
 117290 #endif
 117292 #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
 117293 #endif /* _FTSINT_H */
 117295 /************** End of fts3Int.h *********************************************/
 117296 /************** Continuing where we left off in fts3.c ***********************/
 117297 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 117299 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
 117300 # define SQLITE_CORE 1
 117301 #endif
 117303 /* #include <assert.h> */
 117304 /* #include <stdlib.h> */
 117305 /* #include <stddef.h> */
 117306 /* #include <stdio.h> */
 117307 /* #include <string.h> */
 117308 /* #include <stdarg.h> */
 117310 #ifndef SQLITE_CORE 
 117311   SQLITE_EXTENSION_INIT1
 117312 #endif
 117314 static int fts3EvalNext(Fts3Cursor *pCsr);
 117315 static int fts3EvalStart(Fts3Cursor *pCsr);
 117316 static int fts3TermSegReaderCursor(
 117317     Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
 117320 ** Write a 64-bit variable-length integer to memory starting at p[0].
 117321 ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
 117322 ** The number of bytes written is returned.
 117324 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
 117325   unsigned char *q = (unsigned char *) p;
 117326   sqlite_uint64 vu = v;
 117327   do{
 117328     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
 117329     vu >>= 7;
 117330   }while( vu!=0 );
 117331   q[-1] &= 0x7f;  /* turn off high bit in final byte */
 117332   assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
 117333   return (int) (q - (unsigned char *)p);
 117337 ** Read a 64-bit variable-length integer from memory starting at p[0].
 117338 ** Return the number of bytes read, or 0 on error.
 117339 ** The value is stored in *v.
 117341 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
 117342   const unsigned char *q = (const unsigned char *) p;
 117343   sqlite_uint64 x = 0, y = 1;
 117344   while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
 117345     x += y * (*q++ & 0x7f);
 117346     y <<= 7;
 117348   x += y * (*q++);
 117349   *v = (sqlite_int64) x;
 117350   return (int) (q - (unsigned char *)p);
 117354 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
 117355 ** 32-bit integer before it is returned.
 117357 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
 117358  sqlite_int64 i;
 117359  int ret = sqlite3Fts3GetVarint(p, &i);
 117360  *pi = (int) i;
 117361  return ret;
 117365 ** Return the number of bytes required to encode v as a varint
 117367 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
 117368   int i = 0;
 117369   do{
 117370     i++;
 117371     v >>= 7;
 117372   }while( v!=0 );
 117373   return i;
 117377 ** Convert an SQL-style quoted string into a normal string by removing
 117378 ** the quote characters.  The conversion is done in-place.  If the
 117379 ** input does not begin with a quote character, then this routine
 117380 ** is a no-op.
 117382 ** Examples:
 117384 **     "abc"   becomes   abc
 117385 **     'xyz'   becomes   xyz
 117386 **     [pqr]   becomes   pqr
 117387 **     `mno`   becomes   mno
 117390 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
 117391   char quote;                     /* Quote character (if any ) */
 117393   quote = z[0];
 117394   if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
 117395     int iIn = 1;                  /* Index of next byte to read from input */
 117396     int iOut = 0;                 /* Index of next byte to write to output */
 117398     /* If the first byte was a '[', then the close-quote character is a ']' */
 117399     if( quote=='[' ) quote = ']';  
 117401     while( ALWAYS(z[iIn]) ){
 117402       if( z[iIn]==quote ){
 117403         if( z[iIn+1]!=quote ) break;
 117404         z[iOut++] = quote;
 117405         iIn += 2;
 117406       }else{
 117407         z[iOut++] = z[iIn++];
 117410     z[iOut] = '\0';
 117415 ** Read a single varint from the doclist at *pp and advance *pp to point
 117416 ** to the first byte past the end of the varint.  Add the value of the varint
 117417 ** to *pVal.
 117419 static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
 117420   sqlite3_int64 iVal;
 117421   *pp += sqlite3Fts3GetVarint(*pp, &iVal);
 117422   *pVal += iVal;
 117426 ** When this function is called, *pp points to the first byte following a
 117427 ** varint that is part of a doclist (or position-list, or any other list
 117428 ** of varints). This function moves *pp to point to the start of that varint,
 117429 ** and sets *pVal by the varint value.
 117431 ** Argument pStart points to the first byte of the doclist that the
 117432 ** varint is part of.
 117434 static void fts3GetReverseVarint(
 117435   char **pp, 
 117436   char *pStart, 
 117437   sqlite3_int64 *pVal
 117439   sqlite3_int64 iVal;
 117440   char *p;
 117442   /* Pointer p now points at the first byte past the varint we are 
 117443   ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
 117444   ** clear on character p[-1]. */
 117445   for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
 117446   p++;
 117447   *pp = p;
 117449   sqlite3Fts3GetVarint(p, &iVal);
 117450   *pVal = iVal;
 117454 ** The xDisconnect() virtual table method.
 117456 static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
 117457   Fts3Table *p = (Fts3Table *)pVtab;
 117458   int i;
 117460   assert( p->nPendingData==0 );
 117461   assert( p->pSegments==0 );
 117463   /* Free any prepared statements held */
 117464   for(i=0; i<SizeofArray(p->aStmt); i++){
 117465     sqlite3_finalize(p->aStmt[i]);
 117467   sqlite3_free(p->zSegmentsTbl);
 117468   sqlite3_free(p->zReadExprlist);
 117469   sqlite3_free(p->zWriteExprlist);
 117470   sqlite3_free(p->zContentTbl);
 117471   sqlite3_free(p->zLanguageid);
 117473   /* Invoke the tokenizer destructor to free the tokenizer. */
 117474   p->pTokenizer->pModule->xDestroy(p->pTokenizer);
 117476   sqlite3_free(p);
 117477   return SQLITE_OK;
 117481 ** Construct one or more SQL statements from the format string given
 117482 ** and then evaluate those statements. The success code is written
 117483 ** into *pRc.
 117485 ** If *pRc is initially non-zero then this routine is a no-op.
 117487 static void fts3DbExec(
 117488   int *pRc,              /* Success code */
 117489   sqlite3 *db,           /* Database in which to run SQL */
 117490   const char *zFormat,   /* Format string for SQL */
 117491   ...                    /* Arguments to the format string */
 117493   va_list ap;
 117494   char *zSql;
 117495   if( *pRc ) return;
 117496   va_start(ap, zFormat);
 117497   zSql = sqlite3_vmprintf(zFormat, ap);
 117498   va_end(ap);
 117499   if( zSql==0 ){
 117500     *pRc = SQLITE_NOMEM;
 117501   }else{
 117502     *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
 117503     sqlite3_free(zSql);
 117508 ** The xDestroy() virtual table method.
 117510 static int fts3DestroyMethod(sqlite3_vtab *pVtab){
 117511   Fts3Table *p = (Fts3Table *)pVtab;
 117512   int rc = SQLITE_OK;              /* Return code */
 117513   const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
 117514   sqlite3 *db = p->db;             /* Database handle */
 117516   /* Drop the shadow tables */
 117517   if( p->zContentTbl==0 ){
 117518     fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
 117520   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
 117521   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
 117522   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
 117523   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
 117525   /* If everything has worked, invoke fts3DisconnectMethod() to free the
 117526   ** memory associated with the Fts3Table structure and return SQLITE_OK.
 117527   ** Otherwise, return an SQLite error code.
 117529   return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
 117534 ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
 117535 ** passed as the first argument. This is done as part of the xConnect()
 117536 ** and xCreate() methods.
 117538 ** If *pRc is non-zero when this function is called, it is a no-op. 
 117539 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
 117540 ** before returning.
 117542 static void fts3DeclareVtab(int *pRc, Fts3Table *p){
 117543   if( *pRc==SQLITE_OK ){
 117544     int i;                        /* Iterator variable */
 117545     int rc;                       /* Return code */
 117546     char *zSql;                   /* SQL statement passed to declare_vtab() */
 117547     char *zCols;                  /* List of user defined columns */
 117548     const char *zLanguageid;
 117550     zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
 117551     sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
 117553     /* Create a list of user columns for the virtual table */
 117554     zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
 117555     for(i=1; zCols && i<p->nColumn; i++){
 117556       zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
 117559     /* Create the whole "CREATE TABLE" statement to pass to SQLite */
 117560     zSql = sqlite3_mprintf(
 117561         "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)", 
 117562         zCols, p->zName, zLanguageid
 117564     if( !zCols || !zSql ){
 117565       rc = SQLITE_NOMEM;
 117566     }else{
 117567       rc = sqlite3_declare_vtab(p->db, zSql);
 117570     sqlite3_free(zSql);
 117571     sqlite3_free(zCols);
 117572     *pRc = rc;
 117577 ** Create the %_stat table if it does not already exist.
 117579 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
 117580   fts3DbExec(pRc, p->db, 
 117581       "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
 117582           "(id INTEGER PRIMARY KEY, value BLOB);",
 117583       p->zDb, p->zName
 117585   if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
 117589 ** Create the backing store tables (%_content, %_segments and %_segdir)
 117590 ** required by the FTS3 table passed as the only argument. This is done
 117591 ** as part of the vtab xCreate() method.
 117593 ** If the p->bHasDocsize boolean is true (indicating that this is an
 117594 ** FTS4 table, not an FTS3 table) then also create the %_docsize and
 117595 ** %_stat tables required by FTS4.
 117597 static int fts3CreateTables(Fts3Table *p){
 117598   int rc = SQLITE_OK;             /* Return code */
 117599   int i;                          /* Iterator variable */
 117600   sqlite3 *db = p->db;            /* The database connection */
 117602   if( p->zContentTbl==0 ){
 117603     const char *zLanguageid = p->zLanguageid;
 117604     char *zContentCols;           /* Columns of %_content table */
 117606     /* Create a list of user columns for the content table */
 117607     zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
 117608     for(i=0; zContentCols && i<p->nColumn; i++){
 117609       char *z = p->azColumn[i];
 117610       zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
 117612     if( zLanguageid && zContentCols ){
 117613       zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
 117615     if( zContentCols==0 ) rc = SQLITE_NOMEM;
 117617     /* Create the content table */
 117618     fts3DbExec(&rc, db, 
 117619        "CREATE TABLE %Q.'%q_content'(%s)",
 117620        p->zDb, p->zName, zContentCols
 117622     sqlite3_free(zContentCols);
 117625   /* Create other tables */
 117626   fts3DbExec(&rc, db, 
 117627       "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
 117628       p->zDb, p->zName
 117630   fts3DbExec(&rc, db, 
 117631       "CREATE TABLE %Q.'%q_segdir'("
 117632         "level INTEGER,"
 117633         "idx INTEGER,"
 117634         "start_block INTEGER,"
 117635         "leaves_end_block INTEGER,"
 117636         "end_block INTEGER,"
 117637         "root BLOB,"
 117638         "PRIMARY KEY(level, idx)"
 117639       ");",
 117640       p->zDb, p->zName
 117642   if( p->bHasDocsize ){
 117643     fts3DbExec(&rc, db, 
 117644         "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
 117645         p->zDb, p->zName
 117648   assert( p->bHasStat==p->bFts4 );
 117649   if( p->bHasStat ){
 117650     sqlite3Fts3CreateStatTable(&rc, p);
 117652   return rc;
 117656 ** Store the current database page-size in bytes in p->nPgsz.
 117658 ** If *pRc is non-zero when this function is called, it is a no-op. 
 117659 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
 117660 ** before returning.
 117662 static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
 117663   if( *pRc==SQLITE_OK ){
 117664     int rc;                       /* Return code */
 117665     char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
 117666     sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
 117668     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
 117669     if( !zSql ){
 117670       rc = SQLITE_NOMEM;
 117671     }else{
 117672       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
 117673       if( rc==SQLITE_OK ){
 117674         sqlite3_step(pStmt);
 117675         p->nPgsz = sqlite3_column_int(pStmt, 0);
 117676         rc = sqlite3_finalize(pStmt);
 117677       }else if( rc==SQLITE_AUTH ){
 117678         p->nPgsz = 1024;
 117679         rc = SQLITE_OK;
 117682     assert( p->nPgsz>0 || rc!=SQLITE_OK );
 117683     sqlite3_free(zSql);
 117684     *pRc = rc;
 117689 ** "Special" FTS4 arguments are column specifications of the following form:
 117691 **   <key> = <value>
 117693 ** There may not be whitespace surrounding the "=" character. The <value> 
 117694 ** term may be quoted, but the <key> may not.
 117696 static int fts3IsSpecialColumn(
 117697   const char *z, 
 117698   int *pnKey,
 117699   char **pzValue
 117701   char *zValue;
 117702   const char *zCsr = z;
 117704   while( *zCsr!='=' ){
 117705     if( *zCsr=='\0' ) return 0;
 117706     zCsr++;
 117709   *pnKey = (int)(zCsr-z);
 117710   zValue = sqlite3_mprintf("%s", &zCsr[1]);
 117711   if( zValue ){
 117712     sqlite3Fts3Dequote(zValue);
 117714   *pzValue = zValue;
 117715   return 1;
 117719 ** Append the output of a printf() style formatting to an existing string.
 117721 static void fts3Appendf(
 117722   int *pRc,                       /* IN/OUT: Error code */
 117723   char **pz,                      /* IN/OUT: Pointer to string buffer */
 117724   const char *zFormat,            /* Printf format string to append */
 117725   ...                             /* Arguments for printf format string */
 117727   if( *pRc==SQLITE_OK ){
 117728     va_list ap;
 117729     char *z;
 117730     va_start(ap, zFormat);
 117731     z = sqlite3_vmprintf(zFormat, ap);
 117732     va_end(ap);
 117733     if( z && *pz ){
 117734       char *z2 = sqlite3_mprintf("%s%s", *pz, z);
 117735       sqlite3_free(z);
 117736       z = z2;
 117738     if( z==0 ) *pRc = SQLITE_NOMEM;
 117739     sqlite3_free(*pz);
 117740     *pz = z;
 117745 ** Return a copy of input string zInput enclosed in double-quotes (") and
 117746 ** with all double quote characters escaped. For example:
 117748 **     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
 117750 ** The pointer returned points to memory obtained from sqlite3_malloc(). It
 117751 ** is the callers responsibility to call sqlite3_free() to release this
 117752 ** memory.
 117754 static char *fts3QuoteId(char const *zInput){
 117755   int nRet;
 117756   char *zRet;
 117757   nRet = 2 + (int)strlen(zInput)*2 + 1;
 117758   zRet = sqlite3_malloc(nRet);
 117759   if( zRet ){
 117760     int i;
 117761     char *z = zRet;
 117762     *(z++) = '"';
 117763     for(i=0; zInput[i]; i++){
 117764       if( zInput[i]=='"' ) *(z++) = '"';
 117765       *(z++) = zInput[i];
 117767     *(z++) = '"';
 117768     *(z++) = '\0';
 117770   return zRet;
 117774 ** Return a list of comma separated SQL expressions and a FROM clause that 
 117775 ** could be used in a SELECT statement such as the following:
 117777 **     SELECT <list of expressions> FROM %_content AS x ...
 117779 ** to return the docid, followed by each column of text data in order
 117780 ** from left to write. If parameter zFunc is not NULL, then instead of
 117781 ** being returned directly each column of text data is passed to an SQL
 117782 ** function named zFunc first. For example, if zFunc is "unzip" and the
 117783 ** table has the three user-defined columns "a", "b", and "c", the following
 117784 ** string is returned:
 117786 **     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
 117788 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
 117789 ** is the responsibility of the caller to eventually free it.
 117791 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
 117792 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
 117793 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
 117794 ** no error occurs, *pRc is left unmodified.
 117796 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
 117797   char *zRet = 0;
 117798   char *zFree = 0;
 117799   char *zFunction;
 117800   int i;
 117802   if( p->zContentTbl==0 ){
 117803     if( !zFunc ){
 117804       zFunction = "";
 117805     }else{
 117806       zFree = zFunction = fts3QuoteId(zFunc);
 117808     fts3Appendf(pRc, &zRet, "docid");
 117809     for(i=0; i<p->nColumn; i++){
 117810       fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
 117812     if( p->zLanguageid ){
 117813       fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
 117815     sqlite3_free(zFree);
 117816   }else{
 117817     fts3Appendf(pRc, &zRet, "rowid");
 117818     for(i=0; i<p->nColumn; i++){
 117819       fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
 117821     if( p->zLanguageid ){
 117822       fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
 117825   fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x", 
 117826       p->zDb,
 117827       (p->zContentTbl ? p->zContentTbl : p->zName),
 117828       (p->zContentTbl ? "" : "_content")
 117830   return zRet;
 117834 ** Return a list of N comma separated question marks, where N is the number
 117835 ** of columns in the %_content table (one for the docid plus one for each
 117836 ** user-defined text column).
 117838 ** If argument zFunc is not NULL, then all but the first question mark
 117839 ** is preceded by zFunc and an open bracket, and followed by a closed
 117840 ** bracket. For example, if zFunc is "zip" and the FTS3 table has three 
 117841 ** user-defined text columns, the following string is returned:
 117843 **     "?, zip(?), zip(?), zip(?)"
 117845 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
 117846 ** is the responsibility of the caller to eventually free it.
 117848 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
 117849 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
 117850 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
 117851 ** no error occurs, *pRc is left unmodified.
 117853 static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
 117854   char *zRet = 0;
 117855   char *zFree = 0;
 117856   char *zFunction;
 117857   int i;
 117859   if( !zFunc ){
 117860     zFunction = "";
 117861   }else{
 117862     zFree = zFunction = fts3QuoteId(zFunc);
 117864   fts3Appendf(pRc, &zRet, "?");
 117865   for(i=0; i<p->nColumn; i++){
 117866     fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
 117868   if( p->zLanguageid ){
 117869     fts3Appendf(pRc, &zRet, ", ?");
 117871   sqlite3_free(zFree);
 117872   return zRet;
 117876 ** This function interprets the string at (*pp) as a non-negative integer
 117877 ** value. It reads the integer and sets *pnOut to the value read, then 
 117878 ** sets *pp to point to the byte immediately following the last byte of
 117879 ** the integer value.
 117881 ** Only decimal digits ('0'..'9') may be part of an integer value. 
 117883 ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
 117884 ** the output value undefined. Otherwise SQLITE_OK is returned.
 117886 ** This function is used when parsing the "prefix=" FTS4 parameter.
 117888 static int fts3GobbleInt(const char **pp, int *pnOut){
 117889   const char *p;                  /* Iterator pointer */
 117890   int nInt = 0;                   /* Output value */
 117892   for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
 117893     nInt = nInt * 10 + (p[0] - '0');
 117895   if( p==*pp ) return SQLITE_ERROR;
 117896   *pnOut = nInt;
 117897   *pp = p;
 117898   return SQLITE_OK;
 117902 ** This function is called to allocate an array of Fts3Index structures
 117903 ** representing the indexes maintained by the current FTS table. FTS tables
 117904 ** always maintain the main "terms" index, but may also maintain one or
 117905 ** more "prefix" indexes, depending on the value of the "prefix=" parameter
 117906 ** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
 117908 ** Argument zParam is passed the value of the "prefix=" option if one was
 117909 ** specified, or NULL otherwise.
 117911 ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
 117912 ** the allocated array. *pnIndex is set to the number of elements in the
 117913 ** array. If an error does occur, an SQLite error code is returned.
 117915 ** Regardless of whether or not an error is returned, it is the responsibility
 117916 ** of the caller to call sqlite3_free() on the output array to free it.
 117918 static int fts3PrefixParameter(
 117919   const char *zParam,             /* ABC in prefix=ABC parameter to parse */
 117920   int *pnIndex,                   /* OUT: size of *apIndex[] array */
 117921   struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
 117923   struct Fts3Index *aIndex;       /* Allocated array */
 117924   int nIndex = 1;                 /* Number of entries in array */
 117926   if( zParam && zParam[0] ){
 117927     const char *p;
 117928     nIndex++;
 117929     for(p=zParam; *p; p++){
 117930       if( *p==',' ) nIndex++;
 117934   aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
 117935   *apIndex = aIndex;
 117936   *pnIndex = nIndex;
 117937   if( !aIndex ){
 117938     return SQLITE_NOMEM;
 117941   memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
 117942   if( zParam ){
 117943     const char *p = zParam;
 117944     int i;
 117945     for(i=1; i<nIndex; i++){
 117946       int nPrefix;
 117947       if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
 117948       aIndex[i].nPrefix = nPrefix;
 117949       p++;
 117953   return SQLITE_OK;
 117957 ** This function is called when initializing an FTS4 table that uses the
 117958 ** content=xxx option. It determines the number of and names of the columns
 117959 ** of the new FTS4 table.
 117961 ** The third argument passed to this function is the value passed to the
 117962 ** config=xxx option (i.e. "xxx"). This function queries the database for
 117963 ** a table of that name. If found, the output variables are populated
 117964 ** as follows:
 117966 **   *pnCol:   Set to the number of columns table xxx has,
 117968 **   *pnStr:   Set to the total amount of space required to store a copy
 117969 **             of each columns name, including the nul-terminator.
 117971 **   *pazCol:  Set to point to an array of *pnCol strings. Each string is
 117972 **             the name of the corresponding column in table xxx. The array
 117973 **             and its contents are allocated using a single allocation. It
 117974 **             is the responsibility of the caller to free this allocation
 117975 **             by eventually passing the *pazCol value to sqlite3_free().
 117977 ** If the table cannot be found, an error code is returned and the output
 117978 ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
 117979 ** returned (and the output variables are undefined).
 117981 static int fts3ContentColumns(
 117982   sqlite3 *db,                    /* Database handle */
 117983   const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
 117984   const char *zTbl,               /* Name of content table */
 117985   const char ***pazCol,           /* OUT: Malloc'd array of column names */
 117986   int *pnCol,                     /* OUT: Size of array *pazCol */
 117987   int *pnStr                      /* OUT: Bytes of string content */
 117989   int rc = SQLITE_OK;             /* Return code */
 117990   char *zSql;                     /* "SELECT *" statement on zTbl */  
 117991   sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
 117993   zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
 117994   if( !zSql ){
 117995     rc = SQLITE_NOMEM;
 117996   }else{
 117997     rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 117999   sqlite3_free(zSql);
 118001   if( rc==SQLITE_OK ){
 118002     const char **azCol;           /* Output array */
 118003     int nStr = 0;                 /* Size of all column names (incl. 0x00) */
 118004     int nCol;                     /* Number of table columns */
 118005     int i;                        /* Used to iterate through columns */
 118007     /* Loop through the returned columns. Set nStr to the number of bytes of
 118008     ** space required to store a copy of each column name, including the
 118009     ** nul-terminator byte.  */
 118010     nCol = sqlite3_column_count(pStmt);
 118011     for(i=0; i<nCol; i++){
 118012       const char *zCol = sqlite3_column_name(pStmt, i);
 118013       nStr += (int)strlen(zCol) + 1;
 118016     /* Allocate and populate the array to return. */
 118017     azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
 118018     if( azCol==0 ){
 118019       rc = SQLITE_NOMEM;
 118020     }else{
 118021       char *p = (char *)&azCol[nCol];
 118022       for(i=0; i<nCol; i++){
 118023         const char *zCol = sqlite3_column_name(pStmt, i);
 118024         int n = (int)strlen(zCol)+1;
 118025         memcpy(p, zCol, n);
 118026         azCol[i] = p;
 118027         p += n;
 118030     sqlite3_finalize(pStmt);
 118032     /* Set the output variables. */
 118033     *pnCol = nCol;
 118034     *pnStr = nStr;
 118035     *pazCol = azCol;
 118038   return rc;
 118042 ** This function is the implementation of both the xConnect and xCreate
 118043 ** methods of the FTS3 virtual table.
 118045 ** The argv[] array contains the following:
 118047 **   argv[0]   -> module name  ("fts3" or "fts4")
 118048 **   argv[1]   -> database name
 118049 **   argv[2]   -> table name
 118050 **   argv[...] -> "column name" and other module argument fields.
 118052 static int fts3InitVtab(
 118053   int isCreate,                   /* True for xCreate, false for xConnect */
 118054   sqlite3 *db,                    /* The SQLite database connection */
 118055   void *pAux,                     /* Hash table containing tokenizers */
 118056   int argc,                       /* Number of elements in argv array */
 118057   const char * const *argv,       /* xCreate/xConnect argument array */
 118058   sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
 118059   char **pzErr                    /* Write any error message here */
 118061   Fts3Hash *pHash = (Fts3Hash *)pAux;
 118062   Fts3Table *p = 0;               /* Pointer to allocated vtab */
 118063   int rc = SQLITE_OK;             /* Return code */
 118064   int i;                          /* Iterator variable */
 118065   int nByte;                      /* Size of allocation used for *p */
 118066   int iCol;                       /* Column index */
 118067   int nString = 0;                /* Bytes required to hold all column names */
 118068   int nCol = 0;                   /* Number of columns in the FTS table */
 118069   char *zCsr;                     /* Space for holding column names */
 118070   int nDb;                        /* Bytes required to hold database name */
 118071   int nName;                      /* Bytes required to hold table name */
 118072   int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
 118073   const char **aCol;              /* Array of column names */
 118074   sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
 118076   int nIndex;                     /* Size of aIndex[] array */
 118077   struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
 118079   /* The results of parsing supported FTS4 key=value options: */
 118080   int bNoDocsize = 0;             /* True to omit %_docsize table */
 118081   int bDescIdx = 0;               /* True to store descending indexes */
 118082   char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
 118083   char *zCompress = 0;            /* compress=? parameter (or NULL) */
 118084   char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
 118085   char *zContent = 0;             /* content=? parameter (or NULL) */
 118086   char *zLanguageid = 0;          /* languageid=? parameter (or NULL) */
 118088   assert( strlen(argv[0])==4 );
 118089   assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
 118090        || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
 118093   nDb = (int)strlen(argv[1]) + 1;
 118094   nName = (int)strlen(argv[2]) + 1;
 118096   aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
 118097   if( !aCol ) return SQLITE_NOMEM;
 118098   memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
 118100   /* Loop through all of the arguments passed by the user to the FTS3/4
 118101   ** module (i.e. all the column names and special arguments). This loop
 118102   ** does the following:
 118104   **   + Figures out the number of columns the FTSX table will have, and
 118105   **     the number of bytes of space that must be allocated to store copies
 118106   **     of the column names.
 118108   **   + If there is a tokenizer specification included in the arguments,
 118109   **     initializes the tokenizer pTokenizer.
 118111   for(i=3; rc==SQLITE_OK && i<argc; i++){
 118112     char const *z = argv[i];
 118113     int nKey;
 118114     char *zVal;
 118116     /* Check if this is a tokenizer specification */
 118117     if( !pTokenizer 
 118118      && strlen(z)>8
 118119      && 0==sqlite3_strnicmp(z, "tokenize", 8) 
 118120      && 0==sqlite3Fts3IsIdChar(z[8])
 118122       rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
 118125     /* Check if it is an FTS4 special argument. */
 118126     else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
 118127       struct Fts4Option {
 118128         const char *zOpt;
 118129         int nOpt;
 118130       } aFts4Opt[] = {
 118131         { "matchinfo",   9 },     /* 0 -> MATCHINFO */
 118132         { "prefix",      6 },     /* 1 -> PREFIX */
 118133         { "compress",    8 },     /* 2 -> COMPRESS */
 118134         { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
 118135         { "order",       5 },     /* 4 -> ORDER */
 118136         { "content",     7 },     /* 5 -> CONTENT */
 118137         { "languageid", 10 }      /* 6 -> LANGUAGEID */
 118140       int iOpt;
 118141       if( !zVal ){
 118142         rc = SQLITE_NOMEM;
 118143       }else{
 118144         for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
 118145           struct Fts4Option *pOp = &aFts4Opt[iOpt];
 118146           if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
 118147             break;
 118150         if( iOpt==SizeofArray(aFts4Opt) ){
 118151           *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
 118152           rc = SQLITE_ERROR;
 118153         }else{
 118154           switch( iOpt ){
 118155             case 0:               /* MATCHINFO */
 118156               if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
 118157                 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
 118158                 rc = SQLITE_ERROR;
 118160               bNoDocsize = 1;
 118161               break;
 118163             case 1:               /* PREFIX */
 118164               sqlite3_free(zPrefix);
 118165               zPrefix = zVal;
 118166               zVal = 0;
 118167               break;
 118169             case 2:               /* COMPRESS */
 118170               sqlite3_free(zCompress);
 118171               zCompress = zVal;
 118172               zVal = 0;
 118173               break;
 118175             case 3:               /* UNCOMPRESS */
 118176               sqlite3_free(zUncompress);
 118177               zUncompress = zVal;
 118178               zVal = 0;
 118179               break;
 118181             case 4:               /* ORDER */
 118182               if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
 118183                && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
 118185                 *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
 118186                 rc = SQLITE_ERROR;
 118188               bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
 118189               break;
 118191             case 5:              /* CONTENT */
 118192               sqlite3_free(zContent);
 118193               zContent = zVal;
 118194               zVal = 0;
 118195               break;
 118197             case 6:              /* LANGUAGEID */
 118198               assert( iOpt==6 );
 118199               sqlite3_free(zLanguageid);
 118200               zLanguageid = zVal;
 118201               zVal = 0;
 118202               break;
 118205         sqlite3_free(zVal);
 118209     /* Otherwise, the argument is a column name. */
 118210     else {
 118211       nString += (int)(strlen(z) + 1);
 118212       aCol[nCol++] = z;
 118216   /* If a content=xxx option was specified, the following:
 118218   **   1. Ignore any compress= and uncompress= options.
 118220   **   2. If no column names were specified as part of the CREATE VIRTUAL
 118221   **      TABLE statement, use all columns from the content table.
 118223   if( rc==SQLITE_OK && zContent ){
 118224     sqlite3_free(zCompress); 
 118225     sqlite3_free(zUncompress); 
 118226     zCompress = 0;
 118227     zUncompress = 0;
 118228     if( nCol==0 ){
 118229       sqlite3_free((void*)aCol); 
 118230       aCol = 0;
 118231       rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
 118233       /* If a languageid= option was specified, remove the language id
 118234       ** column from the aCol[] array. */ 
 118235       if( rc==SQLITE_OK && zLanguageid ){
 118236         int j;
 118237         for(j=0; j<nCol; j++){
 118238           if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
 118239             int k;
 118240             for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
 118241             nCol--;
 118242             break;
 118248   if( rc!=SQLITE_OK ) goto fts3_init_out;
 118250   if( nCol==0 ){
 118251     assert( nString==0 );
 118252     aCol[0] = "content";
 118253     nString = 8;
 118254     nCol = 1;
 118257   if( pTokenizer==0 ){
 118258     rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
 118259     if( rc!=SQLITE_OK ) goto fts3_init_out;
 118261   assert( pTokenizer );
 118263   rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
 118264   if( rc==SQLITE_ERROR ){
 118265     assert( zPrefix );
 118266     *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
 118268   if( rc!=SQLITE_OK ) goto fts3_init_out;
 118270   /* Allocate and populate the Fts3Table structure. */
 118271   nByte = sizeof(Fts3Table) +                  /* Fts3Table */
 118272           nCol * sizeof(char *) +              /* azColumn */
 118273           nIndex * sizeof(struct Fts3Index) +  /* aIndex */
 118274           nName +                              /* zName */
 118275           nDb +                                /* zDb */
 118276           nString;                             /* Space for azColumn strings */
 118277   p = (Fts3Table*)sqlite3_malloc(nByte);
 118278   if( p==0 ){
 118279     rc = SQLITE_NOMEM;
 118280     goto fts3_init_out;
 118282   memset(p, 0, nByte);
 118283   p->db = db;
 118284   p->nColumn = nCol;
 118285   p->nPendingData = 0;
 118286   p->azColumn = (char **)&p[1];
 118287   p->pTokenizer = pTokenizer;
 118288   p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
 118289   p->bHasDocsize = (isFts4 && bNoDocsize==0);
 118290   p->bHasStat = isFts4;
 118291   p->bFts4 = isFts4;
 118292   p->bDescIdx = bDescIdx;
 118293   p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
 118294   p->zContentTbl = zContent;
 118295   p->zLanguageid = zLanguageid;
 118296   zContent = 0;
 118297   zLanguageid = 0;
 118298   TESTONLY( p->inTransaction = -1 );
 118299   TESTONLY( p->mxSavepoint = -1 );
 118301   p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
 118302   memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
 118303   p->nIndex = nIndex;
 118304   for(i=0; i<nIndex; i++){
 118305     fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
 118308   /* Fill in the zName and zDb fields of the vtab structure. */
 118309   zCsr = (char *)&p->aIndex[nIndex];
 118310   p->zName = zCsr;
 118311   memcpy(zCsr, argv[2], nName);
 118312   zCsr += nName;
 118313   p->zDb = zCsr;
 118314   memcpy(zCsr, argv[1], nDb);
 118315   zCsr += nDb;
 118317   /* Fill in the azColumn array */
 118318   for(iCol=0; iCol<nCol; iCol++){
 118319     char *z; 
 118320     int n = 0;
 118321     z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
 118322     memcpy(zCsr, z, n);
 118323     zCsr[n] = '\0';
 118324     sqlite3Fts3Dequote(zCsr);
 118325     p->azColumn[iCol] = zCsr;
 118326     zCsr += n+1;
 118327     assert( zCsr <= &((char *)p)[nByte] );
 118330   if( (zCompress==0)!=(zUncompress==0) ){
 118331     char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
 118332     rc = SQLITE_ERROR;
 118333     *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
 118335   p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
 118336   p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
 118337   if( rc!=SQLITE_OK ) goto fts3_init_out;
 118339   /* If this is an xCreate call, create the underlying tables in the 
 118340   ** database. TODO: For xConnect(), it could verify that said tables exist.
 118342   if( isCreate ){
 118343     rc = fts3CreateTables(p);
 118346   /* Check to see if a legacy fts3 table has been "upgraded" by the
 118347   ** addition of a %_stat table so that it can use incremental merge.
 118349   if( !isFts4 && !isCreate ){
 118350     int rc2 = SQLITE_OK;
 118351     fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
 118352                p->zDb, p->zName);
 118353     if( rc2==SQLITE_OK ) p->bHasStat = 1;
 118356   /* Figure out the page-size for the database. This is required in order to
 118357   ** estimate the cost of loading large doclists from the database.  */
 118358   fts3DatabasePageSize(&rc, p);
 118359   p->nNodeSize = p->nPgsz-35;
 118361   /* Declare the table schema to SQLite. */
 118362   fts3DeclareVtab(&rc, p);
 118364 fts3_init_out:
 118365   sqlite3_free(zPrefix);
 118366   sqlite3_free(aIndex);
 118367   sqlite3_free(zCompress);
 118368   sqlite3_free(zUncompress);
 118369   sqlite3_free(zContent);
 118370   sqlite3_free(zLanguageid);
 118371   sqlite3_free((void *)aCol);
 118372   if( rc!=SQLITE_OK ){
 118373     if( p ){
 118374       fts3DisconnectMethod((sqlite3_vtab *)p);
 118375     }else if( pTokenizer ){
 118376       pTokenizer->pModule->xDestroy(pTokenizer);
 118378   }else{
 118379     assert( p->pSegments==0 );
 118380     *ppVTab = &p->base;
 118382   return rc;
 118386 ** The xConnect() and xCreate() methods for the virtual table. All the
 118387 ** work is done in function fts3InitVtab().
 118389 static int fts3ConnectMethod(
 118390   sqlite3 *db,                    /* Database connection */
 118391   void *pAux,                     /* Pointer to tokenizer hash table */
 118392   int argc,                       /* Number of elements in argv array */
 118393   const char * const *argv,       /* xCreate/xConnect argument array */
 118394   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
 118395   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
 118397   return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
 118399 static int fts3CreateMethod(
 118400   sqlite3 *db,                    /* Database connection */
 118401   void *pAux,                     /* Pointer to tokenizer hash table */
 118402   int argc,                       /* Number of elements in argv array */
 118403   const char * const *argv,       /* xCreate/xConnect argument array */
 118404   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
 118405   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
 118407   return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
 118411 ** Implementation of the xBestIndex method for FTS3 tables. There
 118412 ** are three possible strategies, in order of preference:
 118414 **   1. Direct lookup by rowid or docid. 
 118415 **   2. Full-text search using a MATCH operator on a non-docid column.
 118416 **   3. Linear scan of %_content table.
 118418 static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
 118419   Fts3Table *p = (Fts3Table *)pVTab;
 118420   int i;                          /* Iterator variable */
 118421   int iCons = -1;                 /* Index of constraint to use */
 118422   int iLangidCons = -1;           /* Index of langid=x constraint, if present */
 118424   /* By default use a full table scan. This is an expensive option,
 118425   ** so search through the constraints to see if a more efficient 
 118426   ** strategy is possible.
 118428   pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
 118429   pInfo->estimatedCost = 500000;
 118430   for(i=0; i<pInfo->nConstraint; i++){
 118431     struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
 118432     if( pCons->usable==0 ) continue;
 118434     /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
 118435     if( iCons<0 
 118436      && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
 118437      && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
 118439       pInfo->idxNum = FTS3_DOCID_SEARCH;
 118440       pInfo->estimatedCost = 1.0;
 118441       iCons = i;
 118444     /* A MATCH constraint. Use a full-text search.
 118446     ** If there is more than one MATCH constraint available, use the first
 118447     ** one encountered. If there is both a MATCH constraint and a direct
 118448     ** rowid/docid lookup, prefer the MATCH strategy. This is done even 
 118449     ** though the rowid/docid lookup is faster than a MATCH query, selecting
 118450     ** it would lead to an "unable to use function MATCH in the requested 
 118451     ** context" error.
 118453     if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH 
 118454      && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
 118456       pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
 118457       pInfo->estimatedCost = 2.0;
 118458       iCons = i;
 118461     /* Equality constraint on the langid column */
 118462     if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
 118463      && pCons->iColumn==p->nColumn + 2
 118465       iLangidCons = i;
 118469   if( iCons>=0 ){
 118470     pInfo->aConstraintUsage[iCons].argvIndex = 1;
 118471     pInfo->aConstraintUsage[iCons].omit = 1;
 118473   if( iLangidCons>=0 ){
 118474     pInfo->aConstraintUsage[iLangidCons].argvIndex = 2;
 118477   /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
 118478   ** docid) order. Both ascending and descending are possible. 
 118480   if( pInfo->nOrderBy==1 ){
 118481     struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
 118482     if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
 118483       if( pOrder->desc ){
 118484         pInfo->idxStr = "DESC";
 118485       }else{
 118486         pInfo->idxStr = "ASC";
 118488       pInfo->orderByConsumed = 1;
 118492   assert( p->pSegments==0 );
 118493   return SQLITE_OK;
 118497 ** Implementation of xOpen method.
 118499 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
 118500   sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
 118502   UNUSED_PARAMETER(pVTab);
 118504   /* Allocate a buffer large enough for an Fts3Cursor structure. If the
 118505   ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, 
 118506   ** if the allocation fails, return SQLITE_NOMEM.
 118508   *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
 118509   if( !pCsr ){
 118510     return SQLITE_NOMEM;
 118512   memset(pCsr, 0, sizeof(Fts3Cursor));
 118513   return SQLITE_OK;
 118517 ** Close the cursor.  For additional information see the documentation
 118518 ** on the xClose method of the virtual table interface.
 118520 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
 118521   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
 118522   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 118523   sqlite3_finalize(pCsr->pStmt);
 118524   sqlite3Fts3ExprFree(pCsr->pExpr);
 118525   sqlite3Fts3FreeDeferredTokens(pCsr);
 118526   sqlite3_free(pCsr->aDoclist);
 118527   sqlite3_free(pCsr->aMatchinfo);
 118528   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 118529   sqlite3_free(pCsr);
 118530   return SQLITE_OK;
 118534 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
 118535 ** compose and prepare an SQL statement of the form:
 118537 **    "SELECT <columns> FROM %_content WHERE rowid = ?"
 118539 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
 118540 ** it. If an error occurs, return an SQLite error code.
 118542 ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
 118544 static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
 118545   int rc = SQLITE_OK;
 118546   if( pCsr->pStmt==0 ){
 118547     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
 118548     char *zSql;
 118549     zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
 118550     if( !zSql ) return SQLITE_NOMEM;
 118551     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
 118552     sqlite3_free(zSql);
 118554   *ppStmt = pCsr->pStmt;
 118555   return rc;
 118559 ** Position the pCsr->pStmt statement so that it is on the row
 118560 ** of the %_content table that contains the last match.  Return
 118561 ** SQLITE_OK on success.  
 118563 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
 118564   int rc = SQLITE_OK;
 118565   if( pCsr->isRequireSeek ){
 118566     sqlite3_stmt *pStmt = 0;
 118568     rc = fts3CursorSeekStmt(pCsr, &pStmt);
 118569     if( rc==SQLITE_OK ){
 118570       sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
 118571       pCsr->isRequireSeek = 0;
 118572       if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
 118573         return SQLITE_OK;
 118574       }else{
 118575         rc = sqlite3_reset(pCsr->pStmt);
 118576         if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
 118577           /* If no row was found and no error has occured, then the %_content
 118578           ** table is missing a row that is present in the full-text index.
 118579           ** The data structures are corrupt.  */
 118580           rc = FTS_CORRUPT_VTAB;
 118581           pCsr->isEof = 1;
 118587   if( rc!=SQLITE_OK && pContext ){
 118588     sqlite3_result_error_code(pContext, rc);
 118590   return rc;
 118594 ** This function is used to process a single interior node when searching
 118595 ** a b-tree for a term or term prefix. The node data is passed to this 
 118596 ** function via the zNode/nNode parameters. The term to search for is
 118597 ** passed in zTerm/nTerm.
 118599 ** If piFirst is not NULL, then this function sets *piFirst to the blockid
 118600 ** of the child node that heads the sub-tree that may contain the term.
 118602 ** If piLast is not NULL, then *piLast is set to the right-most child node
 118603 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
 118604 ** a prefix.
 118606 ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
 118608 static int fts3ScanInteriorNode(
 118609   const char *zTerm,              /* Term to select leaves for */
 118610   int nTerm,                      /* Size of term zTerm in bytes */
 118611   const char *zNode,              /* Buffer containing segment interior node */
 118612   int nNode,                      /* Size of buffer at zNode */
 118613   sqlite3_int64 *piFirst,         /* OUT: Selected child node */
 118614   sqlite3_int64 *piLast           /* OUT: Selected child node */
 118616   int rc = SQLITE_OK;             /* Return code */
 118617   const char *zCsr = zNode;       /* Cursor to iterate through node */
 118618   const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
 118619   char *zBuffer = 0;              /* Buffer to load terms into */
 118620   int nAlloc = 0;                 /* Size of allocated buffer */
 118621   int isFirstTerm = 1;            /* True when processing first term on page */
 118622   sqlite3_int64 iChild;           /* Block id of child node to descend to */
 118624   /* Skip over the 'height' varint that occurs at the start of every 
 118625   ** interior node. Then load the blockid of the left-child of the b-tree
 118626   ** node into variable iChild.  
 118628   ** Even if the data structure on disk is corrupted, this (reading two
 118629   ** varints from the buffer) does not risk an overread. If zNode is a
 118630   ** root node, then the buffer comes from a SELECT statement. SQLite does
 118631   ** not make this guarantee explicitly, but in practice there are always
 118632   ** either more than 20 bytes of allocated space following the nNode bytes of
 118633   ** contents, or two zero bytes. Or, if the node is read from the %_segments
 118634   ** table, then there are always 20 bytes of zeroed padding following the
 118635   ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
 118637   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
 118638   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
 118639   if( zCsr>zEnd ){
 118640     return FTS_CORRUPT_VTAB;
 118643   while( zCsr<zEnd && (piFirst || piLast) ){
 118644     int cmp;                      /* memcmp() result */
 118645     int nSuffix;                  /* Size of term suffix */
 118646     int nPrefix = 0;              /* Size of term prefix */
 118647     int nBuffer;                  /* Total term size */
 118649     /* Load the next term on the node into zBuffer. Use realloc() to expand
 118650     ** the size of zBuffer if required.  */
 118651     if( !isFirstTerm ){
 118652       zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
 118654     isFirstTerm = 0;
 118655     zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
 118657     if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
 118658       rc = FTS_CORRUPT_VTAB;
 118659       goto finish_scan;
 118661     if( nPrefix+nSuffix>nAlloc ){
 118662       char *zNew;
 118663       nAlloc = (nPrefix+nSuffix) * 2;
 118664       zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
 118665       if( !zNew ){
 118666         rc = SQLITE_NOMEM;
 118667         goto finish_scan;
 118669       zBuffer = zNew;
 118671     assert( zBuffer );
 118672     memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
 118673     nBuffer = nPrefix + nSuffix;
 118674     zCsr += nSuffix;
 118676     /* Compare the term we are searching for with the term just loaded from
 118677     ** the interior node. If the specified term is greater than or equal
 118678     ** to the term from the interior node, then all terms on the sub-tree 
 118679     ** headed by node iChild are smaller than zTerm. No need to search 
 118680     ** iChild.
 118682     ** If the interior node term is larger than the specified term, then
 118683     ** the tree headed by iChild may contain the specified term.
 118685     cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
 118686     if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
 118687       *piFirst = iChild;
 118688       piFirst = 0;
 118691     if( piLast && cmp<0 ){
 118692       *piLast = iChild;
 118693       piLast = 0;
 118696     iChild++;
 118699   if( piFirst ) *piFirst = iChild;
 118700   if( piLast ) *piLast = iChild;
 118702  finish_scan:
 118703   sqlite3_free(zBuffer);
 118704   return rc;
 118709 ** The buffer pointed to by argument zNode (size nNode bytes) contains an
 118710 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
 118711 ** contains a term. This function searches the sub-tree headed by the zNode
 118712 ** node for the range of leaf nodes that may contain the specified term
 118713 ** or terms for which the specified term is a prefix.
 118715 ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the 
 118716 ** left-most leaf node in the tree that may contain the specified term.
 118717 ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
 118718 ** right-most leaf node that may contain a term for which the specified
 118719 ** term is a prefix.
 118721 ** It is possible that the range of returned leaf nodes does not contain 
 118722 ** the specified term or any terms for which it is a prefix. However, if the 
 118723 ** segment does contain any such terms, they are stored within the identified
 118724 ** range. Because this function only inspects interior segment nodes (and
 118725 ** never loads leaf nodes into memory), it is not possible to be sure.
 118727 ** If an error occurs, an error code other than SQLITE_OK is returned.
 118729 static int fts3SelectLeaf(
 118730   Fts3Table *p,                   /* Virtual table handle */
 118731   const char *zTerm,              /* Term to select leaves for */
 118732   int nTerm,                      /* Size of term zTerm in bytes */
 118733   const char *zNode,              /* Buffer containing segment interior node */
 118734   int nNode,                      /* Size of buffer at zNode */
 118735   sqlite3_int64 *piLeaf,          /* Selected leaf node */
 118736   sqlite3_int64 *piLeaf2          /* Selected leaf node */
 118738   int rc;                         /* Return code */
 118739   int iHeight;                    /* Height of this node in tree */
 118741   assert( piLeaf || piLeaf2 );
 118743   sqlite3Fts3GetVarint32(zNode, &iHeight);
 118744   rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
 118745   assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
 118747   if( rc==SQLITE_OK && iHeight>1 ){
 118748     char *zBlob = 0;              /* Blob read from %_segments table */
 118749     int nBlob;                    /* Size of zBlob in bytes */
 118751     if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
 118752       rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
 118753       if( rc==SQLITE_OK ){
 118754         rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
 118756       sqlite3_free(zBlob);
 118757       piLeaf = 0;
 118758       zBlob = 0;
 118761     if( rc==SQLITE_OK ){
 118762       rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
 118764     if( rc==SQLITE_OK ){
 118765       rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
 118767     sqlite3_free(zBlob);
 118770   return rc;
 118774 ** This function is used to create delta-encoded serialized lists of FTS3 
 118775 ** varints. Each call to this function appends a single varint to a list.
 118777 static void fts3PutDeltaVarint(
 118778   char **pp,                      /* IN/OUT: Output pointer */
 118779   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
 118780   sqlite3_int64 iVal              /* Write this value to the list */
 118782   assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
 118783   *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
 118784   *piPrev = iVal;
 118788 ** When this function is called, *ppPoslist is assumed to point to the 
 118789 ** start of a position-list. After it returns, *ppPoslist points to the
 118790 ** first byte after the position-list.
 118792 ** A position list is list of positions (delta encoded) and columns for 
 118793 ** a single document record of a doclist.  So, in other words, this
 118794 ** routine advances *ppPoslist so that it points to the next docid in
 118795 ** the doclist, or to the first byte past the end of the doclist.
 118797 ** If pp is not NULL, then the contents of the position list are copied
 118798 ** to *pp. *pp is set to point to the first byte past the last byte copied
 118799 ** before this function returns.
 118801 static void fts3PoslistCopy(char **pp, char **ppPoslist){
 118802   char *pEnd = *ppPoslist;
 118803   char c = 0;
 118805   /* The end of a position list is marked by a zero encoded as an FTS3 
 118806   ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
 118807   ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
 118808   ** of some other, multi-byte, value.
 118810   ** The following while-loop moves pEnd to point to the first byte that is not 
 118811   ** immediately preceded by a byte with the 0x80 bit set. Then increments
 118812   ** pEnd once more so that it points to the byte immediately following the
 118813   ** last byte in the position-list.
 118815   while( *pEnd | c ){
 118816     c = *pEnd++ & 0x80;
 118817     testcase( c!=0 && (*pEnd)==0 );
 118819   pEnd++;  /* Advance past the POS_END terminator byte */
 118821   if( pp ){
 118822     int n = (int)(pEnd - *ppPoslist);
 118823     char *p = *pp;
 118824     memcpy(p, *ppPoslist, n);
 118825     p += n;
 118826     *pp = p;
 118828   *ppPoslist = pEnd;
 118832 ** When this function is called, *ppPoslist is assumed to point to the 
 118833 ** start of a column-list. After it returns, *ppPoslist points to the
 118834 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
 118836 ** A column-list is list of delta-encoded positions for a single column
 118837 ** within a single document within a doclist.
 118839 ** The column-list is terminated either by a POS_COLUMN varint (1) or
 118840 ** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
 118841 ** the POS_COLUMN or POS_END that terminates the column-list.
 118843 ** If pp is not NULL, then the contents of the column-list are copied
 118844 ** to *pp. *pp is set to point to the first byte past the last byte copied
 118845 ** before this function returns.  The POS_COLUMN or POS_END terminator
 118846 ** is not copied into *pp.
 118848 static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
 118849   char *pEnd = *ppPoslist;
 118850   char c = 0;
 118852   /* A column-list is terminated by either a 0x01 or 0x00 byte that is
 118853   ** not part of a multi-byte varint.
 118855   while( 0xFE & (*pEnd | c) ){
 118856     c = *pEnd++ & 0x80;
 118857     testcase( c!=0 && ((*pEnd)&0xfe)==0 );
 118859   if( pp ){
 118860     int n = (int)(pEnd - *ppPoslist);
 118861     char *p = *pp;
 118862     memcpy(p, *ppPoslist, n);
 118863     p += n;
 118864     *pp = p;
 118866   *ppPoslist = pEnd;
 118870 ** Value used to signify the end of an position-list. This is safe because
 118871 ** it is not possible to have a document with 2^31 terms.
 118873 #define POSITION_LIST_END 0x7fffffff
 118876 ** This function is used to help parse position-lists. When this function is
 118877 ** called, *pp may point to the start of the next varint in the position-list
 118878 ** being parsed, or it may point to 1 byte past the end of the position-list
 118879 ** (in which case **pp will be a terminator bytes POS_END (0) or
 118880 ** (1)).
 118882 ** If *pp points past the end of the current position-list, set *pi to 
 118883 ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
 118884 ** increment the current value of *pi by the value read, and set *pp to
 118885 ** point to the next value before returning.
 118887 ** Before calling this routine *pi must be initialized to the value of
 118888 ** the previous position, or zero if we are reading the first position
 118889 ** in the position-list.  Because positions are delta-encoded, the value
 118890 ** of the previous position is needed in order to compute the value of
 118891 ** the next position.
 118893 static void fts3ReadNextPos(
 118894   char **pp,                    /* IN/OUT: Pointer into position-list buffer */
 118895   sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
 118897   if( (**pp)&0xFE ){
 118898     fts3GetDeltaVarint(pp, pi);
 118899     *pi -= 2;
 118900   }else{
 118901     *pi = POSITION_LIST_END;
 118906 ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
 118907 ** the value of iCol encoded as a varint to *pp.   This will start a new
 118908 ** column list.
 118910 ** Set *pp to point to the byte just after the last byte written before 
 118911 ** returning (do not modify it if iCol==0). Return the total number of bytes
 118912 ** written (0 if iCol==0).
 118914 static int fts3PutColNumber(char **pp, int iCol){
 118915   int n = 0;                      /* Number of bytes written */
 118916   if( iCol ){
 118917     char *p = *pp;                /* Output pointer */
 118918     n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
 118919     *p = 0x01;
 118920     *pp = &p[n];
 118922   return n;
 118926 ** Compute the union of two position lists.  The output written
 118927 ** into *pp contains all positions of both *pp1 and *pp2 in sorted
 118928 ** order and with any duplicates removed.  All pointers are
 118929 ** updated appropriately.   The caller is responsible for insuring
 118930 ** that there is enough space in *pp to hold the complete output.
 118932 static void fts3PoslistMerge(
 118933   char **pp,                      /* Output buffer */
 118934   char **pp1,                     /* Left input list */
 118935   char **pp2                      /* Right input list */
 118937   char *p = *pp;
 118938   char *p1 = *pp1;
 118939   char *p2 = *pp2;
 118941   while( *p1 || *p2 ){
 118942     int iCol1;         /* The current column index in pp1 */
 118943     int iCol2;         /* The current column index in pp2 */
 118945     if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
 118946     else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
 118947     else iCol1 = 0;
 118949     if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
 118950     else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
 118951     else iCol2 = 0;
 118953     if( iCol1==iCol2 ){
 118954       sqlite3_int64 i1 = 0;       /* Last position from pp1 */
 118955       sqlite3_int64 i2 = 0;       /* Last position from pp2 */
 118956       sqlite3_int64 iPrev = 0;
 118957       int n = fts3PutColNumber(&p, iCol1);
 118958       p1 += n;
 118959       p2 += n;
 118961       /* At this point, both p1 and p2 point to the start of column-lists
 118962       ** for the same column (the column with index iCol1 and iCol2).
 118963       ** A column-list is a list of non-negative delta-encoded varints, each 
 118964       ** incremented by 2 before being stored. Each list is terminated by a
 118965       ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
 118966       ** and writes the results to buffer p. p is left pointing to the byte
 118967       ** after the list written. No terminator (POS_END or POS_COLUMN) is
 118968       ** written to the output.
 118970       fts3GetDeltaVarint(&p1, &i1);
 118971       fts3GetDeltaVarint(&p2, &i2);
 118972       do {
 118973         fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2); 
 118974         iPrev -= 2;
 118975         if( i1==i2 ){
 118976           fts3ReadNextPos(&p1, &i1);
 118977           fts3ReadNextPos(&p2, &i2);
 118978         }else if( i1<i2 ){
 118979           fts3ReadNextPos(&p1, &i1);
 118980         }else{
 118981           fts3ReadNextPos(&p2, &i2);
 118983       }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
 118984     }else if( iCol1<iCol2 ){
 118985       p1 += fts3PutColNumber(&p, iCol1);
 118986       fts3ColumnlistCopy(&p, &p1);
 118987     }else{
 118988       p2 += fts3PutColNumber(&p, iCol2);
 118989       fts3ColumnlistCopy(&p, &p2);
 118993   *p++ = POS_END;
 118994   *pp = p;
 118995   *pp1 = p1 + 1;
 118996   *pp2 = p2 + 1;
 119000 ** This function is used to merge two position lists into one. When it is
 119001 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
 119002 ** the part of a doclist that follows each document id. For example, if a row
 119003 ** contains:
 119005 **     'a b c'|'x y z'|'a b b a'
 119007 ** Then the position list for this row for token 'b' would consist of:
 119009 **     0x02 0x01 0x02 0x03 0x03 0x00
 119011 ** When this function returns, both *pp1 and *pp2 are left pointing to the
 119012 ** byte following the 0x00 terminator of their respective position lists.
 119014 ** If isSaveLeft is 0, an entry is added to the output position list for 
 119015 ** each position in *pp2 for which there exists one or more positions in
 119016 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
 119017 ** when the *pp1 token appears before the *pp2 token, but not more than nToken
 119018 ** slots before it.
 119020 ** e.g. nToken==1 searches for adjacent positions.
 119022 static int fts3PoslistPhraseMerge(
 119023   char **pp,                      /* IN/OUT: Preallocated output buffer */
 119024   int nToken,                     /* Maximum difference in token positions */
 119025   int isSaveLeft,                 /* Save the left position */
 119026   int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
 119027   char **pp1,                     /* IN/OUT: Left input list */
 119028   char **pp2                      /* IN/OUT: Right input list */
 119030   char *p = *pp;
 119031   char *p1 = *pp1;
 119032   char *p2 = *pp2;
 119033   int iCol1 = 0;
 119034   int iCol2 = 0;
 119036   /* Never set both isSaveLeft and isExact for the same invocation. */
 119037   assert( isSaveLeft==0 || isExact==0 );
 119039   assert( p!=0 && *p1!=0 && *p2!=0 );
 119040   if( *p1==POS_COLUMN ){ 
 119041     p1++;
 119042     p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
 119044   if( *p2==POS_COLUMN ){ 
 119045     p2++;
 119046     p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
 119049   while( 1 ){
 119050     if( iCol1==iCol2 ){
 119051       char *pSave = p;
 119052       sqlite3_int64 iPrev = 0;
 119053       sqlite3_int64 iPos1 = 0;
 119054       sqlite3_int64 iPos2 = 0;
 119056       if( iCol1 ){
 119057         *p++ = POS_COLUMN;
 119058         p += sqlite3Fts3PutVarint(p, iCol1);
 119061       assert( *p1!=POS_END && *p1!=POS_COLUMN );
 119062       assert( *p2!=POS_END && *p2!=POS_COLUMN );
 119063       fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
 119064       fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
 119066       while( 1 ){
 119067         if( iPos2==iPos1+nToken 
 119068          || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) 
 119070           sqlite3_int64 iSave;
 119071           iSave = isSaveLeft ? iPos1 : iPos2;
 119072           fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
 119073           pSave = 0;
 119074           assert( p );
 119076         if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
 119077           if( (*p2&0xFE)==0 ) break;
 119078           fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
 119079         }else{
 119080           if( (*p1&0xFE)==0 ) break;
 119081           fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
 119085       if( pSave ){
 119086         assert( pp && p );
 119087         p = pSave;
 119090       fts3ColumnlistCopy(0, &p1);
 119091       fts3ColumnlistCopy(0, &p2);
 119092       assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
 119093       if( 0==*p1 || 0==*p2 ) break;
 119095       p1++;
 119096       p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
 119097       p2++;
 119098       p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
 119101     /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
 119102     ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
 119103     ** end of the position list, or the 0x01 that precedes the next 
 119104     ** column-number in the position list. 
 119106     else if( iCol1<iCol2 ){
 119107       fts3ColumnlistCopy(0, &p1);
 119108       if( 0==*p1 ) break;
 119109       p1++;
 119110       p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
 119111     }else{
 119112       fts3ColumnlistCopy(0, &p2);
 119113       if( 0==*p2 ) break;
 119114       p2++;
 119115       p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
 119119   fts3PoslistCopy(0, &p2);
 119120   fts3PoslistCopy(0, &p1);
 119121   *pp1 = p1;
 119122   *pp2 = p2;
 119123   if( *pp==p ){
 119124     return 0;
 119126   *p++ = 0x00;
 119127   *pp = p;
 119128   return 1;
 119132 ** Merge two position-lists as required by the NEAR operator. The argument
 119133 ** position lists correspond to the left and right phrases of an expression 
 119134 ** like:
 119136 **     "phrase 1" NEAR "phrase number 2"
 119138 ** Position list *pp1 corresponds to the left-hand side of the NEAR 
 119139 ** expression and *pp2 to the right. As usual, the indexes in the position 
 119140 ** lists are the offsets of the last token in each phrase (tokens "1" and "2" 
 119141 ** in the example above).
 119143 ** The output position list - written to *pp - is a copy of *pp2 with those
 119144 ** entries that are not sufficiently NEAR entries in *pp1 removed.
 119146 static int fts3PoslistNearMerge(
 119147   char **pp,                      /* Output buffer */
 119148   char *aTmp,                     /* Temporary buffer space */
 119149   int nRight,                     /* Maximum difference in token positions */
 119150   int nLeft,                      /* Maximum difference in token positions */
 119151   char **pp1,                     /* IN/OUT: Left input list */
 119152   char **pp2                      /* IN/OUT: Right input list */
 119154   char *p1 = *pp1;
 119155   char *p2 = *pp2;
 119157   char *pTmp1 = aTmp;
 119158   char *pTmp2;
 119159   char *aTmp2;
 119160   int res = 1;
 119162   fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
 119163   aTmp2 = pTmp2 = pTmp1;
 119164   *pp1 = p1;
 119165   *pp2 = p2;
 119166   fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
 119167   if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
 119168     fts3PoslistMerge(pp, &aTmp, &aTmp2);
 119169   }else if( pTmp1!=aTmp ){
 119170     fts3PoslistCopy(pp, &aTmp);
 119171   }else if( pTmp2!=aTmp2 ){
 119172     fts3PoslistCopy(pp, &aTmp2);
 119173   }else{
 119174     res = 0;
 119177   return res;
 119181 ** An instance of this function is used to merge together the (potentially
 119182 ** large number of) doclists for each term that matches a prefix query.
 119183 ** See function fts3TermSelectMerge() for details.
 119185 typedef struct TermSelect TermSelect;
 119186 struct TermSelect {
 119187   char *aaOutput[16];             /* Malloc'd output buffers */
 119188   int anOutput[16];               /* Size each output buffer in bytes */
 119192 ** This function is used to read a single varint from a buffer. Parameter
 119193 ** pEnd points 1 byte past the end of the buffer. When this function is
 119194 ** called, if *pp points to pEnd or greater, then the end of the buffer
 119195 ** has been reached. In this case *pp is set to 0 and the function returns.
 119197 ** If *pp does not point to or past pEnd, then a single varint is read
 119198 ** from *pp. *pp is then set to point 1 byte past the end of the read varint.
 119200 ** If bDescIdx is false, the value read is added to *pVal before returning.
 119201 ** If it is true, the value read is subtracted from *pVal before this 
 119202 ** function returns.
 119204 static void fts3GetDeltaVarint3(
 119205   char **pp,                      /* IN/OUT: Point to read varint from */
 119206   char *pEnd,                     /* End of buffer */
 119207   int bDescIdx,                   /* True if docids are descending */
 119208   sqlite3_int64 *pVal             /* IN/OUT: Integer value */
 119210   if( *pp>=pEnd ){
 119211     *pp = 0;
 119212   }else{
 119213     sqlite3_int64 iVal;
 119214     *pp += sqlite3Fts3GetVarint(*pp, &iVal);
 119215     if( bDescIdx ){
 119216       *pVal -= iVal;
 119217     }else{
 119218       *pVal += iVal;
 119224 ** This function is used to write a single varint to a buffer. The varint
 119225 ** is written to *pp. Before returning, *pp is set to point 1 byte past the
 119226 ** end of the value written.
 119228 ** If *pbFirst is zero when this function is called, the value written to
 119229 ** the buffer is that of parameter iVal. 
 119231 ** If *pbFirst is non-zero when this function is called, then the value 
 119232 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
 119233 ** (if bDescIdx is non-zero).
 119235 ** Before returning, this function always sets *pbFirst to 1 and *piPrev
 119236 ** to the value of parameter iVal.
 119238 static void fts3PutDeltaVarint3(
 119239   char **pp,                      /* IN/OUT: Output pointer */
 119240   int bDescIdx,                   /* True for descending docids */
 119241   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
 119242   int *pbFirst,                   /* IN/OUT: True after first int written */
 119243   sqlite3_int64 iVal              /* Write this value to the list */
 119245   sqlite3_int64 iWrite;
 119246   if( bDescIdx==0 || *pbFirst==0 ){
 119247     iWrite = iVal - *piPrev;
 119248   }else{
 119249     iWrite = *piPrev - iVal;
 119251   assert( *pbFirst || *piPrev==0 );
 119252   assert( *pbFirst==0 || iWrite>0 );
 119253   *pp += sqlite3Fts3PutVarint(*pp, iWrite);
 119254   *piPrev = iVal;
 119255   *pbFirst = 1;
 119260 ** This macro is used by various functions that merge doclists. The two
 119261 ** arguments are 64-bit docid values. If the value of the stack variable
 119262 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2). 
 119263 ** Otherwise, (i2-i1).
 119265 ** Using this makes it easier to write code that can merge doclists that are
 119266 ** sorted in either ascending or descending order.
 119268 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
 119271 ** This function does an "OR" merge of two doclists (output contains all
 119272 ** positions contained in either argument doclist). If the docids in the 
 119273 ** input doclists are sorted in ascending order, parameter bDescDoclist
 119274 ** should be false. If they are sorted in ascending order, it should be
 119275 ** passed a non-zero value.
 119277 ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
 119278 ** containing the output doclist and SQLITE_OK is returned. In this case
 119279 ** *pnOut is set to the number of bytes in the output doclist.
 119281 ** If an error occurs, an SQLite error code is returned. The output values
 119282 ** are undefined in this case.
 119284 static int fts3DoclistOrMerge(
 119285   int bDescDoclist,               /* True if arguments are desc */
 119286   char *a1, int n1,               /* First doclist */
 119287   char *a2, int n2,               /* Second doclist */
 119288   char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
 119290   sqlite3_int64 i1 = 0;
 119291   sqlite3_int64 i2 = 0;
 119292   sqlite3_int64 iPrev = 0;
 119293   char *pEnd1 = &a1[n1];
 119294   char *pEnd2 = &a2[n2];
 119295   char *p1 = a1;
 119296   char *p2 = a2;
 119297   char *p;
 119298   char *aOut;
 119299   int bFirstOut = 0;
 119301   *paOut = 0;
 119302   *pnOut = 0;
 119304   /* Allocate space for the output. Both the input and output doclists
 119305   ** are delta encoded. If they are in ascending order (bDescDoclist==0),
 119306   ** then the first docid in each list is simply encoded as a varint. For
 119307   ** each subsequent docid, the varint stored is the difference between the
 119308   ** current and previous docid (a positive number - since the list is in
 119309   ** ascending order).
 119311   ** The first docid written to the output is therefore encoded using the 
 119312   ** same number of bytes as it is in whichever of the input lists it is
 119313   ** read from. And each subsequent docid read from the same input list 
 119314   ** consumes either the same or less bytes as it did in the input (since
 119315   ** the difference between it and the previous value in the output must
 119316   ** be a positive value less than or equal to the delta value read from 
 119317   ** the input list). The same argument applies to all but the first docid
 119318   ** read from the 'other' list. And to the contents of all position lists
 119319   ** that will be copied and merged from the input to the output.
 119321   ** However, if the first docid copied to the output is a negative number,
 119322   ** then the encoding of the first docid from the 'other' input list may
 119323   ** be larger in the output than it was in the input (since the delta value
 119324   ** may be a larger positive integer than the actual docid).
 119326   ** The space required to store the output is therefore the sum of the
 119327   ** sizes of the two inputs, plus enough space for exactly one of the input
 119328   ** docids to grow. 
 119330   ** A symetric argument may be made if the doclists are in descending 
 119331   ** order.
 119333   aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
 119334   if( !aOut ) return SQLITE_NOMEM;
 119336   p = aOut;
 119337   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
 119338   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
 119339   while( p1 || p2 ){
 119340     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
 119342     if( p2 && p1 && iDiff==0 ){
 119343       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
 119344       fts3PoslistMerge(&p, &p1, &p2);
 119345       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 119346       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 119347     }else if( !p2 || (p1 && iDiff<0) ){
 119348       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
 119349       fts3PoslistCopy(&p, &p1);
 119350       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 119351     }else{
 119352       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
 119353       fts3PoslistCopy(&p, &p2);
 119354       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 119358   *paOut = aOut;
 119359   *pnOut = (int)(p-aOut);
 119360   assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
 119361   return SQLITE_OK;
 119365 ** This function does a "phrase" merge of two doclists. In a phrase merge,
 119366 ** the output contains a copy of each position from the right-hand input
 119367 ** doclist for which there is a position in the left-hand input doclist
 119368 ** exactly nDist tokens before it.
 119370 ** If the docids in the input doclists are sorted in ascending order,
 119371 ** parameter bDescDoclist should be false. If they are sorted in ascending 
 119372 ** order, it should be passed a non-zero value.
 119374 ** The right-hand input doclist is overwritten by this function.
 119376 static void fts3DoclistPhraseMerge(
 119377   int bDescDoclist,               /* True if arguments are desc */
 119378   int nDist,                      /* Distance from left to right (1=adjacent) */
 119379   char *aLeft, int nLeft,         /* Left doclist */
 119380   char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
 119382   sqlite3_int64 i1 = 0;
 119383   sqlite3_int64 i2 = 0;
 119384   sqlite3_int64 iPrev = 0;
 119385   char *pEnd1 = &aLeft[nLeft];
 119386   char *pEnd2 = &aRight[*pnRight];
 119387   char *p1 = aLeft;
 119388   char *p2 = aRight;
 119389   char *p;
 119390   int bFirstOut = 0;
 119391   char *aOut = aRight;
 119393   assert( nDist>0 );
 119395   p = aOut;
 119396   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
 119397   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
 119399   while( p1 && p2 ){
 119400     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
 119401     if( iDiff==0 ){
 119402       char *pSave = p;
 119403       sqlite3_int64 iPrevSave = iPrev;
 119404       int bFirstOutSave = bFirstOut;
 119406       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
 119407       if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
 119408         p = pSave;
 119409         iPrev = iPrevSave;
 119410         bFirstOut = bFirstOutSave;
 119412       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 119413       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 119414     }else if( iDiff<0 ){
 119415       fts3PoslistCopy(0, &p1);
 119416       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 119417     }else{
 119418       fts3PoslistCopy(0, &p2);
 119419       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 119423   *pnRight = (int)(p - aOut);
 119427 ** Argument pList points to a position list nList bytes in size. This
 119428 ** function checks to see if the position list contains any entries for
 119429 ** a token in position 0 (of any column). If so, it writes argument iDelta
 119430 ** to the output buffer pOut, followed by a position list consisting only
 119431 ** of the entries from pList at position 0, and terminated by an 0x00 byte.
 119432 ** The value returned is the number of bytes written to pOut (if any).
 119434 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
 119435   sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
 119436   char *pList,                    /* Position list (no 0x00 term) */
 119437   int nList,                      /* Size of pList in bytes */
 119438   char *pOut                      /* Write output here */
 119440   int nOut = 0;
 119441   int bWritten = 0;               /* True once iDelta has been written */
 119442   char *p = pList;
 119443   char *pEnd = &pList[nList];
 119445   if( *p!=0x01 ){
 119446     if( *p==0x02 ){
 119447       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
 119448       pOut[nOut++] = 0x02;
 119449       bWritten = 1;
 119451     fts3ColumnlistCopy(0, &p);
 119454   while( p<pEnd && *p==0x01 ){
 119455     sqlite3_int64 iCol;
 119456     p++;
 119457     p += sqlite3Fts3GetVarint(p, &iCol);
 119458     if( *p==0x02 ){
 119459       if( bWritten==0 ){
 119460         nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
 119461         bWritten = 1;
 119463       pOut[nOut++] = 0x01;
 119464       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
 119465       pOut[nOut++] = 0x02;
 119467     fts3ColumnlistCopy(0, &p);
 119469   if( bWritten ){
 119470     pOut[nOut++] = 0x00;
 119473   return nOut;
 119478 ** Merge all doclists in the TermSelect.aaOutput[] array into a single
 119479 ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
 119480 ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
 119482 ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
 119483 ** the responsibility of the caller to free any doclists left in the
 119484 ** TermSelect.aaOutput[] array.
 119486 static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
 119487   char *aOut = 0;
 119488   int nOut = 0;
 119489   int i;
 119491   /* Loop through the doclists in the aaOutput[] array. Merge them all
 119492   ** into a single doclist.
 119494   for(i=0; i<SizeofArray(pTS->aaOutput); i++){
 119495     if( pTS->aaOutput[i] ){
 119496       if( !aOut ){
 119497         aOut = pTS->aaOutput[i];
 119498         nOut = pTS->anOutput[i];
 119499         pTS->aaOutput[i] = 0;
 119500       }else{
 119501         int nNew;
 119502         char *aNew;
 119504         int rc = fts3DoclistOrMerge(p->bDescIdx, 
 119505             pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
 119507         if( rc!=SQLITE_OK ){
 119508           sqlite3_free(aOut);
 119509           return rc;
 119512         sqlite3_free(pTS->aaOutput[i]);
 119513         sqlite3_free(aOut);
 119514         pTS->aaOutput[i] = 0;
 119515         aOut = aNew;
 119516         nOut = nNew;
 119521   pTS->aaOutput[0] = aOut;
 119522   pTS->anOutput[0] = nOut;
 119523   return SQLITE_OK;
 119527 ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
 119528 ** as the first argument. The merge is an "OR" merge (see function
 119529 ** fts3DoclistOrMerge() for details).
 119531 ** This function is called with the doclist for each term that matches
 119532 ** a queried prefix. It merges all these doclists into one, the doclist
 119533 ** for the specified prefix. Since there can be a very large number of
 119534 ** doclists to merge, the merging is done pair-wise using the TermSelect
 119535 ** object.
 119537 ** This function returns SQLITE_OK if the merge is successful, or an
 119538 ** SQLite error code (SQLITE_NOMEM) if an error occurs.
 119540 static int fts3TermSelectMerge(
 119541   Fts3Table *p,                   /* FTS table handle */
 119542   TermSelect *pTS,                /* TermSelect object to merge into */
 119543   char *aDoclist,                 /* Pointer to doclist */
 119544   int nDoclist                    /* Size of aDoclist in bytes */
 119546   if( pTS->aaOutput[0]==0 ){
 119547     /* If this is the first term selected, copy the doclist to the output
 119548     ** buffer using memcpy(). */
 119549     pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
 119550     pTS->anOutput[0] = nDoclist;
 119551     if( pTS->aaOutput[0] ){
 119552       memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
 119553     }else{
 119554       return SQLITE_NOMEM;
 119556   }else{
 119557     char *aMerge = aDoclist;
 119558     int nMerge = nDoclist;
 119559     int iOut;
 119561     for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
 119562       if( pTS->aaOutput[iOut]==0 ){
 119563         assert( iOut>0 );
 119564         pTS->aaOutput[iOut] = aMerge;
 119565         pTS->anOutput[iOut] = nMerge;
 119566         break;
 119567       }else{
 119568         char *aNew;
 119569         int nNew;
 119571         int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge, 
 119572             pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
 119574         if( rc!=SQLITE_OK ){
 119575           if( aMerge!=aDoclist ) sqlite3_free(aMerge);
 119576           return rc;
 119579         if( aMerge!=aDoclist ) sqlite3_free(aMerge);
 119580         sqlite3_free(pTS->aaOutput[iOut]);
 119581         pTS->aaOutput[iOut] = 0;
 119583         aMerge = aNew;
 119584         nMerge = nNew;
 119585         if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
 119586           pTS->aaOutput[iOut] = aMerge;
 119587           pTS->anOutput[iOut] = nMerge;
 119592   return SQLITE_OK;
 119596 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
 119598 static int fts3SegReaderCursorAppend(
 119599   Fts3MultiSegReader *pCsr, 
 119600   Fts3SegReader *pNew
 119602   if( (pCsr->nSegment%16)==0 ){
 119603     Fts3SegReader **apNew;
 119604     int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
 119605     apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
 119606     if( !apNew ){
 119607       sqlite3Fts3SegReaderFree(pNew);
 119608       return SQLITE_NOMEM;
 119610     pCsr->apSegment = apNew;
 119612   pCsr->apSegment[pCsr->nSegment++] = pNew;
 119613   return SQLITE_OK;
 119617 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
 119618 ** 8th argument.
 119620 ** This function returns SQLITE_OK if successful, or an SQLite error code
 119621 ** otherwise.
 119623 static int fts3SegReaderCursor(
 119624   Fts3Table *p,                   /* FTS3 table handle */
 119625   int iLangid,                    /* Language id */
 119626   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
 119627   int iLevel,                     /* Level of segments to scan */
 119628   const char *zTerm,              /* Term to query for */
 119629   int nTerm,                      /* Size of zTerm in bytes */
 119630   int isPrefix,                   /* True for a prefix search */
 119631   int isScan,                     /* True to scan from zTerm to EOF */
 119632   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
 119634   int rc = SQLITE_OK;             /* Error code */
 119635   sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
 119636   int rc2;                        /* Result of sqlite3_reset() */
 119638   /* If iLevel is less than 0 and this is not a scan, include a seg-reader 
 119639   ** for the pending-terms. If this is a scan, then this call must be being
 119640   ** made by an fts4aux module, not an FTS table. In this case calling
 119641   ** Fts3SegReaderPending might segfault, as the data structures used by 
 119642   ** fts4aux are not completely populated. So it's easiest to filter these
 119643   ** calls out here.  */
 119644   if( iLevel<0 && p->aIndex ){
 119645     Fts3SegReader *pSeg = 0;
 119646     rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
 119647     if( rc==SQLITE_OK && pSeg ){
 119648       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
 119652   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
 119653     if( rc==SQLITE_OK ){
 119654       rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
 119657     while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
 119658       Fts3SegReader *pSeg = 0;
 119660       /* Read the values returned by the SELECT into local variables. */
 119661       sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
 119662       sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
 119663       sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
 119664       int nRoot = sqlite3_column_bytes(pStmt, 4);
 119665       char const *zRoot = sqlite3_column_blob(pStmt, 4);
 119667       /* If zTerm is not NULL, and this segment is not stored entirely on its
 119668       ** root node, the range of leaves scanned can be reduced. Do this. */
 119669       if( iStartBlock && zTerm ){
 119670         sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
 119671         rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
 119672         if( rc!=SQLITE_OK ) goto finished;
 119673         if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
 119676       rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1, 
 119677           (isPrefix==0 && isScan==0),
 119678           iStartBlock, iLeavesEndBlock, 
 119679           iEndBlock, zRoot, nRoot, &pSeg
 119681       if( rc!=SQLITE_OK ) goto finished;
 119682       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
 119686  finished:
 119687   rc2 = sqlite3_reset(pStmt);
 119688   if( rc==SQLITE_DONE ) rc = rc2;
 119690   return rc;
 119694 ** Set up a cursor object for iterating through a full-text index or a 
 119695 ** single level therein.
 119697 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
 119698   Fts3Table *p,                   /* FTS3 table handle */
 119699   int iLangid,                    /* Language-id to search */
 119700   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
 119701   int iLevel,                     /* Level of segments to scan */
 119702   const char *zTerm,              /* Term to query for */
 119703   int nTerm,                      /* Size of zTerm in bytes */
 119704   int isPrefix,                   /* True for a prefix search */
 119705   int isScan,                     /* True to scan from zTerm to EOF */
 119706   Fts3MultiSegReader *pCsr       /* Cursor object to populate */
 119708   assert( iIndex>=0 && iIndex<p->nIndex );
 119709   assert( iLevel==FTS3_SEGCURSOR_ALL
 119710       ||  iLevel==FTS3_SEGCURSOR_PENDING 
 119711       ||  iLevel>=0
 119713   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
 119714   assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
 119715   assert( isPrefix==0 || isScan==0 );
 119717   memset(pCsr, 0, sizeof(Fts3MultiSegReader));
 119718   return fts3SegReaderCursor(
 119719       p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
 119724 ** In addition to its current configuration, have the Fts3MultiSegReader
 119725 ** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
 119727 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 119729 static int fts3SegReaderCursorAddZero(
 119730   Fts3Table *p,                   /* FTS virtual table handle */
 119731   int iLangid,
 119732   const char *zTerm,              /* Term to scan doclist of */
 119733   int nTerm,                      /* Number of bytes in zTerm */
 119734   Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
 119736   return fts3SegReaderCursor(p, 
 119737       iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
 119742 ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
 119743 ** if isPrefix is true, to scan the doclist for all terms for which 
 119744 ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
 119745 ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
 119746 ** an SQLite error code.
 119748 ** It is the responsibility of the caller to free this object by eventually
 119749 ** passing it to fts3SegReaderCursorFree() 
 119751 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 119752 ** Output parameter *ppSegcsr is set to 0 if an error occurs.
 119754 static int fts3TermSegReaderCursor(
 119755   Fts3Cursor *pCsr,               /* Virtual table cursor handle */
 119756   const char *zTerm,              /* Term to query for */
 119757   int nTerm,                      /* Size of zTerm in bytes */
 119758   int isPrefix,                   /* True for a prefix search */
 119759   Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
 119761   Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
 119762   int rc = SQLITE_NOMEM;          /* Return code */
 119764   pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
 119765   if( pSegcsr ){
 119766     int i;
 119767     int bFound = 0;               /* True once an index has been found */
 119768     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
 119770     if( isPrefix ){
 119771       for(i=1; bFound==0 && i<p->nIndex; i++){
 119772         if( p->aIndex[i].nPrefix==nTerm ){
 119773           bFound = 1;
 119774           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
 119775               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
 119777           pSegcsr->bLookup = 1;
 119781       for(i=1; bFound==0 && i<p->nIndex; i++){
 119782         if( p->aIndex[i].nPrefix==nTerm+1 ){
 119783           bFound = 1;
 119784           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
 119785               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
 119787           if( rc==SQLITE_OK ){
 119788             rc = fts3SegReaderCursorAddZero(
 119789                 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
 119796     if( bFound==0 ){
 119797       rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
 119798           0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
 119800       pSegcsr->bLookup = !isPrefix;
 119804   *ppSegcsr = pSegcsr;
 119805   return rc;
 119809 ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
 119811 static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
 119812   sqlite3Fts3SegReaderFinish(pSegcsr);
 119813   sqlite3_free(pSegcsr);
 119817 ** This function retreives the doclist for the specified term (or term
 119818 ** prefix) from the database.
 119820 static int fts3TermSelect(
 119821   Fts3Table *p,                   /* Virtual table handle */
 119822   Fts3PhraseToken *pTok,          /* Token to query for */
 119823   int iColumn,                    /* Column to query (or -ve for all columns) */
 119824   int *pnOut,                     /* OUT: Size of buffer at *ppOut */
 119825   char **ppOut                    /* OUT: Malloced result buffer */
 119827   int rc;                         /* Return code */
 119828   Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
 119829   TermSelect tsc;                 /* Object for pair-wise doclist merging */
 119830   Fts3SegFilter filter;           /* Segment term filter configuration */
 119832   pSegcsr = pTok->pSegcsr;
 119833   memset(&tsc, 0, sizeof(TermSelect));
 119835   filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
 119836         | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
 119837         | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
 119838         | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
 119839   filter.iCol = iColumn;
 119840   filter.zTerm = pTok->z;
 119841   filter.nTerm = pTok->n;
 119843   rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
 119844   while( SQLITE_OK==rc
 119845       && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) 
 119847     rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
 119850   if( rc==SQLITE_OK ){
 119851     rc = fts3TermSelectFinishMerge(p, &tsc);
 119853   if( rc==SQLITE_OK ){
 119854     *ppOut = tsc.aaOutput[0];
 119855     *pnOut = tsc.anOutput[0];
 119856   }else{
 119857     int i;
 119858     for(i=0; i<SizeofArray(tsc.aaOutput); i++){
 119859       sqlite3_free(tsc.aaOutput[i]);
 119863   fts3SegReaderCursorFree(pSegcsr);
 119864   pTok->pSegcsr = 0;
 119865   return rc;
 119869 ** This function counts the total number of docids in the doclist stored
 119870 ** in buffer aList[], size nList bytes.
 119872 ** If the isPoslist argument is true, then it is assumed that the doclist
 119873 ** contains a position-list following each docid. Otherwise, it is assumed
 119874 ** that the doclist is simply a list of docids stored as delta encoded 
 119875 ** varints.
 119877 static int fts3DoclistCountDocids(char *aList, int nList){
 119878   int nDoc = 0;                   /* Return value */
 119879   if( aList ){
 119880     char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
 119881     char *p = aList;              /* Cursor */
 119882     while( p<aEnd ){
 119883       nDoc++;
 119884       while( (*p++)&0x80 );     /* Skip docid varint */
 119885       fts3PoslistCopy(0, &p);   /* Skip over position list */
 119889   return nDoc;
 119893 ** Advance the cursor to the next row in the %_content table that
 119894 ** matches the search criteria.  For a MATCH search, this will be
 119895 ** the next row that matches. For a full-table scan, this will be
 119896 ** simply the next row in the %_content table.  For a docid lookup,
 119897 ** this routine simply sets the EOF flag.
 119899 ** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
 119900 ** even if we reach end-of-file.  The fts3EofMethod() will be called
 119901 ** subsequently to determine whether or not an EOF was hit.
 119903 static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
 119904   int rc;
 119905   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
 119906   if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
 119907     if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
 119908       pCsr->isEof = 1;
 119909       rc = sqlite3_reset(pCsr->pStmt);
 119910     }else{
 119911       pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
 119912       rc = SQLITE_OK;
 119914   }else{
 119915     rc = fts3EvalNext((Fts3Cursor *)pCursor);
 119917   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 119918   return rc;
 119922 ** This is the xFilter interface for the virtual table.  See
 119923 ** the virtual table xFilter method documentation for additional
 119924 ** information.
 119926 ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
 119927 ** the %_content table.
 119929 ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
 119930 ** in the %_content table.
 119932 ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
 119933 ** column on the left-hand side of the MATCH operator is column
 119934 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
 119935 ** side of the MATCH operator.
 119937 static int fts3FilterMethod(
 119938   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
 119939   int idxNum,                     /* Strategy index */
 119940   const char *idxStr,             /* Unused */
 119941   int nVal,                       /* Number of elements in apVal */
 119942   sqlite3_value **apVal           /* Arguments for the indexing scheme */
 119944   int rc;
 119945   char *zSql;                     /* SQL statement used to access %_content */
 119946   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
 119947   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
 119949   UNUSED_PARAMETER(idxStr);
 119950   UNUSED_PARAMETER(nVal);
 119952   assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
 119953   assert( nVal==0 || nVal==1 || nVal==2 );
 119954   assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
 119955   assert( p->pSegments==0 );
 119957   /* In case the cursor has been used before, clear it now. */
 119958   sqlite3_finalize(pCsr->pStmt);
 119959   sqlite3_free(pCsr->aDoclist);
 119960   sqlite3Fts3ExprFree(pCsr->pExpr);
 119961   memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
 119963   if( idxStr ){
 119964     pCsr->bDesc = (idxStr[0]=='D');
 119965   }else{
 119966     pCsr->bDesc = p->bDescIdx;
 119968   pCsr->eSearch = (i16)idxNum;
 119970   if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
 119971     int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
 119972     const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
 119974     if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
 119975       return SQLITE_NOMEM;
 119978     pCsr->iLangid = 0;
 119979     if( nVal==2 ) pCsr->iLangid = sqlite3_value_int(apVal[1]);
 119981     rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
 119982         p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
 119984     if( rc!=SQLITE_OK ){
 119985       if( rc==SQLITE_ERROR ){
 119986         static const char *zErr = "malformed MATCH expression: [%s]";
 119987         p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
 119989       return rc;
 119992     rc = sqlite3Fts3ReadLock(p);
 119993     if( rc!=SQLITE_OK ) return rc;
 119995     rc = fts3EvalStart(pCsr);
 119997     sqlite3Fts3SegmentsClose(p);
 119998     if( rc!=SQLITE_OK ) return rc;
 119999     pCsr->pNextId = pCsr->aDoclist;
 120000     pCsr->iPrevId = 0;
 120003   /* Compile a SELECT statement for this cursor. For a full-table-scan, the
 120004   ** statement loops through all rows of the %_content table. For a
 120005   ** full-text query or docid lookup, the statement retrieves a single
 120006   ** row by docid.
 120008   if( idxNum==FTS3_FULLSCAN_SEARCH ){
 120009     zSql = sqlite3_mprintf(
 120010         "SELECT %s ORDER BY rowid %s",
 120011         p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
 120013     if( zSql ){
 120014       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
 120015       sqlite3_free(zSql);
 120016     }else{
 120017       rc = SQLITE_NOMEM;
 120019   }else if( idxNum==FTS3_DOCID_SEARCH ){
 120020     rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
 120021     if( rc==SQLITE_OK ){
 120022       rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
 120025   if( rc!=SQLITE_OK ) return rc;
 120027   return fts3NextMethod(pCursor);
 120031 ** This is the xEof method of the virtual table. SQLite calls this 
 120032 ** routine to find out if it has reached the end of a result set.
 120034 static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
 120035   return ((Fts3Cursor *)pCursor)->isEof;
 120039 ** This is the xRowid method. The SQLite core calls this routine to
 120040 ** retrieve the rowid for the current row of the result set. fts3
 120041 ** exposes %_content.docid as the rowid for the virtual table. The
 120042 ** rowid should be written to *pRowid.
 120044 static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
 120045   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
 120046   *pRowid = pCsr->iPrevId;
 120047   return SQLITE_OK;
 120051 ** This is the xColumn method, called by SQLite to request a value from
 120052 ** the row that the supplied cursor currently points to.
 120054 ** If:
 120056 **   (iCol <  p->nColumn)   -> The value of the iCol'th user column.
 120057 **   (iCol == p->nColumn)   -> Magic column with the same name as the table.
 120058 **   (iCol == p->nColumn+1) -> Docid column
 120059 **   (iCol == p->nColumn+2) -> Langid column
 120061 static int fts3ColumnMethod(
 120062   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 120063   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
 120064   int iCol                        /* Index of column to read value from */
 120066   int rc = SQLITE_OK;             /* Return Code */
 120067   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
 120068   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
 120070   /* The column value supplied by SQLite must be in range. */
 120071   assert( iCol>=0 && iCol<=p->nColumn+2 );
 120073   if( iCol==p->nColumn+1 ){
 120074     /* This call is a request for the "docid" column. Since "docid" is an 
 120075     ** alias for "rowid", use the xRowid() method to obtain the value.
 120077     sqlite3_result_int64(pCtx, pCsr->iPrevId);
 120078   }else if( iCol==p->nColumn ){
 120079     /* The extra column whose name is the same as the table.
 120080     ** Return a blob which is a pointer to the cursor.  */
 120081     sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
 120082   }else if( iCol==p->nColumn+2 && pCsr->pExpr ){
 120083     sqlite3_result_int64(pCtx, pCsr->iLangid);
 120084   }else{
 120085     /* The requested column is either a user column (one that contains 
 120086     ** indexed data), or the language-id column.  */
 120087     rc = fts3CursorSeek(0, pCsr);
 120089     if( rc==SQLITE_OK ){
 120090       if( iCol==p->nColumn+2 ){
 120091         int iLangid = 0;
 120092         if( p->zLanguageid ){
 120093           iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1);
 120095         sqlite3_result_int(pCtx, iLangid);
 120096       }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
 120097         sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
 120102   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 120103   return rc;
 120107 ** This function is the implementation of the xUpdate callback used by 
 120108 ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
 120109 ** inserted, updated or deleted.
 120111 static int fts3UpdateMethod(
 120112   sqlite3_vtab *pVtab,            /* Virtual table handle */
 120113   int nArg,                       /* Size of argument array */
 120114   sqlite3_value **apVal,          /* Array of arguments */
 120115   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
 120117   return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
 120121 ** Implementation of xSync() method. Flush the contents of the pending-terms
 120122 ** hash-table to the database.
 120124 static int fts3SyncMethod(sqlite3_vtab *pVtab){
 120126   /* Following an incremental-merge operation, assuming that the input
 120127   ** segments are not completely consumed (the usual case), they are updated
 120128   ** in place to remove the entries that have already been merged. This
 120129   ** involves updating the leaf block that contains the smallest unmerged
 120130   ** entry and each block (if any) between the leaf and the root node. So
 120131   ** if the height of the input segment b-trees is N, and input segments
 120132   ** are merged eight at a time, updating the input segments at the end
 120133   ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
 120134   ** small - often between 0 and 2. So the overhead of the incremental
 120135   ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
 120136   ** dwarfing the actual productive work accomplished, the incremental merge
 120137   ** is only attempted if it will write at least 64 leaf blocks. Hence
 120138   ** nMinMerge.
 120140   ** Of course, updating the input segments also involves deleting a bunch
 120141   ** of blocks from the segments table. But this is not considered overhead
 120142   ** as it would also be required by a crisis-merge that used the same input 
 120143   ** segments.
 120145   const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
 120147   Fts3Table *p = (Fts3Table*)pVtab;
 120148   int rc = sqlite3Fts3PendingTermsFlush(p);
 120150   if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
 120151     int mxLevel = 0;              /* Maximum relative level value in db */
 120152     int A;                        /* Incr-merge parameter A */
 120154     rc = sqlite3Fts3MaxLevel(p, &mxLevel);
 120155     assert( rc==SQLITE_OK || mxLevel==0 );
 120156     A = p->nLeafAdd * mxLevel;
 120157     A += (A/2);
 120158     if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
 120160   sqlite3Fts3SegmentsClose(p);
 120161   return rc;
 120165 ** Implementation of xBegin() method. This is a no-op.
 120167 static int fts3BeginMethod(sqlite3_vtab *pVtab){
 120168   Fts3Table *p = (Fts3Table*)pVtab;
 120169   UNUSED_PARAMETER(pVtab);
 120170   assert( p->pSegments==0 );
 120171   assert( p->nPendingData==0 );
 120172   assert( p->inTransaction!=1 );
 120173   TESTONLY( p->inTransaction = 1 );
 120174   TESTONLY( p->mxSavepoint = -1; );
 120175   p->nLeafAdd = 0;
 120176   return SQLITE_OK;
 120180 ** Implementation of xCommit() method. This is a no-op. The contents of
 120181 ** the pending-terms hash-table have already been flushed into the database
 120182 ** by fts3SyncMethod().
 120184 static int fts3CommitMethod(sqlite3_vtab *pVtab){
 120185   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
 120186   UNUSED_PARAMETER(pVtab);
 120187   assert( p->nPendingData==0 );
 120188   assert( p->inTransaction!=0 );
 120189   assert( p->pSegments==0 );
 120190   TESTONLY( p->inTransaction = 0 );
 120191   TESTONLY( p->mxSavepoint = -1; );
 120192   return SQLITE_OK;
 120196 ** Implementation of xRollback(). Discard the contents of the pending-terms
 120197 ** hash-table. Any changes made to the database are reverted by SQLite.
 120199 static int fts3RollbackMethod(sqlite3_vtab *pVtab){
 120200   Fts3Table *p = (Fts3Table*)pVtab;
 120201   sqlite3Fts3PendingTermsClear(p);
 120202   assert( p->inTransaction!=0 );
 120203   TESTONLY( p->inTransaction = 0 );
 120204   TESTONLY( p->mxSavepoint = -1; );
 120205   return SQLITE_OK;
 120209 ** When called, *ppPoslist must point to the byte immediately following the
 120210 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
 120211 ** moves *ppPoslist so that it instead points to the first byte of the
 120212 ** same position list.
 120214 static void fts3ReversePoslist(char *pStart, char **ppPoslist){
 120215   char *p = &(*ppPoslist)[-2];
 120216   char c = 0;
 120218   while( p>pStart && (c=*p--)==0 );
 120219   while( p>pStart && (*p & 0x80) | c ){ 
 120220     c = *p--; 
 120222   if( p>pStart ){ p = &p[2]; }
 120223   while( *p++&0x80 );
 120224   *ppPoslist = p;
 120228 ** Helper function used by the implementation of the overloaded snippet(),
 120229 ** offsets() and optimize() SQL functions.
 120231 ** If the value passed as the third argument is a blob of size
 120232 ** sizeof(Fts3Cursor*), then the blob contents are copied to the 
 120233 ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
 120234 ** message is written to context pContext and SQLITE_ERROR returned. The
 120235 ** string passed via zFunc is used as part of the error message.
 120237 static int fts3FunctionArg(
 120238   sqlite3_context *pContext,      /* SQL function call context */
 120239   const char *zFunc,              /* Function name */
 120240   sqlite3_value *pVal,            /* argv[0] passed to function */
 120241   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
 120243   Fts3Cursor *pRet;
 120244   if( sqlite3_value_type(pVal)!=SQLITE_BLOB 
 120245    || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
 120247     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
 120248     sqlite3_result_error(pContext, zErr, -1);
 120249     sqlite3_free(zErr);
 120250     return SQLITE_ERROR;
 120252   memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
 120253   *ppCsr = pRet;
 120254   return SQLITE_OK;
 120258 ** Implementation of the snippet() function for FTS3
 120260 static void fts3SnippetFunc(
 120261   sqlite3_context *pContext,      /* SQLite function call context */
 120262   int nVal,                       /* Size of apVal[] array */
 120263   sqlite3_value **apVal           /* Array of arguments */
 120265   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
 120266   const char *zStart = "<b>";
 120267   const char *zEnd = "</b>";
 120268   const char *zEllipsis = "<b>...</b>";
 120269   int iCol = -1;
 120270   int nToken = 15;                /* Default number of tokens in snippet */
 120272   /* There must be at least one argument passed to this function (otherwise
 120273   ** the non-overloaded version would have been called instead of this one).
 120275   assert( nVal>=1 );
 120277   if( nVal>6 ){
 120278     sqlite3_result_error(pContext, 
 120279         "wrong number of arguments to function snippet()", -1);
 120280     return;
 120282   if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
 120284   switch( nVal ){
 120285     case 6: nToken = sqlite3_value_int(apVal[5]);
 120286     case 5: iCol = sqlite3_value_int(apVal[4]);
 120287     case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
 120288     case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
 120289     case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
 120291   if( !zEllipsis || !zEnd || !zStart ){
 120292     sqlite3_result_error_nomem(pContext);
 120293   }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
 120294     sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
 120299 ** Implementation of the offsets() function for FTS3
 120301 static void fts3OffsetsFunc(
 120302   sqlite3_context *pContext,      /* SQLite function call context */
 120303   int nVal,                       /* Size of argument array */
 120304   sqlite3_value **apVal           /* Array of arguments */
 120306   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
 120308   UNUSED_PARAMETER(nVal);
 120310   assert( nVal==1 );
 120311   if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
 120312   assert( pCsr );
 120313   if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
 120314     sqlite3Fts3Offsets(pContext, pCsr);
 120319 ** Implementation of the special optimize() function for FTS3. This 
 120320 ** function merges all segments in the database to a single segment.
 120321 ** Example usage is:
 120323 **   SELECT optimize(t) FROM t LIMIT 1;
 120325 ** where 't' is the name of an FTS3 table.
 120327 static void fts3OptimizeFunc(
 120328   sqlite3_context *pContext,      /* SQLite function call context */
 120329   int nVal,                       /* Size of argument array */
 120330   sqlite3_value **apVal           /* Array of arguments */
 120332   int rc;                         /* Return code */
 120333   Fts3Table *p;                   /* Virtual table handle */
 120334   Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
 120336   UNUSED_PARAMETER(nVal);
 120338   assert( nVal==1 );
 120339   if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
 120340   p = (Fts3Table *)pCursor->base.pVtab;
 120341   assert( p );
 120343   rc = sqlite3Fts3Optimize(p);
 120345   switch( rc ){
 120346     case SQLITE_OK:
 120347       sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
 120348       break;
 120349     case SQLITE_DONE:
 120350       sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
 120351       break;
 120352     default:
 120353       sqlite3_result_error_code(pContext, rc);
 120354       break;
 120359 ** Implementation of the matchinfo() function for FTS3
 120361 static void fts3MatchinfoFunc(
 120362   sqlite3_context *pContext,      /* SQLite function call context */
 120363   int nVal,                       /* Size of argument array */
 120364   sqlite3_value **apVal           /* Array of arguments */
 120366   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
 120367   assert( nVal==1 || nVal==2 );
 120368   if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
 120369     const char *zArg = 0;
 120370     if( nVal>1 ){
 120371       zArg = (const char *)sqlite3_value_text(apVal[1]);
 120373     sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
 120378 ** This routine implements the xFindFunction method for the FTS3
 120379 ** virtual table.
 120381 static int fts3FindFunctionMethod(
 120382   sqlite3_vtab *pVtab,            /* Virtual table handle */
 120383   int nArg,                       /* Number of SQL function arguments */
 120384   const char *zName,              /* Name of SQL function */
 120385   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
 120386   void **ppArg                    /* Unused */
 120388   struct Overloaded {
 120389     const char *zName;
 120390     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
 120391   } aOverload[] = {
 120392     { "snippet", fts3SnippetFunc },
 120393     { "offsets", fts3OffsetsFunc },
 120394     { "optimize", fts3OptimizeFunc },
 120395     { "matchinfo", fts3MatchinfoFunc },
 120397   int i;                          /* Iterator variable */
 120399   UNUSED_PARAMETER(pVtab);
 120400   UNUSED_PARAMETER(nArg);
 120401   UNUSED_PARAMETER(ppArg);
 120403   for(i=0; i<SizeofArray(aOverload); i++){
 120404     if( strcmp(zName, aOverload[i].zName)==0 ){
 120405       *pxFunc = aOverload[i].xFunc;
 120406       return 1;
 120410   /* No function of the specified name was found. Return 0. */
 120411   return 0;
 120415 ** Implementation of FTS3 xRename method. Rename an fts3 table.
 120417 static int fts3RenameMethod(
 120418   sqlite3_vtab *pVtab,            /* Virtual table handle */
 120419   const char *zName               /* New name of table */
 120421   Fts3Table *p = (Fts3Table *)pVtab;
 120422   sqlite3 *db = p->db;            /* Database connection */
 120423   int rc;                         /* Return Code */
 120425   /* As it happens, the pending terms table is always empty here. This is
 120426   ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction 
 120427   ** always opens a savepoint transaction. And the xSavepoint() method 
 120428   ** flushes the pending terms table. But leave the (no-op) call to
 120429   ** PendingTermsFlush() in in case that changes.
 120431   assert( p->nPendingData==0 );
 120432   rc = sqlite3Fts3PendingTermsFlush(p);
 120434   if( p->zContentTbl==0 ){
 120435     fts3DbExec(&rc, db,
 120436       "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
 120437       p->zDb, p->zName, zName
 120441   if( p->bHasDocsize ){
 120442     fts3DbExec(&rc, db,
 120443       "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
 120444       p->zDb, p->zName, zName
 120447   if( p->bHasStat ){
 120448     fts3DbExec(&rc, db,
 120449       "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
 120450       p->zDb, p->zName, zName
 120453   fts3DbExec(&rc, db,
 120454     "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
 120455     p->zDb, p->zName, zName
 120457   fts3DbExec(&rc, db,
 120458     "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
 120459     p->zDb, p->zName, zName
 120461   return rc;
 120465 ** The xSavepoint() method.
 120467 ** Flush the contents of the pending-terms table to disk.
 120469 static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
 120470   int rc = SQLITE_OK;
 120471   UNUSED_PARAMETER(iSavepoint);
 120472   assert( ((Fts3Table *)pVtab)->inTransaction );
 120473   assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
 120474   TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
 120475   if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
 120476     rc = fts3SyncMethod(pVtab);
 120478   return rc;
 120482 ** The xRelease() method.
 120484 ** This is a no-op.
 120486 static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
 120487   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
 120488   UNUSED_PARAMETER(iSavepoint);
 120489   UNUSED_PARAMETER(pVtab);
 120490   assert( p->inTransaction );
 120491   assert( p->mxSavepoint >= iSavepoint );
 120492   TESTONLY( p->mxSavepoint = iSavepoint-1 );
 120493   return SQLITE_OK;
 120497 ** The xRollbackTo() method.
 120499 ** Discard the contents of the pending terms table.
 120501 static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
 120502   Fts3Table *p = (Fts3Table*)pVtab;
 120503   UNUSED_PARAMETER(iSavepoint);
 120504   assert( p->inTransaction );
 120505   assert( p->mxSavepoint >= iSavepoint );
 120506   TESTONLY( p->mxSavepoint = iSavepoint );
 120507   sqlite3Fts3PendingTermsClear(p);
 120508   return SQLITE_OK;
 120511 static const sqlite3_module fts3Module = {
 120512   /* iVersion      */ 2,
 120513   /* xCreate       */ fts3CreateMethod,
 120514   /* xConnect      */ fts3ConnectMethod,
 120515   /* xBestIndex    */ fts3BestIndexMethod,
 120516   /* xDisconnect   */ fts3DisconnectMethod,
 120517   /* xDestroy      */ fts3DestroyMethod,
 120518   /* xOpen         */ fts3OpenMethod,
 120519   /* xClose        */ fts3CloseMethod,
 120520   /* xFilter       */ fts3FilterMethod,
 120521   /* xNext         */ fts3NextMethod,
 120522   /* xEof          */ fts3EofMethod,
 120523   /* xColumn       */ fts3ColumnMethod,
 120524   /* xRowid        */ fts3RowidMethod,
 120525   /* xUpdate       */ fts3UpdateMethod,
 120526   /* xBegin        */ fts3BeginMethod,
 120527   /* xSync         */ fts3SyncMethod,
 120528   /* xCommit       */ fts3CommitMethod,
 120529   /* xRollback     */ fts3RollbackMethod,
 120530   /* xFindFunction */ fts3FindFunctionMethod,
 120531   /* xRename */       fts3RenameMethod,
 120532   /* xSavepoint    */ fts3SavepointMethod,
 120533   /* xRelease      */ fts3ReleaseMethod,
 120534   /* xRollbackTo   */ fts3RollbackToMethod,
 120538 ** This function is registered as the module destructor (called when an
 120539 ** FTS3 enabled database connection is closed). It frees the memory
 120540 ** allocated for the tokenizer hash table.
 120542 static void hashDestroy(void *p){
 120543   Fts3Hash *pHash = (Fts3Hash *)p;
 120544   sqlite3Fts3HashClear(pHash);
 120545   sqlite3_free(pHash);
 120549 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are 
 120550 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
 120551 ** respectively. The following three forward declarations are for functions
 120552 ** declared in these files used to retrieve the respective implementations.
 120554 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
 120555 ** to by the argument to point to the "simple" tokenizer implementation.
 120556 ** And so on.
 120558 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 120559 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 120560 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 120561 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
 120562 #endif
 120563 #ifdef SQLITE_ENABLE_ICU
 120564 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 120565 #endif
 120568 ** Initialise the fts3 extension. If this extension is built as part
 120569 ** of the sqlite library, then this function is called directly by
 120570 ** SQLite. If fts3 is built as a dynamically loadable extension, this
 120571 ** function is called by the sqlite3_extension_init() entry point.
 120573 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
 120574   int rc = SQLITE_OK;
 120575   Fts3Hash *pHash = 0;
 120576   const sqlite3_tokenizer_module *pSimple = 0;
 120577   const sqlite3_tokenizer_module *pPorter = 0;
 120578 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 120579   const sqlite3_tokenizer_module *pUnicode = 0;
 120580 #endif
 120582 #ifdef SQLITE_ENABLE_ICU
 120583   const sqlite3_tokenizer_module *pIcu = 0;
 120584   sqlite3Fts3IcuTokenizerModule(&pIcu);
 120585 #endif
 120587 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 120588   sqlite3Fts3UnicodeTokenizer(&pUnicode);
 120589 #endif
 120591 #ifdef SQLITE_TEST
 120592   rc = sqlite3Fts3InitTerm(db);
 120593   if( rc!=SQLITE_OK ) return rc;
 120594 #endif
 120596   rc = sqlite3Fts3InitAux(db);
 120597   if( rc!=SQLITE_OK ) return rc;
 120599   sqlite3Fts3SimpleTokenizerModule(&pSimple);
 120600   sqlite3Fts3PorterTokenizerModule(&pPorter);
 120602   /* Allocate and initialise the hash-table used to store tokenizers. */
 120603   pHash = sqlite3_malloc(sizeof(Fts3Hash));
 120604   if( !pHash ){
 120605     rc = SQLITE_NOMEM;
 120606   }else{
 120607     sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
 120610   /* Load the built-in tokenizers into the hash table */
 120611   if( rc==SQLITE_OK ){
 120612     if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
 120613      || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) 
 120615 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 120616      || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) 
 120617 #endif
 120618 #ifdef SQLITE_ENABLE_ICU
 120619      || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
 120620 #endif
 120622       rc = SQLITE_NOMEM;
 120626 #ifdef SQLITE_TEST
 120627   if( rc==SQLITE_OK ){
 120628     rc = sqlite3Fts3ExprInitTestInterface(db);
 120630 #endif
 120632   /* Create the virtual table wrapper around the hash-table and overload 
 120633   ** the two scalar functions. If this is successful, register the
 120634   ** module with sqlite.
 120636   if( SQLITE_OK==rc 
 120637    && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
 120638    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
 120639    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
 120640    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
 120641    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
 120642    && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
 120644     rc = sqlite3_create_module_v2(
 120645         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
 120647     if( rc==SQLITE_OK ){
 120648       rc = sqlite3_create_module_v2(
 120649           db, "fts4", &fts3Module, (void *)pHash, 0
 120652     return rc;
 120655   /* An error has occurred. Delete the hash table and return the error code. */
 120656   assert( rc!=SQLITE_OK );
 120657   if( pHash ){
 120658     sqlite3Fts3HashClear(pHash);
 120659     sqlite3_free(pHash);
 120661   return rc;
 120665 ** Allocate an Fts3MultiSegReader for each token in the expression headed
 120666 ** by pExpr. 
 120668 ** An Fts3SegReader object is a cursor that can seek or scan a range of
 120669 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
 120670 ** Fts3SegReader objects internally to provide an interface to seek or scan
 120671 ** within the union of all segments of a b-tree. Hence the name.
 120673 ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
 120674 ** segment b-tree (if the term is not a prefix or it is a prefix for which
 120675 ** there exists prefix b-tree of the right length) then it may be traversed
 120676 ** and merged incrementally. Otherwise, it has to be merged into an in-memory 
 120677 ** doclist and then traversed.
 120679 static void fts3EvalAllocateReaders(
 120680   Fts3Cursor *pCsr,               /* FTS cursor handle */
 120681   Fts3Expr *pExpr,                /* Allocate readers for this expression */
 120682   int *pnToken,                   /* OUT: Total number of tokens in phrase. */
 120683   int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
 120684   int *pRc                        /* IN/OUT: Error code */
 120686   if( pExpr && SQLITE_OK==*pRc ){
 120687     if( pExpr->eType==FTSQUERY_PHRASE ){
 120688       int i;
 120689       int nToken = pExpr->pPhrase->nToken;
 120690       *pnToken += nToken;
 120691       for(i=0; i<nToken; i++){
 120692         Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
 120693         int rc = fts3TermSegReaderCursor(pCsr, 
 120694             pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
 120696         if( rc!=SQLITE_OK ){
 120697           *pRc = rc;
 120698           return;
 120701       assert( pExpr->pPhrase->iDoclistToken==0 );
 120702       pExpr->pPhrase->iDoclistToken = -1;
 120703     }else{
 120704       *pnOr += (pExpr->eType==FTSQUERY_OR);
 120705       fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
 120706       fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
 120712 ** Arguments pList/nList contain the doclist for token iToken of phrase p.
 120713 ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
 120715 ** This function assumes that pList points to a buffer allocated using
 120716 ** sqlite3_malloc(). This function takes responsibility for eventually
 120717 ** freeing the buffer.
 120719 static void fts3EvalPhraseMergeToken(
 120720   Fts3Table *pTab,                /* FTS Table pointer */
 120721   Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
 120722   int iToken,                     /* Token pList/nList corresponds to */
 120723   char *pList,                    /* Pointer to doclist */
 120724   int nList                       /* Number of bytes in pList */
 120726   assert( iToken!=p->iDoclistToken );
 120728   if( pList==0 ){
 120729     sqlite3_free(p->doclist.aAll);
 120730     p->doclist.aAll = 0;
 120731     p->doclist.nAll = 0;
 120734   else if( p->iDoclistToken<0 ){
 120735     p->doclist.aAll = pList;
 120736     p->doclist.nAll = nList;
 120739   else if( p->doclist.aAll==0 ){
 120740     sqlite3_free(pList);
 120743   else {
 120744     char *pLeft;
 120745     char *pRight;
 120746     int nLeft;
 120747     int nRight;
 120748     int nDiff;
 120750     if( p->iDoclistToken<iToken ){
 120751       pLeft = p->doclist.aAll;
 120752       nLeft = p->doclist.nAll;
 120753       pRight = pList;
 120754       nRight = nList;
 120755       nDiff = iToken - p->iDoclistToken;
 120756     }else{
 120757       pRight = p->doclist.aAll;
 120758       nRight = p->doclist.nAll;
 120759       pLeft = pList;
 120760       nLeft = nList;
 120761       nDiff = p->iDoclistToken - iToken;
 120764     fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
 120765     sqlite3_free(pLeft);
 120766     p->doclist.aAll = pRight;
 120767     p->doclist.nAll = nRight;
 120770   if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
 120774 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
 120775 ** does not take deferred tokens into account.
 120777 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 120779 static int fts3EvalPhraseLoad(
 120780   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 120781   Fts3Phrase *p                   /* Phrase object */
 120783   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 120784   int iToken;
 120785   int rc = SQLITE_OK;
 120787   for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
 120788     Fts3PhraseToken *pToken = &p->aToken[iToken];
 120789     assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
 120791     if( pToken->pSegcsr ){
 120792       int nThis = 0;
 120793       char *pThis = 0;
 120794       rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
 120795       if( rc==SQLITE_OK ){
 120796         fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
 120799     assert( pToken->pSegcsr==0 );
 120802   return rc;
 120806 ** This function is called on each phrase after the position lists for
 120807 ** any deferred tokens have been loaded into memory. It updates the phrases
 120808 ** current position list to include only those positions that are really
 120809 ** instances of the phrase (after considering deferred tokens). If this
 120810 ** means that the phrase does not appear in the current row, doclist.pList
 120811 ** and doclist.nList are both zeroed.
 120813 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 120815 static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
 120816   int iToken;                     /* Used to iterate through phrase tokens */
 120817   char *aPoslist = 0;             /* Position list for deferred tokens */
 120818   int nPoslist = 0;               /* Number of bytes in aPoslist */
 120819   int iPrev = -1;                 /* Token number of previous deferred token */
 120821   assert( pPhrase->doclist.bFreeList==0 );
 120823   for(iToken=0; iToken<pPhrase->nToken; iToken++){
 120824     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
 120825     Fts3DeferredToken *pDeferred = pToken->pDeferred;
 120827     if( pDeferred ){
 120828       char *pList;
 120829       int nList;
 120830       int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
 120831       if( rc!=SQLITE_OK ) return rc;
 120833       if( pList==0 ){
 120834         sqlite3_free(aPoslist);
 120835         pPhrase->doclist.pList = 0;
 120836         pPhrase->doclist.nList = 0;
 120837         return SQLITE_OK;
 120839       }else if( aPoslist==0 ){
 120840         aPoslist = pList;
 120841         nPoslist = nList;
 120843       }else{
 120844         char *aOut = pList;
 120845         char *p1 = aPoslist;
 120846         char *p2 = aOut;
 120848         assert( iPrev>=0 );
 120849         fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
 120850         sqlite3_free(aPoslist);
 120851         aPoslist = pList;
 120852         nPoslist = (int)(aOut - aPoslist);
 120853         if( nPoslist==0 ){
 120854           sqlite3_free(aPoslist);
 120855           pPhrase->doclist.pList = 0;
 120856           pPhrase->doclist.nList = 0;
 120857           return SQLITE_OK;
 120860       iPrev = iToken;
 120864   if( iPrev>=0 ){
 120865     int nMaxUndeferred = pPhrase->iDoclistToken;
 120866     if( nMaxUndeferred<0 ){
 120867       pPhrase->doclist.pList = aPoslist;
 120868       pPhrase->doclist.nList = nPoslist;
 120869       pPhrase->doclist.iDocid = pCsr->iPrevId;
 120870       pPhrase->doclist.bFreeList = 1;
 120871     }else{
 120872       int nDistance;
 120873       char *p1;
 120874       char *p2;
 120875       char *aOut;
 120877       if( nMaxUndeferred>iPrev ){
 120878         p1 = aPoslist;
 120879         p2 = pPhrase->doclist.pList;
 120880         nDistance = nMaxUndeferred - iPrev;
 120881       }else{
 120882         p1 = pPhrase->doclist.pList;
 120883         p2 = aPoslist;
 120884         nDistance = iPrev - nMaxUndeferred;
 120887       aOut = (char *)sqlite3_malloc(nPoslist+8);
 120888       if( !aOut ){
 120889         sqlite3_free(aPoslist);
 120890         return SQLITE_NOMEM;
 120893       pPhrase->doclist.pList = aOut;
 120894       if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
 120895         pPhrase->doclist.bFreeList = 1;
 120896         pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
 120897       }else{
 120898         sqlite3_free(aOut);
 120899         pPhrase->doclist.pList = 0;
 120900         pPhrase->doclist.nList = 0;
 120902       sqlite3_free(aPoslist);
 120906   return SQLITE_OK;
 120910 ** This function is called for each Fts3Phrase in a full-text query 
 120911 ** expression to initialize the mechanism for returning rows. Once this
 120912 ** function has been called successfully on an Fts3Phrase, it may be
 120913 ** used with fts3EvalPhraseNext() to iterate through the matching docids.
 120915 ** If parameter bOptOk is true, then the phrase may (or may not) use the
 120916 ** incremental loading strategy. Otherwise, the entire doclist is loaded into
 120917 ** memory within this call.
 120919 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 120921 static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
 120922   int rc;                         /* Error code */
 120923   Fts3PhraseToken *pFirst = &p->aToken[0];
 120924   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 120926   if( pCsr->bDesc==pTab->bDescIdx 
 120927    && bOptOk==1 
 120928    && p->nToken==1 
 120929    && pFirst->pSegcsr 
 120930    && pFirst->pSegcsr->bLookup 
 120931    && pFirst->bFirst==0
 120933     /* Use the incremental approach. */
 120934     int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
 120935     rc = sqlite3Fts3MsrIncrStart(
 120936         pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n);
 120937     p->bIncr = 1;
 120939   }else{
 120940     /* Load the full doclist for the phrase into memory. */
 120941     rc = fts3EvalPhraseLoad(pCsr, p);
 120942     p->bIncr = 0;
 120945   assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
 120946   return rc;
 120950 ** This function is used to iterate backwards (from the end to start) 
 120951 ** through doclists. It is used by this module to iterate through phrase
 120952 ** doclists in reverse and by the fts3_write.c module to iterate through
 120953 ** pending-terms lists when writing to databases with "order=desc".
 120955 ** The doclist may be sorted in ascending (parameter bDescIdx==0) or 
 120956 ** descending (parameter bDescIdx==1) order of docid. Regardless, this
 120957 ** function iterates from the end of the doclist to the beginning.
 120959 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
 120960   int bDescIdx,                   /* True if the doclist is desc */
 120961   char *aDoclist,                 /* Pointer to entire doclist */
 120962   int nDoclist,                   /* Length of aDoclist in bytes */
 120963   char **ppIter,                  /* IN/OUT: Iterator pointer */
 120964   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
 120965   int *pnList,                    /* OUT: List length pointer */
 120966   u8 *pbEof                       /* OUT: End-of-file flag */
 120968   char *p = *ppIter;
 120970   assert( nDoclist>0 );
 120971   assert( *pbEof==0 );
 120972   assert( p || *piDocid==0 );
 120973   assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
 120975   if( p==0 ){
 120976     sqlite3_int64 iDocid = 0;
 120977     char *pNext = 0;
 120978     char *pDocid = aDoclist;
 120979     char *pEnd = &aDoclist[nDoclist];
 120980     int iMul = 1;
 120982     while( pDocid<pEnd ){
 120983       sqlite3_int64 iDelta;
 120984       pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
 120985       iDocid += (iMul * iDelta);
 120986       pNext = pDocid;
 120987       fts3PoslistCopy(0, &pDocid);
 120988       while( pDocid<pEnd && *pDocid==0 ) pDocid++;
 120989       iMul = (bDescIdx ? -1 : 1);
 120992     *pnList = (int)(pEnd - pNext);
 120993     *ppIter = pNext;
 120994     *piDocid = iDocid;
 120995   }else{
 120996     int iMul = (bDescIdx ? -1 : 1);
 120997     sqlite3_int64 iDelta;
 120998     fts3GetReverseVarint(&p, aDoclist, &iDelta);
 120999     *piDocid -= (iMul * iDelta);
 121001     if( p==aDoclist ){
 121002       *pbEof = 1;
 121003     }else{
 121004       char *pSave = p;
 121005       fts3ReversePoslist(aDoclist, &p);
 121006       *pnList = (int)(pSave - p);
 121008     *ppIter = p;
 121013 ** Iterate forwards through a doclist.
 121015 SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
 121016   int bDescIdx,                   /* True if the doclist is desc */
 121017   char *aDoclist,                 /* Pointer to entire doclist */
 121018   int nDoclist,                   /* Length of aDoclist in bytes */
 121019   char **ppIter,                  /* IN/OUT: Iterator pointer */
 121020   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
 121021   u8 *pbEof                       /* OUT: End-of-file flag */
 121023   char *p = *ppIter;
 121025   assert( nDoclist>0 );
 121026   assert( *pbEof==0 );
 121027   assert( p || *piDocid==0 );
 121028   assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
 121030   if( p==0 ){
 121031     p = aDoclist;
 121032     p += sqlite3Fts3GetVarint(p, piDocid);
 121033   }else{
 121034     fts3PoslistCopy(0, &p);
 121035     if( p>=&aDoclist[nDoclist] ){
 121036       *pbEof = 1;
 121037     }else{
 121038       sqlite3_int64 iVar;
 121039       p += sqlite3Fts3GetVarint(p, &iVar);
 121040       *piDocid += ((bDescIdx ? -1 : 1) * iVar);
 121044   *ppIter = p;
 121048 ** Attempt to move the phrase iterator to point to the next matching docid. 
 121049 ** If an error occurs, return an SQLite error code. Otherwise, return 
 121050 ** SQLITE_OK.
 121052 ** If there is no "next" entry and no error occurs, then *pbEof is set to
 121053 ** 1 before returning. Otherwise, if no error occurs and the iterator is
 121054 ** successfully advanced, *pbEof is set to 0.
 121056 static int fts3EvalPhraseNext(
 121057   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 121058   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
 121059   u8 *pbEof                       /* OUT: Set to 1 if EOF */
 121061   int rc = SQLITE_OK;
 121062   Fts3Doclist *pDL = &p->doclist;
 121063   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 121065   if( p->bIncr ){
 121066     assert( p->nToken==1 );
 121067     assert( pDL->pNextDocid==0 );
 121068     rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, 
 121069         &pDL->iDocid, &pDL->pList, &pDL->nList
 121071     if( rc==SQLITE_OK && !pDL->pList ){
 121072       *pbEof = 1;
 121074   }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
 121075     sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll, 
 121076         &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
 121078     pDL->pList = pDL->pNextDocid;
 121079   }else{
 121080     char *pIter;                            /* Used to iterate through aAll */
 121081     char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
 121082     if( pDL->pNextDocid ){
 121083       pIter = pDL->pNextDocid;
 121084     }else{
 121085       pIter = pDL->aAll;
 121088     if( pIter>=pEnd ){
 121089       /* We have already reached the end of this doclist. EOF. */
 121090       *pbEof = 1;
 121091     }else{
 121092       sqlite3_int64 iDelta;
 121093       pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
 121094       if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
 121095         pDL->iDocid += iDelta;
 121096       }else{
 121097         pDL->iDocid -= iDelta;
 121099       pDL->pList = pIter;
 121100       fts3PoslistCopy(0, &pIter);
 121101       pDL->nList = (int)(pIter - pDL->pList);
 121103       /* pIter now points just past the 0x00 that terminates the position-
 121104       ** list for document pDL->iDocid. However, if this position-list was
 121105       ** edited in place by fts3EvalNearTrim(), then pIter may not actually
 121106       ** point to the start of the next docid value. The following line deals
 121107       ** with this case by advancing pIter past the zero-padding added by
 121108       ** fts3EvalNearTrim().  */
 121109       while( pIter<pEnd && *pIter==0 ) pIter++;
 121111       pDL->pNextDocid = pIter;
 121112       assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
 121113       *pbEof = 0;
 121117   return rc;
 121122 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 121123 ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
 121124 ** expression. Also the Fts3Expr.bDeferred variable is set to true for any
 121125 ** expressions for which all descendent tokens are deferred.
 121127 ** If parameter bOptOk is zero, then it is guaranteed that the
 121128 ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
 121129 ** each phrase in the expression (subject to deferred token processing).
 121130 ** Or, if bOptOk is non-zero, then one or more tokens within the expression
 121131 ** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
 121133 ** If an error occurs within this function, *pRc is set to an SQLite error
 121134 ** code before returning.
 121136 static void fts3EvalStartReaders(
 121137   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 121138   Fts3Expr *pExpr,                /* Expression to initialize phrases in */
 121139   int bOptOk,                     /* True to enable incremental loading */
 121140   int *pRc                        /* IN/OUT: Error code */
 121142   if( pExpr && SQLITE_OK==*pRc ){
 121143     if( pExpr->eType==FTSQUERY_PHRASE ){
 121144       int i;
 121145       int nToken = pExpr->pPhrase->nToken;
 121146       for(i=0; i<nToken; i++){
 121147         if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
 121149       pExpr->bDeferred = (i==nToken);
 121150       *pRc = fts3EvalPhraseStart(pCsr, bOptOk, pExpr->pPhrase);
 121151     }else{
 121152       fts3EvalStartReaders(pCsr, pExpr->pLeft, bOptOk, pRc);
 121153       fts3EvalStartReaders(pCsr, pExpr->pRight, bOptOk, pRc);
 121154       pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
 121160 ** An array of the following structures is assembled as part of the process
 121161 ** of selecting tokens to defer before the query starts executing (as part
 121162 ** of the xFilter() method). There is one element in the array for each
 121163 ** token in the FTS expression.
 121165 ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
 121166 ** to phrases that are connected only by AND and NEAR operators (not OR or
 121167 ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
 121168 ** separately. The root of a tokens AND/NEAR cluster is stored in 
 121169 ** Fts3TokenAndCost.pRoot.
 121171 typedef struct Fts3TokenAndCost Fts3TokenAndCost;
 121172 struct Fts3TokenAndCost {
 121173   Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
 121174   int iToken;                     /* Position of token in phrase */
 121175   Fts3PhraseToken *pToken;        /* The token itself */
 121176   Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
 121177   int nOvfl;                      /* Number of overflow pages to load doclist */
 121178   int iCol;                       /* The column the token must match */
 121182 ** This function is used to populate an allocated Fts3TokenAndCost array.
 121184 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 121185 ** Otherwise, if an error occurs during execution, *pRc is set to an
 121186 ** SQLite error code.
 121188 static void fts3EvalTokenCosts(
 121189   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 121190   Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
 121191   Fts3Expr *pExpr,                /* Expression to consider */
 121192   Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
 121193   Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
 121194   int *pRc                        /* IN/OUT: Error code */
 121196   if( *pRc==SQLITE_OK ){
 121197     if( pExpr->eType==FTSQUERY_PHRASE ){
 121198       Fts3Phrase *pPhrase = pExpr->pPhrase;
 121199       int i;
 121200       for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
 121201         Fts3TokenAndCost *pTC = (*ppTC)++;
 121202         pTC->pPhrase = pPhrase;
 121203         pTC->iToken = i;
 121204         pTC->pRoot = pRoot;
 121205         pTC->pToken = &pPhrase->aToken[i];
 121206         pTC->iCol = pPhrase->iColumn;
 121207         *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
 121209     }else if( pExpr->eType!=FTSQUERY_NOT ){
 121210       assert( pExpr->eType==FTSQUERY_OR
 121211            || pExpr->eType==FTSQUERY_AND
 121212            || pExpr->eType==FTSQUERY_NEAR
 121214       assert( pExpr->pLeft && pExpr->pRight );
 121215       if( pExpr->eType==FTSQUERY_OR ){
 121216         pRoot = pExpr->pLeft;
 121217         **ppOr = pRoot;
 121218         (*ppOr)++;
 121220       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
 121221       if( pExpr->eType==FTSQUERY_OR ){
 121222         pRoot = pExpr->pRight;
 121223         **ppOr = pRoot;
 121224         (*ppOr)++;
 121226       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
 121232 ** Determine the average document (row) size in pages. If successful,
 121233 ** write this value to *pnPage and return SQLITE_OK. Otherwise, return
 121234 ** an SQLite error code.
 121236 ** The average document size in pages is calculated by first calculating 
 121237 ** determining the average size in bytes, B. If B is less than the amount
 121238 ** of data that will fit on a single leaf page of an intkey table in
 121239 ** this database, then the average docsize is 1. Otherwise, it is 1 plus
 121240 ** the number of overflow pages consumed by a record B bytes in size.
 121242 static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
 121243   if( pCsr->nRowAvg==0 ){
 121244     /* The average document size, which is required to calculate the cost
 121245     ** of each doclist, has not yet been determined. Read the required 
 121246     ** data from the %_stat table to calculate it.
 121248     ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 
 121249     ** varints, where nCol is the number of columns in the FTS3 table.
 121250     ** The first varint is the number of documents currently stored in
 121251     ** the table. The following nCol varints contain the total amount of
 121252     ** data stored in all rows of each column of the table, from left
 121253     ** to right.
 121255     int rc;
 121256     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
 121257     sqlite3_stmt *pStmt;
 121258     sqlite3_int64 nDoc = 0;
 121259     sqlite3_int64 nByte = 0;
 121260     const char *pEnd;
 121261     const char *a;
 121263     rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
 121264     if( rc!=SQLITE_OK ) return rc;
 121265     a = sqlite3_column_blob(pStmt, 0);
 121266     assert( a );
 121268     pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
 121269     a += sqlite3Fts3GetVarint(a, &nDoc);
 121270     while( a<pEnd ){
 121271       a += sqlite3Fts3GetVarint(a, &nByte);
 121273     if( nDoc==0 || nByte==0 ){
 121274       sqlite3_reset(pStmt);
 121275       return FTS_CORRUPT_VTAB;
 121278     pCsr->nDoc = nDoc;
 121279     pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
 121280     assert( pCsr->nRowAvg>0 ); 
 121281     rc = sqlite3_reset(pStmt);
 121282     if( rc!=SQLITE_OK ) return rc;
 121285   *pnPage = pCsr->nRowAvg;
 121286   return SQLITE_OK;
 121290 ** This function is called to select the tokens (if any) that will be 
 121291 ** deferred. The array aTC[] has already been populated when this is
 121292 ** called.
 121294 ** This function is called once for each AND/NEAR cluster in the 
 121295 ** expression. Each invocation determines which tokens to defer within
 121296 ** the cluster with root node pRoot. See comments above the definition
 121297 ** of struct Fts3TokenAndCost for more details.
 121299 ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
 121300 ** called on each token to defer. Otherwise, an SQLite error code is
 121301 ** returned.
 121303 static int fts3EvalSelectDeferred(
 121304   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 121305   Fts3Expr *pRoot,                /* Consider tokens with this root node */
 121306   Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
 121307   int nTC                         /* Number of entries in aTC[] */
 121309   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 121310   int nDocSize = 0;               /* Number of pages per doc loaded */
 121311   int rc = SQLITE_OK;             /* Return code */
 121312   int ii;                         /* Iterator variable for various purposes */
 121313   int nOvfl = 0;                  /* Total overflow pages used by doclists */
 121314   int nToken = 0;                 /* Total number of tokens in cluster */
 121316   int nMinEst = 0;                /* The minimum count for any phrase so far. */
 121317   int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
 121319   /* Tokens are never deferred for FTS tables created using the content=xxx
 121320   ** option. The reason being that it is not guaranteed that the content
 121321   ** table actually contains the same data as the index. To prevent this from
 121322   ** causing any problems, the deferred token optimization is completely
 121323   ** disabled for content=xxx tables. */
 121324   if( pTab->zContentTbl ){
 121325     return SQLITE_OK;
 121328   /* Count the tokens in this AND/NEAR cluster. If none of the doclists
 121329   ** associated with the tokens spill onto overflow pages, or if there is
 121330   ** only 1 token, exit early. No tokens to defer in this case. */
 121331   for(ii=0; ii<nTC; ii++){
 121332     if( aTC[ii].pRoot==pRoot ){
 121333       nOvfl += aTC[ii].nOvfl;
 121334       nToken++;
 121337   if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
 121339   /* Obtain the average docsize (in pages). */
 121340   rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
 121341   assert( rc!=SQLITE_OK || nDocSize>0 );
 121344   /* Iterate through all tokens in this AND/NEAR cluster, in ascending order 
 121345   ** of the number of overflow pages that will be loaded by the pager layer 
 121346   ** to retrieve the entire doclist for the token from the full-text index.
 121347   ** Load the doclists for tokens that are either:
 121349   **   a. The cheapest token in the entire query (i.e. the one visited by the
 121350   **      first iteration of this loop), or
 121352   **   b. Part of a multi-token phrase.
 121354   ** After each token doclist is loaded, merge it with the others from the
 121355   ** same phrase and count the number of documents that the merged doclist
 121356   ** contains. Set variable "nMinEst" to the smallest number of documents in 
 121357   ** any phrase doclist for which 1 or more token doclists have been loaded.
 121358   ** Let nOther be the number of other phrases for which it is certain that
 121359   ** one or more tokens will not be deferred.
 121361   ** Then, for each token, defer it if loading the doclist would result in
 121362   ** loading N or more overflow pages into memory, where N is computed as:
 121364   **    (nMinEst + 4^nOther - 1) / (4^nOther)
 121366   for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
 121367     int iTC;                      /* Used to iterate through aTC[] array. */
 121368     Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
 121370     /* Set pTC to point to the cheapest remaining token. */
 121371     for(iTC=0; iTC<nTC; iTC++){
 121372       if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot 
 121373        && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl) 
 121375         pTC = &aTC[iTC];
 121378     assert( pTC );
 121380     if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
 121381       /* The number of overflow pages to load for this (and therefore all
 121382       ** subsequent) tokens is greater than the estimated number of pages 
 121383       ** that will be loaded if all subsequent tokens are deferred.
 121385       Fts3PhraseToken *pToken = pTC->pToken;
 121386       rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
 121387       fts3SegReaderCursorFree(pToken->pSegcsr);
 121388       pToken->pSegcsr = 0;
 121389     }else{
 121390       /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
 121391       ** for-loop. Except, limit the value to 2^24 to prevent it from 
 121392       ** overflowing the 32-bit integer it is stored in. */
 121393       if( ii<12 ) nLoad4 = nLoad4*4;
 121395       if( ii==0 || pTC->pPhrase->nToken>1 ){
 121396         /* Either this is the cheapest token in the entire query, or it is
 121397         ** part of a multi-token phrase. Either way, the entire doclist will
 121398         ** (eventually) be loaded into memory. It may as well be now. */
 121399         Fts3PhraseToken *pToken = pTC->pToken;
 121400         int nList = 0;
 121401         char *pList = 0;
 121402         rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
 121403         assert( rc==SQLITE_OK || pList==0 );
 121404         if( rc==SQLITE_OK ){
 121405           int nCount;
 121406           fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
 121407           nCount = fts3DoclistCountDocids(
 121408               pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
 121410           if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
 121414     pTC->pToken = 0;
 121417   return rc;
 121421 ** This function is called from within the xFilter method. It initializes
 121422 ** the full-text query currently stored in pCsr->pExpr. To iterate through
 121423 ** the results of a query, the caller does:
 121425 **    fts3EvalStart(pCsr);
 121426 **    while( 1 ){
 121427 **      fts3EvalNext(pCsr);
 121428 **      if( pCsr->bEof ) break;
 121429 **      ... return row pCsr->iPrevId to the caller ...
 121430 **    }
 121432 static int fts3EvalStart(Fts3Cursor *pCsr){
 121433   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 121434   int rc = SQLITE_OK;
 121435   int nToken = 0;
 121436   int nOr = 0;
 121438   /* Allocate a MultiSegReader for each token in the expression. */
 121439   fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
 121441   /* Determine which, if any, tokens in the expression should be deferred. */
 121442 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 121443   if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
 121444     Fts3TokenAndCost *aTC;
 121445     Fts3Expr **apOr;
 121446     aTC = (Fts3TokenAndCost *)sqlite3_malloc(
 121447         sizeof(Fts3TokenAndCost) * nToken
 121448       + sizeof(Fts3Expr *) * nOr * 2
 121450     apOr = (Fts3Expr **)&aTC[nToken];
 121452     if( !aTC ){
 121453       rc = SQLITE_NOMEM;
 121454     }else{
 121455       int ii;
 121456       Fts3TokenAndCost *pTC = aTC;
 121457       Fts3Expr **ppOr = apOr;
 121459       fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
 121460       nToken = (int)(pTC-aTC);
 121461       nOr = (int)(ppOr-apOr);
 121463       if( rc==SQLITE_OK ){
 121464         rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
 121465         for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
 121466           rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
 121470       sqlite3_free(aTC);
 121473 #endif
 121475   fts3EvalStartReaders(pCsr, pCsr->pExpr, 1, &rc);
 121476   return rc;
 121480 ** Invalidate the current position list for phrase pPhrase.
 121482 static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
 121483   if( pPhrase->doclist.bFreeList ){
 121484     sqlite3_free(pPhrase->doclist.pList);
 121486   pPhrase->doclist.pList = 0;
 121487   pPhrase->doclist.nList = 0;
 121488   pPhrase->doclist.bFreeList = 0;
 121492 ** This function is called to edit the position list associated with
 121493 ** the phrase object passed as the fifth argument according to a NEAR
 121494 ** condition. For example:
 121496 **     abc NEAR/5 "def ghi"
 121498 ** Parameter nNear is passed the NEAR distance of the expression (5 in
 121499 ** the example above). When this function is called, *paPoslist points to
 121500 ** the position list, and *pnToken is the number of phrase tokens in, the
 121501 ** phrase on the other side of the NEAR operator to pPhrase. For example,
 121502 ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
 121503 ** the position list associated with phrase "abc".
 121505 ** All positions in the pPhrase position list that are not sufficiently
 121506 ** close to a position in the *paPoslist position list are removed. If this
 121507 ** leaves 0 positions, zero is returned. Otherwise, non-zero.
 121509 ** Before returning, *paPoslist is set to point to the position lsit 
 121510 ** associated with pPhrase. And *pnToken is set to the number of tokens in
 121511 ** pPhrase.
 121513 static int fts3EvalNearTrim(
 121514   int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
 121515   char *aTmp,                     /* Temporary space to use */
 121516   char **paPoslist,               /* IN/OUT: Position list */
 121517   int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
 121518   Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
 121520   int nParam1 = nNear + pPhrase->nToken;
 121521   int nParam2 = nNear + *pnToken;
 121522   int nNew;
 121523   char *p2; 
 121524   char *pOut; 
 121525   int res;
 121527   assert( pPhrase->doclist.pList );
 121529   p2 = pOut = pPhrase->doclist.pList;
 121530   res = fts3PoslistNearMerge(
 121531     &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
 121533   if( res ){
 121534     nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
 121535     assert( pPhrase->doclist.pList[nNew]=='\0' );
 121536     assert( nNew<=pPhrase->doclist.nList && nNew>0 );
 121537     memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
 121538     pPhrase->doclist.nList = nNew;
 121539     *paPoslist = pPhrase->doclist.pList;
 121540     *pnToken = pPhrase->nToken;
 121543   return res;
 121547 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
 121548 ** Otherwise, it advances the expression passed as the second argument to
 121549 ** point to the next matching row in the database. Expressions iterate through
 121550 ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
 121551 ** or descending if it is non-zero.
 121553 ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
 121554 ** successful, the following variables in pExpr are set:
 121556 **   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
 121557 **   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
 121559 ** If the expression is of type FTSQUERY_PHRASE, and the expression is not
 121560 ** at EOF, then the following variables are populated with the position list
 121561 ** for the phrase for the visited row:
 121563 **   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
 121564 **   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
 121566 ** It says above that this function advances the expression to the next
 121567 ** matching row. This is usually true, but there are the following exceptions:
 121569 **   1. Deferred tokens are not taken into account. If a phrase consists
 121570 **      entirely of deferred tokens, it is assumed to match every row in
 121571 **      the db. In this case the position-list is not populated at all. 
 121573 **      Or, if a phrase contains one or more deferred tokens and one or
 121574 **      more non-deferred tokens, then the expression is advanced to the 
 121575 **      next possible match, considering only non-deferred tokens. In other
 121576 **      words, if the phrase is "A B C", and "B" is deferred, the expression
 121577 **      is advanced to the next row that contains an instance of "A * C", 
 121578 **      where "*" may match any single token. The position list in this case
 121579 **      is populated as for "A * C" before returning.
 121581 **   2. NEAR is treated as AND. If the expression is "x NEAR y", it is 
 121582 **      advanced to point to the next row that matches "x AND y".
 121584 ** See fts3EvalTestDeferredAndNear() for details on testing if a row is
 121585 ** really a match, taking into account deferred tokens and NEAR operators.
 121587 static void fts3EvalNextRow(
 121588   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 121589   Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
 121590   int *pRc                        /* IN/OUT: Error code */
 121592   if( *pRc==SQLITE_OK ){
 121593     int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
 121594     assert( pExpr->bEof==0 );
 121595     pExpr->bStart = 1;
 121597     switch( pExpr->eType ){
 121598       case FTSQUERY_NEAR:
 121599       case FTSQUERY_AND: {
 121600         Fts3Expr *pLeft = pExpr->pLeft;
 121601         Fts3Expr *pRight = pExpr->pRight;
 121602         assert( !pLeft->bDeferred || !pRight->bDeferred );
 121604         if( pLeft->bDeferred ){
 121605           /* LHS is entirely deferred. So we assume it matches every row.
 121606           ** Advance the RHS iterator to find the next row visited. */
 121607           fts3EvalNextRow(pCsr, pRight, pRc);
 121608           pExpr->iDocid = pRight->iDocid;
 121609           pExpr->bEof = pRight->bEof;
 121610         }else if( pRight->bDeferred ){
 121611           /* RHS is entirely deferred. So we assume it matches every row.
 121612           ** Advance the LHS iterator to find the next row visited. */
 121613           fts3EvalNextRow(pCsr, pLeft, pRc);
 121614           pExpr->iDocid = pLeft->iDocid;
 121615           pExpr->bEof = pLeft->bEof;
 121616         }else{
 121617           /* Neither the RHS or LHS are deferred. */
 121618           fts3EvalNextRow(pCsr, pLeft, pRc);
 121619           fts3EvalNextRow(pCsr, pRight, pRc);
 121620           while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
 121621             sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
 121622             if( iDiff==0 ) break;
 121623             if( iDiff<0 ){
 121624               fts3EvalNextRow(pCsr, pLeft, pRc);
 121625             }else{
 121626               fts3EvalNextRow(pCsr, pRight, pRc);
 121629           pExpr->iDocid = pLeft->iDocid;
 121630           pExpr->bEof = (pLeft->bEof || pRight->bEof);
 121632         break;
 121635       case FTSQUERY_OR: {
 121636         Fts3Expr *pLeft = pExpr->pLeft;
 121637         Fts3Expr *pRight = pExpr->pRight;
 121638         sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
 121640         assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
 121641         assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
 121643         if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
 121644           fts3EvalNextRow(pCsr, pLeft, pRc);
 121645         }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
 121646           fts3EvalNextRow(pCsr, pRight, pRc);
 121647         }else{
 121648           fts3EvalNextRow(pCsr, pLeft, pRc);
 121649           fts3EvalNextRow(pCsr, pRight, pRc);
 121652         pExpr->bEof = (pLeft->bEof && pRight->bEof);
 121653         iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
 121654         if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
 121655           pExpr->iDocid = pLeft->iDocid;
 121656         }else{
 121657           pExpr->iDocid = pRight->iDocid;
 121660         break;
 121663       case FTSQUERY_NOT: {
 121664         Fts3Expr *pLeft = pExpr->pLeft;
 121665         Fts3Expr *pRight = pExpr->pRight;
 121667         if( pRight->bStart==0 ){
 121668           fts3EvalNextRow(pCsr, pRight, pRc);
 121669           assert( *pRc!=SQLITE_OK || pRight->bStart );
 121672         fts3EvalNextRow(pCsr, pLeft, pRc);
 121673         if( pLeft->bEof==0 ){
 121674           while( !*pRc 
 121675               && !pRight->bEof 
 121676               && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0 
 121678             fts3EvalNextRow(pCsr, pRight, pRc);
 121681         pExpr->iDocid = pLeft->iDocid;
 121682         pExpr->bEof = pLeft->bEof;
 121683         break;
 121686       default: {
 121687         Fts3Phrase *pPhrase = pExpr->pPhrase;
 121688         fts3EvalInvalidatePoslist(pPhrase);
 121689         *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
 121690         pExpr->iDocid = pPhrase->doclist.iDocid;
 121691         break;
 121698 ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
 121699 ** cluster, then this function returns 1 immediately.
 121701 ** Otherwise, it checks if the current row really does match the NEAR 
 121702 ** expression, using the data currently stored in the position lists 
 121703 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression. 
 121705 ** If the current row is a match, the position list associated with each
 121706 ** phrase in the NEAR expression is edited in place to contain only those
 121707 ** phrase instances sufficiently close to their peers to satisfy all NEAR
 121708 ** constraints. In this case it returns 1. If the NEAR expression does not 
 121709 ** match the current row, 0 is returned. The position lists may or may not
 121710 ** be edited if 0 is returned.
 121712 static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
 121713   int res = 1;
 121715   /* The following block runs if pExpr is the root of a NEAR query.
 121716   ** For example, the query:
 121718   **         "w" NEAR "x" NEAR "y" NEAR "z"
 121720   ** which is represented in tree form as:
 121722   **                               |
 121723   **                          +--NEAR--+      <-- root of NEAR query
 121724   **                          |        |
 121725   **                     +--NEAR--+   "z"
 121726   **                     |        |
 121727   **                +--NEAR--+   "y"
 121728   **                |        |
 121729   **               "w"      "x"
 121731   ** The right-hand child of a NEAR node is always a phrase. The 
 121732   ** left-hand child may be either a phrase or a NEAR node. There are
 121733   ** no exceptions to this - it's the way the parser in fts3_expr.c works.
 121735   if( *pRc==SQLITE_OK 
 121736    && pExpr->eType==FTSQUERY_NEAR 
 121737    && pExpr->bEof==0
 121738    && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
 121740     Fts3Expr *p; 
 121741     int nTmp = 0;                 /* Bytes of temp space */
 121742     char *aTmp;                   /* Temp space for PoslistNearMerge() */
 121744     /* Allocate temporary working space. */
 121745     for(p=pExpr; p->pLeft; p=p->pLeft){
 121746       nTmp += p->pRight->pPhrase->doclist.nList;
 121748     nTmp += p->pPhrase->doclist.nList;
 121749     if( nTmp==0 ){
 121750       res = 0;
 121751     }else{
 121752       aTmp = sqlite3_malloc(nTmp*2);
 121753       if( !aTmp ){
 121754         *pRc = SQLITE_NOMEM;
 121755         res = 0;
 121756       }else{
 121757         char *aPoslist = p->pPhrase->doclist.pList;
 121758         int nToken = p->pPhrase->nToken;
 121760         for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
 121761           Fts3Phrase *pPhrase = p->pRight->pPhrase;
 121762           int nNear = p->nNear;
 121763           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 121766         aPoslist = pExpr->pRight->pPhrase->doclist.pList;
 121767         nToken = pExpr->pRight->pPhrase->nToken;
 121768         for(p=pExpr->pLeft; p && res; p=p->pLeft){
 121769           int nNear;
 121770           Fts3Phrase *pPhrase;
 121771           assert( p->pParent && p->pParent->pLeft==p );
 121772           nNear = p->pParent->nNear;
 121773           pPhrase = (
 121774               p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
 121776           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 121780       sqlite3_free(aTmp);
 121784   return res;
 121788 ** This function is a helper function for fts3EvalTestDeferredAndNear().
 121789 ** Assuming no error occurs or has occurred, It returns non-zero if the
 121790 ** expression passed as the second argument matches the row that pCsr 
 121791 ** currently points to, or zero if it does not.
 121793 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 121794 ** If an error occurs during execution of this function, *pRc is set to 
 121795 ** the appropriate SQLite error code. In this case the returned value is 
 121796 ** undefined.
 121798 static int fts3EvalTestExpr(
 121799   Fts3Cursor *pCsr,               /* FTS cursor handle */
 121800   Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
 121801   int *pRc                        /* IN/OUT: Error code */
 121803   int bHit = 1;                   /* Return value */
 121804   if( *pRc==SQLITE_OK ){
 121805     switch( pExpr->eType ){
 121806       case FTSQUERY_NEAR:
 121807       case FTSQUERY_AND:
 121808         bHit = (
 121809             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
 121810          && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
 121811          && fts3EvalNearTest(pExpr, pRc)
 121814         /* If the NEAR expression does not match any rows, zero the doclist for 
 121815         ** all phrases involved in the NEAR. This is because the snippet(),
 121816         ** offsets() and matchinfo() functions are not supposed to recognize 
 121817         ** any instances of phrases that are part of unmatched NEAR queries. 
 121818         ** For example if this expression:
 121820         **    ... MATCH 'a OR (b NEAR c)'
 121822         ** is matched against a row containing:
 121824         **        'a b d e'
 121826         ** then any snippet() should ony highlight the "a" term, not the "b"
 121827         ** (as "b" is part of a non-matching NEAR clause).
 121829         if( bHit==0 
 121830          && pExpr->eType==FTSQUERY_NEAR 
 121831          && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
 121833           Fts3Expr *p;
 121834           for(p=pExpr; p->pPhrase==0; p=p->pLeft){
 121835             if( p->pRight->iDocid==pCsr->iPrevId ){
 121836               fts3EvalInvalidatePoslist(p->pRight->pPhrase);
 121839           if( p->iDocid==pCsr->iPrevId ){
 121840             fts3EvalInvalidatePoslist(p->pPhrase);
 121844         break;
 121846       case FTSQUERY_OR: {
 121847         int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
 121848         int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
 121849         bHit = bHit1 || bHit2;
 121850         break;
 121853       case FTSQUERY_NOT:
 121854         bHit = (
 121855             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
 121856          && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
 121858         break;
 121860       default: {
 121861 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 121862         if( pCsr->pDeferred 
 121863          && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
 121865           Fts3Phrase *pPhrase = pExpr->pPhrase;
 121866           assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
 121867           if( pExpr->bDeferred ){
 121868             fts3EvalInvalidatePoslist(pPhrase);
 121870           *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
 121871           bHit = (pPhrase->doclist.pList!=0);
 121872           pExpr->iDocid = pCsr->iPrevId;
 121873         }else
 121874 #endif
 121876           bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
 121878         break;
 121882   return bHit;
 121886 ** This function is called as the second part of each xNext operation when
 121887 ** iterating through the results of a full-text query. At this point the
 121888 ** cursor points to a row that matches the query expression, with the
 121889 ** following caveats:
 121891 **   * Up until this point, "NEAR" operators in the expression have been
 121892 **     treated as "AND".
 121894 **   * Deferred tokens have not yet been considered.
 121896 ** If *pRc is not SQLITE_OK when this function is called, it immediately
 121897 ** returns 0. Otherwise, it tests whether or not after considering NEAR
 121898 ** operators and deferred tokens the current row is still a match for the
 121899 ** expression. It returns 1 if both of the following are true:
 121901 **   1. *pRc is SQLITE_OK when this function returns, and
 121903 **   2. After scanning the current FTS table row for the deferred tokens,
 121904 **      it is determined that the row does *not* match the query.
 121906 ** Or, if no error occurs and it seems the current row does match the FTS
 121907 ** query, return 0.
 121909 static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
 121910   int rc = *pRc;
 121911   int bMiss = 0;
 121912   if( rc==SQLITE_OK ){
 121914     /* If there are one or more deferred tokens, load the current row into
 121915     ** memory and scan it to determine the position list for each deferred
 121916     ** token. Then, see if this row is really a match, considering deferred
 121917     ** tokens and NEAR operators (neither of which were taken into account
 121918     ** earlier, by fts3EvalNextRow()). 
 121920     if( pCsr->pDeferred ){
 121921       rc = fts3CursorSeek(0, pCsr);
 121922       if( rc==SQLITE_OK ){
 121923         rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
 121926     bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
 121928     /* Free the position-lists accumulated for each deferred token above. */
 121929     sqlite3Fts3FreeDeferredDoclists(pCsr);
 121930     *pRc = rc;
 121932   return (rc==SQLITE_OK && bMiss);
 121936 ** Advance to the next document that matches the FTS expression in
 121937 ** Fts3Cursor.pExpr.
 121939 static int fts3EvalNext(Fts3Cursor *pCsr){
 121940   int rc = SQLITE_OK;             /* Return Code */
 121941   Fts3Expr *pExpr = pCsr->pExpr;
 121942   assert( pCsr->isEof==0 );
 121943   if( pExpr==0 ){
 121944     pCsr->isEof = 1;
 121945   }else{
 121946     do {
 121947       if( pCsr->isRequireSeek==0 ){
 121948         sqlite3_reset(pCsr->pStmt);
 121950       assert( sqlite3_data_count(pCsr->pStmt)==0 );
 121951       fts3EvalNextRow(pCsr, pExpr, &rc);
 121952       pCsr->isEof = pExpr->bEof;
 121953       pCsr->isRequireSeek = 1;
 121954       pCsr->isMatchinfoNeeded = 1;
 121955       pCsr->iPrevId = pExpr->iDocid;
 121956     }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
 121958   return rc;
 121962 ** Restart interation for expression pExpr so that the next call to
 121963 ** fts3EvalNext() visits the first row. Do not allow incremental 
 121964 ** loading or merging of phrase doclists for this iteration.
 121966 ** If *pRc is other than SQLITE_OK when this function is called, it is
 121967 ** a no-op. If an error occurs within this function, *pRc is set to an
 121968 ** SQLite error code before returning.
 121970 static void fts3EvalRestart(
 121971   Fts3Cursor *pCsr,
 121972   Fts3Expr *pExpr,
 121973   int *pRc
 121975   if( pExpr && *pRc==SQLITE_OK ){
 121976     Fts3Phrase *pPhrase = pExpr->pPhrase;
 121978     if( pPhrase ){
 121979       fts3EvalInvalidatePoslist(pPhrase);
 121980       if( pPhrase->bIncr ){
 121981         assert( pPhrase->nToken==1 );
 121982         assert( pPhrase->aToken[0].pSegcsr );
 121983         sqlite3Fts3MsrIncrRestart(pPhrase->aToken[0].pSegcsr);
 121984         *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
 121987       pPhrase->doclist.pNextDocid = 0;
 121988       pPhrase->doclist.iDocid = 0;
 121991     pExpr->iDocid = 0;
 121992     pExpr->bEof = 0;
 121993     pExpr->bStart = 0;
 121995     fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
 121996     fts3EvalRestart(pCsr, pExpr->pRight, pRc);
 122001 ** After allocating the Fts3Expr.aMI[] array for each phrase in the 
 122002 ** expression rooted at pExpr, the cursor iterates through all rows matched
 122003 ** by pExpr, calling this function for each row. This function increments
 122004 ** the values in Fts3Expr.aMI[] according to the position-list currently
 122005 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase 
 122006 ** expression nodes.
 122008 static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
 122009   if( pExpr ){
 122010     Fts3Phrase *pPhrase = pExpr->pPhrase;
 122011     if( pPhrase && pPhrase->doclist.pList ){
 122012       int iCol = 0;
 122013       char *p = pPhrase->doclist.pList;
 122015       assert( *p );
 122016       while( 1 ){
 122017         u8 c = 0;
 122018         int iCnt = 0;
 122019         while( 0xFE & (*p | c) ){
 122020           if( (c&0x80)==0 ) iCnt++;
 122021           c = *p++ & 0x80;
 122024         /* aMI[iCol*3 + 1] = Number of occurrences
 122025         ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
 122027         pExpr->aMI[iCol*3 + 1] += iCnt;
 122028         pExpr->aMI[iCol*3 + 2] += (iCnt>0);
 122029         if( *p==0x00 ) break;
 122030         p++;
 122031         p += sqlite3Fts3GetVarint32(p, &iCol);
 122035     fts3EvalUpdateCounts(pExpr->pLeft);
 122036     fts3EvalUpdateCounts(pExpr->pRight);
 122041 ** Expression pExpr must be of type FTSQUERY_PHRASE.
 122043 ** If it is not already allocated and populated, this function allocates and
 122044 ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
 122045 ** of a NEAR expression, then it also allocates and populates the same array
 122046 ** for all other phrases that are part of the NEAR expression.
 122048 ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
 122049 ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
 122051 static int fts3EvalGatherStats(
 122052   Fts3Cursor *pCsr,               /* Cursor object */
 122053   Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
 122055   int rc = SQLITE_OK;             /* Return code */
 122057   assert( pExpr->eType==FTSQUERY_PHRASE );
 122058   if( pExpr->aMI==0 ){
 122059     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 122060     Fts3Expr *pRoot;                /* Root of NEAR expression */
 122061     Fts3Expr *p;                    /* Iterator used for several purposes */
 122063     sqlite3_int64 iPrevId = pCsr->iPrevId;
 122064     sqlite3_int64 iDocid;
 122065     u8 bEof;
 122067     /* Find the root of the NEAR expression */
 122068     pRoot = pExpr;
 122069     while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
 122070       pRoot = pRoot->pParent;
 122072     iDocid = pRoot->iDocid;
 122073     bEof = pRoot->bEof;
 122074     assert( pRoot->bStart );
 122076     /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
 122077     for(p=pRoot; p; p=p->pLeft){
 122078       Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
 122079       assert( pE->aMI==0 );
 122080       pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
 122081       if( !pE->aMI ) return SQLITE_NOMEM;
 122082       memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
 122085     fts3EvalRestart(pCsr, pRoot, &rc);
 122087     while( pCsr->isEof==0 && rc==SQLITE_OK ){
 122089       do {
 122090         /* Ensure the %_content statement is reset. */
 122091         if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
 122092         assert( sqlite3_data_count(pCsr->pStmt)==0 );
 122094         /* Advance to the next document */
 122095         fts3EvalNextRow(pCsr, pRoot, &rc);
 122096         pCsr->isEof = pRoot->bEof;
 122097         pCsr->isRequireSeek = 1;
 122098         pCsr->isMatchinfoNeeded = 1;
 122099         pCsr->iPrevId = pRoot->iDocid;
 122100       }while( pCsr->isEof==0 
 122101            && pRoot->eType==FTSQUERY_NEAR 
 122102            && fts3EvalTestDeferredAndNear(pCsr, &rc) 
 122105       if( rc==SQLITE_OK && pCsr->isEof==0 ){
 122106         fts3EvalUpdateCounts(pRoot);
 122110     pCsr->isEof = 0;
 122111     pCsr->iPrevId = iPrevId;
 122113     if( bEof ){
 122114       pRoot->bEof = bEof;
 122115     }else{
 122116       /* Caution: pRoot may iterate through docids in ascending or descending
 122117       ** order. For this reason, even though it seems more defensive, the 
 122118       ** do loop can not be written:
 122120       **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
 122122       fts3EvalRestart(pCsr, pRoot, &rc);
 122123       do {
 122124         fts3EvalNextRow(pCsr, pRoot, &rc);
 122125         assert( pRoot->bEof==0 );
 122126       }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
 122127       fts3EvalTestDeferredAndNear(pCsr, &rc);
 122130   return rc;
 122134 ** This function is used by the matchinfo() module to query a phrase 
 122135 ** expression node for the following information:
 122137 **   1. The total number of occurrences of the phrase in each column of 
 122138 **      the FTS table (considering all rows), and
 122140 **   2. For each column, the number of rows in the table for which the
 122141 **      column contains at least one instance of the phrase.
 122143 ** If no error occurs, SQLITE_OK is returned and the values for each column
 122144 ** written into the array aiOut as follows:
 122146 **   aiOut[iCol*3 + 1] = Number of occurrences
 122147 **   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
 122149 ** Caveats:
 122151 **   * If a phrase consists entirely of deferred tokens, then all output 
 122152 **     values are set to the number of documents in the table. In other
 122153 **     words we assume that very common tokens occur exactly once in each 
 122154 **     column of each row of the table.
 122156 **   * If a phrase contains some deferred tokens (and some non-deferred 
 122157 **     tokens), count the potential occurrence identified by considering
 122158 **     the non-deferred tokens instead of actual phrase occurrences.
 122160 **   * If the phrase is part of a NEAR expression, then only phrase instances
 122161 **     that meet the NEAR constraint are included in the counts.
 122163 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
 122164   Fts3Cursor *pCsr,               /* FTS cursor handle */
 122165   Fts3Expr *pExpr,                /* Phrase expression */
 122166   u32 *aiOut                      /* Array to write results into (see above) */
 122168   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 122169   int rc = SQLITE_OK;
 122170   int iCol;
 122172   if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
 122173     assert( pCsr->nDoc>0 );
 122174     for(iCol=0; iCol<pTab->nColumn; iCol++){
 122175       aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
 122176       aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
 122178   }else{
 122179     rc = fts3EvalGatherStats(pCsr, pExpr);
 122180     if( rc==SQLITE_OK ){
 122181       assert( pExpr->aMI );
 122182       for(iCol=0; iCol<pTab->nColumn; iCol++){
 122183         aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
 122184         aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
 122189   return rc;
 122193 ** The expression pExpr passed as the second argument to this function
 122194 ** must be of type FTSQUERY_PHRASE. 
 122196 ** The returned value is either NULL or a pointer to a buffer containing
 122197 ** a position-list indicating the occurrences of the phrase in column iCol
 122198 ** of the current row. 
 122200 ** More specifically, the returned buffer contains 1 varint for each 
 122201 ** occurence of the phrase in the column, stored using the normal (delta+2) 
 122202 ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
 122203 ** if the requested column contains "a b X c d X X" and the position-list
 122204 ** for 'X' is requested, the buffer returned may contain:
 122206 **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
 122208 ** This function works regardless of whether or not the phrase is deferred,
 122209 ** incremental, or neither.
 122211 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
 122212   Fts3Cursor *pCsr,               /* FTS3 cursor object */
 122213   Fts3Expr *pExpr,                /* Phrase to return doclist for */
 122214   int iCol,                       /* Column to return position list for */
 122215   char **ppOut                    /* OUT: Pointer to position list */
 122217   Fts3Phrase *pPhrase = pExpr->pPhrase;
 122218   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 122219   char *pIter;
 122220   int iThis;
 122221   sqlite3_int64 iDocid;
 122223   /* If this phrase is applies specifically to some column other than 
 122224   ** column iCol, return a NULL pointer.  */
 122225   *ppOut = 0;
 122226   assert( iCol>=0 && iCol<pTab->nColumn );
 122227   if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
 122228     return SQLITE_OK;
 122231   iDocid = pExpr->iDocid;
 122232   pIter = pPhrase->doclist.pList;
 122233   if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
 122234     int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
 122235     int bOr = 0;
 122236     u8 bEof = 0;
 122237     Fts3Expr *p;
 122239     /* Check if this phrase descends from an OR expression node. If not, 
 122240     ** return NULL. Otherwise, the entry that corresponds to docid 
 122241     ** pCsr->iPrevId may lie earlier in the doclist buffer. */
 122242     for(p=pExpr->pParent; p; p=p->pParent){
 122243       if( p->eType==FTSQUERY_OR ) bOr = 1;
 122245     if( bOr==0 ) return SQLITE_OK;
 122247     /* This is the descendent of an OR node. In this case we cannot use
 122248     ** an incremental phrase. Load the entire doclist for the phrase
 122249     ** into memory in this case.  */
 122250     if( pPhrase->bIncr ){
 122251       int rc = SQLITE_OK;
 122252       int bEofSave = pExpr->bEof;
 122253       fts3EvalRestart(pCsr, pExpr, &rc);
 122254       while( rc==SQLITE_OK && !pExpr->bEof ){
 122255         fts3EvalNextRow(pCsr, pExpr, &rc);
 122256         if( bEofSave==0 && pExpr->iDocid==iDocid ) break;
 122258       pIter = pPhrase->doclist.pList;
 122259       assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
 122260       if( rc!=SQLITE_OK ) return rc;
 122263     if( pExpr->bEof ){
 122264       pIter = 0;
 122265       iDocid = 0;
 122267     bEof = (pPhrase->doclist.nAll==0);
 122268     assert( bDescDoclist==0 || bDescDoclist==1 );
 122269     assert( pCsr->bDesc==0 || pCsr->bDesc==1 );
 122271     if( pCsr->bDesc==bDescDoclist ){
 122272       int dummy;
 122273       while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
 122274         sqlite3Fts3DoclistPrev(
 122275             bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
 122276             &pIter, &iDocid, &dummy, &bEof
 122279     }else{
 122280       while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
 122281         sqlite3Fts3DoclistNext(
 122282             bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
 122283             &pIter, &iDocid, &bEof
 122288     if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
 122290   if( pIter==0 ) return SQLITE_OK;
 122292   if( *pIter==0x01 ){
 122293     pIter++;
 122294     pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
 122295   }else{
 122296     iThis = 0;
 122298   while( iThis<iCol ){
 122299     fts3ColumnlistCopy(0, &pIter);
 122300     if( *pIter==0x00 ) return 0;
 122301     pIter++;
 122302     pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
 122305   *ppOut = ((iCol==iThis)?pIter:0);
 122306   return SQLITE_OK;
 122310 ** Free all components of the Fts3Phrase structure that were allocated by
 122311 ** the eval module. Specifically, this means to free:
 122313 **   * the contents of pPhrase->doclist, and
 122314 **   * any Fts3MultiSegReader objects held by phrase tokens.
 122316 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
 122317   if( pPhrase ){
 122318     int i;
 122319     sqlite3_free(pPhrase->doclist.aAll);
 122320     fts3EvalInvalidatePoslist(pPhrase);
 122321     memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
 122322     for(i=0; i<pPhrase->nToken; i++){
 122323       fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
 122324       pPhrase->aToken[i].pSegcsr = 0;
 122331 ** Return SQLITE_CORRUPT_VTAB.
 122333 #ifdef SQLITE_DEBUG
 122334 SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
 122335   return SQLITE_CORRUPT_VTAB;
 122337 #endif
 122339 #if !SQLITE_CORE
 122341 ** Initialize API pointer table, if required.
 122343 SQLITE_API int sqlite3_extension_init(
 122344   sqlite3 *db, 
 122345   char **pzErrMsg,
 122346   const sqlite3_api_routines *pApi
 122348   SQLITE_EXTENSION_INIT2(pApi)
 122349   return sqlite3Fts3Init(db);
 122351 #endif
 122353 #endif
 122355 /************** End of fts3.c ************************************************/
 122356 /************** Begin file fts3_aux.c ****************************************/
 122358 ** 2011 Jan 27
 122360 ** The author disclaims copyright to this source code.  In place of
 122361 ** a legal notice, here is a blessing:
 122363 **    May you do good and not evil.
 122364 **    May you find forgiveness for yourself and forgive others.
 122365 **    May you share freely, never taking more than you give.
 122367 ******************************************************************************
 122370 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 122372 /* #include <string.h> */
 122373 /* #include <assert.h> */
 122375 typedef struct Fts3auxTable Fts3auxTable;
 122376 typedef struct Fts3auxCursor Fts3auxCursor;
 122378 struct Fts3auxTable {
 122379   sqlite3_vtab base;              /* Base class used by SQLite core */
 122380   Fts3Table *pFts3Tab;
 122383 struct Fts3auxCursor {
 122384   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
 122385   Fts3MultiSegReader csr;        /* Must be right after "base" */
 122386   Fts3SegFilter filter;
 122387   char *zStop;
 122388   int nStop;                      /* Byte-length of string zStop */
 122389   int isEof;                      /* True if cursor is at EOF */
 122390   sqlite3_int64 iRowid;           /* Current rowid */
 122392   int iCol;                       /* Current value of 'col' column */
 122393   int nStat;                      /* Size of aStat[] array */
 122394   struct Fts3auxColstats {
 122395     sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
 122396     sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
 122397   } *aStat;
 122401 ** Schema of the terms table.
 122403 #define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
 122406 ** This function does all the work for both the xConnect and xCreate methods.
 122407 ** These tables have no persistent representation of their own, so xConnect
 122408 ** and xCreate are identical operations.
 122410 static int fts3auxConnectMethod(
 122411   sqlite3 *db,                    /* Database connection */
 122412   void *pUnused,                  /* Unused */
 122413   int argc,                       /* Number of elements in argv array */
 122414   const char * const *argv,       /* xCreate/xConnect argument array */
 122415   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
 122416   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
 122418   char const *zDb;                /* Name of database (e.g. "main") */
 122419   char const *zFts3;              /* Name of fts3 table */
 122420   int nDb;                        /* Result of strlen(zDb) */
 122421   int nFts3;                      /* Result of strlen(zFts3) */
 122422   int nByte;                      /* Bytes of space to allocate here */
 122423   int rc;                         /* value returned by declare_vtab() */
 122424   Fts3auxTable *p;                /* Virtual table object to return */
 122426   UNUSED_PARAMETER(pUnused);
 122428   /* The user should specify a single argument - the name of an fts3 table. */
 122429   if( argc!=4 ){
 122430     *pzErr = sqlite3_mprintf(
 122431         "wrong number of arguments to fts4aux constructor"
 122433     return SQLITE_ERROR;
 122436   zDb = argv[1]; 
 122437   nDb = (int)strlen(zDb);
 122438   zFts3 = argv[3];
 122439   nFts3 = (int)strlen(zFts3);
 122441   rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
 122442   if( rc!=SQLITE_OK ) return rc;
 122444   nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
 122445   p = (Fts3auxTable *)sqlite3_malloc(nByte);
 122446   if( !p ) return SQLITE_NOMEM;
 122447   memset(p, 0, nByte);
 122449   p->pFts3Tab = (Fts3Table *)&p[1];
 122450   p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
 122451   p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
 122452   p->pFts3Tab->db = db;
 122453   p->pFts3Tab->nIndex = 1;
 122455   memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
 122456   memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
 122457   sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
 122459   *ppVtab = (sqlite3_vtab *)p;
 122460   return SQLITE_OK;
 122464 ** This function does the work for both the xDisconnect and xDestroy methods.
 122465 ** These tables have no persistent representation of their own, so xDisconnect
 122466 ** and xDestroy are identical operations.
 122468 static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
 122469   Fts3auxTable *p = (Fts3auxTable *)pVtab;
 122470   Fts3Table *pFts3 = p->pFts3Tab;
 122471   int i;
 122473   /* Free any prepared statements held */
 122474   for(i=0; i<SizeofArray(pFts3->aStmt); i++){
 122475     sqlite3_finalize(pFts3->aStmt[i]);
 122477   sqlite3_free(pFts3->zSegmentsTbl);
 122478   sqlite3_free(p);
 122479   return SQLITE_OK;
 122482 #define FTS4AUX_EQ_CONSTRAINT 1
 122483 #define FTS4AUX_GE_CONSTRAINT 2
 122484 #define FTS4AUX_LE_CONSTRAINT 4
 122487 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
 122489 static int fts3auxBestIndexMethod(
 122490   sqlite3_vtab *pVTab, 
 122491   sqlite3_index_info *pInfo
 122493   int i;
 122494   int iEq = -1;
 122495   int iGe = -1;
 122496   int iLe = -1;
 122498   UNUSED_PARAMETER(pVTab);
 122500   /* This vtab delivers always results in "ORDER BY term ASC" order. */
 122501   if( pInfo->nOrderBy==1 
 122502    && pInfo->aOrderBy[0].iColumn==0 
 122503    && pInfo->aOrderBy[0].desc==0
 122505     pInfo->orderByConsumed = 1;
 122508   /* Search for equality and range constraints on the "term" column. */
 122509   for(i=0; i<pInfo->nConstraint; i++){
 122510     if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
 122511       int op = pInfo->aConstraint[i].op;
 122512       if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
 122513       if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
 122514       if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
 122515       if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
 122516       if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
 122520   if( iEq>=0 ){
 122521     pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
 122522     pInfo->aConstraintUsage[iEq].argvIndex = 1;
 122523     pInfo->estimatedCost = 5;
 122524   }else{
 122525     pInfo->idxNum = 0;
 122526     pInfo->estimatedCost = 20000;
 122527     if( iGe>=0 ){
 122528       pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
 122529       pInfo->aConstraintUsage[iGe].argvIndex = 1;
 122530       pInfo->estimatedCost /= 2;
 122532     if( iLe>=0 ){
 122533       pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
 122534       pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
 122535       pInfo->estimatedCost /= 2;
 122539   return SQLITE_OK;
 122543 ** xOpen - Open a cursor.
 122545 static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
 122546   Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
 122548   UNUSED_PARAMETER(pVTab);
 122550   pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
 122551   if( !pCsr ) return SQLITE_NOMEM;
 122552   memset(pCsr, 0, sizeof(Fts3auxCursor));
 122554   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
 122555   return SQLITE_OK;
 122559 ** xClose - Close a cursor.
 122561 static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
 122562   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
 122563   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 122565   sqlite3Fts3SegmentsClose(pFts3);
 122566   sqlite3Fts3SegReaderFinish(&pCsr->csr);
 122567   sqlite3_free((void *)pCsr->filter.zTerm);
 122568   sqlite3_free(pCsr->zStop);
 122569   sqlite3_free(pCsr->aStat);
 122570   sqlite3_free(pCsr);
 122571   return SQLITE_OK;
 122574 static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
 122575   if( nSize>pCsr->nStat ){
 122576     struct Fts3auxColstats *aNew;
 122577     aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, 
 122578         sizeof(struct Fts3auxColstats) * nSize
 122580     if( aNew==0 ) return SQLITE_NOMEM;
 122581     memset(&aNew[pCsr->nStat], 0, 
 122582         sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
 122584     pCsr->aStat = aNew;
 122585     pCsr->nStat = nSize;
 122587   return SQLITE_OK;
 122591 ** xNext - Advance the cursor to the next row, if any.
 122593 static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
 122594   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 122595   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
 122596   int rc;
 122598   /* Increment our pretend rowid value. */
 122599   pCsr->iRowid++;
 122601   for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
 122602     if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
 122605   rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
 122606   if( rc==SQLITE_ROW ){
 122607     int i = 0;
 122608     int nDoclist = pCsr->csr.nDoclist;
 122609     char *aDoclist = pCsr->csr.aDoclist;
 122610     int iCol;
 122612     int eState = 0;
 122614     if( pCsr->zStop ){
 122615       int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
 122616       int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
 122617       if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
 122618         pCsr->isEof = 1;
 122619         return SQLITE_OK;
 122623     if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
 122624     memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
 122625     iCol = 0;
 122627     while( i<nDoclist ){
 122628       sqlite3_int64 v = 0;
 122630       i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
 122631       switch( eState ){
 122632         /* State 0. In this state the integer just read was a docid. */
 122633         case 0:
 122634           pCsr->aStat[0].nDoc++;
 122635           eState = 1;
 122636           iCol = 0;
 122637           break;
 122639         /* State 1. In this state we are expecting either a 1, indicating
 122640         ** that the following integer will be a column number, or the
 122641         ** start of a position list for column 0.  
 122643         ** The only difference between state 1 and state 2 is that if the
 122644         ** integer encountered in state 1 is not 0 or 1, then we need to
 122645         ** increment the column 0 "nDoc" count for this term.
 122647         case 1:
 122648           assert( iCol==0 );
 122649           if( v>1 ){
 122650             pCsr->aStat[1].nDoc++;
 122652           eState = 2;
 122653           /* fall through */
 122655         case 2:
 122656           if( v==0 ){       /* 0x00. Next integer will be a docid. */
 122657             eState = 0;
 122658           }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
 122659             eState = 3;
 122660           }else{            /* 2 or greater. A position. */
 122661             pCsr->aStat[iCol+1].nOcc++;
 122662             pCsr->aStat[0].nOcc++;
 122664           break;
 122666         /* State 3. The integer just read is a column number. */
 122667         default: assert( eState==3 );
 122668           iCol = (int)v;
 122669           if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
 122670           pCsr->aStat[iCol+1].nDoc++;
 122671           eState = 2;
 122672           break;
 122676     pCsr->iCol = 0;
 122677     rc = SQLITE_OK;
 122678   }else{
 122679     pCsr->isEof = 1;
 122681   return rc;
 122685 ** xFilter - Initialize a cursor to point at the start of its data.
 122687 static int fts3auxFilterMethod(
 122688   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
 122689   int idxNum,                     /* Strategy index */
 122690   const char *idxStr,             /* Unused */
 122691   int nVal,                       /* Number of elements in apVal */
 122692   sqlite3_value **apVal           /* Arguments for the indexing scheme */
 122694   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 122695   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
 122696   int rc;
 122697   int isScan;
 122699   UNUSED_PARAMETER(nVal);
 122700   UNUSED_PARAMETER(idxStr);
 122702   assert( idxStr==0 );
 122703   assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
 122704        || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
 122705        || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
 122707   isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
 122709   /* In case this cursor is being reused, close and zero it. */
 122710   testcase(pCsr->filter.zTerm);
 122711   sqlite3Fts3SegReaderFinish(&pCsr->csr);
 122712   sqlite3_free((void *)pCsr->filter.zTerm);
 122713   sqlite3_free(pCsr->aStat);
 122714   memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
 122716   pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
 122717   if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
 122719   if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
 122720     const unsigned char *zStr = sqlite3_value_text(apVal[0]);
 122721     if( zStr ){
 122722       pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
 122723       pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
 122724       if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
 122727   if( idxNum&FTS4AUX_LE_CONSTRAINT ){
 122728     int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
 122729     pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
 122730     pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
 122731     if( pCsr->zStop==0 ) return SQLITE_NOMEM;
 122734   rc = sqlite3Fts3SegReaderCursor(pFts3, 0, 0, FTS3_SEGCURSOR_ALL,
 122735       pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
 122737   if( rc==SQLITE_OK ){
 122738     rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
 122741   if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
 122742   return rc;
 122746 ** xEof - Return true if the cursor is at EOF, or false otherwise.
 122748 static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
 122749   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 122750   return pCsr->isEof;
 122754 ** xColumn - Return a column value.
 122756 static int fts3auxColumnMethod(
 122757   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 122758   sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
 122759   int iCol                        /* Index of column to read value from */
 122761   Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
 122763   assert( p->isEof==0 );
 122764   if( iCol==0 ){        /* Column "term" */
 122765     sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
 122766   }else if( iCol==1 ){  /* Column "col" */
 122767     if( p->iCol ){
 122768       sqlite3_result_int(pContext, p->iCol-1);
 122769     }else{
 122770       sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
 122772   }else if( iCol==2 ){  /* Column "documents" */
 122773     sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
 122774   }else{                /* Column "occurrences" */
 122775     sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
 122778   return SQLITE_OK;
 122782 ** xRowid - Return the current rowid for the cursor.
 122784 static int fts3auxRowidMethod(
 122785   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 122786   sqlite_int64 *pRowid            /* OUT: Rowid value */
 122788   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 122789   *pRowid = pCsr->iRowid;
 122790   return SQLITE_OK;
 122794 ** Register the fts3aux module with database connection db. Return SQLITE_OK
 122795 ** if successful or an error code if sqlite3_create_module() fails.
 122797 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
 122798   static const sqlite3_module fts3aux_module = {
 122799      0,                           /* iVersion      */
 122800      fts3auxConnectMethod,        /* xCreate       */
 122801      fts3auxConnectMethod,        /* xConnect      */
 122802      fts3auxBestIndexMethod,      /* xBestIndex    */
 122803      fts3auxDisconnectMethod,     /* xDisconnect   */
 122804      fts3auxDisconnectMethod,     /* xDestroy      */
 122805      fts3auxOpenMethod,           /* xOpen         */
 122806      fts3auxCloseMethod,          /* xClose        */
 122807      fts3auxFilterMethod,         /* xFilter       */
 122808      fts3auxNextMethod,           /* xNext         */
 122809      fts3auxEofMethod,            /* xEof          */
 122810      fts3auxColumnMethod,         /* xColumn       */
 122811      fts3auxRowidMethod,          /* xRowid        */
 122812      0,                           /* xUpdate       */
 122813      0,                           /* xBegin        */
 122814      0,                           /* xSync         */
 122815      0,                           /* xCommit       */
 122816      0,                           /* xRollback     */
 122817      0,                           /* xFindFunction */
 122818      0,                           /* xRename       */
 122819      0,                           /* xSavepoint    */
 122820      0,                           /* xRelease      */
 122821      0                            /* xRollbackTo   */
 122823   int rc;                         /* Return code */
 122825   rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
 122826   return rc;
 122829 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 122831 /************** End of fts3_aux.c ********************************************/
 122832 /************** Begin file fts3_expr.c ***************************************/
 122834 ** 2008 Nov 28
 122836 ** The author disclaims copyright to this source code.  In place of
 122837 ** a legal notice, here is a blessing:
 122839 **    May you do good and not evil.
 122840 **    May you find forgiveness for yourself and forgive others.
 122841 **    May you share freely, never taking more than you give.
 122843 ******************************************************************************
 122845 ** This module contains code that implements a parser for fts3 query strings
 122846 ** (the right-hand argument to the MATCH operator). Because the supported 
 122847 ** syntax is relatively simple, the whole tokenizer/parser system is
 122848 ** hand-coded. 
 122850 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 122853 ** By default, this module parses the legacy syntax that has been 
 122854 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
 122855 ** is defined, then it uses the new syntax. The differences between
 122856 ** the new and the old syntaxes are:
 122858 **  a) The new syntax supports parenthesis. The old does not.
 122860 **  b) The new syntax supports the AND and NOT operators. The old does not.
 122862 **  c) The old syntax supports the "-" token qualifier. This is not 
 122863 **     supported by the new syntax (it is replaced by the NOT operator).
 122865 **  d) When using the old syntax, the OR operator has a greater precedence
 122866 **     than an implicit AND. When using the new, both implicity and explicit
 122867 **     AND operators have a higher precedence than OR.
 122869 ** If compiled with SQLITE_TEST defined, then this module exports the
 122870 ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
 122871 ** to zero causes the module to use the old syntax. If it is set to 
 122872 ** non-zero the new syntax is activated. This is so both syntaxes can
 122873 ** be tested using a single build of testfixture.
 122875 ** The following describes the syntax supported by the fts3 MATCH
 122876 ** operator in a similar format to that used by the lemon parser
 122877 ** generator. This module does not use actually lemon, it uses a
 122878 ** custom parser.
 122880 **   query ::= andexpr (OR andexpr)*.
 122882 **   andexpr ::= notexpr (AND? notexpr)*.
 122884 **   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
 122885 **   notexpr ::= LP query RP.
 122887 **   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
 122889 **   distance_opt ::= .
 122890 **   distance_opt ::= / INTEGER.
 122892 **   phrase ::= TOKEN.
 122893 **   phrase ::= COLUMN:TOKEN.
 122894 **   phrase ::= "TOKEN TOKEN TOKEN...".
 122897 #ifdef SQLITE_TEST
 122898 SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
 122899 #else
 122900 # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS 
 122901 #  define sqlite3_fts3_enable_parentheses 1
 122902 # else
 122903 #  define sqlite3_fts3_enable_parentheses 0
 122904 # endif
 122905 #endif
 122908 ** Default span for NEAR operators.
 122910 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
 122912 /* #include <string.h> */
 122913 /* #include <assert.h> */
 122916 ** isNot:
 122917 **   This variable is used by function getNextNode(). When getNextNode() is
 122918 **   called, it sets ParseContext.isNot to true if the 'next node' is a 
 122919 **   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
 122920 **   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
 122921 **   zero.
 122923 typedef struct ParseContext ParseContext;
 122924 struct ParseContext {
 122925   sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
 122926   int iLangid;                        /* Language id used with tokenizer */
 122927   const char **azCol;                 /* Array of column names for fts3 table */
 122928   int bFts4;                          /* True to allow FTS4-only syntax */
 122929   int nCol;                           /* Number of entries in azCol[] */
 122930   int iDefaultCol;                    /* Default column to query */
 122931   int isNot;                          /* True if getNextNode() sees a unary - */
 122932   sqlite3_context *pCtx;              /* Write error message here */
 122933   int nNest;                          /* Number of nested brackets */
 122937 ** This function is equivalent to the standard isspace() function. 
 122939 ** The standard isspace() can be awkward to use safely, because although it
 122940 ** is defined to accept an argument of type int, its behaviour when passed
 122941 ** an integer that falls outside of the range of the unsigned char type
 122942 ** is undefined (and sometimes, "undefined" means segfault). This wrapper
 122943 ** is defined to accept an argument of type char, and always returns 0 for
 122944 ** any values that fall outside of the range of the unsigned char type (i.e.
 122945 ** negative values).
 122947 static int fts3isspace(char c){
 122948   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
 122952 ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
 122953 ** zero the memory before returning a pointer to it. If unsuccessful, 
 122954 ** return NULL.
 122956 static void *fts3MallocZero(int nByte){
 122957   void *pRet = sqlite3_malloc(nByte);
 122958   if( pRet ) memset(pRet, 0, nByte);
 122959   return pRet;
 122962 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
 122963   sqlite3_tokenizer *pTokenizer,
 122964   int iLangid,
 122965   const char *z,
 122966   int n,
 122967   sqlite3_tokenizer_cursor **ppCsr
 122969   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 122970   sqlite3_tokenizer_cursor *pCsr = 0;
 122971   int rc;
 122973   rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
 122974   assert( rc==SQLITE_OK || pCsr==0 );
 122975   if( rc==SQLITE_OK ){
 122976     pCsr->pTokenizer = pTokenizer;
 122977     if( pModule->iVersion>=1 ){
 122978       rc = pModule->xLanguageid(pCsr, iLangid);
 122979       if( rc!=SQLITE_OK ){
 122980         pModule->xClose(pCsr);
 122981         pCsr = 0;
 122985   *ppCsr = pCsr;
 122986   return rc;
 122991 ** Extract the next token from buffer z (length n) using the tokenizer
 122992 ** and other information (column names etc.) in pParse. Create an Fts3Expr
 122993 ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
 122994 ** single token and set *ppExpr to point to it. If the end of the buffer is
 122995 ** reached before a token is found, set *ppExpr to zero. It is the
 122996 ** responsibility of the caller to eventually deallocate the allocated 
 122997 ** Fts3Expr structure (if any) by passing it to sqlite3_free().
 122999 ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
 123000 ** fails.
 123002 static int getNextToken(
 123003   ParseContext *pParse,                   /* fts3 query parse context */
 123004   int iCol,                               /* Value for Fts3Phrase.iColumn */
 123005   const char *z, int n,                   /* Input string */
 123006   Fts3Expr **ppExpr,                      /* OUT: expression */
 123007   int *pnConsumed                         /* OUT: Number of bytes consumed */
 123009   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
 123010   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 123011   int rc;
 123012   sqlite3_tokenizer_cursor *pCursor;
 123013   Fts3Expr *pRet = 0;
 123014   int nConsumed = 0;
 123016   rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
 123017   if( rc==SQLITE_OK ){
 123018     const char *zToken;
 123019     int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
 123020     int nByte;                               /* total space to allocate */
 123022     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
 123023     if( rc==SQLITE_OK ){
 123024       nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
 123025       pRet = (Fts3Expr *)fts3MallocZero(nByte);
 123026       if( !pRet ){
 123027         rc = SQLITE_NOMEM;
 123028       }else{
 123029         pRet->eType = FTSQUERY_PHRASE;
 123030         pRet->pPhrase = (Fts3Phrase *)&pRet[1];
 123031         pRet->pPhrase->nToken = 1;
 123032         pRet->pPhrase->iColumn = iCol;
 123033         pRet->pPhrase->aToken[0].n = nToken;
 123034         pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
 123035         memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
 123037         if( iEnd<n && z[iEnd]=='*' ){
 123038           pRet->pPhrase->aToken[0].isPrefix = 1;
 123039           iEnd++;
 123042         while( 1 ){
 123043           if( !sqlite3_fts3_enable_parentheses 
 123044            && iStart>0 && z[iStart-1]=='-' 
 123046             pParse->isNot = 1;
 123047             iStart--;
 123048           }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
 123049             pRet->pPhrase->aToken[0].bFirst = 1;
 123050             iStart--;
 123051           }else{
 123052             break;
 123057       nConsumed = iEnd;
 123060     pModule->xClose(pCursor);
 123063   *pnConsumed = nConsumed;
 123064   *ppExpr = pRet;
 123065   return rc;
 123070 ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
 123071 ** then free the old allocation.
 123073 static void *fts3ReallocOrFree(void *pOrig, int nNew){
 123074   void *pRet = sqlite3_realloc(pOrig, nNew);
 123075   if( !pRet ){
 123076     sqlite3_free(pOrig);
 123078   return pRet;
 123082 ** Buffer zInput, length nInput, contains the contents of a quoted string
 123083 ** that appeared as part of an fts3 query expression. Neither quote character
 123084 ** is included in the buffer. This function attempts to tokenize the entire
 123085 ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE 
 123086 ** containing the results.
 123088 ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
 123089 ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
 123090 ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
 123091 ** to 0.
 123093 static int getNextString(
 123094   ParseContext *pParse,                   /* fts3 query parse context */
 123095   const char *zInput, int nInput,         /* Input string */
 123096   Fts3Expr **ppExpr                       /* OUT: expression */
 123098   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
 123099   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 123100   int rc;
 123101   Fts3Expr *p = 0;
 123102   sqlite3_tokenizer_cursor *pCursor = 0;
 123103   char *zTemp = 0;
 123104   int nTemp = 0;
 123106   const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
 123107   int nToken = 0;
 123109   /* The final Fts3Expr data structure, including the Fts3Phrase,
 123110   ** Fts3PhraseToken structures token buffers are all stored as a single 
 123111   ** allocation so that the expression can be freed with a single call to
 123112   ** sqlite3_free(). Setting this up requires a two pass approach.
 123114   ** The first pass, in the block below, uses a tokenizer cursor to iterate
 123115   ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
 123116   ** to assemble data in two dynamic buffers:
 123118   **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
 123119   **             structure, followed by the array of Fts3PhraseToken 
 123120   **             structures. This pass only populates the Fts3PhraseToken array.
 123122   **   Buffer zTemp: Contains copies of all tokens.
 123124   ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
 123125   ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
 123126   ** structures.
 123128   rc = sqlite3Fts3OpenTokenizer(
 123129       pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
 123130   if( rc==SQLITE_OK ){
 123131     int ii;
 123132     for(ii=0; rc==SQLITE_OK; ii++){
 123133       const char *zByte;
 123134       int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
 123135       rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
 123136       if( rc==SQLITE_OK ){
 123137         Fts3PhraseToken *pToken;
 123139         p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
 123140         if( !p ) goto no_mem;
 123142         zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
 123143         if( !zTemp ) goto no_mem;
 123145         assert( nToken==ii );
 123146         pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
 123147         memset(pToken, 0, sizeof(Fts3PhraseToken));
 123149         memcpy(&zTemp[nTemp], zByte, nByte);
 123150         nTemp += nByte;
 123152         pToken->n = nByte;
 123153         pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
 123154         pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
 123155         nToken = ii+1;
 123159     pModule->xClose(pCursor);
 123160     pCursor = 0;
 123163   if( rc==SQLITE_DONE ){
 123164     int jj;
 123165     char *zBuf = 0;
 123167     p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
 123168     if( !p ) goto no_mem;
 123169     memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
 123170     p->eType = FTSQUERY_PHRASE;
 123171     p->pPhrase = (Fts3Phrase *)&p[1];
 123172     p->pPhrase->iColumn = pParse->iDefaultCol;
 123173     p->pPhrase->nToken = nToken;
 123175     zBuf = (char *)&p->pPhrase->aToken[nToken];
 123176     if( zTemp ){
 123177       memcpy(zBuf, zTemp, nTemp);
 123178       sqlite3_free(zTemp);
 123179     }else{
 123180       assert( nTemp==0 );
 123183     for(jj=0; jj<p->pPhrase->nToken; jj++){
 123184       p->pPhrase->aToken[jj].z = zBuf;
 123185       zBuf += p->pPhrase->aToken[jj].n;
 123187     rc = SQLITE_OK;
 123190   *ppExpr = p;
 123191   return rc;
 123192 no_mem:
 123194   if( pCursor ){
 123195     pModule->xClose(pCursor);
 123197   sqlite3_free(zTemp);
 123198   sqlite3_free(p);
 123199   *ppExpr = 0;
 123200   return SQLITE_NOMEM;
 123204 ** Function getNextNode(), which is called by fts3ExprParse(), may itself
 123205 ** call fts3ExprParse(). So this forward declaration is required.
 123207 static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
 123210 ** The output variable *ppExpr is populated with an allocated Fts3Expr 
 123211 ** structure, or set to 0 if the end of the input buffer is reached.
 123213 ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
 123214 ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
 123215 ** If SQLITE_ERROR is returned, pContext is populated with an error message.
 123217 static int getNextNode(
 123218   ParseContext *pParse,                   /* fts3 query parse context */
 123219   const char *z, int n,                   /* Input string */
 123220   Fts3Expr **ppExpr,                      /* OUT: expression */
 123221   int *pnConsumed                         /* OUT: Number of bytes consumed */
 123223   static const struct Fts3Keyword {
 123224     char *z;                              /* Keyword text */
 123225     unsigned char n;                      /* Length of the keyword */
 123226     unsigned char parenOnly;              /* Only valid in paren mode */
 123227     unsigned char eType;                  /* Keyword code */
 123228   } aKeyword[] = {
 123229     { "OR" ,  2, 0, FTSQUERY_OR   },
 123230     { "AND",  3, 1, FTSQUERY_AND  },
 123231     { "NOT",  3, 1, FTSQUERY_NOT  },
 123232     { "NEAR", 4, 0, FTSQUERY_NEAR }
 123234   int ii;
 123235   int iCol;
 123236   int iColLen;
 123237   int rc;
 123238   Fts3Expr *pRet = 0;
 123240   const char *zInput = z;
 123241   int nInput = n;
 123243   pParse->isNot = 0;
 123245   /* Skip over any whitespace before checking for a keyword, an open or
 123246   ** close bracket, or a quoted string. 
 123248   while( nInput>0 && fts3isspace(*zInput) ){
 123249     nInput--;
 123250     zInput++;
 123252   if( nInput==0 ){
 123253     return SQLITE_DONE;
 123256   /* See if we are dealing with a keyword. */
 123257   for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
 123258     const struct Fts3Keyword *pKey = &aKeyword[ii];
 123260     if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
 123261       continue;
 123264     if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
 123265       int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
 123266       int nKey = pKey->n;
 123267       char cNext;
 123269       /* If this is a "NEAR" keyword, check for an explicit nearness. */
 123270       if( pKey->eType==FTSQUERY_NEAR ){
 123271         assert( nKey==4 );
 123272         if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
 123273           nNear = 0;
 123274           for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
 123275             nNear = nNear * 10 + (zInput[nKey] - '0');
 123280       /* At this point this is probably a keyword. But for that to be true,
 123281       ** the next byte must contain either whitespace, an open or close
 123282       ** parenthesis, a quote character, or EOF. 
 123284       cNext = zInput[nKey];
 123285       if( fts3isspace(cNext) 
 123286        || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
 123288         pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
 123289         if( !pRet ){
 123290           return SQLITE_NOMEM;
 123292         pRet->eType = pKey->eType;
 123293         pRet->nNear = nNear;
 123294         *ppExpr = pRet;
 123295         *pnConsumed = (int)((zInput - z) + nKey);
 123296         return SQLITE_OK;
 123299       /* Turns out that wasn't a keyword after all. This happens if the
 123300       ** user has supplied a token such as "ORacle". Continue.
 123305   /* Check for an open bracket. */
 123306   if( sqlite3_fts3_enable_parentheses ){
 123307     if( *zInput=='(' ){
 123308       int nConsumed;
 123309       pParse->nNest++;
 123310       rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
 123311       if( rc==SQLITE_OK && !*ppExpr ){
 123312         rc = SQLITE_DONE;
 123314       *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
 123315       return rc;
 123318     /* Check for a close bracket. */
 123319     if( *zInput==')' ){
 123320       pParse->nNest--;
 123321       *pnConsumed = (int)((zInput - z) + 1);
 123322       return SQLITE_DONE;
 123326   /* See if we are dealing with a quoted phrase. If this is the case, then
 123327   ** search for the closing quote and pass the whole string to getNextString()
 123328   ** for processing. This is easy to do, as fts3 has no syntax for escaping
 123329   ** a quote character embedded in a string.
 123331   if( *zInput=='"' ){
 123332     for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
 123333     *pnConsumed = (int)((zInput - z) + ii + 1);
 123334     if( ii==nInput ){
 123335       return SQLITE_ERROR;
 123337     return getNextString(pParse, &zInput[1], ii-1, ppExpr);
 123341   /* If control flows to this point, this must be a regular token, or 
 123342   ** the end of the input. Read a regular token using the sqlite3_tokenizer
 123343   ** interface. Before doing so, figure out if there is an explicit
 123344   ** column specifier for the token. 
 123346   ** TODO: Strangely, it is not possible to associate a column specifier
 123347   ** with a quoted phrase, only with a single token. Not sure if this was
 123348   ** an implementation artifact or an intentional decision when fts3 was
 123349   ** first implemented. Whichever it was, this module duplicates the 
 123350   ** limitation.
 123352   iCol = pParse->iDefaultCol;
 123353   iColLen = 0;
 123354   for(ii=0; ii<pParse->nCol; ii++){
 123355     const char *zStr = pParse->azCol[ii];
 123356     int nStr = (int)strlen(zStr);
 123357     if( nInput>nStr && zInput[nStr]==':' 
 123358      && sqlite3_strnicmp(zStr, zInput, nStr)==0 
 123360       iCol = ii;
 123361       iColLen = (int)((zInput - z) + nStr + 1);
 123362       break;
 123365   rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
 123366   *pnConsumed += iColLen;
 123367   return rc;
 123371 ** The argument is an Fts3Expr structure for a binary operator (any type
 123372 ** except an FTSQUERY_PHRASE). Return an integer value representing the
 123373 ** precedence of the operator. Lower values have a higher precedence (i.e.
 123374 ** group more tightly). For example, in the C language, the == operator
 123375 ** groups more tightly than ||, and would therefore have a higher precedence.
 123377 ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
 123378 ** is defined), the order of the operators in precedence from highest to
 123379 ** lowest is:
 123381 **   NEAR
 123382 **   NOT
 123383 **   AND (including implicit ANDs)
 123384 **   OR
 123386 ** Note that when using the old query syntax, the OR operator has a higher
 123387 ** precedence than the AND operator.
 123389 static int opPrecedence(Fts3Expr *p){
 123390   assert( p->eType!=FTSQUERY_PHRASE );
 123391   if( sqlite3_fts3_enable_parentheses ){
 123392     return p->eType;
 123393   }else if( p->eType==FTSQUERY_NEAR ){
 123394     return 1;
 123395   }else if( p->eType==FTSQUERY_OR ){
 123396     return 2;
 123398   assert( p->eType==FTSQUERY_AND );
 123399   return 3;
 123403 ** Argument ppHead contains a pointer to the current head of a query 
 123404 ** expression tree being parsed. pPrev is the expression node most recently
 123405 ** inserted into the tree. This function adds pNew, which is always a binary
 123406 ** operator node, into the expression tree based on the relative precedence
 123407 ** of pNew and the existing nodes of the tree. This may result in the head
 123408 ** of the tree changing, in which case *ppHead is set to the new root node.
 123410 static void insertBinaryOperator(
 123411   Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
 123412   Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
 123413   Fts3Expr *pNew           /* New binary node to insert into expression tree */
 123415   Fts3Expr *pSplit = pPrev;
 123416   while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
 123417     pSplit = pSplit->pParent;
 123420   if( pSplit->pParent ){
 123421     assert( pSplit->pParent->pRight==pSplit );
 123422     pSplit->pParent->pRight = pNew;
 123423     pNew->pParent = pSplit->pParent;
 123424   }else{
 123425     *ppHead = pNew;
 123427   pNew->pLeft = pSplit;
 123428   pSplit->pParent = pNew;
 123432 ** Parse the fts3 query expression found in buffer z, length n. This function
 123433 ** returns either when the end of the buffer is reached or an unmatched 
 123434 ** closing bracket - ')' - is encountered.
 123436 ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
 123437 ** parsed form of the expression and *pnConsumed is set to the number of
 123438 ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
 123439 ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
 123441 static int fts3ExprParse(
 123442   ParseContext *pParse,                   /* fts3 query parse context */
 123443   const char *z, int n,                   /* Text of MATCH query */
 123444   Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
 123445   int *pnConsumed                         /* OUT: Number of bytes consumed */
 123447   Fts3Expr *pRet = 0;
 123448   Fts3Expr *pPrev = 0;
 123449   Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
 123450   int nIn = n;
 123451   const char *zIn = z;
 123452   int rc = SQLITE_OK;
 123453   int isRequirePhrase = 1;
 123455   while( rc==SQLITE_OK ){
 123456     Fts3Expr *p = 0;
 123457     int nByte = 0;
 123458     rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
 123459     if( rc==SQLITE_OK ){
 123460       int isPhrase;
 123462       if( !sqlite3_fts3_enable_parentheses 
 123463        && p->eType==FTSQUERY_PHRASE && pParse->isNot 
 123465         /* Create an implicit NOT operator. */
 123466         Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
 123467         if( !pNot ){
 123468           sqlite3Fts3ExprFree(p);
 123469           rc = SQLITE_NOMEM;
 123470           goto exprparse_out;
 123472         pNot->eType = FTSQUERY_NOT;
 123473         pNot->pRight = p;
 123474         if( pNotBranch ){
 123475           pNot->pLeft = pNotBranch;
 123477         pNotBranch = pNot;
 123478         p = pPrev;
 123479       }else{
 123480         int eType = p->eType;
 123481         isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
 123483         /* The isRequirePhrase variable is set to true if a phrase or
 123484         ** an expression contained in parenthesis is required. If a
 123485         ** binary operator (AND, OR, NOT or NEAR) is encounted when
 123486         ** isRequirePhrase is set, this is a syntax error.
 123488         if( !isPhrase && isRequirePhrase ){
 123489           sqlite3Fts3ExprFree(p);
 123490           rc = SQLITE_ERROR;
 123491           goto exprparse_out;
 123494         if( isPhrase && !isRequirePhrase ){
 123495           /* Insert an implicit AND operator. */
 123496           Fts3Expr *pAnd;
 123497           assert( pRet && pPrev );
 123498           pAnd = fts3MallocZero(sizeof(Fts3Expr));
 123499           if( !pAnd ){
 123500             sqlite3Fts3ExprFree(p);
 123501             rc = SQLITE_NOMEM;
 123502             goto exprparse_out;
 123504           pAnd->eType = FTSQUERY_AND;
 123505           insertBinaryOperator(&pRet, pPrev, pAnd);
 123506           pPrev = pAnd;
 123509         /* This test catches attempts to make either operand of a NEAR
 123510         ** operator something other than a phrase. For example, either of
 123511         ** the following:
 123513         **    (bracketed expression) NEAR phrase
 123514         **    phrase NEAR (bracketed expression)
 123516         ** Return an error in either case.
 123518         if( pPrev && (
 123519             (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
 123520          || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
 123521         )){
 123522           sqlite3Fts3ExprFree(p);
 123523           rc = SQLITE_ERROR;
 123524           goto exprparse_out;
 123527         if( isPhrase ){
 123528           if( pRet ){
 123529             assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
 123530             pPrev->pRight = p;
 123531             p->pParent = pPrev;
 123532           }else{
 123533             pRet = p;
 123535         }else{
 123536           insertBinaryOperator(&pRet, pPrev, p);
 123538         isRequirePhrase = !isPhrase;
 123540       assert( nByte>0 );
 123542     assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
 123543     nIn -= nByte;
 123544     zIn += nByte;
 123545     pPrev = p;
 123548   if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
 123549     rc = SQLITE_ERROR;
 123552   if( rc==SQLITE_DONE ){
 123553     rc = SQLITE_OK;
 123554     if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
 123555       if( !pRet ){
 123556         rc = SQLITE_ERROR;
 123557       }else{
 123558         Fts3Expr *pIter = pNotBranch;
 123559         while( pIter->pLeft ){
 123560           pIter = pIter->pLeft;
 123562         pIter->pLeft = pRet;
 123563         pRet = pNotBranch;
 123567   *pnConsumed = n - nIn;
 123569 exprparse_out:
 123570   if( rc!=SQLITE_OK ){
 123571     sqlite3Fts3ExprFree(pRet);
 123572     sqlite3Fts3ExprFree(pNotBranch);
 123573     pRet = 0;
 123575   *ppExpr = pRet;
 123576   return rc;
 123580 ** Parameters z and n contain a pointer to and length of a buffer containing
 123581 ** an fts3 query expression, respectively. This function attempts to parse the
 123582 ** query expression and create a tree of Fts3Expr structures representing the
 123583 ** parsed expression. If successful, *ppExpr is set to point to the head
 123584 ** of the parsed expression tree and SQLITE_OK is returned. If an error
 123585 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
 123586 ** error) is returned and *ppExpr is set to 0.
 123588 ** If parameter n is a negative number, then z is assumed to point to a
 123589 ** nul-terminated string and the length is determined using strlen().
 123591 ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
 123592 ** use to normalize query tokens while parsing the expression. The azCol[]
 123593 ** array, which is assumed to contain nCol entries, should contain the names
 123594 ** of each column in the target fts3 table, in order from left to right. 
 123595 ** Column names must be nul-terminated strings.
 123597 ** The iDefaultCol parameter should be passed the index of the table column
 123598 ** that appears on the left-hand-side of the MATCH operator (the default
 123599 ** column to match against for tokens for which a column name is not explicitly
 123600 ** specified as part of the query string), or -1 if tokens may by default
 123601 ** match any table column.
 123603 SQLITE_PRIVATE int sqlite3Fts3ExprParse(
 123604   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
 123605   int iLangid,                        /* Language id for tokenizer */
 123606   char **azCol,                       /* Array of column names for fts3 table */
 123607   int bFts4,                          /* True to allow FTS4-only syntax */
 123608   int nCol,                           /* Number of entries in azCol[] */
 123609   int iDefaultCol,                    /* Default column to query */
 123610   const char *z, int n,               /* Text of MATCH query */
 123611   Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
 123613   int nParsed;
 123614   int rc;
 123615   ParseContext sParse;
 123617   memset(&sParse, 0, sizeof(ParseContext));
 123618   sParse.pTokenizer = pTokenizer;
 123619   sParse.iLangid = iLangid;
 123620   sParse.azCol = (const char **)azCol;
 123621   sParse.nCol = nCol;
 123622   sParse.iDefaultCol = iDefaultCol;
 123623   sParse.bFts4 = bFts4;
 123624   if( z==0 ){
 123625     *ppExpr = 0;
 123626     return SQLITE_OK;
 123628   if( n<0 ){
 123629     n = (int)strlen(z);
 123631   rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
 123633   /* Check for mismatched parenthesis */
 123634   if( rc==SQLITE_OK && sParse.nNest ){
 123635     rc = SQLITE_ERROR;
 123636     sqlite3Fts3ExprFree(*ppExpr);
 123637     *ppExpr = 0;
 123640   return rc;
 123644 ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
 123646 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
 123647   if( p ){
 123648     assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
 123649     sqlite3Fts3ExprFree(p->pLeft);
 123650     sqlite3Fts3ExprFree(p->pRight);
 123651     sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
 123652     sqlite3_free(p->aMI);
 123653     sqlite3_free(p);
 123657 /****************************************************************************
 123658 *****************************************************************************
 123659 ** Everything after this point is just test code.
 123662 #ifdef SQLITE_TEST
 123664 /* #include <stdio.h> */
 123667 ** Function to query the hash-table of tokenizers (see README.tokenizers).
 123669 static int queryTestTokenizer(
 123670   sqlite3 *db, 
 123671   const char *zName,  
 123672   const sqlite3_tokenizer_module **pp
 123674   int rc;
 123675   sqlite3_stmt *pStmt;
 123676   const char zSql[] = "SELECT fts3_tokenizer(?)";
 123678   *pp = 0;
 123679   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 123680   if( rc!=SQLITE_OK ){
 123681     return rc;
 123684   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
 123685   if( SQLITE_ROW==sqlite3_step(pStmt) ){
 123686     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
 123687       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
 123691   return sqlite3_finalize(pStmt);
 123695 ** Return a pointer to a buffer containing a text representation of the
 123696 ** expression passed as the first argument. The buffer is obtained from
 123697 ** sqlite3_malloc(). It is the responsibility of the caller to use 
 123698 ** sqlite3_free() to release the memory. If an OOM condition is encountered,
 123699 ** NULL is returned.
 123701 ** If the second argument is not NULL, then its contents are prepended to 
 123702 ** the returned expression text and then freed using sqlite3_free().
 123704 static char *exprToString(Fts3Expr *pExpr, char *zBuf){
 123705   switch( pExpr->eType ){
 123706     case FTSQUERY_PHRASE: {
 123707       Fts3Phrase *pPhrase = pExpr->pPhrase;
 123708       int i;
 123709       zBuf = sqlite3_mprintf(
 123710           "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
 123711       for(i=0; zBuf && i<pPhrase->nToken; i++){
 123712         zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, 
 123713             pPhrase->aToken[i].n, pPhrase->aToken[i].z,
 123714             (pPhrase->aToken[i].isPrefix?"+":"")
 123717       return zBuf;
 123720     case FTSQUERY_NEAR:
 123721       zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
 123722       break;
 123723     case FTSQUERY_NOT:
 123724       zBuf = sqlite3_mprintf("%zNOT ", zBuf);
 123725       break;
 123726     case FTSQUERY_AND:
 123727       zBuf = sqlite3_mprintf("%zAND ", zBuf);
 123728       break;
 123729     case FTSQUERY_OR:
 123730       zBuf = sqlite3_mprintf("%zOR ", zBuf);
 123731       break;
 123734   if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
 123735   if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
 123736   if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
 123738   if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
 123739   if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
 123741   return zBuf;
 123745 ** This is the implementation of a scalar SQL function used to test the 
 123746 ** expression parser. It should be called as follows:
 123748 **   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
 123750 ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
 123751 ** to parse the query expression (see README.tokenizers). The second argument
 123752 ** is the query expression to parse. Each subsequent argument is the name
 123753 ** of a column of the fts3 table that the query expression may refer to.
 123754 ** For example:
 123756 **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
 123758 static void fts3ExprTest(
 123759   sqlite3_context *context,
 123760   int argc,
 123761   sqlite3_value **argv
 123763   sqlite3_tokenizer_module const *pModule = 0;
 123764   sqlite3_tokenizer *pTokenizer = 0;
 123765   int rc;
 123766   char **azCol = 0;
 123767   const char *zExpr;
 123768   int nExpr;
 123769   int nCol;
 123770   int ii;
 123771   Fts3Expr *pExpr;
 123772   char *zBuf = 0;
 123773   sqlite3 *db = sqlite3_context_db_handle(context);
 123775   if( argc<3 ){
 123776     sqlite3_result_error(context, 
 123777         "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
 123779     return;
 123782   rc = queryTestTokenizer(db,
 123783                           (const char *)sqlite3_value_text(argv[0]), &pModule);
 123784   if( rc==SQLITE_NOMEM ){
 123785     sqlite3_result_error_nomem(context);
 123786     goto exprtest_out;
 123787   }else if( !pModule ){
 123788     sqlite3_result_error(context, "No such tokenizer module", -1);
 123789     goto exprtest_out;
 123792   rc = pModule->xCreate(0, 0, &pTokenizer);
 123793   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
 123794   if( rc==SQLITE_NOMEM ){
 123795     sqlite3_result_error_nomem(context);
 123796     goto exprtest_out;
 123798   pTokenizer->pModule = pModule;
 123800   zExpr = (const char *)sqlite3_value_text(argv[1]);
 123801   nExpr = sqlite3_value_bytes(argv[1]);
 123802   nCol = argc-2;
 123803   azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
 123804   if( !azCol ){
 123805     sqlite3_result_error_nomem(context);
 123806     goto exprtest_out;
 123808   for(ii=0; ii<nCol; ii++){
 123809     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
 123812   rc = sqlite3Fts3ExprParse(
 123813       pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
 123815   if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
 123816     sqlite3_result_error(context, "Error parsing expression", -1);
 123817   }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
 123818     sqlite3_result_error_nomem(context);
 123819   }else{
 123820     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 123821     sqlite3_free(zBuf);
 123824   sqlite3Fts3ExprFree(pExpr);
 123826 exprtest_out:
 123827   if( pModule && pTokenizer ){
 123828     rc = pModule->xDestroy(pTokenizer);
 123830   sqlite3_free(azCol);
 123834 ** Register the query expression parser test function fts3_exprtest() 
 123835 ** with database connection db. 
 123837 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
 123838   return sqlite3_create_function(
 123839       db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
 123843 #endif
 123844 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 123846 /************** End of fts3_expr.c *******************************************/
 123847 /************** Begin file fts3_hash.c ***************************************/
 123849 ** 2001 September 22
 123851 ** The author disclaims copyright to this source code.  In place of
 123852 ** a legal notice, here is a blessing:
 123854 **    May you do good and not evil.
 123855 **    May you find forgiveness for yourself and forgive others.
 123856 **    May you share freely, never taking more than you give.
 123858 *************************************************************************
 123859 ** This is the implementation of generic hash-tables used in SQLite.
 123860 ** We've modified it slightly to serve as a standalone hash table
 123861 ** implementation for the full-text indexing module.
 123865 ** The code in this file is only compiled if:
 123867 **     * The FTS3 module is being built as an extension
 123868 **       (in which case SQLITE_CORE is not defined), or
 123870 **     * The FTS3 module is being built into the core of
 123871 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 123873 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 123875 /* #include <assert.h> */
 123876 /* #include <stdlib.h> */
 123877 /* #include <string.h> */
 123881 ** Malloc and Free functions
 123883 static void *fts3HashMalloc(int n){
 123884   void *p = sqlite3_malloc(n);
 123885   if( p ){
 123886     memset(p, 0, n);
 123888   return p;
 123890 static void fts3HashFree(void *p){
 123891   sqlite3_free(p);
 123894 /* Turn bulk memory into a hash table object by initializing the
 123895 ** fields of the Hash structure.
 123897 ** "pNew" is a pointer to the hash table that is to be initialized.
 123898 ** keyClass is one of the constants 
 123899 ** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass 
 123900 ** determines what kind of key the hash table will use.  "copyKey" is
 123901 ** true if the hash table should make its own private copy of keys and
 123902 ** false if it should just use the supplied pointer.
 123904 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
 123905   assert( pNew!=0 );
 123906   assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
 123907   pNew->keyClass = keyClass;
 123908   pNew->copyKey = copyKey;
 123909   pNew->first = 0;
 123910   pNew->count = 0;
 123911   pNew->htsize = 0;
 123912   pNew->ht = 0;
 123915 /* Remove all entries from a hash table.  Reclaim all memory.
 123916 ** Call this routine to delete a hash table or to reset a hash table
 123917 ** to the empty state.
 123919 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
 123920   Fts3HashElem *elem;         /* For looping over all elements of the table */
 123922   assert( pH!=0 );
 123923   elem = pH->first;
 123924   pH->first = 0;
 123925   fts3HashFree(pH->ht);
 123926   pH->ht = 0;
 123927   pH->htsize = 0;
 123928   while( elem ){
 123929     Fts3HashElem *next_elem = elem->next;
 123930     if( pH->copyKey && elem->pKey ){
 123931       fts3HashFree(elem->pKey);
 123933     fts3HashFree(elem);
 123934     elem = next_elem;
 123936   pH->count = 0;
 123940 ** Hash and comparison functions when the mode is FTS3_HASH_STRING
 123942 static int fts3StrHash(const void *pKey, int nKey){
 123943   const char *z = (const char *)pKey;
 123944   int h = 0;
 123945   if( nKey<=0 ) nKey = (int) strlen(z);
 123946   while( nKey > 0  ){
 123947     h = (h<<3) ^ h ^ *z++;
 123948     nKey--;
 123950   return h & 0x7fffffff;
 123952 static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
 123953   if( n1!=n2 ) return 1;
 123954   return strncmp((const char*)pKey1,(const char*)pKey2,n1);
 123958 ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
 123960 static int fts3BinHash(const void *pKey, int nKey){
 123961   int h = 0;
 123962   const char *z = (const char *)pKey;
 123963   while( nKey-- > 0 ){
 123964     h = (h<<3) ^ h ^ *(z++);
 123966   return h & 0x7fffffff;
 123968 static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
 123969   if( n1!=n2 ) return 1;
 123970   return memcmp(pKey1,pKey2,n1);
 123974 ** Return a pointer to the appropriate hash function given the key class.
 123976 ** The C syntax in this function definition may be unfamilar to some 
 123977 ** programmers, so we provide the following additional explanation:
 123979 ** The name of the function is "ftsHashFunction".  The function takes a
 123980 ** single parameter "keyClass".  The return value of ftsHashFunction()
 123981 ** is a pointer to another function.  Specifically, the return value
 123982 ** of ftsHashFunction() is a pointer to a function that takes two parameters
 123983 ** with types "const void*" and "int" and returns an "int".
 123985 static int (*ftsHashFunction(int keyClass))(const void*,int){
 123986   if( keyClass==FTS3_HASH_STRING ){
 123987     return &fts3StrHash;
 123988   }else{
 123989     assert( keyClass==FTS3_HASH_BINARY );
 123990     return &fts3BinHash;
 123995 ** Return a pointer to the appropriate hash function given the key class.
 123997 ** For help in interpreted the obscure C code in the function definition,
 123998 ** see the header comment on the previous function.
 124000 static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
 124001   if( keyClass==FTS3_HASH_STRING ){
 124002     return &fts3StrCompare;
 124003   }else{
 124004     assert( keyClass==FTS3_HASH_BINARY );
 124005     return &fts3BinCompare;
 124009 /* Link an element into the hash table
 124011 static void fts3HashInsertElement(
 124012   Fts3Hash *pH,            /* The complete hash table */
 124013   struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
 124014   Fts3HashElem *pNew       /* The element to be inserted */
 124016   Fts3HashElem *pHead;     /* First element already in pEntry */
 124017   pHead = pEntry->chain;
 124018   if( pHead ){
 124019     pNew->next = pHead;
 124020     pNew->prev = pHead->prev;
 124021     if( pHead->prev ){ pHead->prev->next = pNew; }
 124022     else             { pH->first = pNew; }
 124023     pHead->prev = pNew;
 124024   }else{
 124025     pNew->next = pH->first;
 124026     if( pH->first ){ pH->first->prev = pNew; }
 124027     pNew->prev = 0;
 124028     pH->first = pNew;
 124030   pEntry->count++;
 124031   pEntry->chain = pNew;
 124035 /* Resize the hash table so that it cantains "new_size" buckets.
 124036 ** "new_size" must be a power of 2.  The hash table might fail 
 124037 ** to resize if sqliteMalloc() fails.
 124039 ** Return non-zero if a memory allocation error occurs.
 124041 static int fts3Rehash(Fts3Hash *pH, int new_size){
 124042   struct _fts3ht *new_ht;          /* The new hash table */
 124043   Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
 124044   int (*xHash)(const void*,int);   /* The hash function */
 124046   assert( (new_size & (new_size-1))==0 );
 124047   new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
 124048   if( new_ht==0 ) return 1;
 124049   fts3HashFree(pH->ht);
 124050   pH->ht = new_ht;
 124051   pH->htsize = new_size;
 124052   xHash = ftsHashFunction(pH->keyClass);
 124053   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
 124054     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
 124055     next_elem = elem->next;
 124056     fts3HashInsertElement(pH, &new_ht[h], elem);
 124058   return 0;
 124061 /* This function (for internal use only) locates an element in an
 124062 ** hash table that matches the given key.  The hash for this key has
 124063 ** already been computed and is passed as the 4th parameter.
 124065 static Fts3HashElem *fts3FindElementByHash(
 124066   const Fts3Hash *pH, /* The pH to be searched */
 124067   const void *pKey,   /* The key we are searching for */
 124068   int nKey,
 124069   int h               /* The hash for this key. */
 124071   Fts3HashElem *elem;            /* Used to loop thru the element list */
 124072   int count;                     /* Number of elements left to test */
 124073   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
 124075   if( pH->ht ){
 124076     struct _fts3ht *pEntry = &pH->ht[h];
 124077     elem = pEntry->chain;
 124078     count = pEntry->count;
 124079     xCompare = ftsCompareFunction(pH->keyClass);
 124080     while( count-- && elem ){
 124081       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ 
 124082         return elem;
 124084       elem = elem->next;
 124087   return 0;
 124090 /* Remove a single entry from the hash table given a pointer to that
 124091 ** element and a hash on the element's key.
 124093 static void fts3RemoveElementByHash(
 124094   Fts3Hash *pH,         /* The pH containing "elem" */
 124095   Fts3HashElem* elem,   /* The element to be removed from the pH */
 124096   int h                 /* Hash value for the element */
 124098   struct _fts3ht *pEntry;
 124099   if( elem->prev ){
 124100     elem->prev->next = elem->next; 
 124101   }else{
 124102     pH->first = elem->next;
 124104   if( elem->next ){
 124105     elem->next->prev = elem->prev;
 124107   pEntry = &pH->ht[h];
 124108   if( pEntry->chain==elem ){
 124109     pEntry->chain = elem->next;
 124111   pEntry->count--;
 124112   if( pEntry->count<=0 ){
 124113     pEntry->chain = 0;
 124115   if( pH->copyKey && elem->pKey ){
 124116     fts3HashFree(elem->pKey);
 124118   fts3HashFree( elem );
 124119   pH->count--;
 124120   if( pH->count<=0 ){
 124121     assert( pH->first==0 );
 124122     assert( pH->count==0 );
 124123     fts3HashClear(pH);
 124127 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
 124128   const Fts3Hash *pH, 
 124129   const void *pKey, 
 124130   int nKey
 124132   int h;                          /* A hash on key */
 124133   int (*xHash)(const void*,int);  /* The hash function */
 124135   if( pH==0 || pH->ht==0 ) return 0;
 124136   xHash = ftsHashFunction(pH->keyClass);
 124137   assert( xHash!=0 );
 124138   h = (*xHash)(pKey,nKey);
 124139   assert( (pH->htsize & (pH->htsize-1))==0 );
 124140   return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
 124144 ** Attempt to locate an element of the hash table pH with a key
 124145 ** that matches pKey,nKey.  Return the data for this element if it is
 124146 ** found, or NULL if there is no match.
 124148 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
 124149   Fts3HashElem *pElem;            /* The element that matches key (if any) */
 124151   pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
 124152   return pElem ? pElem->data : 0;
 124155 /* Insert an element into the hash table pH.  The key is pKey,nKey
 124156 ** and the data is "data".
 124158 ** If no element exists with a matching key, then a new
 124159 ** element is created.  A copy of the key is made if the copyKey
 124160 ** flag is set.  NULL is returned.
 124162 ** If another element already exists with the same key, then the
 124163 ** new data replaces the old data and the old data is returned.
 124164 ** The key is not copied in this instance.  If a malloc fails, then
 124165 ** the new data is returned and the hash table is unchanged.
 124167 ** If the "data" parameter to this function is NULL, then the
 124168 ** element corresponding to "key" is removed from the hash table.
 124170 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
 124171   Fts3Hash *pH,        /* The hash table to insert into */
 124172   const void *pKey,    /* The key */
 124173   int nKey,            /* Number of bytes in the key */
 124174   void *data           /* The data */
 124176   int hraw;                 /* Raw hash value of the key */
 124177   int h;                    /* the hash of the key modulo hash table size */
 124178   Fts3HashElem *elem;       /* Used to loop thru the element list */
 124179   Fts3HashElem *new_elem;   /* New element added to the pH */
 124180   int (*xHash)(const void*,int);  /* The hash function */
 124182   assert( pH!=0 );
 124183   xHash = ftsHashFunction(pH->keyClass);
 124184   assert( xHash!=0 );
 124185   hraw = (*xHash)(pKey, nKey);
 124186   assert( (pH->htsize & (pH->htsize-1))==0 );
 124187   h = hraw & (pH->htsize-1);
 124188   elem = fts3FindElementByHash(pH,pKey,nKey,h);
 124189   if( elem ){
 124190     void *old_data = elem->data;
 124191     if( data==0 ){
 124192       fts3RemoveElementByHash(pH,elem,h);
 124193     }else{
 124194       elem->data = data;
 124196     return old_data;
 124198   if( data==0 ) return 0;
 124199   if( (pH->htsize==0 && fts3Rehash(pH,8))
 124200    || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
 124202     pH->count = 0;
 124203     return data;
 124205   assert( pH->htsize>0 );
 124206   new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
 124207   if( new_elem==0 ) return data;
 124208   if( pH->copyKey && pKey!=0 ){
 124209     new_elem->pKey = fts3HashMalloc( nKey );
 124210     if( new_elem->pKey==0 ){
 124211       fts3HashFree(new_elem);
 124212       return data;
 124214     memcpy((void*)new_elem->pKey, pKey, nKey);
 124215   }else{
 124216     new_elem->pKey = (void*)pKey;
 124218   new_elem->nKey = nKey;
 124219   pH->count++;
 124220   assert( pH->htsize>0 );
 124221   assert( (pH->htsize & (pH->htsize-1))==0 );
 124222   h = hraw & (pH->htsize-1);
 124223   fts3HashInsertElement(pH, &pH->ht[h], new_elem);
 124224   new_elem->data = data;
 124225   return 0;
 124228 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 124230 /************** End of fts3_hash.c *******************************************/
 124231 /************** Begin file fts3_porter.c *************************************/
 124233 ** 2006 September 30
 124235 ** The author disclaims copyright to this source code.  In place of
 124236 ** a legal notice, here is a blessing:
 124238 **    May you do good and not evil.
 124239 **    May you find forgiveness for yourself and forgive others.
 124240 **    May you share freely, never taking more than you give.
 124242 *************************************************************************
 124243 ** Implementation of the full-text-search tokenizer that implements
 124244 ** a Porter stemmer.
 124248 ** The code in this file is only compiled if:
 124250 **     * The FTS3 module is being built as an extension
 124251 **       (in which case SQLITE_CORE is not defined), or
 124253 **     * The FTS3 module is being built into the core of
 124254 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 124256 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 124258 /* #include <assert.h> */
 124259 /* #include <stdlib.h> */
 124260 /* #include <stdio.h> */
 124261 /* #include <string.h> */
 124265 ** Class derived from sqlite3_tokenizer
 124267 typedef struct porter_tokenizer {
 124268   sqlite3_tokenizer base;      /* Base class */
 124269 } porter_tokenizer;
 124272 ** Class derived from sqlite3_tokenizer_cursor
 124274 typedef struct porter_tokenizer_cursor {
 124275   sqlite3_tokenizer_cursor base;
 124276   const char *zInput;          /* input we are tokenizing */
 124277   int nInput;                  /* size of the input */
 124278   int iOffset;                 /* current position in zInput */
 124279   int iToken;                  /* index of next token to be returned */
 124280   char *zToken;                /* storage for current token */
 124281   int nAllocated;              /* space allocated to zToken buffer */
 124282 } porter_tokenizer_cursor;
 124286 ** Create a new tokenizer instance.
 124288 static int porterCreate(
 124289   int argc, const char * const *argv,
 124290   sqlite3_tokenizer **ppTokenizer
 124292   porter_tokenizer *t;
 124294   UNUSED_PARAMETER(argc);
 124295   UNUSED_PARAMETER(argv);
 124297   t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
 124298   if( t==NULL ) return SQLITE_NOMEM;
 124299   memset(t, 0, sizeof(*t));
 124300   *ppTokenizer = &t->base;
 124301   return SQLITE_OK;
 124305 ** Destroy a tokenizer
 124307 static int porterDestroy(sqlite3_tokenizer *pTokenizer){
 124308   sqlite3_free(pTokenizer);
 124309   return SQLITE_OK;
 124313 ** Prepare to begin tokenizing a particular string.  The input
 124314 ** string to be tokenized is zInput[0..nInput-1].  A cursor
 124315 ** used to incrementally tokenize this string is returned in 
 124316 ** *ppCursor.
 124318 static int porterOpen(
 124319   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
 124320   const char *zInput, int nInput,        /* String to be tokenized */
 124321   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
 124323   porter_tokenizer_cursor *c;
 124325   UNUSED_PARAMETER(pTokenizer);
 124327   c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
 124328   if( c==NULL ) return SQLITE_NOMEM;
 124330   c->zInput = zInput;
 124331   if( zInput==0 ){
 124332     c->nInput = 0;
 124333   }else if( nInput<0 ){
 124334     c->nInput = (int)strlen(zInput);
 124335   }else{
 124336     c->nInput = nInput;
 124338   c->iOffset = 0;                 /* start tokenizing at the beginning */
 124339   c->iToken = 0;
 124340   c->zToken = NULL;               /* no space allocated, yet. */
 124341   c->nAllocated = 0;
 124343   *ppCursor = &c->base;
 124344   return SQLITE_OK;
 124348 ** Close a tokenization cursor previously opened by a call to
 124349 ** porterOpen() above.
 124351 static int porterClose(sqlite3_tokenizer_cursor *pCursor){
 124352   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
 124353   sqlite3_free(c->zToken);
 124354   sqlite3_free(c);
 124355   return SQLITE_OK;
 124358 ** Vowel or consonant
 124360 static const char cType[] = {
 124361    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
 124362    1, 1, 1, 2, 1
 124366 ** isConsonant() and isVowel() determine if their first character in
 124367 ** the string they point to is a consonant or a vowel, according
 124368 ** to Porter ruls.  
 124370 ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
 124371 ** 'Y' is a consonant unless it follows another consonant,
 124372 ** in which case it is a vowel.
 124374 ** In these routine, the letters are in reverse order.  So the 'y' rule
 124375 ** is that 'y' is a consonant unless it is followed by another
 124376 ** consonent.
 124378 static int isVowel(const char*);
 124379 static int isConsonant(const char *z){
 124380   int j;
 124381   char x = *z;
 124382   if( x==0 ) return 0;
 124383   assert( x>='a' && x<='z' );
 124384   j = cType[x-'a'];
 124385   if( j<2 ) return j;
 124386   return z[1]==0 || isVowel(z + 1);
 124388 static int isVowel(const char *z){
 124389   int j;
 124390   char x = *z;
 124391   if( x==0 ) return 0;
 124392   assert( x>='a' && x<='z' );
 124393   j = cType[x-'a'];
 124394   if( j<2 ) return 1-j;
 124395   return isConsonant(z + 1);
 124399 ** Let any sequence of one or more vowels be represented by V and let
 124400 ** C be sequence of one or more consonants.  Then every word can be
 124401 ** represented as:
 124403 **           [C] (VC){m} [V]
 124405 ** In prose:  A word is an optional consonant followed by zero or
 124406 ** vowel-consonant pairs followed by an optional vowel.  "m" is the
 124407 ** number of vowel consonant pairs.  This routine computes the value
 124408 ** of m for the first i bytes of a word.
 124410 ** Return true if the m-value for z is 1 or more.  In other words,
 124411 ** return true if z contains at least one vowel that is followed
 124412 ** by a consonant.
 124414 ** In this routine z[] is in reverse order.  So we are really looking
 124415 ** for an instance of of a consonant followed by a vowel.
 124417 static int m_gt_0(const char *z){
 124418   while( isVowel(z) ){ z++; }
 124419   if( *z==0 ) return 0;
 124420   while( isConsonant(z) ){ z++; }
 124421   return *z!=0;
 124424 /* Like mgt0 above except we are looking for a value of m which is
 124425 ** exactly 1
 124427 static int m_eq_1(const char *z){
 124428   while( isVowel(z) ){ z++; }
 124429   if( *z==0 ) return 0;
 124430   while( isConsonant(z) ){ z++; }
 124431   if( *z==0 ) return 0;
 124432   while( isVowel(z) ){ z++; }
 124433   if( *z==0 ) return 1;
 124434   while( isConsonant(z) ){ z++; }
 124435   return *z==0;
 124438 /* Like mgt0 above except we are looking for a value of m>1 instead
 124439 ** or m>0
 124441 static int m_gt_1(const char *z){
 124442   while( isVowel(z) ){ z++; }
 124443   if( *z==0 ) return 0;
 124444   while( isConsonant(z) ){ z++; }
 124445   if( *z==0 ) return 0;
 124446   while( isVowel(z) ){ z++; }
 124447   if( *z==0 ) return 0;
 124448   while( isConsonant(z) ){ z++; }
 124449   return *z!=0;
 124453 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
 124455 static int hasVowel(const char *z){
 124456   while( isConsonant(z) ){ z++; }
 124457   return *z!=0;
 124461 ** Return TRUE if the word ends in a double consonant.
 124463 ** The text is reversed here. So we are really looking at
 124464 ** the first two characters of z[].
 124466 static int doubleConsonant(const char *z){
 124467   return isConsonant(z) && z[0]==z[1];
 124471 ** Return TRUE if the word ends with three letters which
 124472 ** are consonant-vowel-consonent and where the final consonant
 124473 ** is not 'w', 'x', or 'y'.
 124475 ** The word is reversed here.  So we are really checking the
 124476 ** first three letters and the first one cannot be in [wxy].
 124478 static int star_oh(const char *z){
 124479   return
 124480     isConsonant(z) &&
 124481     z[0]!='w' && z[0]!='x' && z[0]!='y' &&
 124482     isVowel(z+1) &&
 124483     isConsonant(z+2);
 124487 ** If the word ends with zFrom and xCond() is true for the stem
 124488 ** of the word that preceeds the zFrom ending, then change the 
 124489 ** ending to zTo.
 124491 ** The input word *pz and zFrom are both in reverse order.  zTo
 124492 ** is in normal order. 
 124494 ** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
 124495 ** match.  Not that TRUE is returned even if xCond() fails and
 124496 ** no substitution occurs.
 124498 static int stem(
 124499   char **pz,             /* The word being stemmed (Reversed) */
 124500   const char *zFrom,     /* If the ending matches this... (Reversed) */
 124501   const char *zTo,       /* ... change the ending to this (not reversed) */
 124502   int (*xCond)(const char*)   /* Condition that must be true */
 124504   char *z = *pz;
 124505   while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
 124506   if( *zFrom!=0 ) return 0;
 124507   if( xCond && !xCond(z) ) return 1;
 124508   while( *zTo ){
 124509     *(--z) = *(zTo++);
 124511   *pz = z;
 124512   return 1;
 124516 ** This is the fallback stemmer used when the porter stemmer is
 124517 ** inappropriate.  The input word is copied into the output with
 124518 ** US-ASCII case folding.  If the input word is too long (more
 124519 ** than 20 bytes if it contains no digits or more than 6 bytes if
 124520 ** it contains digits) then word is truncated to 20 or 6 bytes
 124521 ** by taking 10 or 3 bytes from the beginning and end.
 124523 static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
 124524   int i, mx, j;
 124525   int hasDigit = 0;
 124526   for(i=0; i<nIn; i++){
 124527     char c = zIn[i];
 124528     if( c>='A' && c<='Z' ){
 124529       zOut[i] = c - 'A' + 'a';
 124530     }else{
 124531       if( c>='0' && c<='9' ) hasDigit = 1;
 124532       zOut[i] = c;
 124535   mx = hasDigit ? 3 : 10;
 124536   if( nIn>mx*2 ){
 124537     for(j=mx, i=nIn-mx; i<nIn; i++, j++){
 124538       zOut[j] = zOut[i];
 124540     i = j;
 124542   zOut[i] = 0;
 124543   *pnOut = i;
 124548 ** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
 124549 ** zOut is at least big enough to hold nIn bytes.  Write the actual
 124550 ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
 124552 ** Any upper-case characters in the US-ASCII character set ([A-Z])
 124553 ** are converted to lower case.  Upper-case UTF characters are
 124554 ** unchanged.
 124556 ** Words that are longer than about 20 bytes are stemmed by retaining
 124557 ** a few bytes from the beginning and the end of the word.  If the
 124558 ** word contains digits, 3 bytes are taken from the beginning and
 124559 ** 3 bytes from the end.  For long words without digits, 10 bytes
 124560 ** are taken from each end.  US-ASCII case folding still applies.
 124562 ** If the input word contains not digits but does characters not 
 124563 ** in [a-zA-Z] then no stemming is attempted and this routine just 
 124564 ** copies the input into the input into the output with US-ASCII
 124565 ** case folding.
 124567 ** Stemming never increases the length of the word.  So there is
 124568 ** no chance of overflowing the zOut buffer.
 124570 static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
 124571   int i, j;
 124572   char zReverse[28];
 124573   char *z, *z2;
 124574   if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
 124575     /* The word is too big or too small for the porter stemmer.
 124576     ** Fallback to the copy stemmer */
 124577     copy_stemmer(zIn, nIn, zOut, pnOut);
 124578     return;
 124580   for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
 124581     char c = zIn[i];
 124582     if( c>='A' && c<='Z' ){
 124583       zReverse[j] = c + 'a' - 'A';
 124584     }else if( c>='a' && c<='z' ){
 124585       zReverse[j] = c;
 124586     }else{
 124587       /* The use of a character not in [a-zA-Z] means that we fallback
 124588       ** to the copy stemmer */
 124589       copy_stemmer(zIn, nIn, zOut, pnOut);
 124590       return;
 124593   memset(&zReverse[sizeof(zReverse)-5], 0, 5);
 124594   z = &zReverse[j+1];
 124597   /* Step 1a */
 124598   if( z[0]=='s' ){
 124599     if(
 124600      !stem(&z, "sess", "ss", 0) &&
 124601      !stem(&z, "sei", "i", 0)  &&
 124602      !stem(&z, "ss", "ss", 0)
 124604       z++;
 124608   /* Step 1b */  
 124609   z2 = z;
 124610   if( stem(&z, "dee", "ee", m_gt_0) ){
 124611     /* Do nothing.  The work was all in the test */
 124612   }else if( 
 124613      (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
 124614       && z!=z2
 124616      if( stem(&z, "ta", "ate", 0) ||
 124617          stem(&z, "lb", "ble", 0) ||
 124618          stem(&z, "zi", "ize", 0) ){
 124619        /* Do nothing.  The work was all in the test */
 124620      }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
 124621        z++;
 124622      }else if( m_eq_1(z) && star_oh(z) ){
 124623        *(--z) = 'e';
 124627   /* Step 1c */
 124628   if( z[0]=='y' && hasVowel(z+1) ){
 124629     z[0] = 'i';
 124632   /* Step 2 */
 124633   switch( z[1] ){
 124634    case 'a':
 124635      stem(&z, "lanoita", "ate", m_gt_0) ||
 124636      stem(&z, "lanoit", "tion", m_gt_0);
 124637      break;
 124638    case 'c':
 124639      stem(&z, "icne", "ence", m_gt_0) ||
 124640      stem(&z, "icna", "ance", m_gt_0);
 124641      break;
 124642    case 'e':
 124643      stem(&z, "rezi", "ize", m_gt_0);
 124644      break;
 124645    case 'g':
 124646      stem(&z, "igol", "log", m_gt_0);
 124647      break;
 124648    case 'l':
 124649      stem(&z, "ilb", "ble", m_gt_0) ||
 124650      stem(&z, "illa", "al", m_gt_0) ||
 124651      stem(&z, "iltne", "ent", m_gt_0) ||
 124652      stem(&z, "ile", "e", m_gt_0) ||
 124653      stem(&z, "ilsuo", "ous", m_gt_0);
 124654      break;
 124655    case 'o':
 124656      stem(&z, "noitazi", "ize", m_gt_0) ||
 124657      stem(&z, "noita", "ate", m_gt_0) ||
 124658      stem(&z, "rota", "ate", m_gt_0);
 124659      break;
 124660    case 's':
 124661      stem(&z, "msila", "al", m_gt_0) ||
 124662      stem(&z, "ssenevi", "ive", m_gt_0) ||
 124663      stem(&z, "ssenluf", "ful", m_gt_0) ||
 124664      stem(&z, "ssensuo", "ous", m_gt_0);
 124665      break;
 124666    case 't':
 124667      stem(&z, "itila", "al", m_gt_0) ||
 124668      stem(&z, "itivi", "ive", m_gt_0) ||
 124669      stem(&z, "itilib", "ble", m_gt_0);
 124670      break;
 124673   /* Step 3 */
 124674   switch( z[0] ){
 124675    case 'e':
 124676      stem(&z, "etaci", "ic", m_gt_0) ||
 124677      stem(&z, "evita", "", m_gt_0)   ||
 124678      stem(&z, "ezila", "al", m_gt_0);
 124679      break;
 124680    case 'i':
 124681      stem(&z, "itici", "ic", m_gt_0);
 124682      break;
 124683    case 'l':
 124684      stem(&z, "laci", "ic", m_gt_0) ||
 124685      stem(&z, "luf", "", m_gt_0);
 124686      break;
 124687    case 's':
 124688      stem(&z, "ssen", "", m_gt_0);
 124689      break;
 124692   /* Step 4 */
 124693   switch( z[1] ){
 124694    case 'a':
 124695      if( z[0]=='l' && m_gt_1(z+2) ){
 124696        z += 2;
 124698      break;
 124699    case 'c':
 124700      if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
 124701        z += 4;
 124703      break;
 124704    case 'e':
 124705      if( z[0]=='r' && m_gt_1(z+2) ){
 124706        z += 2;
 124708      break;
 124709    case 'i':
 124710      if( z[0]=='c' && m_gt_1(z+2) ){
 124711        z += 2;
 124713      break;
 124714    case 'l':
 124715      if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
 124716        z += 4;
 124718      break;
 124719    case 'n':
 124720      if( z[0]=='t' ){
 124721        if( z[2]=='a' ){
 124722          if( m_gt_1(z+3) ){
 124723            z += 3;
 124725        }else if( z[2]=='e' ){
 124726          stem(&z, "tneme", "", m_gt_1) ||
 124727          stem(&z, "tnem", "", m_gt_1) ||
 124728          stem(&z, "tne", "", m_gt_1);
 124731      break;
 124732    case 'o':
 124733      if( z[0]=='u' ){
 124734        if( m_gt_1(z+2) ){
 124735          z += 2;
 124737      }else if( z[3]=='s' || z[3]=='t' ){
 124738        stem(&z, "noi", "", m_gt_1);
 124740      break;
 124741    case 's':
 124742      if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
 124743        z += 3;
 124745      break;
 124746    case 't':
 124747      stem(&z, "eta", "", m_gt_1) ||
 124748      stem(&z, "iti", "", m_gt_1);
 124749      break;
 124750    case 'u':
 124751      if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
 124752        z += 3;
 124754      break;
 124755    case 'v':
 124756    case 'z':
 124757      if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
 124758        z += 3;
 124760      break;
 124763   /* Step 5a */
 124764   if( z[0]=='e' ){
 124765     if( m_gt_1(z+1) ){
 124766       z++;
 124767     }else if( m_eq_1(z+1) && !star_oh(z+1) ){
 124768       z++;
 124772   /* Step 5b */
 124773   if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
 124774     z++;
 124777   /* z[] is now the stemmed word in reverse order.  Flip it back
 124778   ** around into forward order and return.
 124780   *pnOut = i = (int)strlen(z);
 124781   zOut[i] = 0;
 124782   while( *z ){
 124783     zOut[--i] = *(z++);
 124788 ** Characters that can be part of a token.  We assume any character
 124789 ** whose value is greater than 0x80 (any UTF character) can be
 124790 ** part of a token.  In other words, delimiters all must have
 124791 ** values of 0x7f or lower.
 124793 static const char porterIdChar[] = {
 124794 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
 124795     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
 124796     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
 124797     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
 124798     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
 124799     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
 124801 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
 124804 ** Extract the next token from a tokenization cursor.  The cursor must
 124805 ** have been opened by a prior call to porterOpen().
 124807 static int porterNext(
 124808   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
 124809   const char **pzToken,               /* OUT: *pzToken is the token text */
 124810   int *pnBytes,                       /* OUT: Number of bytes in token */
 124811   int *piStartOffset,                 /* OUT: Starting offset of token */
 124812   int *piEndOffset,                   /* OUT: Ending offset of token */
 124813   int *piPosition                     /* OUT: Position integer of token */
 124815   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
 124816   const char *z = c->zInput;
 124818   while( c->iOffset<c->nInput ){
 124819     int iStartOffset, ch;
 124821     /* Scan past delimiter characters */
 124822     while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
 124823       c->iOffset++;
 124826     /* Count non-delimiter characters. */
 124827     iStartOffset = c->iOffset;
 124828     while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
 124829       c->iOffset++;
 124832     if( c->iOffset>iStartOffset ){
 124833       int n = c->iOffset-iStartOffset;
 124834       if( n>c->nAllocated ){
 124835         char *pNew;
 124836         c->nAllocated = n+20;
 124837         pNew = sqlite3_realloc(c->zToken, c->nAllocated);
 124838         if( !pNew ) return SQLITE_NOMEM;
 124839         c->zToken = pNew;
 124841       porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
 124842       *pzToken = c->zToken;
 124843       *piStartOffset = iStartOffset;
 124844       *piEndOffset = c->iOffset;
 124845       *piPosition = c->iToken++;
 124846       return SQLITE_OK;
 124849   return SQLITE_DONE;
 124853 ** The set of routines that implement the porter-stemmer tokenizer
 124855 static const sqlite3_tokenizer_module porterTokenizerModule = {
 124857   porterCreate,
 124858   porterDestroy,
 124859   porterOpen,
 124860   porterClose,
 124861   porterNext,
 124866 ** Allocate a new porter tokenizer.  Return a pointer to the new
 124867 ** tokenizer in *ppModule
 124869 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
 124870   sqlite3_tokenizer_module const**ppModule
 124872   *ppModule = &porterTokenizerModule;
 124875 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 124877 /************** End of fts3_porter.c *****************************************/
 124878 /************** Begin file fts3_tokenizer.c **********************************/
 124880 ** 2007 June 22
 124882 ** The author disclaims copyright to this source code.  In place of
 124883 ** a legal notice, here is a blessing:
 124885 **    May you do good and not evil.
 124886 **    May you find forgiveness for yourself and forgive others.
 124887 **    May you share freely, never taking more than you give.
 124889 ******************************************************************************
 124891 ** This is part of an SQLite module implementing full-text search.
 124892 ** This particular file implements the generic tokenizer interface.
 124896 ** The code in this file is only compiled if:
 124898 **     * The FTS3 module is being built as an extension
 124899 **       (in which case SQLITE_CORE is not defined), or
 124901 **     * The FTS3 module is being built into the core of
 124902 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 124904 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 124906 /* #include <assert.h> */
 124907 /* #include <string.h> */
 124910 ** Implementation of the SQL scalar function for accessing the underlying 
 124911 ** hash table. This function may be called as follows:
 124913 **   SELECT <function-name>(<key-name>);
 124914 **   SELECT <function-name>(<key-name>, <pointer>);
 124916 ** where <function-name> is the name passed as the second argument
 124917 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
 124919 ** If the <pointer> argument is specified, it must be a blob value
 124920 ** containing a pointer to be stored as the hash data corresponding
 124921 ** to the string <key-name>. If <pointer> is not specified, then
 124922 ** the string <key-name> must already exist in the has table. Otherwise,
 124923 ** an error is returned.
 124925 ** Whether or not the <pointer> argument is specified, the value returned
 124926 ** is a blob containing the pointer stored as the hash data corresponding
 124927 ** to string <key-name> (after the hash-table is updated, if applicable).
 124929 static void scalarFunc(
 124930   sqlite3_context *context,
 124931   int argc,
 124932   sqlite3_value **argv
 124934   Fts3Hash *pHash;
 124935   void *pPtr = 0;
 124936   const unsigned char *zName;
 124937   int nName;
 124939   assert( argc==1 || argc==2 );
 124941   pHash = (Fts3Hash *)sqlite3_user_data(context);
 124943   zName = sqlite3_value_text(argv[0]);
 124944   nName = sqlite3_value_bytes(argv[0])+1;
 124946   if( argc==2 ){
 124947     void *pOld;
 124948     int n = sqlite3_value_bytes(argv[1]);
 124949     if( n!=sizeof(pPtr) ){
 124950       sqlite3_result_error(context, "argument type mismatch", -1);
 124951       return;
 124953     pPtr = *(void **)sqlite3_value_blob(argv[1]);
 124954     pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
 124955     if( pOld==pPtr ){
 124956       sqlite3_result_error(context, "out of memory", -1);
 124957       return;
 124959   }else{
 124960     pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
 124961     if( !pPtr ){
 124962       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
 124963       sqlite3_result_error(context, zErr, -1);
 124964       sqlite3_free(zErr);
 124965       return;
 124969   sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
 124972 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
 124973   static const char isFtsIdChar[] = {
 124974       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
 124975       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
 124976       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
 124977       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
 124978       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
 124979       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
 124980       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
 124981       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
 124983   return (c&0x80 || isFtsIdChar[(int)(c)]);
 124986 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
 124987   const char *z1;
 124988   const char *z2 = 0;
 124990   /* Find the start of the next token. */
 124991   z1 = zStr;
 124992   while( z2==0 ){
 124993     char c = *z1;
 124994     switch( c ){
 124995       case '\0': return 0;        /* No more tokens here */
 124996       case '\'':
 124997       case '"':
 124998       case '`': {
 124999         z2 = z1;
 125000         while( *++z2 && (*z2!=c || *++z2==c) );
 125001         break;
 125003       case '[':
 125004         z2 = &z1[1];
 125005         while( *z2 && z2[0]!=']' ) z2++;
 125006         if( *z2 ) z2++;
 125007         break;
 125009       default:
 125010         if( sqlite3Fts3IsIdChar(*z1) ){
 125011           z2 = &z1[1];
 125012           while( sqlite3Fts3IsIdChar(*z2) ) z2++;
 125013         }else{
 125014           z1++;
 125019   *pn = (int)(z2-z1);
 125020   return z1;
 125023 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
 125024   Fts3Hash *pHash,                /* Tokenizer hash table */
 125025   const char *zArg,               /* Tokenizer name */
 125026   sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
 125027   char **pzErr                    /* OUT: Set to malloced error message */
 125029   int rc;
 125030   char *z = (char *)zArg;
 125031   int n = 0;
 125032   char *zCopy;
 125033   char *zEnd;                     /* Pointer to nul-term of zCopy */
 125034   sqlite3_tokenizer_module *m;
 125036   zCopy = sqlite3_mprintf("%s", zArg);
 125037   if( !zCopy ) return SQLITE_NOMEM;
 125038   zEnd = &zCopy[strlen(zCopy)];
 125040   z = (char *)sqlite3Fts3NextToken(zCopy, &n);
 125041   z[n] = '\0';
 125042   sqlite3Fts3Dequote(z);
 125044   m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
 125045   if( !m ){
 125046     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
 125047     rc = SQLITE_ERROR;
 125048   }else{
 125049     char const **aArg = 0;
 125050     int iArg = 0;
 125051     z = &z[n+1];
 125052     while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
 125053       int nNew = sizeof(char *)*(iArg+1);
 125054       char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
 125055       if( !aNew ){
 125056         sqlite3_free(zCopy);
 125057         sqlite3_free((void *)aArg);
 125058         return SQLITE_NOMEM;
 125060       aArg = aNew;
 125061       aArg[iArg++] = z;
 125062       z[n] = '\0';
 125063       sqlite3Fts3Dequote(z);
 125064       z = &z[n+1];
 125066     rc = m->xCreate(iArg, aArg, ppTok);
 125067     assert( rc!=SQLITE_OK || *ppTok );
 125068     if( rc!=SQLITE_OK ){
 125069       *pzErr = sqlite3_mprintf("unknown tokenizer");
 125070     }else{
 125071       (*ppTok)->pModule = m; 
 125073     sqlite3_free((void *)aArg);
 125076   sqlite3_free(zCopy);
 125077   return rc;
 125081 #ifdef SQLITE_TEST
 125083 /* #include <tcl.h> */
 125084 /* #include <string.h> */
 125087 ** Implementation of a special SQL scalar function for testing tokenizers 
 125088 ** designed to be used in concert with the Tcl testing framework. This
 125089 ** function must be called with two or more arguments:
 125091 **   SELECT <function-name>(<key-name>, ..., <input-string>);
 125093 ** where <function-name> is the name passed as the second argument
 125094 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
 125095 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
 125097 ** The return value is a string that may be interpreted as a Tcl
 125098 ** list. For each token in the <input-string>, three elements are
 125099 ** added to the returned list. The first is the token position, the 
 125100 ** second is the token text (folded, stemmed, etc.) and the third is the
 125101 ** substring of <input-string> associated with the token. For example, 
 125102 ** using the built-in "simple" tokenizer:
 125104 **   SELECT fts_tokenizer_test('simple', 'I don't see how');
 125106 ** will return the string:
 125108 **   "{0 i I 1 dont don't 2 see see 3 how how}"
 125111 static void testFunc(
 125112   sqlite3_context *context,
 125113   int argc,
 125114   sqlite3_value **argv
 125116   Fts3Hash *pHash;
 125117   sqlite3_tokenizer_module *p;
 125118   sqlite3_tokenizer *pTokenizer = 0;
 125119   sqlite3_tokenizer_cursor *pCsr = 0;
 125121   const char *zErr = 0;
 125123   const char *zName;
 125124   int nName;
 125125   const char *zInput;
 125126   int nInput;
 125128   const char *azArg[64];
 125130   const char *zToken;
 125131   int nToken = 0;
 125132   int iStart = 0;
 125133   int iEnd = 0;
 125134   int iPos = 0;
 125135   int i;
 125137   Tcl_Obj *pRet;
 125139   if( argc<2 ){
 125140     sqlite3_result_error(context, "insufficient arguments", -1);
 125141     return;
 125144   nName = sqlite3_value_bytes(argv[0]);
 125145   zName = (const char *)sqlite3_value_text(argv[0]);
 125146   nInput = sqlite3_value_bytes(argv[argc-1]);
 125147   zInput = (const char *)sqlite3_value_text(argv[argc-1]);
 125149   pHash = (Fts3Hash *)sqlite3_user_data(context);
 125150   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
 125152   if( !p ){
 125153     char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
 125154     sqlite3_result_error(context, zErr, -1);
 125155     sqlite3_free(zErr);
 125156     return;
 125159   pRet = Tcl_NewObj();
 125160   Tcl_IncrRefCount(pRet);
 125162   for(i=1; i<argc-1; i++){
 125163     azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
 125166   if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
 125167     zErr = "error in xCreate()";
 125168     goto finish;
 125170   pTokenizer->pModule = p;
 125171   if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
 125172     zErr = "error in xOpen()";
 125173     goto finish;
 125176   while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
 125177     Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
 125178     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
 125179     zToken = &zInput[iStart];
 125180     nToken = iEnd-iStart;
 125181     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
 125184   if( SQLITE_OK!=p->xClose(pCsr) ){
 125185     zErr = "error in xClose()";
 125186     goto finish;
 125188   if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
 125189     zErr = "error in xDestroy()";
 125190     goto finish;
 125193 finish:
 125194   if( zErr ){
 125195     sqlite3_result_error(context, zErr, -1);
 125196   }else{
 125197     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
 125199   Tcl_DecrRefCount(pRet);
 125202 static
 125203 int registerTokenizer(
 125204   sqlite3 *db, 
 125205   char *zName, 
 125206   const sqlite3_tokenizer_module *p
 125208   int rc;
 125209   sqlite3_stmt *pStmt;
 125210   const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
 125212   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 125213   if( rc!=SQLITE_OK ){
 125214     return rc;
 125217   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
 125218   sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
 125219   sqlite3_step(pStmt);
 125221   return sqlite3_finalize(pStmt);
 125224 static
 125225 int queryTokenizer(
 125226   sqlite3 *db, 
 125227   char *zName,  
 125228   const sqlite3_tokenizer_module **pp
 125230   int rc;
 125231   sqlite3_stmt *pStmt;
 125232   const char zSql[] = "SELECT fts3_tokenizer(?)";
 125234   *pp = 0;
 125235   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 125236   if( rc!=SQLITE_OK ){
 125237     return rc;
 125240   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
 125241   if( SQLITE_ROW==sqlite3_step(pStmt) ){
 125242     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
 125243       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
 125247   return sqlite3_finalize(pStmt);
 125250 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 125253 ** Implementation of the scalar function fts3_tokenizer_internal_test().
 125254 ** This function is used for testing only, it is not included in the
 125255 ** build unless SQLITE_TEST is defined.
 125257 ** The purpose of this is to test that the fts3_tokenizer() function
 125258 ** can be used as designed by the C-code in the queryTokenizer and
 125259 ** registerTokenizer() functions above. These two functions are repeated
 125260 ** in the README.tokenizer file as an example, so it is important to
 125261 ** test them.
 125263 ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
 125264 ** function with no arguments. An assert() will fail if a problem is
 125265 ** detected. i.e.:
 125267 **     SELECT fts3_tokenizer_internal_test();
 125270 static void intTestFunc(
 125271   sqlite3_context *context,
 125272   int argc,
 125273   sqlite3_value **argv
 125275   int rc;
 125276   const sqlite3_tokenizer_module *p1;
 125277   const sqlite3_tokenizer_module *p2;
 125278   sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
 125280   UNUSED_PARAMETER(argc);
 125281   UNUSED_PARAMETER(argv);
 125283   /* Test the query function */
 125284   sqlite3Fts3SimpleTokenizerModule(&p1);
 125285   rc = queryTokenizer(db, "simple", &p2);
 125286   assert( rc==SQLITE_OK );
 125287   assert( p1==p2 );
 125288   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
 125289   assert( rc==SQLITE_ERROR );
 125290   assert( p2==0 );
 125291   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
 125293   /* Test the storage function */
 125294   rc = registerTokenizer(db, "nosuchtokenizer", p1);
 125295   assert( rc==SQLITE_OK );
 125296   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
 125297   assert( rc==SQLITE_OK );
 125298   assert( p2==p1 );
 125300   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
 125303 #endif
 125306 ** Set up SQL objects in database db used to access the contents of
 125307 ** the hash table pointed to by argument pHash. The hash table must
 125308 ** been initialised to use string keys, and to take a private copy 
 125309 ** of the key when a value is inserted. i.e. by a call similar to:
 125311 **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
 125313 ** This function adds a scalar function (see header comment above
 125314 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
 125315 ** defined at compilation time, a temporary virtual table (see header 
 125316 ** comment above struct HashTableVtab) to the database schema. Both 
 125317 ** provide read/write access to the contents of *pHash.
 125319 ** The third argument to this function, zName, is used as the name
 125320 ** of both the scalar and, if created, the virtual table.
 125322 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
 125323   sqlite3 *db, 
 125324   Fts3Hash *pHash, 
 125325   const char *zName
 125327   int rc = SQLITE_OK;
 125328   void *p = (void *)pHash;
 125329   const int any = SQLITE_ANY;
 125331 #ifdef SQLITE_TEST
 125332   char *zTest = 0;
 125333   char *zTest2 = 0;
 125334   void *pdb = (void *)db;
 125335   zTest = sqlite3_mprintf("%s_test", zName);
 125336   zTest2 = sqlite3_mprintf("%s_internal_test", zName);
 125337   if( !zTest || !zTest2 ){
 125338     rc = SQLITE_NOMEM;
 125340 #endif
 125342   if( SQLITE_OK==rc ){
 125343     rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
 125345   if( SQLITE_OK==rc ){
 125346     rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
 125348 #ifdef SQLITE_TEST
 125349   if( SQLITE_OK==rc ){
 125350     rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
 125352   if( SQLITE_OK==rc ){
 125353     rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
 125355 #endif
 125357 #ifdef SQLITE_TEST
 125358   sqlite3_free(zTest);
 125359   sqlite3_free(zTest2);
 125360 #endif
 125362   return rc;
 125365 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 125367 /************** End of fts3_tokenizer.c **************************************/
 125368 /************** Begin file fts3_tokenizer1.c *********************************/
 125370 ** 2006 Oct 10
 125372 ** The author disclaims copyright to this source code.  In place of
 125373 ** a legal notice, here is a blessing:
 125375 **    May you do good and not evil.
 125376 **    May you find forgiveness for yourself and forgive others.
 125377 **    May you share freely, never taking more than you give.
 125379 ******************************************************************************
 125381 ** Implementation of the "simple" full-text-search tokenizer.
 125385 ** The code in this file is only compiled if:
 125387 **     * The FTS3 module is being built as an extension
 125388 **       (in which case SQLITE_CORE is not defined), or
 125390 **     * The FTS3 module is being built into the core of
 125391 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 125393 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 125395 /* #include <assert.h> */
 125396 /* #include <stdlib.h> */
 125397 /* #include <stdio.h> */
 125398 /* #include <string.h> */
 125401 typedef struct simple_tokenizer {
 125402   sqlite3_tokenizer base;
 125403   char delim[128];             /* flag ASCII delimiters */
 125404 } simple_tokenizer;
 125406 typedef struct simple_tokenizer_cursor {
 125407   sqlite3_tokenizer_cursor base;
 125408   const char *pInput;          /* input we are tokenizing */
 125409   int nBytes;                  /* size of the input */
 125410   int iOffset;                 /* current position in pInput */
 125411   int iToken;                  /* index of next token to be returned */
 125412   char *pToken;                /* storage for current token */
 125413   int nTokenAllocated;         /* space allocated to zToken buffer */
 125414 } simple_tokenizer_cursor;
 125417 static int simpleDelim(simple_tokenizer *t, unsigned char c){
 125418   return c<0x80 && t->delim[c];
 125420 static int fts3_isalnum(int x){
 125421   return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
 125425 ** Create a new tokenizer instance.
 125427 static int simpleCreate(
 125428   int argc, const char * const *argv,
 125429   sqlite3_tokenizer **ppTokenizer
 125431   simple_tokenizer *t;
 125433   t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
 125434   if( t==NULL ) return SQLITE_NOMEM;
 125435   memset(t, 0, sizeof(*t));
 125437   /* TODO(shess) Delimiters need to remain the same from run to run,
 125438   ** else we need to reindex.  One solution would be a meta-table to
 125439   ** track such information in the database, then we'd only want this
 125440   ** information on the initial create.
 125442   if( argc>1 ){
 125443     int i, n = (int)strlen(argv[1]);
 125444     for(i=0; i<n; i++){
 125445       unsigned char ch = argv[1][i];
 125446       /* We explicitly don't support UTF-8 delimiters for now. */
 125447       if( ch>=0x80 ){
 125448         sqlite3_free(t);
 125449         return SQLITE_ERROR;
 125451       t->delim[ch] = 1;
 125453   } else {
 125454     /* Mark non-alphanumeric ASCII characters as delimiters */
 125455     int i;
 125456     for(i=1; i<0x80; i++){
 125457       t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
 125461   *ppTokenizer = &t->base;
 125462   return SQLITE_OK;
 125466 ** Destroy a tokenizer
 125468 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
 125469   sqlite3_free(pTokenizer);
 125470   return SQLITE_OK;
 125474 ** Prepare to begin tokenizing a particular string.  The input
 125475 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
 125476 ** used to incrementally tokenize this string is returned in 
 125477 ** *ppCursor.
 125479 static int simpleOpen(
 125480   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
 125481   const char *pInput, int nBytes,        /* String to be tokenized */
 125482   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
 125484   simple_tokenizer_cursor *c;
 125486   UNUSED_PARAMETER(pTokenizer);
 125488   c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
 125489   if( c==NULL ) return SQLITE_NOMEM;
 125491   c->pInput = pInput;
 125492   if( pInput==0 ){
 125493     c->nBytes = 0;
 125494   }else if( nBytes<0 ){
 125495     c->nBytes = (int)strlen(pInput);
 125496   }else{
 125497     c->nBytes = nBytes;
 125499   c->iOffset = 0;                 /* start tokenizing at the beginning */
 125500   c->iToken = 0;
 125501   c->pToken = NULL;               /* no space allocated, yet. */
 125502   c->nTokenAllocated = 0;
 125504   *ppCursor = &c->base;
 125505   return SQLITE_OK;
 125509 ** Close a tokenization cursor previously opened by a call to
 125510 ** simpleOpen() above.
 125512 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
 125513   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
 125514   sqlite3_free(c->pToken);
 125515   sqlite3_free(c);
 125516   return SQLITE_OK;
 125520 ** Extract the next token from a tokenization cursor.  The cursor must
 125521 ** have been opened by a prior call to simpleOpen().
 125523 static int simpleNext(
 125524   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
 125525   const char **ppToken,               /* OUT: *ppToken is the token text */
 125526   int *pnBytes,                       /* OUT: Number of bytes in token */
 125527   int *piStartOffset,                 /* OUT: Starting offset of token */
 125528   int *piEndOffset,                   /* OUT: Ending offset of token */
 125529   int *piPosition                     /* OUT: Position integer of token */
 125531   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
 125532   simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
 125533   unsigned char *p = (unsigned char *)c->pInput;
 125535   while( c->iOffset<c->nBytes ){
 125536     int iStartOffset;
 125538     /* Scan past delimiter characters */
 125539     while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
 125540       c->iOffset++;
 125543     /* Count non-delimiter characters. */
 125544     iStartOffset = c->iOffset;
 125545     while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
 125546       c->iOffset++;
 125549     if( c->iOffset>iStartOffset ){
 125550       int i, n = c->iOffset-iStartOffset;
 125551       if( n>c->nTokenAllocated ){
 125552         char *pNew;
 125553         c->nTokenAllocated = n+20;
 125554         pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
 125555         if( !pNew ) return SQLITE_NOMEM;
 125556         c->pToken = pNew;
 125558       for(i=0; i<n; i++){
 125559         /* TODO(shess) This needs expansion to handle UTF-8
 125560         ** case-insensitivity.
 125562         unsigned char ch = p[iStartOffset+i];
 125563         c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
 125565       *ppToken = c->pToken;
 125566       *pnBytes = n;
 125567       *piStartOffset = iStartOffset;
 125568       *piEndOffset = c->iOffset;
 125569       *piPosition = c->iToken++;
 125571       return SQLITE_OK;
 125574   return SQLITE_DONE;
 125578 ** The set of routines that implement the simple tokenizer
 125580 static const sqlite3_tokenizer_module simpleTokenizerModule = {
 125582   simpleCreate,
 125583   simpleDestroy,
 125584   simpleOpen,
 125585   simpleClose,
 125586   simpleNext,
 125591 ** Allocate a new simple tokenizer.  Return a pointer to the new
 125592 ** tokenizer in *ppModule
 125594 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
 125595   sqlite3_tokenizer_module const**ppModule
 125597   *ppModule = &simpleTokenizerModule;
 125600 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 125602 /************** End of fts3_tokenizer1.c *************************************/
 125603 /************** Begin file fts3_write.c **************************************/
 125605 ** 2009 Oct 23
 125607 ** The author disclaims copyright to this source code.  In place of
 125608 ** a legal notice, here is a blessing:
 125610 **    May you do good and not evil.
 125611 **    May you find forgiveness for yourself and forgive others.
 125612 **    May you share freely, never taking more than you give.
 125614 ******************************************************************************
 125616 ** This file is part of the SQLite FTS3 extension module. Specifically,
 125617 ** this file contains code to insert, update and delete rows from FTS3
 125618 ** tables. It also contains code to merge FTS3 b-tree segments. Some
 125619 ** of the sub-routines used to merge segments are also used by the query 
 125620 ** code in fts3.c.
 125623 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 125625 /* #include <string.h> */
 125626 /* #include <assert.h> */
 125627 /* #include <stdlib.h> */
 125630 #define FTS_MAX_APPENDABLE_HEIGHT 16
 125633 ** When full-text index nodes are loaded from disk, the buffer that they
 125634 ** are loaded into has the following number of bytes of padding at the end 
 125635 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
 125636 ** of 920 bytes is allocated for it.
 125638 ** This means that if we have a pointer into a buffer containing node data,
 125639 ** it is always safe to read up to two varints from it without risking an
 125640 ** overread, even if the node data is corrupted.
 125642 #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
 125645 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
 125646 ** memory incrementally instead of all at once. This can be a big performance
 125647 ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
 125648 ** method before retrieving all query results (as may happen, for example,
 125649 ** if a query has a LIMIT clause).
 125651 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD 
 125652 ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
 125653 ** The code is written so that the hard lower-limit for each of these values 
 125654 ** is 1. Clearly such small values would be inefficient, but can be useful 
 125655 ** for testing purposes.
 125657 ** If this module is built with SQLITE_TEST defined, these constants may
 125658 ** be overridden at runtime for testing purposes. File fts3_test.c contains
 125659 ** a Tcl interface to read and write the values.
 125661 #ifdef SQLITE_TEST
 125662 int test_fts3_node_chunksize = (4*1024);
 125663 int test_fts3_node_chunk_threshold = (4*1024)*4;
 125664 # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
 125665 # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
 125666 #else
 125667 # define FTS3_NODE_CHUNKSIZE (4*1024) 
 125668 # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
 125669 #endif
 125672 ** The two values that may be meaningfully bound to the :1 parameter in
 125673 ** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
 125675 #define FTS_STAT_DOCTOTAL      0
 125676 #define FTS_STAT_INCRMERGEHINT 1
 125677 #define FTS_STAT_AUTOINCRMERGE 2
 125680 ** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
 125681 ** and incremental merge operation that takes place. This is used for 
 125682 ** debugging FTS only, it should not usually be turned on in production
 125683 ** systems.
 125685 #ifdef FTS3_LOG_MERGES
 125686 static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
 125687   sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
 125689 #else
 125690 #define fts3LogMerge(x, y)
 125691 #endif
 125694 typedef struct PendingList PendingList;
 125695 typedef struct SegmentNode SegmentNode;
 125696 typedef struct SegmentWriter SegmentWriter;
 125699 ** An instance of the following data structure is used to build doclists
 125700 ** incrementally. See function fts3PendingListAppend() for details.
 125702 struct PendingList {
 125703   int nData;
 125704   char *aData;
 125705   int nSpace;
 125706   sqlite3_int64 iLastDocid;
 125707   sqlite3_int64 iLastCol;
 125708   sqlite3_int64 iLastPos;
 125713 ** Each cursor has a (possibly empty) linked list of the following objects.
 125715 struct Fts3DeferredToken {
 125716   Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
 125717   int iCol;                       /* Column token must occur in */
 125718   Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
 125719   PendingList *pList;             /* Doclist is assembled here */
 125723 ** An instance of this structure is used to iterate through the terms on
 125724 ** a contiguous set of segment b-tree leaf nodes. Although the details of
 125725 ** this structure are only manipulated by code in this file, opaque handles
 125726 ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
 125727 ** terms when querying the full-text index. See functions:
 125729 **   sqlite3Fts3SegReaderNew()
 125730 **   sqlite3Fts3SegReaderFree()
 125731 **   sqlite3Fts3SegReaderIterate()
 125733 ** Methods used to manipulate Fts3SegReader structures:
 125735 **   fts3SegReaderNext()
 125736 **   fts3SegReaderFirstDocid()
 125737 **   fts3SegReaderNextDocid()
 125739 struct Fts3SegReader {
 125740   int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
 125741   u8 bLookup;                     /* True for a lookup only */
 125742   u8 rootOnly;                    /* True for a root-only reader */
 125744   sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
 125745   sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
 125746   sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
 125747   sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
 125749   char *aNode;                    /* Pointer to node data (or NULL) */
 125750   int nNode;                      /* Size of buffer at aNode (or 0) */
 125751   int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
 125752   sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
 125754   Fts3HashElem **ppNextElem;
 125756   /* Variables set by fts3SegReaderNext(). These may be read directly
 125757   ** by the caller. They are valid from the time SegmentReaderNew() returns
 125758   ** until SegmentReaderNext() returns something other than SQLITE_OK
 125759   ** (i.e. SQLITE_DONE).
 125761   int nTerm;                      /* Number of bytes in current term */
 125762   char *zTerm;                    /* Pointer to current term */
 125763   int nTermAlloc;                 /* Allocated size of zTerm buffer */
 125764   char *aDoclist;                 /* Pointer to doclist of current entry */
 125765   int nDoclist;                   /* Size of doclist in current entry */
 125767   /* The following variables are used by fts3SegReaderNextDocid() to iterate 
 125768   ** through the current doclist (aDoclist/nDoclist).
 125770   char *pOffsetList;
 125771   int nOffsetList;                /* For descending pending seg-readers only */
 125772   sqlite3_int64 iDocid;
 125775 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
 125776 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
 125779 ** An instance of this structure is used to create a segment b-tree in the
 125780 ** database. The internal details of this type are only accessed by the
 125781 ** following functions:
 125783 **   fts3SegWriterAdd()
 125784 **   fts3SegWriterFlush()
 125785 **   fts3SegWriterFree()
 125787 struct SegmentWriter {
 125788   SegmentNode *pTree;             /* Pointer to interior tree structure */
 125789   sqlite3_int64 iFirst;           /* First slot in %_segments written */
 125790   sqlite3_int64 iFree;            /* Next free slot in %_segments */
 125791   char *zTerm;                    /* Pointer to previous term buffer */
 125792   int nTerm;                      /* Number of bytes in zTerm */
 125793   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
 125794   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
 125795   int nSize;                      /* Size of allocation at aData */
 125796   int nData;                      /* Bytes of data in aData */
 125797   char *aData;                    /* Pointer to block from malloc() */
 125801 ** Type SegmentNode is used by the following three functions to create
 125802 ** the interior part of the segment b+-tree structures (everything except
 125803 ** the leaf nodes). These functions and type are only ever used by code
 125804 ** within the fts3SegWriterXXX() family of functions described above.
 125806 **   fts3NodeAddTerm()
 125807 **   fts3NodeWrite()
 125808 **   fts3NodeFree()
 125810 ** When a b+tree is written to the database (either as a result of a merge
 125811 ** or the pending-terms table being flushed), leaves are written into the 
 125812 ** database file as soon as they are completely populated. The interior of
 125813 ** the tree is assembled in memory and written out only once all leaves have
 125814 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
 125815 ** very large, meaning that the interior of the tree consumes relatively 
 125816 ** little memory.
 125818 struct SegmentNode {
 125819   SegmentNode *pParent;           /* Parent node (or NULL for root node) */
 125820   SegmentNode *pRight;            /* Pointer to right-sibling */
 125821   SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
 125822   int nEntry;                     /* Number of terms written to node so far */
 125823   char *zTerm;                    /* Pointer to previous term buffer */
 125824   int nTerm;                      /* Number of bytes in zTerm */
 125825   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
 125826   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
 125827   int nData;                      /* Bytes of valid data so far */
 125828   char *aData;                    /* Node data */
 125832 ** Valid values for the second argument to fts3SqlStmt().
 125834 #define SQL_DELETE_CONTENT             0
 125835 #define SQL_IS_EMPTY                   1
 125836 #define SQL_DELETE_ALL_CONTENT         2 
 125837 #define SQL_DELETE_ALL_SEGMENTS        3
 125838 #define SQL_DELETE_ALL_SEGDIR          4
 125839 #define SQL_DELETE_ALL_DOCSIZE         5
 125840 #define SQL_DELETE_ALL_STAT            6
 125841 #define SQL_SELECT_CONTENT_BY_ROWID    7
 125842 #define SQL_NEXT_SEGMENT_INDEX         8
 125843 #define SQL_INSERT_SEGMENTS            9
 125844 #define SQL_NEXT_SEGMENTS_ID          10
 125845 #define SQL_INSERT_SEGDIR             11
 125846 #define SQL_SELECT_LEVEL              12
 125847 #define SQL_SELECT_LEVEL_RANGE        13
 125848 #define SQL_SELECT_LEVEL_COUNT        14
 125849 #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
 125850 #define SQL_DELETE_SEGDIR_LEVEL       16
 125851 #define SQL_DELETE_SEGMENTS_RANGE     17
 125852 #define SQL_CONTENT_INSERT            18
 125853 #define SQL_DELETE_DOCSIZE            19
 125854 #define SQL_REPLACE_DOCSIZE           20
 125855 #define SQL_SELECT_DOCSIZE            21
 125856 #define SQL_SELECT_STAT               22
 125857 #define SQL_REPLACE_STAT              23
 125859 #define SQL_SELECT_ALL_PREFIX_LEVEL   24
 125860 #define SQL_DELETE_ALL_TERMS_SEGDIR   25
 125861 #define SQL_DELETE_SEGDIR_RANGE       26
 125862 #define SQL_SELECT_ALL_LANGID         27
 125863 #define SQL_FIND_MERGE_LEVEL          28
 125864 #define SQL_MAX_LEAF_NODE_ESTIMATE    29
 125865 #define SQL_DELETE_SEGDIR_ENTRY       30
 125866 #define SQL_SHIFT_SEGDIR_ENTRY        31
 125867 #define SQL_SELECT_SEGDIR             32
 125868 #define SQL_CHOMP_SEGDIR              33
 125869 #define SQL_SEGMENT_IS_APPENDABLE     34
 125870 #define SQL_SELECT_INDEXES            35
 125871 #define SQL_SELECT_MXLEVEL            36
 125874 ** This function is used to obtain an SQLite prepared statement handle
 125875 ** for the statement identified by the second argument. If successful,
 125876 ** *pp is set to the requested statement handle and SQLITE_OK returned.
 125877 ** Otherwise, an SQLite error code is returned and *pp is set to 0.
 125879 ** If argument apVal is not NULL, then it must point to an array with
 125880 ** at least as many entries as the requested statement has bound 
 125881 ** parameters. The values are bound to the statements parameters before
 125882 ** returning.
 125884 static int fts3SqlStmt(
 125885   Fts3Table *p,                   /* Virtual table handle */
 125886   int eStmt,                      /* One of the SQL_XXX constants above */
 125887   sqlite3_stmt **pp,              /* OUT: Statement handle */
 125888   sqlite3_value **apVal           /* Values to bind to statement */
 125890   const char *azSql[] = {
 125891 /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
 125892 /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
 125893 /* 2  */  "DELETE FROM %Q.'%q_content'",
 125894 /* 3  */  "DELETE FROM %Q.'%q_segments'",
 125895 /* 4  */  "DELETE FROM %Q.'%q_segdir'",
 125896 /* 5  */  "DELETE FROM %Q.'%q_docsize'",
 125897 /* 6  */  "DELETE FROM %Q.'%q_stat'",
 125898 /* 7  */  "SELECT %s WHERE rowid=?",
 125899 /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
 125900 /* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
 125901 /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
 125902 /* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
 125904           /* Return segments in order from oldest to newest.*/ 
 125905 /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
 125906             "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
 125907 /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
 125908             "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
 125909             "ORDER BY level DESC, idx ASC",
 125911 /* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
 125912 /* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
 125914 /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
 125915 /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
 125916 /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
 125917 /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
 125918 /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
 125919 /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
 125920 /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
 125921 /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
 125922 /* 24 */  "",
 125923 /* 25 */  "",
 125925 /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
 125926 /* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
 125928 /* This statement is used to determine which level to read the input from
 125929 ** when performing an incremental merge. It returns the absolute level number
 125930 ** of the oldest level in the db that contains at least ? segments. Or,
 125931 ** if no level in the FTS index contains more than ? segments, the statement
 125932 ** returns zero rows.  */
 125933 /* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
 125934          "  ORDER BY (level %% 1024) ASC LIMIT 1",
 125936 /* Estimate the upper limit on the number of leaf nodes in a new segment
 125937 ** created by merging the oldest :2 segments from absolute level :1. See 
 125938 ** function sqlite3Fts3Incrmerge() for details.  */
 125939 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
 125940          "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
 125942 /* SQL_DELETE_SEGDIR_ENTRY
 125943 **   Delete the %_segdir entry on absolute level :1 with index :2.  */
 125944 /* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
 125946 /* SQL_SHIFT_SEGDIR_ENTRY
 125947 **   Modify the idx value for the segment with idx=:3 on absolute level :2
 125948 **   to :1.  */
 125949 /* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
 125951 /* SQL_SELECT_SEGDIR
 125952 **   Read a single entry from the %_segdir table. The entry from absolute 
 125953 **   level :1 with index value :2.  */
 125954 /* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
 125955             "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
 125957 /* SQL_CHOMP_SEGDIR
 125958 **   Update the start_block (:1) and root (:2) fields of the %_segdir
 125959 **   entry located on absolute level :3 with index :4.  */
 125960 /* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
 125961             "WHERE level = ? AND idx = ?",
 125963 /* SQL_SEGMENT_IS_APPENDABLE
 125964 **   Return a single row if the segment with end_block=? is appendable. Or
 125965 **   no rows otherwise.  */
 125966 /* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
 125968 /* SQL_SELECT_INDEXES
 125969 **   Return the list of valid segment indexes for absolute level ?  */
 125970 /* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
 125972 /* SQL_SELECT_MXLEVEL
 125973 **   Return the largest relative level in the FTS index or indexes.  */
 125974 /* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
 125976   int rc = SQLITE_OK;
 125977   sqlite3_stmt *pStmt;
 125979   assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
 125980   assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
 125982   pStmt = p->aStmt[eStmt];
 125983   if( !pStmt ){
 125984     char *zSql;
 125985     if( eStmt==SQL_CONTENT_INSERT ){
 125986       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
 125987     }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
 125988       zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
 125989     }else{
 125990       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
 125992     if( !zSql ){
 125993       rc = SQLITE_NOMEM;
 125994     }else{
 125995       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
 125996       sqlite3_free(zSql);
 125997       assert( rc==SQLITE_OK || pStmt==0 );
 125998       p->aStmt[eStmt] = pStmt;
 126001   if( apVal ){
 126002     int i;
 126003     int nParam = sqlite3_bind_parameter_count(pStmt);
 126004     for(i=0; rc==SQLITE_OK && i<nParam; i++){
 126005       rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
 126008   *pp = pStmt;
 126009   return rc;
 126013 static int fts3SelectDocsize(
 126014   Fts3Table *pTab,                /* FTS3 table handle */
 126015   sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
 126016   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
 126018   sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
 126019   int rc;                         /* Return code */
 126021   rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
 126022   if( rc==SQLITE_OK ){
 126023     sqlite3_bind_int64(pStmt, 1, iDocid);
 126024     rc = sqlite3_step(pStmt);
 126025     if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
 126026       rc = sqlite3_reset(pStmt);
 126027       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
 126028       pStmt = 0;
 126029     }else{
 126030       rc = SQLITE_OK;
 126034   *ppStmt = pStmt;
 126035   return rc;
 126038 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
 126039   Fts3Table *pTab,                /* Fts3 table handle */
 126040   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
 126042   sqlite3_stmt *pStmt = 0;
 126043   int rc;
 126044   rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
 126045   if( rc==SQLITE_OK ){
 126046     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
 126047     if( sqlite3_step(pStmt)!=SQLITE_ROW
 126048      || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
 126050       rc = sqlite3_reset(pStmt);
 126051       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
 126052       pStmt = 0;
 126055   *ppStmt = pStmt;
 126056   return rc;
 126059 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
 126060   Fts3Table *pTab,                /* Fts3 table handle */
 126061   sqlite3_int64 iDocid,           /* Docid to read size data for */
 126062   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
 126064   return fts3SelectDocsize(pTab, iDocid, ppStmt);
 126068 ** Similar to fts3SqlStmt(). Except, after binding the parameters in
 126069 ** array apVal[] to the SQL statement identified by eStmt, the statement
 126070 ** is executed.
 126072 ** Returns SQLITE_OK if the statement is successfully executed, or an
 126073 ** SQLite error code otherwise.
 126075 static void fts3SqlExec(
 126076   int *pRC,                /* Result code */
 126077   Fts3Table *p,            /* The FTS3 table */
 126078   int eStmt,               /* Index of statement to evaluate */
 126079   sqlite3_value **apVal    /* Parameters to bind */
 126081   sqlite3_stmt *pStmt;
 126082   int rc;
 126083   if( *pRC ) return;
 126084   rc = fts3SqlStmt(p, eStmt, &pStmt, apVal); 
 126085   if( rc==SQLITE_OK ){
 126086     sqlite3_step(pStmt);
 126087     rc = sqlite3_reset(pStmt);
 126089   *pRC = rc;
 126094 ** This function ensures that the caller has obtained a shared-cache
 126095 ** table-lock on the %_content table. This is required before reading
 126096 ** data from the fts3 table. If this lock is not acquired first, then
 126097 ** the caller may end up holding read-locks on the %_segments and %_segdir
 126098 ** tables, but no read-lock on the %_content table. If this happens 
 126099 ** a second connection will be able to write to the fts3 table, but
 126100 ** attempting to commit those writes might return SQLITE_LOCKED or
 126101 ** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain 
 126102 ** write-locks on the %_segments and %_segdir ** tables). 
 126104 ** We try to avoid this because if FTS3 returns any error when committing
 126105 ** a transaction, the whole transaction will be rolled back. And this is
 126106 ** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
 126107 ** still happen if the user reads data directly from the %_segments or
 126108 ** %_segdir tables instead of going through FTS3 though.
 126110 ** This reasoning does not apply to a content=xxx table.
 126112 SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
 126113   int rc;                         /* Return code */
 126114   sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
 126116   if( p->zContentTbl==0 ){
 126117     rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
 126118     if( rc==SQLITE_OK ){
 126119       sqlite3_bind_null(pStmt, 1);
 126120       sqlite3_step(pStmt);
 126121       rc = sqlite3_reset(pStmt);
 126123   }else{
 126124     rc = SQLITE_OK;
 126127   return rc;
 126131 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
 126132 ** Within each language id, a separate index is maintained to store the
 126133 ** document terms, and each configured prefix size (configured the FTS 
 126134 ** "prefix=" option). And each index consists of multiple levels ("relative
 126135 ** levels").
 126137 ** All three of these values (the language id, the specific index and the
 126138 ** level within the index) are encoded in 64-bit integer values stored
 126139 ** in the %_segdir table on disk. This function is used to convert three
 126140 ** separate component values into the single 64-bit integer value that
 126141 ** can be used to query the %_segdir table.
 126143 ** Specifically, each language-id/index combination is allocated 1024 
 126144 ** 64-bit integer level values ("absolute levels"). The main terms index
 126145 ** for language-id 0 is allocate values 0-1023. The first prefix index
 126146 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
 126147 ** Language 1 indexes are allocated immediately following language 0.
 126149 ** So, for a system with nPrefix prefix indexes configured, the block of
 126150 ** absolute levels that corresponds to language-id iLangid and index 
 126151 ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
 126153 static sqlite3_int64 getAbsoluteLevel(
 126154   Fts3Table *p,                   /* FTS3 table handle */
 126155   int iLangid,                    /* Language id */
 126156   int iIndex,                     /* Index in p->aIndex[] */
 126157   int iLevel                      /* Level of segments */
 126159   sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
 126160   assert( iLangid>=0 );
 126161   assert( p->nIndex>0 );
 126162   assert( iIndex>=0 && iIndex<p->nIndex );
 126164   iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
 126165   return iBase + iLevel;
 126169 ** Set *ppStmt to a statement handle that may be used to iterate through
 126170 ** all rows in the %_segdir table, from oldest to newest. If successful,
 126171 ** return SQLITE_OK. If an error occurs while preparing the statement, 
 126172 ** return an SQLite error code.
 126174 ** There is only ever one instance of this SQL statement compiled for
 126175 ** each FTS3 table.
 126177 ** The statement returns the following columns from the %_segdir table:
 126179 **   0: idx
 126180 **   1: start_block
 126181 **   2: leaves_end_block
 126182 **   3: end_block
 126183 **   4: root
 126185 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
 126186   Fts3Table *p,                   /* FTS3 table */
 126187   int iLangid,                    /* Language being queried */
 126188   int iIndex,                     /* Index for p->aIndex[] */
 126189   int iLevel,                     /* Level to select (relative level) */
 126190   sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
 126192   int rc;
 126193   sqlite3_stmt *pStmt = 0;
 126195   assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
 126196   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
 126197   assert( iIndex>=0 && iIndex<p->nIndex );
 126199   if( iLevel<0 ){
 126200     /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
 126201     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
 126202     if( rc==SQLITE_OK ){ 
 126203       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
 126204       sqlite3_bind_int64(pStmt, 2, 
 126205           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
 126208   }else{
 126209     /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
 126210     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
 126211     if( rc==SQLITE_OK ){ 
 126212       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
 126215   *ppStmt = pStmt;
 126216   return rc;
 126221 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
 126222 ** if successful, or an SQLite error code otherwise.
 126224 ** This function also serves to allocate the PendingList structure itself.
 126225 ** For example, to create a new PendingList structure containing two
 126226 ** varints:
 126228 **   PendingList *p = 0;
 126229 **   fts3PendingListAppendVarint(&p, 1);
 126230 **   fts3PendingListAppendVarint(&p, 2);
 126232 static int fts3PendingListAppendVarint(
 126233   PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
 126234   sqlite3_int64 i                 /* Value to append to data */
 126236   PendingList *p = *pp;
 126238   /* Allocate or grow the PendingList as required. */
 126239   if( !p ){
 126240     p = sqlite3_malloc(sizeof(*p) + 100);
 126241     if( !p ){
 126242       return SQLITE_NOMEM;
 126244     p->nSpace = 100;
 126245     p->aData = (char *)&p[1];
 126246     p->nData = 0;
 126248   else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
 126249     int nNew = p->nSpace * 2;
 126250     p = sqlite3_realloc(p, sizeof(*p) + nNew);
 126251     if( !p ){
 126252       sqlite3_free(*pp);
 126253       *pp = 0;
 126254       return SQLITE_NOMEM;
 126256     p->nSpace = nNew;
 126257     p->aData = (char *)&p[1];
 126260   /* Append the new serialized varint to the end of the list. */
 126261   p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
 126262   p->aData[p->nData] = '\0';
 126263   *pp = p;
 126264   return SQLITE_OK;
 126268 ** Add a docid/column/position entry to a PendingList structure. Non-zero
 126269 ** is returned if the structure is sqlite3_realloced as part of adding
 126270 ** the entry. Otherwise, zero.
 126272 ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
 126273 ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
 126274 ** it is set to SQLITE_OK.
 126276 static int fts3PendingListAppend(
 126277   PendingList **pp,               /* IN/OUT: PendingList structure */
 126278   sqlite3_int64 iDocid,           /* Docid for entry to add */
 126279   sqlite3_int64 iCol,             /* Column for entry to add */
 126280   sqlite3_int64 iPos,             /* Position of term for entry to add */
 126281   int *pRc                        /* OUT: Return code */
 126283   PendingList *p = *pp;
 126284   int rc = SQLITE_OK;
 126286   assert( !p || p->iLastDocid<=iDocid );
 126288   if( !p || p->iLastDocid!=iDocid ){
 126289     sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
 126290     if( p ){
 126291       assert( p->nData<p->nSpace );
 126292       assert( p->aData[p->nData]==0 );
 126293       p->nData++;
 126295     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
 126296       goto pendinglistappend_out;
 126298     p->iLastCol = -1;
 126299     p->iLastPos = 0;
 126300     p->iLastDocid = iDocid;
 126302   if( iCol>0 && p->iLastCol!=iCol ){
 126303     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
 126304      || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
 126306       goto pendinglistappend_out;
 126308     p->iLastCol = iCol;
 126309     p->iLastPos = 0;
 126311   if( iCol>=0 ){
 126312     assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
 126313     rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
 126314     if( rc==SQLITE_OK ){
 126315       p->iLastPos = iPos;
 126319  pendinglistappend_out:
 126320   *pRc = rc;
 126321   if( p!=*pp ){
 126322     *pp = p;
 126323     return 1;
 126325   return 0;
 126329 ** Free a PendingList object allocated by fts3PendingListAppend().
 126331 static void fts3PendingListDelete(PendingList *pList){
 126332   sqlite3_free(pList);
 126336 ** Add an entry to one of the pending-terms hash tables.
 126338 static int fts3PendingTermsAddOne(
 126339   Fts3Table *p,
 126340   int iCol,
 126341   int iPos,
 126342   Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
 126343   const char *zToken,
 126344   int nToken
 126346   PendingList *pList;
 126347   int rc = SQLITE_OK;
 126349   pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
 126350   if( pList ){
 126351     p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
 126353   if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
 126354     if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
 126355       /* Malloc failed while inserting the new entry. This can only 
 126356       ** happen if there was no previous entry for this token.
 126358       assert( 0==fts3HashFind(pHash, zToken, nToken) );
 126359       sqlite3_free(pList);
 126360       rc = SQLITE_NOMEM;
 126363   if( rc==SQLITE_OK ){
 126364     p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
 126366   return rc;
 126370 ** Tokenize the nul-terminated string zText and add all tokens to the
 126371 ** pending-terms hash-table. The docid used is that currently stored in
 126372 ** p->iPrevDocid, and the column is specified by argument iCol.
 126374 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
 126376 static int fts3PendingTermsAdd(
 126377   Fts3Table *p,                   /* Table into which text will be inserted */
 126378   int iLangid,                    /* Language id to use */
 126379   const char *zText,              /* Text of document to be inserted */
 126380   int iCol,                       /* Column into which text is being inserted */
 126381   u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
 126383   int rc;
 126384   int iStart = 0;
 126385   int iEnd = 0;
 126386   int iPos = 0;
 126387   int nWord = 0;
 126389   char const *zToken;
 126390   int nToken = 0;
 126392   sqlite3_tokenizer *pTokenizer = p->pTokenizer;
 126393   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 126394   sqlite3_tokenizer_cursor *pCsr;
 126395   int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
 126396       const char**,int*,int*,int*,int*);
 126398   assert( pTokenizer && pModule );
 126400   /* If the user has inserted a NULL value, this function may be called with
 126401   ** zText==0. In this case, add zero token entries to the hash table and 
 126402   ** return early. */
 126403   if( zText==0 ){
 126404     *pnWord = 0;
 126405     return SQLITE_OK;
 126408   rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
 126409   if( rc!=SQLITE_OK ){
 126410     return rc;
 126413   xNext = pModule->xNext;
 126414   while( SQLITE_OK==rc
 126415       && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
 126417     int i;
 126418     if( iPos>=nWord ) nWord = iPos+1;
 126420     /* Positions cannot be negative; we use -1 as a terminator internally.
 126421     ** Tokens must have a non-zero length.
 126423     if( iPos<0 || !zToken || nToken<=0 ){
 126424       rc = SQLITE_ERROR;
 126425       break;
 126428     /* Add the term to the terms index */
 126429     rc = fts3PendingTermsAddOne(
 126430         p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
 126433     /* Add the term to each of the prefix indexes that it is not too 
 126434     ** short for. */
 126435     for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
 126436       struct Fts3Index *pIndex = &p->aIndex[i];
 126437       if( nToken<pIndex->nPrefix ) continue;
 126438       rc = fts3PendingTermsAddOne(
 126439           p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
 126444   pModule->xClose(pCsr);
 126445   *pnWord += nWord;
 126446   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
 126450 ** Calling this function indicates that subsequent calls to 
 126451 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
 126452 ** contents of the document with docid iDocid.
 126454 static int fts3PendingTermsDocid(
 126455   Fts3Table *p,                   /* Full-text table handle */
 126456   int iLangid,                    /* Language id of row being written */
 126457   sqlite_int64 iDocid             /* Docid of row being written */
 126459   assert( iLangid>=0 );
 126461   /* TODO(shess) Explore whether partially flushing the buffer on
 126462   ** forced-flush would provide better performance.  I suspect that if
 126463   ** we ordered the doclists by size and flushed the largest until the
 126464   ** buffer was half empty, that would let the less frequent terms
 126465   ** generate longer doclists.
 126467   if( iDocid<=p->iPrevDocid 
 126468    || p->iPrevLangid!=iLangid
 126469    || p->nPendingData>p->nMaxPendingData 
 126471     int rc = sqlite3Fts3PendingTermsFlush(p);
 126472     if( rc!=SQLITE_OK ) return rc;
 126474   p->iPrevDocid = iDocid;
 126475   p->iPrevLangid = iLangid;
 126476   return SQLITE_OK;
 126480 ** Discard the contents of the pending-terms hash tables. 
 126482 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
 126483   int i;
 126484   for(i=0; i<p->nIndex; i++){
 126485     Fts3HashElem *pElem;
 126486     Fts3Hash *pHash = &p->aIndex[i].hPending;
 126487     for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
 126488       PendingList *pList = (PendingList *)fts3HashData(pElem);
 126489       fts3PendingListDelete(pList);
 126491     fts3HashClear(pHash);
 126493   p->nPendingData = 0;
 126497 ** This function is called by the xUpdate() method as part of an INSERT
 126498 ** operation. It adds entries for each term in the new record to the
 126499 ** pendingTerms hash table.
 126501 ** Argument apVal is the same as the similarly named argument passed to
 126502 ** fts3InsertData(). Parameter iDocid is the docid of the new row.
 126504 static int fts3InsertTerms(
 126505   Fts3Table *p, 
 126506   int iLangid, 
 126507   sqlite3_value **apVal, 
 126508   u32 *aSz
 126510   int i;                          /* Iterator variable */
 126511   for(i=2; i<p->nColumn+2; i++){
 126512     const char *zText = (const char *)sqlite3_value_text(apVal[i]);
 126513     int rc = fts3PendingTermsAdd(p, iLangid, zText, i-2, &aSz[i-2]);
 126514     if( rc!=SQLITE_OK ){
 126515       return rc;
 126517     aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
 126519   return SQLITE_OK;
 126523 ** This function is called by the xUpdate() method for an INSERT operation.
 126524 ** The apVal parameter is passed a copy of the apVal argument passed by
 126525 ** SQLite to the xUpdate() method. i.e:
 126527 **   apVal[0]                Not used for INSERT.
 126528 **   apVal[1]                rowid
 126529 **   apVal[2]                Left-most user-defined column
 126530 **   ...
 126531 **   apVal[p->nColumn+1]     Right-most user-defined column
 126532 **   apVal[p->nColumn+2]     Hidden column with same name as table
 126533 **   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
 126534 **   apVal[p->nColumn+4]     Hidden languageid column
 126536 static int fts3InsertData(
 126537   Fts3Table *p,                   /* Full-text table */
 126538   sqlite3_value **apVal,          /* Array of values to insert */
 126539   sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
 126541   int rc;                         /* Return code */
 126542   sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
 126544   if( p->zContentTbl ){
 126545     sqlite3_value *pRowid = apVal[p->nColumn+3];
 126546     if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
 126547       pRowid = apVal[1];
 126549     if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
 126550       return SQLITE_CONSTRAINT;
 126552     *piDocid = sqlite3_value_int64(pRowid);
 126553     return SQLITE_OK;
 126556   /* Locate the statement handle used to insert data into the %_content
 126557   ** table. The SQL for this statement is:
 126559   **   INSERT INTO %_content VALUES(?, ?, ?, ...)
 126561   ** The statement features N '?' variables, where N is the number of user
 126562   ** defined columns in the FTS3 table, plus one for the docid field.
 126564   rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
 126565   if( rc==SQLITE_OK && p->zLanguageid ){
 126566     rc = sqlite3_bind_int(
 126567         pContentInsert, p->nColumn+2, 
 126568         sqlite3_value_int(apVal[p->nColumn+4])
 126571   if( rc!=SQLITE_OK ) return rc;
 126573   /* There is a quirk here. The users INSERT statement may have specified
 126574   ** a value for the "rowid" field, for the "docid" field, or for both.
 126575   ** Which is a problem, since "rowid" and "docid" are aliases for the
 126576   ** same value. For example:
 126578   **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
 126580   ** In FTS3, this is an error. It is an error to specify non-NULL values
 126581   ** for both docid and some other rowid alias.
 126583   if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
 126584     if( SQLITE_NULL==sqlite3_value_type(apVal[0])
 126585      && SQLITE_NULL!=sqlite3_value_type(apVal[1])
 126587       /* A rowid/docid conflict. */
 126588       return SQLITE_ERROR;
 126590     rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
 126591     if( rc!=SQLITE_OK ) return rc;
 126594   /* Execute the statement to insert the record. Set *piDocid to the 
 126595   ** new docid value. 
 126597   sqlite3_step(pContentInsert);
 126598   rc = sqlite3_reset(pContentInsert);
 126600   *piDocid = sqlite3_last_insert_rowid(p->db);
 126601   return rc;
 126607 ** Remove all data from the FTS3 table. Clear the hash table containing
 126608 ** pending terms.
 126610 static int fts3DeleteAll(Fts3Table *p, int bContent){
 126611   int rc = SQLITE_OK;             /* Return code */
 126613   /* Discard the contents of the pending-terms hash table. */
 126614   sqlite3Fts3PendingTermsClear(p);
 126616   /* Delete everything from the shadow tables. Except, leave %_content as
 126617   ** is if bContent is false.  */
 126618   assert( p->zContentTbl==0 || bContent==0 );
 126619   if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
 126620   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
 126621   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
 126622   if( p->bHasDocsize ){
 126623     fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
 126625   if( p->bHasStat ){
 126626     fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
 126628   return rc;
 126634 static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
 126635   int iLangid = 0;
 126636   if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
 126637   return iLangid;
 126641 ** The first element in the apVal[] array is assumed to contain the docid
 126642 ** (an integer) of a row about to be deleted. Remove all terms from the
 126643 ** full-text index.
 126645 static void fts3DeleteTerms( 
 126646   int *pRC,               /* Result code */
 126647   Fts3Table *p,           /* The FTS table to delete from */
 126648   sqlite3_value *pRowid,  /* The docid to be deleted */
 126649   u32 *aSz,               /* Sizes of deleted document written here */
 126650   int *pbFound            /* OUT: Set to true if row really does exist */
 126652   int rc;
 126653   sqlite3_stmt *pSelect;
 126655   assert( *pbFound==0 );
 126656   if( *pRC ) return;
 126657   rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
 126658   if( rc==SQLITE_OK ){
 126659     if( SQLITE_ROW==sqlite3_step(pSelect) ){
 126660       int i;
 126661       int iLangid = langidFromSelect(p, pSelect);
 126662       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
 126663       for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
 126664         const char *zText = (const char *)sqlite3_column_text(pSelect, i);
 126665         rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[i-1]);
 126666         aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
 126668       if( rc!=SQLITE_OK ){
 126669         sqlite3_reset(pSelect);
 126670         *pRC = rc;
 126671         return;
 126673       *pbFound = 1;
 126675     rc = sqlite3_reset(pSelect);
 126676   }else{
 126677     sqlite3_reset(pSelect);
 126679   *pRC = rc;
 126683 ** Forward declaration to account for the circular dependency between
 126684 ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
 126686 static int fts3SegmentMerge(Fts3Table *, int, int, int);
 126689 ** This function allocates a new level iLevel index in the segdir table.
 126690 ** Usually, indexes are allocated within a level sequentially starting
 126691 ** with 0, so the allocated index is one greater than the value returned
 126692 ** by:
 126694 **   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
 126696 ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
 126697 ** level, they are merged into a single level (iLevel+1) segment and the 
 126698 ** allocated index is 0.
 126700 ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
 126701 ** returned. Otherwise, an SQLite error code is returned.
 126703 static int fts3AllocateSegdirIdx(
 126704   Fts3Table *p, 
 126705   int iLangid,                    /* Language id */
 126706   int iIndex,                     /* Index for p->aIndex */
 126707   int iLevel, 
 126708   int *piIdx
 126710   int rc;                         /* Return Code */
 126711   sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
 126712   int iNext = 0;                  /* Result of query pNextIdx */
 126714   assert( iLangid>=0 );
 126715   assert( p->nIndex>=1 );
 126717   /* Set variable iNext to the next available segdir index at level iLevel. */
 126718   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
 126719   if( rc==SQLITE_OK ){
 126720     sqlite3_bind_int64(
 126721         pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
 126723     if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
 126724       iNext = sqlite3_column_int(pNextIdx, 0);
 126726     rc = sqlite3_reset(pNextIdx);
 126729   if( rc==SQLITE_OK ){
 126730     /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
 126731     ** full, merge all segments in level iLevel into a single iLevel+1
 126732     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
 126733     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
 126735     if( iNext>=FTS3_MERGE_COUNT ){
 126736       fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
 126737       rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
 126738       *piIdx = 0;
 126739     }else{
 126740       *piIdx = iNext;
 126744   return rc;
 126748 ** The %_segments table is declared as follows:
 126750 **   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
 126752 ** This function reads data from a single row of the %_segments table. The
 126753 ** specific row is identified by the iBlockid parameter. If paBlob is not
 126754 ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
 126755 ** with the contents of the blob stored in the "block" column of the 
 126756 ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
 126757 ** to the size of the blob in bytes before returning.
 126759 ** If an error occurs, or the table does not contain the specified row,
 126760 ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
 126761 ** paBlob is non-NULL, then it is the responsibility of the caller to
 126762 ** eventually free the returned buffer.
 126764 ** This function may leave an open sqlite3_blob* handle in the
 126765 ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
 126766 ** to this function. The handle may be closed by calling the
 126767 ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
 126768 ** performance improvement, but the blob handle should always be closed
 126769 ** before control is returned to the user (to prevent a lock being held
 126770 ** on the database file for longer than necessary). Thus, any virtual table
 126771 ** method (xFilter etc.) that may directly or indirectly call this function
 126772 ** must call sqlite3Fts3SegmentsClose() before returning.
 126774 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
 126775   Fts3Table *p,                   /* FTS3 table handle */
 126776   sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
 126777   char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
 126778   int *pnBlob,                    /* OUT: Size of blob data */
 126779   int *pnLoad                     /* OUT: Bytes actually loaded */
 126781   int rc;                         /* Return code */
 126783   /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
 126784   assert( pnBlob );
 126786   if( p->pSegments ){
 126787     rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
 126788   }else{
 126789     if( 0==p->zSegmentsTbl ){
 126790       p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
 126791       if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
 126793     rc = sqlite3_blob_open(
 126794        p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
 126798   if( rc==SQLITE_OK ){
 126799     int nByte = sqlite3_blob_bytes(p->pSegments);
 126800     *pnBlob = nByte;
 126801     if( paBlob ){
 126802       char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
 126803       if( !aByte ){
 126804         rc = SQLITE_NOMEM;
 126805       }else{
 126806         if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
 126807           nByte = FTS3_NODE_CHUNKSIZE;
 126808           *pnLoad = nByte;
 126810         rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
 126811         memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
 126812         if( rc!=SQLITE_OK ){
 126813           sqlite3_free(aByte);
 126814           aByte = 0;
 126817       *paBlob = aByte;
 126821   return rc;
 126825 ** Close the blob handle at p->pSegments, if it is open. See comments above
 126826 ** the sqlite3Fts3ReadBlock() function for details.
 126828 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
 126829   sqlite3_blob_close(p->pSegments);
 126830   p->pSegments = 0;
 126833 static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
 126834   int nRead;                      /* Number of bytes to read */
 126835   int rc;                         /* Return code */
 126837   nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
 126838   rc = sqlite3_blob_read(
 126839       pReader->pBlob, 
 126840       &pReader->aNode[pReader->nPopulate],
 126841       nRead,
 126842       pReader->nPopulate
 126845   if( rc==SQLITE_OK ){
 126846     pReader->nPopulate += nRead;
 126847     memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
 126848     if( pReader->nPopulate==pReader->nNode ){
 126849       sqlite3_blob_close(pReader->pBlob);
 126850       pReader->pBlob = 0;
 126851       pReader->nPopulate = 0;
 126854   return rc;
 126857 static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
 126858   int rc = SQLITE_OK;
 126859   assert( !pReader->pBlob 
 126860        || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
 126862   while( pReader->pBlob && rc==SQLITE_OK 
 126863      &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
 126865     rc = fts3SegReaderIncrRead(pReader);
 126867   return rc;
 126871 ** Set an Fts3SegReader cursor to point at EOF.
 126873 static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
 126874   if( !fts3SegReaderIsRootOnly(pSeg) ){
 126875     sqlite3_free(pSeg->aNode);
 126876     sqlite3_blob_close(pSeg->pBlob);
 126877     pSeg->pBlob = 0;
 126879   pSeg->aNode = 0;
 126883 ** Move the iterator passed as the first argument to the next term in the
 126884 ** segment. If successful, SQLITE_OK is returned. If there is no next term,
 126885 ** SQLITE_DONE. Otherwise, an SQLite error code.
 126887 static int fts3SegReaderNext(
 126888   Fts3Table *p, 
 126889   Fts3SegReader *pReader,
 126890   int bIncr
 126892   int rc;                         /* Return code of various sub-routines */
 126893   char *pNext;                    /* Cursor variable */
 126894   int nPrefix;                    /* Number of bytes in term prefix */
 126895   int nSuffix;                    /* Number of bytes in term suffix */
 126897   if( !pReader->aDoclist ){
 126898     pNext = pReader->aNode;
 126899   }else{
 126900     pNext = &pReader->aDoclist[pReader->nDoclist];
 126903   if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
 126905     if( fts3SegReaderIsPending(pReader) ){
 126906       Fts3HashElem *pElem = *(pReader->ppNextElem);
 126907       if( pElem==0 ){
 126908         pReader->aNode = 0;
 126909       }else{
 126910         PendingList *pList = (PendingList *)fts3HashData(pElem);
 126911         pReader->zTerm = (char *)fts3HashKey(pElem);
 126912         pReader->nTerm = fts3HashKeysize(pElem);
 126913         pReader->nNode = pReader->nDoclist = pList->nData + 1;
 126914         pReader->aNode = pReader->aDoclist = pList->aData;
 126915         pReader->ppNextElem++;
 126916         assert( pReader->aNode );
 126918       return SQLITE_OK;
 126921     fts3SegReaderSetEof(pReader);
 126923     /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
 126924     ** blocks have already been traversed.  */
 126925     assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
 126926     if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
 126927       return SQLITE_OK;
 126930     rc = sqlite3Fts3ReadBlock(
 126931         p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode, 
 126932         (bIncr ? &pReader->nPopulate : 0)
 126934     if( rc!=SQLITE_OK ) return rc;
 126935     assert( pReader->pBlob==0 );
 126936     if( bIncr && pReader->nPopulate<pReader->nNode ){
 126937       pReader->pBlob = p->pSegments;
 126938       p->pSegments = 0;
 126940     pNext = pReader->aNode;
 126943   assert( !fts3SegReaderIsPending(pReader) );
 126945   rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
 126946   if( rc!=SQLITE_OK ) return rc;
 126948   /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
 126949   ** safe (no risk of overread) even if the node data is corrupted. */
 126950   pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
 126951   pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
 126952   if( nPrefix<0 || nSuffix<=0 
 126953    || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
 126955     return FTS_CORRUPT_VTAB;
 126958   if( nPrefix+nSuffix>pReader->nTermAlloc ){
 126959     int nNew = (nPrefix+nSuffix)*2;
 126960     char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
 126961     if( !zNew ){
 126962       return SQLITE_NOMEM;
 126964     pReader->zTerm = zNew;
 126965     pReader->nTermAlloc = nNew;
 126968   rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
 126969   if( rc!=SQLITE_OK ) return rc;
 126971   memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
 126972   pReader->nTerm = nPrefix+nSuffix;
 126973   pNext += nSuffix;
 126974   pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist);
 126975   pReader->aDoclist = pNext;
 126976   pReader->pOffsetList = 0;
 126978   /* Check that the doclist does not appear to extend past the end of the
 126979   ** b-tree node. And that the final byte of the doclist is 0x00. If either 
 126980   ** of these statements is untrue, then the data structure is corrupt.
 126982   if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
 126983    || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
 126985     return FTS_CORRUPT_VTAB;
 126987   return SQLITE_OK;
 126991 ** Set the SegReader to point to the first docid in the doclist associated
 126992 ** with the current term.
 126994 static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
 126995   int rc = SQLITE_OK;
 126996   assert( pReader->aDoclist );
 126997   assert( !pReader->pOffsetList );
 126998   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
 126999     u8 bEof = 0;
 127000     pReader->iDocid = 0;
 127001     pReader->nOffsetList = 0;
 127002     sqlite3Fts3DoclistPrev(0,
 127003         pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList, 
 127004         &pReader->iDocid, &pReader->nOffsetList, &bEof
 127006   }else{
 127007     rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
 127008     if( rc==SQLITE_OK ){
 127009       int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
 127010       pReader->pOffsetList = &pReader->aDoclist[n];
 127013   return rc;
 127017 ** Advance the SegReader to point to the next docid in the doclist
 127018 ** associated with the current term.
 127020 ** If arguments ppOffsetList and pnOffsetList are not NULL, then 
 127021 ** *ppOffsetList is set to point to the first column-offset list
 127022 ** in the doclist entry (i.e. immediately past the docid varint).
 127023 ** *pnOffsetList is set to the length of the set of column-offset
 127024 ** lists, not including the nul-terminator byte. For example:
 127026 static int fts3SegReaderNextDocid(
 127027   Fts3Table *pTab,
 127028   Fts3SegReader *pReader,         /* Reader to advance to next docid */
 127029   char **ppOffsetList,            /* OUT: Pointer to current position-list */
 127030   int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
 127032   int rc = SQLITE_OK;
 127033   char *p = pReader->pOffsetList;
 127034   char c = 0;
 127036   assert( p );
 127038   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
 127039     /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
 127040     ** Pending-terms doclists are always built up in ascending order, so
 127041     ** we have to iterate through them backwards here. */
 127042     u8 bEof = 0;
 127043     if( ppOffsetList ){
 127044       *ppOffsetList = pReader->pOffsetList;
 127045       *pnOffsetList = pReader->nOffsetList - 1;
 127047     sqlite3Fts3DoclistPrev(0,
 127048         pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
 127049         &pReader->nOffsetList, &bEof
 127051     if( bEof ){
 127052       pReader->pOffsetList = 0;
 127053     }else{
 127054       pReader->pOffsetList = p;
 127056   }else{
 127057     char *pEnd = &pReader->aDoclist[pReader->nDoclist];
 127059     /* Pointer p currently points at the first byte of an offset list. The
 127060     ** following block advances it to point one byte past the end of
 127061     ** the same offset list. */
 127062     while( 1 ){
 127064       /* The following line of code (and the "p++" below the while() loop) is
 127065       ** normally all that is required to move pointer p to the desired 
 127066       ** position. The exception is if this node is being loaded from disk
 127067       ** incrementally and pointer "p" now points to the first byte passed
 127068       ** the populated part of pReader->aNode[].
 127070       while( *p | c ) c = *p++ & 0x80;
 127071       assert( *p==0 );
 127073       if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
 127074       rc = fts3SegReaderIncrRead(pReader);
 127075       if( rc!=SQLITE_OK ) return rc;
 127077     p++;
 127079     /* If required, populate the output variables with a pointer to and the
 127080     ** size of the previous offset-list.
 127082     if( ppOffsetList ){
 127083       *ppOffsetList = pReader->pOffsetList;
 127084       *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
 127087     while( p<pEnd && *p==0 ) p++;
 127089     /* If there are no more entries in the doclist, set pOffsetList to
 127090     ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
 127091     ** Fts3SegReader.pOffsetList to point to the next offset list before
 127092     ** returning.
 127094     if( p>=pEnd ){
 127095       pReader->pOffsetList = 0;
 127096     }else{
 127097       rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
 127098       if( rc==SQLITE_OK ){
 127099         sqlite3_int64 iDelta;
 127100         pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
 127101         if( pTab->bDescIdx ){
 127102           pReader->iDocid -= iDelta;
 127103         }else{
 127104           pReader->iDocid += iDelta;
 127110   return SQLITE_OK;
 127114 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
 127115   Fts3Cursor *pCsr, 
 127116   Fts3MultiSegReader *pMsr,
 127117   int *pnOvfl
 127119   Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
 127120   int nOvfl = 0;
 127121   int ii;
 127122   int rc = SQLITE_OK;
 127123   int pgsz = p->nPgsz;
 127125   assert( p->bFts4 );
 127126   assert( pgsz>0 );
 127128   for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
 127129     Fts3SegReader *pReader = pMsr->apSegment[ii];
 127130     if( !fts3SegReaderIsPending(pReader) 
 127131      && !fts3SegReaderIsRootOnly(pReader) 
 127133       sqlite3_int64 jj;
 127134       for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
 127135         int nBlob;
 127136         rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
 127137         if( rc!=SQLITE_OK ) break;
 127138         if( (nBlob+35)>pgsz ){
 127139           nOvfl += (nBlob + 34)/pgsz;
 127144   *pnOvfl = nOvfl;
 127145   return rc;
 127149 ** Free all allocations associated with the iterator passed as the 
 127150 ** second argument.
 127152 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
 127153   if( pReader && !fts3SegReaderIsPending(pReader) ){
 127154     sqlite3_free(pReader->zTerm);
 127155     if( !fts3SegReaderIsRootOnly(pReader) ){
 127156       sqlite3_free(pReader->aNode);
 127157       sqlite3_blob_close(pReader->pBlob);
 127160   sqlite3_free(pReader);
 127164 ** Allocate a new SegReader object.
 127166 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
 127167   int iAge,                       /* Segment "age". */
 127168   int bLookup,                    /* True for a lookup only */
 127169   sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
 127170   sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
 127171   sqlite3_int64 iEndBlock,        /* Final block of segment */
 127172   const char *zRoot,              /* Buffer containing root node */
 127173   int nRoot,                      /* Size of buffer containing root node */
 127174   Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
 127176   Fts3SegReader *pReader;         /* Newly allocated SegReader object */
 127177   int nExtra = 0;                 /* Bytes to allocate segment root node */
 127179   assert( iStartLeaf<=iEndLeaf );
 127180   if( iStartLeaf==0 ){
 127181     nExtra = nRoot + FTS3_NODE_PADDING;
 127184   pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
 127185   if( !pReader ){
 127186     return SQLITE_NOMEM;
 127188   memset(pReader, 0, sizeof(Fts3SegReader));
 127189   pReader->iIdx = iAge;
 127190   pReader->bLookup = bLookup!=0;
 127191   pReader->iStartBlock = iStartLeaf;
 127192   pReader->iLeafEndBlock = iEndLeaf;
 127193   pReader->iEndBlock = iEndBlock;
 127195   if( nExtra ){
 127196     /* The entire segment is stored in the root node. */
 127197     pReader->aNode = (char *)&pReader[1];
 127198     pReader->rootOnly = 1;
 127199     pReader->nNode = nRoot;
 127200     memcpy(pReader->aNode, zRoot, nRoot);
 127201     memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
 127202   }else{
 127203     pReader->iCurrentBlock = iStartLeaf-1;
 127205   *ppReader = pReader;
 127206   return SQLITE_OK;
 127210 ** This is a comparison function used as a qsort() callback when sorting
 127211 ** an array of pending terms by term. This occurs as part of flushing
 127212 ** the contents of the pending-terms hash table to the database.
 127214 static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
 127215   char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
 127216   char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
 127217   int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
 127218   int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
 127220   int n = (n1<n2 ? n1 : n2);
 127221   int c = memcmp(z1, z2, n);
 127222   if( c==0 ){
 127223     c = n1 - n2;
 127225   return c;
 127229 ** This function is used to allocate an Fts3SegReader that iterates through
 127230 ** a subset of the terms stored in the Fts3Table.pendingTerms array.
 127232 ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
 127233 ** through each term in the pending-terms table. Or, if isPrefixIter is
 127234 ** non-zero, it iterates through each term and its prefixes. For example, if
 127235 ** the pending terms hash table contains the terms "sqlite", "mysql" and
 127236 ** "firebird", then the iterator visits the following 'terms' (in the order
 127237 ** shown):
 127239 **   f fi fir fire fireb firebi firebir firebird
 127240 **   m my mys mysq mysql
 127241 **   s sq sql sqli sqlit sqlite
 127243 ** Whereas if isPrefixIter is zero, the terms visited are:
 127245 **   firebird mysql sqlite
 127247 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
 127248   Fts3Table *p,                   /* Virtual table handle */
 127249   int iIndex,                     /* Index for p->aIndex */
 127250   const char *zTerm,              /* Term to search for */
 127251   int nTerm,                      /* Size of buffer zTerm */
 127252   int bPrefix,                    /* True for a prefix iterator */
 127253   Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
 127255   Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
 127256   Fts3HashElem *pE;               /* Iterator variable */
 127257   Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
 127258   int nElem = 0;                  /* Size of array at aElem */
 127259   int rc = SQLITE_OK;             /* Return Code */
 127260   Fts3Hash *pHash;
 127262   pHash = &p->aIndex[iIndex].hPending;
 127263   if( bPrefix ){
 127264     int nAlloc = 0;               /* Size of allocated array at aElem */
 127266     for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
 127267       char *zKey = (char *)fts3HashKey(pE);
 127268       int nKey = fts3HashKeysize(pE);
 127269       if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
 127270         if( nElem==nAlloc ){
 127271           Fts3HashElem **aElem2;
 127272           nAlloc += 16;
 127273           aElem2 = (Fts3HashElem **)sqlite3_realloc(
 127274               aElem, nAlloc*sizeof(Fts3HashElem *)
 127276           if( !aElem2 ){
 127277             rc = SQLITE_NOMEM;
 127278             nElem = 0;
 127279             break;
 127281           aElem = aElem2;
 127284         aElem[nElem++] = pE;
 127288     /* If more than one term matches the prefix, sort the Fts3HashElem
 127289     ** objects in term order using qsort(). This uses the same comparison
 127290     ** callback as is used when flushing terms to disk.
 127292     if( nElem>1 ){
 127293       qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
 127296   }else{
 127297     /* The query is a simple term lookup that matches at most one term in
 127298     ** the index. All that is required is a straight hash-lookup. 
 127300     ** Because the stack address of pE may be accessed via the aElem pointer
 127301     ** below, the "Fts3HashElem *pE" must be declared so that it is valid
 127302     ** within this entire function, not just this "else{...}" block.
 127304     pE = fts3HashFindElem(pHash, zTerm, nTerm);
 127305     if( pE ){
 127306       aElem = &pE;
 127307       nElem = 1;
 127311   if( nElem>0 ){
 127312     int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
 127313     pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
 127314     if( !pReader ){
 127315       rc = SQLITE_NOMEM;
 127316     }else{
 127317       memset(pReader, 0, nByte);
 127318       pReader->iIdx = 0x7FFFFFFF;
 127319       pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
 127320       memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
 127324   if( bPrefix ){
 127325     sqlite3_free(aElem);
 127327   *ppReader = pReader;
 127328   return rc;
 127332 ** Compare the entries pointed to by two Fts3SegReader structures. 
 127333 ** Comparison is as follows:
 127335 **   1) EOF is greater than not EOF.
 127337 **   2) The current terms (if any) are compared using memcmp(). If one
 127338 **      term is a prefix of another, the longer term is considered the
 127339 **      larger.
 127341 **   3) By segment age. An older segment is considered larger.
 127343 static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
 127344   int rc;
 127345   if( pLhs->aNode && pRhs->aNode ){
 127346     int rc2 = pLhs->nTerm - pRhs->nTerm;
 127347     if( rc2<0 ){
 127348       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
 127349     }else{
 127350       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
 127352     if( rc==0 ){
 127353       rc = rc2;
 127355   }else{
 127356     rc = (pLhs->aNode==0) - (pRhs->aNode==0);
 127358   if( rc==0 ){
 127359     rc = pRhs->iIdx - pLhs->iIdx;
 127361   assert( rc!=0 );
 127362   return rc;
 127366 ** A different comparison function for SegReader structures. In this
 127367 ** version, it is assumed that each SegReader points to an entry in
 127368 ** a doclist for identical terms. Comparison is made as follows:
 127370 **   1) EOF (end of doclist in this case) is greater than not EOF.
 127372 **   2) By current docid.
 127374 **   3) By segment age. An older segment is considered larger.
 127376 static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
 127377   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
 127378   if( rc==0 ){
 127379     if( pLhs->iDocid==pRhs->iDocid ){
 127380       rc = pRhs->iIdx - pLhs->iIdx;
 127381     }else{
 127382       rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
 127385   assert( pLhs->aNode && pRhs->aNode );
 127386   return rc;
 127388 static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
 127389   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
 127390   if( rc==0 ){
 127391     if( pLhs->iDocid==pRhs->iDocid ){
 127392       rc = pRhs->iIdx - pLhs->iIdx;
 127393     }else{
 127394       rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
 127397   assert( pLhs->aNode && pRhs->aNode );
 127398   return rc;
 127402 ** Compare the term that the Fts3SegReader object passed as the first argument
 127403 ** points to with the term specified by arguments zTerm and nTerm. 
 127405 ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
 127406 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
 127407 ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
 127409 static int fts3SegReaderTermCmp(
 127410   Fts3SegReader *pSeg,            /* Segment reader object */
 127411   const char *zTerm,              /* Term to compare to */
 127412   int nTerm                       /* Size of term zTerm in bytes */
 127414   int res = 0;
 127415   if( pSeg->aNode ){
 127416     if( pSeg->nTerm>nTerm ){
 127417       res = memcmp(pSeg->zTerm, zTerm, nTerm);
 127418     }else{
 127419       res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
 127421     if( res==0 ){
 127422       res = pSeg->nTerm-nTerm;
 127425   return res;
 127429 ** Argument apSegment is an array of nSegment elements. It is known that
 127430 ** the final (nSegment-nSuspect) members are already in sorted order
 127431 ** (according to the comparison function provided). This function shuffles
 127432 ** the array around until all entries are in sorted order.
 127434 static void fts3SegReaderSort(
 127435   Fts3SegReader **apSegment,                     /* Array to sort entries of */
 127436   int nSegment,                                  /* Size of apSegment array */
 127437   int nSuspect,                                  /* Unsorted entry count */
 127438   int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
 127440   int i;                          /* Iterator variable */
 127442   assert( nSuspect<=nSegment );
 127444   if( nSuspect==nSegment ) nSuspect--;
 127445   for(i=nSuspect-1; i>=0; i--){
 127446     int j;
 127447     for(j=i; j<(nSegment-1); j++){
 127448       Fts3SegReader *pTmp;
 127449       if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
 127450       pTmp = apSegment[j+1];
 127451       apSegment[j+1] = apSegment[j];
 127452       apSegment[j] = pTmp;
 127456 #ifndef NDEBUG
 127457   /* Check that the list really is sorted now. */
 127458   for(i=0; i<(nSuspect-1); i++){
 127459     assert( xCmp(apSegment[i], apSegment[i+1])<0 );
 127461 #endif
 127465 ** Insert a record into the %_segments table.
 127467 static int fts3WriteSegment(
 127468   Fts3Table *p,                   /* Virtual table handle */
 127469   sqlite3_int64 iBlock,           /* Block id for new block */
 127470   char *z,                        /* Pointer to buffer containing block data */
 127471   int n                           /* Size of buffer z in bytes */
 127473   sqlite3_stmt *pStmt;
 127474   int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
 127475   if( rc==SQLITE_OK ){
 127476     sqlite3_bind_int64(pStmt, 1, iBlock);
 127477     sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
 127478     sqlite3_step(pStmt);
 127479     rc = sqlite3_reset(pStmt);
 127481   return rc;
 127485 ** Find the largest relative level number in the table. If successful, set
 127486 ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
 127487 ** set *pnMax to zero and return an SQLite error code.
 127489 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
 127490   int rc;
 127491   int mxLevel = 0;
 127492   sqlite3_stmt *pStmt = 0;
 127494   rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
 127495   if( rc==SQLITE_OK ){
 127496     if( SQLITE_ROW==sqlite3_step(pStmt) ){
 127497       mxLevel = sqlite3_column_int(pStmt, 0);
 127499     rc = sqlite3_reset(pStmt);
 127501   *pnMax = mxLevel;
 127502   return rc;
 127506 ** Insert a record into the %_segdir table.
 127508 static int fts3WriteSegdir(
 127509   Fts3Table *p,                   /* Virtual table handle */
 127510   sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
 127511   int iIdx,                       /* Value for "idx" field */
 127512   sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
 127513   sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
 127514   sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
 127515   char *zRoot,                    /* Blob value for "root" field */
 127516   int nRoot                       /* Number of bytes in buffer zRoot */
 127518   sqlite3_stmt *pStmt;
 127519   int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
 127520   if( rc==SQLITE_OK ){
 127521     sqlite3_bind_int64(pStmt, 1, iLevel);
 127522     sqlite3_bind_int(pStmt, 2, iIdx);
 127523     sqlite3_bind_int64(pStmt, 3, iStartBlock);
 127524     sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
 127525     sqlite3_bind_int64(pStmt, 5, iEndBlock);
 127526     sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
 127527     sqlite3_step(pStmt);
 127528     rc = sqlite3_reset(pStmt);
 127530   return rc;
 127534 ** Return the size of the common prefix (if any) shared by zPrev and
 127535 ** zNext, in bytes. For example, 
 127537 **   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
 127538 **   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
 127539 **   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
 127541 static int fts3PrefixCompress(
 127542   const char *zPrev,              /* Buffer containing previous term */
 127543   int nPrev,                      /* Size of buffer zPrev in bytes */
 127544   const char *zNext,              /* Buffer containing next term */
 127545   int nNext                       /* Size of buffer zNext in bytes */
 127547   int n;
 127548   UNUSED_PARAMETER(nNext);
 127549   for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
 127550   return n;
 127554 ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
 127555 ** (according to memcmp) than the previous term.
 127557 static int fts3NodeAddTerm(
 127558   Fts3Table *p,                   /* Virtual table handle */
 127559   SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */ 
 127560   int isCopyTerm,                 /* True if zTerm/nTerm is transient */
 127561   const char *zTerm,              /* Pointer to buffer containing term */
 127562   int nTerm                       /* Size of term in bytes */
 127564   SegmentNode *pTree = *ppTree;
 127565   int rc;
 127566   SegmentNode *pNew;
 127568   /* First try to append the term to the current node. Return early if 
 127569   ** this is possible.
 127571   if( pTree ){
 127572     int nData = pTree->nData;     /* Current size of node in bytes */
 127573     int nReq = nData;             /* Required space after adding zTerm */
 127574     int nPrefix;                  /* Number of bytes of prefix compression */
 127575     int nSuffix;                  /* Suffix length */
 127577     nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
 127578     nSuffix = nTerm-nPrefix;
 127580     nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
 127581     if( nReq<=p->nNodeSize || !pTree->zTerm ){
 127583       if( nReq>p->nNodeSize ){
 127584         /* An unusual case: this is the first term to be added to the node
 127585         ** and the static node buffer (p->nNodeSize bytes) is not large
 127586         ** enough. Use a separately malloced buffer instead This wastes
 127587         ** p->nNodeSize bytes, but since this scenario only comes about when
 127588         ** the database contain two terms that share a prefix of almost 2KB, 
 127589         ** this is not expected to be a serious problem. 
 127591         assert( pTree->aData==(char *)&pTree[1] );
 127592         pTree->aData = (char *)sqlite3_malloc(nReq);
 127593         if( !pTree->aData ){
 127594           return SQLITE_NOMEM;
 127598       if( pTree->zTerm ){
 127599         /* There is no prefix-length field for first term in a node */
 127600         nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
 127603       nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
 127604       memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
 127605       pTree->nData = nData + nSuffix;
 127606       pTree->nEntry++;
 127608       if( isCopyTerm ){
 127609         if( pTree->nMalloc<nTerm ){
 127610           char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
 127611           if( !zNew ){
 127612             return SQLITE_NOMEM;
 127614           pTree->nMalloc = nTerm*2;
 127615           pTree->zMalloc = zNew;
 127617         pTree->zTerm = pTree->zMalloc;
 127618         memcpy(pTree->zTerm, zTerm, nTerm);
 127619         pTree->nTerm = nTerm;
 127620       }else{
 127621         pTree->zTerm = (char *)zTerm;
 127622         pTree->nTerm = nTerm;
 127624       return SQLITE_OK;
 127628   /* If control flows to here, it was not possible to append zTerm to the
 127629   ** current node. Create a new node (a right-sibling of the current node).
 127630   ** If this is the first node in the tree, the term is added to it.
 127632   ** Otherwise, the term is not added to the new node, it is left empty for
 127633   ** now. Instead, the term is inserted into the parent of pTree. If pTree 
 127634   ** has no parent, one is created here.
 127636   pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
 127637   if( !pNew ){
 127638     return SQLITE_NOMEM;
 127640   memset(pNew, 0, sizeof(SegmentNode));
 127641   pNew->nData = 1 + FTS3_VARINT_MAX;
 127642   pNew->aData = (char *)&pNew[1];
 127644   if( pTree ){
 127645     SegmentNode *pParent = pTree->pParent;
 127646     rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
 127647     if( pTree->pParent==0 ){
 127648       pTree->pParent = pParent;
 127650     pTree->pRight = pNew;
 127651     pNew->pLeftmost = pTree->pLeftmost;
 127652     pNew->pParent = pParent;
 127653     pNew->zMalloc = pTree->zMalloc;
 127654     pNew->nMalloc = pTree->nMalloc;
 127655     pTree->zMalloc = 0;
 127656   }else{
 127657     pNew->pLeftmost = pNew;
 127658     rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); 
 127661   *ppTree = pNew;
 127662   return rc;
 127666 ** Helper function for fts3NodeWrite().
 127668 static int fts3TreeFinishNode(
 127669   SegmentNode *pTree, 
 127670   int iHeight, 
 127671   sqlite3_int64 iLeftChild
 127673   int nStart;
 127674   assert( iHeight>=1 && iHeight<128 );
 127675   nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
 127676   pTree->aData[nStart] = (char)iHeight;
 127677   sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
 127678   return nStart;
 127682 ** Write the buffer for the segment node pTree and all of its peers to the
 127683 ** database. Then call this function recursively to write the parent of 
 127684 ** pTree and its peers to the database. 
 127686 ** Except, if pTree is a root node, do not write it to the database. Instead,
 127687 ** set output variables *paRoot and *pnRoot to contain the root node.
 127689 ** If successful, SQLITE_OK is returned and output variable *piLast is
 127690 ** set to the largest blockid written to the database (or zero if no
 127691 ** blocks were written to the db). Otherwise, an SQLite error code is 
 127692 ** returned.
 127694 static int fts3NodeWrite(
 127695   Fts3Table *p,                   /* Virtual table handle */
 127696   SegmentNode *pTree,             /* SegmentNode handle */
 127697   int iHeight,                    /* Height of this node in tree */
 127698   sqlite3_int64 iLeaf,            /* Block id of first leaf node */
 127699   sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
 127700   sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
 127701   char **paRoot,                  /* OUT: Data for root node */
 127702   int *pnRoot                     /* OUT: Size of root node in bytes */
 127704   int rc = SQLITE_OK;
 127706   if( !pTree->pParent ){
 127707     /* Root node of the tree. */
 127708     int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
 127709     *piLast = iFree-1;
 127710     *pnRoot = pTree->nData - nStart;
 127711     *paRoot = &pTree->aData[nStart];
 127712   }else{
 127713     SegmentNode *pIter;
 127714     sqlite3_int64 iNextFree = iFree;
 127715     sqlite3_int64 iNextLeaf = iLeaf;
 127716     for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
 127717       int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
 127718       int nWrite = pIter->nData - nStart;
 127720       rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
 127721       iNextFree++;
 127722       iNextLeaf += (pIter->nEntry+1);
 127724     if( rc==SQLITE_OK ){
 127725       assert( iNextLeaf==iFree );
 127726       rc = fts3NodeWrite(
 127727           p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
 127732   return rc;
 127736 ** Free all memory allocations associated with the tree pTree.
 127738 static void fts3NodeFree(SegmentNode *pTree){
 127739   if( pTree ){
 127740     SegmentNode *p = pTree->pLeftmost;
 127741     fts3NodeFree(p->pParent);
 127742     while( p ){
 127743       SegmentNode *pRight = p->pRight;
 127744       if( p->aData!=(char *)&p[1] ){
 127745         sqlite3_free(p->aData);
 127747       assert( pRight==0 || p->zMalloc==0 );
 127748       sqlite3_free(p->zMalloc);
 127749       sqlite3_free(p);
 127750       p = pRight;
 127756 ** Add a term to the segment being constructed by the SegmentWriter object
 127757 ** *ppWriter. When adding the first term to a segment, *ppWriter should
 127758 ** be passed NULL. This function will allocate a new SegmentWriter object
 127759 ** and return it via the input/output variable *ppWriter in this case.
 127761 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
 127763 static int fts3SegWriterAdd(
 127764   Fts3Table *p,                   /* Virtual table handle */
 127765   SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */ 
 127766   int isCopyTerm,                 /* True if buffer zTerm must be copied */
 127767   const char *zTerm,              /* Pointer to buffer containing term */
 127768   int nTerm,                      /* Size of term in bytes */
 127769   const char *aDoclist,           /* Pointer to buffer containing doclist */
 127770   int nDoclist                    /* Size of doclist in bytes */
 127772   int nPrefix;                    /* Size of term prefix in bytes */
 127773   int nSuffix;                    /* Size of term suffix in bytes */
 127774   int nReq;                       /* Number of bytes required on leaf page */
 127775   int nData;
 127776   SegmentWriter *pWriter = *ppWriter;
 127778   if( !pWriter ){
 127779     int rc;
 127780     sqlite3_stmt *pStmt;
 127782     /* Allocate the SegmentWriter structure */
 127783     pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
 127784     if( !pWriter ) return SQLITE_NOMEM;
 127785     memset(pWriter, 0, sizeof(SegmentWriter));
 127786     *ppWriter = pWriter;
 127788     /* Allocate a buffer in which to accumulate data */
 127789     pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
 127790     if( !pWriter->aData ) return SQLITE_NOMEM;
 127791     pWriter->nSize = p->nNodeSize;
 127793     /* Find the next free blockid in the %_segments table */
 127794     rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
 127795     if( rc!=SQLITE_OK ) return rc;
 127796     if( SQLITE_ROW==sqlite3_step(pStmt) ){
 127797       pWriter->iFree = sqlite3_column_int64(pStmt, 0);
 127798       pWriter->iFirst = pWriter->iFree;
 127800     rc = sqlite3_reset(pStmt);
 127801     if( rc!=SQLITE_OK ) return rc;
 127803   nData = pWriter->nData;
 127805   nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
 127806   nSuffix = nTerm-nPrefix;
 127808   /* Figure out how many bytes are required by this new entry */
 127809   nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
 127810     sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
 127811     nSuffix +                               /* Term suffix */
 127812     sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
 127813     nDoclist;                               /* Doclist data */
 127815   if( nData>0 && nData+nReq>p->nNodeSize ){
 127816     int rc;
 127818     /* The current leaf node is full. Write it out to the database. */
 127819     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
 127820     if( rc!=SQLITE_OK ) return rc;
 127821     p->nLeafAdd++;
 127823     /* Add the current term to the interior node tree. The term added to
 127824     ** the interior tree must:
 127826     **   a) be greater than the largest term on the leaf node just written
 127827     **      to the database (still available in pWriter->zTerm), and
 127829     **   b) be less than or equal to the term about to be added to the new
 127830     **      leaf node (zTerm/nTerm).
 127832     ** In other words, it must be the prefix of zTerm 1 byte longer than
 127833     ** the common prefix (if any) of zTerm and pWriter->zTerm.
 127835     assert( nPrefix<nTerm );
 127836     rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
 127837     if( rc!=SQLITE_OK ) return rc;
 127839     nData = 0;
 127840     pWriter->nTerm = 0;
 127842     nPrefix = 0;
 127843     nSuffix = nTerm;
 127844     nReq = 1 +                              /* varint containing prefix size */
 127845       sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
 127846       nTerm +                               /* Term suffix */
 127847       sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
 127848       nDoclist;                             /* Doclist data */
 127851   /* If the buffer currently allocated is too small for this entry, realloc
 127852   ** the buffer to make it large enough.
 127854   if( nReq>pWriter->nSize ){
 127855     char *aNew = sqlite3_realloc(pWriter->aData, nReq);
 127856     if( !aNew ) return SQLITE_NOMEM;
 127857     pWriter->aData = aNew;
 127858     pWriter->nSize = nReq;
 127860   assert( nData+nReq<=pWriter->nSize );
 127862   /* Append the prefix-compressed term and doclist to the buffer. */
 127863   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
 127864   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
 127865   memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
 127866   nData += nSuffix;
 127867   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
 127868   memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
 127869   pWriter->nData = nData + nDoclist;
 127871   /* Save the current term so that it can be used to prefix-compress the next.
 127872   ** If the isCopyTerm parameter is true, then the buffer pointed to by
 127873   ** zTerm is transient, so take a copy of the term data. Otherwise, just
 127874   ** store a copy of the pointer.
 127876   if( isCopyTerm ){
 127877     if( nTerm>pWriter->nMalloc ){
 127878       char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
 127879       if( !zNew ){
 127880         return SQLITE_NOMEM;
 127882       pWriter->nMalloc = nTerm*2;
 127883       pWriter->zMalloc = zNew;
 127884       pWriter->zTerm = zNew;
 127886     assert( pWriter->zTerm==pWriter->zMalloc );
 127887     memcpy(pWriter->zTerm, zTerm, nTerm);
 127888   }else{
 127889     pWriter->zTerm = (char *)zTerm;
 127891   pWriter->nTerm = nTerm;
 127893   return SQLITE_OK;
 127897 ** Flush all data associated with the SegmentWriter object pWriter to the
 127898 ** database. This function must be called after all terms have been added
 127899 ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
 127900 ** returned. Otherwise, an SQLite error code.
 127902 static int fts3SegWriterFlush(
 127903   Fts3Table *p,                   /* Virtual table handle */
 127904   SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
 127905   sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
 127906   int iIdx                        /* Value for 'idx' column of %_segdir */
 127908   int rc;                         /* Return code */
 127909   if( pWriter->pTree ){
 127910     sqlite3_int64 iLast = 0;      /* Largest block id written to database */
 127911     sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
 127912     char *zRoot = NULL;           /* Pointer to buffer containing root node */
 127913     int nRoot = 0;                /* Size of buffer zRoot */
 127915     iLastLeaf = pWriter->iFree;
 127916     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
 127917     if( rc==SQLITE_OK ){
 127918       rc = fts3NodeWrite(p, pWriter->pTree, 1,
 127919           pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
 127921     if( rc==SQLITE_OK ){
 127922       rc = fts3WriteSegdir(
 127923           p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
 127925   }else{
 127926     /* The entire tree fits on the root node. Write it to the segdir table. */
 127927     rc = fts3WriteSegdir(
 127928         p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
 127930   p->nLeafAdd++;
 127931   return rc;
 127935 ** Release all memory held by the SegmentWriter object passed as the 
 127936 ** first argument.
 127938 static void fts3SegWriterFree(SegmentWriter *pWriter){
 127939   if( pWriter ){
 127940     sqlite3_free(pWriter->aData);
 127941     sqlite3_free(pWriter->zMalloc);
 127942     fts3NodeFree(pWriter->pTree);
 127943     sqlite3_free(pWriter);
 127948 ** The first value in the apVal[] array is assumed to contain an integer.
 127949 ** This function tests if there exist any documents with docid values that
 127950 ** are different from that integer. i.e. if deleting the document with docid
 127951 ** pRowid would mean the FTS3 table were empty.
 127953 ** If successful, *pisEmpty is set to true if the table is empty except for
 127954 ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
 127955 ** error occurs, an SQLite error code is returned.
 127957 static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
 127958   sqlite3_stmt *pStmt;
 127959   int rc;
 127960   if( p->zContentTbl ){
 127961     /* If using the content=xxx option, assume the table is never empty */
 127962     *pisEmpty = 0;
 127963     rc = SQLITE_OK;
 127964   }else{
 127965     rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
 127966     if( rc==SQLITE_OK ){
 127967       if( SQLITE_ROW==sqlite3_step(pStmt) ){
 127968         *pisEmpty = sqlite3_column_int(pStmt, 0);
 127970       rc = sqlite3_reset(pStmt);
 127973   return rc;
 127977 ** Set *pnMax to the largest segment level in the database for the index
 127978 ** iIndex.
 127980 ** Segment levels are stored in the 'level' column of the %_segdir table.
 127982 ** Return SQLITE_OK if successful, or an SQLite error code if not.
 127984 static int fts3SegmentMaxLevel(
 127985   Fts3Table *p, 
 127986   int iLangid,
 127987   int iIndex, 
 127988   sqlite3_int64 *pnMax
 127990   sqlite3_stmt *pStmt;
 127991   int rc;
 127992   assert( iIndex>=0 && iIndex<p->nIndex );
 127994   /* Set pStmt to the compiled version of:
 127996   **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
 127998   ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
 128000   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
 128001   if( rc!=SQLITE_OK ) return rc;
 128002   sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
 128003   sqlite3_bind_int64(pStmt, 2, 
 128004       getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
 128006   if( SQLITE_ROW==sqlite3_step(pStmt) ){
 128007     *pnMax = sqlite3_column_int64(pStmt, 0);
 128009   return sqlite3_reset(pStmt);
 128013 ** Delete all entries in the %_segments table associated with the segment
 128014 ** opened with seg-reader pSeg. This function does not affect the contents
 128015 ** of the %_segdir table.
 128017 static int fts3DeleteSegment(
 128018   Fts3Table *p,                   /* FTS table handle */
 128019   Fts3SegReader *pSeg             /* Segment to delete */
 128021   int rc = SQLITE_OK;             /* Return code */
 128022   if( pSeg->iStartBlock ){
 128023     sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
 128024     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
 128025     if( rc==SQLITE_OK ){
 128026       sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
 128027       sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
 128028       sqlite3_step(pDelete);
 128029       rc = sqlite3_reset(pDelete);
 128032   return rc;
 128036 ** This function is used after merging multiple segments into a single large
 128037 ** segment to delete the old, now redundant, segment b-trees. Specifically,
 128038 ** it:
 128040 **   1) Deletes all %_segments entries for the segments associated with 
 128041 **      each of the SegReader objects in the array passed as the third 
 128042 **      argument, and
 128044 **   2) deletes all %_segdir entries with level iLevel, or all %_segdir
 128045 **      entries regardless of level if (iLevel<0).
 128047 ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
 128049 static int fts3DeleteSegdir(
 128050   Fts3Table *p,                   /* Virtual table handle */
 128051   int iLangid,                    /* Language id */
 128052   int iIndex,                     /* Index for p->aIndex */
 128053   int iLevel,                     /* Level of %_segdir entries to delete */
 128054   Fts3SegReader **apSegment,      /* Array of SegReader objects */
 128055   int nReader                     /* Size of array apSegment */
 128057   int rc = SQLITE_OK;             /* Return Code */
 128058   int i;                          /* Iterator variable */
 128059   sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
 128061   for(i=0; rc==SQLITE_OK && i<nReader; i++){
 128062     rc = fts3DeleteSegment(p, apSegment[i]);
 128064   if( rc!=SQLITE_OK ){
 128065     return rc;
 128068   assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
 128069   if( iLevel==FTS3_SEGCURSOR_ALL ){
 128070     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
 128071     if( rc==SQLITE_OK ){
 128072       sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
 128073       sqlite3_bind_int64(pDelete, 2, 
 128074           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
 128077   }else{
 128078     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
 128079     if( rc==SQLITE_OK ){
 128080       sqlite3_bind_int64(
 128081           pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
 128086   if( rc==SQLITE_OK ){
 128087     sqlite3_step(pDelete);
 128088     rc = sqlite3_reset(pDelete);
 128091   return rc;
 128095 ** When this function is called, buffer *ppList (size *pnList bytes) contains 
 128096 ** a position list that may (or may not) feature multiple columns. This
 128097 ** function adjusts the pointer *ppList and the length *pnList so that they
 128098 ** identify the subset of the position list that corresponds to column iCol.
 128100 ** If there are no entries in the input position list for column iCol, then
 128101 ** *pnList is set to zero before returning.
 128103 static void fts3ColumnFilter(
 128104   int iCol,                       /* Column to filter on */
 128105   char **ppList,                  /* IN/OUT: Pointer to position list */
 128106   int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
 128108   char *pList = *ppList;
 128109   int nList = *pnList;
 128110   char *pEnd = &pList[nList];
 128111   int iCurrent = 0;
 128112   char *p = pList;
 128114   assert( iCol>=0 );
 128115   while( 1 ){
 128116     char c = 0;
 128117     while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
 128119     if( iCol==iCurrent ){
 128120       nList = (int)(p - pList);
 128121       break;
 128124     nList -= (int)(p - pList);
 128125     pList = p;
 128126     if( nList==0 ){
 128127       break;
 128129     p = &pList[1];
 128130     p += sqlite3Fts3GetVarint32(p, &iCurrent);
 128133   *ppList = pList;
 128134   *pnList = nList;
 128138 ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
 128139 ** existing data). Grow the buffer if required.
 128141 ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
 128142 ** trying to resize the buffer, return SQLITE_NOMEM.
 128144 static int fts3MsrBufferData(
 128145   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
 128146   char *pList,
 128147   int nList
 128149   if( nList>pMsr->nBuffer ){
 128150     char *pNew;
 128151     pMsr->nBuffer = nList*2;
 128152     pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
 128153     if( !pNew ) return SQLITE_NOMEM;
 128154     pMsr->aBuffer = pNew;
 128157   memcpy(pMsr->aBuffer, pList, nList);
 128158   return SQLITE_OK;
 128161 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
 128162   Fts3Table *p,                   /* Virtual table handle */
 128163   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
 128164   sqlite3_int64 *piDocid,         /* OUT: Docid value */
 128165   char **paPoslist,               /* OUT: Pointer to position list */
 128166   int *pnPoslist                  /* OUT: Size of position list in bytes */
 128168   int nMerge = pMsr->nAdvance;
 128169   Fts3SegReader **apSegment = pMsr->apSegment;
 128170   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
 128171     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
 128174   if( nMerge==0 ){
 128175     *paPoslist = 0;
 128176     return SQLITE_OK;
 128179   while( 1 ){
 128180     Fts3SegReader *pSeg;
 128181     pSeg = pMsr->apSegment[0];
 128183     if( pSeg->pOffsetList==0 ){
 128184       *paPoslist = 0;
 128185       break;
 128186     }else{
 128187       int rc;
 128188       char *pList;
 128189       int nList;
 128190       int j;
 128191       sqlite3_int64 iDocid = apSegment[0]->iDocid;
 128193       rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
 128194       j = 1;
 128195       while( rc==SQLITE_OK 
 128196         && j<nMerge
 128197         && apSegment[j]->pOffsetList
 128198         && apSegment[j]->iDocid==iDocid
 128200         rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
 128201         j++;
 128203       if( rc!=SQLITE_OK ) return rc;
 128204       fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
 128206       if( pMsr->iColFilter>=0 ){
 128207         fts3ColumnFilter(pMsr->iColFilter, &pList, &nList);
 128210       if( nList>0 ){
 128211         if( fts3SegReaderIsPending(apSegment[0]) ){
 128212           rc = fts3MsrBufferData(pMsr, pList, nList+1);
 128213           if( rc!=SQLITE_OK ) return rc;
 128214           *paPoslist = pMsr->aBuffer;
 128215           assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
 128216         }else{
 128217           *paPoslist = pList;
 128219         *piDocid = iDocid;
 128220         *pnPoslist = nList;
 128221         break;
 128226   return SQLITE_OK;
 128229 static int fts3SegReaderStart(
 128230   Fts3Table *p,                   /* Virtual table handle */
 128231   Fts3MultiSegReader *pCsr,       /* Cursor object */
 128232   const char *zTerm,              /* Term searched for (or NULL) */
 128233   int nTerm                       /* Length of zTerm in bytes */
 128235   int i;
 128236   int nSeg = pCsr->nSegment;
 128238   /* If the Fts3SegFilter defines a specific term (or term prefix) to search 
 128239   ** for, then advance each segment iterator until it points to a term of
 128240   ** equal or greater value than the specified term. This prevents many
 128241   ** unnecessary merge/sort operations for the case where single segment
 128242   ** b-tree leaf nodes contain more than one term.
 128244   for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
 128245     int res = 0;
 128246     Fts3SegReader *pSeg = pCsr->apSegment[i];
 128247     do {
 128248       int rc = fts3SegReaderNext(p, pSeg, 0);
 128249       if( rc!=SQLITE_OK ) return rc;
 128250     }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
 128252     if( pSeg->bLookup && res!=0 ){
 128253       fts3SegReaderSetEof(pSeg);
 128256   fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
 128258   return SQLITE_OK;
 128261 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
 128262   Fts3Table *p,                   /* Virtual table handle */
 128263   Fts3MultiSegReader *pCsr,       /* Cursor object */
 128264   Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
 128266   pCsr->pFilter = pFilter;
 128267   return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
 128270 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
 128271   Fts3Table *p,                   /* Virtual table handle */
 128272   Fts3MultiSegReader *pCsr,       /* Cursor object */
 128273   int iCol,                       /* Column to match on. */
 128274   const char *zTerm,              /* Term to iterate through a doclist for */
 128275   int nTerm                       /* Number of bytes in zTerm */
 128277   int i;
 128278   int rc;
 128279   int nSegment = pCsr->nSegment;
 128280   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
 128281     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
 128284   assert( pCsr->pFilter==0 );
 128285   assert( zTerm && nTerm>0 );
 128287   /* Advance each segment iterator until it points to the term zTerm/nTerm. */
 128288   rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
 128289   if( rc!=SQLITE_OK ) return rc;
 128291   /* Determine how many of the segments actually point to zTerm/nTerm. */
 128292   for(i=0; i<nSegment; i++){
 128293     Fts3SegReader *pSeg = pCsr->apSegment[i];
 128294     if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
 128295       break;
 128298   pCsr->nAdvance = i;
 128300   /* Advance each of the segments to point to the first docid. */
 128301   for(i=0; i<pCsr->nAdvance; i++){
 128302     rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
 128303     if( rc!=SQLITE_OK ) return rc;
 128305   fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
 128307   assert( iCol<0 || iCol<p->nColumn );
 128308   pCsr->iColFilter = iCol;
 128310   return SQLITE_OK;
 128314 ** This function is called on a MultiSegReader that has been started using
 128315 ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
 128316 ** have been made. Calling this function puts the MultiSegReader in such
 128317 ** a state that if the next two calls are:
 128319 **   sqlite3Fts3SegReaderStart()
 128320 **   sqlite3Fts3SegReaderStep()
 128322 ** then the entire doclist for the term is available in 
 128323 ** MultiSegReader.aDoclist/nDoclist.
 128325 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
 128326   int i;                          /* Used to iterate through segment-readers */
 128328   assert( pCsr->zTerm==0 );
 128329   assert( pCsr->nTerm==0 );
 128330   assert( pCsr->aDoclist==0 );
 128331   assert( pCsr->nDoclist==0 );
 128333   pCsr->nAdvance = 0;
 128334   pCsr->bRestart = 1;
 128335   for(i=0; i<pCsr->nSegment; i++){
 128336     pCsr->apSegment[i]->pOffsetList = 0;
 128337     pCsr->apSegment[i]->nOffsetList = 0;
 128338     pCsr->apSegment[i]->iDocid = 0;
 128341   return SQLITE_OK;
 128345 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
 128346   Fts3Table *p,                   /* Virtual table handle */
 128347   Fts3MultiSegReader *pCsr        /* Cursor object */
 128349   int rc = SQLITE_OK;
 128351   int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
 128352   int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
 128353   int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
 128354   int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
 128355   int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
 128356   int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
 128358   Fts3SegReader **apSegment = pCsr->apSegment;
 128359   int nSegment = pCsr->nSegment;
 128360   Fts3SegFilter *pFilter = pCsr->pFilter;
 128361   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
 128362     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
 128365   if( pCsr->nSegment==0 ) return SQLITE_OK;
 128367   do {
 128368     int nMerge;
 128369     int i;
 128371     /* Advance the first pCsr->nAdvance entries in the apSegment[] array
 128372     ** forward. Then sort the list in order of current term again.  
 128374     for(i=0; i<pCsr->nAdvance; i++){
 128375       Fts3SegReader *pSeg = apSegment[i];
 128376       if( pSeg->bLookup ){
 128377         fts3SegReaderSetEof(pSeg);
 128378       }else{
 128379         rc = fts3SegReaderNext(p, pSeg, 0);
 128381       if( rc!=SQLITE_OK ) return rc;
 128383     fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
 128384     pCsr->nAdvance = 0;
 128386     /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
 128387     assert( rc==SQLITE_OK );
 128388     if( apSegment[0]->aNode==0 ) break;
 128390     pCsr->nTerm = apSegment[0]->nTerm;
 128391     pCsr->zTerm = apSegment[0]->zTerm;
 128393     /* If this is a prefix-search, and if the term that apSegment[0] points
 128394     ** to does not share a suffix with pFilter->zTerm/nTerm, then all 
 128395     ** required callbacks have been made. In this case exit early.
 128397     ** Similarly, if this is a search for an exact match, and the first term
 128398     ** of segment apSegment[0] is not a match, exit early.
 128400     if( pFilter->zTerm && !isScan ){
 128401       if( pCsr->nTerm<pFilter->nTerm 
 128402        || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
 128403        || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) 
 128405         break;
 128409     nMerge = 1;
 128410     while( nMerge<nSegment 
 128411         && apSegment[nMerge]->aNode
 128412         && apSegment[nMerge]->nTerm==pCsr->nTerm 
 128413         && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
 128415       nMerge++;
 128418     assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
 128419     if( nMerge==1 
 128420      && !isIgnoreEmpty 
 128421      && !isFirst 
 128422      && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
 128424       pCsr->nDoclist = apSegment[0]->nDoclist;
 128425       if( fts3SegReaderIsPending(apSegment[0]) ){
 128426         rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
 128427         pCsr->aDoclist = pCsr->aBuffer;
 128428       }else{
 128429         pCsr->aDoclist = apSegment[0]->aDoclist;
 128431       if( rc==SQLITE_OK ) rc = SQLITE_ROW;
 128432     }else{
 128433       int nDoclist = 0;           /* Size of doclist */
 128434       sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
 128436       /* The current term of the first nMerge entries in the array
 128437       ** of Fts3SegReader objects is the same. The doclists must be merged
 128438       ** and a single term returned with the merged doclist.
 128440       for(i=0; i<nMerge; i++){
 128441         fts3SegReaderFirstDocid(p, apSegment[i]);
 128443       fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
 128444       while( apSegment[0]->pOffsetList ){
 128445         int j;                    /* Number of segments that share a docid */
 128446         char *pList;
 128447         int nList;
 128448         int nByte;
 128449         sqlite3_int64 iDocid = apSegment[0]->iDocid;
 128450         fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
 128451         j = 1;
 128452         while( j<nMerge
 128453             && apSegment[j]->pOffsetList
 128454             && apSegment[j]->iDocid==iDocid
 128456           fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
 128457           j++;
 128460         if( isColFilter ){
 128461           fts3ColumnFilter(pFilter->iCol, &pList, &nList);
 128464         if( !isIgnoreEmpty || nList>0 ){
 128466           /* Calculate the 'docid' delta value to write into the merged 
 128467           ** doclist. */
 128468           sqlite3_int64 iDelta;
 128469           if( p->bDescIdx && nDoclist>0 ){
 128470             iDelta = iPrev - iDocid;
 128471           }else{
 128472             iDelta = iDocid - iPrev;
 128474           assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
 128475           assert( nDoclist>0 || iDelta==iDocid );
 128477           nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
 128478           if( nDoclist+nByte>pCsr->nBuffer ){
 128479             char *aNew;
 128480             pCsr->nBuffer = (nDoclist+nByte)*2;
 128481             aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
 128482             if( !aNew ){
 128483               return SQLITE_NOMEM;
 128485             pCsr->aBuffer = aNew;
 128488           if( isFirst ){
 128489             char *a = &pCsr->aBuffer[nDoclist];
 128490             int nWrite;
 128492             nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
 128493             if( nWrite ){
 128494               iPrev = iDocid;
 128495               nDoclist += nWrite;
 128497           }else{
 128498             nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
 128499             iPrev = iDocid;
 128500             if( isRequirePos ){
 128501               memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
 128502               nDoclist += nList;
 128503               pCsr->aBuffer[nDoclist++] = '\0';
 128508         fts3SegReaderSort(apSegment, nMerge, j, xCmp);
 128510       if( nDoclist>0 ){
 128511         pCsr->aDoclist = pCsr->aBuffer;
 128512         pCsr->nDoclist = nDoclist;
 128513         rc = SQLITE_ROW;
 128516     pCsr->nAdvance = nMerge;
 128517   }while( rc==SQLITE_OK );
 128519   return rc;
 128523 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
 128524   Fts3MultiSegReader *pCsr       /* Cursor object */
 128526   if( pCsr ){
 128527     int i;
 128528     for(i=0; i<pCsr->nSegment; i++){
 128529       sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
 128531     sqlite3_free(pCsr->apSegment);
 128532     sqlite3_free(pCsr->aBuffer);
 128534     pCsr->nSegment = 0;
 128535     pCsr->apSegment = 0;
 128536     pCsr->aBuffer = 0;
 128541 ** Merge all level iLevel segments in the database into a single 
 128542 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
 128543 ** single segment with a level equal to the numerically largest level 
 128544 ** currently present in the database.
 128546 ** If this function is called with iLevel<0, but there is only one
 128547 ** segment in the database, SQLITE_DONE is returned immediately. 
 128548 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, 
 128549 ** an SQLite error code is returned.
 128551 static int fts3SegmentMerge(
 128552   Fts3Table *p, 
 128553   int iLangid,                    /* Language id to merge */
 128554   int iIndex,                     /* Index in p->aIndex[] to merge */
 128555   int iLevel                      /* Level to merge */
 128557   int rc;                         /* Return code */
 128558   int iIdx = 0;                   /* Index of new segment */
 128559   sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
 128560   SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
 128561   Fts3SegFilter filter;           /* Segment term filter condition */
 128562   Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
 128563   int bIgnoreEmpty = 0;           /* True to ignore empty segments */
 128565   assert( iLevel==FTS3_SEGCURSOR_ALL
 128566        || iLevel==FTS3_SEGCURSOR_PENDING
 128567        || iLevel>=0
 128569   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
 128570   assert( iIndex>=0 && iIndex<p->nIndex );
 128572   rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
 128573   if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
 128575   if( iLevel==FTS3_SEGCURSOR_ALL ){
 128576     /* This call is to merge all segments in the database to a single
 128577     ** segment. The level of the new segment is equal to the numerically
 128578     ** greatest segment level currently present in the database for this
 128579     ** index. The idx of the new segment is always 0.  */
 128580     if( csr.nSegment==1 ){
 128581       rc = SQLITE_DONE;
 128582       goto finished;
 128584     rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
 128585     bIgnoreEmpty = 1;
 128587   }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
 128588     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
 128589     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
 128590   }else{
 128591     /* This call is to merge all segments at level iLevel. find the next
 128592     ** available segment index at level iLevel+1. The call to
 128593     ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to 
 128594     ** a single iLevel+2 segment if necessary.  */
 128595     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
 128596     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
 128598   if( rc!=SQLITE_OK ) goto finished;
 128599   assert( csr.nSegment>0 );
 128600   assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
 128601   assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
 128603   memset(&filter, 0, sizeof(Fts3SegFilter));
 128604   filter.flags = FTS3_SEGMENT_REQUIRE_POS;
 128605   filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
 128607   rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
 128608   while( SQLITE_OK==rc ){
 128609     rc = sqlite3Fts3SegReaderStep(p, &csr);
 128610     if( rc!=SQLITE_ROW ) break;
 128611     rc = fts3SegWriterAdd(p, &pWriter, 1, 
 128612         csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
 128614   if( rc!=SQLITE_OK ) goto finished;
 128615   assert( pWriter );
 128617   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
 128618     rc = fts3DeleteSegdir(
 128619         p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
 128621     if( rc!=SQLITE_OK ) goto finished;
 128623   rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
 128625  finished:
 128626   fts3SegWriterFree(pWriter);
 128627   sqlite3Fts3SegReaderFinish(&csr);
 128628   return rc;
 128633 ** Flush the contents of pendingTerms to level 0 segments.
 128635 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
 128636   int rc = SQLITE_OK;
 128637   int i;
 128639   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
 128640     rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
 128641     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 128643   sqlite3Fts3PendingTermsClear(p);
 128645   /* Determine the auto-incr-merge setting if unknown.  If enabled,
 128646   ** estimate the number of leaf blocks of content to be written
 128648   if( rc==SQLITE_OK && p->bHasStat
 128649    && p->bAutoincrmerge==0xff && p->nLeafAdd>0
 128651     sqlite3_stmt *pStmt = 0;
 128652     rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
 128653     if( rc==SQLITE_OK ){
 128654       sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
 128655       rc = sqlite3_step(pStmt);
 128656       p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
 128657       rc = sqlite3_reset(pStmt);
 128660   return rc;
 128664 ** Encode N integers as varints into a blob.
 128666 static void fts3EncodeIntArray(
 128667   int N,             /* The number of integers to encode */
 128668   u32 *a,            /* The integer values */
 128669   char *zBuf,        /* Write the BLOB here */
 128670   int *pNBuf         /* Write number of bytes if zBuf[] used here */
 128672   int i, j;
 128673   for(i=j=0; i<N; i++){
 128674     j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
 128676   *pNBuf = j;
 128680 ** Decode a blob of varints into N integers
 128682 static void fts3DecodeIntArray(
 128683   int N,             /* The number of integers to decode */
 128684   u32 *a,            /* Write the integer values */
 128685   const char *zBuf,  /* The BLOB containing the varints */
 128686   int nBuf           /* size of the BLOB */
 128688   int i, j;
 128689   UNUSED_PARAMETER(nBuf);
 128690   for(i=j=0; i<N; i++){
 128691     sqlite3_int64 x;
 128692     j += sqlite3Fts3GetVarint(&zBuf[j], &x);
 128693     assert(j<=nBuf);
 128694     a[i] = (u32)(x & 0xffffffff);
 128699 ** Insert the sizes (in tokens) for each column of the document
 128700 ** with docid equal to p->iPrevDocid.  The sizes are encoded as
 128701 ** a blob of varints.
 128703 static void fts3InsertDocsize(
 128704   int *pRC,                       /* Result code */
 128705   Fts3Table *p,                   /* Table into which to insert */
 128706   u32 *aSz                        /* Sizes of each column, in tokens */
 128708   char *pBlob;             /* The BLOB encoding of the document size */
 128709   int nBlob;               /* Number of bytes in the BLOB */
 128710   sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
 128711   int rc;                  /* Result code from subfunctions */
 128713   if( *pRC ) return;
 128714   pBlob = sqlite3_malloc( 10*p->nColumn );
 128715   if( pBlob==0 ){
 128716     *pRC = SQLITE_NOMEM;
 128717     return;
 128719   fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
 128720   rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
 128721   if( rc ){
 128722     sqlite3_free(pBlob);
 128723     *pRC = rc;
 128724     return;
 128726   sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
 128727   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
 128728   sqlite3_step(pStmt);
 128729   *pRC = sqlite3_reset(pStmt);
 128733 ** Record 0 of the %_stat table contains a blob consisting of N varints,
 128734 ** where N is the number of user defined columns in the fts3 table plus
 128735 ** two. If nCol is the number of user defined columns, then values of the 
 128736 ** varints are set as follows:
 128738 **   Varint 0:       Total number of rows in the table.
 128740 **   Varint 1..nCol: For each column, the total number of tokens stored in
 128741 **                   the column for all rows of the table.
 128743 **   Varint 1+nCol:  The total size, in bytes, of all text values in all
 128744 **                   columns of all rows of the table.
 128747 static void fts3UpdateDocTotals(
 128748   int *pRC,                       /* The result code */
 128749   Fts3Table *p,                   /* Table being updated */
 128750   u32 *aSzIns,                    /* Size increases */
 128751   u32 *aSzDel,                    /* Size decreases */
 128752   int nChng                       /* Change in the number of documents */
 128754   char *pBlob;             /* Storage for BLOB written into %_stat */
 128755   int nBlob;               /* Size of BLOB written into %_stat */
 128756   u32 *a;                  /* Array of integers that becomes the BLOB */
 128757   sqlite3_stmt *pStmt;     /* Statement for reading and writing */
 128758   int i;                   /* Loop counter */
 128759   int rc;                  /* Result code from subfunctions */
 128761   const int nStat = p->nColumn+2;
 128763   if( *pRC ) return;
 128764   a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
 128765   if( a==0 ){
 128766     *pRC = SQLITE_NOMEM;
 128767     return;
 128769   pBlob = (char*)&a[nStat];
 128770   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
 128771   if( rc ){
 128772     sqlite3_free(a);
 128773     *pRC = rc;
 128774     return;
 128776   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
 128777   if( sqlite3_step(pStmt)==SQLITE_ROW ){
 128778     fts3DecodeIntArray(nStat, a,
 128779          sqlite3_column_blob(pStmt, 0),
 128780          sqlite3_column_bytes(pStmt, 0));
 128781   }else{
 128782     memset(a, 0, sizeof(u32)*(nStat) );
 128784   rc = sqlite3_reset(pStmt);
 128785   if( rc!=SQLITE_OK ){
 128786     sqlite3_free(a);
 128787     *pRC = rc;
 128788     return;
 128790   if( nChng<0 && a[0]<(u32)(-nChng) ){
 128791     a[0] = 0;
 128792   }else{
 128793     a[0] += nChng;
 128795   for(i=0; i<p->nColumn+1; i++){
 128796     u32 x = a[i+1];
 128797     if( x+aSzIns[i] < aSzDel[i] ){
 128798       x = 0;
 128799     }else{
 128800       x = x + aSzIns[i] - aSzDel[i];
 128802     a[i+1] = x;
 128804   fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
 128805   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
 128806   if( rc ){
 128807     sqlite3_free(a);
 128808     *pRC = rc;
 128809     return;
 128811   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
 128812   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
 128813   sqlite3_step(pStmt);
 128814   *pRC = sqlite3_reset(pStmt);
 128815   sqlite3_free(a);
 128819 ** Merge the entire database so that there is one segment for each 
 128820 ** iIndex/iLangid combination.
 128822 static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
 128823   int bSeenDone = 0;
 128824   int rc;
 128825   sqlite3_stmt *pAllLangid = 0;
 128827   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
 128828   if( rc==SQLITE_OK ){
 128829     int rc2;
 128830     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
 128831     while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
 128832       int i;
 128833       int iLangid = sqlite3_column_int(pAllLangid, 0);
 128834       for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
 128835         rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
 128836         if( rc==SQLITE_DONE ){
 128837           bSeenDone = 1;
 128838           rc = SQLITE_OK;
 128842     rc2 = sqlite3_reset(pAllLangid);
 128843     if( rc==SQLITE_OK ) rc = rc2;
 128846   sqlite3Fts3SegmentsClose(p);
 128847   sqlite3Fts3PendingTermsClear(p);
 128849   return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
 128853 ** This function is called when the user executes the following statement:
 128855 **     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
 128857 ** The entire FTS index is discarded and rebuilt. If the table is one 
 128858 ** created using the content=xxx option, then the new index is based on
 128859 ** the current contents of the xxx table. Otherwise, it is rebuilt based
 128860 ** on the contents of the %_content table.
 128862 static int fts3DoRebuild(Fts3Table *p){
 128863   int rc;                         /* Return Code */
 128865   rc = fts3DeleteAll(p, 0);
 128866   if( rc==SQLITE_OK ){
 128867     u32 *aSz = 0;
 128868     u32 *aSzIns = 0;
 128869     u32 *aSzDel = 0;
 128870     sqlite3_stmt *pStmt = 0;
 128871     int nEntry = 0;
 128873     /* Compose and prepare an SQL statement to loop through the content table */
 128874     char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
 128875     if( !zSql ){
 128876       rc = SQLITE_NOMEM;
 128877     }else{
 128878       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 128879       sqlite3_free(zSql);
 128882     if( rc==SQLITE_OK ){
 128883       int nByte = sizeof(u32) * (p->nColumn+1)*3;
 128884       aSz = (u32 *)sqlite3_malloc(nByte);
 128885       if( aSz==0 ){
 128886         rc = SQLITE_NOMEM;
 128887       }else{
 128888         memset(aSz, 0, nByte);
 128889         aSzIns = &aSz[p->nColumn+1];
 128890         aSzDel = &aSzIns[p->nColumn+1];
 128894     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
 128895       int iCol;
 128896       int iLangid = langidFromSelect(p, pStmt);
 128897       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
 128898       memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
 128899       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
 128900         const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
 128901         rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
 128902         aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
 128904       if( p->bHasDocsize ){
 128905         fts3InsertDocsize(&rc, p, aSz);
 128907       if( rc!=SQLITE_OK ){
 128908         sqlite3_finalize(pStmt);
 128909         pStmt = 0;
 128910       }else{
 128911         nEntry++;
 128912         for(iCol=0; iCol<=p->nColumn; iCol++){
 128913           aSzIns[iCol] += aSz[iCol];
 128917     if( p->bFts4 ){
 128918       fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
 128920     sqlite3_free(aSz);
 128922     if( pStmt ){
 128923       int rc2 = sqlite3_finalize(pStmt);
 128924       if( rc==SQLITE_OK ){
 128925         rc = rc2;
 128930   return rc;
 128935 ** This function opens a cursor used to read the input data for an 
 128936 ** incremental merge operation. Specifically, it opens a cursor to scan
 128937 ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute 
 128938 ** level iAbsLevel.
 128940 static int fts3IncrmergeCsr(
 128941   Fts3Table *p,                   /* FTS3 table handle */
 128942   sqlite3_int64 iAbsLevel,        /* Absolute level to open */
 128943   int nSeg,                       /* Number of segments to merge */
 128944   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
 128946   int rc;                         /* Return Code */
 128947   sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */  
 128948   int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
 128950   /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
 128951   memset(pCsr, 0, sizeof(*pCsr));
 128952   nByte = sizeof(Fts3SegReader *) * nSeg;
 128953   pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
 128955   if( pCsr->apSegment==0 ){
 128956     rc = SQLITE_NOMEM;
 128957   }else{
 128958     memset(pCsr->apSegment, 0, nByte);
 128959     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
 128961   if( rc==SQLITE_OK ){
 128962     int i;
 128963     int rc2;
 128964     sqlite3_bind_int64(pStmt, 1, iAbsLevel);
 128965     assert( pCsr->nSegment==0 );
 128966     for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
 128967       rc = sqlite3Fts3SegReaderNew(i, 0,
 128968           sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
 128969           sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
 128970           sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
 128971           sqlite3_column_blob(pStmt, 4),         /* segdir.root */
 128972           sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
 128973           &pCsr->apSegment[i]
 128975       pCsr->nSegment++;
 128977     rc2 = sqlite3_reset(pStmt);
 128978     if( rc==SQLITE_OK ) rc = rc2;
 128981   return rc;
 128984 typedef struct IncrmergeWriter IncrmergeWriter;
 128985 typedef struct NodeWriter NodeWriter;
 128986 typedef struct Blob Blob;
 128987 typedef struct NodeReader NodeReader;
 128990 ** An instance of the following structure is used as a dynamic buffer
 128991 ** to build up nodes or other blobs of data in.
 128993 ** The function blobGrowBuffer() is used to extend the allocation.
 128995 struct Blob {
 128996   char *a;                        /* Pointer to allocation */
 128997   int n;                          /* Number of valid bytes of data in a[] */
 128998   int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
 129002 ** This structure is used to build up buffers containing segment b-tree 
 129003 ** nodes (blocks).
 129005 struct NodeWriter {
 129006   sqlite3_int64 iBlock;           /* Current block id */
 129007   Blob key;                       /* Last key written to the current block */
 129008   Blob block;                     /* Current block image */
 129012 ** An object of this type contains the state required to create or append
 129013 ** to an appendable b-tree segment.
 129015 struct IncrmergeWriter {
 129016   int nLeafEst;                   /* Space allocated for leaf blocks */
 129017   int nWork;                      /* Number of leaf pages flushed */
 129018   sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
 129019   int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
 129020   sqlite3_int64 iStart;           /* Block number of first allocated block */
 129021   sqlite3_int64 iEnd;             /* Block number of last allocated block */
 129022   NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
 129026 ** An object of the following type is used to read data from a single
 129027 ** FTS segment node. See the following functions:
 129029 **     nodeReaderInit()
 129030 **     nodeReaderNext()
 129031 **     nodeReaderRelease()
 129033 struct NodeReader {
 129034   const char *aNode;
 129035   int nNode;
 129036   int iOff;                       /* Current offset within aNode[] */
 129038   /* Output variables. Containing the current node entry. */
 129039   sqlite3_int64 iChild;           /* Pointer to child node */
 129040   Blob term;                      /* Current term */
 129041   const char *aDoclist;           /* Pointer to doclist */
 129042   int nDoclist;                   /* Size of doclist in bytes */
 129046 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 129047 ** Otherwise, if the allocation at pBlob->a is not already at least nMin
 129048 ** bytes in size, extend (realloc) it to be so.
 129050 ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
 129051 ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
 129052 ** to reflect the new size of the pBlob->a[] buffer.
 129054 static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
 129055   if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
 129056     int nAlloc = nMin;
 129057     char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
 129058     if( a ){
 129059       pBlob->nAlloc = nAlloc;
 129060       pBlob->a = a;
 129061     }else{
 129062       *pRc = SQLITE_NOMEM;
 129068 ** Attempt to advance the node-reader object passed as the first argument to
 129069 ** the next entry on the node. 
 129071 ** Return an error code if an error occurs (SQLITE_NOMEM is possible). 
 129072 ** Otherwise return SQLITE_OK. If there is no next entry on the node
 129073 ** (e.g. because the current entry is the last) set NodeReader->aNode to
 129074 ** NULL to indicate EOF. Otherwise, populate the NodeReader structure output 
 129075 ** variables for the new entry.
 129077 static int nodeReaderNext(NodeReader *p){
 129078   int bFirst = (p->term.n==0);    /* True for first term on the node */
 129079   int nPrefix = 0;                /* Bytes to copy from previous term */
 129080   int nSuffix = 0;                /* Bytes to append to the prefix */
 129081   int rc = SQLITE_OK;             /* Return code */
 129083   assert( p->aNode );
 129084   if( p->iChild && bFirst==0 ) p->iChild++;
 129085   if( p->iOff>=p->nNode ){
 129086     /* EOF */
 129087     p->aNode = 0;
 129088   }else{
 129089     if( bFirst==0 ){
 129090       p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
 129092     p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
 129094     blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
 129095     if( rc==SQLITE_OK ){
 129096       memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
 129097       p->term.n = nPrefix+nSuffix;
 129098       p->iOff += nSuffix;
 129099       if( p->iChild==0 ){
 129100         p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
 129101         p->aDoclist = &p->aNode[p->iOff];
 129102         p->iOff += p->nDoclist;
 129107   assert( p->iOff<=p->nNode );
 129109   return rc;
 129113 ** Release all dynamic resources held by node-reader object *p.
 129115 static void nodeReaderRelease(NodeReader *p){
 129116   sqlite3_free(p->term.a);
 129120 ** Initialize a node-reader object to read the node in buffer aNode/nNode.
 129122 ** If successful, SQLITE_OK is returned and the NodeReader object set to 
 129123 ** point to the first entry on the node (if any). Otherwise, an SQLite
 129124 ** error code is returned.
 129126 static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
 129127   memset(p, 0, sizeof(NodeReader));
 129128   p->aNode = aNode;
 129129   p->nNode = nNode;
 129131   /* Figure out if this is a leaf or an internal node. */
 129132   if( p->aNode[0] ){
 129133     /* An internal node. */
 129134     p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
 129135   }else{
 129136     p->iOff = 1;
 129139   return nodeReaderNext(p);
 129143 ** This function is called while writing an FTS segment each time a leaf o
 129144 ** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
 129145 ** to be greater than the largest key on the node just written, but smaller
 129146 ** than or equal to the first key that will be written to the next leaf
 129147 ** node.
 129149 ** The block id of the leaf node just written to disk may be found in
 129150 ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
 129152 static int fts3IncrmergePush(
 129153   Fts3Table *p,                   /* Fts3 table handle */
 129154   IncrmergeWriter *pWriter,       /* Writer object */
 129155   const char *zTerm,              /* Term to write to internal node */
 129156   int nTerm                       /* Bytes at zTerm */
 129158   sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
 129159   int iLayer;
 129161   assert( nTerm>0 );
 129162   for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
 129163     sqlite3_int64 iNextPtr = 0;
 129164     NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
 129165     int rc = SQLITE_OK;
 129166     int nPrefix;
 129167     int nSuffix;
 129168     int nSpace;
 129170     /* Figure out how much space the key will consume if it is written to
 129171     ** the current node of layer iLayer. Due to the prefix compression, 
 129172     ** the space required changes depending on which node the key is to
 129173     ** be added to.  */
 129174     nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
 129175     nSuffix = nTerm - nPrefix;
 129176     nSpace  = sqlite3Fts3VarintLen(nPrefix);
 129177     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
 129179     if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){ 
 129180       /* If the current node of layer iLayer contains zero keys, or if adding
 129181       ** the key to it will not cause it to grow to larger than nNodeSize 
 129182       ** bytes in size, write the key here.  */
 129184       Blob *pBlk = &pNode->block;
 129185       if( pBlk->n==0 ){
 129186         blobGrowBuffer(pBlk, p->nNodeSize, &rc);
 129187         if( rc==SQLITE_OK ){
 129188           pBlk->a[0] = (char)iLayer;
 129189           pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
 129192       blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
 129193       blobGrowBuffer(&pNode->key, nTerm, &rc);
 129195       if( rc==SQLITE_OK ){
 129196         if( pNode->key.n ){
 129197           pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
 129199         pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
 129200         memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
 129201         pBlk->n += nSuffix;
 129203         memcpy(pNode->key.a, zTerm, nTerm);
 129204         pNode->key.n = nTerm;
 129206     }else{
 129207       /* Otherwise, flush the current node of layer iLayer to disk.
 129208       ** Then allocate a new, empty sibling node. The key will be written
 129209       ** into the parent of this node. */
 129210       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
 129212       assert( pNode->block.nAlloc>=p->nNodeSize );
 129213       pNode->block.a[0] = (char)iLayer;
 129214       pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
 129216       iNextPtr = pNode->iBlock;
 129217       pNode->iBlock++;
 129218       pNode->key.n = 0;
 129221     if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
 129222     iPtr = iNextPtr;
 129225   assert( 0 );
 129226   return 0;
 129230 ** Append a term and (optionally) doclist to the FTS segment node currently
 129231 ** stored in blob *pNode. The node need not contain any terms, but the
 129232 ** header must be written before this function is called.
 129234 ** A node header is a single 0x00 byte for a leaf node, or a height varint
 129235 ** followed by the left-hand-child varint for an internal node.
 129237 ** The term to be appended is passed via arguments zTerm/nTerm. For a 
 129238 ** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
 129239 ** node, both aDoclist and nDoclist must be passed 0.
 129241 ** If the size of the value in blob pPrev is zero, then this is the first
 129242 ** term written to the node. Otherwise, pPrev contains a copy of the 
 129243 ** previous term. Before this function returns, it is updated to contain a
 129244 ** copy of zTerm/nTerm.
 129246 ** It is assumed that the buffer associated with pNode is already large
 129247 ** enough to accommodate the new entry. The buffer associated with pPrev
 129248 ** is extended by this function if requrired.
 129250 ** If an error (i.e. OOM condition) occurs, an SQLite error code is
 129251 ** returned. Otherwise, SQLITE_OK.
 129253 static int fts3AppendToNode(
 129254   Blob *pNode,                    /* Current node image to append to */
 129255   Blob *pPrev,                    /* Buffer containing previous term written */
 129256   const char *zTerm,              /* New term to write */
 129257   int nTerm,                      /* Size of zTerm in bytes */
 129258   const char *aDoclist,           /* Doclist (or NULL) to write */
 129259   int nDoclist                    /* Size of aDoclist in bytes */ 
 129261   int rc = SQLITE_OK;             /* Return code */
 129262   int bFirst = (pPrev->n==0);     /* True if this is the first term written */
 129263   int nPrefix;                    /* Size of term prefix in bytes */
 129264   int nSuffix;                    /* Size of term suffix in bytes */
 129266   /* Node must have already been started. There must be a doclist for a
 129267   ** leaf node, and there must not be a doclist for an internal node.  */
 129268   assert( pNode->n>0 );
 129269   assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
 129271   blobGrowBuffer(pPrev, nTerm, &rc);
 129272   if( rc!=SQLITE_OK ) return rc;
 129274   nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
 129275   nSuffix = nTerm - nPrefix;
 129276   memcpy(pPrev->a, zTerm, nTerm);
 129277   pPrev->n = nTerm;
 129279   if( bFirst==0 ){
 129280     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
 129282   pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
 129283   memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
 129284   pNode->n += nSuffix;
 129286   if( aDoclist ){
 129287     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
 129288     memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
 129289     pNode->n += nDoclist;
 129292   assert( pNode->n<=pNode->nAlloc );
 129294   return SQLITE_OK;
 129298 ** Append the current term and doclist pointed to by cursor pCsr to the
 129299 ** appendable b-tree segment opened for writing by pWriter.
 129301 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
 129303 static int fts3IncrmergeAppend(
 129304   Fts3Table *p,                   /* Fts3 table handle */
 129305   IncrmergeWriter *pWriter,       /* Writer object */
 129306   Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
 129308   const char *zTerm = pCsr->zTerm;
 129309   int nTerm = pCsr->nTerm;
 129310   const char *aDoclist = pCsr->aDoclist;
 129311   int nDoclist = pCsr->nDoclist;
 129312   int rc = SQLITE_OK;           /* Return code */
 129313   int nSpace;                   /* Total space in bytes required on leaf */
 129314   int nPrefix;                  /* Size of prefix shared with previous term */
 129315   int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
 129316   NodeWriter *pLeaf;            /* Object used to write leaf nodes */
 129318   pLeaf = &pWriter->aNodeWriter[0];
 129319   nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
 129320   nSuffix = nTerm - nPrefix;
 129322   nSpace  = sqlite3Fts3VarintLen(nPrefix);
 129323   nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
 129324   nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
 129326   /* If the current block is not empty, and if adding this term/doclist
 129327   ** to the current block would make it larger than Fts3Table.nNodeSize
 129328   ** bytes, write this block out to the database. */
 129329   if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
 129330     rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
 129331     pWriter->nWork++;
 129333     /* Add the current term to the parent node. The term added to the 
 129334     ** parent must:
 129336     **   a) be greater than the largest term on the leaf node just written
 129337     **      to the database (still available in pLeaf->key), and
 129339     **   b) be less than or equal to the term about to be added to the new
 129340     **      leaf node (zTerm/nTerm).
 129342     ** In other words, it must be the prefix of zTerm 1 byte longer than
 129343     ** the common prefix (if any) of zTerm and pWriter->zTerm.
 129345     if( rc==SQLITE_OK ){
 129346       rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
 129349     /* Advance to the next output block */
 129350     pLeaf->iBlock++;
 129351     pLeaf->key.n = 0;
 129352     pLeaf->block.n = 0;
 129354     nSuffix = nTerm;
 129355     nSpace  = 1;
 129356     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
 129357     nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
 129360   blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
 129362   if( rc==SQLITE_OK ){
 129363     if( pLeaf->block.n==0 ){
 129364       pLeaf->block.n = 1;
 129365       pLeaf->block.a[0] = '\0';
 129367     rc = fts3AppendToNode(
 129368         &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
 129372   return rc;
 129376 ** This function is called to release all dynamic resources held by the
 129377 ** merge-writer object pWriter, and if no error has occurred, to flush
 129378 ** all outstanding node buffers held by pWriter to disk.
 129380 ** If *pRc is not SQLITE_OK when this function is called, then no attempt
 129381 ** is made to write any data to disk. Instead, this function serves only
 129382 ** to release outstanding resources.
 129384 ** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
 129385 ** flushing buffers to disk, *pRc is set to an SQLite error code before
 129386 ** returning.
 129388 static void fts3IncrmergeRelease(
 129389   Fts3Table *p,                   /* FTS3 table handle */
 129390   IncrmergeWriter *pWriter,       /* Merge-writer object */
 129391   int *pRc                        /* IN/OUT: Error code */
 129393   int i;                          /* Used to iterate through non-root layers */
 129394   int iRoot;                      /* Index of root in pWriter->aNodeWriter */
 129395   NodeWriter *pRoot;              /* NodeWriter for root node */
 129396   int rc = *pRc;                  /* Error code */
 129398   /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment 
 129399   ** root node. If the segment fits entirely on a single leaf node, iRoot
 129400   ** will be set to 0. If the root node is the parent of the leaves, iRoot
 129401   ** will be 1. And so on.  */
 129402   for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
 129403     NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
 129404     if( pNode->block.n>0 ) break;
 129405     assert( *pRc || pNode->block.nAlloc==0 );
 129406     assert( *pRc || pNode->key.nAlloc==0 );
 129407     sqlite3_free(pNode->block.a);
 129408     sqlite3_free(pNode->key.a);
 129411   /* Empty output segment. This is a no-op. */
 129412   if( iRoot<0 ) return;
 129414   /* The entire output segment fits on a single node. Normally, this means
 129415   ** the node would be stored as a blob in the "root" column of the %_segdir
 129416   ** table. However, this is not permitted in this case. The problem is that 
 129417   ** space has already been reserved in the %_segments table, and so the 
 129418   ** start_block and end_block fields of the %_segdir table must be populated. 
 129419   ** And, by design or by accident, released versions of FTS cannot handle 
 129420   ** segments that fit entirely on the root node with start_block!=0.
 129422   ** Instead, create a synthetic root node that contains nothing but a 
 129423   ** pointer to the single content node. So that the segment consists of a
 129424   ** single leaf and a single interior (root) node.
 129426   ** Todo: Better might be to defer allocating space in the %_segments 
 129427   ** table until we are sure it is needed.
 129429   if( iRoot==0 ){
 129430     Blob *pBlock = &pWriter->aNodeWriter[1].block;
 129431     blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
 129432     if( rc==SQLITE_OK ){
 129433       pBlock->a[0] = 0x01;
 129434       pBlock->n = 1 + sqlite3Fts3PutVarint(
 129435           &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
 129438     iRoot = 1;
 129440   pRoot = &pWriter->aNodeWriter[iRoot];
 129442   /* Flush all currently outstanding nodes to disk. */
 129443   for(i=0; i<iRoot; i++){
 129444     NodeWriter *pNode = &pWriter->aNodeWriter[i];
 129445     if( pNode->block.n>0 && rc==SQLITE_OK ){
 129446       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
 129448     sqlite3_free(pNode->block.a);
 129449     sqlite3_free(pNode->key.a);
 129452   /* Write the %_segdir record. */
 129453   if( rc==SQLITE_OK ){
 129454     rc = fts3WriteSegdir(p, 
 129455         pWriter->iAbsLevel+1,               /* level */
 129456         pWriter->iIdx,                      /* idx */
 129457         pWriter->iStart,                    /* start_block */
 129458         pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
 129459         pWriter->iEnd,                      /* end_block */
 129460         pRoot->block.a, pRoot->block.n      /* root */
 129463   sqlite3_free(pRoot->block.a);
 129464   sqlite3_free(pRoot->key.a);
 129466   *pRc = rc;
 129470 ** Compare the term in buffer zLhs (size in bytes nLhs) with that in
 129471 ** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
 129472 ** the other, it is considered to be smaller than the other.
 129474 ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
 129475 ** if it is greater.
 129477 static int fts3TermCmp(
 129478   const char *zLhs, int nLhs,     /* LHS of comparison */
 129479   const char *zRhs, int nRhs      /* RHS of comparison */
 129481   int nCmp = MIN(nLhs, nRhs);
 129482   int res;
 129484   res = memcmp(zLhs, zRhs, nCmp);
 129485   if( res==0 ) res = nLhs - nRhs;
 129487   return res;
 129492 ** Query to see if the entry in the %_segments table with blockid iEnd is 
 129493 ** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
 129494 ** returning. Otherwise, set *pbRes to 0. 
 129496 ** Or, if an error occurs while querying the database, return an SQLite 
 129497 ** error code. The final value of *pbRes is undefined in this case.
 129499 ** This is used to test if a segment is an "appendable" segment. If it
 129500 ** is, then a NULL entry has been inserted into the %_segments table
 129501 ** with blockid %_segdir.end_block.
 129503 static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
 129504   int bRes = 0;                   /* Result to set *pbRes to */
 129505   sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
 129506   int rc;                         /* Return code */
 129508   rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
 129509   if( rc==SQLITE_OK ){
 129510     sqlite3_bind_int64(pCheck, 1, iEnd);
 129511     if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
 129512     rc = sqlite3_reset(pCheck);
 129515   *pbRes = bRes;
 129516   return rc;
 129520 ** This function is called when initializing an incremental-merge operation.
 129521 ** It checks if the existing segment with index value iIdx at absolute level 
 129522 ** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
 129523 ** merge-writer object *pWriter is initialized to write to it.
 129525 ** An existing segment can be appended to by an incremental merge if:
 129527 **   * It was initially created as an appendable segment (with all required
 129528 **     space pre-allocated), and
 129530 **   * The first key read from the input (arguments zKey and nKey) is 
 129531 **     greater than the largest key currently stored in the potential
 129532 **     output segment.
 129534 static int fts3IncrmergeLoad(
 129535   Fts3Table *p,                   /* Fts3 table handle */
 129536   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
 129537   int iIdx,                       /* Index of candidate output segment */
 129538   const char *zKey,               /* First key to write */
 129539   int nKey,                       /* Number of bytes in nKey */
 129540   IncrmergeWriter *pWriter        /* Populate this object */
 129542   int rc;                         /* Return code */
 129543   sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
 129545   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
 129546   if( rc==SQLITE_OK ){
 129547     sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
 129548     sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
 129549     sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
 129550     const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
 129551     int nRoot = 0;                /* Size of aRoot[] in bytes */
 129552     int rc2;                      /* Return code from sqlite3_reset() */
 129553     int bAppendable = 0;          /* Set to true if segment is appendable */
 129555     /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
 129556     sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
 129557     sqlite3_bind_int(pSelect, 2, iIdx);
 129558     if( sqlite3_step(pSelect)==SQLITE_ROW ){
 129559       iStart = sqlite3_column_int64(pSelect, 1);
 129560       iLeafEnd = sqlite3_column_int64(pSelect, 2);
 129561       iEnd = sqlite3_column_int64(pSelect, 3);
 129562       nRoot = sqlite3_column_bytes(pSelect, 4);
 129563       aRoot = sqlite3_column_blob(pSelect, 4);
 129564     }else{
 129565       return sqlite3_reset(pSelect);
 129568     /* Check for the zero-length marker in the %_segments table */
 129569     rc = fts3IsAppendable(p, iEnd, &bAppendable);
 129571     /* Check that zKey/nKey is larger than the largest key the candidate */
 129572     if( rc==SQLITE_OK && bAppendable ){
 129573       char *aLeaf = 0;
 129574       int nLeaf = 0;
 129576       rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
 129577       if( rc==SQLITE_OK ){
 129578         NodeReader reader;
 129579         for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
 129580             rc==SQLITE_OK && reader.aNode;
 129581             rc = nodeReaderNext(&reader)
 129583           assert( reader.aNode );
 129585         if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
 129586           bAppendable = 0;
 129588         nodeReaderRelease(&reader);
 129590       sqlite3_free(aLeaf);
 129593     if( rc==SQLITE_OK && bAppendable ){
 129594       /* It is possible to append to this segment. Set up the IncrmergeWriter
 129595       ** object to do so.  */
 129596       int i;
 129597       int nHeight = (int)aRoot[0];
 129598       NodeWriter *pNode;
 129600       pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
 129601       pWriter->iStart = iStart;
 129602       pWriter->iEnd = iEnd;
 129603       pWriter->iAbsLevel = iAbsLevel;
 129604       pWriter->iIdx = iIdx;
 129606       for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
 129607         pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
 129610       pNode = &pWriter->aNodeWriter[nHeight];
 129611       pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
 129612       blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
 129613       if( rc==SQLITE_OK ){
 129614         memcpy(pNode->block.a, aRoot, nRoot);
 129615         pNode->block.n = nRoot;
 129618       for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
 129619         NodeReader reader;
 129620         pNode = &pWriter->aNodeWriter[i];
 129622         rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
 129623         while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
 129624         blobGrowBuffer(&pNode->key, reader.term.n, &rc);
 129625         if( rc==SQLITE_OK ){
 129626           memcpy(pNode->key.a, reader.term.a, reader.term.n);
 129627           pNode->key.n = reader.term.n;
 129628           if( i>0 ){
 129629             char *aBlock = 0;
 129630             int nBlock = 0;
 129631             pNode = &pWriter->aNodeWriter[i-1];
 129632             pNode->iBlock = reader.iChild;
 129633             rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
 129634             blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
 129635             if( rc==SQLITE_OK ){
 129636               memcpy(pNode->block.a, aBlock, nBlock);
 129637               pNode->block.n = nBlock;
 129639             sqlite3_free(aBlock);
 129642         nodeReaderRelease(&reader);
 129646     rc2 = sqlite3_reset(pSelect);
 129647     if( rc==SQLITE_OK ) rc = rc2;
 129650   return rc;
 129654 ** Determine the largest segment index value that exists within absolute
 129655 ** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
 129656 ** one before returning SQLITE_OK. Or, if there are no segments at all 
 129657 ** within level iAbsLevel, set *piIdx to zero.
 129659 ** If an error occurs, return an SQLite error code. The final value of
 129660 ** *piIdx is undefined in this case.
 129662 static int fts3IncrmergeOutputIdx( 
 129663   Fts3Table *p,                   /* FTS Table handle */
 129664   sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
 129665   int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
 129667   int rc;
 129668   sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
 129670   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
 129671   if( rc==SQLITE_OK ){
 129672     sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
 129673     sqlite3_step(pOutputIdx);
 129674     *piIdx = sqlite3_column_int(pOutputIdx, 0);
 129675     rc = sqlite3_reset(pOutputIdx);
 129678   return rc;
 129682 ** Allocate an appendable output segment on absolute level iAbsLevel+1
 129683 ** with idx value iIdx.
 129685 ** In the %_segdir table, a segment is defined by the values in three
 129686 ** columns:
 129688 **     start_block
 129689 **     leaves_end_block
 129690 **     end_block
 129692 ** When an appendable segment is allocated, it is estimated that the
 129693 ** maximum number of leaf blocks that may be required is the sum of the
 129694 ** number of leaf blocks consumed by the input segments, plus the number
 129695 ** of input segments, multiplied by two. This value is stored in stack 
 129696 ** variable nLeafEst.
 129698 ** A total of 16*nLeafEst blocks are allocated when an appendable segment
 129699 ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
 129700 ** array of leaf nodes starts at the first block allocated. The array
 129701 ** of interior nodes that are parents of the leaf nodes start at block
 129702 ** (start_block + (1 + end_block - start_block) / 16). And so on.
 129704 ** In the actual code below, the value "16" is replaced with the 
 129705 ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
 129707 static int fts3IncrmergeWriter( 
 129708   Fts3Table *p,                   /* Fts3 table handle */
 129709   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
 129710   int iIdx,                       /* Index of new output segment */
 129711   Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
 129712   IncrmergeWriter *pWriter        /* Populate this object */
 129714   int rc;                         /* Return Code */
 129715   int i;                          /* Iterator variable */
 129716   int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
 129717   sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
 129718   sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
 129720   /* Calculate nLeafEst. */
 129721   rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
 129722   if( rc==SQLITE_OK ){
 129723     sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
 129724     sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
 129725     if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
 129726       nLeafEst = sqlite3_column_int(pLeafEst, 0);
 129728     rc = sqlite3_reset(pLeafEst);
 129730   if( rc!=SQLITE_OK ) return rc;
 129732   /* Calculate the first block to use in the output segment */
 129733   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
 129734   if( rc==SQLITE_OK ){
 129735     if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
 129736       pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
 129737       pWriter->iEnd = pWriter->iStart - 1;
 129738       pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
 129740     rc = sqlite3_reset(pFirstBlock);
 129742   if( rc!=SQLITE_OK ) return rc;
 129744   /* Insert the marker in the %_segments table to make sure nobody tries
 129745   ** to steal the space just allocated. This is also used to identify 
 129746   ** appendable segments.  */
 129747   rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
 129748   if( rc!=SQLITE_OK ) return rc;
 129750   pWriter->iAbsLevel = iAbsLevel;
 129751   pWriter->nLeafEst = nLeafEst;
 129752   pWriter->iIdx = iIdx;
 129754   /* Set up the array of NodeWriter objects */
 129755   for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
 129756     pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
 129758   return SQLITE_OK;
 129762 ** Remove an entry from the %_segdir table. This involves running the 
 129763 ** following two statements:
 129765 **   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
 129766 **   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
 129768 ** The DELETE statement removes the specific %_segdir level. The UPDATE 
 129769 ** statement ensures that the remaining segments have contiguously allocated
 129770 ** idx values.
 129772 static int fts3RemoveSegdirEntry(
 129773   Fts3Table *p,                   /* FTS3 table handle */
 129774   sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
 129775   int iIdx                        /* Index of %_segdir entry to delete */
 129777   int rc;                         /* Return code */
 129778   sqlite3_stmt *pDelete = 0;      /* DELETE statement */
 129780   rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
 129781   if( rc==SQLITE_OK ){
 129782     sqlite3_bind_int64(pDelete, 1, iAbsLevel);
 129783     sqlite3_bind_int(pDelete, 2, iIdx);
 129784     sqlite3_step(pDelete);
 129785     rc = sqlite3_reset(pDelete);
 129788   return rc;
 129792 ** One or more segments have just been removed from absolute level iAbsLevel.
 129793 ** Update the 'idx' values of the remaining segments in the level so that
 129794 ** the idx values are a contiguous sequence starting from 0.
 129796 static int fts3RepackSegdirLevel(
 129797   Fts3Table *p,                   /* FTS3 table handle */
 129798   sqlite3_int64 iAbsLevel         /* Absolute level to repack */
 129800   int rc;                         /* Return code */
 129801   int *aIdx = 0;                  /* Array of remaining idx values */
 129802   int nIdx = 0;                   /* Valid entries in aIdx[] */
 129803   int nAlloc = 0;                 /* Allocated size of aIdx[] */
 129804   int i;                          /* Iterator variable */
 129805   sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
 129806   sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
 129808   rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
 129809   if( rc==SQLITE_OK ){
 129810     int rc2;
 129811     sqlite3_bind_int64(pSelect, 1, iAbsLevel);
 129812     while( SQLITE_ROW==sqlite3_step(pSelect) ){
 129813       if( nIdx>=nAlloc ){
 129814         int *aNew;
 129815         nAlloc += 16;
 129816         aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
 129817         if( !aNew ){
 129818           rc = SQLITE_NOMEM;
 129819           break;
 129821         aIdx = aNew;
 129823       aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
 129825     rc2 = sqlite3_reset(pSelect);
 129826     if( rc==SQLITE_OK ) rc = rc2;
 129829   if( rc==SQLITE_OK ){
 129830     rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
 129832   if( rc==SQLITE_OK ){
 129833     sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
 129836   assert( p->bIgnoreSavepoint==0 );
 129837   p->bIgnoreSavepoint = 1;
 129838   for(i=0; rc==SQLITE_OK && i<nIdx; i++){
 129839     if( aIdx[i]!=i ){
 129840       sqlite3_bind_int(pUpdate, 3, aIdx[i]);
 129841       sqlite3_bind_int(pUpdate, 1, i);
 129842       sqlite3_step(pUpdate);
 129843       rc = sqlite3_reset(pUpdate);
 129846   p->bIgnoreSavepoint = 0;
 129848   sqlite3_free(aIdx);
 129849   return rc;
 129852 static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
 129853   pNode->a[0] = (char)iHeight;
 129854   if( iChild ){
 129855     assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
 129856     pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
 129857   }else{
 129858     assert( pNode->nAlloc>=1 );
 129859     pNode->n = 1;
 129864 ** The first two arguments are a pointer to and the size of a segment b-tree
 129865 ** node. The node may be a leaf or an internal node.
 129867 ** This function creates a new node image in blob object *pNew by copying
 129868 ** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
 129869 ** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
 129871 static int fts3TruncateNode(
 129872   const char *aNode,              /* Current node image */
 129873   int nNode,                      /* Size of aNode in bytes */
 129874   Blob *pNew,                     /* OUT: Write new node image here */
 129875   const char *zTerm,              /* Omit all terms smaller than this */
 129876   int nTerm,                      /* Size of zTerm in bytes */
 129877   sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
 129879   NodeReader reader;              /* Reader object */
 129880   Blob prev = {0, 0, 0};          /* Previous term written to new node */
 129881   int rc = SQLITE_OK;             /* Return code */
 129882   int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
 129884   /* Allocate required output space */
 129885   blobGrowBuffer(pNew, nNode, &rc);
 129886   if( rc!=SQLITE_OK ) return rc;
 129887   pNew->n = 0;
 129889   /* Populate new node buffer */
 129890   for(rc = nodeReaderInit(&reader, aNode, nNode); 
 129891       rc==SQLITE_OK && reader.aNode; 
 129892       rc = nodeReaderNext(&reader)
 129894     if( pNew->n==0 ){
 129895       int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
 129896       if( res<0 || (bLeaf==0 && res==0) ) continue;
 129897       fts3StartNode(pNew, (int)aNode[0], reader.iChild);
 129898       *piBlock = reader.iChild;
 129900     rc = fts3AppendToNode(
 129901         pNew, &prev, reader.term.a, reader.term.n,
 129902         reader.aDoclist, reader.nDoclist
 129904     if( rc!=SQLITE_OK ) break;
 129906   if( pNew->n==0 ){
 129907     fts3StartNode(pNew, (int)aNode[0], reader.iChild);
 129908     *piBlock = reader.iChild;
 129910   assert( pNew->n<=pNew->nAlloc );
 129912   nodeReaderRelease(&reader);
 129913   sqlite3_free(prev.a);
 129914   return rc;
 129918 ** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute 
 129919 ** level iAbsLevel. This may involve deleting entries from the %_segments
 129920 ** table, and modifying existing entries in both the %_segments and %_segdir
 129921 ** tables.
 129923 ** SQLITE_OK is returned if the segment is updated successfully. Or an
 129924 ** SQLite error code otherwise.
 129926 static int fts3TruncateSegment(
 129927   Fts3Table *p,                   /* FTS3 table handle */
 129928   sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
 129929   int iIdx,                       /* Index within level of segment to modify */
 129930   const char *zTerm,              /* Remove terms smaller than this */
 129931   int nTerm                      /* Number of bytes in buffer zTerm */
 129933   int rc = SQLITE_OK;             /* Return code */
 129934   Blob root = {0,0,0};            /* New root page image */
 129935   Blob block = {0,0,0};           /* Buffer used for any other block */
 129936   sqlite3_int64 iBlock = 0;       /* Block id */
 129937   sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
 129938   sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
 129939   sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
 129941   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
 129942   if( rc==SQLITE_OK ){
 129943     int rc2;                      /* sqlite3_reset() return code */
 129944     sqlite3_bind_int64(pFetch, 1, iAbsLevel);
 129945     sqlite3_bind_int(pFetch, 2, iIdx);
 129946     if( SQLITE_ROW==sqlite3_step(pFetch) ){
 129947       const char *aRoot = sqlite3_column_blob(pFetch, 4);
 129948       int nRoot = sqlite3_column_bytes(pFetch, 4);
 129949       iOldStart = sqlite3_column_int64(pFetch, 1);
 129950       rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
 129952     rc2 = sqlite3_reset(pFetch);
 129953     if( rc==SQLITE_OK ) rc = rc2;
 129956   while( rc==SQLITE_OK && iBlock ){
 129957     char *aBlock = 0;
 129958     int nBlock = 0;
 129959     iNewStart = iBlock;
 129961     rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
 129962     if( rc==SQLITE_OK ){
 129963       rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
 129965     if( rc==SQLITE_OK ){
 129966       rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
 129968     sqlite3_free(aBlock);
 129971   /* Variable iNewStart now contains the first valid leaf node. */
 129972   if( rc==SQLITE_OK && iNewStart ){
 129973     sqlite3_stmt *pDel = 0;
 129974     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
 129975     if( rc==SQLITE_OK ){
 129976       sqlite3_bind_int64(pDel, 1, iOldStart);
 129977       sqlite3_bind_int64(pDel, 2, iNewStart-1);
 129978       sqlite3_step(pDel);
 129979       rc = sqlite3_reset(pDel);
 129983   if( rc==SQLITE_OK ){
 129984     sqlite3_stmt *pChomp = 0;
 129985     rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
 129986     if( rc==SQLITE_OK ){
 129987       sqlite3_bind_int64(pChomp, 1, iNewStart);
 129988       sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
 129989       sqlite3_bind_int64(pChomp, 3, iAbsLevel);
 129990       sqlite3_bind_int(pChomp, 4, iIdx);
 129991       sqlite3_step(pChomp);
 129992       rc = sqlite3_reset(pChomp);
 129996   sqlite3_free(root.a);
 129997   sqlite3_free(block.a);
 129998   return rc;
 130002 ** This function is called after an incrmental-merge operation has run to
 130003 ** merge (or partially merge) two or more segments from absolute level
 130004 ** iAbsLevel.
 130006 ** Each input segment is either removed from the db completely (if all of
 130007 ** its data was copied to the output segment by the incrmerge operation)
 130008 ** or modified in place so that it no longer contains those entries that
 130009 ** have been duplicated in the output segment.
 130011 static int fts3IncrmergeChomp(
 130012   Fts3Table *p,                   /* FTS table handle */
 130013   sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
 130014   Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
 130015   int *pnRem                      /* Number of segments not deleted */
 130017   int i;
 130018   int nRem = 0;
 130019   int rc = SQLITE_OK;
 130021   for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
 130022     Fts3SegReader *pSeg = 0;
 130023     int j;
 130025     /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
 130026     ** somewhere in the pCsr->apSegment[] array.  */
 130027     for(j=0; ALWAYS(j<pCsr->nSegment); j++){
 130028       pSeg = pCsr->apSegment[j];
 130029       if( pSeg->iIdx==i ) break;
 130031     assert( j<pCsr->nSegment && pSeg->iIdx==i );
 130033     if( pSeg->aNode==0 ){
 130034       /* Seg-reader is at EOF. Remove the entire input segment. */
 130035       rc = fts3DeleteSegment(p, pSeg);
 130036       if( rc==SQLITE_OK ){
 130037         rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
 130039       *pnRem = 0;
 130040     }else{
 130041       /* The incremental merge did not copy all the data from this 
 130042       ** segment to the upper level. The segment is modified in place
 130043       ** so that it contains no keys smaller than zTerm/nTerm. */ 
 130044       const char *zTerm = pSeg->zTerm;
 130045       int nTerm = pSeg->nTerm;
 130046       rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
 130047       nRem++;
 130051   if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
 130052     rc = fts3RepackSegdirLevel(p, iAbsLevel);
 130055   *pnRem = nRem;
 130056   return rc;
 130060 ** Store an incr-merge hint in the database.
 130062 static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
 130063   sqlite3_stmt *pReplace = 0;
 130064   int rc;                         /* Return code */
 130066   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
 130067   if( rc==SQLITE_OK ){
 130068     sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
 130069     sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
 130070     sqlite3_step(pReplace);
 130071     rc = sqlite3_reset(pReplace);
 130074   return rc;
 130078 ** Load an incr-merge hint from the database. The incr-merge hint, if one 
 130079 ** exists, is stored in the rowid==1 row of the %_stat table.
 130081 ** If successful, populate blob *pHint with the value read from the %_stat
 130082 ** table and return SQLITE_OK. Otherwise, if an error occurs, return an
 130083 ** SQLite error code.
 130085 static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
 130086   sqlite3_stmt *pSelect = 0;
 130087   int rc;
 130089   pHint->n = 0;
 130090   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
 130091   if( rc==SQLITE_OK ){
 130092     int rc2;
 130093     sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
 130094     if( SQLITE_ROW==sqlite3_step(pSelect) ){
 130095       const char *aHint = sqlite3_column_blob(pSelect, 0);
 130096       int nHint = sqlite3_column_bytes(pSelect, 0);
 130097       if( aHint ){
 130098         blobGrowBuffer(pHint, nHint, &rc);
 130099         if( rc==SQLITE_OK ){
 130100           memcpy(pHint->a, aHint, nHint);
 130101           pHint->n = nHint;
 130105     rc2 = sqlite3_reset(pSelect);
 130106     if( rc==SQLITE_OK ) rc = rc2;
 130109   return rc;
 130113 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 130114 ** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
 130115 ** consists of two varints, the absolute level number of the input segments 
 130116 ** and the number of input segments.
 130118 ** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
 130119 ** set *pRc to an SQLite error code before returning.
 130121 static void fts3IncrmergeHintPush(
 130122   Blob *pHint,                    /* Hint blob to append to */
 130123   i64 iAbsLevel,                  /* First varint to store in hint */
 130124   int nInput,                     /* Second varint to store in hint */
 130125   int *pRc                        /* IN/OUT: Error code */
 130127   blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
 130128   if( *pRc==SQLITE_OK ){
 130129     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
 130130     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
 130135 ** Read the last entry (most recently pushed) from the hint blob *pHint
 130136 ** and then remove the entry. Write the two values read to *piAbsLevel and 
 130137 ** *pnInput before returning.
 130139 ** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
 130140 ** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
 130142 static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
 130143   const int nHint = pHint->n;
 130144   int i;
 130146   i = pHint->n-2;
 130147   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
 130148   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
 130150   pHint->n = i;
 130151   i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
 130152   i += sqlite3Fts3GetVarint32(&pHint->a[i], pnInput);
 130153   if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
 130155   return SQLITE_OK;
 130160 ** Attempt an incremental merge that writes nMerge leaf blocks.
 130162 ** Incremental merges happen nMin segments at a time. The two
 130163 ** segments to be merged are the nMin oldest segments (the ones with
 130164 ** the smallest indexes) in the highest level that contains at least
 130165 ** nMin segments. Multiple merges might occur in an attempt to write the 
 130166 ** quota of nMerge leaf blocks.
 130168 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
 130169   int rc;                         /* Return code */
 130170   int nRem = nMerge;              /* Number of leaf pages yet to  be written */
 130171   Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
 130172   Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
 130173   IncrmergeWriter *pWriter;       /* Writer object */
 130174   int nSeg = 0;                   /* Number of input segments */
 130175   sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
 130176   Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
 130177   int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
 130179   /* Allocate space for the cursor, filter and writer objects */
 130180   const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
 130181   pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
 130182   if( !pWriter ) return SQLITE_NOMEM;
 130183   pFilter = (Fts3SegFilter *)&pWriter[1];
 130184   pCsr = (Fts3MultiSegReader *)&pFilter[1];
 130186   rc = fts3IncrmergeHintLoad(p, &hint);
 130187   while( rc==SQLITE_OK && nRem>0 ){
 130188     const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
 130189     sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
 130190     int bUseHint = 0;             /* True if attempting to append */
 130192     /* Search the %_segdir table for the absolute level with the smallest
 130193     ** relative level number that contains at least nMin segments, if any.
 130194     ** If one is found, set iAbsLevel to the absolute level number and
 130195     ** nSeg to nMin. If no level with at least nMin segments can be found, 
 130196     ** set nSeg to -1.
 130198     rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
 130199     sqlite3_bind_int(pFindLevel, 1, nMin);
 130200     if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
 130201       iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
 130202       nSeg = nMin;
 130203     }else{
 130204       nSeg = -1;
 130206     rc = sqlite3_reset(pFindLevel);
 130208     /* If the hint read from the %_stat table is not empty, check if the
 130209     ** last entry in it specifies a relative level smaller than or equal
 130210     ** to the level identified by the block above (if any). If so, this 
 130211     ** iteration of the loop will work on merging at the hinted level.
 130213     if( rc==SQLITE_OK && hint.n ){
 130214       int nHint = hint.n;
 130215       sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
 130216       int nHintSeg = 0;                     /* Hint number of segments */
 130218       rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
 130219       if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
 130220         iAbsLevel = iHintAbsLevel;
 130221         nSeg = nHintSeg;
 130222         bUseHint = 1;
 130223         bDirtyHint = 1;
 130224       }else{
 130225         /* This undoes the effect of the HintPop() above - so that no entry
 130226         ** is removed from the hint blob.  */
 130227         hint.n = nHint;
 130231     /* If nSeg is less that zero, then there is no level with at least
 130232     ** nMin segments and no hint in the %_stat table. No work to do.
 130233     ** Exit early in this case.  */
 130234     if( nSeg<0 ) break;
 130236     /* Open a cursor to iterate through the contents of the oldest nSeg 
 130237     ** indexes of absolute level iAbsLevel. If this cursor is opened using 
 130238     ** the 'hint' parameters, it is possible that there are less than nSeg
 130239     ** segments available in level iAbsLevel. In this case, no work is
 130240     ** done on iAbsLevel - fall through to the next iteration of the loop 
 130241     ** to start work on some other level.  */
 130242     memset(pWriter, 0, nAlloc);
 130243     pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
 130244     if( rc==SQLITE_OK ){
 130245       rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
 130247     if( SQLITE_OK==rc && pCsr->nSegment==nSeg
 130248      && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
 130249      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
 130251       int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
 130252       rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
 130253       if( rc==SQLITE_OK ){
 130254         if( bUseHint && iIdx>0 ){
 130255           const char *zKey = pCsr->zTerm;
 130256           int nKey = pCsr->nTerm;
 130257           rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
 130258         }else{
 130259           rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
 130263       if( rc==SQLITE_OK && pWriter->nLeafEst ){
 130264         fts3LogMerge(nSeg, iAbsLevel);
 130265         do {
 130266           rc = fts3IncrmergeAppend(p, pWriter, pCsr);
 130267           if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
 130268           if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
 130269         }while( rc==SQLITE_ROW );
 130271         /* Update or delete the input segments */
 130272         if( rc==SQLITE_OK ){
 130273           nRem -= (1 + pWriter->nWork);
 130274           rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
 130275           if( nSeg!=0 ){
 130276             bDirtyHint = 1;
 130277             fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
 130282       fts3IncrmergeRelease(p, pWriter, &rc);
 130285     sqlite3Fts3SegReaderFinish(pCsr);
 130288   /* Write the hint values into the %_stat table for the next incr-merger */
 130289   if( bDirtyHint && rc==SQLITE_OK ){
 130290     rc = fts3IncrmergeHintStore(p, &hint);
 130293   sqlite3_free(pWriter);
 130294   sqlite3_free(hint.a);
 130295   return rc;
 130299 ** Convert the text beginning at *pz into an integer and return
 130300 ** its value.  Advance *pz to point to the first character past
 130301 ** the integer.
 130303 static int fts3Getint(const char **pz){
 130304   const char *z = *pz;
 130305   int i = 0;
 130306   while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
 130307   *pz = z;
 130308   return i;
 130312 ** Process statements of the form:
 130314 **    INSERT INTO table(table) VALUES('merge=A,B');
 130316 ** A and B are integers that decode to be the number of leaf pages
 130317 ** written for the merge, and the minimum number of segments on a level
 130318 ** before it will be selected for a merge, respectively.
 130320 static int fts3DoIncrmerge(
 130321   Fts3Table *p,                   /* FTS3 table handle */
 130322   const char *zParam              /* Nul-terminated string containing "A,B" */
 130324   int rc;
 130325   int nMin = (FTS3_MERGE_COUNT / 2);
 130326   int nMerge = 0;
 130327   const char *z = zParam;
 130329   /* Read the first integer value */
 130330   nMerge = fts3Getint(&z);
 130332   /* If the first integer value is followed by a ',',  read the second
 130333   ** integer value. */
 130334   if( z[0]==',' && z[1]!='\0' ){
 130335     z++;
 130336     nMin = fts3Getint(&z);
 130339   if( z[0]!='\0' || nMin<2 ){
 130340     rc = SQLITE_ERROR;
 130341   }else{
 130342     rc = SQLITE_OK;
 130343     if( !p->bHasStat ){
 130344       assert( p->bFts4==0 );
 130345       sqlite3Fts3CreateStatTable(&rc, p);
 130347     if( rc==SQLITE_OK ){
 130348       rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
 130350     sqlite3Fts3SegmentsClose(p);
 130352   return rc;
 130356 ** Process statements of the form:
 130358 **    INSERT INTO table(table) VALUES('automerge=X');
 130360 ** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
 130361 ** turn it on.  The setting is persistent.
 130363 static int fts3DoAutoincrmerge(
 130364   Fts3Table *p,                   /* FTS3 table handle */
 130365   const char *zParam              /* Nul-terminated string containing boolean */
 130367   int rc = SQLITE_OK;
 130368   sqlite3_stmt *pStmt = 0;
 130369   p->bAutoincrmerge = fts3Getint(&zParam)!=0;
 130370   if( !p->bHasStat ){
 130371     assert( p->bFts4==0 );
 130372     sqlite3Fts3CreateStatTable(&rc, p);
 130373     if( rc ) return rc;
 130375   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
 130376   if( rc ) return rc;;
 130377   sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
 130378   sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
 130379   sqlite3_step(pStmt);
 130380   rc = sqlite3_reset(pStmt);
 130381   return rc;
 130385 ** Return a 64-bit checksum for the FTS index entry specified by the
 130386 ** arguments to this function.
 130388 static u64 fts3ChecksumEntry(
 130389   const char *zTerm,              /* Pointer to buffer containing term */
 130390   int nTerm,                      /* Size of zTerm in bytes */
 130391   int iLangid,                    /* Language id for current row */
 130392   int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
 130393   i64 iDocid,                     /* Docid for current row. */
 130394   int iCol,                       /* Column number */
 130395   int iPos                        /* Position */
 130397   int i;
 130398   u64 ret = (u64)iDocid;
 130400   ret += (ret<<3) + iLangid;
 130401   ret += (ret<<3) + iIndex;
 130402   ret += (ret<<3) + iCol;
 130403   ret += (ret<<3) + iPos;
 130404   for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
 130406   return ret;
 130410 ** Return a checksum of all entries in the FTS index that correspond to
 130411 ** language id iLangid. The checksum is calculated by XORing the checksums
 130412 ** of each individual entry (see fts3ChecksumEntry()) together.
 130414 ** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
 130415 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
 130416 ** return value is undefined in this case.
 130418 static u64 fts3ChecksumIndex(
 130419   Fts3Table *p,                   /* FTS3 table handle */
 130420   int iLangid,                    /* Language id to return cksum for */
 130421   int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
 130422   int *pRc                        /* OUT: Return code */
 130424   Fts3SegFilter filter;
 130425   Fts3MultiSegReader csr;
 130426   int rc;
 130427   u64 cksum = 0;
 130429   assert( *pRc==SQLITE_OK );
 130431   memset(&filter, 0, sizeof(filter));
 130432   memset(&csr, 0, sizeof(csr));
 130433   filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
 130434   filter.flags |= FTS3_SEGMENT_SCAN;
 130436   rc = sqlite3Fts3SegReaderCursor(
 130437       p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
 130439   if( rc==SQLITE_OK ){
 130440     rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
 130443   if( rc==SQLITE_OK ){
 130444     while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
 130445       char *pCsr = csr.aDoclist;
 130446       char *pEnd = &pCsr[csr.nDoclist];
 130448       i64 iDocid = 0;
 130449       i64 iCol = 0;
 130450       i64 iPos = 0;
 130452       pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
 130453       while( pCsr<pEnd ){
 130454         i64 iVal = 0;
 130455         pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
 130456         if( pCsr<pEnd ){
 130457           if( iVal==0 || iVal==1 ){
 130458             iCol = 0;
 130459             iPos = 0;
 130460             if( iVal ){
 130461               pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
 130462             }else{
 130463               pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
 130464               iDocid += iVal;
 130466           }else{
 130467             iPos += (iVal - 2);
 130468             cksum = cksum ^ fts3ChecksumEntry(
 130469                 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
 130470                 (int)iCol, (int)iPos
 130477   sqlite3Fts3SegReaderFinish(&csr);
 130479   *pRc = rc;
 130480   return cksum;
 130484 ** Check if the contents of the FTS index match the current contents of the
 130485 ** content table. If no error occurs and the contents do match, set *pbOk
 130486 ** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
 130487 ** to false before returning.
 130489 ** If an error occurs (e.g. an OOM or IO error), return an SQLite error 
 130490 ** code. The final value of *pbOk is undefined in this case.
 130492 static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
 130493   int rc = SQLITE_OK;             /* Return code */
 130494   u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
 130495   u64 cksum2 = 0;                 /* Checksum based on %_content contents */
 130496   sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
 130498   /* This block calculates the checksum according to the FTS index. */
 130499   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
 130500   if( rc==SQLITE_OK ){
 130501     int rc2;
 130502     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
 130503     while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
 130504       int iLangid = sqlite3_column_int(pAllLangid, 0);
 130505       int i;
 130506       for(i=0; i<p->nIndex; i++){
 130507         cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
 130510     rc2 = sqlite3_reset(pAllLangid);
 130511     if( rc==SQLITE_OK ) rc = rc2;
 130514   /* This block calculates the checksum according to the %_content table */
 130515   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
 130516   if( rc==SQLITE_OK ){
 130517     sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
 130518     sqlite3_stmt *pStmt = 0;
 130519     char *zSql;
 130521     zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
 130522     if( !zSql ){
 130523       rc = SQLITE_NOMEM;
 130524     }else{
 130525       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 130526       sqlite3_free(zSql);
 130529     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
 130530       i64 iDocid = sqlite3_column_int64(pStmt, 0);
 130531       int iLang = langidFromSelect(p, pStmt);
 130532       int iCol;
 130534       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
 130535         const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
 130536         int nText = sqlite3_column_bytes(pStmt, iCol+1);
 130537         sqlite3_tokenizer_cursor *pT = 0;
 130539         rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
 130540         while( rc==SQLITE_OK ){
 130541           char const *zToken;       /* Buffer containing token */
 130542           int nToken = 0;           /* Number of bytes in token */
 130543           int iDum1 = 0, iDum2 = 0; /* Dummy variables */
 130544           int iPos = 0;             /* Position of token in zText */
 130546           rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
 130547           if( rc==SQLITE_OK ){
 130548             int i;
 130549             cksum2 = cksum2 ^ fts3ChecksumEntry(
 130550                 zToken, nToken, iLang, 0, iDocid, iCol, iPos
 130552             for(i=1; i<p->nIndex; i++){
 130553               if( p->aIndex[i].nPrefix<=nToken ){
 130554                 cksum2 = cksum2 ^ fts3ChecksumEntry(
 130555                   zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
 130561         if( pT ) pModule->xClose(pT);
 130562         if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 130566     sqlite3_finalize(pStmt);
 130569   *pbOk = (cksum1==cksum2);
 130570   return rc;
 130574 ** Run the integrity-check. If no error occurs and the current contents of
 130575 ** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
 130576 ** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
 130578 ** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite 
 130579 ** error code.
 130581 ** The integrity-check works as follows. For each token and indexed token
 130582 ** prefix in the document set, a 64-bit checksum is calculated (by code
 130583 ** in fts3ChecksumEntry()) based on the following:
 130585 **     + The index number (0 for the main index, 1 for the first prefix
 130586 **       index etc.),
 130587 **     + The token (or token prefix) text itself, 
 130588 **     + The language-id of the row it appears in,
 130589 **     + The docid of the row it appears in,
 130590 **     + The column it appears in, and
 130591 **     + The tokens position within that column.
 130593 ** The checksums for all entries in the index are XORed together to create
 130594 ** a single checksum for the entire index.
 130596 ** The integrity-check code calculates the same checksum in two ways:
 130598 **     1. By scanning the contents of the FTS index, and 
 130599 **     2. By scanning and tokenizing the content table.
 130601 ** If the two checksums are identical, the integrity-check is deemed to have
 130602 ** passed.
 130604 static int fts3DoIntegrityCheck(
 130605   Fts3Table *p                    /* FTS3 table handle */
 130607   int rc;
 130608   int bOk = 0;
 130609   rc = fts3IntegrityCheck(p, &bOk);
 130610   if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
 130611   return rc;
 130615 ** Handle a 'special' INSERT of the form:
 130617 **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
 130619 ** Argument pVal contains the result of <expr>. Currently the only 
 130620 ** meaningful value to insert is the text 'optimize'.
 130622 static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
 130623   int rc;                         /* Return Code */
 130624   const char *zVal = (const char *)sqlite3_value_text(pVal);
 130625   int nVal = sqlite3_value_bytes(pVal);
 130627   if( !zVal ){
 130628     return SQLITE_NOMEM;
 130629   }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
 130630     rc = fts3DoOptimize(p, 0);
 130631   }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
 130632     rc = fts3DoRebuild(p);
 130633   }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
 130634     rc = fts3DoIntegrityCheck(p);
 130635   }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
 130636     rc = fts3DoIncrmerge(p, &zVal[6]);
 130637   }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
 130638     rc = fts3DoAutoincrmerge(p, &zVal[10]);
 130639 #ifdef SQLITE_TEST
 130640   }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
 130641     p->nNodeSize = atoi(&zVal[9]);
 130642     rc = SQLITE_OK;
 130643   }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
 130644     p->nMaxPendingData = atoi(&zVal[11]);
 130645     rc = SQLITE_OK;
 130646 #endif
 130647   }else{
 130648     rc = SQLITE_ERROR;
 130651   return rc;
 130654 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 130656 ** Delete all cached deferred doclists. Deferred doclists are cached
 130657 ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
 130659 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
 130660   Fts3DeferredToken *pDef;
 130661   for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
 130662     fts3PendingListDelete(pDef->pList);
 130663     pDef->pList = 0;
 130668 ** Free all entries in the pCsr->pDeffered list. Entries are added to 
 130669 ** this list using sqlite3Fts3DeferToken().
 130671 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
 130672   Fts3DeferredToken *pDef;
 130673   Fts3DeferredToken *pNext;
 130674   for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
 130675     pNext = pDef->pNext;
 130676     fts3PendingListDelete(pDef->pList);
 130677     sqlite3_free(pDef);
 130679   pCsr->pDeferred = 0;
 130683 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
 130684 ** based on the row that pCsr currently points to.
 130686 ** A deferred-doclist is like any other doclist with position information
 130687 ** included, except that it only contains entries for a single row of the
 130688 ** table, not for all rows.
 130690 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
 130691   int rc = SQLITE_OK;             /* Return code */
 130692   if( pCsr->pDeferred ){
 130693     int i;                        /* Used to iterate through table columns */
 130694     sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
 130695     Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
 130697     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
 130698     sqlite3_tokenizer *pT = p->pTokenizer;
 130699     sqlite3_tokenizer_module const *pModule = pT->pModule;
 130701     assert( pCsr->isRequireSeek==0 );
 130702     iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
 130704     for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
 130705       const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
 130706       sqlite3_tokenizer_cursor *pTC = 0;
 130708       rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
 130709       while( rc==SQLITE_OK ){
 130710         char const *zToken;       /* Buffer containing token */
 130711         int nToken = 0;           /* Number of bytes in token */
 130712         int iDum1 = 0, iDum2 = 0; /* Dummy variables */
 130713         int iPos = 0;             /* Position of token in zText */
 130715         rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
 130716         for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
 130717           Fts3PhraseToken *pPT = pDef->pToken;
 130718           if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
 130719            && (pPT->bFirst==0 || iPos==0)
 130720            && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
 130721            && (0==memcmp(zToken, pPT->z, pPT->n))
 130723             fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
 130727       if( pTC ) pModule->xClose(pTC);
 130728       if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 130731     for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
 130732       if( pDef->pList ){
 130733         rc = fts3PendingListAppendVarint(&pDef->pList, 0);
 130738   return rc;
 130741 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
 130742   Fts3DeferredToken *p, 
 130743   char **ppData, 
 130744   int *pnData
 130746   char *pRet;
 130747   int nSkip;
 130748   sqlite3_int64 dummy;
 130750   *ppData = 0;
 130751   *pnData = 0;
 130753   if( p->pList==0 ){
 130754     return SQLITE_OK;
 130757   pRet = (char *)sqlite3_malloc(p->pList->nData);
 130758   if( !pRet ) return SQLITE_NOMEM;
 130760   nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
 130761   *pnData = p->pList->nData - nSkip;
 130762   *ppData = pRet;
 130764   memcpy(pRet, &p->pList->aData[nSkip], *pnData);
 130765   return SQLITE_OK;
 130769 ** Add an entry for token pToken to the pCsr->pDeferred list.
 130771 SQLITE_PRIVATE int sqlite3Fts3DeferToken(
 130772   Fts3Cursor *pCsr,               /* Fts3 table cursor */
 130773   Fts3PhraseToken *pToken,        /* Token to defer */
 130774   int iCol                        /* Column that token must appear in (or -1) */
 130776   Fts3DeferredToken *pDeferred;
 130777   pDeferred = sqlite3_malloc(sizeof(*pDeferred));
 130778   if( !pDeferred ){
 130779     return SQLITE_NOMEM;
 130781   memset(pDeferred, 0, sizeof(*pDeferred));
 130782   pDeferred->pToken = pToken;
 130783   pDeferred->pNext = pCsr->pDeferred; 
 130784   pDeferred->iCol = iCol;
 130785   pCsr->pDeferred = pDeferred;
 130787   assert( pToken->pDeferred==0 );
 130788   pToken->pDeferred = pDeferred;
 130790   return SQLITE_OK;
 130792 #endif
 130795 ** SQLite value pRowid contains the rowid of a row that may or may not be
 130796 ** present in the FTS3 table. If it is, delete it and adjust the contents
 130797 ** of subsiduary data structures accordingly.
 130799 static int fts3DeleteByRowid(
 130800   Fts3Table *p, 
 130801   sqlite3_value *pRowid, 
 130802   int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
 130803   u32 *aSzDel
 130805   int rc = SQLITE_OK;             /* Return code */
 130806   int bFound = 0;                 /* True if *pRowid really is in the table */
 130808   fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
 130809   if( bFound && rc==SQLITE_OK ){
 130810     int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
 130811     rc = fts3IsEmpty(p, pRowid, &isEmpty);
 130812     if( rc==SQLITE_OK ){
 130813       if( isEmpty ){
 130814         /* Deleting this row means the whole table is empty. In this case
 130815         ** delete the contents of all three tables and throw away any
 130816         ** data in the pendingTerms hash table.  */
 130817         rc = fts3DeleteAll(p, 1);
 130818         *pnChng = 0;
 130819         memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
 130820       }else{
 130821         *pnChng = *pnChng - 1;
 130822         if( p->zContentTbl==0 ){
 130823           fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
 130825         if( p->bHasDocsize ){
 130826           fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
 130832   return rc;
 130836 ** This function does the work for the xUpdate method of FTS3 virtual
 130837 ** tables. The schema of the virtual table being:
 130839 **     CREATE TABLE <table name>( 
 130840 **       <user columns>,
 130841 **       <table name> HIDDEN, 
 130842 **       docid HIDDEN, 
 130843 **       <langid> HIDDEN
 130844 **     );
 130848 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
 130849   sqlite3_vtab *pVtab,            /* FTS3 vtab object */
 130850   int nArg,                       /* Size of argument array */
 130851   sqlite3_value **apVal,          /* Array of arguments */
 130852   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
 130854   Fts3Table *p = (Fts3Table *)pVtab;
 130855   int rc = SQLITE_OK;             /* Return Code */
 130856   int isRemove = 0;               /* True for an UPDATE or DELETE */
 130857   u32 *aSzIns = 0;                /* Sizes of inserted documents */
 130858   u32 *aSzDel = 0;                /* Sizes of deleted documents */
 130859   int nChng = 0;                  /* Net change in number of documents */
 130860   int bInsertDone = 0;
 130862   assert( p->pSegments==0 );
 130863   assert( 
 130864       nArg==1                     /* DELETE operations */
 130865    || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
 130868   /* Check for a "special" INSERT operation. One of the form:
 130870   **   INSERT INTO xyz(xyz) VALUES('command');
 130872   if( nArg>1 
 130873    && sqlite3_value_type(apVal[0])==SQLITE_NULL 
 130874    && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL 
 130876     rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
 130877     goto update_out;
 130880   if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
 130881     rc = SQLITE_CONSTRAINT;
 130882     goto update_out;
 130885   /* Allocate space to hold the change in document sizes */
 130886   aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
 130887   if( aSzDel==0 ){
 130888     rc = SQLITE_NOMEM;
 130889     goto update_out;
 130891   aSzIns = &aSzDel[p->nColumn+1];
 130892   memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
 130894   /* If this is an INSERT operation, or an UPDATE that modifies the rowid
 130895   ** value, then this operation requires constraint handling.
 130897   ** If the on-conflict mode is REPLACE, this means that the existing row
 130898   ** should be deleted from the database before inserting the new row. Or,
 130899   ** if the on-conflict mode is other than REPLACE, then this method must
 130900   ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
 130901   ** modify the database file.
 130903   if( nArg>1 && p->zContentTbl==0 ){
 130904     /* Find the value object that holds the new rowid value. */
 130905     sqlite3_value *pNewRowid = apVal[3+p->nColumn];
 130906     if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
 130907       pNewRowid = apVal[1];
 130910     if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && ( 
 130911         sqlite3_value_type(apVal[0])==SQLITE_NULL
 130912      || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
 130913     )){
 130914       /* The new rowid is not NULL (in this case the rowid will be
 130915       ** automatically assigned and there is no chance of a conflict), and 
 130916       ** the statement is either an INSERT or an UPDATE that modifies the
 130917       ** rowid column. So if the conflict mode is REPLACE, then delete any
 130918       ** existing row with rowid=pNewRowid. 
 130920       ** Or, if the conflict mode is not REPLACE, insert the new record into 
 130921       ** the %_content table. If we hit the duplicate rowid constraint (or any
 130922       ** other error) while doing so, return immediately.
 130924       ** This branch may also run if pNewRowid contains a value that cannot
 130925       ** be losslessly converted to an integer. In this case, the eventual 
 130926       ** call to fts3InsertData() (either just below or further on in this
 130927       ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is 
 130928       ** invoked, it will delete zero rows (since no row will have
 130929       ** docid=$pNewRowid if $pNewRowid is not an integer value).
 130931       if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
 130932         rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
 130933       }else{
 130934         rc = fts3InsertData(p, apVal, pRowid);
 130935         bInsertDone = 1;
 130939   if( rc!=SQLITE_OK ){
 130940     goto update_out;
 130943   /* If this is a DELETE or UPDATE operation, remove the old record. */
 130944   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
 130945     assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
 130946     rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
 130947     isRemove = 1;
 130950   /* If this is an INSERT or UPDATE operation, insert the new record. */
 130951   if( nArg>1 && rc==SQLITE_OK ){
 130952     int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
 130953     if( bInsertDone==0 ){
 130954       rc = fts3InsertData(p, apVal, pRowid);
 130955       if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
 130956         rc = FTS_CORRUPT_VTAB;
 130959     if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
 130960       rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
 130962     if( rc==SQLITE_OK ){
 130963       assert( p->iPrevDocid==*pRowid );
 130964       rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
 130966     if( p->bHasDocsize ){
 130967       fts3InsertDocsize(&rc, p, aSzIns);
 130969     nChng++;
 130972   if( p->bFts4 ){
 130973     fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
 130976  update_out:
 130977   sqlite3_free(aSzDel);
 130978   sqlite3Fts3SegmentsClose(p);
 130979   return rc;
 130983 ** Flush any data in the pending-terms hash table to disk. If successful,
 130984 ** merge all segments in the database (including the new segment, if 
 130985 ** there was any data to flush) into a single segment. 
 130987 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
 130988   int rc;
 130989   rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
 130990   if( rc==SQLITE_OK ){
 130991     rc = fts3DoOptimize(p, 1);
 130992     if( rc==SQLITE_OK || rc==SQLITE_DONE ){
 130993       int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
 130994       if( rc2!=SQLITE_OK ) rc = rc2;
 130995     }else{
 130996       sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
 130997       sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
 131000   sqlite3Fts3SegmentsClose(p);
 131001   return rc;
 131004 #endif
 131006 /************** End of fts3_write.c ******************************************/
 131007 /************** Begin file fts3_snippet.c ************************************/
 131009 ** 2009 Oct 23
 131011 ** The author disclaims copyright to this source code.  In place of
 131012 ** a legal notice, here is a blessing:
 131014 **    May you do good and not evil.
 131015 **    May you find forgiveness for yourself and forgive others.
 131016 **    May you share freely, never taking more than you give.
 131018 ******************************************************************************
 131021 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 131023 /* #include <string.h> */
 131024 /* #include <assert.h> */
 131027 ** Characters that may appear in the second argument to matchinfo().
 131029 #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
 131030 #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
 131031 #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
 131032 #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
 131033 #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
 131034 #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
 131035 #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
 131038 ** The default value for the second argument to matchinfo(). 
 131040 #define FTS3_MATCHINFO_DEFAULT   "pcx"
 131044 ** Used as an fts3ExprIterate() context when loading phrase doclists to
 131045 ** Fts3Expr.aDoclist[]/nDoclist.
 131047 typedef struct LoadDoclistCtx LoadDoclistCtx;
 131048 struct LoadDoclistCtx {
 131049   Fts3Cursor *pCsr;               /* FTS3 Cursor */
 131050   int nPhrase;                    /* Number of phrases seen so far */
 131051   int nToken;                     /* Number of tokens seen so far */
 131055 ** The following types are used as part of the implementation of the 
 131056 ** fts3BestSnippet() routine.
 131058 typedef struct SnippetIter SnippetIter;
 131059 typedef struct SnippetPhrase SnippetPhrase;
 131060 typedef struct SnippetFragment SnippetFragment;
 131062 struct SnippetIter {
 131063   Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
 131064   int iCol;                       /* Extract snippet from this column */
 131065   int nSnippet;                   /* Requested snippet length (in tokens) */
 131066   int nPhrase;                    /* Number of phrases in query */
 131067   SnippetPhrase *aPhrase;         /* Array of size nPhrase */
 131068   int iCurrent;                   /* First token of current snippet */
 131071 struct SnippetPhrase {
 131072   int nToken;                     /* Number of tokens in phrase */
 131073   char *pList;                    /* Pointer to start of phrase position list */
 131074   int iHead;                      /* Next value in position list */
 131075   char *pHead;                    /* Position list data following iHead */
 131076   int iTail;                      /* Next value in trailing position list */
 131077   char *pTail;                    /* Position list data following iTail */
 131080 struct SnippetFragment {
 131081   int iCol;                       /* Column snippet is extracted from */
 131082   int iPos;                       /* Index of first token in snippet */
 131083   u64 covered;                    /* Mask of query phrases covered */
 131084   u64 hlmask;                     /* Mask of snippet terms to highlight */
 131088 ** This type is used as an fts3ExprIterate() context object while 
 131089 ** accumulating the data returned by the matchinfo() function.
 131091 typedef struct MatchInfo MatchInfo;
 131092 struct MatchInfo {
 131093   Fts3Cursor *pCursor;            /* FTS3 Cursor */
 131094   int nCol;                       /* Number of columns in table */
 131095   int nPhrase;                    /* Number of matchable phrases in query */
 131096   sqlite3_int64 nDoc;             /* Number of docs in database */
 131097   u32 *aMatchinfo;                /* Pre-allocated buffer */
 131103 ** The snippet() and offsets() functions both return text values. An instance
 131104 ** of the following structure is used to accumulate those values while the
 131105 ** functions are running. See fts3StringAppend() for details.
 131107 typedef struct StrBuffer StrBuffer;
 131108 struct StrBuffer {
 131109   char *z;                        /* Pointer to buffer containing string */
 131110   int n;                          /* Length of z in bytes (excl. nul-term) */
 131111   int nAlloc;                     /* Allocated size of buffer z in bytes */
 131116 ** This function is used to help iterate through a position-list. A position
 131117 ** list is a list of unique integers, sorted from smallest to largest. Each
 131118 ** element of the list is represented by an FTS3 varint that takes the value
 131119 ** of the difference between the current element and the previous one plus
 131120 ** two. For example, to store the position-list:
 131122 **     4 9 113
 131124 ** the three varints:
 131126 **     6 7 106
 131128 ** are encoded.
 131130 ** When this function is called, *pp points to the start of an element of
 131131 ** the list. *piPos contains the value of the previous entry in the list.
 131132 ** After it returns, *piPos contains the value of the next element of the
 131133 ** list and *pp is advanced to the following varint.
 131135 static void fts3GetDeltaPosition(char **pp, int *piPos){
 131136   int iVal;
 131137   *pp += sqlite3Fts3GetVarint32(*pp, &iVal);
 131138   *piPos += (iVal-2);
 131142 ** Helper function for fts3ExprIterate() (see below).
 131144 static int fts3ExprIterate2(
 131145   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
 131146   int *piPhrase,                  /* Pointer to phrase counter */
 131147   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
 131148   void *pCtx                      /* Second argument to pass to callback */
 131150   int rc;                         /* Return code */
 131151   int eType = pExpr->eType;       /* Type of expression node pExpr */
 131153   if( eType!=FTSQUERY_PHRASE ){
 131154     assert( pExpr->pLeft && pExpr->pRight );
 131155     rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
 131156     if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
 131157       rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
 131159   }else{
 131160     rc = x(pExpr, *piPhrase, pCtx);
 131161     (*piPhrase)++;
 131163   return rc;
 131167 ** Iterate through all phrase nodes in an FTS3 query, except those that
 131168 ** are part of a sub-tree that is the right-hand-side of a NOT operator.
 131169 ** For each phrase node found, the supplied callback function is invoked.
 131171 ** If the callback function returns anything other than SQLITE_OK, 
 131172 ** the iteration is abandoned and the error code returned immediately.
 131173 ** Otherwise, SQLITE_OK is returned after a callback has been made for
 131174 ** all eligible phrase nodes.
 131176 static int fts3ExprIterate(
 131177   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
 131178   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
 131179   void *pCtx                      /* Second argument to pass to callback */
 131181   int iPhrase = 0;                /* Variable used as the phrase counter */
 131182   return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
 131186 ** This is an fts3ExprIterate() callback used while loading the doclists
 131187 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
 131188 ** fts3ExprLoadDoclists().
 131190 static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
 131191   int rc = SQLITE_OK;
 131192   Fts3Phrase *pPhrase = pExpr->pPhrase;
 131193   LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
 131195   UNUSED_PARAMETER(iPhrase);
 131197   p->nPhrase++;
 131198   p->nToken += pPhrase->nToken;
 131200   return rc;
 131204 ** Load the doclists for each phrase in the query associated with FTS3 cursor
 131205 ** pCsr. 
 131207 ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable 
 131208 ** phrases in the expression (all phrases except those directly or 
 131209 ** indirectly descended from the right-hand-side of a NOT operator). If 
 131210 ** pnToken is not NULL, then it is set to the number of tokens in all
 131211 ** matchable phrases of the expression.
 131213 static int fts3ExprLoadDoclists(
 131214   Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
 131215   int *pnPhrase,                  /* OUT: Number of phrases in query */
 131216   int *pnToken                    /* OUT: Number of tokens in query */
 131218   int rc;                         /* Return Code */
 131219   LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
 131220   sCtx.pCsr = pCsr;
 131221   rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
 131222   if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
 131223   if( pnToken ) *pnToken = sCtx.nToken;
 131224   return rc;
 131227 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
 131228   (*(int *)ctx)++;
 131229   UNUSED_PARAMETER(pExpr);
 131230   UNUSED_PARAMETER(iPhrase);
 131231   return SQLITE_OK;
 131233 static int fts3ExprPhraseCount(Fts3Expr *pExpr){
 131234   int nPhrase = 0;
 131235   (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
 131236   return nPhrase;
 131240 ** Advance the position list iterator specified by the first two 
 131241 ** arguments so that it points to the first element with a value greater
 131242 ** than or equal to parameter iNext.
 131244 static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
 131245   char *pIter = *ppIter;
 131246   if( pIter ){
 131247     int iIter = *piIter;
 131249     while( iIter<iNext ){
 131250       if( 0==(*pIter & 0xFE) ){
 131251         iIter = -1;
 131252         pIter = 0;
 131253         break;
 131255       fts3GetDeltaPosition(&pIter, &iIter);
 131258     *piIter = iIter;
 131259     *ppIter = pIter;
 131264 ** Advance the snippet iterator to the next candidate snippet.
 131266 static int fts3SnippetNextCandidate(SnippetIter *pIter){
 131267   int i;                          /* Loop counter */
 131269   if( pIter->iCurrent<0 ){
 131270     /* The SnippetIter object has just been initialized. The first snippet
 131271     ** candidate always starts at offset 0 (even if this candidate has a
 131272     ** score of 0.0).
 131274     pIter->iCurrent = 0;
 131276     /* Advance the 'head' iterator of each phrase to the first offset that
 131277     ** is greater than or equal to (iNext+nSnippet).
 131279     for(i=0; i<pIter->nPhrase; i++){
 131280       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 131281       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
 131283   }else{
 131284     int iStart;
 131285     int iEnd = 0x7FFFFFFF;
 131287     for(i=0; i<pIter->nPhrase; i++){
 131288       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 131289       if( pPhrase->pHead && pPhrase->iHead<iEnd ){
 131290         iEnd = pPhrase->iHead;
 131293     if( iEnd==0x7FFFFFFF ){
 131294       return 1;
 131297     pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
 131298     for(i=0; i<pIter->nPhrase; i++){
 131299       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 131300       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
 131301       fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
 131305   return 0;
 131309 ** Retrieve information about the current candidate snippet of snippet 
 131310 ** iterator pIter.
 131312 static void fts3SnippetDetails(
 131313   SnippetIter *pIter,             /* Snippet iterator */
 131314   u64 mCovered,                   /* Bitmask of phrases already covered */
 131315   int *piToken,                   /* OUT: First token of proposed snippet */
 131316   int *piScore,                   /* OUT: "Score" for this snippet */
 131317   u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
 131318   u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
 131320   int iStart = pIter->iCurrent;   /* First token of snippet */
 131321   int iScore = 0;                 /* Score of this snippet */
 131322   int i;                          /* Loop counter */
 131323   u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
 131324   u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
 131326   for(i=0; i<pIter->nPhrase; i++){
 131327     SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 131328     if( pPhrase->pTail ){
 131329       char *pCsr = pPhrase->pTail;
 131330       int iCsr = pPhrase->iTail;
 131332       while( iCsr<(iStart+pIter->nSnippet) ){
 131333         int j;
 131334         u64 mPhrase = (u64)1 << i;
 131335         u64 mPos = (u64)1 << (iCsr - iStart);
 131336         assert( iCsr>=iStart );
 131337         if( (mCover|mCovered)&mPhrase ){
 131338           iScore++;
 131339         }else{
 131340           iScore += 1000;
 131342         mCover |= mPhrase;
 131344         for(j=0; j<pPhrase->nToken; j++){
 131345           mHighlight |= (mPos>>j);
 131348         if( 0==(*pCsr & 0x0FE) ) break;
 131349         fts3GetDeltaPosition(&pCsr, &iCsr);
 131354   /* Set the output variables before returning. */
 131355   *piToken = iStart;
 131356   *piScore = iScore;
 131357   *pmCover = mCover;
 131358   *pmHighlight = mHighlight;
 131362 ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
 131363 ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
 131365 static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
 131366   SnippetIter *p = (SnippetIter *)ctx;
 131367   SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
 131368   char *pCsr;
 131369   int rc;
 131371   pPhrase->nToken = pExpr->pPhrase->nToken;
 131372   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
 131373   assert( rc==SQLITE_OK || pCsr==0 );
 131374   if( pCsr ){
 131375     int iFirst = 0;
 131376     pPhrase->pList = pCsr;
 131377     fts3GetDeltaPosition(&pCsr, &iFirst);
 131378     assert( iFirst>=0 );
 131379     pPhrase->pHead = pCsr;
 131380     pPhrase->pTail = pCsr;
 131381     pPhrase->iHead = iFirst;
 131382     pPhrase->iTail = iFirst;
 131383   }else{
 131384     assert( rc!=SQLITE_OK || (
 131385        pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 
 131386     ));
 131389   return rc;
 131393 ** Select the fragment of text consisting of nFragment contiguous tokens 
 131394 ** from column iCol that represent the "best" snippet. The best snippet
 131395 ** is the snippet with the highest score, where scores are calculated
 131396 ** by adding:
 131398 **   (a) +1 point for each occurence of a matchable phrase in the snippet.
 131400 **   (b) +1000 points for the first occurence of each matchable phrase in 
 131401 **       the snippet for which the corresponding mCovered bit is not set.
 131403 ** The selected snippet parameters are stored in structure *pFragment before
 131404 ** returning. The score of the selected snippet is stored in *piScore
 131405 ** before returning.
 131407 static int fts3BestSnippet(
 131408   int nSnippet,                   /* Desired snippet length */
 131409   Fts3Cursor *pCsr,               /* Cursor to create snippet for */
 131410   int iCol,                       /* Index of column to create snippet from */
 131411   u64 mCovered,                   /* Mask of phrases already covered */
 131412   u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
 131413   SnippetFragment *pFragment,     /* OUT: Best snippet found */
 131414   int *piScore                    /* OUT: Score of snippet pFragment */
 131416   int rc;                         /* Return Code */
 131417   int nList;                      /* Number of phrases in expression */
 131418   SnippetIter sIter;              /* Iterates through snippet candidates */
 131419   int nByte;                      /* Number of bytes of space to allocate */
 131420   int iBestScore = -1;            /* Best snippet score found so far */
 131421   int i;                          /* Loop counter */
 131423   memset(&sIter, 0, sizeof(sIter));
 131425   /* Iterate through the phrases in the expression to count them. The same
 131426   ** callback makes sure the doclists are loaded for each phrase.
 131428   rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
 131429   if( rc!=SQLITE_OK ){
 131430     return rc;
 131433   /* Now that it is known how many phrases there are, allocate and zero
 131434   ** the required space using malloc().
 131436   nByte = sizeof(SnippetPhrase) * nList;
 131437   sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
 131438   if( !sIter.aPhrase ){
 131439     return SQLITE_NOMEM;
 131441   memset(sIter.aPhrase, 0, nByte);
 131443   /* Initialize the contents of the SnippetIter object. Then iterate through
 131444   ** the set of phrases in the expression to populate the aPhrase[] array.
 131446   sIter.pCsr = pCsr;
 131447   sIter.iCol = iCol;
 131448   sIter.nSnippet = nSnippet;
 131449   sIter.nPhrase = nList;
 131450   sIter.iCurrent = -1;
 131451   (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
 131453   /* Set the *pmSeen output variable. */
 131454   for(i=0; i<nList; i++){
 131455     if( sIter.aPhrase[i].pHead ){
 131456       *pmSeen |= (u64)1 << i;
 131460   /* Loop through all candidate snippets. Store the best snippet in 
 131461   ** *pFragment. Store its associated 'score' in iBestScore.
 131463   pFragment->iCol = iCol;
 131464   while( !fts3SnippetNextCandidate(&sIter) ){
 131465     int iPos;
 131466     int iScore;
 131467     u64 mCover;
 131468     u64 mHighlight;
 131469     fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
 131470     assert( iScore>=0 );
 131471     if( iScore>iBestScore ){
 131472       pFragment->iPos = iPos;
 131473       pFragment->hlmask = mHighlight;
 131474       pFragment->covered = mCover;
 131475       iBestScore = iScore;
 131479   sqlite3_free(sIter.aPhrase);
 131480   *piScore = iBestScore;
 131481   return SQLITE_OK;
 131486 ** Append a string to the string-buffer passed as the first argument.
 131488 ** If nAppend is negative, then the length of the string zAppend is
 131489 ** determined using strlen().
 131491 static int fts3StringAppend(
 131492   StrBuffer *pStr,                /* Buffer to append to */
 131493   const char *zAppend,            /* Pointer to data to append to buffer */
 131494   int nAppend                     /* Size of zAppend in bytes (or -1) */
 131496   if( nAppend<0 ){
 131497     nAppend = (int)strlen(zAppend);
 131500   /* If there is insufficient space allocated at StrBuffer.z, use realloc()
 131501   ** to grow the buffer until so that it is big enough to accomadate the
 131502   ** appended data.
 131504   if( pStr->n+nAppend+1>=pStr->nAlloc ){
 131505     int nAlloc = pStr->nAlloc+nAppend+100;
 131506     char *zNew = sqlite3_realloc(pStr->z, nAlloc);
 131507     if( !zNew ){
 131508       return SQLITE_NOMEM;
 131510     pStr->z = zNew;
 131511     pStr->nAlloc = nAlloc;
 131514   /* Append the data to the string buffer. */
 131515   memcpy(&pStr->z[pStr->n], zAppend, nAppend);
 131516   pStr->n += nAppend;
 131517   pStr->z[pStr->n] = '\0';
 131519   return SQLITE_OK;
 131523 ** The fts3BestSnippet() function often selects snippets that end with a
 131524 ** query term. That is, the final term of the snippet is always a term
 131525 ** that requires highlighting. For example, if 'X' is a highlighted term
 131526 ** and '.' is a non-highlighted term, BestSnippet() may select:
 131528 **     ........X.....X
 131530 ** This function "shifts" the beginning of the snippet forward in the 
 131531 ** document so that there are approximately the same number of 
 131532 ** non-highlighted terms to the right of the final highlighted term as there
 131533 ** are to the left of the first highlighted term. For example, to this:
 131535 **     ....X.....X....
 131537 ** This is done as part of extracting the snippet text, not when selecting
 131538 ** the snippet. Snippet selection is done based on doclists only, so there
 131539 ** is no way for fts3BestSnippet() to know whether or not the document 
 131540 ** actually contains terms that follow the final highlighted term. 
 131542 static int fts3SnippetShift(
 131543   Fts3Table *pTab,                /* FTS3 table snippet comes from */
 131544   int iLangid,                    /* Language id to use in tokenizing */
 131545   int nSnippet,                   /* Number of tokens desired for snippet */
 131546   const char *zDoc,               /* Document text to extract snippet from */
 131547   int nDoc,                       /* Size of buffer zDoc in bytes */
 131548   int *piPos,                     /* IN/OUT: First token of snippet */
 131549   u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
 131551   u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
 131553   if( hlmask ){
 131554     int nLeft;                    /* Tokens to the left of first highlight */
 131555     int nRight;                   /* Tokens to the right of last highlight */
 131556     int nDesired;                 /* Ideal number of tokens to shift forward */
 131558     for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
 131559     for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
 131560     nDesired = (nLeft-nRight)/2;
 131562     /* Ideally, the start of the snippet should be pushed forward in the
 131563     ** document nDesired tokens. This block checks if there are actually
 131564     ** nDesired tokens to the right of the snippet. If so, *piPos and
 131565     ** *pHlMask are updated to shift the snippet nDesired tokens to the
 131566     ** right. Otherwise, the snippet is shifted by the number of tokens
 131567     ** available.
 131569     if( nDesired>0 ){
 131570       int nShift;                 /* Number of tokens to shift snippet by */
 131571       int iCurrent = 0;           /* Token counter */
 131572       int rc;                     /* Return Code */
 131573       sqlite3_tokenizer_module *pMod;
 131574       sqlite3_tokenizer_cursor *pC;
 131575       pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
 131577       /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
 131578       ** or more tokens in zDoc/nDoc.
 131580       rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
 131581       if( rc!=SQLITE_OK ){
 131582         return rc;
 131584       while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
 131585         const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
 131586         rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
 131588       pMod->xClose(pC);
 131589       if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
 131591       nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
 131592       assert( nShift<=nDesired );
 131593       if( nShift>0 ){
 131594         *piPos += nShift;
 131595         *pHlmask = hlmask >> nShift;
 131599   return SQLITE_OK;
 131603 ** Extract the snippet text for fragment pFragment from cursor pCsr and
 131604 ** append it to string buffer pOut.
 131606 static int fts3SnippetText(
 131607   Fts3Cursor *pCsr,               /* FTS3 Cursor */
 131608   SnippetFragment *pFragment,     /* Snippet to extract */
 131609   int iFragment,                  /* Fragment number */
 131610   int isLast,                     /* True for final fragment in snippet */
 131611   int nSnippet,                   /* Number of tokens in extracted snippet */
 131612   const char *zOpen,              /* String inserted before highlighted term */
 131613   const char *zClose,             /* String inserted after highlighted term */
 131614   const char *zEllipsis,          /* String inserted between snippets */
 131615   StrBuffer *pOut                 /* Write output here */
 131617   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 131618   int rc;                         /* Return code */
 131619   const char *zDoc;               /* Document text to extract snippet from */
 131620   int nDoc;                       /* Size of zDoc in bytes */
 131621   int iCurrent = 0;               /* Current token number of document */
 131622   int iEnd = 0;                   /* Byte offset of end of current token */
 131623   int isShiftDone = 0;            /* True after snippet is shifted */
 131624   int iPos = pFragment->iPos;     /* First token of snippet */
 131625   u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
 131626   int iCol = pFragment->iCol+1;   /* Query column to extract text from */
 131627   sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
 131628   sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
 131630   zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
 131631   if( zDoc==0 ){
 131632     if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
 131633       return SQLITE_NOMEM;
 131635     return SQLITE_OK;
 131637   nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
 131639   /* Open a token cursor on the document. */
 131640   pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
 131641   rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
 131642   if( rc!=SQLITE_OK ){
 131643     return rc;
 131646   while( rc==SQLITE_OK ){
 131647     const char *ZDUMMY;           /* Dummy argument used with tokenizer */
 131648     int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
 131649     int iBegin = 0;               /* Offset in zDoc of start of token */
 131650     int iFin = 0;                 /* Offset in zDoc of end of token */
 131651     int isHighlight = 0;          /* True for highlighted terms */
 131653     /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
 131654     ** in the FTS code the variable that the third argument to xNext points to
 131655     ** is initialized to zero before the first (*but not necessarily
 131656     ** subsequent*) call to xNext(). This is done for a particular application
 131657     ** that needs to know whether or not the tokenizer is being used for
 131658     ** snippet generation or for some other purpose.
 131660     ** Extreme care is required when writing code to depend on this
 131661     ** initialization. It is not a documented part of the tokenizer interface.
 131662     ** If a tokenizer is used directly by any code outside of FTS, this
 131663     ** convention might not be respected.  */
 131664     rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
 131665     if( rc!=SQLITE_OK ){
 131666       if( rc==SQLITE_DONE ){
 131667         /* Special case - the last token of the snippet is also the last token
 131668         ** of the column. Append any punctuation that occurred between the end
 131669         ** of the previous token and the end of the document to the output. 
 131670         ** Then break out of the loop. */
 131671         rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
 131673       break;
 131675     if( iCurrent<iPos ){ continue; }
 131677     if( !isShiftDone ){
 131678       int n = nDoc - iBegin;
 131679       rc = fts3SnippetShift(
 131680           pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
 131682       isShiftDone = 1;
 131684       /* Now that the shift has been done, check if the initial "..." are
 131685       ** required. They are required if (a) this is not the first fragment,
 131686       ** or (b) this fragment does not begin at position 0 of its column. 
 131688       if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
 131689         rc = fts3StringAppend(pOut, zEllipsis, -1);
 131691       if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
 131694     if( iCurrent>=(iPos+nSnippet) ){
 131695       if( isLast ){
 131696         rc = fts3StringAppend(pOut, zEllipsis, -1);
 131698       break;
 131701     /* Set isHighlight to true if this term should be highlighted. */
 131702     isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
 131704     if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
 131705     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
 131706     if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
 131707     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
 131709     iEnd = iFin;
 131712   pMod->xClose(pC);
 131713   return rc;
 131718 ** This function is used to count the entries in a column-list (a 
 131719 ** delta-encoded list of term offsets within a single column of a single 
 131720 ** row). When this function is called, *ppCollist should point to the
 131721 ** beginning of the first varint in the column-list (the varint that
 131722 ** contains the position of the first matching term in the column data).
 131723 ** Before returning, *ppCollist is set to point to the first byte after
 131724 ** the last varint in the column-list (either the 0x00 signifying the end
 131725 ** of the position-list, or the 0x01 that precedes the column number of
 131726 ** the next column in the position-list).
 131728 ** The number of elements in the column-list is returned.
 131730 static int fts3ColumnlistCount(char **ppCollist){
 131731   char *pEnd = *ppCollist;
 131732   char c = 0;
 131733   int nEntry = 0;
 131735   /* A column-list is terminated by either a 0x01 or 0x00. */
 131736   while( 0xFE & (*pEnd | c) ){
 131737     c = *pEnd++ & 0x80;
 131738     if( !c ) nEntry++;
 131741   *ppCollist = pEnd;
 131742   return nEntry;
 131746 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
 131747 ** for a single query. 
 131749 ** fts3ExprIterate() callback to load the 'global' elements of a
 131750 ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements 
 131751 ** of the matchinfo array that are constant for all rows returned by the 
 131752 ** current query.
 131754 ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
 131755 ** function populates Matchinfo.aMatchinfo[] as follows:
 131757 **   for(iCol=0; iCol<nCol; iCol++){
 131758 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
 131759 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
 131760 **   }
 131762 ** where X is the number of matches for phrase iPhrase is column iCol of all
 131763 ** rows of the table. Y is the number of rows for which column iCol contains
 131764 ** at least one instance of phrase iPhrase.
 131766 ** If the phrase pExpr consists entirely of deferred tokens, then all X and
 131767 ** Y values are set to nDoc, where nDoc is the number of documents in the 
 131768 ** file system. This is done because the full-text index doclist is required
 131769 ** to calculate these values properly, and the full-text index doclist is
 131770 ** not available for deferred tokens.
 131772 static int fts3ExprGlobalHitsCb(
 131773   Fts3Expr *pExpr,                /* Phrase expression node */
 131774   int iPhrase,                    /* Phrase number (numbered from zero) */
 131775   void *pCtx                      /* Pointer to MatchInfo structure */
 131777   MatchInfo *p = (MatchInfo *)pCtx;
 131778   return sqlite3Fts3EvalPhraseStats(
 131779       p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
 131784 ** fts3ExprIterate() callback used to collect the "local" part of the
 131785 ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the 
 131786 ** array that are different for each row returned by the query.
 131788 static int fts3ExprLocalHitsCb(
 131789   Fts3Expr *pExpr,                /* Phrase expression node */
 131790   int iPhrase,                    /* Phrase number */
 131791   void *pCtx                      /* Pointer to MatchInfo structure */
 131793   int rc = SQLITE_OK;
 131794   MatchInfo *p = (MatchInfo *)pCtx;
 131795   int iStart = iPhrase * p->nCol * 3;
 131796   int i;
 131798   for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
 131799     char *pCsr;
 131800     rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
 131801     if( pCsr ){
 131802       p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
 131803     }else{
 131804       p->aMatchinfo[iStart+i*3] = 0;
 131808   return rc;
 131811 static int fts3MatchinfoCheck(
 131812   Fts3Table *pTab, 
 131813   char cArg,
 131814   char **pzErr
 131816   if( (cArg==FTS3_MATCHINFO_NPHRASE)
 131817    || (cArg==FTS3_MATCHINFO_NCOL)
 131818    || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
 131819    || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
 131820    || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
 131821    || (cArg==FTS3_MATCHINFO_LCS)
 131822    || (cArg==FTS3_MATCHINFO_HITS)
 131824     return SQLITE_OK;
 131826   *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
 131827   return SQLITE_ERROR;
 131830 static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
 131831   int nVal;                       /* Number of integers output by cArg */
 131833   switch( cArg ){
 131834     case FTS3_MATCHINFO_NDOC:
 131835     case FTS3_MATCHINFO_NPHRASE: 
 131836     case FTS3_MATCHINFO_NCOL: 
 131837       nVal = 1;
 131838       break;
 131840     case FTS3_MATCHINFO_AVGLENGTH:
 131841     case FTS3_MATCHINFO_LENGTH:
 131842     case FTS3_MATCHINFO_LCS:
 131843       nVal = pInfo->nCol;
 131844       break;
 131846     default:
 131847       assert( cArg==FTS3_MATCHINFO_HITS );
 131848       nVal = pInfo->nCol * pInfo->nPhrase * 3;
 131849       break;
 131852   return nVal;
 131855 static int fts3MatchinfoSelectDoctotal(
 131856   Fts3Table *pTab,
 131857   sqlite3_stmt **ppStmt,
 131858   sqlite3_int64 *pnDoc,
 131859   const char **paLen
 131861   sqlite3_stmt *pStmt;
 131862   const char *a;
 131863   sqlite3_int64 nDoc;
 131865   if( !*ppStmt ){
 131866     int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
 131867     if( rc!=SQLITE_OK ) return rc;
 131869   pStmt = *ppStmt;
 131870   assert( sqlite3_data_count(pStmt)==1 );
 131872   a = sqlite3_column_blob(pStmt, 0);
 131873   a += sqlite3Fts3GetVarint(a, &nDoc);
 131874   if( nDoc==0 ) return FTS_CORRUPT_VTAB;
 131875   *pnDoc = (u32)nDoc;
 131877   if( paLen ) *paLen = a;
 131878   return SQLITE_OK;
 131882 ** An instance of the following structure is used to store state while 
 131883 ** iterating through a multi-column position-list corresponding to the
 131884 ** hits for a single phrase on a single row in order to calculate the
 131885 ** values for a matchinfo() FTS3_MATCHINFO_LCS request.
 131887 typedef struct LcsIterator LcsIterator;
 131888 struct LcsIterator {
 131889   Fts3Expr *pExpr;                /* Pointer to phrase expression */
 131890   int iPosOffset;                 /* Tokens count up to end of this phrase */
 131891   char *pRead;                    /* Cursor used to iterate through aDoclist */
 131892   int iPos;                       /* Current position */
 131896 ** If LcsIterator.iCol is set to the following value, the iterator has
 131897 ** finished iterating through all offsets for all columns.
 131899 #define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
 131901 static int fts3MatchinfoLcsCb(
 131902   Fts3Expr *pExpr,                /* Phrase expression node */
 131903   int iPhrase,                    /* Phrase number (numbered from zero) */
 131904   void *pCtx                      /* Pointer to MatchInfo structure */
 131906   LcsIterator *aIter = (LcsIterator *)pCtx;
 131907   aIter[iPhrase].pExpr = pExpr;
 131908   return SQLITE_OK;
 131912 ** Advance the iterator passed as an argument to the next position. Return
 131913 ** 1 if the iterator is at EOF or if it now points to the start of the
 131914 ** position list for the next column.
 131916 static int fts3LcsIteratorAdvance(LcsIterator *pIter){
 131917   char *pRead = pIter->pRead;
 131918   sqlite3_int64 iRead;
 131919   int rc = 0;
 131921   pRead += sqlite3Fts3GetVarint(pRead, &iRead);
 131922   if( iRead==0 || iRead==1 ){
 131923     pRead = 0;
 131924     rc = 1;
 131925   }else{
 131926     pIter->iPos += (int)(iRead-2);
 131929   pIter->pRead = pRead;
 131930   return rc;
 131934 ** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. 
 131936 ** If the call is successful, the longest-common-substring lengths for each
 131937 ** column are written into the first nCol elements of the pInfo->aMatchinfo[] 
 131938 ** array before returning. SQLITE_OK is returned in this case.
 131940 ** Otherwise, if an error occurs, an SQLite error code is returned and the
 131941 ** data written to the first nCol elements of pInfo->aMatchinfo[] is 
 131942 ** undefined.
 131944 static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
 131945   LcsIterator *aIter;
 131946   int i;
 131947   int iCol;
 131948   int nToken = 0;
 131950   /* Allocate and populate the array of LcsIterator objects. The array
 131951   ** contains one element for each matchable phrase in the query.
 131952   **/
 131953   aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
 131954   if( !aIter ) return SQLITE_NOMEM;
 131955   memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
 131956   (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
 131958   for(i=0; i<pInfo->nPhrase; i++){
 131959     LcsIterator *pIter = &aIter[i];
 131960     nToken -= pIter->pExpr->pPhrase->nToken;
 131961     pIter->iPosOffset = nToken;
 131964   for(iCol=0; iCol<pInfo->nCol; iCol++){
 131965     int nLcs = 0;                 /* LCS value for this column */
 131966     int nLive = 0;                /* Number of iterators in aIter not at EOF */
 131968     for(i=0; i<pInfo->nPhrase; i++){
 131969       int rc;
 131970       LcsIterator *pIt = &aIter[i];
 131971       rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
 131972       if( rc!=SQLITE_OK ) return rc;
 131973       if( pIt->pRead ){
 131974         pIt->iPos = pIt->iPosOffset;
 131975         fts3LcsIteratorAdvance(&aIter[i]);
 131976         nLive++;
 131980     while( nLive>0 ){
 131981       LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
 131982       int nThisLcs = 0;           /* LCS for the current iterator positions */
 131984       for(i=0; i<pInfo->nPhrase; i++){
 131985         LcsIterator *pIter = &aIter[i];
 131986         if( pIter->pRead==0 ){
 131987           /* This iterator is already at EOF for this column. */
 131988           nThisLcs = 0;
 131989         }else{
 131990           if( pAdv==0 || pIter->iPos<pAdv->iPos ){
 131991             pAdv = pIter;
 131993           if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
 131994             nThisLcs++;
 131995           }else{
 131996             nThisLcs = 1;
 131998           if( nThisLcs>nLcs ) nLcs = nThisLcs;
 132001       if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
 132004     pInfo->aMatchinfo[iCol] = nLcs;
 132007   sqlite3_free(aIter);
 132008   return SQLITE_OK;
 132012 ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
 132013 ** be returned by the matchinfo() function. Argument zArg contains the 
 132014 ** format string passed as the second argument to matchinfo (or the
 132015 ** default value "pcx" if no second argument was specified). The format
 132016 ** string has already been validated and the pInfo->aMatchinfo[] array
 132017 ** is guaranteed to be large enough for the output.
 132019 ** If bGlobal is true, then populate all fields of the matchinfo() output.
 132020 ** If it is false, then assume that those fields that do not change between
 132021 ** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
 132022 ** have already been populated.
 132024 ** Return SQLITE_OK if successful, or an SQLite error code if an error 
 132025 ** occurs. If a value other than SQLITE_OK is returned, the state the
 132026 ** pInfo->aMatchinfo[] buffer is left in is undefined.
 132028 static int fts3MatchinfoValues(
 132029   Fts3Cursor *pCsr,               /* FTS3 cursor object */
 132030   int bGlobal,                    /* True to grab the global stats */
 132031   MatchInfo *pInfo,               /* Matchinfo context object */
 132032   const char *zArg                /* Matchinfo format string */
 132034   int rc = SQLITE_OK;
 132035   int i;
 132036   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 132037   sqlite3_stmt *pSelect = 0;
 132039   for(i=0; rc==SQLITE_OK && zArg[i]; i++){
 132041     switch( zArg[i] ){
 132042       case FTS3_MATCHINFO_NPHRASE:
 132043         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
 132044         break;
 132046       case FTS3_MATCHINFO_NCOL:
 132047         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
 132048         break;
 132050       case FTS3_MATCHINFO_NDOC:
 132051         if( bGlobal ){
 132052           sqlite3_int64 nDoc = 0;
 132053           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
 132054           pInfo->aMatchinfo[0] = (u32)nDoc;
 132056         break;
 132058       case FTS3_MATCHINFO_AVGLENGTH: 
 132059         if( bGlobal ){
 132060           sqlite3_int64 nDoc;     /* Number of rows in table */
 132061           const char *a;          /* Aggregate column length array */
 132063           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
 132064           if( rc==SQLITE_OK ){
 132065             int iCol;
 132066             for(iCol=0; iCol<pInfo->nCol; iCol++){
 132067               u32 iVal;
 132068               sqlite3_int64 nToken;
 132069               a += sqlite3Fts3GetVarint(a, &nToken);
 132070               iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
 132071               pInfo->aMatchinfo[iCol] = iVal;
 132075         break;
 132077       case FTS3_MATCHINFO_LENGTH: {
 132078         sqlite3_stmt *pSelectDocsize = 0;
 132079         rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
 132080         if( rc==SQLITE_OK ){
 132081           int iCol;
 132082           const char *a = sqlite3_column_blob(pSelectDocsize, 0);
 132083           for(iCol=0; iCol<pInfo->nCol; iCol++){
 132084             sqlite3_int64 nToken;
 132085             a += sqlite3Fts3GetVarint(a, &nToken);
 132086             pInfo->aMatchinfo[iCol] = (u32)nToken;
 132089         sqlite3_reset(pSelectDocsize);
 132090         break;
 132093       case FTS3_MATCHINFO_LCS:
 132094         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
 132095         if( rc==SQLITE_OK ){
 132096           rc = fts3MatchinfoLcs(pCsr, pInfo);
 132098         break;
 132100       default: {
 132101         Fts3Expr *pExpr;
 132102         assert( zArg[i]==FTS3_MATCHINFO_HITS );
 132103         pExpr = pCsr->pExpr;
 132104         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
 132105         if( rc!=SQLITE_OK ) break;
 132106         if( bGlobal ){
 132107           if( pCsr->pDeferred ){
 132108             rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
 132109             if( rc!=SQLITE_OK ) break;
 132111           rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
 132112           if( rc!=SQLITE_OK ) break;
 132114         (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
 132115         break;
 132119     pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
 132122   sqlite3_reset(pSelect);
 132123   return rc;
 132128 ** Populate pCsr->aMatchinfo[] with data for the current row. The 
 132129 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
 132131 static int fts3GetMatchinfo(
 132132   Fts3Cursor *pCsr,               /* FTS3 Cursor object */
 132133   const char *zArg                /* Second argument to matchinfo() function */
 132135   MatchInfo sInfo;
 132136   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 132137   int rc = SQLITE_OK;
 132138   int bGlobal = 0;                /* Collect 'global' stats as well as local */
 132140   memset(&sInfo, 0, sizeof(MatchInfo));
 132141   sInfo.pCursor = pCsr;
 132142   sInfo.nCol = pTab->nColumn;
 132144   /* If there is cached matchinfo() data, but the format string for the 
 132145   ** cache does not match the format string for this request, discard 
 132146   ** the cached data. */
 132147   if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
 132148     assert( pCsr->aMatchinfo );
 132149     sqlite3_free(pCsr->aMatchinfo);
 132150     pCsr->zMatchinfo = 0;
 132151     pCsr->aMatchinfo = 0;
 132154   /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
 132155   ** matchinfo function has been called for this query. In this case 
 132156   ** allocate the array used to accumulate the matchinfo data and
 132157   ** initialize those elements that are constant for every row.
 132159   if( pCsr->aMatchinfo==0 ){
 132160     int nMatchinfo = 0;           /* Number of u32 elements in match-info */
 132161     int nArg;                     /* Bytes in zArg */
 132162     int i;                        /* Used to iterate through zArg */
 132164     /* Determine the number of phrases in the query */
 132165     pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
 132166     sInfo.nPhrase = pCsr->nPhrase;
 132168     /* Determine the number of integers in the buffer returned by this call. */
 132169     for(i=0; zArg[i]; i++){
 132170       nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
 132173     /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
 132174     nArg = (int)strlen(zArg);
 132175     pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
 132176     if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
 132178     pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
 132179     pCsr->nMatchinfo = nMatchinfo;
 132180     memcpy(pCsr->zMatchinfo, zArg, nArg+1);
 132181     memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
 132182     pCsr->isMatchinfoNeeded = 1;
 132183     bGlobal = 1;
 132186   sInfo.aMatchinfo = pCsr->aMatchinfo;
 132187   sInfo.nPhrase = pCsr->nPhrase;
 132188   if( pCsr->isMatchinfoNeeded ){
 132189     rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
 132190     pCsr->isMatchinfoNeeded = 0;
 132193   return rc;
 132197 ** Implementation of snippet() function.
 132199 SQLITE_PRIVATE void sqlite3Fts3Snippet(
 132200   sqlite3_context *pCtx,          /* SQLite function call context */
 132201   Fts3Cursor *pCsr,               /* Cursor object */
 132202   const char *zStart,             /* Snippet start text - "<b>" */
 132203   const char *zEnd,               /* Snippet end text - "</b>" */
 132204   const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
 132205   int iCol,                       /* Extract snippet from this column */
 132206   int nToken                      /* Approximate number of tokens in snippet */
 132208   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 132209   int rc = SQLITE_OK;
 132210   int i;
 132211   StrBuffer res = {0, 0, 0};
 132213   /* The returned text includes up to four fragments of text extracted from
 132214   ** the data in the current row. The first iteration of the for(...) loop
 132215   ** below attempts to locate a single fragment of text nToken tokens in 
 132216   ** size that contains at least one instance of all phrases in the query
 132217   ** expression that appear in the current row. If such a fragment of text
 132218   ** cannot be found, the second iteration of the loop attempts to locate
 132219   ** a pair of fragments, and so on.
 132221   int nSnippet = 0;               /* Number of fragments in this snippet */
 132222   SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
 132223   int nFToken = -1;               /* Number of tokens in each fragment */
 132225   if( !pCsr->pExpr ){
 132226     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
 132227     return;
 132230   for(nSnippet=1; 1; nSnippet++){
 132232     int iSnip;                    /* Loop counter 0..nSnippet-1 */
 132233     u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
 132234     u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
 132236     if( nToken>=0 ){
 132237       nFToken = (nToken+nSnippet-1) / nSnippet;
 132238     }else{
 132239       nFToken = -1 * nToken;
 132242     for(iSnip=0; iSnip<nSnippet; iSnip++){
 132243       int iBestScore = -1;        /* Best score of columns checked so far */
 132244       int iRead;                  /* Used to iterate through columns */
 132245       SnippetFragment *pFragment = &aSnippet[iSnip];
 132247       memset(pFragment, 0, sizeof(*pFragment));
 132249       /* Loop through all columns of the table being considered for snippets.
 132250       ** If the iCol argument to this function was negative, this means all
 132251       ** columns of the FTS3 table. Otherwise, only column iCol is considered.
 132253       for(iRead=0; iRead<pTab->nColumn; iRead++){
 132254         SnippetFragment sF = {0, 0, 0, 0};
 132255         int iS;
 132256         if( iCol>=0 && iRead!=iCol ) continue;
 132258         /* Find the best snippet of nFToken tokens in column iRead. */
 132259         rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
 132260         if( rc!=SQLITE_OK ){
 132261           goto snippet_out;
 132263         if( iS>iBestScore ){
 132264           *pFragment = sF;
 132265           iBestScore = iS;
 132269       mCovered |= pFragment->covered;
 132272     /* If all query phrases seen by fts3BestSnippet() are present in at least
 132273     ** one of the nSnippet snippet fragments, break out of the loop.
 132275     assert( (mCovered&mSeen)==mCovered );
 132276     if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
 132279   assert( nFToken>0 );
 132281   for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
 132282     rc = fts3SnippetText(pCsr, &aSnippet[i], 
 132283         i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
 132287  snippet_out:
 132288   sqlite3Fts3SegmentsClose(pTab);
 132289   if( rc!=SQLITE_OK ){
 132290     sqlite3_result_error_code(pCtx, rc);
 132291     sqlite3_free(res.z);
 132292   }else{
 132293     sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
 132298 typedef struct TermOffset TermOffset;
 132299 typedef struct TermOffsetCtx TermOffsetCtx;
 132301 struct TermOffset {
 132302   char *pList;                    /* Position-list */
 132303   int iPos;                       /* Position just read from pList */
 132304   int iOff;                       /* Offset of this term from read positions */
 132307 struct TermOffsetCtx {
 132308   Fts3Cursor *pCsr;
 132309   int iCol;                       /* Column of table to populate aTerm for */
 132310   int iTerm;
 132311   sqlite3_int64 iDocid;
 132312   TermOffset *aTerm;
 132316 ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
 132318 static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
 132319   TermOffsetCtx *p = (TermOffsetCtx *)ctx;
 132320   int nTerm;                      /* Number of tokens in phrase */
 132321   int iTerm;                      /* For looping through nTerm phrase terms */
 132322   char *pList;                    /* Pointer to position list for phrase */
 132323   int iPos = 0;                   /* First position in position-list */
 132324   int rc;
 132326   UNUSED_PARAMETER(iPhrase);
 132327   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
 132328   nTerm = pExpr->pPhrase->nToken;
 132329   if( pList ){
 132330     fts3GetDeltaPosition(&pList, &iPos);
 132331     assert( iPos>=0 );
 132334   for(iTerm=0; iTerm<nTerm; iTerm++){
 132335     TermOffset *pT = &p->aTerm[p->iTerm++];
 132336     pT->iOff = nTerm-iTerm-1;
 132337     pT->pList = pList;
 132338     pT->iPos = iPos;
 132341   return rc;
 132345 ** Implementation of offsets() function.
 132347 SQLITE_PRIVATE void sqlite3Fts3Offsets(
 132348   sqlite3_context *pCtx,          /* SQLite function call context */
 132349   Fts3Cursor *pCsr                /* Cursor object */
 132351   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 132352   sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
 132353   int rc;                         /* Return Code */
 132354   int nToken;                     /* Number of tokens in query */
 132355   int iCol;                       /* Column currently being processed */
 132356   StrBuffer res = {0, 0, 0};      /* Result string */
 132357   TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
 132359   if( !pCsr->pExpr ){
 132360     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
 132361     return;
 132364   memset(&sCtx, 0, sizeof(sCtx));
 132365   assert( pCsr->isRequireSeek==0 );
 132367   /* Count the number of terms in the query */
 132368   rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
 132369   if( rc!=SQLITE_OK ) goto offsets_out;
 132371   /* Allocate the array of TermOffset iterators. */
 132372   sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
 132373   if( 0==sCtx.aTerm ){
 132374     rc = SQLITE_NOMEM;
 132375     goto offsets_out;
 132377   sCtx.iDocid = pCsr->iPrevId;
 132378   sCtx.pCsr = pCsr;
 132380   /* Loop through the table columns, appending offset information to 
 132381   ** string-buffer res for each column.
 132383   for(iCol=0; iCol<pTab->nColumn; iCol++){
 132384     sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
 132385     const char *ZDUMMY;           /* Dummy argument used with xNext() */
 132386     int NDUMMY = 0;               /* Dummy argument used with xNext() */
 132387     int iStart = 0;
 132388     int iEnd = 0;
 132389     int iCurrent = 0;
 132390     const char *zDoc;
 132391     int nDoc;
 132393     /* Initialize the contents of sCtx.aTerm[] for column iCol. There is 
 132394     ** no way that this operation can fail, so the return code from
 132395     ** fts3ExprIterate() can be discarded.
 132397     sCtx.iCol = iCol;
 132398     sCtx.iTerm = 0;
 132399     (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
 132401     /* Retreive the text stored in column iCol. If an SQL NULL is stored 
 132402     ** in column iCol, jump immediately to the next iteration of the loop.
 132403     ** If an OOM occurs while retrieving the data (this can happen if SQLite
 132404     ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM 
 132405     ** to the caller. 
 132407     zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
 132408     nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
 132409     if( zDoc==0 ){
 132410       if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
 132411         continue;
 132413       rc = SQLITE_NOMEM;
 132414       goto offsets_out;
 132417     /* Initialize a tokenizer iterator to iterate through column iCol. */
 132418     rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
 132419         zDoc, nDoc, &pC
 132421     if( rc!=SQLITE_OK ) goto offsets_out;
 132423     rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
 132424     while( rc==SQLITE_OK ){
 132425       int i;                      /* Used to loop through terms */
 132426       int iMinPos = 0x7FFFFFFF;   /* Position of next token */
 132427       TermOffset *pTerm = 0;      /* TermOffset associated with next token */
 132429       for(i=0; i<nToken; i++){
 132430         TermOffset *pT = &sCtx.aTerm[i];
 132431         if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
 132432           iMinPos = pT->iPos-pT->iOff;
 132433           pTerm = pT;
 132437       if( !pTerm ){
 132438         /* All offsets for this column have been gathered. */
 132439         rc = SQLITE_DONE;
 132440       }else{
 132441         assert( iCurrent<=iMinPos );
 132442         if( 0==(0xFE&*pTerm->pList) ){
 132443           pTerm->pList = 0;
 132444         }else{
 132445           fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
 132447         while( rc==SQLITE_OK && iCurrent<iMinPos ){
 132448           rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
 132450         if( rc==SQLITE_OK ){
 132451           char aBuffer[64];
 132452           sqlite3_snprintf(sizeof(aBuffer), aBuffer, 
 132453               "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
 132455           rc = fts3StringAppend(&res, aBuffer, -1);
 132456         }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
 132457           rc = FTS_CORRUPT_VTAB;
 132461     if( rc==SQLITE_DONE ){
 132462       rc = SQLITE_OK;
 132465     pMod->xClose(pC);
 132466     if( rc!=SQLITE_OK ) goto offsets_out;
 132469  offsets_out:
 132470   sqlite3_free(sCtx.aTerm);
 132471   assert( rc!=SQLITE_DONE );
 132472   sqlite3Fts3SegmentsClose(pTab);
 132473   if( rc!=SQLITE_OK ){
 132474     sqlite3_result_error_code(pCtx,  rc);
 132475     sqlite3_free(res.z);
 132476   }else{
 132477     sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
 132479   return;
 132483 ** Implementation of matchinfo() function.
 132485 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
 132486   sqlite3_context *pContext,      /* Function call context */
 132487   Fts3Cursor *pCsr,               /* FTS3 table cursor */
 132488   const char *zArg                /* Second arg to matchinfo() function */
 132490   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 132491   int rc;
 132492   int i;
 132493   const char *zFormat;
 132495   if( zArg ){
 132496     for(i=0; zArg[i]; i++){
 132497       char *zErr = 0;
 132498       if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
 132499         sqlite3_result_error(pContext, zErr, -1);
 132500         sqlite3_free(zErr);
 132501         return;
 132504     zFormat = zArg;
 132505   }else{
 132506     zFormat = FTS3_MATCHINFO_DEFAULT;
 132509   if( !pCsr->pExpr ){
 132510     sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
 132511     return;
 132514   /* Retrieve matchinfo() data. */
 132515   rc = fts3GetMatchinfo(pCsr, zFormat);
 132516   sqlite3Fts3SegmentsClose(pTab);
 132518   if( rc!=SQLITE_OK ){
 132519     sqlite3_result_error_code(pContext, rc);
 132520   }else{
 132521     int n = pCsr->nMatchinfo * sizeof(u32);
 132522     sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
 132526 #endif
 132528 /************** End of fts3_snippet.c ****************************************/
 132529 /************** Begin file fts3_unicode.c ************************************/
 132531 ** 2012 May 24
 132533 ** The author disclaims copyright to this source code.  In place of
 132534 ** a legal notice, here is a blessing:
 132536 **    May you do good and not evil.
 132537 **    May you find forgiveness for yourself and forgive others.
 132538 **    May you share freely, never taking more than you give.
 132540 ******************************************************************************
 132542 ** Implementation of the "unicode" full-text-search tokenizer.
 132545 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 132547 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 132549 /* #include <assert.h> */
 132550 /* #include <stdlib.h> */
 132551 /* #include <stdio.h> */
 132552 /* #include <string.h> */
 132556 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
 132557 ** from the sqlite3 source file utf.c. If this file is compiled as part
 132558 ** of the amalgamation, they are not required.
 132560 #ifndef SQLITE_AMALGAMATION
 132562 static const unsigned char sqlite3Utf8Trans1[] = {
 132563   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 132564   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 132565   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 132566   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
 132567   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 132568   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 132569   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 132570   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
 132573 #define READ_UTF8(zIn, zTerm, c)                           \
 132574   c = *(zIn++);                                            \
 132575   if( c>=0xc0 ){                                           \
 132576     c = sqlite3Utf8Trans1[c-0xc0];                         \
 132577     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
 132578       c = (c<<6) + (0x3f & *(zIn++));                      \
 132579     }                                                      \
 132580     if( c<0x80                                             \
 132581         || (c&0xFFFFF800)==0xD800                          \
 132582         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
 132585 #define WRITE_UTF8(zOut, c) {                          \
 132586   if( c<0x00080 ){                                     \
 132587     *zOut++ = (u8)(c&0xFF);                            \
 132588   }                                                    \
 132589   else if( c<0x00800 ){                                \
 132590     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
 132591     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 132592   }                                                    \
 132593   else if( c<0x10000 ){                                \
 132594     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
 132595     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 132596     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 132597   }else{                                               \
 132598     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
 132599     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
 132600     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 132601     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 132602   }                                                    \
 132605 #endif /* ifndef SQLITE_AMALGAMATION */
 132607 typedef struct unicode_tokenizer unicode_tokenizer;
 132608 typedef struct unicode_cursor unicode_cursor;
 132610 struct unicode_tokenizer {
 132611   sqlite3_tokenizer base;
 132612   int bRemoveDiacritic;
 132613   int nException;
 132614   int *aiException;
 132617 struct unicode_cursor {
 132618   sqlite3_tokenizer_cursor base;
 132619   const unsigned char *aInput;    /* Input text being tokenized */
 132620   int nInput;                     /* Size of aInput[] in bytes */
 132621   int iOff;                       /* Current offset within aInput[] */
 132622   int iToken;                     /* Index of next token to be returned */
 132623   char *zToken;                   /* storage for current token */
 132624   int nAlloc;                     /* space allocated at zToken */
 132629 ** Destroy a tokenizer allocated by unicodeCreate().
 132631 static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
 132632   if( pTokenizer ){
 132633     unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
 132634     sqlite3_free(p->aiException);
 132635     sqlite3_free(p);
 132637   return SQLITE_OK;
 132641 ** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
 132642 ** statement has specified that the tokenizer for this table shall consider
 132643 ** all characters in string zIn/nIn to be separators (if bAlnum==0) or
 132644 ** token characters (if bAlnum==1).
 132646 ** For each codepoint in the zIn/nIn string, this function checks if the
 132647 ** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
 132648 ** If so, no action is taken. Otherwise, the codepoint is added to the 
 132649 ** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
 132650 ** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
 132651 ** codepoints in the aiException[] array.
 132653 ** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
 132654 ** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
 132655 ** It is not possible to change the behaviour of the tokenizer with respect
 132656 ** to these codepoints.
 132658 static int unicodeAddExceptions(
 132659   unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
 132660   int bAlnum,                     /* Replace Isalnum() return value with this */
 132661   const char *zIn,                /* Array of characters to make exceptions */
 132662   int nIn                         /* Length of z in bytes */
 132664   const unsigned char *z = (const unsigned char *)zIn;
 132665   const unsigned char *zTerm = &z[nIn];
 132666   int iCode;
 132667   int nEntry = 0;
 132669   assert( bAlnum==0 || bAlnum==1 );
 132671   while( z<zTerm ){
 132672     READ_UTF8(z, zTerm, iCode);
 132673     assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
 132674     if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
 132675      && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
 132677       nEntry++;
 132681   if( nEntry ){
 132682     int *aNew;                    /* New aiException[] array */
 132683     int nNew;                     /* Number of valid entries in array aNew[] */
 132685     aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
 132686     if( aNew==0 ) return SQLITE_NOMEM;
 132687     nNew = p->nException;
 132689     z = (const unsigned char *)zIn;
 132690     while( z<zTerm ){
 132691       READ_UTF8(z, zTerm, iCode);
 132692       if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
 132693        && sqlite3FtsUnicodeIsdiacritic(iCode)==0
 132695         int i, j;
 132696         for(i=0; i<nNew && aNew[i]<iCode; i++);
 132697         for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
 132698         aNew[i] = iCode;
 132699         nNew++;
 132702     p->aiException = aNew;
 132703     p->nException = nNew;
 132706   return SQLITE_OK;
 132710 ** Return true if the p->aiException[] array contains the value iCode.
 132712 static int unicodeIsException(unicode_tokenizer *p, int iCode){
 132713   if( p->nException>0 ){
 132714     int *a = p->aiException;
 132715     int iLo = 0;
 132716     int iHi = p->nException-1;
 132718     while( iHi>=iLo ){
 132719       int iTest = (iHi + iLo) / 2;
 132720       if( iCode==a[iTest] ){
 132721         return 1;
 132722       }else if( iCode>a[iTest] ){
 132723         iLo = iTest+1;
 132724       }else{
 132725         iHi = iTest-1;
 132730   return 0;
 132734 ** Return true if, for the purposes of tokenization, codepoint iCode is
 132735 ** considered a token character (not a separator).
 132737 static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
 132738   assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
 132739   return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
 132743 ** Create a new tokenizer instance.
 132745 static int unicodeCreate(
 132746   int nArg,                       /* Size of array argv[] */
 132747   const char * const *azArg,      /* Tokenizer creation arguments */
 132748   sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
 132750   unicode_tokenizer *pNew;        /* New tokenizer object */
 132751   int i;
 132752   int rc = SQLITE_OK;
 132754   pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
 132755   if( pNew==NULL ) return SQLITE_NOMEM;
 132756   memset(pNew, 0, sizeof(unicode_tokenizer));
 132757   pNew->bRemoveDiacritic = 1;
 132759   for(i=0; rc==SQLITE_OK && i<nArg; i++){
 132760     const char *z = azArg[i];
 132761     int n = strlen(z);
 132763     if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
 132764       pNew->bRemoveDiacritic = 1;
 132766     else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
 132767       pNew->bRemoveDiacritic = 0;
 132769     else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
 132770       rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
 132772     else if( n>=11 && memcmp("separators=", z, 11)==0 ){
 132773       rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
 132775     else{
 132776       /* Unrecognized argument */
 132777       rc  = SQLITE_ERROR;
 132781   if( rc!=SQLITE_OK ){
 132782     unicodeDestroy((sqlite3_tokenizer *)pNew);
 132783     pNew = 0;
 132785   *pp = (sqlite3_tokenizer *)pNew;
 132786   return rc;
 132790 ** Prepare to begin tokenizing a particular string.  The input
 132791 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
 132792 ** used to incrementally tokenize this string is returned in 
 132793 ** *ppCursor.
 132795 static int unicodeOpen(
 132796   sqlite3_tokenizer *p,           /* The tokenizer */
 132797   const char *aInput,             /* Input string */
 132798   int nInput,                     /* Size of string aInput in bytes */
 132799   sqlite3_tokenizer_cursor **pp   /* OUT: New cursor object */
 132801   unicode_cursor *pCsr;
 132803   pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
 132804   if( pCsr==0 ){
 132805     return SQLITE_NOMEM;
 132807   memset(pCsr, 0, sizeof(unicode_cursor));
 132809   pCsr->aInput = (const unsigned char *)aInput;
 132810   if( aInput==0 ){
 132811     pCsr->nInput = 0;
 132812   }else if( nInput<0 ){
 132813     pCsr->nInput = (int)strlen(aInput);
 132814   }else{
 132815     pCsr->nInput = nInput;
 132818   *pp = &pCsr->base;
 132819   UNUSED_PARAMETER(p);
 132820   return SQLITE_OK;
 132824 ** Close a tokenization cursor previously opened by a call to
 132825 ** simpleOpen() above.
 132827 static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
 132828   unicode_cursor *pCsr = (unicode_cursor *) pCursor;
 132829   sqlite3_free(pCsr->zToken);
 132830   sqlite3_free(pCsr);
 132831   return SQLITE_OK;
 132835 ** Extract the next token from a tokenization cursor.  The cursor must
 132836 ** have been opened by a prior call to simpleOpen().
 132838 static int unicodeNext(
 132839   sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
 132840   const char **paToken,           /* OUT: Token text */
 132841   int *pnToken,                   /* OUT: Number of bytes at *paToken */
 132842   int *piStart,                   /* OUT: Starting offset of token */
 132843   int *piEnd,                     /* OUT: Ending offset of token */
 132844   int *piPos                      /* OUT: Position integer of token */
 132846   unicode_cursor *pCsr = (unicode_cursor *)pC;
 132847   unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
 132848   int iCode;
 132849   char *zOut;
 132850   const unsigned char *z = &pCsr->aInput[pCsr->iOff];
 132851   const unsigned char *zStart = z;
 132852   const unsigned char *zEnd;
 132853   const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
 132855   /* Scan past any delimiter characters before the start of the next token.
 132856   ** Return SQLITE_DONE early if this takes us all the way to the end of 
 132857   ** the input.  */
 132858   while( z<zTerm ){
 132859     READ_UTF8(z, zTerm, iCode);
 132860     if( unicodeIsAlnum(p, iCode) ) break;
 132861     zStart = z;
 132863   if( zStart>=zTerm ) return SQLITE_DONE;
 132865   zOut = pCsr->zToken;
 132866   do {
 132867     int iOut;
 132869     /* Grow the output buffer if required. */
 132870     if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
 132871       char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
 132872       if( !zNew ) return SQLITE_NOMEM;
 132873       zOut = &zNew[zOut - pCsr->zToken];
 132874       pCsr->zToken = zNew;
 132875       pCsr->nAlloc += 64;
 132878     /* Write the folded case of the last character read to the output */
 132879     zEnd = z;
 132880     iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
 132881     if( iOut ){
 132882       WRITE_UTF8(zOut, iOut);
 132885     /* If the cursor is not at EOF, read the next character */
 132886     if( z>=zTerm ) break;
 132887     READ_UTF8(z, zTerm, iCode);
 132888   }while( unicodeIsAlnum(p, iCode) 
 132889        || sqlite3FtsUnicodeIsdiacritic(iCode)
 132892   /* Set the output variables and return. */
 132893   pCsr->iOff = (z - pCsr->aInput);
 132894   *paToken = pCsr->zToken;
 132895   *pnToken = zOut - pCsr->zToken;
 132896   *piStart = (zStart - pCsr->aInput);
 132897   *piEnd = (zEnd - pCsr->aInput);
 132898   *piPos = pCsr->iToken++;
 132899   return SQLITE_OK;
 132903 ** Set *ppModule to a pointer to the sqlite3_tokenizer_module 
 132904 ** structure for the unicode tokenizer.
 132906 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
 132907   static const sqlite3_tokenizer_module module = {
 132909     unicodeCreate,
 132910     unicodeDestroy,
 132911     unicodeOpen,
 132912     unicodeClose,
 132913     unicodeNext,
 132916   *ppModule = &module;
 132919 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 132920 #endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
 132922 /************** End of fts3_unicode.c ****************************************/
 132923 /************** Begin file fts3_unicode2.c ***********************************/
 132925 ** 2012 May 25
 132927 ** The author disclaims copyright to this source code.  In place of
 132928 ** a legal notice, here is a blessing:
 132930 **    May you do good and not evil.
 132931 **    May you find forgiveness for yourself and forgive others.
 132932 **    May you share freely, never taking more than you give.
 132934 ******************************************************************************
 132938 ** DO NOT EDIT THIS MACHINE GENERATED FILE.
 132941 #if defined(SQLITE_ENABLE_FTS4_UNICODE61)
 132942 #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
 132944 /* #include <assert.h> */
 132947 ** Return true if the argument corresponds to a unicode codepoint
 132948 ** classified as either a letter or a number. Otherwise false.
 132950 ** The results are undefined if the value passed to this function
 132951 ** is less than zero.
 132953 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
 132954   /* Each unsigned integer in the following array corresponds to a contiguous
 132955   ** range of unicode codepoints that are not either letters or numbers (i.e.
 132956   ** codepoints for which this function should return 0).
 132958   ** The most significant 22 bits in each 32-bit value contain the first 
 132959   ** codepoint in the range. The least significant 10 bits are used to store
 132960   ** the size of the range (always at least 1). In other words, the value 
 132961   ** ((C<<22) + N) represents a range of N codepoints starting with codepoint 
 132962   ** C. It is not possible to represent a range larger than 1023 codepoints 
 132963   ** using this format.
 132965   const static unsigned int aEntry[] = {
 132966     0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
 132967     0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
 132968     0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
 132969     0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
 132970     0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
 132971     0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
 132972     0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
 132973     0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
 132974     0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
 132975     0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
 132976     0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
 132977     0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
 132978     0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
 132979     0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
 132980     0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
 132981     0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
 132982     0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
 132983     0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
 132984     0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
 132985     0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
 132986     0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
 132987     0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
 132988     0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
 132989     0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
 132990     0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
 132991     0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
 132992     0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
 132993     0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
 132994     0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
 132995     0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
 132996     0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
 132997     0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
 132998     0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
 132999     0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
 133000     0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
 133001     0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
 133002     0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
 133003     0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
 133004     0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
 133005     0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
 133006     0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
 133007     0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
 133008     0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
 133009     0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
 133010     0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
 133011     0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
 133012     0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
 133013     0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
 133014     0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
 133015     0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
 133016     0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
 133017     0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
 133018     0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
 133019     0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
 133020     0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
 133021     0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
 133022     0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
 133023     0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
 133024     0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
 133025     0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
 133026     0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
 133027     0x037FFC02, 0x03E3FC01, 0x03EC7801, 0x03ECA401, 0x03EEC810,
 133028     0x03F4F802, 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023,
 133029     0x03F95013, 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807,
 133030     0x03FCEC06, 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405,
 133031     0x04040003, 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E,
 133032     0x040E7C01, 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01,
 133033     0x04280403, 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01,
 133034     0x04294009, 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016,
 133035     0x04420003, 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004,
 133036     0x04460003, 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004,
 133037     0x05BD442E, 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5,
 133038     0x07480046, 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01,
 133039     0x075C5401, 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401,
 133040     0x075EA401, 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064,
 133041     0x07C2800F, 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F,
 133042     0x07C4C03C, 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009,
 133043     0x07C94002, 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014,
 133044     0x07CE8025, 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001,
 133045     0x07D108B6, 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018,
 133046     0x07D7EC46, 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401,
 133047     0x38008060, 0x380400F0, 0x3C000001, 0x3FFFF401, 0x40000001,
 133048     0x43FFF401,
 133050   static const unsigned int aAscii[4] = {
 133051     0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
 133054   if( c<128 ){
 133055     return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
 133056   }else if( c<(1<<22) ){
 133057     unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
 133058     int iRes;
 133059     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
 133060     int iLo = 0;
 133061     while( iHi>=iLo ){
 133062       int iTest = (iHi + iLo) / 2;
 133063       if( key >= aEntry[iTest] ){
 133064         iRes = iTest;
 133065         iLo = iTest+1;
 133066       }else{
 133067         iHi = iTest-1;
 133070     assert( aEntry[0]<key );
 133071     assert( key>=aEntry[iRes] );
 133072     return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
 133074   return 1;
 133079 ** If the argument is a codepoint corresponding to a lowercase letter
 133080 ** in the ASCII range with a diacritic added, return the codepoint
 133081 ** of the ASCII letter only. For example, if passed 235 - "LATIN
 133082 ** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
 133083 ** E"). The resuls of passing a codepoint that corresponds to an
 133084 ** uppercase letter are undefined.
 133086 static int remove_diacritic(int c){
 133087   unsigned short aDia[] = {
 133088         0,  1797,  1848,  1859,  1891,  1928,  1940,  1995, 
 133089      2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286, 
 133090      2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732, 
 133091      2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336, 
 133092      3456,  3696,  3712,  3728,  3744,  3896,  3912,  3928, 
 133093      3968,  4008,  4040,  4106,  4138,  4170,  4202,  4234, 
 133094      4266,  4296,  4312,  4344,  4408,  4424,  4472,  4504, 
 133095      6148,  6198,  6264,  6280,  6360,  6429,  6505,  6529, 
 133096     61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726, 
 133097     61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122, 
 133098     62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536, 
 133099     62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730, 
 133100     62924, 63050, 63082, 63274, 63390, 
 133102   char aChar[] = {
 133103     '\0', 'a',  'c',  'e',  'i',  'n',  'o',  'u',  'y',  'y',  'a',  'c',  
 133104     'd',  'e',  'e',  'g',  'h',  'i',  'j',  'k',  'l',  'n',  'o',  'r',  
 133105     's',  't',  'u',  'u',  'w',  'y',  'z',  'o',  'u',  'a',  'i',  'o',  
 133106     'u',  'g',  'k',  'o',  'j',  'g',  'n',  'a',  'e',  'i',  'o',  'r',  
 133107     'u',  's',  't',  'h',  'a',  'e',  'o',  'y',  '\0', '\0', '\0', '\0', 
 133108     '\0', '\0', '\0', '\0', 'a',  'b',  'd',  'd',  'e',  'f',  'g',  'h',  
 133109     'h',  'i',  'k',  'l',  'l',  'm',  'n',  'p',  'r',  'r',  's',  't',  
 133110     'u',  'v',  'w',  'w',  'x',  'y',  'z',  'h',  't',  'w',  'y',  'a',  
 133111     'e',  'i',  'o',  'u',  'y',  
 133114   unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
 133115   int iRes = 0;
 133116   int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
 133117   int iLo = 0;
 133118   while( iHi>=iLo ){
 133119     int iTest = (iHi + iLo) / 2;
 133120     if( key >= aDia[iTest] ){
 133121       iRes = iTest;
 133122       iLo = iTest+1;
 133123     }else{
 133124       iHi = iTest-1;
 133127   assert( key>=aDia[iRes] );
 133128   return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
 133133 ** Return true if the argument interpreted as a unicode codepoint
 133134 ** is a diacritical modifier character.
 133136 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
 133137   unsigned int mask0 = 0x08029FDF;
 133138   unsigned int mask1 = 0x000361F8;
 133139   if( c<768 || c>817 ) return 0;
 133140   return (c < 768+32) ?
 133141       (mask0 & (1 << (c-768))) :
 133142       (mask1 & (1 << (c-768-32)));
 133147 ** Interpret the argument as a unicode codepoint. If the codepoint
 133148 ** is an upper case character that has a lower case equivalent,
 133149 ** return the codepoint corresponding to the lower case version.
 133150 ** Otherwise, return a copy of the argument.
 133152 ** The results are undefined if the value passed to this function
 133153 ** is less than zero.
 133155 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
 133156   /* Each entry in the following array defines a rule for folding a range
 133157   ** of codepoints to lower case. The rule applies to a range of nRange
 133158   ** codepoints starting at codepoint iCode.
 133160   ** If the least significant bit in flags is clear, then the rule applies
 133161   ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
 133162   ** need to be folded). Or, if it is set, then the rule only applies to
 133163   ** every second codepoint in the range, starting with codepoint C.
 133165   ** The 7 most significant bits in flags are an index into the aiOff[]
 133166   ** array. If a specific codepoint C does require folding, then its lower
 133167   ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
 133169   ** The contents of this array are generated by parsing the CaseFolding.txt
 133170   ** file distributed as part of the "Unicode Character Database". See
 133171   ** http://www.unicode.org for details.
 133173   static const struct TableEntry {
 133174     unsigned short iCode;
 133175     unsigned char flags;
 133176     unsigned char nRange;
 133177   } aEntry[] = {
 133178     {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
 133179     {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
 133180     {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
 133181     {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
 133182     {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
 133183     {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
 133184     {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
 133185     {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
 133186     {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
 133187     {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
 133188     {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
 133189     {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
 133190     {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
 133191     {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
 133192     {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
 133193     {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
 133194     {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
 133195     {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
 133196     {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
 133197     {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
 133198     {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
 133199     {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
 133200     {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
 133201     {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
 133202     {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
 133203     {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
 133204     {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
 133205     {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
 133206     {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
 133207     {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
 133208     {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
 133209     {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
 133210     {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
 133211     {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
 133212     {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
 133213     {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
 133214     {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
 133215     {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
 133216     {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
 133217     {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
 133218     {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
 133219     {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
 133220     {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
 133221     {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
 133222     {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
 133223     {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
 133224     {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
 133225     {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
 133226     {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
 133227     {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
 133228     {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
 133229     {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
 133230     {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
 133231     {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
 133232     {65313, 14, 26},       
 133234   static const unsigned short aiOff[] = {
 133235    1,     2,     8,     15,    16,    26,    28,    32,    
 133236    37,    38,    40,    48,    63,    64,    69,    71,    
 133237    79,    80,    116,   202,   203,   205,   206,   207,   
 133238    209,   210,   211,   213,   214,   217,   218,   219,   
 133239    775,   7264,  10792, 10795, 23228, 23256, 30204, 54721, 
 133240    54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274, 
 133241    57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406, 
 133242    65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, 
 133243    65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, 
 133244    65514, 65521, 65527, 65528, 65529, 
 133247   int ret = c;
 133249   assert( c>=0 );
 133250   assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
 133252   if( c<128 ){
 133253     if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
 133254   }else if( c<65536 ){
 133255     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
 133256     int iLo = 0;
 133257     int iRes = -1;
 133259     while( iHi>=iLo ){
 133260       int iTest = (iHi + iLo) / 2;
 133261       int cmp = (c - aEntry[iTest].iCode);
 133262       if( cmp>=0 ){
 133263         iRes = iTest;
 133264         iLo = iTest+1;
 133265       }else{
 133266         iHi = iTest-1;
 133269     assert( iRes<0 || c>=aEntry[iRes].iCode );
 133271     if( iRes>=0 ){
 133272       const struct TableEntry *p = &aEntry[iRes];
 133273       if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
 133274         ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
 133275         assert( ret>0 );
 133279     if( bRemoveDiacritic ) ret = remove_diacritic(ret);
 133282   else if( c>=66560 && c<66600 ){
 133283     ret = c + 40;
 133286   return ret;
 133288 #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
 133289 #endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
 133291 /************** End of fts3_unicode2.c ***************************************/
 133292 /************** Begin file rtree.c *******************************************/
 133294 ** 2001 September 15
 133296 ** The author disclaims copyright to this source code.  In place of
 133297 ** a legal notice, here is a blessing:
 133299 **    May you do good and not evil.
 133300 **    May you find forgiveness for yourself and forgive others.
 133301 **    May you share freely, never taking more than you give.
 133303 *************************************************************************
 133304 ** This file contains code for implementations of the r-tree and r*-tree
 133305 ** algorithms packaged as an SQLite virtual table module.
 133309 ** Database Format of R-Tree Tables
 133310 ** --------------------------------
 133312 ** The data structure for a single virtual r-tree table is stored in three 
 133313 ** native SQLite tables declared as follows. In each case, the '%' character
 133314 ** in the table name is replaced with the user-supplied name of the r-tree
 133315 ** table.
 133317 **   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
 133318 **   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
 133319 **   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
 133321 ** The data for each node of the r-tree structure is stored in the %_node
 133322 ** table. For each node that is not the root node of the r-tree, there is
 133323 ** an entry in the %_parent table associating the node with its parent.
 133324 ** And for each row of data in the table, there is an entry in the %_rowid
 133325 ** table that maps from the entries rowid to the id of the node that it
 133326 ** is stored on.
 133328 ** The root node of an r-tree always exists, even if the r-tree table is
 133329 ** empty. The nodeno of the root node is always 1. All other nodes in the
 133330 ** table must be the same size as the root node. The content of each node
 133331 ** is formatted as follows:
 133333 **   1. If the node is the root node (node 1), then the first 2 bytes
 133334 **      of the node contain the tree depth as a big-endian integer.
 133335 **      For non-root nodes, the first 2 bytes are left unused.
 133337 **   2. The next 2 bytes contain the number of entries currently 
 133338 **      stored in the node.
 133340 **   3. The remainder of the node contains the node entries. Each entry
 133341 **      consists of a single 8-byte integer followed by an even number
 133342 **      of 4-byte coordinates. For leaf nodes the integer is the rowid
 133343 **      of a record. For internal nodes it is the node number of a
 133344 **      child page.
 133347 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
 133350 ** This file contains an implementation of a couple of different variants
 133351 ** of the r-tree algorithm. See the README file for further details. The 
 133352 ** same data-structure is used for all, but the algorithms for insert and
 133353 ** delete operations vary. The variants used are selected at compile time 
 133354 ** by defining the following symbols:
 133357 /* Either, both or none of the following may be set to activate 
 133358 ** r*tree variant algorithms.
 133360 #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
 133361 #define VARIANT_RSTARTREE_REINSERT      1
 133364 ** Exactly one of the following must be set to 1.
 133366 #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
 133367 #define VARIANT_GUTTMAN_LINEAR_SPLIT    0
 133368 #define VARIANT_RSTARTREE_SPLIT         1
 133370 #define VARIANT_GUTTMAN_SPLIT \
 133371         (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
 133373 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
 133374   #define PickNext QuadraticPickNext
 133375   #define PickSeeds QuadraticPickSeeds
 133376   #define AssignCells splitNodeGuttman
 133377 #endif
 133378 #if VARIANT_GUTTMAN_LINEAR_SPLIT
 133379   #define PickNext LinearPickNext
 133380   #define PickSeeds LinearPickSeeds
 133381   #define AssignCells splitNodeGuttman
 133382 #endif
 133383 #if VARIANT_RSTARTREE_SPLIT
 133384   #define AssignCells splitNodeStartree
 133385 #endif
 133387 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
 133388 # define NDEBUG 1
 133389 #endif
 133391 #ifndef SQLITE_CORE
 133392   SQLITE_EXTENSION_INIT1
 133393 #else
 133394 #endif
 133396 /* #include <string.h> */
 133397 /* #include <assert.h> */
 133399 #ifndef SQLITE_AMALGAMATION
 133400 #include "sqlite3rtree.h"
 133401 typedef sqlite3_int64 i64;
 133402 typedef unsigned char u8;
 133403 typedef unsigned int u32;
 133404 #endif
 133406 /*  The following macro is used to suppress compiler warnings.
 133408 #ifndef UNUSED_PARAMETER
 133409 # define UNUSED_PARAMETER(x) (void)(x)
 133410 #endif
 133412 typedef struct Rtree Rtree;
 133413 typedef struct RtreeCursor RtreeCursor;
 133414 typedef struct RtreeNode RtreeNode;
 133415 typedef struct RtreeCell RtreeCell;
 133416 typedef struct RtreeConstraint RtreeConstraint;
 133417 typedef struct RtreeMatchArg RtreeMatchArg;
 133418 typedef struct RtreeGeomCallback RtreeGeomCallback;
 133419 typedef union RtreeCoord RtreeCoord;
 133421 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
 133422 #define RTREE_MAX_DIMENSIONS 5
 133424 /* Size of hash table Rtree.aHash. This hash table is not expected to
 133425 ** ever contain very many entries, so a fixed number of buckets is 
 133426 ** used.
 133428 #define HASHSIZE 128
 133431 ** An rtree virtual-table object.
 133433 struct Rtree {
 133434   sqlite3_vtab base;
 133435   sqlite3 *db;                /* Host database connection */
 133436   int iNodeSize;              /* Size in bytes of each node in the node table */
 133437   int nDim;                   /* Number of dimensions */
 133438   int nBytesPerCell;          /* Bytes consumed per cell */
 133439   int iDepth;                 /* Current depth of the r-tree structure */
 133440   char *zDb;                  /* Name of database containing r-tree table */
 133441   char *zName;                /* Name of r-tree table */ 
 133442   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
 133443   int nBusy;                  /* Current number of users of this structure */
 133445   /* List of nodes removed during a CondenseTree operation. List is
 133446   ** linked together via the pointer normally used for hash chains -
 133447   ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
 133448   ** headed by the node (leaf nodes have RtreeNode.iNode==0).
 133450   RtreeNode *pDeleted;
 133451   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
 133453   /* Statements to read/write/delete a record from xxx_node */
 133454   sqlite3_stmt *pReadNode;
 133455   sqlite3_stmt *pWriteNode;
 133456   sqlite3_stmt *pDeleteNode;
 133458   /* Statements to read/write/delete a record from xxx_rowid */
 133459   sqlite3_stmt *pReadRowid;
 133460   sqlite3_stmt *pWriteRowid;
 133461   sqlite3_stmt *pDeleteRowid;
 133463   /* Statements to read/write/delete a record from xxx_parent */
 133464   sqlite3_stmt *pReadParent;
 133465   sqlite3_stmt *pWriteParent;
 133466   sqlite3_stmt *pDeleteParent;
 133468   int eCoordType;
 133471 /* Possible values for eCoordType: */
 133472 #define RTREE_COORD_REAL32 0
 133473 #define RTREE_COORD_INT32  1
 133476 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
 133477 ** only deal with integer coordinates.  No floating point operations
 133478 ** will be done.
 133480 #ifdef SQLITE_RTREE_INT_ONLY
 133481   typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
 133482   typedef int RtreeValue;                  /* Low accuracy coordinate */
 133483 #else
 133484   typedef double RtreeDValue;              /* High accuracy coordinate */
 133485   typedef float RtreeValue;                /* Low accuracy coordinate */
 133486 #endif
 133489 ** The minimum number of cells allowed for a node is a third of the 
 133490 ** maximum. In Gutman's notation:
 133492 **     m = M/3
 133494 ** If an R*-tree "Reinsert" operation is required, the same number of
 133495 ** cells are removed from the overfull node and reinserted into the tree.
 133497 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
 133498 #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
 133499 #define RTREE_MAXCELLS 51
 133502 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
 133503 ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
 133504 ** Therefore all non-root nodes must contain at least 3 entries. Since 
 133505 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
 133506 ** 40 or less.
 133508 #define RTREE_MAX_DEPTH 40
 133511 ** An rtree cursor object.
 133513 struct RtreeCursor {
 133514   sqlite3_vtab_cursor base;
 133515   RtreeNode *pNode;                 /* Node cursor is currently pointing at */
 133516   int iCell;                        /* Index of current cell in pNode */
 133517   int iStrategy;                    /* Copy of idxNum search parameter */
 133518   int nConstraint;                  /* Number of entries in aConstraint */
 133519   RtreeConstraint *aConstraint;     /* Search constraints. */
 133522 union RtreeCoord {
 133523   RtreeValue f;
 133524   int i;
 133528 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
 133529 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
 133530 ** variable pRtree points to the Rtree structure associated with the
 133531 ** RtreeCoord.
 133533 #ifdef SQLITE_RTREE_INT_ONLY
 133534 # define DCOORD(coord) ((RtreeDValue)coord.i)
 133535 #else
 133536 # define DCOORD(coord) (                           \
 133537     (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
 133538       ((double)coord.f) :                           \
 133539       ((double)coord.i)                             \
 133541 #endif
 133544 ** A search constraint.
 133546 struct RtreeConstraint {
 133547   int iCoord;                     /* Index of constrained coordinate */
 133548   int op;                         /* Constraining operation */
 133549   RtreeDValue rValue;             /* Constraint value. */
 133550   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
 133551   sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
 133554 /* Possible values for RtreeConstraint.op */
 133555 #define RTREE_EQ    0x41
 133556 #define RTREE_LE    0x42
 133557 #define RTREE_LT    0x43
 133558 #define RTREE_GE    0x44
 133559 #define RTREE_GT    0x45
 133560 #define RTREE_MATCH 0x46
 133563 ** An rtree structure node.
 133565 struct RtreeNode {
 133566   RtreeNode *pParent;               /* Parent node */
 133567   i64 iNode;
 133568   int nRef;
 133569   int isDirty;
 133570   u8 *zData;
 133571   RtreeNode *pNext;                 /* Next node in this hash chain */
 133573 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
 133576 ** Structure to store a deserialized rtree record.
 133578 struct RtreeCell {
 133579   i64 iRowid;
 133580   RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
 133585 ** Value for the first field of every RtreeMatchArg object. The MATCH
 133586 ** operator tests that the first field of a blob operand matches this
 133587 ** value to avoid operating on invalid blobs (which could cause a segfault).
 133589 #define RTREE_GEOMETRY_MAGIC 0x891245AB
 133592 ** An instance of this structure must be supplied as a blob argument to
 133593 ** the right-hand-side of an SQL MATCH operator used to constrain an
 133594 ** r-tree query.
 133596 struct RtreeMatchArg {
 133597   u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
 133598   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
 133599   void *pContext;
 133600   int nParam;
 133601   RtreeDValue aParam[1];
 133605 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
 133606 ** a single instance of the following structure is allocated. It is used
 133607 ** as the context for the user-function created by by s_r_g_c(). The object
 133608 ** is eventually deleted by the destructor mechanism provided by
 133609 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
 133610 ** the geometry callback function).
 133612 struct RtreeGeomCallback {
 133613   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
 133614   void *pContext;
 133617 #ifndef MAX
 133618 # define MAX(x,y) ((x) < (y) ? (y) : (x))
 133619 #endif
 133620 #ifndef MIN
 133621 # define MIN(x,y) ((x) > (y) ? (y) : (x))
 133622 #endif
 133625 ** Functions to deserialize a 16 bit integer, 32 bit real number and
 133626 ** 64 bit integer. The deserialized value is returned.
 133628 static int readInt16(u8 *p){
 133629   return (p[0]<<8) + p[1];
 133631 static void readCoord(u8 *p, RtreeCoord *pCoord){
 133632   u32 i = (
 133633     (((u32)p[0]) << 24) + 
 133634     (((u32)p[1]) << 16) + 
 133635     (((u32)p[2]) <<  8) + 
 133636     (((u32)p[3]) <<  0)
 133638   *(u32 *)pCoord = i;
 133640 static i64 readInt64(u8 *p){
 133641   return (
 133642     (((i64)p[0]) << 56) + 
 133643     (((i64)p[1]) << 48) + 
 133644     (((i64)p[2]) << 40) + 
 133645     (((i64)p[3]) << 32) + 
 133646     (((i64)p[4]) << 24) + 
 133647     (((i64)p[5]) << 16) + 
 133648     (((i64)p[6]) <<  8) + 
 133649     (((i64)p[7]) <<  0)
 133654 ** Functions to serialize a 16 bit integer, 32 bit real number and
 133655 ** 64 bit integer. The value returned is the number of bytes written
 133656 ** to the argument buffer (always 2, 4 and 8 respectively).
 133658 static int writeInt16(u8 *p, int i){
 133659   p[0] = (i>> 8)&0xFF;
 133660   p[1] = (i>> 0)&0xFF;
 133661   return 2;
 133663 static int writeCoord(u8 *p, RtreeCoord *pCoord){
 133664   u32 i;
 133665   assert( sizeof(RtreeCoord)==4 );
 133666   assert( sizeof(u32)==4 );
 133667   i = *(u32 *)pCoord;
 133668   p[0] = (i>>24)&0xFF;
 133669   p[1] = (i>>16)&0xFF;
 133670   p[2] = (i>> 8)&0xFF;
 133671   p[3] = (i>> 0)&0xFF;
 133672   return 4;
 133674 static int writeInt64(u8 *p, i64 i){
 133675   p[0] = (i>>56)&0xFF;
 133676   p[1] = (i>>48)&0xFF;
 133677   p[2] = (i>>40)&0xFF;
 133678   p[3] = (i>>32)&0xFF;
 133679   p[4] = (i>>24)&0xFF;
 133680   p[5] = (i>>16)&0xFF;
 133681   p[6] = (i>> 8)&0xFF;
 133682   p[7] = (i>> 0)&0xFF;
 133683   return 8;
 133687 ** Increment the reference count of node p.
 133689 static void nodeReference(RtreeNode *p){
 133690   if( p ){
 133691     p->nRef++;
 133696 ** Clear the content of node p (set all bytes to 0x00).
 133698 static void nodeZero(Rtree *pRtree, RtreeNode *p){
 133699   memset(&p->zData[2], 0, pRtree->iNodeSize-2);
 133700   p->isDirty = 1;
 133704 ** Given a node number iNode, return the corresponding key to use
 133705 ** in the Rtree.aHash table.
 133707 static int nodeHash(i64 iNode){
 133708   return (
 133709     (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ 
 133710     (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
 133711   ) % HASHSIZE;
 133715 ** Search the node hash table for node iNode. If found, return a pointer
 133716 ** to it. Otherwise, return 0.
 133718 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
 133719   RtreeNode *p;
 133720   for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
 133721   return p;
 133725 ** Add node pNode to the node hash table.
 133727 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
 133728   int iHash;
 133729   assert( pNode->pNext==0 );
 133730   iHash = nodeHash(pNode->iNode);
 133731   pNode->pNext = pRtree->aHash[iHash];
 133732   pRtree->aHash[iHash] = pNode;
 133736 ** Remove node pNode from the node hash table.
 133738 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
 133739   RtreeNode **pp;
 133740   if( pNode->iNode!=0 ){
 133741     pp = &pRtree->aHash[nodeHash(pNode->iNode)];
 133742     for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
 133743     *pp = pNode->pNext;
 133744     pNode->pNext = 0;
 133749 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
 133750 ** indicating that node has not yet been assigned a node number. It is
 133751 ** assigned a node number when nodeWrite() is called to write the
 133752 ** node contents out to the database.
 133754 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
 133755   RtreeNode *pNode;
 133756   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
 133757   if( pNode ){
 133758     memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
 133759     pNode->zData = (u8 *)&pNode[1];
 133760     pNode->nRef = 1;
 133761     pNode->pParent = pParent;
 133762     pNode->isDirty = 1;
 133763     nodeReference(pParent);
 133765   return pNode;
 133769 ** Obtain a reference to an r-tree node.
 133771 static int
 133772 nodeAcquire(
 133773   Rtree *pRtree,             /* R-tree structure */
 133774   i64 iNode,                 /* Node number to load */
 133775   RtreeNode *pParent,        /* Either the parent node or NULL */
 133776   RtreeNode **ppNode         /* OUT: Acquired node */
 133778   int rc;
 133779   int rc2 = SQLITE_OK;
 133780   RtreeNode *pNode;
 133782   /* Check if the requested node is already in the hash table. If so,
 133783   ** increase its reference count and return it.
 133785   if( (pNode = nodeHashLookup(pRtree, iNode)) ){
 133786     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
 133787     if( pParent && !pNode->pParent ){
 133788       nodeReference(pParent);
 133789       pNode->pParent = pParent;
 133791     pNode->nRef++;
 133792     *ppNode = pNode;
 133793     return SQLITE_OK;
 133796   sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
 133797   rc = sqlite3_step(pRtree->pReadNode);
 133798   if( rc==SQLITE_ROW ){
 133799     const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
 133800     if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
 133801       pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
 133802       if( !pNode ){
 133803         rc2 = SQLITE_NOMEM;
 133804       }else{
 133805         pNode->pParent = pParent;
 133806         pNode->zData = (u8 *)&pNode[1];
 133807         pNode->nRef = 1;
 133808         pNode->iNode = iNode;
 133809         pNode->isDirty = 0;
 133810         pNode->pNext = 0;
 133811         memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
 133812         nodeReference(pParent);
 133816   rc = sqlite3_reset(pRtree->pReadNode);
 133817   if( rc==SQLITE_OK ) rc = rc2;
 133819   /* If the root node was just loaded, set pRtree->iDepth to the height
 133820   ** of the r-tree structure. A height of zero means all data is stored on
 133821   ** the root node. A height of one means the children of the root node
 133822   ** are the leaves, and so on. If the depth as specified on the root node
 133823   ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
 133825   if( pNode && iNode==1 ){
 133826     pRtree->iDepth = readInt16(pNode->zData);
 133827     if( pRtree->iDepth>RTREE_MAX_DEPTH ){
 133828       rc = SQLITE_CORRUPT_VTAB;
 133832   /* If no error has occurred so far, check if the "number of entries"
 133833   ** field on the node is too large. If so, set the return code to 
 133834   ** SQLITE_CORRUPT_VTAB.
 133836   if( pNode && rc==SQLITE_OK ){
 133837     if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
 133838       rc = SQLITE_CORRUPT_VTAB;
 133842   if( rc==SQLITE_OK ){
 133843     if( pNode!=0 ){
 133844       nodeHashInsert(pRtree, pNode);
 133845     }else{
 133846       rc = SQLITE_CORRUPT_VTAB;
 133848     *ppNode = pNode;
 133849   }else{
 133850     sqlite3_free(pNode);
 133851     *ppNode = 0;
 133854   return rc;
 133858 ** Overwrite cell iCell of node pNode with the contents of pCell.
 133860 static void nodeOverwriteCell(
 133861   Rtree *pRtree, 
 133862   RtreeNode *pNode,  
 133863   RtreeCell *pCell, 
 133864   int iCell
 133866   int ii;
 133867   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
 133868   p += writeInt64(p, pCell->iRowid);
 133869   for(ii=0; ii<(pRtree->nDim*2); ii++){
 133870     p += writeCoord(p, &pCell->aCoord[ii]);
 133872   pNode->isDirty = 1;
 133876 ** Remove cell the cell with index iCell from node pNode.
 133878 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
 133879   u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
 133880   u8 *pSrc = &pDst[pRtree->nBytesPerCell];
 133881   int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
 133882   memmove(pDst, pSrc, nByte);
 133883   writeInt16(&pNode->zData[2], NCELL(pNode)-1);
 133884   pNode->isDirty = 1;
 133888 ** Insert the contents of cell pCell into node pNode. If the insert
 133889 ** is successful, return SQLITE_OK.
 133891 ** If there is not enough free space in pNode, return SQLITE_FULL.
 133893 static int
 133894 nodeInsertCell(
 133895   Rtree *pRtree, 
 133896   RtreeNode *pNode, 
 133897   RtreeCell *pCell 
 133899   int nCell;                    /* Current number of cells in pNode */
 133900   int nMaxCell;                 /* Maximum number of cells for pNode */
 133902   nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
 133903   nCell = NCELL(pNode);
 133905   assert( nCell<=nMaxCell );
 133906   if( nCell<nMaxCell ){
 133907     nodeOverwriteCell(pRtree, pNode, pCell, nCell);
 133908     writeInt16(&pNode->zData[2], nCell+1);
 133909     pNode->isDirty = 1;
 133912   return (nCell==nMaxCell);
 133916 ** If the node is dirty, write it out to the database.
 133918 static int
 133919 nodeWrite(Rtree *pRtree, RtreeNode *pNode){
 133920   int rc = SQLITE_OK;
 133921   if( pNode->isDirty ){
 133922     sqlite3_stmt *p = pRtree->pWriteNode;
 133923     if( pNode->iNode ){
 133924       sqlite3_bind_int64(p, 1, pNode->iNode);
 133925     }else{
 133926       sqlite3_bind_null(p, 1);
 133928     sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
 133929     sqlite3_step(p);
 133930     pNode->isDirty = 0;
 133931     rc = sqlite3_reset(p);
 133932     if( pNode->iNode==0 && rc==SQLITE_OK ){
 133933       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
 133934       nodeHashInsert(pRtree, pNode);
 133937   return rc;
 133941 ** Release a reference to a node. If the node is dirty and the reference
 133942 ** count drops to zero, the node data is written to the database.
 133944 static int
 133945 nodeRelease(Rtree *pRtree, RtreeNode *pNode){
 133946   int rc = SQLITE_OK;
 133947   if( pNode ){
 133948     assert( pNode->nRef>0 );
 133949     pNode->nRef--;
 133950     if( pNode->nRef==0 ){
 133951       if( pNode->iNode==1 ){
 133952         pRtree->iDepth = -1;
 133954       if( pNode->pParent ){
 133955         rc = nodeRelease(pRtree, pNode->pParent);
 133957       if( rc==SQLITE_OK ){
 133958         rc = nodeWrite(pRtree, pNode);
 133960       nodeHashDelete(pRtree, pNode);
 133961       sqlite3_free(pNode);
 133964   return rc;
 133968 ** Return the 64-bit integer value associated with cell iCell of
 133969 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
 133970 ** an internal node, then the 64-bit integer is a child page number.
 133972 static i64 nodeGetRowid(
 133973   Rtree *pRtree, 
 133974   RtreeNode *pNode, 
 133975   int iCell
 133977   assert( iCell<NCELL(pNode) );
 133978   return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
 133982 ** Return coordinate iCoord from cell iCell in node pNode.
 133984 static void nodeGetCoord(
 133985   Rtree *pRtree, 
 133986   RtreeNode *pNode, 
 133987   int iCell,
 133988   int iCoord,
 133989   RtreeCoord *pCoord           /* Space to write result to */
 133991   readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
 133995 ** Deserialize cell iCell of node pNode. Populate the structure pointed
 133996 ** to by pCell with the results.
 133998 static void nodeGetCell(
 133999   Rtree *pRtree, 
 134000   RtreeNode *pNode, 
 134001   int iCell,
 134002   RtreeCell *pCell
 134004   int ii;
 134005   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
 134006   for(ii=0; ii<pRtree->nDim*2; ii++){
 134007     nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
 134012 /* Forward declaration for the function that does the work of
 134013 ** the virtual table module xCreate() and xConnect() methods.
 134015 static int rtreeInit(
 134016   sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
 134020 ** Rtree virtual table module xCreate method.
 134022 static int rtreeCreate(
 134023   sqlite3 *db,
 134024   void *pAux,
 134025   int argc, const char *const*argv,
 134026   sqlite3_vtab **ppVtab,
 134027   char **pzErr
 134029   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
 134033 ** Rtree virtual table module xConnect method.
 134035 static int rtreeConnect(
 134036   sqlite3 *db,
 134037   void *pAux,
 134038   int argc, const char *const*argv,
 134039   sqlite3_vtab **ppVtab,
 134040   char **pzErr
 134042   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
 134046 ** Increment the r-tree reference count.
 134048 static void rtreeReference(Rtree *pRtree){
 134049   pRtree->nBusy++;
 134053 ** Decrement the r-tree reference count. When the reference count reaches
 134054 ** zero the structure is deleted.
 134056 static void rtreeRelease(Rtree *pRtree){
 134057   pRtree->nBusy--;
 134058   if( pRtree->nBusy==0 ){
 134059     sqlite3_finalize(pRtree->pReadNode);
 134060     sqlite3_finalize(pRtree->pWriteNode);
 134061     sqlite3_finalize(pRtree->pDeleteNode);
 134062     sqlite3_finalize(pRtree->pReadRowid);
 134063     sqlite3_finalize(pRtree->pWriteRowid);
 134064     sqlite3_finalize(pRtree->pDeleteRowid);
 134065     sqlite3_finalize(pRtree->pReadParent);
 134066     sqlite3_finalize(pRtree->pWriteParent);
 134067     sqlite3_finalize(pRtree->pDeleteParent);
 134068     sqlite3_free(pRtree);
 134073 ** Rtree virtual table module xDisconnect method.
 134075 static int rtreeDisconnect(sqlite3_vtab *pVtab){
 134076   rtreeRelease((Rtree *)pVtab);
 134077   return SQLITE_OK;
 134081 ** Rtree virtual table module xDestroy method.
 134083 static int rtreeDestroy(sqlite3_vtab *pVtab){
 134084   Rtree *pRtree = (Rtree *)pVtab;
 134085   int rc;
 134086   char *zCreate = sqlite3_mprintf(
 134087     "DROP TABLE '%q'.'%q_node';"
 134088     "DROP TABLE '%q'.'%q_rowid';"
 134089     "DROP TABLE '%q'.'%q_parent';",
 134090     pRtree->zDb, pRtree->zName, 
 134091     pRtree->zDb, pRtree->zName,
 134092     pRtree->zDb, pRtree->zName
 134094   if( !zCreate ){
 134095     rc = SQLITE_NOMEM;
 134096   }else{
 134097     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
 134098     sqlite3_free(zCreate);
 134100   if( rc==SQLITE_OK ){
 134101     rtreeRelease(pRtree);
 134104   return rc;
 134108 ** Rtree virtual table module xOpen method.
 134110 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
 134111   int rc = SQLITE_NOMEM;
 134112   RtreeCursor *pCsr;
 134114   pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
 134115   if( pCsr ){
 134116     memset(pCsr, 0, sizeof(RtreeCursor));
 134117     pCsr->base.pVtab = pVTab;
 134118     rc = SQLITE_OK;
 134120   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
 134122   return rc;
 134127 ** Free the RtreeCursor.aConstraint[] array and its contents.
 134129 static void freeCursorConstraints(RtreeCursor *pCsr){
 134130   if( pCsr->aConstraint ){
 134131     int i;                        /* Used to iterate through constraint array */
 134132     for(i=0; i<pCsr->nConstraint; i++){
 134133       sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
 134134       if( pGeom ){
 134135         if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
 134136         sqlite3_free(pGeom);
 134139     sqlite3_free(pCsr->aConstraint);
 134140     pCsr->aConstraint = 0;
 134145 ** Rtree virtual table module xClose method.
 134147 static int rtreeClose(sqlite3_vtab_cursor *cur){
 134148   Rtree *pRtree = (Rtree *)(cur->pVtab);
 134149   int rc;
 134150   RtreeCursor *pCsr = (RtreeCursor *)cur;
 134151   freeCursorConstraints(pCsr);
 134152   rc = nodeRelease(pRtree, pCsr->pNode);
 134153   sqlite3_free(pCsr);
 134154   return rc;
 134158 ** Rtree virtual table module xEof method.
 134160 ** Return non-zero if the cursor does not currently point to a valid 
 134161 ** record (i.e if the scan has finished), or zero otherwise.
 134163 static int rtreeEof(sqlite3_vtab_cursor *cur){
 134164   RtreeCursor *pCsr = (RtreeCursor *)cur;
 134165   return (pCsr->pNode==0);
 134169 ** The r-tree constraint passed as the second argument to this function is
 134170 ** guaranteed to be a MATCH constraint.
 134172 static int testRtreeGeom(
 134173   Rtree *pRtree,                  /* R-Tree object */
 134174   RtreeConstraint *pConstraint,   /* MATCH constraint to test */
 134175   RtreeCell *pCell,               /* Cell to test */
 134176   int *pbRes                      /* OUT: Test result */
 134178   int i;
 134179   RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
 134180   int nCoord = pRtree->nDim*2;
 134182   assert( pConstraint->op==RTREE_MATCH );
 134183   assert( pConstraint->pGeom );
 134185   for(i=0; i<nCoord; i++){
 134186     aCoord[i] = DCOORD(pCell->aCoord[i]);
 134188   return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
 134192 ** Cursor pCursor currently points to a cell in a non-leaf page.
 134193 ** Set *pbEof to true if the sub-tree headed by the cell is filtered
 134194 ** (excluded) by the constraints in the pCursor->aConstraint[] 
 134195 ** array, or false otherwise.
 134197 ** Return SQLITE_OK if successful or an SQLite error code if an error
 134198 ** occurs within a geometry callback.
 134200 static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
 134201   RtreeCell cell;
 134202   int ii;
 134203   int bRes = 0;
 134204   int rc = SQLITE_OK;
 134206   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
 134207   for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
 134208     RtreeConstraint *p = &pCursor->aConstraint[ii];
 134209     RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
 134210     RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
 134212     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
 134213         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
 134216     switch( p->op ){
 134217       case RTREE_LE: case RTREE_LT: 
 134218         bRes = p->rValue<cell_min; 
 134219         break;
 134221       case RTREE_GE: case RTREE_GT: 
 134222         bRes = p->rValue>cell_max; 
 134223         break;
 134225       case RTREE_EQ:
 134226         bRes = (p->rValue>cell_max || p->rValue<cell_min);
 134227         break;
 134229       default: {
 134230         assert( p->op==RTREE_MATCH );
 134231         rc = testRtreeGeom(pRtree, p, &cell, &bRes);
 134232         bRes = !bRes;
 134233         break;
 134238   *pbEof = bRes;
 134239   return rc;
 134243 ** Test if the cell that cursor pCursor currently points to
 134244 ** would be filtered (excluded) by the constraints in the 
 134245 ** pCursor->aConstraint[] array. If so, set *pbEof to true before
 134246 ** returning. If the cell is not filtered (excluded) by the constraints,
 134247 ** set pbEof to zero.
 134249 ** Return SQLITE_OK if successful or an SQLite error code if an error
 134250 ** occurs within a geometry callback.
 134252 ** This function assumes that the cell is part of a leaf node.
 134254 static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
 134255   RtreeCell cell;
 134256   int ii;
 134257   *pbEof = 0;
 134259   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
 134260   for(ii=0; ii<pCursor->nConstraint; ii++){
 134261     RtreeConstraint *p = &pCursor->aConstraint[ii];
 134262     RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
 134263     int res;
 134264     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
 134265         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
 134267     switch( p->op ){
 134268       case RTREE_LE: res = (coord<=p->rValue); break;
 134269       case RTREE_LT: res = (coord<p->rValue);  break;
 134270       case RTREE_GE: res = (coord>=p->rValue); break;
 134271       case RTREE_GT: res = (coord>p->rValue);  break;
 134272       case RTREE_EQ: res = (coord==p->rValue); break;
 134273       default: {
 134274         int rc;
 134275         assert( p->op==RTREE_MATCH );
 134276         rc = testRtreeGeom(pRtree, p, &cell, &res);
 134277         if( rc!=SQLITE_OK ){
 134278           return rc;
 134280         break;
 134284     if( !res ){
 134285       *pbEof = 1;
 134286       return SQLITE_OK;
 134290   return SQLITE_OK;
 134294 ** Cursor pCursor currently points at a node that heads a sub-tree of
 134295 ** height iHeight (if iHeight==0, then the node is a leaf). Descend
 134296 ** to point to the left-most cell of the sub-tree that matches the 
 134297 ** configured constraints.
 134299 static int descendToCell(
 134300   Rtree *pRtree, 
 134301   RtreeCursor *pCursor, 
 134302   int iHeight,
 134303   int *pEof                 /* OUT: Set to true if cannot descend */
 134305   int isEof;
 134306   int rc;
 134307   int ii;
 134308   RtreeNode *pChild;
 134309   sqlite3_int64 iRowid;
 134311   RtreeNode *pSavedNode = pCursor->pNode;
 134312   int iSavedCell = pCursor->iCell;
 134314   assert( iHeight>=0 );
 134316   if( iHeight==0 ){
 134317     rc = testRtreeEntry(pRtree, pCursor, &isEof);
 134318   }else{
 134319     rc = testRtreeCell(pRtree, pCursor, &isEof);
 134321   if( rc!=SQLITE_OK || isEof || iHeight==0 ){
 134322     goto descend_to_cell_out;
 134325   iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
 134326   rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
 134327   if( rc!=SQLITE_OK ){
 134328     goto descend_to_cell_out;
 134331   nodeRelease(pRtree, pCursor->pNode);
 134332   pCursor->pNode = pChild;
 134333   isEof = 1;
 134334   for(ii=0; isEof && ii<NCELL(pChild); ii++){
 134335     pCursor->iCell = ii;
 134336     rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
 134337     if( rc!=SQLITE_OK ){
 134338       goto descend_to_cell_out;
 134342   if( isEof ){
 134343     assert( pCursor->pNode==pChild );
 134344     nodeReference(pSavedNode);
 134345     nodeRelease(pRtree, pChild);
 134346     pCursor->pNode = pSavedNode;
 134347     pCursor->iCell = iSavedCell;
 134350 descend_to_cell_out:
 134351   *pEof = isEof;
 134352   return rc;
 134356 ** One of the cells in node pNode is guaranteed to have a 64-bit 
 134357 ** integer value equal to iRowid. Return the index of this cell.
 134359 static int nodeRowidIndex(
 134360   Rtree *pRtree, 
 134361   RtreeNode *pNode, 
 134362   i64 iRowid,
 134363   int *piIndex
 134365   int ii;
 134366   int nCell = NCELL(pNode);
 134367   for(ii=0; ii<nCell; ii++){
 134368     if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
 134369       *piIndex = ii;
 134370       return SQLITE_OK;
 134373   return SQLITE_CORRUPT_VTAB;
 134377 ** Return the index of the cell containing a pointer to node pNode
 134378 ** in its parent. If pNode is the root node, return -1.
 134380 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
 134381   RtreeNode *pParent = pNode->pParent;
 134382   if( pParent ){
 134383     return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
 134385   *piIndex = -1;
 134386   return SQLITE_OK;
 134390 ** Rtree virtual table module xNext method.
 134392 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
 134393   Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
 134394   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
 134395   int rc = SQLITE_OK;
 134397   /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
 134398   ** already at EOF. It is against the rules to call the xNext() method of
 134399   ** a cursor that has already reached EOF.
 134401   assert( pCsr->pNode );
 134403   if( pCsr->iStrategy==1 ){
 134404     /* This "scan" is a direct lookup by rowid. There is no next entry. */
 134405     nodeRelease(pRtree, pCsr->pNode);
 134406     pCsr->pNode = 0;
 134407   }else{
 134408     /* Move to the next entry that matches the configured constraints. */
 134409     int iHeight = 0;
 134410     while( pCsr->pNode ){
 134411       RtreeNode *pNode = pCsr->pNode;
 134412       int nCell = NCELL(pNode);
 134413       for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
 134414         int isEof;
 134415         rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
 134416         if( rc!=SQLITE_OK || !isEof ){
 134417           return rc;
 134420       pCsr->pNode = pNode->pParent;
 134421       rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
 134422       if( rc!=SQLITE_OK ){
 134423         return rc;
 134425       nodeReference(pCsr->pNode);
 134426       nodeRelease(pRtree, pNode);
 134427       iHeight++;
 134431   return rc;
 134435 ** Rtree virtual table module xRowid method.
 134437 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
 134438   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
 134439   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
 134441   assert(pCsr->pNode);
 134442   *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
 134444   return SQLITE_OK;
 134448 ** Rtree virtual table module xColumn method.
 134450 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
 134451   Rtree *pRtree = (Rtree *)cur->pVtab;
 134452   RtreeCursor *pCsr = (RtreeCursor *)cur;
 134454   if( i==0 ){
 134455     i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
 134456     sqlite3_result_int64(ctx, iRowid);
 134457   }else{
 134458     RtreeCoord c;
 134459     nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
 134460 #ifndef SQLITE_RTREE_INT_ONLY
 134461     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
 134462       sqlite3_result_double(ctx, c.f);
 134463     }else
 134464 #endif
 134466       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
 134467       sqlite3_result_int(ctx, c.i);
 134471   return SQLITE_OK;
 134475 ** Use nodeAcquire() to obtain the leaf node containing the record with 
 134476 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
 134477 ** return SQLITE_OK. If there is no such record in the table, set
 134478 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
 134479 ** to zero and return an SQLite error code.
 134481 static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
 134482   int rc;
 134483   *ppLeaf = 0;
 134484   sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
 134485   if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
 134486     i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
 134487     rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
 134488     sqlite3_reset(pRtree->pReadRowid);
 134489   }else{
 134490     rc = sqlite3_reset(pRtree->pReadRowid);
 134492   return rc;
 134496 ** This function is called to configure the RtreeConstraint object passed
 134497 ** as the second argument for a MATCH constraint. The value passed as the
 134498 ** first argument to this function is the right-hand operand to the MATCH
 134499 ** operator.
 134501 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
 134502   RtreeMatchArg *p;
 134503   sqlite3_rtree_geometry *pGeom;
 134504   int nBlob;
 134506   /* Check that value is actually a blob. */
 134507   if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
 134509   /* Check that the blob is roughly the right size. */
 134510   nBlob = sqlite3_value_bytes(pValue);
 134511   if( nBlob<(int)sizeof(RtreeMatchArg) 
 134512    || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
 134514     return SQLITE_ERROR;
 134517   pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
 134518       sizeof(sqlite3_rtree_geometry) + nBlob
 134520   if( !pGeom ) return SQLITE_NOMEM;
 134521   memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
 134522   p = (RtreeMatchArg *)&pGeom[1];
 134524   memcpy(p, sqlite3_value_blob(pValue), nBlob);
 134525   if( p->magic!=RTREE_GEOMETRY_MAGIC 
 134526    || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
 134528     sqlite3_free(pGeom);
 134529     return SQLITE_ERROR;
 134532   pGeom->pContext = p->pContext;
 134533   pGeom->nParam = p->nParam;
 134534   pGeom->aParam = p->aParam;
 134536   pCons->xGeom = p->xGeom;
 134537   pCons->pGeom = pGeom;
 134538   return SQLITE_OK;
 134542 ** Rtree virtual table module xFilter method.
 134544 static int rtreeFilter(
 134545   sqlite3_vtab_cursor *pVtabCursor, 
 134546   int idxNum, const char *idxStr,
 134547   int argc, sqlite3_value **argv
 134549   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
 134550   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
 134552   RtreeNode *pRoot = 0;
 134553   int ii;
 134554   int rc = SQLITE_OK;
 134556   rtreeReference(pRtree);
 134558   freeCursorConstraints(pCsr);
 134559   pCsr->iStrategy = idxNum;
 134561   if( idxNum==1 ){
 134562     /* Special case - lookup by rowid. */
 134563     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
 134564     i64 iRowid = sqlite3_value_int64(argv[0]);
 134565     rc = findLeafNode(pRtree, iRowid, &pLeaf);
 134566     pCsr->pNode = pLeaf; 
 134567     if( pLeaf ){
 134568       assert( rc==SQLITE_OK );
 134569       rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
 134571   }else{
 134572     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
 134573     ** with the configured constraints. 
 134575     if( argc>0 ){
 134576       pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
 134577       pCsr->nConstraint = argc;
 134578       if( !pCsr->aConstraint ){
 134579         rc = SQLITE_NOMEM;
 134580       }else{
 134581         memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
 134582         assert( (idxStr==0 && argc==0)
 134583                 || (idxStr && (int)strlen(idxStr)==argc*2) );
 134584         for(ii=0; ii<argc; ii++){
 134585           RtreeConstraint *p = &pCsr->aConstraint[ii];
 134586           p->op = idxStr[ii*2];
 134587           p->iCoord = idxStr[ii*2+1]-'a';
 134588           if( p->op==RTREE_MATCH ){
 134589             /* A MATCH operator. The right-hand-side must be a blob that
 134590             ** can be cast into an RtreeMatchArg object. One created using
 134591             ** an sqlite3_rtree_geometry_callback() SQL user function.
 134593             rc = deserializeGeometry(argv[ii], p);
 134594             if( rc!=SQLITE_OK ){
 134595               break;
 134597           }else{
 134598 #ifdef SQLITE_RTREE_INT_ONLY
 134599             p->rValue = sqlite3_value_int64(argv[ii]);
 134600 #else
 134601             p->rValue = sqlite3_value_double(argv[ii]);
 134602 #endif
 134608     if( rc==SQLITE_OK ){
 134609       pCsr->pNode = 0;
 134610       rc = nodeAcquire(pRtree, 1, 0, &pRoot);
 134612     if( rc==SQLITE_OK ){
 134613       int isEof = 1;
 134614       int nCell = NCELL(pRoot);
 134615       pCsr->pNode = pRoot;
 134616       for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
 134617         assert( pCsr->pNode==pRoot );
 134618         rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
 134619         if( !isEof ){
 134620           break;
 134623       if( rc==SQLITE_OK && isEof ){
 134624         assert( pCsr->pNode==pRoot );
 134625         nodeRelease(pRtree, pRoot);
 134626         pCsr->pNode = 0;
 134628       assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
 134632   rtreeRelease(pRtree);
 134633   return rc;
 134637 ** Rtree virtual table module xBestIndex method. There are three
 134638 ** table scan strategies to choose from (in order from most to 
 134639 ** least desirable):
 134641 **   idxNum     idxStr        Strategy
 134642 **   ------------------------------------------------
 134643 **     1        Unused        Direct lookup by rowid.
 134644 **     2        See below     R-tree query or full-table scan.
 134645 **   ------------------------------------------------
 134647 ** If strategy 1 is used, then idxStr is not meaningful. If strategy
 134648 ** 2 is used, idxStr is formatted to contain 2 bytes for each 
 134649 ** constraint used. The first two bytes of idxStr correspond to 
 134650 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
 134651 ** (argvIndex==1) etc.
 134653 ** The first of each pair of bytes in idxStr identifies the constraint
 134654 ** operator as follows:
 134656 **   Operator    Byte Value
 134657 **   ----------------------
 134658 **      =        0x41 ('A')
 134659 **     <=        0x42 ('B')
 134660 **      <        0x43 ('C')
 134661 **     >=        0x44 ('D')
 134662 **      >        0x45 ('E')
 134663 **   MATCH       0x46 ('F')
 134664 **   ----------------------
 134666 ** The second of each pair of bytes identifies the coordinate column
 134667 ** to which the constraint applies. The leftmost coordinate column
 134668 ** is 'a', the second from the left 'b' etc.
 134670 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
 134671   int rc = SQLITE_OK;
 134672   int ii;
 134674   int iIdx = 0;
 134675   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
 134676   memset(zIdxStr, 0, sizeof(zIdxStr));
 134677   UNUSED_PARAMETER(tab);
 134679   assert( pIdxInfo->idxStr==0 );
 134680   for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
 134681     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
 134683     if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
 134684       /* We have an equality constraint on the rowid. Use strategy 1. */
 134685       int jj;
 134686       for(jj=0; jj<ii; jj++){
 134687         pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
 134688         pIdxInfo->aConstraintUsage[jj].omit = 0;
 134690       pIdxInfo->idxNum = 1;
 134691       pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
 134692       pIdxInfo->aConstraintUsage[jj].omit = 1;
 134694       /* This strategy involves a two rowid lookups on an B-Tree structures
 134695       ** and then a linear search of an R-Tree node. This should be 
 134696       ** considered almost as quick as a direct rowid lookup (for which 
 134697       ** sqlite uses an internal cost of 0.0).
 134699       pIdxInfo->estimatedCost = 10.0;
 134700       return SQLITE_OK;
 134703     if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
 134704       u8 op;
 134705       switch( p->op ){
 134706         case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
 134707         case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
 134708         case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
 134709         case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
 134710         case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
 134711         default:
 134712           assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
 134713           op = RTREE_MATCH; 
 134714           break;
 134716       zIdxStr[iIdx++] = op;
 134717       zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
 134718       pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
 134719       pIdxInfo->aConstraintUsage[ii].omit = 1;
 134723   pIdxInfo->idxNum = 2;
 134724   pIdxInfo->needToFreeIdxStr = 1;
 134725   if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
 134726     return SQLITE_NOMEM;
 134728   assert( iIdx>=0 );
 134729   pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
 134730   return rc;
 134734 ** Return the N-dimensional volumn of the cell stored in *p.
 134736 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
 134737   RtreeDValue area = (RtreeDValue)1;
 134738   int ii;
 134739   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 134740     area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
 134742   return area;
 134746 ** Return the margin length of cell p. The margin length is the sum
 134747 ** of the objects size in each dimension.
 134749 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
 134750   RtreeDValue margin = (RtreeDValue)0;
 134751   int ii;
 134752   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 134753     margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
 134755   return margin;
 134759 ** Store the union of cells p1 and p2 in p1.
 134761 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
 134762   int ii;
 134763   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
 134764     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 134765       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
 134766       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
 134768   }else{
 134769     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 134770       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
 134771       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
 134777 ** Return true if the area covered by p2 is a subset of the area covered
 134778 ** by p1. False otherwise.
 134780 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
 134781   int ii;
 134782   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
 134783   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 134784     RtreeCoord *a1 = &p1->aCoord[ii];
 134785     RtreeCoord *a2 = &p2->aCoord[ii];
 134786     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
 134787      || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
 134789       return 0;
 134792   return 1;
 134796 ** Return the amount cell p would grow by if it were unioned with pCell.
 134798 static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
 134799   RtreeDValue area;
 134800   RtreeCell cell;
 134801   memcpy(&cell, p, sizeof(RtreeCell));
 134802   area = cellArea(pRtree, &cell);
 134803   cellUnion(pRtree, &cell, pCell);
 134804   return (cellArea(pRtree, &cell)-area);
 134807 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
 134808 static RtreeDValue cellOverlap(
 134809   Rtree *pRtree, 
 134810   RtreeCell *p, 
 134811   RtreeCell *aCell, 
 134812   int nCell, 
 134813   int iExclude
 134815   int ii;
 134816   RtreeDValue overlap = 0.0;
 134817   for(ii=0; ii<nCell; ii++){
 134818 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 134819     if( ii!=iExclude )
 134820 #else
 134821     assert( iExclude==-1 );
 134822     UNUSED_PARAMETER(iExclude);
 134823 #endif
 134825       int jj;
 134826       RtreeDValue o = (RtreeDValue)1;
 134827       for(jj=0; jj<(pRtree->nDim*2); jj+=2){
 134828         RtreeDValue x1, x2;
 134830         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
 134831         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
 134833         if( x2<x1 ){
 134834           o = 0.0;
 134835           break;
 134836         }else{
 134837           o = o * (x2-x1);
 134840       overlap += o;
 134843   return overlap;
 134845 #endif
 134847 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 134848 static RtreeDValue cellOverlapEnlargement(
 134849   Rtree *pRtree, 
 134850   RtreeCell *p, 
 134851   RtreeCell *pInsert, 
 134852   RtreeCell *aCell, 
 134853   int nCell, 
 134854   int iExclude
 134856   RtreeDValue before, after;
 134857   before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
 134858   cellUnion(pRtree, p, pInsert);
 134859   after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
 134860   return (after-before);
 134862 #endif
 134866 ** This function implements the ChooseLeaf algorithm from Gutman[84].
 134867 ** ChooseSubTree in r*tree terminology.
 134869 static int ChooseLeaf(
 134870   Rtree *pRtree,               /* Rtree table */
 134871   RtreeCell *pCell,            /* Cell to insert into rtree */
 134872   int iHeight,                 /* Height of sub-tree rooted at pCell */
 134873   RtreeNode **ppLeaf           /* OUT: Selected leaf page */
 134875   int rc;
 134876   int ii;
 134877   RtreeNode *pNode;
 134878   rc = nodeAcquire(pRtree, 1, 0, &pNode);
 134880   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
 134881     int iCell;
 134882     sqlite3_int64 iBest = 0;
 134884     RtreeDValue fMinGrowth = 0.0;
 134885     RtreeDValue fMinArea = 0.0;
 134886 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 134887     RtreeDValue fMinOverlap = 0.0;
 134888     RtreeDValue overlap;
 134889 #endif
 134891     int nCell = NCELL(pNode);
 134892     RtreeCell cell;
 134893     RtreeNode *pChild;
 134895     RtreeCell *aCell = 0;
 134897 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 134898     if( ii==(pRtree->iDepth-1) ){
 134899       int jj;
 134900       aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
 134901       if( !aCell ){
 134902         rc = SQLITE_NOMEM;
 134903         nodeRelease(pRtree, pNode);
 134904         pNode = 0;
 134905         continue;
 134907       for(jj=0; jj<nCell; jj++){
 134908         nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
 134911 #endif
 134913     /* Select the child node which will be enlarged the least if pCell
 134914     ** is inserted into it. Resolve ties by choosing the entry with
 134915     ** the smallest area.
 134917     for(iCell=0; iCell<nCell; iCell++){
 134918       int bBest = 0;
 134919       RtreeDValue growth;
 134920       RtreeDValue area;
 134921       nodeGetCell(pRtree, pNode, iCell, &cell);
 134922       growth = cellGrowth(pRtree, &cell, pCell);
 134923       area = cellArea(pRtree, &cell);
 134925 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 134926       if( ii==(pRtree->iDepth-1) ){
 134927         overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
 134928       }else{
 134929         overlap = 0.0;
 134931       if( (iCell==0) 
 134932        || (overlap<fMinOverlap) 
 134933        || (overlap==fMinOverlap && growth<fMinGrowth)
 134934        || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
 134936         bBest = 1;
 134937         fMinOverlap = overlap;
 134939 #else
 134940       if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
 134941         bBest = 1;
 134943 #endif
 134944       if( bBest ){
 134945         fMinGrowth = growth;
 134946         fMinArea = area;
 134947         iBest = cell.iRowid;
 134951     sqlite3_free(aCell);
 134952     rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
 134953     nodeRelease(pRtree, pNode);
 134954     pNode = pChild;
 134957   *ppLeaf = pNode;
 134958   return rc;
 134962 ** A cell with the same content as pCell has just been inserted into
 134963 ** the node pNode. This function updates the bounding box cells in
 134964 ** all ancestor elements.
 134966 static int AdjustTree(
 134967   Rtree *pRtree,                    /* Rtree table */
 134968   RtreeNode *pNode,                 /* Adjust ancestry of this node. */
 134969   RtreeCell *pCell                  /* This cell was just inserted */
 134971   RtreeNode *p = pNode;
 134972   while( p->pParent ){
 134973     RtreeNode *pParent = p->pParent;
 134974     RtreeCell cell;
 134975     int iCell;
 134977     if( nodeParentIndex(pRtree, p, &iCell) ){
 134978       return SQLITE_CORRUPT_VTAB;
 134981     nodeGetCell(pRtree, pParent, iCell, &cell);
 134982     if( !cellContains(pRtree, &cell, pCell) ){
 134983       cellUnion(pRtree, &cell, pCell);
 134984       nodeOverwriteCell(pRtree, pParent, &cell, iCell);
 134987     p = pParent;
 134989   return SQLITE_OK;
 134993 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
 134995 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
 134996   sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
 134997   sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
 134998   sqlite3_step(pRtree->pWriteRowid);
 134999   return sqlite3_reset(pRtree->pWriteRowid);
 135003 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
 135005 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
 135006   sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
 135007   sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
 135008   sqlite3_step(pRtree->pWriteParent);
 135009   return sqlite3_reset(pRtree->pWriteParent);
 135012 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
 135014 #if VARIANT_GUTTMAN_LINEAR_SPLIT
 135016 ** Implementation of the linear variant of the PickNext() function from
 135017 ** Guttman[84].
 135019 static RtreeCell *LinearPickNext(
 135020   Rtree *pRtree,
 135021   RtreeCell *aCell, 
 135022   int nCell, 
 135023   RtreeCell *pLeftBox, 
 135024   RtreeCell *pRightBox,
 135025   int *aiUsed
 135027   int ii;
 135028   for(ii=0; aiUsed[ii]; ii++);
 135029   aiUsed[ii] = 1;
 135030   return &aCell[ii];
 135034 ** Implementation of the linear variant of the PickSeeds() function from
 135035 ** Guttman[84].
 135037 static void LinearPickSeeds(
 135038   Rtree *pRtree,
 135039   RtreeCell *aCell, 
 135040   int nCell, 
 135041   int *piLeftSeed, 
 135042   int *piRightSeed
 135044   int i;
 135045   int iLeftSeed = 0;
 135046   int iRightSeed = 1;
 135047   RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
 135049   /* Pick two "seed" cells from the array of cells. The algorithm used
 135050   ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
 135051   ** indices of the two seed cells in the array are stored in local
 135052   ** variables iLeftSeek and iRightSeed.
 135054   for(i=0; i<pRtree->nDim; i++){
 135055     RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
 135056     RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
 135057     RtreeDValue x3 = x1;
 135058     RtreeDValue x4 = x2;
 135059     int jj;
 135061     int iCellLeft = 0;
 135062     int iCellRight = 0;
 135064     for(jj=1; jj<nCell; jj++){
 135065       RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
 135066       RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
 135068       if( left<x1 ) x1 = left;
 135069       if( right>x4 ) x4 = right;
 135070       if( left>x3 ){
 135071         x3 = left;
 135072         iCellRight = jj;
 135074       if( right<x2 ){
 135075         x2 = right;
 135076         iCellLeft = jj;
 135080     if( x4!=x1 ){
 135081       RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
 135082       if( normalwidth>maxNormalInnerWidth ){
 135083         iLeftSeed = iCellLeft;
 135084         iRightSeed = iCellRight;
 135089   *piLeftSeed = iLeftSeed;
 135090   *piRightSeed = iRightSeed;
 135092 #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
 135094 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
 135096 ** Implementation of the quadratic variant of the PickNext() function from
 135097 ** Guttman[84].
 135099 static RtreeCell *QuadraticPickNext(
 135100   Rtree *pRtree,
 135101   RtreeCell *aCell, 
 135102   int nCell, 
 135103   RtreeCell *pLeftBox, 
 135104   RtreeCell *pRightBox,
 135105   int *aiUsed
 135107   #define FABS(a) ((a)<0.0?-1.0*(a):(a))
 135109   int iSelect = -1;
 135110   RtreeDValue fDiff;
 135111   int ii;
 135112   for(ii=0; ii<nCell; ii++){
 135113     if( aiUsed[ii]==0 ){
 135114       RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
 135115       RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
 135116       RtreeDValue diff = FABS(right-left);
 135117       if( iSelect<0 || diff>fDiff ){
 135118         fDiff = diff;
 135119         iSelect = ii;
 135123   aiUsed[iSelect] = 1;
 135124   return &aCell[iSelect];
 135128 ** Implementation of the quadratic variant of the PickSeeds() function from
 135129 ** Guttman[84].
 135131 static void QuadraticPickSeeds(
 135132   Rtree *pRtree,
 135133   RtreeCell *aCell, 
 135134   int nCell, 
 135135   int *piLeftSeed, 
 135136   int *piRightSeed
 135138   int ii;
 135139   int jj;
 135141   int iLeftSeed = 0;
 135142   int iRightSeed = 1;
 135143   RtreeDValue fWaste = 0.0;
 135145   for(ii=0; ii<nCell; ii++){
 135146     for(jj=ii+1; jj<nCell; jj++){
 135147       RtreeDValue right = cellArea(pRtree, &aCell[jj]);
 135148       RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
 135149       RtreeDValue waste = growth - right;
 135151       if( waste>fWaste ){
 135152         iLeftSeed = ii;
 135153         iRightSeed = jj;
 135154         fWaste = waste;
 135159   *piLeftSeed = iLeftSeed;
 135160   *piRightSeed = iRightSeed;
 135162 #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
 135165 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
 135166 ** nIdx. The aIdx array contains the set of integers from 0 to 
 135167 ** (nIdx-1) in no particular order. This function sorts the values
 135168 ** in aIdx according to the indexed values in aDistance. For
 135169 ** example, assuming the inputs:
 135171 **   aIdx      = { 0,   1,   2,   3 }
 135172 **   aDistance = { 5.0, 2.0, 7.0, 6.0 }
 135174 ** this function sets the aIdx array to contain:
 135176 **   aIdx      = { 0,   1,   2,   3 }
 135178 ** The aSpare array is used as temporary working space by the
 135179 ** sorting algorithm.
 135181 static void SortByDistance(
 135182   int *aIdx, 
 135183   int nIdx, 
 135184   RtreeDValue *aDistance, 
 135185   int *aSpare
 135187   if( nIdx>1 ){
 135188     int iLeft = 0;
 135189     int iRight = 0;
 135191     int nLeft = nIdx/2;
 135192     int nRight = nIdx-nLeft;
 135193     int *aLeft = aIdx;
 135194     int *aRight = &aIdx[nLeft];
 135196     SortByDistance(aLeft, nLeft, aDistance, aSpare);
 135197     SortByDistance(aRight, nRight, aDistance, aSpare);
 135199     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
 135200     aLeft = aSpare;
 135202     while( iLeft<nLeft || iRight<nRight ){
 135203       if( iLeft==nLeft ){
 135204         aIdx[iLeft+iRight] = aRight[iRight];
 135205         iRight++;
 135206       }else if( iRight==nRight ){
 135207         aIdx[iLeft+iRight] = aLeft[iLeft];
 135208         iLeft++;
 135209       }else{
 135210         RtreeDValue fLeft = aDistance[aLeft[iLeft]];
 135211         RtreeDValue fRight = aDistance[aRight[iRight]];
 135212         if( fLeft<fRight ){
 135213           aIdx[iLeft+iRight] = aLeft[iLeft];
 135214           iLeft++;
 135215         }else{
 135216           aIdx[iLeft+iRight] = aRight[iRight];
 135217           iRight++;
 135222 #if 0
 135223     /* Check that the sort worked */
 135225       int jj;
 135226       for(jj=1; jj<nIdx; jj++){
 135227         RtreeDValue left = aDistance[aIdx[jj-1]];
 135228         RtreeDValue right = aDistance[aIdx[jj]];
 135229         assert( left<=right );
 135232 #endif
 135237 ** Arguments aIdx, aCell and aSpare all point to arrays of size
 135238 ** nIdx. The aIdx array contains the set of integers from 0 to 
 135239 ** (nIdx-1) in no particular order. This function sorts the values
 135240 ** in aIdx according to dimension iDim of the cells in aCell. The
 135241 ** minimum value of dimension iDim is considered first, the
 135242 ** maximum used to break ties.
 135244 ** The aSpare array is used as temporary working space by the
 135245 ** sorting algorithm.
 135247 static void SortByDimension(
 135248   Rtree *pRtree,
 135249   int *aIdx, 
 135250   int nIdx, 
 135251   int iDim, 
 135252   RtreeCell *aCell, 
 135253   int *aSpare
 135255   if( nIdx>1 ){
 135257     int iLeft = 0;
 135258     int iRight = 0;
 135260     int nLeft = nIdx/2;
 135261     int nRight = nIdx-nLeft;
 135262     int *aLeft = aIdx;
 135263     int *aRight = &aIdx[nLeft];
 135265     SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
 135266     SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
 135268     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
 135269     aLeft = aSpare;
 135270     while( iLeft<nLeft || iRight<nRight ){
 135271       RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
 135272       RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
 135273       RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
 135274       RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
 135275       if( (iLeft!=nLeft) && ((iRight==nRight)
 135276        || (xleft1<xright1)
 135277        || (xleft1==xright1 && xleft2<xright2)
 135278       )){
 135279         aIdx[iLeft+iRight] = aLeft[iLeft];
 135280         iLeft++;
 135281       }else{
 135282         aIdx[iLeft+iRight] = aRight[iRight];
 135283         iRight++;
 135287 #if 0
 135288     /* Check that the sort worked */
 135290       int jj;
 135291       for(jj=1; jj<nIdx; jj++){
 135292         RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
 135293         RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
 135294         RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
 135295         RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
 135296         assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
 135299 #endif
 135303 #if VARIANT_RSTARTREE_SPLIT
 135305 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
 135307 static int splitNodeStartree(
 135308   Rtree *pRtree,
 135309   RtreeCell *aCell,
 135310   int nCell,
 135311   RtreeNode *pLeft,
 135312   RtreeNode *pRight,
 135313   RtreeCell *pBboxLeft,
 135314   RtreeCell *pBboxRight
 135316   int **aaSorted;
 135317   int *aSpare;
 135318   int ii;
 135320   int iBestDim = 0;
 135321   int iBestSplit = 0;
 135322   RtreeDValue fBestMargin = 0.0;
 135324   int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
 135326   aaSorted = (int **)sqlite3_malloc(nByte);
 135327   if( !aaSorted ){
 135328     return SQLITE_NOMEM;
 135331   aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
 135332   memset(aaSorted, 0, nByte);
 135333   for(ii=0; ii<pRtree->nDim; ii++){
 135334     int jj;
 135335     aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
 135336     for(jj=0; jj<nCell; jj++){
 135337       aaSorted[ii][jj] = jj;
 135339     SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
 135342   for(ii=0; ii<pRtree->nDim; ii++){
 135343     RtreeDValue margin = 0.0;
 135344     RtreeDValue fBestOverlap = 0.0;
 135345     RtreeDValue fBestArea = 0.0;
 135346     int iBestLeft = 0;
 135347     int nLeft;
 135349     for(
 135350       nLeft=RTREE_MINCELLS(pRtree); 
 135351       nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
 135352       nLeft++
 135354       RtreeCell left;
 135355       RtreeCell right;
 135356       int kk;
 135357       RtreeDValue overlap;
 135358       RtreeDValue area;
 135360       memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
 135361       memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
 135362       for(kk=1; kk<(nCell-1); kk++){
 135363         if( kk<nLeft ){
 135364           cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
 135365         }else{
 135366           cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
 135369       margin += cellMargin(pRtree, &left);
 135370       margin += cellMargin(pRtree, &right);
 135371       overlap = cellOverlap(pRtree, &left, &right, 1, -1);
 135372       area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
 135373       if( (nLeft==RTREE_MINCELLS(pRtree))
 135374        || (overlap<fBestOverlap)
 135375        || (overlap==fBestOverlap && area<fBestArea)
 135377         iBestLeft = nLeft;
 135378         fBestOverlap = overlap;
 135379         fBestArea = area;
 135383     if( ii==0 || margin<fBestMargin ){
 135384       iBestDim = ii;
 135385       fBestMargin = margin;
 135386       iBestSplit = iBestLeft;
 135390   memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
 135391   memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
 135392   for(ii=0; ii<nCell; ii++){
 135393     RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
 135394     RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
 135395     RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
 135396     nodeInsertCell(pRtree, pTarget, pCell);
 135397     cellUnion(pRtree, pBbox, pCell);
 135400   sqlite3_free(aaSorted);
 135401   return SQLITE_OK;
 135403 #endif
 135405 #if VARIANT_GUTTMAN_SPLIT
 135407 ** Implementation of the regular R-tree SplitNode from Guttman[1984].
 135409 static int splitNodeGuttman(
 135410   Rtree *pRtree,
 135411   RtreeCell *aCell,
 135412   int nCell,
 135413   RtreeNode *pLeft,
 135414   RtreeNode *pRight,
 135415   RtreeCell *pBboxLeft,
 135416   RtreeCell *pBboxRight
 135418   int iLeftSeed = 0;
 135419   int iRightSeed = 1;
 135420   int *aiUsed;
 135421   int i;
 135423   aiUsed = sqlite3_malloc(sizeof(int)*nCell);
 135424   if( !aiUsed ){
 135425     return SQLITE_NOMEM;
 135427   memset(aiUsed, 0, sizeof(int)*nCell);
 135429   PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
 135431   memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
 135432   memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
 135433   nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
 135434   nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
 135435   aiUsed[iLeftSeed] = 1;
 135436   aiUsed[iRightSeed] = 1;
 135438   for(i=nCell-2; i>0; i--){
 135439     RtreeCell *pNext;
 135440     pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
 135441     RtreeDValue diff =  
 135442       cellGrowth(pRtree, pBboxLeft, pNext) - 
 135443       cellGrowth(pRtree, pBboxRight, pNext)
 135445     if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
 135446      || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
 135448       nodeInsertCell(pRtree, pRight, pNext);
 135449       cellUnion(pRtree, pBboxRight, pNext);
 135450     }else{
 135451       nodeInsertCell(pRtree, pLeft, pNext);
 135452       cellUnion(pRtree, pBboxLeft, pNext);
 135456   sqlite3_free(aiUsed);
 135457   return SQLITE_OK;
 135459 #endif
 135461 static int updateMapping(
 135462   Rtree *pRtree, 
 135463   i64 iRowid, 
 135464   RtreeNode *pNode, 
 135465   int iHeight
 135467   int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
 135468   xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
 135469   if( iHeight>0 ){
 135470     RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
 135471     if( pChild ){
 135472       nodeRelease(pRtree, pChild->pParent);
 135473       nodeReference(pNode);
 135474       pChild->pParent = pNode;
 135477   return xSetMapping(pRtree, iRowid, pNode->iNode);
 135480 static int SplitNode(
 135481   Rtree *pRtree,
 135482   RtreeNode *pNode,
 135483   RtreeCell *pCell,
 135484   int iHeight
 135486   int i;
 135487   int newCellIsRight = 0;
 135489   int rc = SQLITE_OK;
 135490   int nCell = NCELL(pNode);
 135491   RtreeCell *aCell;
 135492   int *aiUsed;
 135494   RtreeNode *pLeft = 0;
 135495   RtreeNode *pRight = 0;
 135497   RtreeCell leftbbox;
 135498   RtreeCell rightbbox;
 135500   /* Allocate an array and populate it with a copy of pCell and 
 135501   ** all cells from node pLeft. Then zero the original node.
 135503   aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
 135504   if( !aCell ){
 135505     rc = SQLITE_NOMEM;
 135506     goto splitnode_out;
 135508   aiUsed = (int *)&aCell[nCell+1];
 135509   memset(aiUsed, 0, sizeof(int)*(nCell+1));
 135510   for(i=0; i<nCell; i++){
 135511     nodeGetCell(pRtree, pNode, i, &aCell[i]);
 135513   nodeZero(pRtree, pNode);
 135514   memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
 135515   nCell++;
 135517   if( pNode->iNode==1 ){
 135518     pRight = nodeNew(pRtree, pNode);
 135519     pLeft = nodeNew(pRtree, pNode);
 135520     pRtree->iDepth++;
 135521     pNode->isDirty = 1;
 135522     writeInt16(pNode->zData, pRtree->iDepth);
 135523   }else{
 135524     pLeft = pNode;
 135525     pRight = nodeNew(pRtree, pLeft->pParent);
 135526     nodeReference(pLeft);
 135529   if( !pLeft || !pRight ){
 135530     rc = SQLITE_NOMEM;
 135531     goto splitnode_out;
 135534   memset(pLeft->zData, 0, pRtree->iNodeSize);
 135535   memset(pRight->zData, 0, pRtree->iNodeSize);
 135537   rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
 135538   if( rc!=SQLITE_OK ){
 135539     goto splitnode_out;
 135542   /* Ensure both child nodes have node numbers assigned to them by calling
 135543   ** nodeWrite(). Node pRight always needs a node number, as it was created
 135544   ** by nodeNew() above. But node pLeft sometimes already has a node number.
 135545   ** In this case avoid the all to nodeWrite().
 135547   if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
 135548    || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
 135550     goto splitnode_out;
 135553   rightbbox.iRowid = pRight->iNode;
 135554   leftbbox.iRowid = pLeft->iNode;
 135556   if( pNode->iNode==1 ){
 135557     rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
 135558     if( rc!=SQLITE_OK ){
 135559       goto splitnode_out;
 135561   }else{
 135562     RtreeNode *pParent = pLeft->pParent;
 135563     int iCell;
 135564     rc = nodeParentIndex(pRtree, pLeft, &iCell);
 135565     if( rc==SQLITE_OK ){
 135566       nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
 135567       rc = AdjustTree(pRtree, pParent, &leftbbox);
 135569     if( rc!=SQLITE_OK ){
 135570       goto splitnode_out;
 135573   if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
 135574     goto splitnode_out;
 135577   for(i=0; i<NCELL(pRight); i++){
 135578     i64 iRowid = nodeGetRowid(pRtree, pRight, i);
 135579     rc = updateMapping(pRtree, iRowid, pRight, iHeight);
 135580     if( iRowid==pCell->iRowid ){
 135581       newCellIsRight = 1;
 135583     if( rc!=SQLITE_OK ){
 135584       goto splitnode_out;
 135587   if( pNode->iNode==1 ){
 135588     for(i=0; i<NCELL(pLeft); i++){
 135589       i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
 135590       rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
 135591       if( rc!=SQLITE_OK ){
 135592         goto splitnode_out;
 135595   }else if( newCellIsRight==0 ){
 135596     rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
 135599   if( rc==SQLITE_OK ){
 135600     rc = nodeRelease(pRtree, pRight);
 135601     pRight = 0;
 135603   if( rc==SQLITE_OK ){
 135604     rc = nodeRelease(pRtree, pLeft);
 135605     pLeft = 0;
 135608 splitnode_out:
 135609   nodeRelease(pRtree, pRight);
 135610   nodeRelease(pRtree, pLeft);
 135611   sqlite3_free(aCell);
 135612   return rc;
 135616 ** If node pLeaf is not the root of the r-tree and its pParent pointer is 
 135617 ** still NULL, load all ancestor nodes of pLeaf into memory and populate
 135618 ** the pLeaf->pParent chain all the way up to the root node.
 135620 ** This operation is required when a row is deleted (or updated - an update
 135621 ** is implemented as a delete followed by an insert). SQLite provides the
 135622 ** rowid of the row to delete, which can be used to find the leaf on which
 135623 ** the entry resides (argument pLeaf). Once the leaf is located, this 
 135624 ** function is called to determine its ancestry.
 135626 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
 135627   int rc = SQLITE_OK;
 135628   RtreeNode *pChild = pLeaf;
 135629   while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
 135630     int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
 135631     sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
 135632     rc = sqlite3_step(pRtree->pReadParent);
 135633     if( rc==SQLITE_ROW ){
 135634       RtreeNode *pTest;           /* Used to test for reference loops */
 135635       i64 iNode;                  /* Node number of parent node */
 135637       /* Before setting pChild->pParent, test that we are not creating a
 135638       ** loop of references (as we would if, say, pChild==pParent). We don't
 135639       ** want to do this as it leads to a memory leak when trying to delete
 135640       ** the referenced counted node structures.
 135642       iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
 135643       for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
 135644       if( !pTest ){
 135645         rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
 135648     rc = sqlite3_reset(pRtree->pReadParent);
 135649     if( rc==SQLITE_OK ) rc = rc2;
 135650     if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
 135651     pChild = pChild->pParent;
 135653   return rc;
 135656 static int deleteCell(Rtree *, RtreeNode *, int, int);
 135658 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
 135659   int rc;
 135660   int rc2;
 135661   RtreeNode *pParent = 0;
 135662   int iCell;
 135664   assert( pNode->nRef==1 );
 135666   /* Remove the entry in the parent cell. */
 135667   rc = nodeParentIndex(pRtree, pNode, &iCell);
 135668   if( rc==SQLITE_OK ){
 135669     pParent = pNode->pParent;
 135670     pNode->pParent = 0;
 135671     rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
 135673   rc2 = nodeRelease(pRtree, pParent);
 135674   if( rc==SQLITE_OK ){
 135675     rc = rc2;
 135677   if( rc!=SQLITE_OK ){
 135678     return rc;
 135681   /* Remove the xxx_node entry. */
 135682   sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
 135683   sqlite3_step(pRtree->pDeleteNode);
 135684   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
 135685     return rc;
 135688   /* Remove the xxx_parent entry. */
 135689   sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
 135690   sqlite3_step(pRtree->pDeleteParent);
 135691   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
 135692     return rc;
 135695   /* Remove the node from the in-memory hash table and link it into
 135696   ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
 135698   nodeHashDelete(pRtree, pNode);
 135699   pNode->iNode = iHeight;
 135700   pNode->pNext = pRtree->pDeleted;
 135701   pNode->nRef++;
 135702   pRtree->pDeleted = pNode;
 135704   return SQLITE_OK;
 135707 static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
 135708   RtreeNode *pParent = pNode->pParent;
 135709   int rc = SQLITE_OK; 
 135710   if( pParent ){
 135711     int ii; 
 135712     int nCell = NCELL(pNode);
 135713     RtreeCell box;                            /* Bounding box for pNode */
 135714     nodeGetCell(pRtree, pNode, 0, &box);
 135715     for(ii=1; ii<nCell; ii++){
 135716       RtreeCell cell;
 135717       nodeGetCell(pRtree, pNode, ii, &cell);
 135718       cellUnion(pRtree, &box, &cell);
 135720     box.iRowid = pNode->iNode;
 135721     rc = nodeParentIndex(pRtree, pNode, &ii);
 135722     if( rc==SQLITE_OK ){
 135723       nodeOverwriteCell(pRtree, pParent, &box, ii);
 135724       rc = fixBoundingBox(pRtree, pParent);
 135727   return rc;
 135731 ** Delete the cell at index iCell of node pNode. After removing the
 135732 ** cell, adjust the r-tree data structure if required.
 135734 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
 135735   RtreeNode *pParent;
 135736   int rc;
 135738   if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
 135739     return rc;
 135742   /* Remove the cell from the node. This call just moves bytes around
 135743   ** the in-memory node image, so it cannot fail.
 135745   nodeDeleteCell(pRtree, pNode, iCell);
 135747   /* If the node is not the tree root and now has less than the minimum
 135748   ** number of cells, remove it from the tree. Otherwise, update the
 135749   ** cell in the parent node so that it tightly contains the updated
 135750   ** node.
 135752   pParent = pNode->pParent;
 135753   assert( pParent || pNode->iNode==1 );
 135754   if( pParent ){
 135755     if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
 135756       rc = removeNode(pRtree, pNode, iHeight);
 135757     }else{
 135758       rc = fixBoundingBox(pRtree, pNode);
 135762   return rc;
 135765 static int Reinsert(
 135766   Rtree *pRtree, 
 135767   RtreeNode *pNode, 
 135768   RtreeCell *pCell, 
 135769   int iHeight
 135771   int *aOrder;
 135772   int *aSpare;
 135773   RtreeCell *aCell;
 135774   RtreeDValue *aDistance;
 135775   int nCell;
 135776   RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
 135777   int iDim;
 135778   int ii;
 135779   int rc = SQLITE_OK;
 135780   int n;
 135782   memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
 135784   nCell = NCELL(pNode)+1;
 135785   n = (nCell+1)&(~1);
 135787   /* Allocate the buffers used by this operation. The allocation is
 135788   ** relinquished before this function returns.
 135790   aCell = (RtreeCell *)sqlite3_malloc(n * (
 135791     sizeof(RtreeCell)     +         /* aCell array */
 135792     sizeof(int)           +         /* aOrder array */
 135793     sizeof(int)           +         /* aSpare array */
 135794     sizeof(RtreeDValue)             /* aDistance array */
 135795   ));
 135796   if( !aCell ){
 135797     return SQLITE_NOMEM;
 135799   aOrder    = (int *)&aCell[n];
 135800   aSpare    = (int *)&aOrder[n];
 135801   aDistance = (RtreeDValue *)&aSpare[n];
 135803   for(ii=0; ii<nCell; ii++){
 135804     if( ii==(nCell-1) ){
 135805       memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
 135806     }else{
 135807       nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
 135809     aOrder[ii] = ii;
 135810     for(iDim=0; iDim<pRtree->nDim; iDim++){
 135811       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
 135812       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
 135815   for(iDim=0; iDim<pRtree->nDim; iDim++){
 135816     aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
 135819   for(ii=0; ii<nCell; ii++){
 135820     aDistance[ii] = 0.0;
 135821     for(iDim=0; iDim<pRtree->nDim; iDim++){
 135822       RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
 135823                                DCOORD(aCell[ii].aCoord[iDim*2]));
 135824       aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
 135828   SortByDistance(aOrder, nCell, aDistance, aSpare);
 135829   nodeZero(pRtree, pNode);
 135831   for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
 135832     RtreeCell *p = &aCell[aOrder[ii]];
 135833     nodeInsertCell(pRtree, pNode, p);
 135834     if( p->iRowid==pCell->iRowid ){
 135835       if( iHeight==0 ){
 135836         rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
 135837       }else{
 135838         rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
 135842   if( rc==SQLITE_OK ){
 135843     rc = fixBoundingBox(pRtree, pNode);
 135845   for(; rc==SQLITE_OK && ii<nCell; ii++){
 135846     /* Find a node to store this cell in. pNode->iNode currently contains
 135847     ** the height of the sub-tree headed by the cell.
 135849     RtreeNode *pInsert;
 135850     RtreeCell *p = &aCell[aOrder[ii]];
 135851     rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
 135852     if( rc==SQLITE_OK ){
 135853       int rc2;
 135854       rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
 135855       rc2 = nodeRelease(pRtree, pInsert);
 135856       if( rc==SQLITE_OK ){
 135857         rc = rc2;
 135862   sqlite3_free(aCell);
 135863   return rc;
 135867 ** Insert cell pCell into node pNode. Node pNode is the head of a 
 135868 ** subtree iHeight high (leaf nodes have iHeight==0).
 135870 static int rtreeInsertCell(
 135871   Rtree *pRtree,
 135872   RtreeNode *pNode,
 135873   RtreeCell *pCell,
 135874   int iHeight
 135876   int rc = SQLITE_OK;
 135877   if( iHeight>0 ){
 135878     RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
 135879     if( pChild ){
 135880       nodeRelease(pRtree, pChild->pParent);
 135881       nodeReference(pNode);
 135882       pChild->pParent = pNode;
 135885   if( nodeInsertCell(pRtree, pNode, pCell) ){
 135886 #if VARIANT_RSTARTREE_REINSERT
 135887     if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
 135888       rc = SplitNode(pRtree, pNode, pCell, iHeight);
 135889     }else{
 135890       pRtree->iReinsertHeight = iHeight;
 135891       rc = Reinsert(pRtree, pNode, pCell, iHeight);
 135893 #else
 135894     rc = SplitNode(pRtree, pNode, pCell, iHeight);
 135895 #endif
 135896   }else{
 135897     rc = AdjustTree(pRtree, pNode, pCell);
 135898     if( rc==SQLITE_OK ){
 135899       if( iHeight==0 ){
 135900         rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
 135901       }else{
 135902         rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
 135906   return rc;
 135909 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
 135910   int ii;
 135911   int rc = SQLITE_OK;
 135912   int nCell = NCELL(pNode);
 135914   for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
 135915     RtreeNode *pInsert;
 135916     RtreeCell cell;
 135917     nodeGetCell(pRtree, pNode, ii, &cell);
 135919     /* Find a node to store this cell in. pNode->iNode currently contains
 135920     ** the height of the sub-tree headed by the cell.
 135922     rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
 135923     if( rc==SQLITE_OK ){
 135924       int rc2;
 135925       rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
 135926       rc2 = nodeRelease(pRtree, pInsert);
 135927       if( rc==SQLITE_OK ){
 135928         rc = rc2;
 135932   return rc;
 135936 ** Select a currently unused rowid for a new r-tree record.
 135938 static int newRowid(Rtree *pRtree, i64 *piRowid){
 135939   int rc;
 135940   sqlite3_bind_null(pRtree->pWriteRowid, 1);
 135941   sqlite3_bind_null(pRtree->pWriteRowid, 2);
 135942   sqlite3_step(pRtree->pWriteRowid);
 135943   rc = sqlite3_reset(pRtree->pWriteRowid);
 135944   *piRowid = sqlite3_last_insert_rowid(pRtree->db);
 135945   return rc;
 135949 ** Remove the entry with rowid=iDelete from the r-tree structure.
 135951 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
 135952   int rc;                         /* Return code */
 135953   RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
 135954   int iCell;                      /* Index of iDelete cell in pLeaf */
 135955   RtreeNode *pRoot;               /* Root node of rtree structure */
 135958   /* Obtain a reference to the root node to initialise Rtree.iDepth */
 135959   rc = nodeAcquire(pRtree, 1, 0, &pRoot);
 135961   /* Obtain a reference to the leaf node that contains the entry 
 135962   ** about to be deleted. 
 135964   if( rc==SQLITE_OK ){
 135965     rc = findLeafNode(pRtree, iDelete, &pLeaf);
 135968   /* Delete the cell in question from the leaf node. */
 135969   if( rc==SQLITE_OK ){
 135970     int rc2;
 135971     rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
 135972     if( rc==SQLITE_OK ){
 135973       rc = deleteCell(pRtree, pLeaf, iCell, 0);
 135975     rc2 = nodeRelease(pRtree, pLeaf);
 135976     if( rc==SQLITE_OK ){
 135977       rc = rc2;
 135981   /* Delete the corresponding entry in the <rtree>_rowid table. */
 135982   if( rc==SQLITE_OK ){
 135983     sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
 135984     sqlite3_step(pRtree->pDeleteRowid);
 135985     rc = sqlite3_reset(pRtree->pDeleteRowid);
 135988   /* Check if the root node now has exactly one child. If so, remove
 135989   ** it, schedule the contents of the child for reinsertion and 
 135990   ** reduce the tree height by one.
 135992   ** This is equivalent to copying the contents of the child into
 135993   ** the root node (the operation that Gutman's paper says to perform 
 135994   ** in this scenario).
 135996   if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
 135997     int rc2;
 135998     RtreeNode *pChild;
 135999     i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
 136000     rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
 136001     if( rc==SQLITE_OK ){
 136002       rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
 136004     rc2 = nodeRelease(pRtree, pChild);
 136005     if( rc==SQLITE_OK ) rc = rc2;
 136006     if( rc==SQLITE_OK ){
 136007       pRtree->iDepth--;
 136008       writeInt16(pRoot->zData, pRtree->iDepth);
 136009       pRoot->isDirty = 1;
 136013   /* Re-insert the contents of any underfull nodes removed from the tree. */
 136014   for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
 136015     if( rc==SQLITE_OK ){
 136016       rc = reinsertNodeContent(pRtree, pLeaf);
 136018     pRtree->pDeleted = pLeaf->pNext;
 136019     sqlite3_free(pLeaf);
 136022   /* Release the reference to the root node. */
 136023   if( rc==SQLITE_OK ){
 136024     rc = nodeRelease(pRtree, pRoot);
 136025   }else{
 136026     nodeRelease(pRtree, pRoot);
 136029   return rc;
 136033 ** Rounding constants for float->double conversion.
 136035 #define RNDTOWARDS  (1.0 - 1.0/8388608.0)  /* Round towards zero */
 136036 #define RNDAWAY     (1.0 + 1.0/8388608.0)  /* Round away from zero */
 136038 #if !defined(SQLITE_RTREE_INT_ONLY)
 136040 ** Convert an sqlite3_value into an RtreeValue (presumably a float)
 136041 ** while taking care to round toward negative or positive, respectively.
 136043 static RtreeValue rtreeValueDown(sqlite3_value *v){
 136044   double d = sqlite3_value_double(v);
 136045   float f = (float)d;
 136046   if( f>d ){
 136047     f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
 136049   return f;
 136051 static RtreeValue rtreeValueUp(sqlite3_value *v){
 136052   double d = sqlite3_value_double(v);
 136053   float f = (float)d;
 136054   if( f<d ){
 136055     f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
 136057   return f;
 136059 #endif /* !defined(SQLITE_RTREE_INT_ONLY) */
 136063 ** The xUpdate method for rtree module virtual tables.
 136065 static int rtreeUpdate(
 136066   sqlite3_vtab *pVtab, 
 136067   int nData, 
 136068   sqlite3_value **azData, 
 136069   sqlite_int64 *pRowid
 136071   Rtree *pRtree = (Rtree *)pVtab;
 136072   int rc = SQLITE_OK;
 136073   RtreeCell cell;                 /* New cell to insert if nData>1 */
 136074   int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
 136076   rtreeReference(pRtree);
 136077   assert(nData>=1);
 136079   /* Constraint handling. A write operation on an r-tree table may return
 136080   ** SQLITE_CONSTRAINT for two reasons:
 136082   **   1. A duplicate rowid value, or
 136083   **   2. The supplied data violates the "x2>=x1" constraint.
 136085   ** In the first case, if the conflict-handling mode is REPLACE, then
 136086   ** the conflicting row can be removed before proceeding. In the second
 136087   ** case, SQLITE_CONSTRAINT must be returned regardless of the
 136088   ** conflict-handling mode specified by the user.
 136090   if( nData>1 ){
 136091     int ii;
 136093     /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
 136094     assert( nData==(pRtree->nDim*2 + 3) );
 136095 #ifndef SQLITE_RTREE_INT_ONLY
 136096     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
 136097       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 136098         cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
 136099         cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
 136100         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
 136101           rc = SQLITE_CONSTRAINT;
 136102           goto constraint;
 136105     }else
 136106 #endif
 136108       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 136109         cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
 136110         cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
 136111         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
 136112           rc = SQLITE_CONSTRAINT;
 136113           goto constraint;
 136118     /* If a rowid value was supplied, check if it is already present in 
 136119     ** the table. If so, the constraint has failed. */
 136120     if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
 136121       cell.iRowid = sqlite3_value_int64(azData[2]);
 136122       if( sqlite3_value_type(azData[0])==SQLITE_NULL
 136123        || sqlite3_value_int64(azData[0])!=cell.iRowid
 136125         int steprc;
 136126         sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
 136127         steprc = sqlite3_step(pRtree->pReadRowid);
 136128         rc = sqlite3_reset(pRtree->pReadRowid);
 136129         if( SQLITE_ROW==steprc ){
 136130           if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
 136131             rc = rtreeDeleteRowid(pRtree, cell.iRowid);
 136132           }else{
 136133             rc = SQLITE_CONSTRAINT;
 136134             goto constraint;
 136138       bHaveRowid = 1;
 136142   /* If azData[0] is not an SQL NULL value, it is the rowid of a
 136143   ** record to delete from the r-tree table. The following block does
 136144   ** just that.
 136146   if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
 136147     rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
 136150   /* If the azData[] array contains more than one element, elements
 136151   ** (azData[2]..azData[argc-1]) contain a new record to insert into
 136152   ** the r-tree structure.
 136154   if( rc==SQLITE_OK && nData>1 ){
 136155     /* Insert the new record into the r-tree */
 136156     RtreeNode *pLeaf = 0;
 136158     /* Figure out the rowid of the new row. */
 136159     if( bHaveRowid==0 ){
 136160       rc = newRowid(pRtree, &cell.iRowid);
 136162     *pRowid = cell.iRowid;
 136164     if( rc==SQLITE_OK ){
 136165       rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
 136167     if( rc==SQLITE_OK ){
 136168       int rc2;
 136169       pRtree->iReinsertHeight = -1;
 136170       rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
 136171       rc2 = nodeRelease(pRtree, pLeaf);
 136172       if( rc==SQLITE_OK ){
 136173         rc = rc2;
 136178 constraint:
 136179   rtreeRelease(pRtree);
 136180   return rc;
 136184 ** The xRename method for rtree module virtual tables.
 136186 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
 136187   Rtree *pRtree = (Rtree *)pVtab;
 136188   int rc = SQLITE_NOMEM;
 136189   char *zSql = sqlite3_mprintf(
 136190     "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
 136191     "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
 136192     "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
 136193     , pRtree->zDb, pRtree->zName, zNewName 
 136194     , pRtree->zDb, pRtree->zName, zNewName 
 136195     , pRtree->zDb, pRtree->zName, zNewName
 136197   if( zSql ){
 136198     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
 136199     sqlite3_free(zSql);
 136201   return rc;
 136204 static sqlite3_module rtreeModule = {
 136205   0,                          /* iVersion */
 136206   rtreeCreate,                /* xCreate - create a table */
 136207   rtreeConnect,               /* xConnect - connect to an existing table */
 136208   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
 136209   rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
 136210   rtreeDestroy,               /* xDestroy - Drop a table */
 136211   rtreeOpen,                  /* xOpen - open a cursor */
 136212   rtreeClose,                 /* xClose - close a cursor */
 136213   rtreeFilter,                /* xFilter - configure scan constraints */
 136214   rtreeNext,                  /* xNext - advance a cursor */
 136215   rtreeEof,                   /* xEof */
 136216   rtreeColumn,                /* xColumn - read data */
 136217   rtreeRowid,                 /* xRowid - read data */
 136218   rtreeUpdate,                /* xUpdate - write data */
 136219   0,                          /* xBegin - begin transaction */
 136220   0,                          /* xSync - sync transaction */
 136221   0,                          /* xCommit - commit transaction */
 136222   0,                          /* xRollback - rollback transaction */
 136223   0,                          /* xFindFunction - function overloading */
 136224   rtreeRename,                /* xRename - rename the table */
 136225   0,                          /* xSavepoint */
 136226   0,                          /* xRelease */
 136227   0                           /* xRollbackTo */
 136230 static int rtreeSqlInit(
 136231   Rtree *pRtree, 
 136232   sqlite3 *db, 
 136233   const char *zDb, 
 136234   const char *zPrefix, 
 136235   int isCreate
 136237   int rc = SQLITE_OK;
 136239   #define N_STATEMENT 9
 136240   static const char *azSql[N_STATEMENT] = {
 136241     /* Read and write the xxx_node table */
 136242     "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
 136243     "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
 136244     "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
 136246     /* Read and write the xxx_rowid table */
 136247     "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
 136248     "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
 136249     "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
 136251     /* Read and write the xxx_parent table */
 136252     "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
 136253     "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
 136254     "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
 136256   sqlite3_stmt **appStmt[N_STATEMENT];
 136257   int i;
 136259   pRtree->db = db;
 136261   if( isCreate ){
 136262     char *zCreate = sqlite3_mprintf(
 136263 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
 136264 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
 136265 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
 136266 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
 136267       zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
 136269     if( !zCreate ){
 136270       return SQLITE_NOMEM;
 136272     rc = sqlite3_exec(db, zCreate, 0, 0, 0);
 136273     sqlite3_free(zCreate);
 136274     if( rc!=SQLITE_OK ){
 136275       return rc;
 136279   appStmt[0] = &pRtree->pReadNode;
 136280   appStmt[1] = &pRtree->pWriteNode;
 136281   appStmt[2] = &pRtree->pDeleteNode;
 136282   appStmt[3] = &pRtree->pReadRowid;
 136283   appStmt[4] = &pRtree->pWriteRowid;
 136284   appStmt[5] = &pRtree->pDeleteRowid;
 136285   appStmt[6] = &pRtree->pReadParent;
 136286   appStmt[7] = &pRtree->pWriteParent;
 136287   appStmt[8] = &pRtree->pDeleteParent;
 136289   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
 136290     char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
 136291     if( zSql ){
 136292       rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
 136293     }else{
 136294       rc = SQLITE_NOMEM;
 136296     sqlite3_free(zSql);
 136299   return rc;
 136303 ** The second argument to this function contains the text of an SQL statement
 136304 ** that returns a single integer value. The statement is compiled and executed
 136305 ** using database connection db. If successful, the integer value returned
 136306 ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
 136307 ** code is returned and the value of *piVal after returning is not defined.
 136309 static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
 136310   int rc = SQLITE_NOMEM;
 136311   if( zSql ){
 136312     sqlite3_stmt *pStmt = 0;
 136313     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 136314     if( rc==SQLITE_OK ){
 136315       if( SQLITE_ROW==sqlite3_step(pStmt) ){
 136316         *piVal = sqlite3_column_int(pStmt, 0);
 136318       rc = sqlite3_finalize(pStmt);
 136321   return rc;
 136325 ** This function is called from within the xConnect() or xCreate() method to
 136326 ** determine the node-size used by the rtree table being created or connected
 136327 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
 136328 ** Otherwise, an SQLite error code is returned.
 136330 ** If this function is being called as part of an xConnect(), then the rtree
 136331 ** table already exists. In this case the node-size is determined by inspecting
 136332 ** the root node of the tree.
 136334 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. 
 136335 ** This ensures that each node is stored on a single database page. If the 
 136336 ** database page-size is so large that more than RTREE_MAXCELLS entries 
 136337 ** would fit in a single node, use a smaller node-size.
 136339 static int getNodeSize(
 136340   sqlite3 *db,                    /* Database handle */
 136341   Rtree *pRtree,                  /* Rtree handle */
 136342   int isCreate                    /* True for xCreate, false for xConnect */
 136344   int rc;
 136345   char *zSql;
 136346   if( isCreate ){
 136347     int iPageSize = 0;
 136348     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
 136349     rc = getIntFromStmt(db, zSql, &iPageSize);
 136350     if( rc==SQLITE_OK ){
 136351       pRtree->iNodeSize = iPageSize-64;
 136352       if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
 136353         pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
 136356   }else{
 136357     zSql = sqlite3_mprintf(
 136358         "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
 136359         pRtree->zDb, pRtree->zName
 136361     rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
 136364   sqlite3_free(zSql);
 136365   return rc;
 136369 ** This function is the implementation of both the xConnect and xCreate
 136370 ** methods of the r-tree virtual table.
 136372 **   argv[0]   -> module name
 136373 **   argv[1]   -> database name
 136374 **   argv[2]   -> table name
 136375 **   argv[...] -> column names...
 136377 static int rtreeInit(
 136378   sqlite3 *db,                        /* Database connection */
 136379   void *pAux,                         /* One of the RTREE_COORD_* constants */
 136380   int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
 136381   sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
 136382   char **pzErr,                       /* OUT: Error message, if any */
 136383   int isCreate                        /* True for xCreate, false for xConnect */
 136385   int rc = SQLITE_OK;
 136386   Rtree *pRtree;
 136387   int nDb;              /* Length of string argv[1] */
 136388   int nName;            /* Length of string argv[2] */
 136389   int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
 136391   const char *aErrMsg[] = {
 136392     0,                                                    /* 0 */
 136393     "Wrong number of columns for an rtree table",         /* 1 */
 136394     "Too few columns for an rtree table",                 /* 2 */
 136395     "Too many columns for an rtree table"                 /* 3 */
 136398   int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
 136399   if( aErrMsg[iErr] ){
 136400     *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
 136401     return SQLITE_ERROR;
 136404   sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
 136406   /* Allocate the sqlite3_vtab structure */
 136407   nDb = (int)strlen(argv[1]);
 136408   nName = (int)strlen(argv[2]);
 136409   pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
 136410   if( !pRtree ){
 136411     return SQLITE_NOMEM;
 136413   memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
 136414   pRtree->nBusy = 1;
 136415   pRtree->base.pModule = &rtreeModule;
 136416   pRtree->zDb = (char *)&pRtree[1];
 136417   pRtree->zName = &pRtree->zDb[nDb+1];
 136418   pRtree->nDim = (argc-4)/2;
 136419   pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
 136420   pRtree->eCoordType = eCoordType;
 136421   memcpy(pRtree->zDb, argv[1], nDb);
 136422   memcpy(pRtree->zName, argv[2], nName);
 136424   /* Figure out the node size to use. */
 136425   rc = getNodeSize(db, pRtree, isCreate);
 136427   /* Create/Connect to the underlying relational database schema. If
 136428   ** that is successful, call sqlite3_declare_vtab() to configure
 136429   ** the r-tree table schema.
 136431   if( rc==SQLITE_OK ){
 136432     if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
 136433       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
 136434     }else{
 136435       char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
 136436       char *zTmp;
 136437       int ii;
 136438       for(ii=4; zSql && ii<argc; ii++){
 136439         zTmp = zSql;
 136440         zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
 136441         sqlite3_free(zTmp);
 136443       if( zSql ){
 136444         zTmp = zSql;
 136445         zSql = sqlite3_mprintf("%s);", zTmp);
 136446         sqlite3_free(zTmp);
 136448       if( !zSql ){
 136449         rc = SQLITE_NOMEM;
 136450       }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
 136451         *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
 136453       sqlite3_free(zSql);
 136457   if( rc==SQLITE_OK ){
 136458     *ppVtab = (sqlite3_vtab *)pRtree;
 136459   }else{
 136460     rtreeRelease(pRtree);
 136462   return rc;
 136467 ** Implementation of a scalar function that decodes r-tree nodes to
 136468 ** human readable strings. This can be used for debugging and analysis.
 136470 ** The scalar function takes two arguments, a blob of data containing
 136471 ** an r-tree node, and the number of dimensions the r-tree indexes.
 136472 ** For a two-dimensional r-tree structure called "rt", to deserialize
 136473 ** all nodes, a statement like:
 136475 **   SELECT rtreenode(2, data) FROM rt_node;
 136477 ** The human readable string takes the form of a Tcl list with one
 136478 ** entry for each cell in the r-tree node. Each entry is itself a
 136479 ** list, containing the 8-byte rowid/pageno followed by the 
 136480 ** <num-dimension>*2 coordinates.
 136482 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
 136483   char *zText = 0;
 136484   RtreeNode node;
 136485   Rtree tree;
 136486   int ii;
 136488   UNUSED_PARAMETER(nArg);
 136489   memset(&node, 0, sizeof(RtreeNode));
 136490   memset(&tree, 0, sizeof(Rtree));
 136491   tree.nDim = sqlite3_value_int(apArg[0]);
 136492   tree.nBytesPerCell = 8 + 8 * tree.nDim;
 136493   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
 136495   for(ii=0; ii<NCELL(&node); ii++){
 136496     char zCell[512];
 136497     int nCell = 0;
 136498     RtreeCell cell;
 136499     int jj;
 136501     nodeGetCell(&tree, &node, ii, &cell);
 136502     sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
 136503     nCell = (int)strlen(zCell);
 136504     for(jj=0; jj<tree.nDim*2; jj++){
 136505 #ifndef SQLITE_RTREE_INT_ONLY
 136506       sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
 136507                        (double)cell.aCoord[jj].f);
 136508 #else
 136509       sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
 136510                        cell.aCoord[jj].i);
 136511 #endif
 136512       nCell = (int)strlen(zCell);
 136515     if( zText ){
 136516       char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
 136517       sqlite3_free(zText);
 136518       zText = zTextNew;
 136519     }else{
 136520       zText = sqlite3_mprintf("{%s}", zCell);
 136524   sqlite3_result_text(ctx, zText, -1, sqlite3_free);
 136527 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
 136528   UNUSED_PARAMETER(nArg);
 136529   if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB 
 136530    || sqlite3_value_bytes(apArg[0])<2
 136532     sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 
 136533   }else{
 136534     u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
 136535     sqlite3_result_int(ctx, readInt16(zBlob));
 136540 ** Register the r-tree module with database handle db. This creates the
 136541 ** virtual table module "rtree" and the debugging/analysis scalar 
 136542 ** function "rtreenode".
 136544 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
 136545   const int utf8 = SQLITE_UTF8;
 136546   int rc;
 136548   rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
 136549   if( rc==SQLITE_OK ){
 136550     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
 136552   if( rc==SQLITE_OK ){
 136553 #ifdef SQLITE_RTREE_INT_ONLY
 136554     void *c = (void *)RTREE_COORD_INT32;
 136555 #else
 136556     void *c = (void *)RTREE_COORD_REAL32;
 136557 #endif
 136558     rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
 136560   if( rc==SQLITE_OK ){
 136561     void *c = (void *)RTREE_COORD_INT32;
 136562     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
 136565   return rc;
 136569 ** A version of sqlite3_free() that can be used as a callback. This is used
 136570 ** in two places - as the destructor for the blob value returned by the
 136571 ** invocation of a geometry function, and as the destructor for the geometry
 136572 ** functions themselves.
 136574 static void doSqlite3Free(void *p){
 136575   sqlite3_free(p);
 136579 ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
 136580 ** scalar user function. This C function is the callback used for all such
 136581 ** registered SQL functions.
 136583 ** The scalar user functions return a blob that is interpreted by r-tree
 136584 ** table MATCH operators.
 136586 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
 136587   RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
 136588   RtreeMatchArg *pBlob;
 136589   int nBlob;
 136591   nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
 136592   pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
 136593   if( !pBlob ){
 136594     sqlite3_result_error_nomem(ctx);
 136595   }else{
 136596     int i;
 136597     pBlob->magic = RTREE_GEOMETRY_MAGIC;
 136598     pBlob->xGeom = pGeomCtx->xGeom;
 136599     pBlob->pContext = pGeomCtx->pContext;
 136600     pBlob->nParam = nArg;
 136601     for(i=0; i<nArg; i++){
 136602 #ifdef SQLITE_RTREE_INT_ONLY
 136603       pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
 136604 #else
 136605       pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
 136606 #endif
 136608     sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
 136613 ** Register a new geometry function for use with the r-tree MATCH operator.
 136615 SQLITE_API int sqlite3_rtree_geometry_callback(
 136616   sqlite3 *db,
 136617   const char *zGeom,
 136618   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
 136619   void *pContext
 136621   RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
 136623   /* Allocate and populate the context object. */
 136624   pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
 136625   if( !pGeomCtx ) return SQLITE_NOMEM;
 136626   pGeomCtx->xGeom = xGeom;
 136627   pGeomCtx->pContext = pContext;
 136629   /* Create the new user-function. Register a destructor function to delete
 136630   ** the context object when it is no longer required.  */
 136631   return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, 
 136632       (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
 136636 #if !SQLITE_CORE
 136637 SQLITE_API int sqlite3_extension_init(
 136638   sqlite3 *db,
 136639   char **pzErrMsg,
 136640   const sqlite3_api_routines *pApi
 136642   SQLITE_EXTENSION_INIT2(pApi)
 136643   return sqlite3RtreeInit(db);
 136645 #endif
 136647 #endif
 136649 /************** End of rtree.c ***********************************************/
 136650 /************** Begin file icu.c *********************************************/
 136652 ** 2007 May 6
 136654 ** The author disclaims copyright to this source code.  In place of
 136655 ** a legal notice, here is a blessing:
 136657 **    May you do good and not evil.
 136658 **    May you find forgiveness for yourself and forgive others.
 136659 **    May you share freely, never taking more than you give.
 136661 *************************************************************************
 136662 ** $Id$
 136664 ** This file implements an integration between the ICU library 
 136665 ** ("International Components for Unicode", an open-source library 
 136666 ** for handling unicode data) and SQLite. The integration uses 
 136667 ** ICU to provide the following to SQLite:
 136669 **   * An implementation of the SQL regexp() function (and hence REGEXP
 136670 **     operator) using the ICU uregex_XX() APIs.
 136672 **   * Implementations of the SQL scalar upper() and lower() functions
 136673 **     for case mapping.
 136675 **   * Integration of ICU and SQLite collation seqences.
 136677 **   * An implementation of the LIKE operator that uses ICU to 
 136678 **     provide case-independent matching.
 136681 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
 136683 /* Include ICU headers */
 136684 #include <unicode/utypes.h>
 136685 #include <unicode/uregex.h>
 136686 #include <unicode/ustring.h>
 136687 #include <unicode/ucol.h>
 136689 /* #include <assert.h> */
 136691 #ifndef SQLITE_CORE
 136692   SQLITE_EXTENSION_INIT1
 136693 #else
 136694 #endif
 136697 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
 136698 ** operator.
 136700 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
 136701 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
 136702 #endif
 136705 ** Version of sqlite3_free() that is always a function, never a macro.
 136707 static void xFree(void *p){
 136708   sqlite3_free(p);
 136712 ** Compare two UTF-8 strings for equality where the first string is
 136713 ** a "LIKE" expression. Return true (1) if they are the same and 
 136714 ** false (0) if they are different.
 136716 static int icuLikeCompare(
 136717   const uint8_t *zPattern,   /* LIKE pattern */
 136718   const uint8_t *zString,    /* The UTF-8 string to compare against */
 136719   const UChar32 uEsc         /* The escape character */
 136721   static const int MATCH_ONE = (UChar32)'_';
 136722   static const int MATCH_ALL = (UChar32)'%';
 136724   int iPattern = 0;       /* Current byte index in zPattern */
 136725   int iString = 0;        /* Current byte index in zString */
 136727   int prevEscape = 0;     /* True if the previous character was uEsc */
 136729   while( zPattern[iPattern]!=0 ){
 136731     /* Read (and consume) the next character from the input pattern. */
 136732     UChar32 uPattern;
 136733     U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
 136734     assert(uPattern!=0);
 136736     /* There are now 4 possibilities:
 136738     **     1. uPattern is an unescaped match-all character "%",
 136739     **     2. uPattern is an unescaped match-one character "_",
 136740     **     3. uPattern is an unescaped escape character, or
 136741     **     4. uPattern is to be handled as an ordinary character
 136743     if( !prevEscape && uPattern==MATCH_ALL ){
 136744       /* Case 1. */
 136745       uint8_t c;
 136747       /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
 136748       ** MATCH_ALL. For each MATCH_ONE, skip one character in the 
 136749       ** test string.
 136751       while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
 136752         if( c==MATCH_ONE ){
 136753           if( zString[iString]==0 ) return 0;
 136754           U8_FWD_1_UNSAFE(zString, iString);
 136756         iPattern++;
 136759       if( zPattern[iPattern]==0 ) return 1;
 136761       while( zString[iString] ){
 136762         if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
 136763           return 1;
 136765         U8_FWD_1_UNSAFE(zString, iString);
 136767       return 0;
 136769     }else if( !prevEscape && uPattern==MATCH_ONE ){
 136770       /* Case 2. */
 136771       if( zString[iString]==0 ) return 0;
 136772       U8_FWD_1_UNSAFE(zString, iString);
 136774     }else if( !prevEscape && uPattern==uEsc){
 136775       /* Case 3. */
 136776       prevEscape = 1;
 136778     }else{
 136779       /* Case 4. */
 136780       UChar32 uString;
 136781       U8_NEXT_UNSAFE(zString, iString, uString);
 136782       uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
 136783       uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
 136784       if( uString!=uPattern ){
 136785         return 0;
 136787       prevEscape = 0;
 136791   return zString[iString]==0;
 136795 ** Implementation of the like() SQL function.  This function implements
 136796 ** the build-in LIKE operator.  The first argument to the function is the
 136797 ** pattern and the second argument is the string.  So, the SQL statements:
 136799 **       A LIKE B
 136801 ** is implemented as like(B, A). If there is an escape character E, 
 136803 **       A LIKE B ESCAPE E
 136805 ** is mapped to like(B, A, E).
 136807 static void icuLikeFunc(
 136808   sqlite3_context *context, 
 136809   int argc, 
 136810   sqlite3_value **argv
 136812   const unsigned char *zA = sqlite3_value_text(argv[0]);
 136813   const unsigned char *zB = sqlite3_value_text(argv[1]);
 136814   UChar32 uEsc = 0;
 136816   /* Limit the length of the LIKE or GLOB pattern to avoid problems
 136817   ** of deep recursion and N*N behavior in patternCompare().
 136819   if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
 136820     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
 136821     return;
 136825   if( argc==3 ){
 136826     /* The escape character string must consist of a single UTF-8 character.
 136827     ** Otherwise, return an error.
 136829     int nE= sqlite3_value_bytes(argv[2]);
 136830     const unsigned char *zE = sqlite3_value_text(argv[2]);
 136831     int i = 0;
 136832     if( zE==0 ) return;
 136833     U8_NEXT(zE, i, nE, uEsc);
 136834     if( i!=nE){
 136835       sqlite3_result_error(context, 
 136836           "ESCAPE expression must be a single character", -1);
 136837       return;
 136841   if( zA && zB ){
 136842     sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
 136847 ** This function is called when an ICU function called from within
 136848 ** the implementation of an SQL scalar function returns an error.
 136850 ** The scalar function context passed as the first argument is 
 136851 ** loaded with an error message based on the following two args.
 136853 static void icuFunctionError(
 136854   sqlite3_context *pCtx,       /* SQLite scalar function context */
 136855   const char *zName,           /* Name of ICU function that failed */
 136856   UErrorCode e                 /* Error code returned by ICU function */
 136858   char zBuf[128];
 136859   sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
 136860   zBuf[127] = '\0';
 136861   sqlite3_result_error(pCtx, zBuf, -1);
 136865 ** Function to delete compiled regexp objects. Registered as
 136866 ** a destructor function with sqlite3_set_auxdata().
 136868 static void icuRegexpDelete(void *p){
 136869   URegularExpression *pExpr = (URegularExpression *)p;
 136870   uregex_close(pExpr);
 136874 ** Implementation of SQLite REGEXP operator. This scalar function takes
 136875 ** two arguments. The first is a regular expression pattern to compile
 136876 ** the second is a string to match against that pattern. If either 
 136877 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
 136878 ** is 1 if the string matches the pattern, or 0 otherwise.
 136880 ** SQLite maps the regexp() function to the regexp() operator such
 136881 ** that the following two are equivalent:
 136883 **     zString REGEXP zPattern
 136884 **     regexp(zPattern, zString)
 136886 ** Uses the following ICU regexp APIs:
 136888 **     uregex_open()
 136889 **     uregex_matches()
 136890 **     uregex_close()
 136892 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
 136893   UErrorCode status = U_ZERO_ERROR;
 136894   URegularExpression *pExpr;
 136895   UBool res;
 136896   const UChar *zString = sqlite3_value_text16(apArg[1]);
 136898   (void)nArg;  /* Unused parameter */
 136900   /* If the left hand side of the regexp operator is NULL, 
 136901   ** then the result is also NULL. 
 136903   if( !zString ){
 136904     return;
 136907   pExpr = sqlite3_get_auxdata(p, 0);
 136908   if( !pExpr ){
 136909     const UChar *zPattern = sqlite3_value_text16(apArg[0]);
 136910     if( !zPattern ){
 136911       return;
 136913     pExpr = uregex_open(zPattern, -1, 0, 0, &status);
 136915     if( U_SUCCESS(status) ){
 136916       sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
 136917     }else{
 136918       assert(!pExpr);
 136919       icuFunctionError(p, "uregex_open", status);
 136920       return;
 136924   /* Configure the text that the regular expression operates on. */
 136925   uregex_setText(pExpr, zString, -1, &status);
 136926   if( !U_SUCCESS(status) ){
 136927     icuFunctionError(p, "uregex_setText", status);
 136928     return;
 136931   /* Attempt the match */
 136932   res = uregex_matches(pExpr, 0, &status);
 136933   if( !U_SUCCESS(status) ){
 136934     icuFunctionError(p, "uregex_matches", status);
 136935     return;
 136938   /* Set the text that the regular expression operates on to a NULL
 136939   ** pointer. This is not really necessary, but it is tidier than 
 136940   ** leaving the regular expression object configured with an invalid
 136941   ** pointer after this function returns.
 136943   uregex_setText(pExpr, 0, 0, &status);
 136945   /* Return 1 or 0. */
 136946   sqlite3_result_int(p, res ? 1 : 0);
 136950 ** Implementations of scalar functions for case mapping - upper() and 
 136951 ** lower(). Function upper() converts its input to upper-case (ABC).
 136952 ** Function lower() converts to lower-case (abc).
 136954 ** ICU provides two types of case mapping, "general" case mapping and
 136955 ** "language specific". Refer to ICU documentation for the differences
 136956 ** between the two.
 136958 ** To utilise "general" case mapping, the upper() or lower() scalar 
 136959 ** functions are invoked with one argument:
 136961 **     upper('ABC') -> 'abc'
 136962 **     lower('abc') -> 'ABC'
 136964 ** To access ICU "language specific" case mapping, upper() or lower()
 136965 ** should be invoked with two arguments. The second argument is the name
 136966 ** of the locale to use. Passing an empty string ("") or SQL NULL value
 136967 ** as the second argument is the same as invoking the 1 argument version
 136968 ** of upper() or lower().
 136970 **     lower('I', 'en_us') -> 'i'
 136971 **     lower('I', 'tr_tr') -> 'ı' (small dotless i)
 136973 ** http://www.icu-project.org/userguide/posix.html#case_mappings
 136975 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
 136976   const UChar *zInput;
 136977   UChar *zOutput;
 136978   int nInput;
 136979   int nOutput;
 136981   UErrorCode status = U_ZERO_ERROR;
 136982   const char *zLocale = 0;
 136984   assert(nArg==1 || nArg==2);
 136985   if( nArg==2 ){
 136986     zLocale = (const char *)sqlite3_value_text(apArg[1]);
 136989   zInput = sqlite3_value_text16(apArg[0]);
 136990   if( !zInput ){
 136991     return;
 136993   nInput = sqlite3_value_bytes16(apArg[0]);
 136995   nOutput = nInput * 2 + 2;
 136996   zOutput = sqlite3_malloc(nOutput);
 136997   if( !zOutput ){
 136998     return;
 137001   if( sqlite3_user_data(p) ){
 137002     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
 137003   }else{
 137004     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
 137007   if( !U_SUCCESS(status) ){
 137008     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
 137009     return;
 137012   sqlite3_result_text16(p, zOutput, -1, xFree);
 137016 ** Collation sequence destructor function. The pCtx argument points to
 137017 ** a UCollator structure previously allocated using ucol_open().
 137019 static void icuCollationDel(void *pCtx){
 137020   UCollator *p = (UCollator *)pCtx;
 137021   ucol_close(p);
 137025 ** Collation sequence comparison function. The pCtx argument points to
 137026 ** a UCollator structure previously allocated using ucol_open().
 137028 static int icuCollationColl(
 137029   void *pCtx,
 137030   int nLeft,
 137031   const void *zLeft,
 137032   int nRight,
 137033   const void *zRight
 137035   UCollationResult res;
 137036   UCollator *p = (UCollator *)pCtx;
 137037   res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
 137038   switch( res ){
 137039     case UCOL_LESS:    return -1;
 137040     case UCOL_GREATER: return +1;
 137041     case UCOL_EQUAL:   return 0;
 137043   assert(!"Unexpected return value from ucol_strcoll()");
 137044   return 0;
 137048 ** Implementation of the scalar function icu_load_collation().
 137050 ** This scalar function is used to add ICU collation based collation 
 137051 ** types to an SQLite database connection. It is intended to be called
 137052 ** as follows:
 137054 **     SELECT icu_load_collation(<locale>, <collation-name>);
 137056 ** Where <locale> is a string containing an ICU locale identifier (i.e.
 137057 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
 137058 ** collation sequence to create.
 137060 static void icuLoadCollation(
 137061   sqlite3_context *p, 
 137062   int nArg, 
 137063   sqlite3_value **apArg
 137065   sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
 137066   UErrorCode status = U_ZERO_ERROR;
 137067   const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
 137068   const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
 137069   UCollator *pUCollator;    /* ICU library collation object */
 137070   int rc;                   /* Return code from sqlite3_create_collation_x() */
 137072   assert(nArg==2);
 137073   zLocale = (const char *)sqlite3_value_text(apArg[0]);
 137074   zName = (const char *)sqlite3_value_text(apArg[1]);
 137076   if( !zLocale || !zName ){
 137077     return;
 137080   pUCollator = ucol_open(zLocale, &status);
 137081   if( !U_SUCCESS(status) ){
 137082     icuFunctionError(p, "ucol_open", status);
 137083     return;
 137085   assert(p);
 137087   rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, 
 137088       icuCollationColl, icuCollationDel
 137090   if( rc!=SQLITE_OK ){
 137091     ucol_close(pUCollator);
 137092     sqlite3_result_error(p, "Error registering collation function", -1);
 137097 ** Register the ICU extension functions with database db.
 137099 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
 137100   struct IcuScalar {
 137101     const char *zName;                        /* Function name */
 137102     int nArg;                                 /* Number of arguments */
 137103     int enc;                                  /* Optimal text encoding */
 137104     void *pContext;                           /* sqlite3_user_data() context */
 137105     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
 137106   } scalars[] = {
 137107     {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
 137109     {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
 137110     {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
 137111     {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
 137112     {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
 137114     {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
 137115     {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
 137116     {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
 137117     {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
 137119     {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
 137120     {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
 137122     {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
 137125   int rc = SQLITE_OK;
 137126   int i;
 137128   for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
 137129     struct IcuScalar *p = &scalars[i];
 137130     rc = sqlite3_create_function(
 137131         db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
 137135   return rc;
 137138 #if !SQLITE_CORE
 137139 SQLITE_API int sqlite3_extension_init(
 137140   sqlite3 *db, 
 137141   char **pzErrMsg,
 137142   const sqlite3_api_routines *pApi
 137144   SQLITE_EXTENSION_INIT2(pApi)
 137145   return sqlite3IcuInit(db);
 137147 #endif
 137149 #endif
 137151 /************** End of icu.c *************************************************/
 137152 /************** Begin file fts3_icu.c ****************************************/
 137154 ** 2007 June 22
 137156 ** The author disclaims copyright to this source code.  In place of
 137157 ** a legal notice, here is a blessing:
 137159 **    May you do good and not evil.
 137160 **    May you find forgiveness for yourself and forgive others.
 137161 **    May you share freely, never taking more than you give.
 137163 *************************************************************************
 137164 ** This file implements a tokenizer for fts3 based on the ICU library.
 137166 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 137167 #ifdef SQLITE_ENABLE_ICU
 137169 /* #include <assert.h> */
 137170 /* #include <string.h> */
 137172 #include <unicode/ubrk.h>
 137173 /* #include <unicode/ucol.h> */
 137174 /* #include <unicode/ustring.h> */
 137175 #include <unicode/utf16.h>
 137177 typedef struct IcuTokenizer IcuTokenizer;
 137178 typedef struct IcuCursor IcuCursor;
 137180 struct IcuTokenizer {
 137181   sqlite3_tokenizer base;
 137182   char *zLocale;
 137185 struct IcuCursor {
 137186   sqlite3_tokenizer_cursor base;
 137188   UBreakIterator *pIter;      /* ICU break-iterator object */
 137189   int nChar;                  /* Number of UChar elements in pInput */
 137190   UChar *aChar;               /* Copy of input using utf-16 encoding */
 137191   int *aOffset;               /* Offsets of each character in utf-8 input */
 137193   int nBuffer;
 137194   char *zBuffer;
 137196   int iToken;
 137200 ** Create a new tokenizer instance.
 137202 static int icuCreate(
 137203   int argc,                            /* Number of entries in argv[] */
 137204   const char * const *argv,            /* Tokenizer creation arguments */
 137205   sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
 137207   IcuTokenizer *p;
 137208   int n = 0;
 137210   if( argc>0 ){
 137211     n = strlen(argv[0])+1;
 137213   p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
 137214   if( !p ){
 137215     return SQLITE_NOMEM;
 137217   memset(p, 0, sizeof(IcuTokenizer));
 137219   if( n ){
 137220     p->zLocale = (char *)&p[1];
 137221     memcpy(p->zLocale, argv[0], n);
 137224   *ppTokenizer = (sqlite3_tokenizer *)p;
 137226   return SQLITE_OK;
 137230 ** Destroy a tokenizer
 137232 static int icuDestroy(sqlite3_tokenizer *pTokenizer){
 137233   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
 137234   sqlite3_free(p);
 137235   return SQLITE_OK;
 137239 ** Prepare to begin tokenizing a particular string.  The input
 137240 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
 137241 ** used to incrementally tokenize this string is returned in 
 137242 ** *ppCursor.
 137244 static int icuOpen(
 137245   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
 137246   const char *zInput,                    /* Input string */
 137247   int nInput,                            /* Length of zInput in bytes */
 137248   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
 137250   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
 137251   IcuCursor *pCsr;
 137253   const int32_t opt = U_FOLD_CASE_DEFAULT;
 137254   UErrorCode status = U_ZERO_ERROR;
 137255   int nChar;
 137257   UChar32 c;
 137258   int iInput = 0;
 137259   int iOut = 0;
 137261   *ppCursor = 0;
 137263   if( zInput==0 ){
 137264     nInput = 0;
 137265     zInput = "";
 137266   }else if( nInput<0 ){
 137267     nInput = strlen(zInput);
 137269   nChar = nInput+1;
 137270   pCsr = (IcuCursor *)sqlite3_malloc(
 137271       sizeof(IcuCursor) +                /* IcuCursor */
 137272       ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
 137273       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
 137275   if( !pCsr ){
 137276     return SQLITE_NOMEM;
 137278   memset(pCsr, 0, sizeof(IcuCursor));
 137279   pCsr->aChar = (UChar *)&pCsr[1];
 137280   pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
 137282   pCsr->aOffset[iOut] = iInput;
 137283   U8_NEXT(zInput, iInput, nInput, c); 
 137284   while( c>0 ){
 137285     int isError = 0;
 137286     c = u_foldCase(c, opt);
 137287     U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
 137288     if( isError ){
 137289       sqlite3_free(pCsr);
 137290       return SQLITE_ERROR;
 137292     pCsr->aOffset[iOut] = iInput;
 137294     if( iInput<nInput ){
 137295       U8_NEXT(zInput, iInput, nInput, c);
 137296     }else{
 137297       c = 0;
 137301   pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
 137302   if( !U_SUCCESS(status) ){
 137303     sqlite3_free(pCsr);
 137304     return SQLITE_ERROR;
 137306   pCsr->nChar = iOut;
 137308   ubrk_first(pCsr->pIter);
 137309   *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
 137310   return SQLITE_OK;
 137314 ** Close a tokenization cursor previously opened by a call to icuOpen().
 137316 static int icuClose(sqlite3_tokenizer_cursor *pCursor){
 137317   IcuCursor *pCsr = (IcuCursor *)pCursor;
 137318   ubrk_close(pCsr->pIter);
 137319   sqlite3_free(pCsr->zBuffer);
 137320   sqlite3_free(pCsr);
 137321   return SQLITE_OK;
 137325 ** Extract the next token from a tokenization cursor.
 137327 static int icuNext(
 137328   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
 137329   const char **ppToken,               /* OUT: *ppToken is the token text */
 137330   int *pnBytes,                       /* OUT: Number of bytes in token */
 137331   int *piStartOffset,                 /* OUT: Starting offset of token */
 137332   int *piEndOffset,                   /* OUT: Ending offset of token */
 137333   int *piPosition                     /* OUT: Position integer of token */
 137335   IcuCursor *pCsr = (IcuCursor *)pCursor;
 137337   int iStart = 0;
 137338   int iEnd = 0;
 137339   int nByte = 0;
 137341   while( iStart==iEnd ){
 137342     UChar32 c;
 137344     iStart = ubrk_current(pCsr->pIter);
 137345     iEnd = ubrk_next(pCsr->pIter);
 137346     if( iEnd==UBRK_DONE ){
 137347       return SQLITE_DONE;
 137350     while( iStart<iEnd ){
 137351       int iWhite = iStart;
 137352       U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
 137353       if( u_isspace(c) ){
 137354         iStart = iWhite;
 137355       }else{
 137356         break;
 137359     assert(iStart<=iEnd);
 137362   do {
 137363     UErrorCode status = U_ZERO_ERROR;
 137364     if( nByte ){
 137365       char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
 137366       if( !zNew ){
 137367         return SQLITE_NOMEM;
 137369       pCsr->zBuffer = zNew;
 137370       pCsr->nBuffer = nByte;
 137373     u_strToUTF8(
 137374         pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
 137375         &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
 137376         &status                                  /* Output success/failure */
 137378   } while( nByte>pCsr->nBuffer );
 137380   *ppToken = pCsr->zBuffer;
 137381   *pnBytes = nByte;
 137382   *piStartOffset = pCsr->aOffset[iStart];
 137383   *piEndOffset = pCsr->aOffset[iEnd];
 137384   *piPosition = pCsr->iToken++;
 137386   return SQLITE_OK;
 137390 ** The set of routines that implement the simple tokenizer
 137392 static const sqlite3_tokenizer_module icuTokenizerModule = {
 137393   0,                           /* iVersion */
 137394   icuCreate,                   /* xCreate  */
 137395   icuDestroy,                  /* xCreate  */
 137396   icuOpen,                     /* xOpen    */
 137397   icuClose,                    /* xClose   */
 137398   icuNext,                     /* xNext    */
 137402 ** Set *ppModule to point at the implementation of the ICU tokenizer.
 137404 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
 137405   sqlite3_tokenizer_module const**ppModule
 137407   *ppModule = &icuTokenizerModule;
 137410 #endif /* defined(SQLITE_ENABLE_ICU) */
 137411 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 137413 /************** End of fts3_icu.c ********************************************/

mercurial